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

/**
 * 工班作业量明细Controller
 *
 * @author bs
 * @date 2025-01-15
 */
@Api(tags = "工班作业量明细")
@RestController
@RequestMapping("/work/workload")
public class TmWorkWorkloadController extends BaseController {
    @Resource
    private ITmWorkWorkloadService tmWorkWorkloadService;

    /**
     * 分页查询工班作业量明细列表
     */
    @ApiOperation("分页查询工班作业量明细列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmWorkWorkload tmWorkWorkload) {
        startPage();
        LambdaQueryWrapper<TmWorkWorkload> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkWorkload);
        List<TmWorkWorkload> list = tmWorkWorkloadService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询工班作业量明细列表
     */
    @ApiOperation("查询工班作业量明细列表")
    @GetMapping("/list")
    public AjaxResult list(TmWorkWorkload tmWorkWorkload) {
        LambdaQueryWrapper<TmWorkWorkload> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkWorkload);
        List<TmWorkWorkload> list = tmWorkWorkloadService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出工班作业量明细列表
     */
    @ApiOperation("导出工班作业量明细列表")
    @Log(title = "工班作业量明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmWorkWorkload tmWorkWorkload) {
        LambdaQueryWrapper<TmWorkWorkload> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkWorkload);
        List<TmWorkWorkload> list = tmWorkWorkloadService.list(queryWrapper);
        ExcelUtil<TmWorkWorkload> util = new ExcelUtil<TmWorkWorkload>(TmWorkWorkload. class);
        util.exportExcel(response, list, "工班作业量明细数据");
    }

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

    /**
     * 新增工班作业量明细
     */
    @ApiOperation("新增工班作业量明细")
    @Log(title = "工班作业量明细新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmWorkWorkload tmWorkWorkload) {
        return toAjax(tmWorkWorkloadService.save(tmWorkWorkload));
    }

    /**
     * 修改工班作业量明细
     */
    @ApiOperation("修改工班作业量明细")
    @Log(title = "工班作业量明细修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmWorkWorkload tmWorkWorkload) {
        return toAjax(tmWorkWorkloadService.updateById(tmWorkWorkload));
    }

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

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


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

        //指令id
        if(Validator.isNotEmpty(tmWorkWorkload.getOrderId())){
            queryWrapper.eq(TmWorkWorkload::getOrderId,tmWorkWorkload.getOrderId());
        }

        //班次id
        if(Validator.isNotEmpty(tmWorkWorkload.getShiftId())){
            queryWrapper.eq(TmWorkWorkload::getShiftId,tmWorkWorkload.getShiftId());
        }

        //作业设备id
        if(Validator.isNotEmpty(tmWorkWorkload.getWorkEquipId())){
            queryWrapper.eq(TmWorkWorkload::getWorkEquipId,tmWorkWorkload.getWorkEquipId());
        }

        //设备类型
        if(Validator.isNotEmpty(tmWorkWorkload.getEquipType())){
            queryWrapper.eq(TmWorkWorkload::getEquipType,tmWorkWorkload.getEquipType());
        }

        //设备id
        if(Validator.isNotEmpty(tmWorkWorkload.getEquipId())){
            queryWrapper.eq(TmWorkWorkload::getEquipId,tmWorkWorkload.getEquipId());
        }

        //设备名称
        if(Validator.isNotEmpty(tmWorkWorkload.getEquipName())){
            queryWrapper.eq(TmWorkWorkload::getEquipName,tmWorkWorkload.getEquipName());
        }

        //司机id
        if(Validator.isNotEmpty(tmWorkWorkload.getDriverId())){
            queryWrapper.eq(TmWorkWorkload::getDriverId,tmWorkWorkload.getDriverId());
        }

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

        //作业内容
        if(Validator.isNotEmpty(tmWorkWorkload.getWorkContent())){
            queryWrapper.eq(TmWorkWorkload::getWorkContent,tmWorkWorkload.getWorkContent());
        }

        //实际作业量
        if(Validator.isNotEmpty(tmWorkWorkload.getActWt())){
            queryWrapper.eq(TmWorkWorkload::getActWt,tmWorkWorkload.getActWt());
        }

        //实际开始日期
        if(Validator.isNotEmpty(tmWorkWorkload.getActStart())){
            queryWrapper.eq(TmWorkWorkload::getActStart,tmWorkWorkload.getActStart());
        }

        //实际结束日期
        if(Validator.isNotEmpty(tmWorkWorkload.getActEnd())){
            queryWrapper.eq(TmWorkWorkload::getActEnd,tmWorkWorkload.getActEnd());
        }

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

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

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

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

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

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

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

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

        }

}
