package com.sqx.modules.car.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.car.entity.Car;
import com.sqx.modules.car.service.CarService;
import com.sqx.modules.driver.entity.Driver;
import com.sqx.modules.driver.service.DriverService;
import com.sqx.modules.emergencyContact.entity.EmergencyContact;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "车辆管理", tags = {"车辆管理"})
@RequestMapping("/app/car")
public class AppCarController {

    @Autowired
    private CarService carService;
    @Resource
    private DriverService driverService;

    @Login
    @GetMapping("/selectCarList")
    @ApiOperation("查询用户车辆")
    public Result selectCarList(@RequestAttribute("userId")Long userId){
        return Result.success().put("data",carService.list(new QueryWrapper<Car>().eq("user_id",userId)));
    }

    @Login
    @PostMapping("/insertCar")
    @ApiOperation("新增车辆")
    public Result insertCar(@RequestAttribute("userId")Long userId, Car car){
        car.setUserId(userId);
        car.setStatus(1);
        carService.insert(car);
        return Result.success();
    }

    @GetMapping("/selectCarById")
    @ApiOperation("根据ID查询车辆")
    public Result selectCarById(Long carId){
        return Result.success().put("data",carService.getOne(new QueryWrapper<Car>().eq("car_id",carId)));
    }

    @PostMapping("/updateCar")
    @ApiOperation("修改车辆信息")
    public Result updateEmergency(Car car){
        return carService.updateCar(car);
    }

    @PostMapping("/deleteCar")
    @ApiOperation("删除车辆信息")
    public Result deleteCar(Long carId){
        return carService.deleteCar(carId);
    }

    @Login
    @PostMapping("/updateChecked")
    @ApiOperation("修改车辆选中")
    public Result updateChecked(@RequestAttribute("userId")Long userId, Car car){
        if (car.getStatus() != 2){
            return Result.error(999,"未审核通过车辆禁止切换");
        }
        carService.updateCar(car);
        Driver driver = driverService.selectDriverByUserId(userId);
        driver.setDriverColor(car.getCarColor());
        driver.setDriverPlate(car.getCarPlate());
        driver.setDriverBrand(car.getCarBrand());
//        driver.setStatus(car.getStatus());
        driverService.updateDriver(driver);
        List<Car> carList = carService.list(new QueryWrapper<Car>().eq("user_id", userId));
        if (carList.size() == 1){
            return Result.success();
        }else if (carList.size() > 1){
            for (Car c : carList){
                if (c.getCarId() != car.getCarId()){
                    c.setChecked(0);
                    carService.updateCar(c);
                }
            }
        }
        return Result.success();
    }

}
