【停车场车辆管理系统】从零搭建——UserController搭建

【停车场车辆管理系统】从零搭建——项目分析

【停车场车辆管理系统】从零搭建——数据库搭建

【停车场车辆管理系统】从零搭建——后端搭建

【停车场车辆管理系统】从零搭建——后端Model类

【停车场车辆管理系统】从零搭建——Mapper搭建

【停车场车辆管理系统】从零搭建——AdminController搭建

【停车场车辆管理系统】从零搭建——UserController搭建

【停车场车辆管理系统】从零搭建——前端react搭建

文章目录

  • Controller结构
  • 登录
  • 注册
  • 个人信息
    • 展示个人信息
    • 修改个人信息
    • 车辆管理
  • 车位信息
  • 费用信息
  • 承包申请
  • 完整代码

上一篇完成了AdminController的搭建,这一篇来讲讲UserController,这是后端部分的最后一篇。

再来回顾下功能:

管理员模块

  1. 个人信息模块:
    查找数据库中个人信息展示,修改按钮=》修改所有个人信息包括密码。个人车辆信息,修改车辆信息
  2. 车位信息:
    表格形式展示自己所管理的小区的所有车位,录入车位、修改车位信息、删除车位
  3. 车位承包信息:
    表格形式展示自己所管理小区的所有【车位承包申请表】,这个模块需要车主发起申请才会有数据,管理员可以选择同意或拒绝。临时停车信息就不用发起申请了
  4. 停车收费信息:
    表格形式展示自己所管理小区的车位停车情况,录入停车订单这块我考虑订单直接在承包申请同意之后自动生成

车主

  1. 个人信息:展示个人信息,修改所有信息包括密码
  2. 车位占用:查看所有未被占用车位,发起承包或停车
  3. 停车费用管理:查看自己的订单,选择缴费

Controller结构

和上一篇一样,先把controller的结构以及自动装配写好:

@Controller
@RequestMapping("user")
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    CarMapper carMapper;
    @Autowired
    CityMapper cityMapper;
    @Autowired
    ParkingOrderMapper parkingOrderMapper;
    @Autowired
    ParkMapper parkMapper;
    @Autowired
    RentApplicationMapper rentApplicationMapper;
    @Autowired
    UserMapper userMapper;
}

然后我们开始写方法

登录

		/**
     * 登录
     *
     * @param user
     * @return
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public User userLogin(@RequestBody User user) {
        return userMapper.selectFromUserByUserPhoneAndUserPassWord(user.getUserPhone(), user.getUserPassword());
    }

注册

注册的时候需要调用insert方法,由于注册时需要选择小区,所以还需要搜索一下小区列表。

    /**
     * 注册
     * 
     * @param user
     */
    @RequestMapping("userRegister")
    @ResponseBody
    public void userRegister(@RequestBody User user) {
        userMapper.insertIntoUser(user);
    }
    
     /**
     * 显示所有小区
     */
    @RequestMapping("selectCity")
    @ResponseBody
    public List<City> selectCity() {
        return cityMapper.selectAllFromCity();
    }

个人信息

展示个人信息

    /**
     * 个人信息展示
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyInfo")
    @ResponseBody
    public User selectMyInfo(@RequestBody User user) {
        return userMapper.selectFromUserByUserId(user.getUserId());
    }

由于小区属性在user表中是以id存储的,所以还需要再搜索一下小区名。

    /**
     * 显示小区名
     * 
     * @param cityId
     * @return
     */
    @RequestMapping("selectCityName")
    @ResponseBody
    public String selectCityName(@RequestParam Integer cityId) {
        return cityMapper.selectCtyNameByCityId(cityId);
    }

修改个人信息

    /**
     * 修改个人信息
     *
     * @param user
     */
    @RequestMapping("updateMyInfo")
    @ResponseBody
    public void updateMyInfo(@RequestBody User user) {
        userMapper.updateUserByUserId(user);
    }

