package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesReworkTaskService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesReworkTaskModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesReworkTaskRequestModel;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionAssembly;
import cn.estsh.i3plus.pojo.mes.bean.rework.MesReworkTask;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 返工单
 * @Author: zxw
 * @Date: 2024/5/25 18:16
 * @Modify:
 */
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/reworkTask")
public class MesReworkTaskController {

    @Autowired
    private IMesReworkTaskService mesReworkTaskService;

    @GetMapping("/query-pager")
    @ApiOperation(value = "查询所有返工单")
    public ResultBean queryReworkTaskByPager(MesReworkTask reworkTask, Pager pager) {
        try {
            reworkTask.setOrganizeCode(!StringUtils.isEmpty(reworkTask.getOrganizeCode())?reworkTask.getOrganizeCode():AuthUtil.getOrganizeCode());
            ListPager<MesReworkTask> mesReworkTaskListPager = mesReworkTaskService.queryReworkTask(reworkTask, pager);
            return ResultBean.success("查询成功").setListPager(mesReworkTaskListPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/queryBySn")
    @ApiOperation(value = "根据条码查询返工详情")
    public ResultBean queryBySn(MesReworkTaskRequestModel requestModel) {
        try {
            requestModel.setOrganizeCode(!StringUtils.isEmpty(requestModel.getOrganizeCode())?requestModel.getOrganizeCode():AuthUtil.getOrganizeCode());
            MesReworkTaskModel mesReworkTaskModel = mesReworkTaskService.queryReworkTaskModel(requestModel);
            return ResultBean.success("查询成功").setResultObject(mesReworkTaskModel);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/reworkRecord")
    @ApiOperation(value = "返工")
    public ResultBean reworkRecord(@RequestBody MesReworkTaskRequestModel mesReworkTaskRequestModel) {
        try {
            String userName = mesReworkTaskRequestModel.getUserName();
            String organizeCode = !StringUtils.isEmpty(mesReworkTaskRequestModel.getOrganizeCode())?mesReworkTaskRequestModel.getOrganizeCode():AuthUtil.getOrganizeCode();
            for (long inspectionDetailId : mesReworkTaskRequestModel.getIds()) {
                mesReworkTaskService.reworkRecord(inspectionDetailId,userName,organizeCode);
            }
            return ResultBean.success("返工成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/assemblyQuery")
    @ApiOperation(value = "装配件查询")
    public ResultBean assemblyQuery(MesReworkTaskRequestModel requestModel) {
        // 数据校验
        ValidatorBean.checkNotNull(requestModel.getCustSn(), "客户条码不能为空");
        /*ValidatorBean.checkNotNull(requestModel.getPartNo(), "零件号不能为空");*/
        try {
            List<MesProductionAssembly> mesProductionRecords = mesReworkTaskService.assemblyQuery(requestModel);
            return ResultBean.success("返工成功").setResultObject(mesProductionRecords);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/queryAssemblyTree")
    @ApiOperation(value = "装配件树查询")
    public ResultBean queryAssemblyTree(@RequestBody MesReworkTaskRequestModel requestModel) {
        try {
            ValidatorBean.checkNotNull(requestModel.getCustSn(), "客户条码不能为空");
            return ResultBean.success("查询成功").setResultList(mesReworkTaskService.queryAssemblyTree(requestModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/assemblySnRepeat")
    @ApiOperation(value = "装配件替换")
    public ResultBean assemblySnRepeat(MesReworkTaskRequestModel requestModel) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(requestModel.getCustSn(), "客户条码不能为空");
            ValidatorBean.checkNotNull(requestModel.getAssemblyIds(), "替换装配件id不能为空");
            ValidatorBean.checkNotNull(requestModel.getSn(), "替换条码不能为空");
            ValidatorBean.checkNotNull(requestModel.getDetailIds(), "缺陷位置id不能为空");

            //mesReworkTaskService.validateSn(requestModel);
            mesReworkTaskService.doAssemblySnRepeat(requestModel);
            return ResultBean.success("返工成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/assemblySnRepeat/byScan")
    @ApiOperation(value = "扫描条码替换装配件")
    public ResultBean doAssemblySnRepeatByScan(MesReworkTaskRequestModel requestModel) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(requestModel.getCustSn(), "客户条码不能为空");
            ValidatorBean.checkNotNull(requestModel.getSn(), "替换条码不能为空");
            ValidatorBean.checkNotNull(requestModel.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(requestModel.getDetailIds(), "缺陷位置id不能为空");

            mesReworkTaskService.doAssemblySnRepeatByScan(requestModel);
            return ResultBean.success("替换成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/reworkSuccess")
    @ApiOperation(value = "返工成功")
    public ResultBean doReworkSuccess(@RequestBody MesReworkTaskRequestModel requestModel) {
        // 数据校验
        //ValidatorBean.checkNotNull(requestModel.getSn(), "条码不能为空");
        try {
            mesReworkTaskService.doReworkSuccess(requestModel);
            return ResultBean.success("返工成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/sort-order-dismantle/do")
    @ApiOperation(value = "排序拆解")
    public ResultBean doSortOrderDismantle(@RequestBody MesReworkTaskRequestModel requestModel) {
        try {
            ValidatorBean.checkNotNull(requestModel.getReworkOrder(), "返工单不能为空");
            ValidatorBean.checkNotNull(requestModel.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(requestModel.getUserName(), "操作人不能为空");
            mesReworkTaskService.doSortOrderDismantle(requestModel);
            return ResultBean.success("排序拆解成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/doPrint")
    @ApiOperation(value = "打印")
    public ResultBean doPrint(MesReworkTaskRequestModel requestModel) {
        // 数据校验
        ValidatorBean.checkNotNull(requestModel.getSn(), "条码不能为空");
        ValidatorBean.checkNotNull(requestModel.getOrganizeCode(), "工厂代码不能为空");
        ValidatorBean.checkNotNull(requestModel.getUserName(), "操作人不能为空");
        try {
            return ResultBean.success("打印成功").setResultObject(mesReworkTaskService.doPrint(requestModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
