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.TmVehicleWorkload;
import com.bs.tm.service.ITmVehicleWorkloadService;
import javax.annotation.Resource;

/**
 * 车辆作业量Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "车辆作业量")
@RestController
@RequestMapping("/tm/vehicle/workload")
public class TmVehicleWorkloadController extends BaseController {
    @Resource
    private ITmVehicleWorkloadService tmVehicleWorkloadService;

    /**
     * 分页查询车辆作业量列表
     */
    @ApiOperation("分页查询车辆作业量列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmVehicleWorkload tmVehicleWorkload) {
        startPage();
        LambdaQueryWrapper<TmVehicleWorkload> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmVehicleWorkload);
        List<TmVehicleWorkload> list = tmVehicleWorkloadService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询车辆作业量列表
     */
    @ApiOperation("查询车辆作业量列表")
    @GetMapping("/list")
    public AjaxResult list(TmVehicleWorkload tmVehicleWorkload) {
        LambdaQueryWrapper<TmVehicleWorkload> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmVehicleWorkload);
        List<TmVehicleWorkload> list = tmVehicleWorkloadService.list(queryWrapper);
        return success(list);
    }

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

    /**
     * 获取车辆作业量详细信息
     */
    @ApiOperation("获取车辆作业量详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tmVehicleWorkloadService.getById(id));
    }

    /**
     * 新增车辆作业量
     */
    @ApiOperation("新增车辆作业量")
    @Log(title = "车辆作业量新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmVehicleWorkload tmVehicleWorkload) {
        return toAjax(tmVehicleWorkloadService.save(tmVehicleWorkload));
    }

    /**
     * 修改车辆作业量
     */
    @ApiOperation("修改车辆作业量")
    @Log(title = "车辆作业量修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmVehicleWorkload tmVehicleWorkload) {
        return toAjax(tmVehicleWorkloadService.updateById(tmVehicleWorkload));
    }

    /**
     * 删除车辆作业量
     */
    @ApiOperation("删除车辆作业量")
    @Log(title = "车辆作业量删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(tmVehicleWorkloadService.removeBatchByIds(ids));
    }

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


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

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

        //车牌号
        if(Validator.isNotEmpty(tmVehicleWorkload.getVehicleNo())){
            queryWrapper.eq(TmVehicleWorkload::getVehicleNo,tmVehicleWorkload.getVehicleNo());
        }

        //车辆类型
        if(Validator.isNotEmpty(tmVehicleWorkload.getVehicleType())){
            queryWrapper.eq(TmVehicleWorkload::getVehicleType,tmVehicleWorkload.getVehicleType());
        }

        //趟次开始时间
        if(Validator.isNotEmpty(tmVehicleWorkload.getStartTime())){
            queryWrapper.eq(TmVehicleWorkload::getStartTime,tmVehicleWorkload.getStartTime());
        }

        //趟次结束时间
        if(Validator.isNotEmpty(tmVehicleWorkload.getFinishTime())){
            queryWrapper.eq(TmVehicleWorkload::getFinishTime,tmVehicleWorkload.getFinishTime());
        }

        //趟次作业量
        if(Validator.isNotEmpty(tmVehicleWorkload.getTripWorknum())){
            queryWrapper.eq(TmVehicleWorkload::getTripWorknum,tmVehicleWorkload.getTripWorknum());
        }

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

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

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

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

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

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

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

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

        }

}
