package com.bs.tm.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.tm.domain.TmVehicle;
import com.bs.tm.service.ITmVehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆信息Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "车辆信息")
@RestController
@RequestMapping("/tm/vehicle")
public class TmVehicleController extends BaseController {
    @Resource
    private ITmVehicleService tmVehicleService;

    /**
     * 分页查询车辆信息列表
     */
    @ApiOperation("分页查询车辆信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmVehicle tmVehicle) {
        startPage();
        QueryWrapper<TmVehicle> queryWrapper = new QueryWrapper();
        condition(queryWrapper, tmVehicle);
        List<TmVehicle> list = tmVehicleService.selectTmVehicleList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询车辆信息列表
     */
    @ApiOperation("查询车辆信息列表")
    @GetMapping("/list")
    public AjaxResult list(TmVehicle tmVehicle) {
        LambdaQueryWrapper<TmVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tmVehicle);
        List<TmVehicle> list = tmVehicleService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出车辆信息列表
     */
    @ApiOperation("导出车辆信息列表")
    @Log(title = "车辆信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmVehicle tmVehicle) {
        LambdaQueryWrapper<TmVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tmVehicle);
        List<TmVehicle> list = tmVehicleService.list(queryWrapper);
        ExcelUtil<TmVehicle> util = new ExcelUtil<TmVehicle>(TmVehicle.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @ApiOperation("获取车辆信息详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(tmVehicleService.selectTmVehicleById(ID));
    }

    /**
     * 新增车辆信息
     */
    @ApiOperation("新增车辆信息")
    @Log(title = "车辆信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmVehicle tmVehicle) {
        return toAjax(tmVehicleService.save(tmVehicle));
    }

    /**
     * 修改车辆信息
     */
    @ApiOperation("修改车辆信息")
    @Log(title = "车辆信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmVehicle tmVehicle) {
        return toAjax(tmVehicleService.updateById(tmVehicle));
    }

    /**
     * 删除车辆信息
     */
    @ApiOperation("删除车辆信息")
    @Log(title = "车辆信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(tmVehicleService.removeBatchByIds(IDs));
    }

    /**
     * 条件设置
     */
    private void condition(LambdaQueryWrapper<TmVehicle> queryWrapper, TmVehicle tmVehicle) {


        //id
        if (Validator.isNotEmpty(tmVehicle.getId())) {
            queryWrapper.eq(TmVehicle::getId, tmVehicle.getId());
        }

        //客户id
        if (Validator.isNotEmpty(tmVehicle.getCustId())) {
            queryWrapper.eq(TmVehicle::getCustId, tmVehicle.getCustId());
        }

        //车源类型
        if (Validator.isNotEmpty(tmVehicle.getVehicleType())) {
            queryWrapper.eq(TmVehicle::getVehicleType, tmVehicle.getVehicleType());
        }

        //车辆结构
        if (Validator.isNotEmpty(tmVehicle.getVehicleStucture())) {
            queryWrapper.eq(TmVehicle::getVehicleStucture, tmVehicle.getVehicleStucture());
        }

        //车型
        if (Validator.isNotEmpty(tmVehicle.getVehicleModel())) {
            queryWrapper.eq(TmVehicle::getVehicleModel, tmVehicle.getVehicleModel());
        }

        //车长
        if (Validator.isNotEmpty(tmVehicle.getVehicleLength())) {
            queryWrapper.eq(TmVehicle::getVehicleLength, tmVehicle.getVehicleLength());
        }

        //载重
        if (Validator.isNotEmpty(tmVehicle.getVehicleLoad())) {
            queryWrapper.eq(TmVehicle::getVehicleLoad, tmVehicle.getVehicleLoad());
        }

        //装载体积
        if (Validator.isNotEmpty(tmVehicle.getVehicleVol())) {
            queryWrapper.eq(TmVehicle::getVehicleVol, tmVehicle.getVehicleVol());
        }

        //品牌型号
        if (Validator.isNotEmpty(tmVehicle.getVehicleBrand())) {
            queryWrapper.like(TmVehicle::getVehicleBrand, tmVehicle.getVehicleBrand());
        }

        //内部编号
        if (Validator.isNotEmpty(tmVehicle.getVehicleNo())) {
            queryWrapper.eq(TmVehicle::getVehicleNo, tmVehicle.getVehicleNo());
        }

        //车牌号码
        if (Validator.isNotEmpty(tmVehicle.getVehicleNumber())) {
            queryWrapper.like(TmVehicle::getVehicleNumber, tmVehicle.getVehicleNumber());
        }

        //套牌号码
        if (Validator.isNotEmpty(tmVehicle.getVehicleFakeNumber())) {
            queryWrapper.eq(TmVehicle::getVehicleFakeNumber, tmVehicle.getVehicleFakeNumber());
        }

        //车架号
        if (Validator.isNotEmpty(tmVehicle.getFrameNumber())) {
            queryWrapper.eq(TmVehicle::getFrameNumber, tmVehicle.getFrameNumber());
        }

        //发动机号
        if (Validator.isNotEmpty(tmVehicle.getEngineNumber())) {
            queryWrapper.eq(TmVehicle::getEngineNumber, tmVehicle.getEngineNumber());
        }

        //行驶证
        if (Validator.isNotEmpty(tmVehicle.getVehicleLicense())) {
            queryWrapper.eq(TmVehicle::getVehicleLicense, tmVehicle.getVehicleLicense());
        }

        //营运证编号
        if (Validator.isNotEmpty(tmVehicle.getOperationNumber())) {
            queryWrapper.eq(TmVehicle::getOperationNumber, tmVehicle.getOperationNumber());
        }

        //默认司机
        if (Validator.isNotEmpty(tmVehicle.getDriverId())) {
            queryWrapper.eq(TmVehicle::getDriverId, tmVehicle.getDriverId());
        }

        //营运证发证日期
        if (Validator.isNotEmpty(tmVehicle.getCertDate())) {
            queryWrapper.eq(TmVehicle::getCertDate, tmVehicle.getCertDate());
        }

        //保险起始日期
        if (Validator.isNotEmpty(tmVehicle.getInsuranceBeginDate())) {
            queryWrapper.eq(TmVehicle::getInsuranceBeginDate, tmVehicle.getInsuranceBeginDate());
        }

        //保险结束日期
        if (Validator.isNotEmpty(tmVehicle.getInsuranceEndDate())) {
            queryWrapper.eq(TmVehicle::getInsuranceEndDate, tmVehicle.getInsuranceEndDate());
        }

        //保险单号
        if (Validator.isNotEmpty(tmVehicle.getInsuranceNo())) {
            queryWrapper.eq(TmVehicle::getInsuranceNo, tmVehicle.getInsuranceNo());
        }

        //gps唯一编号
        if (Validator.isNotEmpty(tmVehicle.getGpsNo())) {
            queryWrapper.eq(TmVehicle::getGpsNo, tmVehicle.getGpsNo());
        }

        //行驶证签发日期
        if (Validator.isNotEmpty(tmVehicle.getDriveBeginDate())) {
            queryWrapper.eq(TmVehicle::getDriveBeginDate, tmVehicle.getDriveBeginDate());
        }

        //行驶证结束日期
        if (Validator.isNotEmpty(tmVehicle.getDriveEndDate())) {
            queryWrapper.eq(TmVehicle::getDriveEndDate, tmVehicle.getDriveEndDate());
        }

        //商险起始日期
        if (Validator.isNotEmpty(tmVehicle.getConsultBeginDate())) {
            queryWrapper.eq(TmVehicle::getConsultBeginDate, tmVehicle.getConsultBeginDate());
        }

        //商险结束日期
        if (Validator.isNotEmpty(tmVehicle.getConsultEndDate())) {
            queryWrapper.eq(TmVehicle::getConsultEndDate, tmVehicle.getConsultEndDate());
        }

        //年检日期
        if (Validator.isNotEmpty(tmVehicle.getAsDate())) {
            queryWrapper.eq(TmVehicle::getAsDate, tmVehicle.getAsDate());
        }

        //年检单号
        if (Validator.isNotEmpty(tmVehicle.getAsNo())) {
            queryWrapper.eq(TmVehicle::getAsNo, tmVehicle.getAsNo());
        }

        //商险单号
        if (Validator.isNotEmpty(tmVehicle.getConsultNo())) {
            queryWrapper.eq(TmVehicle::getConsultNo, tmVehicle.getConsultNo());
        }

        //车辆源类型
        if (Validator.isNotEmpty(tmVehicle.getOptions())) {
            queryWrapper.eq(TmVehicle::getOptions, tmVehicle.getOptions());
        }

        //营运证截止日期
        if (Validator.isNotEmpty(tmVehicle.getCertValid())) {
            queryWrapper.eq(TmVehicle::getCertValid, tmVehicle.getCertValid());
        }

        //是否外借
        if (Validator.isNotEmpty(tmVehicle.getLendOut())) {
            queryWrapper.eq(TmVehicle::getLendOut, tmVehicle.getLendOut());
        }

        //自重
        if (Validator.isNotEmpty(tmVehicle.getWeight())) {
            queryWrapper.eq(TmVehicle::getWeight, tmVehicle.getWeight());
        }

        //车架尺寸
        if (Validator.isNotEmpty(tmVehicle.getFrameSize())) {
            queryWrapper.eq(TmVehicle::getFrameSize, tmVehicle.getFrameSize());
        }

        //行驶证ID
        if (Validator.isNotEmpty(tmVehicle.getVehicleLicenseId())) {
            queryWrapper.eq(TmVehicle::getVehicleLicenseId, tmVehicle.getVehicleLicenseId());
        }

        //营运证ID
        if (Validator.isNotEmpty(tmVehicle.getOperationCertificateId())) {
            queryWrapper.eq(TmVehicle::getOperationCertificateId, tmVehicle.getOperationCertificateId());
        }

        //商业险ID
        if (Validator.isNotEmpty(tmVehicle.getCommercialInsuranceId())) {
            queryWrapper.eq(TmVehicle::getCommercialInsuranceId, tmVehicle.getCommercialInsuranceId());
        }

        //交强险ID
        if (Validator.isNotEmpty(tmVehicle.getCompulsoryTrafficInsuranceId())) {
            queryWrapper.eq(TmVehicle::getCompulsoryTrafficInsuranceId, tmVehicle.getCompulsoryTrafficInsuranceId());
        }

        //产权证ID
        if (Validator.isNotEmpty(tmVehicle.getVehicleOwnershipCertificateId())) {
            queryWrapper.eq(TmVehicle::getVehicleOwnershipCertificateId, tmVehicle.getVehicleOwnershipCertificateId());
        }

        //其他文件ID
        if (Validator.isNotEmpty(tmVehicle.getOtherFileId())) {
            queryWrapper.eq(TmVehicle::getOtherFileId, tmVehicle.getOtherFileId());
        }

        //状态
        if (Validator.isNotEmpty(tmVehicle.getStatus())) {
            queryWrapper.eq(TmVehicle::getStatus, tmVehicle.getStatus());
        }

        //创建人
        if (Validator.isNotEmpty(tmVehicle.getCreateBy())) {
            queryWrapper.eq(TmVehicle::getCreateBy, tmVehicle.getCreateBy());
        }

        //创建时间
        if (Validator.isNotEmpty(tmVehicle.getCreateTime())) {
            queryWrapper.eq(TmVehicle::getCreateTime, tmVehicle.getCreateTime());
        }

        //修改人
        if (Validator.isNotEmpty(tmVehicle.getUpdateBy())) {
            queryWrapper.eq(TmVehicle::getUpdateBy, tmVehicle.getUpdateBy());
        }

        //修改时间
        if (Validator.isNotEmpty(tmVehicle.getUpdateTime())) {
            queryWrapper.eq(TmVehicle::getUpdateTime, tmVehicle.getUpdateTime());
        }

        //删除标志（0代表存在 2代表删除）
        if (Validator.isNotEmpty(tmVehicle.getDelFlag())) {
            queryWrapper.eq(TmVehicle::getDelFlag, tmVehicle.getDelFlag());
        }

        //备注
        if (Validator.isNotEmpty(tmVehicle.getRemark())) {
            queryWrapper.eq(TmVehicle::getRemark, tmVehicle.getRemark());
        }

    }



    private void condition(QueryWrapper<TmVehicle> queryWrapper, TmVehicle tmVehicle) {

        //id
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getId()), "ID", tmVehicle.getId());

        //客户id
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCustId()), "a.CUST_ID", tmVehicle.getCustId());

        //车源类型
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleType()), "VEHICLE_TYPE", tmVehicle.getVehicleType());

        //车辆结构
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleStucture()), "VEHICLE_STUCTURE", tmVehicle.getVehicleStucture());

        //车型
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleModel()), "VEHICLE_MODEL", tmVehicle.getVehicleModel());

        //车长
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleLength()), "VEHICLE_LENGTH", tmVehicle.getVehicleLength());

        //载重
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleLoad()), "VEHICLE_LOAD", tmVehicle.getVehicleLoad());

        //装载体积
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleVol()), "VEHICLE_VOL", tmVehicle.getVehicleVol());

        //品牌型号
        queryWrapper.like(Validator.isNotEmpty(tmVehicle.getVehicleBrand()), "VEHICLE_BRAND", tmVehicle.getVehicleBrand());

        //内部编号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleNo()), "VEHICLE_NO", tmVehicle.getVehicleNo());

        //车牌号码
        queryWrapper.like(Validator.isNotEmpty(tmVehicle.getVehicleNumber()), "VEHICLE_NUMBER", tmVehicle.getVehicleNumber());

        //套牌号码
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleFakeNumber()), "VEHICLE_FAKE_NUMBER", tmVehicle.getVehicleFakeNumber());

        //车架号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getFrameNumber()), "FRAME_NUMBER", tmVehicle.getFrameNumber());

        //发动机号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getEngineNumber()), "ENGINE_NUMBER", tmVehicle.getEngineNumber());

        //行驶证
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleLicense()), "VEHICLE_LICENSE", tmVehicle.getVehicleLicense());

        //营运证编号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getOperationNumber()), "OPERATION_NUMBER", tmVehicle.getOperationNumber());

        //默认司机
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getDriverId()), "DRIVER_ID", tmVehicle.getDriverId());

        //营运证发证日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCertDate()), "CERT_DATE", tmVehicle.getCertDate());

        //保险起始日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getInsuranceBeginDate()), "INSURANCE_BEGIN_DATE", tmVehicle.getInsuranceBeginDate());

        //保险结束日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getInsuranceEndDate()), "INSURANCE_END_DATE", tmVehicle.getInsuranceEndDate());

        //保险单号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getInsuranceNo()), "INSURANCE_NO", tmVehicle.getInsuranceNo());

        //gps唯一编号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getGpsNo()), "GPS_NO", tmVehicle.getGpsNo());

        //行驶证签发日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getDriveBeginDate()), "DRIVE_BEGIN_DATE", tmVehicle.getDriveBeginDate());

        //行驶证结束日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getDriveEndDate()), "DRIVE_END_DATE", tmVehicle.getDriveEndDate());

        //商险起始日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getConsultBeginDate()), "CONSULT_BEGIN_DATE", tmVehicle.getConsultBeginDate());

        //商险结束日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getConsultEndDate()), "CONSULT_END_DATE", tmVehicle.getConsultEndDate());

        //年检日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getAsDate()), "AS_DATE", tmVehicle.getAsDate());

        //年检单号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getAsNo()), "AS_NO", tmVehicle.getAsNo());

        //商险单号
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getConsultNo()), "CONSULT_NO", tmVehicle.getConsultNo());

        //车辆源类型
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getOptions()), "OPTIONS", tmVehicle.getOptions());

        //营运证截止日期
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCertValid()), "CERT_VALID", tmVehicle.getCertValid());

        //是否外借
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getLendOut()), "LEND_OUT", tmVehicle.getLendOut());

        //自重
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getWeight()), "WEIGHT", tmVehicle.getWeight());

        //车架尺寸
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getFrameSize()), "FRAME_SIZE", tmVehicle.getFrameSize());

        //行驶证ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleLicenseId()), "VEHICLE_LICENSE_ID", tmVehicle.getVehicleLicenseId());

        //营运证ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getOperationCertificateId()), "OPERATION_CERTIFICATE_ID", tmVehicle.getOperationCertificateId());

        //商业险ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCommercialInsuranceId()), "COMMERCIAL_INSURANCE_ID", tmVehicle.getCommercialInsuranceId());

        //交强险ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCompulsoryTrafficInsuranceId()), "COMPULSORY_TRAFFIC_INSURANCE_ID", tmVehicle.getCompulsoryTrafficInsuranceId());

        //产权证ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getVehicleOwnershipCertificateId()), "VEHICLE_OWNERSHIP_CERTIFICATE_ID", tmVehicle.getVehicleOwnershipCertificateId());

        //其他文件ID
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getOtherFileId()), "OTHER_FILE_ID", tmVehicle.getOtherFileId());

        //状态
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getStatus()), "STATUS", tmVehicle.getStatus());

        //创建人
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCreateBy()), "CREATE_BY", tmVehicle.getCreateBy());

        //创建时间
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getCreateTime()), "CREATE_TIME", tmVehicle.getCreateTime());

        //修改人
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getUpdateBy()), "UPDATE_BY", tmVehicle.getUpdateBy());

        //修改时间
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getUpdateTime()), "UPDATE_TIME", tmVehicle.getUpdateTime());

        //删除标志（0代表存在 2代表删除）
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getDelFlag()), "DEL_FLAG", tmVehicle.getDelFlag());

        //备注
        queryWrapper.eq(Validator.isNotEmpty(tmVehicle.getRemark()), "REMARK", tmVehicle.getRemark());

    }


}
