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

/**
 * 工班设备作业Controller
 *
 * @author bs
 * @date 2025-01-15
 */
@Api(tags = "工班设备作业")
@RestController
@RequestMapping("/work/equip")
public class TmWorkEquipController extends BaseController {
    @Resource
    private ITmWorkEquipService tmWorkEquipService;

    /**
     * 分页查询工班设备作业列表
     */
    @ApiOperation("分页查询工班设备作业列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmWorkEquip tmWorkEquip) {
        startPage();
        LambdaQueryWrapper<TmWorkEquip> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkEquip);
        List<TmWorkEquip> list = tmWorkEquipService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询工班设备作业列表
     */
    @ApiOperation("查询工班设备作业列表")
    @GetMapping("/list")
    public AjaxResult list(TmWorkEquip tmWorkEquip) {
        LambdaQueryWrapper<TmWorkEquip> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkEquip);
        List<TmWorkEquip> list = tmWorkEquipService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 新增工班设备作业
     */
    @ApiOperation("新增工班设备作业")
    @Log(title = "工班设备作业新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmWorkEquip tmWorkEquip) {
        return toAjax(tmWorkEquipService.save(tmWorkEquip));
    }

    /**
     * 修改工班设备作业
     */
    @ApiOperation("修改工班设备作业")
    @Log(title = "工班设备作业修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmWorkEquip tmWorkEquip) {
        return toAjax(tmWorkEquipService.updateById(tmWorkEquip));
    }

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        }

}
