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

import cn.estsh.i3plus.ext.mes.pcn.api.base.bu3.ISxPartCategoryPlanRateService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartCategoryPlanRate;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartCategoryPlanRateRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxPartCategoryPlanRateService implements ISxPartCategoryPlanRateService {

    @Autowired
    private MesPartCategoryPlanRateRepository partCategoryPlanRateRepository;

    @Override
    public List<MesPartCategoryPlanRate> getPartCategoryPlanRateList(String organizeCode) {
        return partCategoryPlanRateRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
    }

    @Override
    public Map<String, String> getCategoryMap(String organizeCode) {
        List<MesPartCategoryPlanRate> partCategoryPlanRateList = getPartCategoryPlanRateList(organizeCode);
        partCategoryPlanRateList = CollectionUtils.isEmpty(partCategoryPlanRateList) ? null :
                partCategoryPlanRateList.stream().filter(o -> !StringUtils.isEmpty(o.getCategoryCode()) && !StringUtils.isEmpty(o.getCategoryName())).collect(Collectors.toList());
        return CollectionUtils.isEmpty(partCategoryPlanRateList) ? null :
                partCategoryPlanRateList.stream().filter(o -> null != o).collect(Collectors.toMap(MesPartCategoryPlanRate::getCategoryCode, MesPartCategoryPlanRate::getCategoryName));
    }
}
