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.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductData;
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 cn.estsh.i3plus.pojo.mes.repository.MesProductDataRepository;
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("jxSnFinalInspectionDataReviewStepService")
public class JxSnFinalInspectionDataReviewStepService extends BaseStepService {

    @Autowired
    private JxSnFinalInspectionModuleService snFinalInspectionModuleService;

    @Autowired
    private MesProductDataRepository productDataRepository;

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

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- JxSnFinalInspectionDataReviewStepService --- 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) && busiFlag.equals(BUSI_FLAG.ASPECT_CHECK.getValue()))
            return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean, String.format("%s[%s]已跳过数据复核进入外观检查!", suffix, suffixSn));

        StepResult stepResult = StepResult.getSuccessComplete();

        if (!StringUtils.isEmpty(busiFlag) && busiFlag.equals(BUSI_FLAG.REPAIR.getValue()))
            return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult.nextTriggerEvent(MesPcnExtConstWords.STATE_REPAIR), String.format("%s[%s]已跳过数据复核!即将进行返修状态标记!", suffix, suffixSn));

        MesProductData productData = getProductData(reqBean, suffixSn);

        if (null == productData) execSendDataReviewBusiFlag(reqBean, resultBean, suffix, suffixSn);

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

        return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, String.format("%s数据复核验证成功!", suffix));
    }

    private MesProductData getProductData(StationRequestBean reqBean, String suffixSn) {
        return productDataRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), suffixSn});
    }

    private void execSendDataReviewBusiFlag(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.ASPECT_CHECK.value).buttonName(BUSI_FLAG.ASPECT_CHECK.description).methodContent(suffixSn).buttonDesc(BUSI_FLAG.ASPECT_CHECK.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.EXP_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 {
        ASPECT_CHECK("ASPECT_CHECK", "最终检查"),
        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;
        }
    }

}
