package com.ningdatech.pmapi.expert.helper;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.file.entity.vo.result.AttachFileVo;
import com.ningdatech.file.service.FileService;
import com.ningdatech.pmapi.common.model.FileBasicInfo;
import com.ningdatech.pmapi.expert.assembler.ExpertUserInfoAssembler;
import com.ningdatech.pmapi.expert.constant.ExpertSensitiveFieldTypeEnum;
import com.ningdatech.pmapi.expert.constant.ExpertUserInfoSensitiveFieldEnum;
import com.ningdatech.pmapi.expert.entity.ExpertIntentionWorkRegion;
import com.ningdatech.pmapi.expert.model.*;
import com.ningdatech.pmapi.expert.model.bo.ExpertInfoSensitiveFieldCheckBO;
import com.ningdatech.pmapi.expert.model.dto.ExpertFullInfoAllDTO;
import com.ningdatech.pmapi.expert.model.dto.ExpertUserFullInfoDTO;
import com.ningdatech.pmapi.expert.service.ExpertInfoService;
import com.ningdatech.pmapi.expert.service.IExpertIntentionWorkRegionService;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

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

/**
 * @author liuxinxin
 * @date 2022/7/28 上午10:09
 */
@Component
@RequiredArgsConstructor
public class ExpertInfoSensitiveFieldModifyCheckHelper {

    private final ExpertInfoService expertInfoService;
    private final ExpertInfoCommonHelper expertInfoCommonHelper;
    private final FileService fileService;
    private final ExpertUserInfoAssembler expertUserInfoAssembler;
    private final IExpertIntentionWorkRegionService iExpertIntentionWorkRegionService;