车辆管理

    /**
     * 新增车辆
     * 
     * @param car
     */
    @RequestMapping("addCar")
    @ResponseBody
    public void addCar(@RequestBody Car car) {
        carMapper.insertIntoCar(car);
    }

    /**
     * 查看车辆信息
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyCar")
    @ResponseBody
    public List<Car> selectMyCar(@RequestBody User user) {
        return carMapper.selectFromCarByUserId(user.getUserId());
    }

    /**
     * 修改车辆信息
     *
     * @param car
     */
    @RequestMapping("updateMyCar")
    @ResponseBody
    public void updataMyCar(@RequestBody Car car) {
        carMapper.updateCarByCarId(car);
    }

    /**
     * 删除车辆
     * @param car
     */
    @RequestMapping("deleteMyCar")
    @ResponseBody
    public void deleteMyCar(@RequestBody Car car) {
        carMapper.deleteCar(car.getCarId());
    }

车位信息

首先我们需要看看有哪些车位是空闲的

    /**
     * 查看空闲车位
     *
     * @param user
     * @return
     */
    @RequestMapping("selectEmptyPark")
    @ResponseBody
    public List<Park> selectEmptyPark(@RequestBody User user) {
        return parkMapper.selectFromParkByCityIdAndParkState(user.getCityId(), 0);
    }

然后我们要看看自己有哪些车还没有停进车位

    /**
     * 查看没有停进车位的车辆
     * 
     * @param user
     * @return
     */
    @RequestMapping("selectEmptyCar")
    @ResponseBody
    public List<Car> selectEmptyCar(@RequestBody User user) {
        return carMapper.selectCarByCarIdNotInParkByCityId(user.getCityId());
    }

下面我们就可以对空闲的车位进行【承包】或者【停车】的操作了。

    /**
     * 提交承包申请
     *
     * @param rentApplication
     */
    @RequestMapping("submitRentApplication")
    @ResponseBody
    public void submitRentApplication(@RequestBody RentApplication rentApplication) {
        rentApplicationMapper.insertIntoRentApplication(rentApplication);
    }

    /**
     * 停车
     *
     * @param parkingOrder 停车的车位状态被占用
     */
    @RequestMapping("submitParking")
    @ResponseBody
    public void submitParking(@RequestBody ParkingOrder parkingOrder) {
        parkingOrderMapper.insertIntoParkingOrder(parkingOrder);
        parkMapper.updateParkSetParkStateAndCarIdByParkId(2, parkingOrder.getCarId(), parkingOrder.getParkId());

    }

费用信息

和AdminController中一样,需要用一个ParkingOrderUse类来封装数据。

    /**
     * 查看个人订单
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyOrder")
    @ResponseBody
    public List<ParkingOrderUse> selectMyOrder(@RequestBody User user) {
        List<ParkingOrder> parkingOrderList = parkingOrderMapper.selectFromParkingOrderByUserIdOrderByParkingOrderIdDesc(user.getUserId());
        List<ParkingOrderUse> parkingOrderUses = new ArrayList<>();
        for (ParkingOrder parkingOrder : parkingOrderList) {
            List<String> strings = selectCarNumberAndParkNumber(parkingOrder.getCarId(), parkingOrder.getParkId());
            ParkingOrderUse parkingOrderUse = new ParkingOrderUse(parkingOrder, strings);
            parkingOrderUses.add(parkingOrderUse);
        }
        return parkingOrderUses;
    }
        /**
     * 搜索车牌、停车位、所属人
     * @param carId
     * @param parkId
     * @return
     */
    @RequestMapping("selectCarNumberAndParkNumber")
    @ResponseBody
    public List<String> selectCarNumberAndParkNumber(Integer carId, Integer parkId) {
        List<String> res = new ArrayList<>();
        res.add(carMapper.selectCarNumberFromCarByCarId(carId));
        res.add(parkMapper.selectFromParkByParkId(parkId).getParkNumber());
        return res;
    }

