package com.yc.ship.module.resource.controller.admin.car;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.car.vo.*;
import com.yc.ship.module.resource.controller.admin.driver.vo.DriverSimpleRespVO;
import com.yc.ship.module.resource.dal.dataobject.car.CarDO;
import com.yc.ship.module.resource.dal.dataobject.cartype.CarTypeDO;
import com.yc.ship.module.resource.dal.dataobject.company.CompanyDO;
import com.yc.ship.module.resource.dal.dataobject.driver.DriverDO;
import com.yc.ship.module.resource.service.car.CarService;
import com.yc.ship.module.resource.service.cartype.CarTypeService;
import com.yc.ship.module.resource.service.company.CompanyService;
import com.yc.ship.module.resource.service.driver.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 车辆管理")
@RestController
@RequestMapping("/resource/car")
@Validated
public class CarController {

    @Resource
    private CarService carService;

    @Resource
    private DriverService driverService;

    @Resource
    private CompanyService companyService;

    @Resource
    private CarTypeService carTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆管理")
    @PreAuthorize("@ss.hasPermission('dispatch:car:create')")
    public CommonResult<String> createCar(@Valid @RequestBody CarSaveReqVO createReqVO) {
        return success(carService.createCar(createReqVO));
    }

    @PostMapping("/creates")
    @Operation(summary = "导入车辆信息")
    @PreAuthorize("@ss.hasPermission('dispatch:car:create')")
    public CommonResult<Boolean> createCars(@Valid @RequestBody List<CarSaveReqVO> dataList) {
        dataList.forEach(createReqVO -> {
            CarReqVO reqVO = new CarReqVO();
            reqVO.setCarNo(createReqVO.getCarNo());
            List<CarDO> carList = carService.getCarList(reqVO);
            if (null == carList || carList.isEmpty()) {
                if (StringUtils.isNotBlank(createReqVO.getDriverName())) {
                    DriverSimpleRespVO respVO = new DriverSimpleRespVO();
                    respVO.setName(createReqVO.getDriverName());
                    List<DriverDO> driverList = driverService.getDriverList(respVO);
                    if (null != driverList && !driverList.isEmpty()) {
                        DriverDO driverDO = driverList.get(0);
                        createReqVO.setDriverId(driverDO.getId());
                    }
                }
                carService.createCar(createReqVO);
            }
        });
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆管理")
    @PreAuthorize("@ss.hasPermission('dispatch:car:update')")
    public CommonResult<Boolean> updateCar(@Valid @RequestBody CarSaveReqVO updateReqVO) {
        carService.updateCar(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:car:delete')")
    public CommonResult<Boolean> deleteCar(@RequestParam("id") String id) {
        carService.deleteCar(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:car:query')")
    public CommonResult<CarRespVO> getCar(@RequestParam("id") String id) {
        CarDO car = carService.getCar(id);
        CarRespVO carRespVO = BeanUtils.toBean(car, CarRespVO.class);
        DriverDO driverDO = driverService.getDriver(car.getDriverId());
        if (driverDO != null) {
            carRespVO.setDriverName(driverDO.getName());
        }
        return success(carRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆管理分页")
//    @PreAuthorize("@ss.hasPermission('dispatch:car:query')")
    public CommonResult<PageResult<CarListRespVO>> getCarPage(@Valid CarPageReqVO pageReqVO) {
        PageResult<CarDO> pageResult = carService.getCarPage(pageReqVO);
        PageResult<CarListRespVO> list = BeanUtils.toBean(pageResult, CarListRespVO.class);
        for (CarListRespVO carListRespVO : list.getList()) {
            DriverDO driver = driverService.getDriver(carListRespVO.getDriverId());
            if (driver != null) {
                carListRespVO.setDriverName(driver.getName());
                carListRespVO.setPhone(driver.getPhone());
            }
            CompanyDO company = companyService.getCompany(carListRespVO.getCompanyId());
            if (company != null) {
                carListRespVO.setCompanyName(StringUtils.isEmpty(company.getShortName()) ? company.getCompanyName() : company.getShortName());
            }
            if (carListRespVO.getCarTypeId() != null) {
                CarTypeDO carTypeDO = carTypeService.getCarType(carListRespVO.getCarTypeId());
                if (null != carTypeDO) {
                    carListRespVO.setCarTypeName(carTypeDO.getName());
                }
            }
        }
        //list.getList().sort(Comparator.comparing(CarListRespVO::getSort));
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆管理 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:car:export')")
    @OperateLog(type = EXPORT)
    public void exportCarExcel(@Valid CarPageReqVO pageReqVO,
                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarDO> list = carService.getCarPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆管理.xls", "数据", CarRespVO.class,
                BeanUtils.toBean(list, CarRespVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
//    @PreAuthorize("@ss.hasPermission('dispatch:car:query')")
    @Operation(summary = "获取精简信息列表")
    public CommonResult<List<CarOptionListVO>> getSimpleList(@Valid CarPageReqVO pageReqVO) {
        List<CarDO> simpleCarList = carService.getSimpleCarList(pageReqVO);
        return success(BeanUtils.toBean(simpleCarList, CarOptionListVO.class));
    }
}