package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.step.jx;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProdBusiStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxProdBusiStrategyParamsBuilder;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 嘉兴条码终检标记返修工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("jxSnFinalInspectionMarkRepairStepService")
public class JxSnFinalInspectionMarkRepairStepService extends BaseStepService {

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- JxSnFinalInspectionMarkRepairStepService --- START --- ", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        StationResultBean resultBean = new StationResultBean();

        String suffix = StringUtils.isEmpty(reqBean.getProductSn()) ? String.format("管理编码[%s]", getManageCode(reqBean.getTray(), 1)) : String.format("产品条码[%s]", reqBean.getProductSn());

        execFinalInspectionMarkRepair(reqBean);

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- JxSnFinalInspectionMarkRepairStepService --- SUCCESS --- ", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean, String.format("%s标记返修完成!", suffix));
    }

    private String getManageCode(String manageCode, Integer length) {
        if (StringUtils.isEmpty(manageCode)) return null;
        String[] manageCodeArr = manageCode.split(MesPcnExtConstWords.AND);
        if (manageCodeArr.length >= length) return manageCodeArr[length - 1];
        else return null;
    }

    private void execFinalInspectionMarkRepair(StationRequestBean reqBean) {
        ((IJxProdBusiStrategyService) SpringContextsUtil.getBean(MesPcnExtEnumUtil.JX_PROD_BUSI_STRATEGY.COMMON.getClazz()))
                .execFinalInspectionMarkRepair(new JxProdBusiStrategyParamsBuilder.builder()
                        .organizeCode(reqBean.getOrganizeCode())
                        .userInfo(reqBean.getUserInfo())
                        .location(reqBean.getWorkCenterCode(), reqBean.getWorkCellCode())
                        .serialNumber(reqBean.getSerialNumber())
                        .productSn(reqBean.getProductSn())
                        .manageCode(reqBean.getTray())
                        .build());
    }

}
