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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxReworkOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu3.SxPackSnReportModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.JsonUtilTool;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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 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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-07-04 17:42
 */
@RestController
@Slf4j
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/ext/rework-order")
@Api(tags = "BU三期返工工单")
public class SxReworkOrderController {

    @Autowired
    private ISxReworkOrderService reworkOrderService;

    @PostMapping(value = "/update-rework-order-status")
    @ApiOperation(value = "修改返工工单状态及相关关联信息")
    public ResultBean updateReworkOrderStatus(@RequestBody Map<String, Object> params) {

        log.info("updateReworkOrderStatus --- params : {}", params);

        try {
            if(!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工厂代码不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.USER_NAME)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("操作人不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.REWORK_ORDER_NO)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工单号不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.STATUS)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工单状态不能为空")
                        .build();
            }

            reworkOrderService.updateReworkOrderStatus((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE),
                    (String) params.get(MesPcnExtConstWords.USER_NAME),
                    (String) params.get("nowTime"),
                    Integer.parseInt((String) params.get(MesPcnExtConstWords.STATUS)),
                    JsonUtilTool.toList((String) params.get(MesPcnExtConstWords.REWORK_ORDER_NO), String.class));

            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/produce-sn-repair/query")
    @ApiOperation(value = "修改返工工单及相关关联信息")
    public ResultBean queryMesProduceSnRepairList(@RequestBody Map<String, Object> params) {
        try {
            if(!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工厂代码不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.REWORK_ORDER_NO)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工单号不能为空")
                        .build();
            }

            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reworkOrderService.queryMesProduceSnRepairList((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE),
                            (String) params.get(MesPcnExtConstWords.REWORK_ORDER_NO)));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-ext/rework-range/standard-work-order-lot/pack-sn/query")
    @ApiOperation(value = "QMS调MES 获取工单某一批次条码")
    public ResultBean queryStandardWorkOrderPackSnListWithQms(@RequestBody Map<String, Object> params) {
        try {
            log.info(" queryStandardWorkOrderPackSnListWithQms 参数 param:{} ", params);
            SxPackSnReportModel packSnReportModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), SxPackSnReportModel.class);
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reworkOrderService.queryStandardWorkOrderPackSn(packSnReportModel));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
