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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxEquipmentCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEquipmentCheck;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEquipmentCheckRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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.*;
import java.util.stream.Collectors;

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

    @Autowired
    private MesEquipmentCheckRepository equipmentCheckRepository;

    @Override
    public Map<String, String> queryEquipmentCheckEquInfoMap(MesEquipmentCheck equipmentCheck) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(equipmentCheck.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringLikerPack(equipmentCheck.getEquNo(), MesExtConstWords.EQU_NO, packBean);
        DdlPreparedPack.getStringLikerPack(equipmentCheck.getEquName(), MesExtConstWords.EQU_NAME, packBean);
        List<MesEquipmentCheck> equipmentCheckList = equipmentCheckRepository.findByHqlWhere(packBean);
        equipmentCheckList = CollectionUtils.isEmpty(equipmentCheckList) ? null :
                equipmentCheckList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getEquNo()))).collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MesEquipmentCheck::getEquNo))), ArrayList::new)
                );
        equipmentCheckList = CollectionUtils.isEmpty(equipmentCheckList) ? null :
                equipmentCheckList.stream().filter(o -> null != o).sorted(Comparator.comparing(MesEquipmentCheck::getEquNo)).collect(Collectors.toList());
        return CollectionUtils.isEmpty(equipmentCheckList) ? null :
                equipmentCheckList.stream().filter(o -> null != o).collect(Collectors.toMap(MesEquipmentCheck::getEquNo, MesEquipmentCheck::getEquName));
    }
}