由于停车订单中有一些信息是随着时间发生变化的,所以要对其进行更新

    /**
     * 更新停车订单
     * 
     * @throws ParseException
     */
    @RequestMapping("updateParkingOrder")
    @ResponseBody
    public void updateParkingOrder() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String expirationTime = simpleDateFormat.format(date);
        List<ParkingOrder> parkingOrderList = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(1, 0);
        for (ParkingOrder parkingOrder : parkingOrderList) {
            Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
            String parkingTime = parkingOrder.getParkingTime();
            Date pt = simpleDateFormat.parse(parkingTime);
            Date et = simpleDateFormat.parse(expirationTime);
            long ptt = pt.getTime();
            long ett = et.getTime();
            Double price = (ett - ptt) / (1000 * 60 * 60) * park.getParkingPrice();
            parkingOrder.setExpirationTime(expirationTime);
            parkingOrder.setParkingOrderPrice(price);
            parkingOrderMapper.updateParkingOrder(parkingOrder);
        }
        List<ParkingOrder> parkingOrderList1 = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(0, 0);
        for (ParkingOrder parkingOrder : parkingOrderList1) {
            String et = parkingOrder.getExpirationTime();
            if (date.getTime() > simpleDateFormat.parse(et).getTime()) {
                Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
                park.setCarId(0);
                park.setParkState(0);
                parkMapper.updateParkByParkId(park);

            }
        }
        List<ParkingOrder> parkingOrderList2 = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(0, 1);
        for (ParkingOrder parkingOrder : parkingOrderList2) {
            String et = parkingOrder.getExpirationTime();
            if (date.getTime() > simpleDateFormat.parse(et).getTime()) {
                Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
                park.setCarId(0);
                park.setParkState(0);
                parkMapper.updateParkByParkId(park);
            }
        }

    }

当车离开停车位时需要进行支付,承包订单也需要进行缴费

    /**
     * 支付订单
     * 改变订单的支付状态
     * 停车订单支付表示车辆已经离开,
     * 修改状态
     *
     * @param parkingOrder 
     */
    @RequestMapping("pay")
    @ResponseBody
    public void pay(@RequestBody ParkingOrder parkingOrder) {
        if (parkingOrder.getParkingOrderState() == 0) {
            parkingOrderMapper.updateParkingOrderSetPayStateByParkingOrderId(1, parkingOrder.getParkingOrderId());
        } else {
            parkingOrder.setPayState(1);
            parkingOrderMapper.updateParkingOrder(parkingOrder);
            parkMapper.updateParkSetParkStateAndCarIdByParkId(0, 0, parkingOrder.getParkId());
        }
    }

承包申请

用户可以查看自己的申请是否被审批。

    /**
     * 查看我的申请
     * 
     * @param user
     * @return
     */
    @RequestMapping("selectMyRentApplication")
    @ResponseBody
    public List<RentApplicationUse> selectMyRentApplication(@RequestBody User user) {
        System.out.println(user.getUserId());
        List<RentApplication> rentApplicationList = rentApplicationMapper.selectFromApplicationByUserId(user.getUserId());
        List<RentApplicationUse> rentApplicationUseList = new ArrayList<>();
        for (RentApplication rentApplication : rentApplicationList) {
            String car = carMapper.selectCarNumberFromCarByCarId(rentApplication.getCarId());
            String park = parkMapper.selectFromParkByParkId(rentApplication.getParkId()).getParkNumber();
            String userName = user.getUserName();
            List<String> strings = new ArrayList<>();
            strings.add(car);
            strings.add(park);
            strings.add(userName);
            RentApplicationUse rentApplicationUse = new RentApplicationUse(rentApplication, strings);
            rentApplicationUseList.add(rentApplicationUse);
        }
        return rentApplicationUseList;
    }

完整代码

后端的代码基本上就这些,下一篇开始讲前端了,这里贴出UserController的完整代码

package com.example.vehiclemanagement.controller;

