package com.hz.pm.api.expert.assembler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hz.pm.api.expert.model.DictFieldInfoDTO;
import com.hz.pm.api.expert.model.dto.ExpertAdminExpertListDTO;
import com.hz.pm.api.expert.model.vo.ExpertLibraryListItemVO;
import com.hz.pm.api.meta.helper.DictionaryCache;
import com.hz.pm.api.meta.model.dto.DictionaryDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/8/5 下午4:32
 */
@Component
@RequiredArgsConstructor
public class ExpertAdminExpertManageAssembler {

    private final DictionaryCache dictionaryCache;

    public List<ExpertLibraryListItemVO> toViews(
            List<ExpertAdminExpertListDTO> experts) {
        if (CollUtil.isEmpty(experts)) {
            return Collections.emptyList();
        }
        return experts.stream().map(r -> {
            ExpertLibraryListItemVO item = new ExpertLibraryListItemVO();
            item.setUserId(r.getUserId());
            item.setExpertName(r.getExpertName());
            item.setPhoneNo(r.getPhoneNo());
            item.setCompany(r.getCompany());
            item.setExpertType(fillDictName(r.getExpertType()));
            item.setExpertSource(fillDictName(r.getExpertSource()));
            item.setExpertLibrary(fillDictName(r.getExpertLibrary()));
            item.setExpertSpecialty(fillDictName(r.getExpertSpecialty()));
            item.setInPutTime(r.getInPutTime());
            item.setCreateTime(r.getCreateTime());
            return item;
        }).collect(Collectors.toList());

    }

    private List<DictFieldInfoDTO> fillDictName(List<DictFieldInfoDTO> dicts) {
        if (CollectionUtil.isEmpty(dicts)) {
            return dicts;
        }
        dicts.forEach(r -> {
            Optional<DictionaryDTO> dict = dictionaryCache.getByCode(r.getDictionaryCode());
            dict.ifPresent(dictDTO -> r.setDictionaryName(dictDTO.getName()));
        });
        return dicts;
    }

}
