package com.ningdatech.pmapi.expert.helper;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.expert.model.*;
import com.ningdatech.pmapi.meta.helper.DictionaryCache;
import com.ningdatech.pmapi.meta.helper.TagCache;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import com.ningdatech.pmapi.meta.model.dto.DictionaryDTO;
import com.ningdatech.pmapi.meta.model.dto.TagDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

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

@Component
public class ExpertManageHelper {

//    @Autowired
//    private RegionCache regionCache;

    @Autowired
    private TagCache tagCache;

    @Autowired
    private DictionaryCache dictionaryCache;


    /**
     * 校验区域code
     *
     * @param expertRegionInfo
     */
    public void expertRegionInfoCheck(ExpertRegionInfo expertRegionInfo) {
//        // todo 必须为叶子节点编码
//        String regionCode = expertRegionInfo.getRegionCode();
//        Integer regionLevel = expertRegionInfo.getRegionLevel();
//        RegionDTO regionDTO = regionCache.getByCodeAndLevel(regionCode, regionLevel);
//        if (Objects.isNull(regionDTO)) {
//            throw new BadRequestException("illegal regionInfo");
//        }
    }


    /**
     * 校验标签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);
        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 expertBasicInfo
     * @param expertEduInfo
     * @param expertJobInfo
     */
    public void dictionaryFieldCheck(ExpertBasicInfo expertBasicInfo, ExpertEduInfo expertEduInfo
            , ExpertJobInfo expertJobInfo) {
        List<DictionaryFieldInfo> political = expertBasicInfo.getPolitical();
        List<DictionaryFieldInfo> edu = expertEduInfo.getEdu();
        List<DictionaryFieldInfo> degree = expertEduInfo.getDegree();
        List<DictionaryFieldInfo> jobStatus = expertJobInfo.getJobStatus();
        List<DictionaryFieldInfo> companyAttribute = expertJobInfo.getCompanyAttribute();
        List<DictionaryFieldInfo> administrativeRank = expertJobInfo.getAdministrativeRank();

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

        for (DictionaryFieldInfo dictionaryFieldInfo : dictionaryFieldInfoList) {
            String dictionaryFieldName = dictionaryFieldInfo.getDictionaryFieldName();
            String dictionaryCode = dictionaryFieldInfo.getDictionaryCode();
            DictionaryDTO dictionaryDTO = dictionaryCache.getByCode(dictionaryCode);
            if (Objects.isNull(dictionaryDTO)) {
                throw new BizException("illegal dictionaryCode:  dictionaryFieldName=" + dictionaryFieldName + ",dictionaryCode=" + dictionaryCode);
            }
        }
    }

}
