package com.bs.cm.controller;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.cm.domain.CmDriver;
import com.bs.cm.service.ICmDriverService;
import javax.annotation.Resource;

/**
 * 司机信息Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "司机信息")
@RestController
@RequestMapping("/cm/driver")
public class CmDriverController extends BaseController {
    @Resource
    private ICmDriverService cmDriverService;

    /**
     * 分页查询司机信息列表
     */
    @ApiOperation("分页查询司机信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmDriver cmDriver) {
        startPage();
        LambdaQueryWrapper<CmDriver> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmDriver);
        List<CmDriver> list = cmDriverService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询司机信息列表
     */
    @ApiOperation("查询司机信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmDriver cmDriver) {
        LambdaQueryWrapper<CmDriver> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmDriver);
        List<CmDriver> list = cmDriverService.list(queryWrapper);
        return success(list);
    }

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

    /**
     * 获取司机信息详细信息
     */
    @ApiOperation("获取司机信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmDriverService.getById(id));
    }

    /**
     * 新增司机信息
     */
    @ApiOperation("新增司机信息")
    @Log(title = "司机信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmDriver cmDriver) {
        return toAjax(cmDriverService.save(cmDriver));
    }

    /**
     * 修改司机信息
     */
    @ApiOperation("修改司机信息")
    @Log(title = "司机信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmDriver cmDriver) {
        return toAjax(cmDriverService.updateById(cmDriver));
    }

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

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


        //ID
        if(Validator.isNotEmpty(cmDriver.getId())){
            queryWrapper.eq(CmDriver::getId,cmDriver.getId());
        }

        //工号
        if(Validator.isNotEmpty(cmDriver.getWorkNumber())){
            queryWrapper.eq(CmDriver::getWorkNumber,cmDriver.getWorkNumber());
        }

        //司机姓名
        if(Validator.isNotEmpty(cmDriver.getDriverName())){
            queryWrapper.eq(CmDriver::getDriverName,cmDriver.getDriverName());
        }

        //出生年月
        if(Validator.isNotEmpty(cmDriver.getBirthdate())){
            queryWrapper.eq(CmDriver::getBirthdate,cmDriver.getBirthdate());
        }

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

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

        //手机号码
        if(Validator.isNotEmpty(cmDriver.getMobileNumber())){
            queryWrapper.eq(CmDriver::getMobileNumber,cmDriver.getMobileNumber());
        }

        //是否生成账号
        if(Validator.isNotEmpty(cmDriver.getIsGenerate())){
            queryWrapper.eq(CmDriver::getIsGenerate,cmDriver.getIsGenerate());
        }

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

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

        //创建部门
        if(Validator.isNotEmpty(cmDriver.getCreateDept())){
            queryWrapper.eq(CmDriver::getCreateDept,cmDriver.getCreateDept());
        }

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

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

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

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

        //租户ID
        if(Validator.isNotEmpty(cmDriver.getTenantId())){
            queryWrapper.eq(CmDriver::getTenantId,cmDriver.getTenantId());
        }

        }

}