    public ExpertInfoSensitiveFieldCheckBO expertInfoSensitiveFieldModifyCheck(ExpertBasicInfo applyBasicInfo
            , ExpertEduInfo applyEduInfo, ExpertJobInfo applyJobInfo, ExpertProfessionalInfo applyProfessionalInfo, Long expertUserId) {

        ExpertFullInfoAllDTO expertUserFullInfoAll = expertInfoService.getExpertUserFullInfoAll(expertUserId);
        if (Objects.isNull(expertUserFullInfoAll)) {
            throw new BizException("当前账号尚未有专家相关信息,无法更新");
        }
        List<AttachFileVo> attachFiles = fileService.getByIds(expertInfoCommonHelper.getExpertFileIdList(expertUserFullInfoAll));

        ExpertUserFullInfoDTO expertUserInfoDTO = expertUserFullInfoAll.getExpertUserInfoDTO();
        // 字典字典段map
        Map<String, List<DictionaryFieldInfo>> dictionaryFieldInfoMap =
                expertUserInfoAssembler.buildDictInfoMap(expertUserFullInfoAll.getExpertDictionaryList());
        // 专家文件资料map
        Map<Long, FileBasicInfo> fileBasicInfoMap = ExpertUserInfoAssembler.buildFileBasicInfoMap(attachFiles);
        // 专家标签字段map
        Map<String, List<TagFieldInfo>> tagFieldInfoMap = expertUserInfoAssembler.buildTagFieldInfoMap(expertUserFullInfoAll.getExpertTagList());
        ExpertBasicInfo originalBasicInfo = expertUserInfoAssembler
                .buildExpertBasicInfo(expertUserInfoDTO, dictionaryFieldInfoMap
                        , tagFieldInfoMap, expertUserFullInfoAll.getExpertIntentionWorkRegionInfo(), fileBasicInfoMap);

        ExpertJobInfo originalJobInfo = ExpertUserInfoAssembler.buildExpertJobInfo(expertUserInfoDTO, dictionaryFieldInfoMap);
        ExpertProfessionalInfo originalProfessionalInfo = ExpertUserInfoAssembler.buildExpertProfessionalInfo(expertUserInfoDTO
                , dictionaryFieldInfoMap, tagFieldInfoMap, fileBasicInfoMap, expertUserFullInfoAll.getExpertAvoidCompanyList());


        // 敏感字段 手机号/电子邮箱/专家层级/履职意向/工作单位/单位法人编号/行政职级/职称级别/擅长方向/技术专长/行业领域/其他标签
        // 敏感信息字段修改
        List<SensitiveModifySegment> sensitiveModifySegmentList = new ArrayList<>();
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.phone_no
                        , originalBasicInfo.getPhoneNo(), applyBasicInfo.getPhoneNo()));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.email
                        , originalBasicInfo.getEmail(), applyBasicInfo.getEmail()));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.company
                        , originalJobInfo.getCompany(), applyJobInfo.getCompany()));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.company_uniq_code
                        , originalJobInfo.getCompanyUniqCode(), applyJobInfo.getCompanyUniqCode()));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.legal_entity_code
                        , originalJobInfo.getLegalEntityCode(), applyJobInfo.getLegalEntityCode()));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.administrative_rank
                        , originalJobInfo.getAdministrativeRank()
                        , assemblerDictionaryFieldInfoList(applyJobInfo.getAdministrativeRank(), ExpertUserInfoSensitiveFieldEnum.administrative_rank.getKey())));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.title_level
                        , originalProfessionalInfo.getTitleLevel()
                        , assemblerDictionaryFieldInfoList(applyProfessionalInfo.getTitleLevel(), ExpertUserInfoSensitiveFieldEnum.title_level.getKey())));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.good_at
                        , originalProfessionalInfo.getGoodAt()
                        , assemblerTagFieldInfoList(applyProfessionalInfo.getGoodAt(), ExpertUserInfoSensitiveFieldEnum.good_at.getKey())));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.technical_expertise
                        , originalProfessionalInfo.getTechnicalExpertise()
                        , assemblerTagFieldInfoList(applyProfessionalInfo.getTechnicalExpertise(), ExpertUserInfoSensitiveFieldEnum.technical_expertise.getKey())));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.industry_sector
                        , originalProfessionalInfo.getIndustrySector()
                        , assemblerTagFieldInfoList(applyProfessionalInfo.getIndustrySector(), ExpertUserInfoSensitiveFieldEnum.industry_sector.getKey())));
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertUserInfoSensitiveFieldEnum.other
                        , originalProfessionalInfo.getOther()
                        , assemblerTagFieldInfoList(applyProfessionalInfo.getOther(), ExpertUserInfoSensitiveFieldEnum.other.getKey())));
        // 专家层级修改
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(ExpertSensitiveFieldTypeEnum.expert_region_field, ExpertUserInfoSensitiveFieldEnum.expert_region
                        , originalBasicInfo.getExpertRegionInfo(), applyBasicInfo.getExpertRegionInfo()));
        // 履职意向修改
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(
                        ExpertSensitiveFieldTypeEnum.expert_intention_field, ExpertUserInfoSensitiveFieldEnum.expert_intention_work_region
                        , originalBasicInfo.getExpertIntentionWorkRegions(), applyBasicInfo.getExpertIntentionWorkRegions()));

        return new ExpertInfoSensitiveFieldCheckBO(sensitiveModifySegmentList);
    }

    public ExpertInfoSensitiveFieldCheckBO intentionWorkRegionsModifyCheck(Long expertUserId, List<ExpertRegionInfo> applyExpertIntentionWorkRegions) {
        List<SensitiveModifySegment> sensitiveModifySegmentList = new ArrayList<>();

        LambdaQueryWrapper<ExpertIntentionWorkRegion> expertIntentionWorkRegionEq = Wrappers.lambdaQuery(ExpertIntentionWorkRegion.class)
                .eq(ExpertIntentionWorkRegion::getUserId, expertUserId);
        List<ExpertIntentionWorkRegion> expertIntentionWorkRegions = iExpertIntentionWorkRegionService.list(expertIntentionWorkRegionEq);

        List<ExpertRegionInfo> currentExpertIntentionWorkRegions = new ArrayList<>();
        if (CollUtil.isNotEmpty(expertIntentionWorkRegions)) {
            expertIntentionWorkRegions.forEach(r -> {
                ExpertRegionInfo expertRegionInfo = new ExpertRegionInfo();
                expertRegionInfo.setRegionLevel(r.getRegionLevel());
                expertRegionInfo.setRegionCode(r.getRegionCode());
                currentExpertIntentionWorkRegions.add(expertRegionInfo);
            });
        }
        // 履职意向修改
        sensitiveModifySegmentList
                .add(new SensitiveModifySegment(
                        ExpertSensitiveFieldTypeEnum.expert_intention_field, ExpertUserInfoSensitiveFieldEnum.expert_intention_work_region
                        , currentExpertIntentionWorkRegions, applyExpertIntentionWorkRegions));

        return new ExpertInfoSensitiveFieldCheckBO(sensitiveModifySegmentList);
    }


    private List<DictionaryFieldInfo> assemblerDictionaryFieldInfoList(List<DictionaryFieldInfo> dictionaryFieldInfoList, String fieldName) {
        if (CollUtil.isNotEmpty(dictionaryFieldInfoList)) {
            dictionaryFieldInfoList.forEach(r -> r.setDictionaryFieldName(fieldName));
        }
        return dictionaryFieldInfoList;
    }

    private List<TagFieldInfo> assemblerTagFieldInfoList(List<TagFieldInfo> tagFieldInfoList, String fieldName) {
        if (CollUtil.isNotEmpty(tagFieldInfoList)) {
            tagFieldInfoList.forEach(r -> r.setTagFieldName(fieldName));
        }
        return tagFieldInfoList;
    }

}
