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

/**
 * 港区作业指令Controller
 *
 * @author bs
 * @date 2025-01-15
 */
@Api(tags = "港区作业指令")
@RestController
@RequestMapping("/work/order")
public class TmWorkOrderController extends BaseController {
    @Resource
    private ITmWorkOrderService tmWorkOrderService;

    /**
     * 分页查询港区作业指令列表
     */
    @ApiOperation("分页查询港区作业指令列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmWorkOrder tmWorkOrder) {
        startPage();
        LambdaQueryWrapper<TmWorkOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkOrder);
        List<TmWorkOrder> list = tmWorkOrderService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询港区作业指令列表
     */
    @ApiOperation("查询港区作业指令列表")
    @GetMapping("/list")
    public AjaxResult list(TmWorkOrder tmWorkOrder) {
        LambdaQueryWrapper<TmWorkOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkOrder);
        List<TmWorkOrder> list = tmWorkOrderService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出港区作业指令列表
     */
    @ApiOperation("导出港区作业指令列表")
    @Log(title = "港区作业指令导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmWorkOrder tmWorkOrder) {
        LambdaQueryWrapper<TmWorkOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmWorkOrder);
        List<TmWorkOrder> list = tmWorkOrderService.list(queryWrapper);
        ExcelUtil<TmWorkOrder> util = new ExcelUtil<TmWorkOrder>(TmWorkOrder. class);
        util.exportExcel(response, list, "港区作业指令数据");
    }

    /**
     * 获取港区作业指令详细信息
     */
    @ApiOperation("获取港区作业指令详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tmWorkOrderService.getById(id));
    }

    /**
     * 新增港区作业指令
     */
    @ApiOperation("新增港区作业指令")
    @Log(title = "港区作业指令新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmWorkOrder tmWorkOrder) {
        return toAjax(tmWorkOrderService.save(tmWorkOrder));
    }

    /**
     * 修改港区作业指令
     */
    @ApiOperation("修改港区作业指令")
    @Log(title = "港区作业指令修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmWorkOrder tmWorkOrder) {
        return toAjax(tmWorkOrderService.updateById(tmWorkOrder));
    }

    /**
     * 删除港区作业指令
     */
    @ApiOperation("删除港区作业指令")
    @Log(title = "港区作业指令删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(tmWorkOrderService.removeBatchByIds(ids));
    }

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


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

        //作业类型
        if(Validator.isNotEmpty(tmWorkOrder.getWorkType())){
            queryWrapper.eq(TmWorkOrder::getWorkType,tmWorkOrder.getWorkType());
        }

        //作业港区
        if(Validator.isNotEmpty(tmWorkOrder.getWorkArea())){
            queryWrapper.eq(TmWorkOrder::getWorkArea,tmWorkOrder.getWorkArea());
        }

        //作业标题
        if(Validator.isNotEmpty(tmWorkOrder.getWorkTitle())){
            queryWrapper.eq(TmWorkOrder::getWorkTitle,tmWorkOrder.getWorkTitle());
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        }

}
