package com.ningdatech.pmapi.expert.manage;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.file.entity.vo.result.AttachFileVo;
import com.ningdatech.file.service.FileService;
import com.ningdatech.pmapi.expert.assembler.ExpertInfoCmdAssembler;
import com.ningdatech.pmapi.expert.assembler.ExpertUserInfoAssembler;
import com.ningdatech.pmapi.expert.constant.ExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.constant.ExpertUserInfoStepEnum;
import com.ningdatech.pmapi.expert.entity.ExpertUserFullInfo;
import com.ningdatech.pmapi.expert.helper.ExpertInfoCommonHelper;
import com.ningdatech.pmapi.expert.helper.ExpertManageHelper;
import com.ningdatech.pmapi.expert.model.*;
import com.ningdatech.pmapi.expert.model.cmd.ExpertFullInfoSaveCmd;
import com.ningdatech.pmapi.expert.model.cmd.ExpertRecommendProofSaveCmd;
import com.ningdatech.pmapi.expert.model.dto.ExpertDictionaryDTO;
import com.ningdatech.pmapi.expert.model.dto.ExpertFullInfoAllDTO;
import com.ningdatech.pmapi.expert.model.req.ExpertUserBasicInfoSubmitRequest;
import com.ningdatech.pmapi.expert.model.vo.ExpertFullInfoVO;
import com.ningdatech.pmapi.expert.service.ExpertInfoService;
import com.ningdatech.pmapi.expert.service.IExpertUserFullInfoService;
import com.ningdatech.pmapi.meta.constant.DictExpertInfoTypeEnum;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

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

/**
 * @author liuxinxin
 * @date 2023/2/23 上午8:55
 */

@Component
@RequiredArgsConstructor
public class ExpertManage {

    private final ExpertManageHelper expertManageHelper;
    private final IExpertUserFullInfoService iExpertUserFullInfoService;
    private final ExpertInfoService expertInfoService;
    private final ExpertInfoCommonHelper expertInfoCommonHelper;
    private final FileService fileService;
    private final ExpertUserInfoAssembler expertUserInfoAssembler;


    /**
     * 填写基本信息,只有专家自己可用
     *
     * @param request
     */
    public void expertBasicInfoSubmit(ExpertUserBasicInfoSubmitRequest request) {
        // 用户id
        Long userId = LoginUserUtil.getUserId();
        ExpertBasicInfo basicInfo = request.getBasicInfo();
        // 校验区域编码合法性 校验履职意向编码合法性
        ExpertRegionInfo expertRegionInfo = basicInfo.getExpertRegionInfo();
        expertManageHelper.expertRegionInfoCheck(expertRegionInfo);
        List<ExpertRegionInfo> expertIntentionWorkRegions = basicInfo.getExpertIntentionWorkRegions();
        for (ExpertRegionInfo expertIntentionWorkRegion : expertIntentionWorkRegions) {
            expertManageHelper.expertRegionInfoCheck(expertIntentionWorkRegion);
        }
        ExpertEduInfo eduInfo = request.getEduInfo();
        ExpertJobInfo jobInfo = request.getJobInfo();
        ExpertProfessionalInfo professionalInfo = request.getProfessionalInfo();
        // 校验标签字段
        expertManageHelper.tagFieldCheck(professionalInfo, basicInfo);
        // 校验字典字段
        expertManageHelper.dictionaryFieldCheck(basicInfo, eduInfo, jobInfo);
        // 判断专家提交状态，判断是否可以进行此操作
        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(userId);

        boolean submitBasicInfoStatusEnable = Objects.isNull(expertUserFullInfo)
                || (ExpertAccountStatusEnum.APPLYING.getKey().equals(expertUserFullInfo.getExpertAccountStatus())
                && !ExpertUserInfoStepEnum.EVIDENCE_HAS_BEEN_SUBMITTED.getKey().equals(expertUserFullInfo.getUserInfoStep()));
        if (submitBasicInfoStatusEnable) {
            // 新建 保存
            ExpertFullInfoSaveCmd expertFullInfoSaveCmd = ExpertInfoCmdAssembler
                    .buildExpertFullInfoSaveCmd(userId, basicInfo, eduInfo, jobInfo, professionalInfo);
            expertInfoService.saveExpertInfo(expertFullInfoSaveCmd);
        }

        // 推荐证明材料
        List<DictionaryFieldInfo> recommendedWay = request.getRecommendedWay();
        // 推荐方式
        List<FileBasicInfo> recommendProofFile = request.getRecommendProofFile();
        expertRecommendProofSubmit(recommendedWay, recommendProofFile);
    }


    public void expertRecommendProofSubmit(List<DictionaryFieldInfo> recommendedWay, List<FileBasicInfo> recommendProofFile) {
        // 用户id
        Long expertUserId = LoginUserUtil.getUserId();
        ExpertUserFullInfo expertUserFullInfo = iExpertUserFullInfoService.getByUserId(expertUserId);
        // 判断专家状态，是否可以进行证明材料提交
        if (Objects.isNull(expertUserFullInfo)
                || !ExpertAccountStatusEnum.APPLYING.getKey().equals(expertUserFullInfo.getExpertAccountStatus())
                || ExpertUserInfoStepEnum.EVIDENCE_HAS_BEEN_SUBMITTED.getKey().equals(expertUserFullInfo.getUserInfoStep())) {
            throw new BizException("当前状态无法提交证明材料");
        }

        // 证明材料提交
        ExpertRecommendProofSaveCmd expertRecommendProofSaveCmd = new ExpertRecommendProofSaveCmd();
        if (CollectionUtils.isNotEmpty(recommendProofFile)) {
            expertRecommendProofSaveCmd.setRecommendationProofFileIdList(recommendProofFile.stream().map(FileBasicInfo::getFileId).collect(Collectors.toList()));
        }
        List<ExpertDictionaryDTO> recommendedWayDictionaryDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(recommendedWay)) {
            recommendedWayDictionaryDTOList = recommendedWay.stream().map(r -> {
                ExpertDictionaryDTO expertDictionaryDTO = new ExpertDictionaryDTO();
                expertDictionaryDTO.setDictionaryCode(r.getDictionaryCode());
                expertDictionaryDTO.setExpertInfoField(DictExpertInfoTypeEnum.RECOMMENDED_WAY.getKey());
                return expertDictionaryDTO;
            }).collect(Collectors.toList());
        }
        expertRecommendProofSaveCmd.setRecommendedWay(recommendedWayDictionaryDTOList);
        expertRecommendProofSaveCmd.setUserId(expertUserId);
        expertInfoService.expertRecommendProofSave(expertRecommendProofSaveCmd);
    }

    public ExpertFullInfoVO getExpertFullInfoDetail(Long expertUserId) {
        Long currentUserId = LoginUserUtil.getUserId();
        if (Objects.isNull(expertUserId)) {
            expertUserId = currentUserId;
        }
        ExpertFullInfoAllDTO expertUserFullInfoAll = expertInfoService.getExpertUserFullInfoAll(expertUserId);
        if (Objects.isNull(expertUserFullInfoAll)) {
            return null;
        }
        List<Long> fileIdList = expertInfoCommonHelper.getExpertFileIdList(expertUserFullInfoAll);
        List<AttachFileVo> attachFiles = fileService.getByIds(fileIdList);
        return expertUserInfoAssembler.buildExpertFullInfoVO(attachFiles, expertUserFullInfoAll);
    }
}
