package com.bs.tm.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.tm.domain.TmDriverSchedule;
import com.bs.tm.service.ITmDriverScheduleService;
import javax.annotation.Resource;

/**
 * 司机驾驶车辆Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "司机驾驶车辆")
@RestController
@RequestMapping("/tm/driver/schedule")
public class TmDriverScheduleController extends BaseController {
    @Resource
    private ITmDriverScheduleService tmDriverScheduleService;

    /**
     * 分页查询司机驾驶车辆列表
     */
    @ApiOperation("分页查询司机驾驶车辆列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmDriverSchedule tmDriverSchedule) {
        startPage();
        LambdaQueryWrapper<TmDriverSchedule> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmDriverSchedule);
        List<TmDriverSchedule> list = tmDriverScheduleService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询司机驾驶车辆列表
     */
    @ApiOperation("查询司机驾驶车辆列表")
    @GetMapping("/list")
    public AjaxResult list(TmDriverSchedule tmDriverSchedule) {
        LambdaQueryWrapper<TmDriverSchedule> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmDriverSchedule);
        List<TmDriverSchedule> list = tmDriverScheduleService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出司机驾驶车辆列表
     */
    @ApiOperation("导出司机驾驶车辆列表")
    @Log(title = "司机驾驶车辆导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmDriverSchedule tmDriverSchedule) {
        LambdaQueryWrapper<TmDriverSchedule> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmDriverSchedule);
        List<TmDriverSchedule> list = tmDriverScheduleService.list(queryWrapper);
        ExcelUtil<TmDriverSchedule> util = new ExcelUtil<TmDriverSchedule>(TmDriverSchedule. class);
        util.exportExcel(response, list, "司机驾驶车辆数据");
    }

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

    /**
     * 新增司机驾驶车辆
     */
    @ApiOperation("新增司机驾驶车辆")
    @Log(title = "司机驾驶车辆新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmDriverSchedule tmDriverSchedule) {
        return toAjax(tmDriverScheduleService.save(tmDriverSchedule));
    }

    /**
     * 修改司机驾驶车辆
     */
    @ApiOperation("修改司机驾驶车辆")
    @Log(title = "司机驾驶车辆修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmDriverSchedule tmDriverSchedule) {
        return toAjax(tmDriverScheduleService.updateById(tmDriverSchedule));
    }

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

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


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

        //车辆ID
        if(Validator.isNotEmpty(tmDriverSchedule.getVehicleId())){
            queryWrapper.eq(TmDriverSchedule::getVehicleId,tmDriverSchedule.getVehicleId());
        }

        //司机ID
        if(Validator.isNotEmpty(tmDriverSchedule.getDrvierId())){
            queryWrapper.eq(TmDriverSchedule::getDrvierId,tmDriverSchedule.getDrvierId());
        }

        //开始日期
        if(Validator.isNotEmpty(tmDriverSchedule.getStartDate())){
            queryWrapper.eq(TmDriverSchedule::getStartDate,tmDriverSchedule.getStartDate());
        }

        //结束日期
        if(Validator.isNotEmpty(tmDriverSchedule.getFinishDate())){
            queryWrapper.eq(TmDriverSchedule::getFinishDate,tmDriverSchedule.getFinishDate());
        }

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

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

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

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

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

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

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

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

        }

}
