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

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.driver.vo.*;
import com.yc.ship.module.resource.dal.dataobject.driver.DriverDO;
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.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/driver")
@Validated
public class DriverController {

    @Resource
    private DriverService driverService;

    /**
     * @description: 创建驾驶员时，同时会在系统中创建同名的用户
     * @author yxq
     * @date: 2025/8/13 16:27
     * @Param createReqVO:
     * @return: com.zshl.system.framework.common.pojo.CommonResult<java.lang.String>
     */
    @PostMapping("/create")
    @Operation(summary = "创建驾驶员管理")
    @PreAuthorize("@ss.hasPermission('dispatch:driver:create')")
    public CommonResult<String> createDriver(@Valid @RequestBody DriverSaveReqVO createReqVO) {
        driverService.createDriver(createReqVO);
        return success(null);
    }

    @PostMapping("/creates")
    @Operation(summary = "导入驾驶员管理")
    @PreAuthorize("@ss.hasPermission('dispatch:driver:create')")
    public CommonResult<String> createDrivers(@Valid @RequestBody List<DriverSaveReqVO> dataList) {
        dataList.forEach(driverSaveReqVO -> driverService.createDriver(driverSaveReqVO));
        return success(null);
    }

    @PutMapping("/update")
    @Operation(summary = "更新驾驶员管理")
    @PreAuthorize("@ss.hasPermission('dispatch:driver:update')")
    public CommonResult<Boolean> updateDriver(@Valid @RequestBody DriverSaveReqVO updateReqVO) {
        driverService.updateDriver(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾驶员管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:driver:delete')")
    public CommonResult<Boolean> deleteDriver(@RequestParam("id") String id) {
        driverService.deleteDriver(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得驾驶员管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:driver:query')")
    public CommonResult<DriverRespVO> getDriver(@RequestParam("id") String id) {
        DriverDO driver = driverService.getDriver(id);
        return success(BeanUtils.toBean(driver, DriverRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得驾驶员管理分页")
//    @PreAuthorize("@ss.hasPermission('dispatch:driver:query')")
    public CommonResult<PageResult<DriverRespVO>> getDriverPage(@Valid DriverPageReqVO pageReqVO) {
        PageResult<DriverDO> pageResult = driverService.getDriverPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DriverRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出驾驶员管理 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:driver:export')")
    @OperateLog(type = EXPORT)
    public void exportDriverExcel(@Valid DriverPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DriverDO> list = driverService.getDriverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "驾驶员管理.xls", "数据", DriverRespVO.class,
                BeanUtils.toBean(list, DriverRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取驾驶员精简信息列表", description = "列表数据")
    public CommonResult<List<DriverSimpleRespVO>> getSimpleDriverList() {
        List<DriverDO> list = driverService.getSimpleCarList();
        return success(BeanUtils.toBean(list, DriverSimpleRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple-by-carType", "/simple-list"})
    @Operation(summary = "根据车型获取车辆默认驾驶员精简信息列表", description = "列表数据")
    public CommonResult<List<DriverSimpleRespVO>> getSimpleDriverListByCarType(@RequestParam("carType") String carType) {
        List<DriverDO> list = driverService.getSimpleDriverListByCarType(carType);
        return success(BeanUtils.toBean(list, DriverSimpleRespVO.class));
    }

    /**
     * @description: 获取驾驶员的默认车辆列表
     * @author yxq
     * @date: 2025/8/26 12:15
     * @Param null:
     * @return: null
     */
    @GetMapping("/driver-car-list")
    @Operation(summary = "获取驾驶员默认车辆列表")
    public CommonResult<List<DriverRespVO>> getDriverDefaultCarList(DriverReqVO reqVO) {
        return success(driverService.getDriverDefaultCarList(reqVO));
    }

}