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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.jx.JxSnFinalInspectionModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.ButtonBean;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 嘉兴条码终检外观检查工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("jxSnFinalInspectionAspectCheckStepService")
public class JxSnFinalInspectionAspectCheckStepService extends BaseStepService {

    @Autowired
    private JxSnFinalInspectionModuleService snFinalInspectionModuleService;

    @Override
    public StepResult init(StationRequestBean reqBean) {
        clearStepContent(reqBean);
        return super.init(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {

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

        StationResultBean resultBean = new StationResultBean();

        String busiFlag = (String) snFinalInspectionModuleService.doHandleBusiFlag(reqBean, 1, null);
        if (!StringUtils.isEmpty(busiFlag)) snFinalInspectionModuleService.doHandleBusiFlag(reqBean, 3, null);

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

        if (StringUtils.isEmpty(busiFlag)) execSendAspectCheckBusiFlag(reqBean, resultBean, suffix, suffixSn);

        StepResult stepResult = StepResult.getSuccessComplete();

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

        if (busiFlag.equals(BUSI_FLAG.QUALIFIED.getValue()))
            return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, String.format("%s[%s]外观检查[合格]!", suffix, suffixSn));

        return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, String.format("%s[%s]外观检查[不合格]!即将进行返修状态标记!", suffix, suffixSn));

    }

    private void execSendAspectCheckBusiFlag(StationRequestBean reqBean, StationResultBean resultBean, String suffix, String suffixSn) {
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.BUTTON.getValue());
        List<ButtonBean> buttonBeanList = new ArrayList<>();
        buttonBeanList.add(ButtonBean.builder().buttonCode(BUSI_FLAG.QUALIFIED.value).buttonName(BUSI_FLAG.QUALIFIED.description).methodContent(suffixSn).buttonDesc(BUSI_FLAG.QUALIFIED.description).build());
        buttonBeanList.add(ButtonBean.builder().buttonCode(BUSI_FLAG.REPAIR.value).buttonName(BUSI_FLAG.REPAIR.description).methodContent(suffixSn).buttonDesc(BUSI_FLAG.REPAIR.description).build());
        resultBean.setResultList(buttonBeanList);
        this.sendMessage(reqBean, resultBean);
        this.sendMessage(reqBean, resultBean, String.format("请对当前产品:%s[%s]进行外观检查!", suffix, suffixSn), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        execSendGuideAndThrowEx(reqBean, resultBean, "请点击业务按钮!");
    }

    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;
    }

    enum BUSI_FLAG {
        QUALIFIED("QUALIFIED", "检查合格"),
        REPAIR("REPAIR", "返修");

        private String value;
        private String description;

        BUSI_FLAG(String value, String description) {
            this.value = value;
            this.description = description;
        }

        public String getValue() {
            return value;
        }

        public String getDescription() {
            return description;
        }
    }

}
