package com.ningdatech.pmapi.expert.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.enumeration.BoolDisplayEnum;
import com.ningdatech.pmapi.expert.assembler.ExpertUserInfoAssembler;
import com.ningdatech.pmapi.expert.constant.ExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.constant.ExpertApplyStatusEnum;
import com.ningdatech.pmapi.expert.constant.ExpertApplyTypeEnum;
import com.ningdatech.pmapi.expert.constant.ExpertUserInfoStepEnum;
import com.ningdatech.pmapi.expert.entity.ExpertAvoidCompany;
import com.ningdatech.pmapi.expert.entity.ExpertIntentionWorkRegion;
import com.ningdatech.pmapi.expert.entity.ExpertMetaApply;
import com.ningdatech.pmapi.expert.entity.ExpertUserFullInfo;
import com.ningdatech.pmapi.expert.model.cmd.ExpertFullInfoSaveCmd;
import com.ningdatech.pmapi.expert.model.cmd.ExpertRecommendProofSaveCmd;
import com.ningdatech.pmapi.expert.model.cmd.ExpertStorageDealCmd;
import com.ningdatech.pmapi.expert.model.dto.*;
import com.ningdatech.pmapi.expert.service.*;
import com.ningdatech.pmapi.meta.constant.DictExpertInfoTypeEnum;
import com.ningdatech.pmapi.meta.model.entity.ExpertDictionary;
import com.ningdatech.pmapi.meta.model.entity.ExpertTag;
import com.ningdatech.pmapi.meta.service.IExpertDictionaryService;
import com.ningdatech.pmapi.meta.service.IExpertTagService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/7/22 下午4:39
 */
@Component
@RequiredArgsConstructor
public class ExpertInfoServiceImpl implements ExpertInfoService {

    private final IExpertUserFullInfoService iExpertUserFullInfoService;
    private final IExpertTagService iExpertTagService;
    private final IExpertDictionaryService iExpertDictionaryService;
    private final IExpertAvoidCompanyService iExpertAvoidCompanyService;
    private final IExpertIntentionWorkRegionService iExpertIntentionWorkRegionService;
    private final IExpertMetaApplyService iExpertMetaApplyService;


