package cn.iocoder.yudao.module.custom.controller.admin;

import cn.iocoder.yudao.module.custom.entity.CarMain;
import cn.iocoder.yudao.module.custom.service.CarMainService;
import cn.iocoder.yudao.module.custom.service.UserCarService;
import cn.iocoder.yudao.module.custom.vo.CarMainReqVO;
import cn.iocoder.yudao.module.custom.vo.CarMainRespVO;
import cn.iocoder.yudao.module.custom.vo.CustomerMainSaveReqVO;
import cn.iocoder.yudao.module.label.service.BusiLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.servlet.http.*;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.common.BaseConstants.TABLE_BASE_CAR_MAIN;
import static cn.iocoder.yudao.common.BaseConstants.TABLE_BASE_CUSTOMER_MAIN;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 车辆信息")
@RestController
@RequestMapping("/base/carMain")
@Validated
public class CarMainController {

    @Autowired
    private CarMainService carMainService;

    @Autowired
    private BusiLabelService busiLabelService;

    @Resource
    private UserCarService userCarService;

    /**
     * 创建车辆信息
     *
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建车辆信息")
    @PreAuthorize("@ss.hasPermission('base:car-main:create')")
    public CommonResult<String> createCarMain(@RequestBody CarMainReqVO createReqVO) {
        return carMainService.createCarMain(createReqVO);
    }

    /**
     * 更新车辆信息
     *
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新车辆信息")
    @PreAuthorize("@ss.hasPermission('base:car-main:update')")
    public CommonResult<String> updateCarMain(@RequestBody CarMainReqVO updateReqVO) {
        return carMainService.updateCarMain(updateReqVO);
    }

    /**
     * 删除车辆信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:car-main:delete')")
    public CommonResult<Boolean> deleteCarMain(@RequestParam("id") String id) {
        carMainService.deleteCarMain(id);
        return success(true);
    }

    /**
     * 获得车辆信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得车辆信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:car-main:query')")
    public CommonResult<CarMainRespVO> getCarMain(@RequestParam("id") String id) {
        CarMain carMain = carMainService.getCarMain(id);
        return success(BeanUtils.toBean(carMain, CarMainRespVO.class));
    }

    /**
     * 获得车辆信息分页
     *
     * @param pageReqVO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "获得车辆信息分页")
    @PreAuthorize("@ss.hasPermission('base:car-main:query')")
    public CommonResult<IPage<CarMainRespVO>> getCarMainPage(CarMainReqVO pageReqVO) {
        IPage<CarMainRespVO> pageResult = carMainService.getCarMainPage(pageReqVO);
        return success(pageResult);
    }

    /**
     * 保险/年检/保养提醒列表
     *
     * @param pageReqVO CarMainReqVO（入参）
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < cn.iocoder.yudao.module.custom.vo.CarMainRespVO>>
     * @author PQZ
     * @date 11:01 2024/8/14
     **/
    @GetMapping("/remindCarMainPage")
    @Operation(summary = "获得车辆信息分页")
    public CommonResult<IPage<CarMainRespVO>> remindCarMainPage(CarMainReqVO pageReqVO) {
        IPage<CarMainRespVO> pageResult = carMainService.remindCarMainPage(pageReqVO);
        return success(pageResult);
    }


    /**
     * 测算车辆下一次保养/检测/保险时间
     *
     * @param pageReqVO CarMainReqVO
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.custom.vo.CarMainRespVO>
     * @author PQZ
     * @date 11:48 2024/8/13
     **/
    @PostMapping("/compute")
    @Operation(summary = "测算车辆下一次保养/检测/保险时间")
    public CommonResult<CarMainRespVO> compute(@RequestBody CarMainReqVO pageReqVO) {
        CarMainRespVO result = carMainService.compute(pageReqVO);
        return success(result);
    }


    /**
     * 导出车辆信息
     *
     * @param pageReqVO
     * @param response
     * @throws IOException
     */
    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:car-main:customer')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCarMainExcel(CarMainReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarMainRespVO> list = carMainService.getCarMainPage(pageReqVO).getRecords();
        // 导出 Excel
        ExcelUtils.write(response, "车辆信息.xls", "数据", CarMainRespVO.class,
                BeanUtils.toBean(list, CarMainRespVO.class));
    }

    /**
     * 绑定用户
     *
     * @param saveReqVO CustomerMainSaveReqVO
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     **/
    @PostMapping("/bindCustomerCar")
    @Operation(summary = "绑定用户")
    @PreAuthorize("@ss.hasPermission('base:car-main:bindCustomer')")
    public CommonResult<Boolean> bindCustomerCar(@RequestBody CarMainReqVO saveReqVO) {
        carMainService.bindCustomAndCar(saveReqVO);
        return success(true);
    }

    /**
     * 设置标签
     *
     * @param saveReqVO CustomerMainSaveReqVO
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 16:35 2024/8/6
     **/
    @PostMapping("/setLabel")
    @Operation(summary = "设置标签")
    public CommonResult<Boolean> setLabel(@RequestBody CarMainReqVO saveReqVO) {
        busiLabelService.saveBusiLable(saveReqVO.getId(), TABLE_BASE_CAR_MAIN, saveReqVO.getLabelList());
        return success(true);
    }

    /**
     * 根据车牌查单条记录
     *
     * @author 小李
     * @date 11:26 2024/10/8
     * @param carMain 根据车牌查单条记录
    **/
    @PostMapping("/getByLicenseNumber")
    @Operation(summary = "根据车牌查单条记录")
    public CommonResult<?> getByLicenseNumber(@RequestBody CarMain carMain) {
        CarMain one = carMainService.getOne(new LambdaQueryWrapper<CarMain>().eq(CarMain::getLicenseNumber, carMain.getLicenseNumber()));
        return success(one);
    }

    /**
     * OCR识别
     * @author vinjor-M
     * @date 16:58 2024/10/11
     * @param imagePath 公网图片地址
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
     **/
    @PostMapping("/vehicleLicenseOCR")
    public CommonResult<?> vehicleLicenseOCR(@RequestBody String imagePath) throws Exception {
        return CommonResult.success(userCarService.vehicleLicenseOCR(imagePath));
    }
}