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

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.StringJoiner;

/**
 * @Description : 嘉兴通知PLC放行工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("jxCenterPlcPassStepService")
public class JxCenterPlcPassStepService extends BaseStepService {

    @Override
    public StepResult execute(StationRequestBean reqBean) {

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

        StationResultBean resultBean = new StationResultBean();

        String forceCmdKey = new StringJoiner(MesPcnExtConstWords.COLON).add(MesPcnExtConstWords.FORCE).add(reqBean.getStepCode()).toString();
        Boolean forceCmd = reqBean.getDataMap().containsKey(forceCmdKey);
        if (forceCmd) reqBean.getDataMap().remove(forceCmdKey);

        //通知PLC放行逻辑暂无

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

        if (!forceCmd) return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean,
                String.format("%s产线放行成功!", StringUtils.isEmpty(reqBean.getProductSn()) ? String.format("管理编码[%s]", getManageCode(reqBean.getTray(), 1)) : String.format("产品条码[%s]", reqBean.getProductSn())));
        else return execSuccessCompleteAndSendMsgReturn(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;
    }

}
