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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesMouldMultiCavityService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPartContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesMouldMultiCavity;
import cn.estsh.i3plus.pojo.mes.repository.MesMouldMultiCavityRepository;
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;

@Slf4j
@Service
public class MesMouldMultiCavityService implements IMesMouldMultiCavityService {

    @Autowired
    private MesMouldMultiCavityRepository mouldMultiCavityRepository;

    @Override
    public List<MesMouldMultiCavity> getMouldMultiCavityList(String organizeCode, String equipmentCode, String mouldNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(equipmentCode) || StringUtils.isEmpty(mouldNo)) return null;
        return mouldMultiCavityRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.EQUIPMENT_CODE, MesPcnExtConstWords.MOULD_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), equipmentCode, mouldNo});
    }

    @Override
    public List<MesProductionPartContext> getProductionPartList(String organizeCode, String equipmentCode, String mouldNo) {
        List<MesMouldMultiCavity> mouldMultiCavityList = getMouldMultiCavityList(organizeCode, equipmentCode, mouldNo);
        if (CollectionUtils.isEmpty(mouldMultiCavityList)) return null;
        List<MesProductionPartContext> productionPartContextList = new ArrayList<>();
        mouldMultiCavityList.stream().filter(o -> null != o).forEach(o -> productionPartContextList.add(new MesProductionPartContext().copyMouldMultiCavity(o)));
        return productionPartContextList;
    }

}