import com.example.vehiclemanagement.mapper.*;
import com.example.vehiclemanagement.model.*;
import com.example.vehiclemanagement.model.utilModel.ParkingOrderUse;
import com.example.vehiclemanagement.model.utilModel.RentApplicationUse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("user")
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    CarMapper carMapper;
    @Autowired
    CityMapper cityMapper;
    @Autowired
    ParkingOrderMapper parkingOrderMapper;
    @Autowired
    ParkMapper parkMapper;
    @Autowired
    RentApplicationMapper rentApplicationMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public User userLogin(@RequestBody User user) {
        return userMapper.selectFromUserByUserPhoneAndUserPassWord(user.getUserPhone(), user.getUserPassword());
    }

    /**
     * 注册
     * 
     * @param user
     */
    @RequestMapping("userRegister")
    @ResponseBody
    public void userRegister(@RequestBody User user) {
        userMapper.insertIntoUser(user);
    }

    /**
     * 显示所有小区
     */
    @RequestMapping("selectCity")
    @ResponseBody
    public List<City> selectCity() {
        return cityMapper.selectAllFromCity();
    }

    /**
     * 显示小区名
     * 
     * @param cityId
     * @return
     */
    @RequestMapping("selectCityName")
    @ResponseBody
    public String selectCityName(@RequestParam Integer cityId) {
        return cityMapper.selectCtyNameByCityId(cityId);
    }

    /**
     * 个人信息展示
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyInfo")
    @ResponseBody
    public User selectMyInfo(@RequestBody User user) {
        return userMapper.selectFromUserByUserId(user.getUserId());
    }

    /**
     * 修改个人信息
     *
     * @param user
     */
    @RequestMapping("updateMyInfo")
    @ResponseBody
    public void updateMyInfo(@RequestBody User user) {
        userMapper.updateUserByUserId(user);
    }

    /**
     * 新增车辆
     * 
     * @param car
     */
    @RequestMapping("addCar")
    @ResponseBody
    public void addCar(@RequestBody Car car) {
        carMapper.insertIntoCar(car);
    }

    /**
     * 查看车辆信息
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyCar")
    @ResponseBody
    public List<Car> selectMyCar(@RequestBody User user) {
        return carMapper.selectFromCarByUserId(user.getUserId());
    }

    /**
     * 修改车辆信息
     *
     * @param car
     */
    @RequestMapping("updateMyCar")
    @ResponseBody
    public void updataMyCar(@RequestBody Car car) {
        carMapper.updateCarByCarId(car);
    }

    /**
     * 删除车辆
     * @param car
     */
    @RequestMapping("deleteMyCar")
    @ResponseBody
    public void deleteMyCar(@RequestBody Car car) {
        carMapper.deleteCar(car.getCarId());
    }

    /**
     * 查看空闲车位
     *
     * @param user
     * @return
     */
    @RequestMapping("selectEmptyPark")
    @ResponseBody
    public List<Park> selectEmptyPark(@RequestBody User user) {
        return parkMapper.selectFromParkByCityIdAndParkState(user.getCityId(), 0);
    }

    /**
     * 查看没有停进车位的车辆
     * 
     * @param user
     * @return
     */
    @RequestMapping("selectEmptyCar")
    @ResponseBody
    public List<Car> selectEmptyCar(@RequestBody User user) {
        return carMapper.selectCarByCarIdNotInParkByCityId(user.getCityId());
    }

    /**
     * 提交承包申请
     *
     * @param rentApplication
     */
    @RequestMapping("submitRentApplication")
    @ResponseBody
    public void submitRentApplication(@RequestBody RentApplication rentApplication) {
        rentApplicationMapper.insertIntoRentApplication(rentApplication);
    }

    /**
     * 停车
     *
     * @param parkingOrder 停车的车位状态被占用
     */
    @RequestMapping("submitParking")
    @ResponseBody
    public void submitParking(@RequestBody ParkingOrder parkingOrder) {
        parkingOrderMapper.insertIntoParkingOrder(parkingOrder);
        parkMapper.updateParkSetParkStateAndCarIdByParkId(2, parkingOrder.getCarId(), parkingOrder.getParkId());

    }

    /**
     * 查看个人订单
     *
     * @param user
     * @return
     */
    @RequestMapping("selectMyOrder")
    @ResponseBody
    public List<ParkingOrderUse> selectMyOrder(@RequestBody User user) {
        List<ParkingOrder> parkingOrderList = parkingOrderMapper.selectFromParkingOrderByUserIdOrderByParkingOrderIdDesc(user.getUserId());
        List<ParkingOrderUse> parkingOrderUses = new ArrayList<>();
        for (ParkingOrder parkingOrder : parkingOrderList) {
            List<String> strings = selectCarNumberAndParkNumber(parkingOrder.getCarId(), parkingOrder.getParkId());
            ParkingOrderUse parkingOrderUse = new ParkingOrderUse(parkingOrder, strings);
            parkingOrderUses.add(parkingOrderUse);
        }
        return parkingOrderUses;
    }

    /**
     * 搜索车牌、停车位、所属人
     * @param carId
     * @param parkId
     * @return
     */
    @RequestMapping("selectCarNumberAndParkNumber")
    @ResponseBody
    public List<String> selectCarNumberAndParkNumber(Integer carId, Integer parkId) {
        List<String> res = new ArrayList<>();
        res.add(carMapper.selectCarNumberFromCarByCarId(carId));
        res.add(parkMapper.selectFromParkByParkId(parkId).getParkNumber());
        return res;
    }

    /**
     * 更新停车订单
     * 
     * @throws ParseException
     */
    @RequestMapping("updateParkingOrder")
    @ResponseBody
    public void updateParkingOrder() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String expirationTime = simpleDateFormat.format(date);
        List<ParkingOrder> parkingOrderList = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(1, 0);
        for (ParkingOrder parkingOrder : parkingOrderList) {
            Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
            String parkingTime = parkingOrder.getParkingTime();
            Date pt = simpleDateFormat.parse(parkingTime);
            Date et = simpleDateFormat.parse(expirationTime);
            long ptt = pt.getTime();
            long ett = et.getTime();
            Double price = (ett - ptt) / (1000 * 60 * 60) * park.getParkingPrice();
            parkingOrder.setExpirationTime(expirationTime);
            parkingOrder.setParkingOrderPrice(price);
            parkingOrderMapper.updateParkingOrder(parkingOrder);
        }
        List<ParkingOrder> parkingOrderList1 = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(0, 0);
        for (ParkingOrder parkingOrder : parkingOrderList1) {
            String et = parkingOrder.getExpirationTime();
            if (date.getTime() > simpleDateFormat.parse(et).getTime()) {
                Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
                park.setCarId(0);
                park.setParkState(0);
                parkMapper.updateParkByParkId(park);

            }
        }
        List<ParkingOrder> parkingOrderList2 = parkingOrderMapper.selectFromParkingOrderByParkingOrderStateAndPayState(0, 1);
        for (ParkingOrder parkingOrder : parkingOrderList2) {
            String et = parkingOrder.getExpirationTime();
            if (date.getTime() > simpleDateFormat.parse(et).getTime()) {
                Park park = parkMapper.selectFromParkByParkId(parkingOrder.getParkId());
                park.setCarId(0);
                park.setParkState(0);
                parkMapper.updateParkByParkId(park);
            }
        }

    }

    /**
     * 支付订单
     * 改变订单的支付状态
     * 停车订单支付表示车辆已经离开,
     * 修改状态
     *
     * @param parkingOrder 
     */
    @RequestMapping("pay")
    @ResponseBody
    public void pay(@RequestBody ParkingOrder parkingOrder) {
        if (parkingOrder.getParkingOrderState() == 0) {
            parkingOrderMapper.updateParkingOrderSetPayStateByParkingOrderId(1, parkingOrder.getParkingOrderId());
        } else {
            parkingOrder.setPayState(1);
            parkingOrderMapper.updateParkingOrder(parkingOrder);
            parkMapper.updateParkSetParkStateAndCarIdByParkId(0, 0, parkingOrder.getParkId());
        }
    }

    /**
     * 查看我的申请
     * 
     * @param user
     * @return
     */
    @RequestMapping("selectMyRentApplication")
    @ResponseBody
    public List<RentApplicationUse> selectMyRentApplication(@RequestBody User user) {
        System.out.println(user.getUserId());
        List<RentApplication> rentApplicationList = rentApplicationMapper.selectFromApplicationByUserId(user.getUserId());
        List<RentApplicationUse> rentApplicationUseList = new ArrayList<>();
        for (RentApplication rentApplication : rentApplicationList) {
            String car = carMapper.selectCarNumberFromCarByCarId(rentApplication.getCarId());
            String park = parkMapper.selectFromParkByParkId(rentApplication.getParkId()).getParkNumber();
            String userName = user.getUserName();
            List<String> strings = new ArrayList<>();
            strings.add(car);
            strings.add(park);
            strings.add(userName);
            RentApplicationUse rentApplicationUse = new RentApplicationUse(rentApplication, strings);
            rentApplicationUseList.add(rentApplicationUse);
        }
        return rentApplicationUseList;
    }

}

你可能感兴趣的:(java,mysql,reactjs,spring,boot)