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.TmDriver;
import com.bs.tm.service.ITmDriverService;
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.Arrays;
import java.util.List;

/**
 * 驾驶员信息Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "驾驶员信息")
@RestController
@RequestMapping("/tm/driver")
public class TmDriverController extends BaseController {
    @Resource
    private ITmDriverService tmDriverService;

    /**
     * 分页查询驾驶员信息列表
     */
    @ApiOperation("分页查询驾驶员信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmDriver tmDriver) {
        startPage();
        QueryWrapper<TmDriver> queryWrapper = new QueryWrapper();
        condition(queryWrapper, tmDriver);
        List<TmDriver> list = tmDriverService.selectDriverList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询驾驶员信息列表
     */
    @ApiOperation("查询驾驶员信息列表")
    @GetMapping("/list")
    public AjaxResult list(TmDriver tmDriver) {
        LambdaQueryWrapper<TmDriver> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tmDriver);
        List<TmDriver> list = tmDriverService.list(queryWrapper);
        return success(list);
    }

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

    /**
     * 获取驾驶员信息详细信息
     */
    @ApiOperation("获取驾驶员信息详细信息")
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Long  driverId) {
        return success(tmDriverService.selectDriverById(driverId));
    }

    /**
     * 新增驾驶员信息
     */
    @ApiOperation("新增驾驶员信息")
    @Log(title = "驾驶员信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmDriver tmDriver) {
        return toAjax(tmDriverService.save(tmDriver));
    }

    /**
     * 修改驾驶员信息
     */
    @ApiOperation("修改驾驶员信息")
    @Log(title = "驾驶员信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmDriver tmDriver) {
        return toAjax(tmDriverService.updateById(tmDriver));
    }

    /**
     * 删除驾驶员信息
     */
    @ApiOperation("删除驾驶员信息")
    @Log(title = "驾驶员信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Long[] driverIds) {
        return toAjax(tmDriverService.removeBatchByIds(Arrays.asList(driverIds)));
    }

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


        //驾驶员ID
        if (Validator.isNotEmpty(tmDriver.getDriverId())) {
            queryWrapper.eq(TmDriver::getDriverId, tmDriver.getDriverId());
        }

        //客户代码
        if (Validator.isNotEmpty(tmDriver.getCustCode())) {
            queryWrapper.eq(TmDriver::getCustCode, tmDriver.getCustCode());
        }

        //驾驶员姓名
        if (Validator.isNotEmpty(tmDriver.getDriverName())) {
            queryWrapper.like(TmDriver::getDriverName, tmDriver.getDriverName());
        }

        //性别
        if (Validator.isNotEmpty(tmDriver.getSex())) {
            queryWrapper.eq(TmDriver::getSex, tmDriver.getSex());
        }

        //出生年月
        if (Validator.isNotEmpty(tmDriver.getBirthDate())) {
            queryWrapper.eq(TmDriver::getBirthDate, tmDriver.getBirthDate());
        }

        //手机号码
        if (Validator.isNotEmpty(tmDriver.getMobileNumber())) {
            queryWrapper.like(TmDriver::getMobileNumber, tmDriver.getMobileNumber());
        }

        //驾驶证类型
        if (Validator.isNotEmpty(tmDriver.getLicenseType())) {
            queryWrapper.eq(TmDriver::getLicenseType, tmDriver.getLicenseType());
        }

        //驾驶证号
        if (Validator.isNotEmpty(tmDriver.getLicenseNo())) {
            queryWrapper.eq(TmDriver::getLicenseNo, tmDriver.getLicenseNo());
        }

        //身份证号
        if (Validator.isNotEmpty(tmDriver.getIdNumber())) {
            queryWrapper.eq(TmDriver::getIdNumber, tmDriver.getIdNumber());
        }

        //有效期起
        if (Validator.isNotEmpty(tmDriver.getValidBegin())) {
            queryWrapper.eq(TmDriver::getValidBegin, tmDriver.getValidBegin());
        }

        //有效期止
        if (Validator.isNotEmpty(tmDriver.getValidEnd())) {
            queryWrapper.eq(TmDriver::getValidEnd, tmDriver.getValidEnd());
        }

        //下一次年检时间
        if (Validator.isNotEmpty(tmDriver.getNextinspecTime())) {
            queryWrapper.eq(TmDriver::getNextinspecTime, tmDriver.getNextinspecTime());
        }

        //准驾车型
        if (Validator.isNotEmpty(tmDriver.getDriveModel())) {
            queryWrapper.eq(TmDriver::getDriveModel, tmDriver.getDriveModel());
        }

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

        //创建人
        if (Validator.isNotEmpty(tmDriver.getCreateUser())) {
            queryWrapper.eq(TmDriver::getCreateUser, tmDriver.getCreateUser());
        }

        //创建时间
        if (Validator.isNotEmpty(tmDriver.getCreateDate())) {
            queryWrapper.eq(TmDriver::getCreateDate, tmDriver.getCreateDate());
        }

        //修改人
        if (Validator.isNotEmpty(tmDriver.getModifyUser())) {
            queryWrapper.eq(TmDriver::getModifyUser, tmDriver.getModifyUser());
        }

        //修改时间
        if (Validator.isNotEmpty(tmDriver.getModifyDate())) {
            queryWrapper.eq(TmDriver::getModifyDate, tmDriver.getModifyDate());
        }

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

        //所属车队
        if (Validator.isNotEmpty(tmDriver.getMotorcade())) {
            queryWrapper.eq(TmDriver::getMotorcade, tmDriver.getMotorcade());
        }

        //默认车头号
        if (Validator.isNotEmpty(tmDriver.getCarNo())) {
            queryWrapper.eq(TmDriver::getCarNo, tmDriver.getCarNo());
        }

        //默认车架号
        if (Validator.isNotEmpty(tmDriver.getTruckNo())) {
            queryWrapper.eq(TmDriver::getTruckNo, tmDriver.getTruckNo());
        }

        //司机类型
        if (Validator.isNotEmpty(tmDriver.getDriverType())) {
            queryWrapper.eq(TmDriver::getDriverType, tmDriver.getDriverType());
        }

        //司机属性
        if (Validator.isNotEmpty(tmDriver.getDriverAttribute())) {
            queryWrapper.eq(TmDriver::getDriverAttribute, tmDriver.getDriverAttribute());
        }

        //拼音码
        if (Validator.isNotEmpty(tmDriver.getPinyinCode())) {
            queryWrapper.eq(TmDriver::getPinyinCode, tmDriver.getPinyinCode());
        }

        //入职日期
        if (Validator.isNotEmpty(tmDriver.getEntryDate())) {
            queryWrapper.eq(TmDriver::getEntryDate, tmDriver.getEntryDate());
        }

        //离职日期
        if (Validator.isNotEmpty(tmDriver.getRepartureDate())) {
            queryWrapper.eq(TmDriver::getRepartureDate, tmDriver.getRepartureDate());
        }

        //离职状态
        if (Validator.isNotEmpty(tmDriver.getRepartureStatus())) {
            queryWrapper.eq(TmDriver::getRepartureStatus, tmDriver.getRepartureStatus());
        }

        //从业证书ID
        if (Validator.isNotEmpty(tmDriver.getProfessionalCertificateId())) {
            queryWrapper.eq(TmDriver::getProfessionalCertificateId, tmDriver.getProfessionalCertificateId());
        }

        //驾驶证书ID
        if (Validator.isNotEmpty(tmDriver.getDrivingCertificateId())) {
            queryWrapper.eq(TmDriver::getDrivingCertificateId, tmDriver.getDrivingCertificateId());
        }

        //身份证ID
        if (Validator.isNotEmpty(tmDriver.getIdentityCardId())) {
            queryWrapper.eq(TmDriver::getIdentityCardId, tmDriver.getIdentityCardId());
        }

    }


    /**
     * 条件设置
     */
    private void condition(QueryWrapper<TmDriver> queryWrapper, TmDriver tmDriver) {

        //驾驶员ID
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDriverId()), "DRIVER_ID", tmDriver.getDriverId());
        queryWrapper.eq("a.del_flag", "0");

        //客户代码
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getCustCode()), "CUST_CODE", tmDriver.getCustCode());

        //驾驶员姓名
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDriverName()), "DRIVER_NAME", tmDriver.getDriverName());

        //性别
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getSex()), "SEX", tmDriver.getSex());

        //出生年月
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getBirthDate()), "BIRTH_DATE", tmDriver.getBirthDate());

        //手机号码
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getMobileNumber()), "MOBILE_NUMBER", tmDriver.getMobileNumber());

        //驾驶证类型
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getLicenseType()), "LICENSE_TYPE", tmDriver.getLicenseType());

        //驾驶证号
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getLicenseNo()), "LICENSE_NO", tmDriver.getLicenseNo());

        //身份证号
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getIdNumber()), "ID_NUMBER", tmDriver.getIdNumber());

        //有效期起
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getValidBegin()), "VALID_BEGIN", tmDriver.getValidBegin());

        //有效期止
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getValidEnd()), "VALID_END", tmDriver.getValidEnd());

        //下一次年检时间
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getNextinspecTime()), "NEXTINSPECT_TIME", tmDriver.getNextinspecTime());

        //准驾车型
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDriveModel()), "DRIVE_MODEL", tmDriver.getDriveModel());

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

        //创建人
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getCreateUser()), "CREATE_USER", tmDriver.getCreateUser());

        //创建时间
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getCreateDate()), "CREATE_DATE", tmDriver.getCreateDate());

        //修改人
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getModifyUser()), "MODIFY_USER", tmDriver.getModifyUser());

        //修改时间
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getModifyDate()), "MODIFY_DATE", tmDriver.getModifyDate());

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

        //所属车队
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getMotorcade()), "MOTORCADE", tmDriver.getMotorcade());

        //默认车头号
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getCarNo()), "CAR_NO", tmDriver.getCarNo());

        //默认车架号
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getTruckNo()), "TRUCK_NO", tmDriver.getTruckNo());

        //司机类型
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDriverType()), "DRIVER_TYPE", tmDriver.getDriverType());

        //司机属性
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDriverAttribute()), "DRIVER_ATTRIBUTE", tmDriver.getDriverAttribute());

        //拼音码
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getPinyinCode()), "PINYIN_CODE", tmDriver.getPinyinCode());

        //入职日期
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getEntryDate()), "ENTRY_DATE", tmDriver.getEntryDate());

        //离职日期
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getRepartureDate()), "REPARTURE_DATE", tmDriver.getRepartureDate());

        //离职状态
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getRepartureStatus()), "REPARTURE_STATUS", tmDriver.getRepartureStatus());

        //从业证书ID
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getProfessionalCertificateId()), "PROFESSIONAL_CERTIFICATE_ID", tmDriver.getProfessionalCertificateId());

        //驾驶证书ID
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getDrivingCertificateId()), "DRIVING_CERTIFICATE_ID", tmDriver.getDrivingCertificateId());

        //身份证ID
        queryWrapper.eq(Validator.isNotEmpty(tmDriver.getIdentityCardId()), "IDENTITY_CARD_ID", tmDriver.getIdentityCardId());

    }



}
