package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNumberRuleMatchDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdTempDataContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionAssemblyNosortContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 规则并截取右侧流水
 */
@Slf4j
@Service
public class MesNumberRuleMatchSubstringSerialService implements IMesNumberRuleMatchDispatchService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Override
    public Map<String, Object> matchNumberRule(String organizeCode, String sn, Object... params) {
        // 校验正则表达式
        Map<String, Object> result = ((IMesNumberRuleMatchDispatchService) SpringContextsUtil.getBean(MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.MATCH_TYPE_20.getStrategyClass())).matchNumberRule(organizeCode, sn, params);

        if (!(Boolean) result.get(MesPcnExtConstWords.RESULT)) return result;

        MesProductionAssemblyNosortContext context = (MesProductionAssemblyNosortContext) params[0];

        if (StringUtils.isEmpty(context.getSerialLength())) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, String.format("装配件ID[%s]信息未维护[截取的流水号长度]!", context.getSourceId()));
            return result;
        }

        if (context.getSerialLength().compareTo(sn.length()) > 0 ) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, String.format("装配件条码[%s]不匹配截取的流水号长度[%s]!", sn, context.getSerialLength()));
            return result;
        }

        //截取右侧流水, 生成零件条码工步需要使用此流水生成新的零件条码
        String serialNo = substringSerial(context.getAssemblySn(), context.getSerialLength());

        //一腔如果存在两个这个规则的情况下,需要验证两次的流水号是否一直
        List<MesProdTempDataContext> prodTempDataContextList = productionDispatchContextStepService.getProdTempDataContext(organizeCode, context.getWorkCenterCode(), context.getWorkCellCode());
        Optional<MesProdTempDataContext> optional = CollectionUtils.isEmpty(prodTempDataContextList) ? null :
                prodTempDataContextList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getForeignKey()) && o.getForeignKey().compareTo(context.getForeignKey()) == 0)).findFirst();
        if (null == optional || !optional.isPresent() || StringUtils.isEmpty(optional.get().getSerialNo())) {
            if (CollectionUtils.isEmpty(prodTempDataContextList)) prodTempDataContextList = new ArrayList<>();
            prodTempDataContextList.add(new MesProdTempDataContext(organizeCode, context.getForeignKey()).serialNo(serialNo));
            productionDispatchContextStepService.dispatchProdTempDataContext(organizeCode, context.getWorkCenterCode(), context.getWorkCellCode(), prodTempDataContextList);
            return result;
        }

        if (!serialNo.equals(optional.get().getSerialNo())) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, String.format("装配件条码[%s]的右侧流水号[%s]与首次扫描的流水号[%s]不一致!", sn, serialNo, optional.get().getSerialNo()));
            return result;
        }

        return result;

    }

    //截取右侧流水, 生成零件条码工步需要使用此流水生成新的零件条码
    private String substringSerial(String sn, Integer serialLength) {
        return sn.substring(sn.length() - serialLength);
    }

}
