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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesAssemblyExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProdRuleCfgExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRuleIgnoreCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRuleNosortCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.repository.MesProdRuleNoSortCfgRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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;

@Slf4j
@Service
public class MesProdRuleCfgExtService implements IMesProdRuleCfgExtService {

    @Autowired
    private IMesAssemblyExtService assemblyExtService;

    @Autowired
    private MesProdRuleNoSortCfgRepository prodRuleNoSortCfgRepository;

    //【非排序线】获取产品加工规则
    @Override
    public List<MesProdRuleNosortCfg> getProdRuleNosortCfgList(MesProdRuleContext prodRuleContext) {
        if (StringUtils.isEmpty(prodRuleContext.getOrganizeCode()) || StringUtils.isEmpty(prodRuleContext.getEquipmentCode())) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(prodRuleContext.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(prodRuleContext.getEquipmentCode(), MesPcnExtConstWords.EQUIPMENT_CODE, packBean);

        //outPartNo与outPartNos 带入条件必须互斥 , 此处查询时不作判断
        DdlPreparedPack.getStringEqualPack(prodRuleContext.getOutPartNo(), MesPcnExtConstWords.OUT_PART_NO, packBean);
        DdlPreparedPack.getInPackList(prodRuleContext.getOutPartNoList(), MesPcnExtConstWords.OUT_PART_NO, packBean);

        //inPartNo与inPartNos 带入条件必须互斥 , 此处查询时不作判断
        DdlPreparedPack.getStringEqualPack(prodRuleContext.getInPartNo(), MesPcnExtConstWords.IN_PART_NO, packBean);
        DdlPreparedPack.getInPackList(prodRuleContext.getInPartNoList(), MesPcnExtConstWords.IN_PART_NO, packBean);

        //进料是否为NULL
        if (!StringUtils.isEmpty(prodRuleContext.getInPartIsEmpty())) {
            if (prodRuleContext.getInPartIsEmpty()) DdlPreparedPack.getIsNull(MesPcnExtConstWords.IN_PART_NO, packBean);
            else DdlPreparedPack.getIsNotNull(MesPcnExtConstWords.IN_PART_NO, packBean);
        }

        //进料规则是否为NULL
        if (!StringUtils.isEmpty(prodRuleContext.getInPartRuleIsEmpty())) {
            if (prodRuleContext.getInPartRuleIsEmpty()) DdlPreparedPack.getIsNull(MesPcnExtConstWords.IN_PART_NO_RULE, packBean);
            else DdlPreparedPack.getIsNotNull(MesPcnExtConstWords.IN_PART_NO_RULE, packBean);
        }

        return prodRuleNoSortCfgRepository.findByHqlWhere(packBean);

    }

    //【非排序线】获取产品加工规则
    @Override
    public List<MesProdRuleNosortCfg> getProdRuleNosortCfgList(MesProdRuleContext... prodRuleContexts) {
        if (null == prodRuleContexts) return null;
        List<MesProdRuleNosortCfg> prodRuleNosortCfgList = null;
        for (MesProdRuleContext prodRuleContext : prodRuleContexts) {
            if (null == prodRuleContext) continue;
            List<MesProdRuleNosortCfg> itemList = getProdRuleNosortCfgList(prodRuleContext);
            if (CollectionUtils.isEmpty(itemList)) continue;
            if (CollectionUtils.isEmpty(prodRuleNosortCfgList)) prodRuleNosortCfgList = new ArrayList<>();
            prodRuleNosortCfgList.addAll(itemList);
        }
        return prodRuleNosortCfgList;
    }

    //【非排序线】获取产品加工规则 -之后- 关联不可用规则
    @Override
    public List<MesProdRuleNosortCfg> getProdRuleNosortCfgList(List<MesProdRuleNosortCfg> prodRuleNosortCfgList, Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap) {
        if (CollectionUtils.isEmpty(prodRuleNosortCfgList) || CollectionUtils.isEmpty(prodRuleIgnoreCfgMap)) return prodRuleNosortCfgList;
        prodRuleNosortCfgList.stream().forEach(o -> o.setProdRuleIgnoreCfg(assemblyExtService.getProdRuleIgnoreCfg(prodRuleIgnoreCfgMap, o.getId(), o.getEquipmentCode(), MesExtEnumUtil.PROD_RULE_IGNORE_DATA_SOURCE.DATA_SOURCE20.getValueStr())));
        return null;
    }

    //【非排序线】获取产品加工规则对应的装配件信息 -同时- 关联不可用规则
    @Override
    public MesProdRuleContext getProdRuleNosortContext(MesProdRuleContext prodRuleContext, Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap) {
        //获取非排序装配件配置信息
        if (!StringUtils.isEmpty(prodRuleContext.getIsHasBind()) && CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == prodRuleContext.getIsHasBind()) prodRuleContext.assemblyDataJson(assemblyExtService.getProductionAssemblyNosortContextList(prodRuleContext, prodRuleIgnoreCfgMap));
        return prodRuleContext;
    }

    //【非排序线】获取产品加工规则 【根据PID获取】
    @Override
    public MesProdRuleContext getProdRuleNosortContextByPid(MesProdRuleContext prodRuleContext) {

        if (StringUtils.isEmpty(prodRuleContext.getOrganizeCode()) || StringUtils.isEmpty(prodRuleContext.getPid())) return null;

        MesProdRuleNosortCfg prodRuleNosortCfg = getProdRuleNosortCfg(prodRuleContext.getOrganizeCode(), prodRuleContext.getPid());

        if (null == prodRuleNosortCfg) return null;

        BeanUtils.copyProperties(prodRuleNosortCfg, prodRuleContext);

        return prodRuleContext;

    }

    //【非排序线】获取产品加工规则 【根据PID获取】
    @Override
    public MesProdRuleNosortCfg getProdRuleNosortCfg(String organizeCode, Long id) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(id)) return null;
        return prodRuleNoSortCfgRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), id});
    }


    //【非排序线】获取产品加工规则 【根据PID获取】 -之后- 关联不可用规则
    @Override
    public MesProdRuleContext getProdRuleNosortContextByPid(MesProdRuleContext prodRuleContext, Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap) {
        if (null == prodRuleContext || CollectionUtils.isEmpty(prodRuleIgnoreCfgMap)) return prodRuleContext;
        prodRuleContext.setProdRuleIgnoreCfg(assemblyExtService.getProdRuleIgnoreCfg(prodRuleIgnoreCfgMap, prodRuleContext.getPid(), prodRuleContext.getEquipmentCode(), MesExtEnumUtil.PROD_RULE_IGNORE_DATA_SOURCE.DATA_SOURCE20.getValueStr()));
        return prodRuleContext;
    }

    //【排序线】获取产品加工规则(条码对应的装配件绑定记录,目前条码等同工单) -同时- 关联不可用规则; 判断是否剔除平行工位
    @Override
    public MesProdRuleContext getProdRuleSortContext(MesProdRuleContext prodRuleContext, Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap, Boolean isFilterParallel) {
        return prodRuleContext.assemblyDataJson(assemblyExtService.getProductionAssemblySortContextList(prodRuleContext, prodRuleIgnoreCfgMap, isFilterParallel));
    }

    //【排序线】获取产品加工规则 [前道所有装配件] -同时- 关联不可用规则; 判断是否剔除平行工位 ; 判断是否根据工序顺序号过滤后道
    @Override
    public MesProdRuleContext getProdRuleSortPreCraftContext(MesProdRuleContext prodRuleContext, Map<String, MesWorkCell> workCellMap, Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap, Boolean isFilterParallel, Boolean isFilterAfterCraft) {
        return prodRuleContext.assemblyDataJson(assemblyExtService.getProductionAssemblySortPreCraftContextList(prodRuleContext, workCellMap, prodRuleIgnoreCfgMap, isFilterParallel, isFilterAfterCraft));
    }

}
