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

import cn.hutool.core.collection.CollUtil;
import com.hz.pm.api.expert.model.*;
import com.hz.pm.api.meta.helper.DictionaryCache;
import com.hz.pm.api.meta.helper.TagCache;
import com.hz.pm.api.meta.model.dto.DictionaryDTO;
import com.hz.pm.api.meta.model.dto.TagDTO;
import com.ningdatech.basic.exception.BizException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author liuxinxin
 * @date 2023/2/23 上午9:18
 */

@Component
@RequiredArgsConstructor
public class ExpertManageHelper {

    private final TagCache tagCache;
    private final DictionaryCache dictionaryCache;

    /**
     * 校验标签code是否合法
     *
     * @param expertProfessionalInfo \
     * @param expertBasicInfo \
     */
    public void tagFieldCheck(ExpertProfessionalInfo expertProfessionalInfo, ExpertBasicInfo expertBasicInfo) {
        List<TagFieldInfo> goodAt = expertProfessionalInfo.getGoodAt();
        List<TagFieldInfo> technicalExpertise = expertProfessionalInfo.getTechnicalExpertise();
        List<TagFieldInfo> industrySector = expertProfessionalInfo.getIndustrySector();
        List<TagFieldInfo> expertSource = expertBasicInfo.getExpertSource();

        List<TagFieldInfo> tagFieldInfoList = new ArrayList<>();
        tagFieldInfoList.addAll(goodAt);
        tagFieldInfoList.addAll(technicalExpertise);
        tagFieldInfoList.addAll(industrySector);
        if (CollUtil.isNotEmpty(expertSource)) {
            tagFieldInfoList.addAll(expertSource);
        }
        for (TagFieldInfo tagFieldInfo : tagFieldInfoList) {
            String tagCode = tagFieldInfo.getTagCode();
            TagDTO tagCodeDTO = tagCache.getByTagCode(tagCode);
            if (Objects.isNull(tagCodeDTO)) {
                throw new BizException("illegal tagCode:  tagField=" + tagFieldInfo.getTagFieldName() + ",tagCode=" + tagCode);
            }
        }
    }


    /**
     * 校验字典code是否合法
     *
     * @param basicInfo \
     * @param eduInfo   \
     * @param jobInfo   \
     */
    public void dictionaryFieldCheck(ExpertBasicInfo basicInfo, ExpertEduInfo eduInfo, ExpertJobInfo jobInfo) {
        List<DictFieldInfoDTO> political = basicInfo.getPolitical();
        List<DictFieldInfoDTO> edu = eduInfo.getEdu();
        List<DictFieldInfoDTO> degree = eduInfo.getDegree();
        List<DictFieldInfoDTO> jobStatus = jobInfo.getJobStatus();
        List<DictFieldInfoDTO> companyAttribute = jobInfo.getCompanyAttribute();
        List<DictFieldInfoDTO> administrativeRank = jobInfo.getAdministrativeRank();

        List<DictFieldInfoDTO> dictFieldList = new ArrayList<>();
        dictFieldList.addAll(political);
        dictFieldList.addAll(edu);
        dictFieldList.addAll(degree);
        dictFieldList.addAll(jobStatus);
        dictFieldList.addAll(companyAttribute);
        dictFieldList.addAll(administrativeRank);

        for (DictFieldInfoDTO dictField : dictFieldList) {
            String dictCode = dictField.getDictionaryCode();
            Optional<DictionaryDTO> dict = dictionaryCache.getByCode(dictCode);
            if (!dict.isPresent()) {
                String dictFieldName = dictField.getDictionaryFieldName();
                throw BizException.wrap("字典数据无效：%s=%s", dictFieldName, dictCode);
            }
        }
    }

}
