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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesEquipmentProdParamCfgService;
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.MesEquipmentProdParamCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentProdParamCfgRepository;
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.stream.Collectors;

@Slf4j
@Service
public class MesEquipmentProdParamCfgService implements IMesEquipmentProdParamCfgService {

    @Autowired
    private MesEquipmentProdParamCfgRepository equipmentProdParamCfgRepository;

    //根据来源ID查询设备加工参数信息
    @Override
    public List<MesEquipmentProdParamCfg> getEquipmentProdParamCfgList(String organizeCode, List<Long> sourceIdList) {
        if (StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(sourceIdList)) return null;
        sourceIdList = sourceIdList.stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (sourceIdList.size() == 1) DdlPreparedPack.getNumEqualPack(sourceIdList.get(0), MesPcnExtConstWords.SOURCE_ID, packBean);
        else DdlPreparedPack.getInPackList(sourceIdList, MesPcnExtConstWords.SOURCE_ID, packBean);
        List<MesEquipmentProdParamCfg> equipmentProdParamCfgList = equipmentProdParamCfgRepository.findByHqlWhere(packBean);
        return CollectionUtils.isEmpty(equipmentProdParamCfgList) ? null :
                equipmentProdParamCfgList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getEquipVariableId()) && !StringUtils.isEmpty(o.getMatchRule()))).collect(Collectors.toList());
    }

}