    /**
     * 用户第一次申请修改，仅在专家用户状态为applying 状态时调用
     *
     * @param cmd
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveExpertInfo(ExpertFullInfoSaveCmd cmd) {
        Long userId = cmd.getUserId();
        List<ExpertAvoidCompanyDTO> expertAvoidCompanyList = cmd.getExpertAvoidCompanyList();
        List<ExpertDictionaryDTO> expertDictionaryList = cmd.getExpertDictionaryList();
        List<ExpertRegionDTO> expertIntentionWorkRegionInfoList = cmd.getExpertIntentionWorkRegionInfo();
        List<ExpertTagDTO> expertTagList = cmd.getExpertTagList();
        ExpertUserFullInfoDTO expertUserInfoDTO = cmd.getExpertUserInfoDTO();

        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(userId);

        ExpertUserFullInfo saveExpertUserFullInfo = buildSaveExpertUserFullInfo(expertUserInfoDTO);
        if (Objects.nonNull(expertUserFullInfo)) {
            // 专家信息审核通过之前，所有信息无需备份
            iExpertUserFullInfoService.removeById(expertUserFullInfo.getId());
            // 删除所有专家标签字段
            LambdaQueryWrapper<ExpertTag> expertTagRemove = Wrappers.lambdaQuery(ExpertTag.class)
                    .eq(ExpertTag::getUserId, userId);
            iExpertTagService.remove(expertTagRemove);
            // 删除所有专家字典字段
            LambdaQueryWrapper<ExpertDictionary> expertDictionaryRemove = Wrappers.lambdaQuery(ExpertDictionary.class)
                    .eq(ExpertDictionary::getUserId, userId);
            iExpertDictionaryService.remove(expertDictionaryRemove);
            // 删除所有专家履职意向申请
            // 补充审核逻辑
            LambdaQueryWrapper<ExpertMetaApply> expertMetaApplyRemove = Wrappers.lambdaQuery(ExpertMetaApply.class)
                    .eq(ExpertMetaApply::getUserId, userId)
                    .eq(ExpertMetaApply::getApplyType, ExpertApplyTypeEnum.EXPERT_INTENTION_JOIN.getKey())
                    .eq(ExpertMetaApply::getDisplayEnable, BoolDisplayEnum.N.name());
            iExpertMetaApplyService.remove(expertMetaApplyRemove);
            // 删除回避单位
            LambdaQueryWrapper<ExpertAvoidCompany> expertAvoidCompanyRemove = Wrappers.lambdaQuery(ExpertAvoidCompany.class)
                    .eq(ExpertAvoidCompany::getUserId, userId);
            iExpertAvoidCompanyService.remove(expertAvoidCompanyRemove);
        }
        saveExpertUserFullInfo.setUserId(userId);
        saveExpertUserFullInfo.setExpertAccountStatus(ExpertAccountStatusEnum.APPLYING.getKey());
        saveExpertUserFullInfo.setUserInfoStep(ExpertUserInfoStepEnum.BASIC_INFORMATION_SUBMITTED.getKey());
        saveExpertUserFullInfo.setUpdateOn(LocalDateTime.now());
        saveExpertUserFullInfo.setCreateOn(LocalDateTime.now());
        iExpertUserFullInfoService.save(saveExpertUserFullInfo);
        // 保存所有专家标签字段
        List<ExpertTag> saveExpertTagList = buildSaveExpertTagList(userId, expertTagList);
        if (CollectionUtils.isNotEmpty(saveExpertTagList)) {
            iExpertTagService.saveBatch(saveExpertTagList);
        }
        // 保存所有专家字典字段
        List<ExpertDictionary> saveExpertDictionaryList = buildSaveExpertDictionaryList(userId, expertDictionaryList);
        if (CollectionUtils.isNotEmpty(saveExpertDictionaryList)) {
            iExpertDictionaryService.saveBatch(saveExpertDictionaryList);
        }
        // 保存所有专家履职意向申请

        // 审核逻辑
        List<ExpertMetaApply> saveExpertIntentionWorkRegionApplyList = buildSaveExpertIntentionWorkRegionApplyList(userId, expertIntentionWorkRegionInfoList);
        if (CollectionUtils.isNotEmpty(saveExpertDictionaryList)) {
            iExpertMetaApplyService.saveBatch(saveExpertIntentionWorkRegionApplyList);
        }
        // 保存所有专家回避单位
        List<ExpertAvoidCompany> saveExpertAvoidCompanyList = buildSaveExpertAvoidCompanyList(userId, expertAvoidCompanyList);
        if (CollectionUtils.isNotEmpty(saveExpertAvoidCompanyList)) {
            iExpertAvoidCompanyService.saveBatch(saveExpertAvoidCompanyList);
        }
    }


    private ExpertUserFullInfo buildSaveExpertUserFullInfo(ExpertUserFullInfoDTO expertUserInfoDTO) {
        ExpertUserFullInfo expertUserFullInfo = new ExpertUserFullInfo();
        if (Objects.nonNull(expertUserInfoDTO.getIsDingUser())) {
            expertUserFullInfo.setIsDingUser(expertUserInfoDTO.getIsDingUser() ? BoolDisplayEnum.Y.name() : BoolDisplayEnum.N.name());
        }
        expertUserFullInfo.setPhoneNo(expertUserInfoDTO.getPhoneNo());
        expertUserFullInfo.setGender(expertUserInfoDTO.getGender());
        expertUserFullInfo.setExpertName(expertUserInfoDTO.getName());
        expertUserFullInfo.setAvatarFileId(expertUserInfoDTO.getAvatarFileId());
        expertUserFullInfo.setIdCard(expertUserInfoDTO.getIdCard());
        expertUserFullInfo.setOfficePhone(expertUserInfoDTO.getOfficePhone());
        expertUserFullInfo.setBirth(expertUserInfoDTO.getBirth());
        expertUserFullInfo.setBankNo(expertUserInfoDTO.getBankNo());
        expertUserFullInfo.setBank(expertUserInfoDTO.getBank());
        expertUserFullInfo.setEmail(expertUserInfoDTO.getEmail());
        expertUserFullInfo.setHometown(expertUserInfoDTO.getHometown());
        expertUserFullInfo.setNationality(expertUserInfoDTO.getNationality());

        expertUserFullInfo.setSchool(expertUserInfoDTO.getSchool());
        expertUserFullInfo.setGraduatedAt(expertUserInfoDTO.getGraduatedAt());
        expertUserFullInfo.setAcademicTitle(expertUserInfoDTO.getAcademicTitle());

        if (CollectionUtils.isNotEmpty(expertUserInfoDTO.getGraduationCertificateFileIdList())) {
            expertUserFullInfo.setGraduationCertificateFileIdList(JSONObject.toJSONString(expertUserInfoDTO.getGraduationCertificateFileIdList()));
        }
        if (CollectionUtils.isNotEmpty(expertUserInfoDTO.getDegreeCertificateFileIdList())) {
            expertUserFullInfo.setDegreeCertificateFileIdList(JSONObject.toJSONString(expertUserInfoDTO.getDegreeCertificateFileIdList()));
        }
        expertUserFullInfo.setRetiredAt(expertUserInfoDTO.getRetiredAt());
        expertUserFullInfo.setCompany(expertUserInfoDTO.getCompany());
        expertUserFullInfo.setLegalEntityCode(expertUserInfoDTO.getLegalEntityCode());
        expertUserFullInfo.setAdministrativeDuties(expertUserInfoDTO.getAdministrativeDuties());
        expertUserFullInfo.setStartWorkAt(expertUserInfoDTO.getStartWorkAt());
        expertUserFullInfo.setAddress(expertUserInfoDTO.getAddress());
        expertUserFullInfo.setExperience(expertUserInfoDTO.getExperience());

        expertUserFullInfo.setTechnicalTitles(expertUserInfoDTO.getTechnicalTitles());
        if (CollectionUtils.isNotEmpty(expertUserInfoDTO.getTitleCertificateFileIdList())) {
            expertUserFullInfo.setTitleCertificateFileIdList(JSONObject.toJSONString(expertUserInfoDTO.getTitleCertificateFileIdList()));
        }
        expertUserFullInfo.setAwards(expertUserInfoDTO.getAwards());
        expertUserFullInfo.setRecognitionReward(expertUserInfoDTO.getRecognitionReward());

        expertUserFullInfo.setRegionCode(expertUserInfoDTO.getRegionCode());
        expertUserFullInfo.setRegionLevel(expertUserInfoDTO.getRegionLevel());
        if (CollectionUtils.isNotEmpty(expertUserInfoDTO.getRecommendationProofFileIdList())) {
            expertUserFullInfo.setRecommendationProofFileIdList(JSONObject.toJSONString(expertUserInfoDTO.getRecommendationProofFileIdList()));
        }
        expertUserFullInfo.setRemark(expertUserInfoDTO.getRemark());
        return expertUserFullInfo;
    }


    private List<ExpertTag> buildSaveExpertTagList(Long userId, List<ExpertTagDTO> expertTagList) {
        if (CollectionUtils.isEmpty(expertTagList)) {
            return new ArrayList<>();
        }
        return expertTagList.stream().map(r -> {
            ExpertTag expertTag = new ExpertTag();
            expertTag.setExpertInfoField(r.getExpertInfoField());
            expertTag.setTagCode(r.getTagCode());
            expertTag.setCreateOn(LocalDateTime.now());
            expertTag.setUpdateOn(LocalDateTime.now());
            expertTag.setUserId(userId);
            return expertTag;
        }).collect(Collectors.toList());
    }

    private List<ExpertDictionary> buildSaveExpertDictionaryList(Long userId, List<ExpertDictionaryDTO> expertDictionaryList) {
        if (CollectionUtils.isEmpty(expertDictionaryList)) {
            return new ArrayList<>();
        }
        return expertDictionaryList.stream().map(r -> {
            ExpertDictionary expertDictionary = new ExpertDictionary();
            expertDictionary.setUserId(userId);
            expertDictionary.setExpertInfoField(r.getExpertInfoField());
            expertDictionary.setDictionaryCode(r.getDictionaryCode());
            expertDictionary.setUpdateOn(LocalDateTime.now());
            expertDictionary.setCreateOn(LocalDateTime.now());
            return expertDictionary;
        }).collect(Collectors.toList());
    }


    private List<ExpertAvoidCompany> buildSaveExpertAvoidCompanyList(Long userId, List<ExpertAvoidCompanyDTO> expertAvoidCompanyList) {
        if (CollectionUtils.isEmpty(expertAvoidCompanyList)) {
            return new ArrayList<>();
        }
        return expertAvoidCompanyList.stream().map(r -> {
            ExpertAvoidCompany expertAvoidCompany = new ExpertAvoidCompany();
            expertAvoidCompany.setCompanyName(r.getCompanyName());
            expertAvoidCompany.setUserId(userId);
            expertAvoidCompany.setCompanyUniqCode(r.getCompanyUniqCode());
            expertAvoidCompany.setCreateOn(LocalDateTime.now());
            expertAvoidCompany.setUpdateOn(LocalDateTime.now());
            return expertAvoidCompany;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void expertRecommendProofSave(ExpertRecommendProofSaveCmd cmd) {
        // 保存专家证明材料
        Long userId = cmd.getUserId();
        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(userId);
        expertUserFullInfo.setRecommendationProofFileIdList(JSONObject.toJSONString(cmd.getRecommendationProofFileIdList()));
        expertUserFullInfo.setUpdateOn(LocalDateTime.now());
        expertUserFullInfo.setUserInfoStep(ExpertUserInfoStepEnum.EVIDENCE_HAS_BEEN_SUBMITTED.getKey());
        iExpertUserFullInfoService.saveOrUpdate(expertUserFullInfo);

        List<ExpertDictionaryDTO> recommendedWayList = cmd.getRecommendedWay();
        if (CollectionUtils.isNotEmpty(recommendedWayList)) {
            List<ExpertDictionary> saveExpertDictionaryList = recommendedWayList.stream().map(r -> {
                ExpertDictionary expertDictionary = new ExpertDictionary();
                expertDictionary.setUserId(userId);
                expertDictionary.setExpertInfoField(r.getExpertInfoField());
                expertDictionary.setDictionaryCode(r.getDictionaryCode());
                expertDictionary.setUpdateOn(LocalDateTime.now());
                expertDictionary.setCreateOn(LocalDateTime.now());
                return expertDictionary;
            }).collect(Collectors.toList());
            iExpertDictionaryService.saveBatch(saveExpertDictionaryList);
        }

        // 创建入库申请
        ExpertMetaApply expertRegionApply = new ExpertMetaApply();
        expertRegionApply.setApplyType(ExpertApplyTypeEnum.EXPERT_STORAGE.getKey());
        expertRegionApply.setApplyStatus(ExpertApplyStatusEnum.PENDING_REVIEW.getKey());
        expertRegionApply.setRegionCode(expertUserFullInfo.getRegionCode());
        expertRegionApply.setRegionLevel(expertUserFullInfo.getRegionLevel());
        expertRegionApply.setUserId(userId);
        expertRegionApply.setDisplayEnable(BoolDisplayEnum.Y.name());
        iExpertMetaApplyService.save(expertRegionApply);
    }

    @Override
    public ExpertFullInfoAllDTO getExpertUserFullInfoAll(Long userId) {
        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(userId);
        if (Objects.isNull(expertUserFullInfo)) {
            return null;
        }
        // 所有专家标签字段
        List<ExpertTag> expertTagList = iExpertTagService
                .list(Wrappers.lambdaQuery(ExpertTag.class).eq(ExpertTag::getUserId, userId));
        // 所有专家字典字段
        List<ExpertDictionary> expertDictionaryList = iExpertDictionaryService
                .list(Wrappers.lambdaQuery(ExpertDictionary.class).eq(ExpertDictionary::getUserId, userId));
        // 专家履职意向列表
        List<ExpertIntentionWorkRegion> expertIntentionWorkRegionList = iExpertIntentionWorkRegionService
                .list(Wrappers.lambdaQuery(ExpertIntentionWorkRegion.class).eq(ExpertIntentionWorkRegion::getUserId, userId));
        // 专家履职意向申请列表
        List<ExpertMetaApply> expertMetaApplyList = iExpertMetaApplyService.list(Wrappers.lambdaQuery(ExpertMetaApply.class)
                .eq(ExpertMetaApply::getUserId, userId)
                .eq(ExpertMetaApply::getApplyStatus, ExpertApplyStatusEnum.PENDING_REVIEW.getKey())
                .in(ExpertMetaApply::getApplyType, ExpertApplyTypeEnum.EXPERT_INTENTION_JOIN.getKey(), ExpertApplyTypeEnum.EXPERT_INTENTION_LEAVE.getKey()));
        // 所有专家回避单位
        List<ExpertAvoidCompany> expertAvoidCompanyList = iExpertAvoidCompanyService
                .list(Wrappers.lambdaQuery(ExpertAvoidCompany.class).eq(ExpertAvoidCompany::getUserId, userId));

        return ExpertUserInfoAssembler.buildExpertFullInfoAllDTO(expertUserFullInfo, expertTagList, expertDictionaryList
                , expertIntentionWorkRegionList, expertMetaApplyList, expertAvoidCompanyList);

    }


    private List<ExpertMetaApply> buildSaveExpertIntentionWorkRegionApplyList(Long userId, List<ExpertRegionDTO> expertIntentionWorkRegionInfoList) {
        if (CollectionUtils.isEmpty(expertIntentionWorkRegionInfoList)) {
            return new ArrayList<>();
        }
        return expertIntentionWorkRegionInfoList.stream().map(r -> {
            ExpertMetaApply expertMetaApply = new ExpertMetaApply();
            expertMetaApply.setApplyType(ExpertApplyTypeEnum.EXPERT_INTENTION_JOIN.getKey());
            expertMetaApply.setApplyStatus(ExpertApplyStatusEnum.PENDING_REVIEW.getKey());
            expertMetaApply.setRegionCode(r.getRegionCode());
            expertMetaApply.setRegionLevel(r.getRegionLevel());
            expertMetaApply.setUserId(userId);
            expertMetaApply.setDisplayEnable(BoolDisplayEnum.N.name());
            expertMetaApply.setUpdateOn(LocalDateTime.now());
            expertMetaApply.setCreateOn(LocalDateTime.now());
            return expertMetaApply;
        }).collect(Collectors.toList());
    }


    /**
     * 专家入库审核
     *
     * @param cmd
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void expertStorageDeal(ExpertStorageDealCmd cmd) {
        boolean applyResult = cmd.getApplyResult();
        Long expertUserId = cmd.getExpertUserId();
        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(expertUserId);

        if (applyResult) {
            // 修改专家状态为可用
            expertUserFullInfo.setExpertAccountStatus(ExpertAccountStatusEnum.AVAILABLE.getKey());
            expertUserFullInfo.setRemark(cmd.getRemark());
            expertUserFullInfo.setRegionCode(cmd.getJoinRegionCode());
            expertUserFullInfo.setRegionLevel(cmd.getJoinRegionLevel());

            // 保存所有专家标签字段
            List<ExpertTag> saveExpertTagList = buildSaveExpertTagList(expertUserId, cmd.getOther());
            iExpertTagService.saveBatch(saveExpertTagList);
            // 保存所有专家字典字段
            List<ExpertDictionaryDTO> expertType = cmd.getExpertType();
            if (CollectionUtils.isNotEmpty(expertType)) {
                expertType = expertType.stream().peek(r -> r.setExpertInfoField(DictExpertInfoTypeEnum.EXPERT_TYPE.getKey())).collect(Collectors.toList());
                List<ExpertDictionary> saveExpertDictionaryList = buildSaveExpertDictionaryList(expertUserId, expertType);
                iExpertDictionaryService.saveBatch(saveExpertDictionaryList);
            }

            // 修改履职意向申请状态
            LambdaUpdateWrapper<ExpertMetaApply> set = Wrappers.lambdaUpdate(ExpertMetaApply.class).eq(ExpertMetaApply::getUserId, expertUserId)
                    .eq(ExpertMetaApply::getApplyType, ExpertApplyTypeEnum.EXPERT_INTENTION_JOIN.getKey())
                    .eq(ExpertMetaApply::getApplyStatus, ExpertApplyStatusEnum.PENDING_REVIEW.getKey())
                    .eq(ExpertMetaApply::getDisplayEnable, BoolDisplayEnum.N.name())
                    .set(ExpertMetaApply::getDisplayEnable, BoolDisplayEnum.Y.name());
            iExpertMetaApplyService.update(set);

        } else {
            expertUserFullInfo.setUserInfoStep(ExpertUserInfoStepEnum.INFORMATION_TO_BE_SUBMITTED.getKey());
        }
        if (applyResult) {
            // 补充专家 专家角色 TODO
//            SysRole sysRole = roleService.getOne(Wrappers.lambdaQuery(SysRole.class)
//                    .eq(SysRole::getCode, SysRoleEnum.EXPERT.getKey()));
//            if (Objects.nonNull(sysRole)) {
//                Long roleId = sysRole.getRoleId();
//                LambdaQueryWrapper<SysUsersRoles> eq = Wrappers.lambdaQuery(SysUsersRoles.class)
//                        .eq(SysUsersRoles::getUserId, expertUserFullInfo.getUserId())
//                        .eq(SysUsersRoles::getRoleId, sysRole.getRoleId());
//                SysUsersRoles one = userRoleService.getOne(eq);
//                if (Objects.isNull(one)) {
//                    SysUsersRoles sysUsersRolesSaveRecord = new SysUsersRoles();
//                    sysUsersRolesSaveRecord.setUserId(expertUserFullInfo.getUserId());
//                    sysUsersRolesSaveRecord.setRoleId(roleId);
//                    userRoleService.saveAndReloadRoleListForToken(Collections.singletonList(sysUsersRolesSaveRecord), false);
//                }
//            }
        }
        iExpertUserFullInfoService.saveOrUpdate(expertUserFullInfo);
    }


    @Override
    public List<ExpertFullInfoAllDTO> listExpertUserFullInfoAll(List<Long> userIds) {
        List<ExpertUserFullInfo> expertUserFullInfos = iExpertUserFullInfoService.listByUserIds(userIds);
        // 所有专家标签字段
        List<ExpertTag> expertTagList = iExpertTagService.list(Wrappers.lambdaQuery(ExpertTag.class)
                .in(ExpertTag::getUserId, userIds));
        Map<Long, List<ExpertTag>> expertTagListMap = CollUtils.group(expertTagList, ExpertTag::getUserId);
        // 所有专家字典字段
        List<ExpertDictionary> expertDictionaryList = iExpertDictionaryService.list(Wrappers.lambdaQuery(ExpertDictionary.class)
                .in(ExpertDictionary::getUserId, userIds));
        Map<Long, List<ExpertDictionary>> expertDictMap = CollUtils.group(expertDictionaryList, ExpertDictionary::getUserId);
        // 专家履职意向列表
        List<ExpertIntentionWorkRegion> expertIntentionWorkRegionList = iExpertIntentionWorkRegionService
                .list(Wrappers.lambdaQuery(ExpertIntentionWorkRegion.class)
                        .in(ExpertIntentionWorkRegion::getUserId, userIds));
        Map<Long, List<ExpertIntentionWorkRegion>> intentionRegionMap = CollUtils.group(expertIntentionWorkRegionList, ExpertIntentionWorkRegion::getUserId);
        // 专家履职意向申请列表
        List<ExpertMetaApply> expertMetaApplyList = iExpertMetaApplyService.list(Wrappers.lambdaQuery(ExpertMetaApply.class)
                .in(ExpertMetaApply::getUserId, userIds)
                .eq(ExpertMetaApply::getApplyStatus, ExpertApplyStatusEnum.PENDING_REVIEW.getKey())
                .in(ExpertMetaApply::getApplyType, ExpertApplyTypeEnum.EXPERT_INTENTION_JOIN.getKey(), ExpertApplyTypeEnum.EXPERT_INTENTION_LEAVE.getKey()));
        Map<Long, List<ExpertMetaApply>> metaApplyMap = CollUtils.group(expertMetaApplyList, ExpertMetaApply::getUserId);
        // 所有专家回避单位
        List<ExpertAvoidCompany> expertAvoidCompanyList = iExpertAvoidCompanyService.list(Wrappers.lambdaQuery(ExpertAvoidCompany.class)
                .in(ExpertAvoidCompany::getUserId, userIds));
        Map<Long, List<ExpertAvoidCompany>> avoidInfoMap = CollUtils.group(expertAvoidCompanyList, ExpertAvoidCompany::getUserId);
        return expertUserFullInfos.stream().map(w -> ExpertUserInfoAssembler.buildExpertFullInfoAllDTO(w,
                expertTagListMap.getOrDefault(w.getUserId(), Collections.emptyList()),
                expertDictMap.getOrDefault(w.getUserId(), Collections.emptyList()),
                intentionRegionMap.getOrDefault(w.getUserId(), Collections.emptyList()),
                metaApplyMap.getOrDefault(w.getUserId(), Collections.emptyList()),
                avoidInfoMap.getOrDefault(w.getUserId(), Collections.emptyList())))
                .collect(Collectors.toList());
    }

}
