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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesFengRenBatchReportService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.step.method.IMesPackageNoGenerateStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesWorkOrderExtModel;
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 com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("缝纫批量汇报")
@Slf4j
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN)
public class MesFengRenBatchReportController {

    @Autowired
    private IMesPackageNoGenerateStepService packageNoGenerateStepService;

    @Autowired
    private IMesFengRenBatchReportService fengRenBatchReportService;

    @GetMapping("/feng-ren-batch-report/get-pack-spec-qty")
    @ApiOperation(value = "缝纫批量汇报获取默认标包")
    public ResultBean queryPackSpecQtyFengRenBatchReport(String organizeCode, String partNo) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            ValidatorBean.checkNotNull(partNo, "零件编码不能为空");
            return ResultBean.success("查询成功").setResultObject(fengRenBatchReportService.queryPackSpecQtyFengRenBatchReport(organizeCode, partNo));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/feng-ren-batch-report/do")
    @ApiOperation(value = "缝纫批量汇报")
    public ResultBean doFengRenBatchReport(MesWorkOrderExtModel model) {
        log.info("工厂{}缝纫批量汇报 --- START --- PARAM:{} --- {}", model.getOrganizeCode(), JSONObject.toJSONString(model), Thread.currentThread().getName());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getModifyUser(), "操作人不能为空");
            ValidatorBean.checkNotNull(model.getPartNo(), "零件编码不能为空");
            ValidatorBean.checkNotNull(model.getWorkOrderNo(), "生产工单号不能为空");
            ValidatorBean.checkNotNull(model.getId(), "生产工单ID不能为空");
            return ResultBean.success("汇报成功").setResultObject(fengRenBatchReportService.doFengRenBatchReport(model));
        } catch (ImppBusiException imppException) {
            log.info("工厂{}缝纫批量汇报 --- ImppBusiException --- {} --- {}", model.getOrganizeCode(), imppException.getErrorMsg(), Thread.currentThread().getName());
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            log.info("工厂{}缝纫批量汇报 --- Exception --- {} --- {}", model.getOrganizeCode(), JSONObject.toJSONString(e), Thread.currentThread().getName());
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        } finally {
            stopWatch.stop();
            log.info("工厂{}缝纫批量汇报 --- FINALLY --- 耗时:{} --- {}", model.getOrganizeCode(), stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());
        }
    }

}
