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

/**
 * 工班作业调度Controller
 *
 * @author bs
 * @date 2025-01-15
 */
@Api(tags = "工班作业调度")
@RestController
@RequestMapping("/work/shift")
public class TmWorkShiftController extends BaseController {
    @Resource
    private ITmWorkShiftService tmWorkShiftService;

    /**
     * 分页查询工班作业调度列表
     */
    @ApiOperation("分页查询工班作业调度列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmWorkShift tmWorkShift) {
        startPage();
        LambdaQueryWrapper<TmWorkShift> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkShift);
        List<TmWorkShift> list = tmWorkShiftService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询工班作业调度列表
     */
    @ApiOperation("查询工班作业调度列表")
    @GetMapping("/list")
    public AjaxResult list(TmWorkShift tmWorkShift) {
        LambdaQueryWrapper<TmWorkShift> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkShift);
        List<TmWorkShift> list = tmWorkShiftService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 新增工班作业调度
     */
    @ApiOperation("新增工班作业调度")
    @Log(title = "工班作业调度新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmWorkShift tmWorkShift) {
        return toAjax(tmWorkShiftService.save(tmWorkShift));
    }

    /**
     * 修改工班作业调度
     */
    @ApiOperation("修改工班作业调度")
    @Log(title = "工班作业调度修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmWorkShift tmWorkShift) {
        return toAjax(tmWorkShiftService.updateById(tmWorkShift));
    }

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

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


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

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

        //班次名称
        if(Validator.isNotEmpty(tmWorkShift.getShiftName())){
            queryWrapper.eq(TmWorkShift::getShiftName,tmWorkShift.getShiftName());
        }

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

        //计划作业量
        if(Validator.isNotEmpty(tmWorkShift.getPlanWt())){
            queryWrapper.eq(TmWorkShift::getPlanWt,tmWorkShift.getPlanWt());
        }

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

        //计划开始日期
        if(Validator.isNotEmpty(tmWorkShift.getPlanStart())){
            queryWrapper.eq(TmWorkShift::getPlanStart,tmWorkShift.getPlanStart());
        }

        //计划结束日期
        if(Validator.isNotEmpty(tmWorkShift.getPlanEnd())){
            queryWrapper.eq(TmWorkShift::getPlanEnd,tmWorkShift.getPlanEnd());
        }

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

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

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

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

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

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

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

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

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

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

        }

}
