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.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionAssemblyContext;
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.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesPartPackageRule;
import cn.estsh.i3plus.pojo.mes.repository.IMesPartPackageRuleDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.IMesPartPackageRuleRepository;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description : 通用包装组零件校验(VKM)
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/20 16:54
 * @Modify:
 **/
@Slf4j
@Service
public class MesNumberRuleMatchVkmService implements IMesNumberRuleMatchDispatchService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesPartPackageRuleRepository mesPartPackageRuleRepository;

    @Autowired
    private IMesPartPackageRuleDetailRepository mesPartPackageRuleDetailRepository;

    @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;
        }
        //当前装配件清单信息
        MesProductionAssemblyContext context = (MesProductionAssemblyContext) params[0];
        //获取上下文产品加工规则数据信息集合
        List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(organizeCode, context.getWorkCenterCode(), context.getWorkCellCode());
        if (CollectionUtils.isEmpty(prodRuleContextList)) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "当前不存在非排序加工规则数据,请重置工序!");
            return result;
        }
        //获取加工规则
        Optional<MesProdRuleContext> contextOptional = prodRuleContextList.stream().filter(t -> Objects.equals(t.getForeignKey(), context.getForeignKey())).findFirst();
        if (!contextOptional.isPresent()) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "当前不存在非排序加工规则数据,请重置工序!");
            return result;
        }
        //装配件清单
        List<MesProductionAssemblyNosortContext> assemblyNosortContextList = contextOptional.get().getNosortAssemblyDataContext();
        if (CollectionUtils.isEmpty(assemblyNosortContextList)) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "当前不存在已装配的数据!");
            return result;
        }
        //装配件条码字段的长度等于60(固定)为VKM包条码
        Optional<MesProductionAssemblyNosortContext> assemblyNosortContext = assemblyNosortContextList.stream().filter(t -> MesPcnExtConstWords.SIXTY == t.getAssemblySn().length()).findFirst();
        if (!assemblyNosortContext.isPresent()) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "不存在主零件!");
            return result;
        }
        //通用包装组零件规则
        MesPartPackageRule mesPartPackageRule = getMesPartPackageRule(organizeCode, assemblyNosortContext.get().getAssemblyPartNo());
        if (Objects.isNull(mesPartPackageRule)) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "通用包装组零件规则不存在!");
            return result;
        }
        //通用包装组零件规则明细
        if (!isExitMesPartPackageRuleDetail(organizeCode, context.getAssemblyPartNo(), mesPartPackageRule.getId())) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "通用包装组零件规则明细不存在!");
            return result;
        }
        //获取大包装流水最后十四位数(固定)
        String seq = assemblyNosortContext.get().getAssemblySn().substring(assemblyNosortContext.get().getAssemblySn().length() - MesPcnExtConstWords.FOURTEEN);
        //如果seq(22810138101376)的前四位(固定)与当前扫描的装配件条码的(26502271 8211001000000X 654406818 2123198103002281)后4位一致，则代表匹配成功，否则失败则提示:"流水号不一致"
        if (!sn.endsWith(seq.substring(MesPcnExtConstWords.ZERO, MesPcnExtConstWords.FOUR))) {
            result.put(MesPcnExtConstWords.RESULT, false);
            result.put(MesPcnExtConstWords.MESSAGE, "流水号不一致!");
            return result;
        }

        return result;
    }

    private MesPartPackageRule getMesPartPackageRule(String organizeCode, String assemblyPartNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(assemblyPartNo)) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(assemblyPartNo, MesPcnExtConstWords.PACKAGE_PART_NO, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PACKAGE_RULE_TYPE.PACKAGE_RULE_TYPE_10.getValue(), MesPcnExtConstWords.RULE_TYPE, packBean);
        return mesPartPackageRuleRepository.getByProperty(packBean);
    }

    private boolean isExitMesPartPackageRuleDetail(String organizeCode, String assemblyPartNo, long id) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(assemblyPartNo)) {
            return false;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(id, MesPcnExtConstWords.PID, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PACKAGE_RULE_MATCH_TYPE.PACKAGE_RULE_MATCH_TYPE_10.getValue(), MesPcnExtConstWords.MATCH_TYPE, packBean);
        DdlPreparedPack.getStringEqualPack(assemblyPartNo, MesPcnExtConstWords.PART_NO, packBean);
        return mesPartPackageRuleDetailRepository.isExitByHql(packBean);
    }
}
