package com.vetti.hotake.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.vetti.common.core.domain.entity.SysUser;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.common.enums.StageEnum;
import com.vetti.common.exception.ServiceException;
import com.vetti.common.utils.MessageUtils;
import com.vetti.common.utils.SecurityUtils;
import com.vetti.hotake.domain.*;
import com.vetti.hotake.domain.dto.*;
import com.vetti.hotake.domain.dto.VcDto.*;
import com.vetti.hotake.domain.vo.HotakeRolesApplyStageVo;
import com.vetti.hotake.mapper.HotakeInitScreQuestionsReplyRecordInfoMapper;
import com.vetti.hotake.mapper.HotakeInitialScreeningQuestionsInfoMapper;
import com.vetti.hotake.mapper.HotakeRolesApplyInfoMapper;
import com.vetti.hotake.mapper.HotakeRolesInfoMapper;
import com.vetti.hotake.service.IHotakeRolesApplyInfoService;
import com.vetti.hotake.service.IHotakeRolesApplyOperRecordService;
import com.vetti.system.service.ISysUserService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 候选人岗位申请信息Service业务层处理
 * 
 * @author wangxiangshun
 * @date 2025-12-17
 */
@SuppressWarnings("all")
@Service
public class HotakeRolesApplyInfoServiceImpl extends BaseServiceImpl implements IHotakeRolesApplyInfoService
{
    @Autowired
    private HotakeRolesApplyInfoMapper hotakeRolesApplyInfoMapper;

    @Autowired
    private HotakeRolesInfoMapper hotakeRolesInfoMapper;

    @Autowired
    private IHotakeRolesApplyOperRecordService rolesApplyOperRecordService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private HotakeInitScreQuestionsReplyRecordInfoMapper initScreQuestionsReplyRecordInfoMapper;

    @Autowired
    private HotakeInitialScreeningQuestionsInfoMapper initialScreeningQuestionsInfoMapper;

    /**
     * 查询候选人岗位申请信息
     * 
     * @param id 候选人岗位申请信息主键
     * @return 候选人岗位申请信息
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeRolesApplyInfo selectHotakeRolesApplyInfoById(Long id)
    {
        HotakeRolesApplyInfo applyInfo = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoById(id);
        //查询岗位数据集合
        HotakeRolesInfo query = new HotakeRolesInfo();
        List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);
        if(applyInfo.getRoleId() != null){
            List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
            if(CollectionUtil.isNotEmpty(rolesInfos)) {
                applyInfo.setRolesInfo(rolesInfos.get(0));
            }
        }
        if(StrUtil.isNotEmpty(applyInfo.getCvTemplateJson())){
            HotakeCvInfoDto cvInfoDto = handleAnalysisCvInfo(applyInfo.getCvTemplateJson());
            applyInfo.setCvInfoDto(cvInfoDto);
        }

        HotakeRolesApplyOperRecord queryApplyOperRecord = new HotakeRolesApplyOperRecord();
        queryApplyOperRecord.setRoleApplyId(id);
        List<HotakeRolesApplyOperRecord> applyOperRecords = rolesApplyOperRecordService.selectHotakeRolesApplyOperRecordList(queryApplyOperRecord);
        applyInfo.setApplyOperRecords(applyOperRecords);

        return applyInfo;
    }

    /**
     * 查询候选人岗位申请信息列表
     * 
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return 候选人岗位申请信息
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeRolesApplyInfo> selectHotakeRolesApplyInfoList(HotakeRolesApplyInfo hotakeRolesApplyInfo)
    {
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoList(hotakeRolesApplyInfo);
        if(CollectionUtil.isNotEmpty(applyInfoList)) {

            List<SysUser> sysUserList = userService.selectUserList(new SysUser());

            List<HotakeInitScreQuestionsReplyRecordInfo> screQuestionsReplyRecordInfoList = initScreQuestionsReplyRecordInfoMapper.selectHotakeInitScreQuestionsReplyRecordInfoList(new HotakeInitScreQuestionsReplyRecordInfo());

            List<HotakeInitialScreeningQuestionsInfo> screeningQuestionsInfoList = initialScreeningQuestionsInfoMapper.selectHotakeInitialScreeningQuestionsInfoList(new HotakeInitialScreeningQuestionsInfo());
            //查询岗位数据集合
            HotakeRolesInfo query = new HotakeRolesInfo();
//            query.setRecruiterId(SecurityUtils.getUserId());
            List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);

            HotakeRolesApplyOperRecord queryApplyOperRecord = new HotakeRolesApplyOperRecord();
            List<HotakeRolesApplyOperRecord> applyOperRecords = rolesApplyOperRecordService.selectHotakeRolesApplyOperRecordList(queryApplyOperRecord);

            for(HotakeRolesApplyInfo applyInfo : applyInfoList) {
                if(applyInfo.getRoleId() != null){
                    List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(rolesInfos)) {
                        applyInfo.setRolesInfo(rolesInfos.get(0));
                    }
                }
                if(StrUtil.isNotEmpty(applyInfo.getCvTemplateJson())){
                    HotakeCvInfoDto cvInfoDto = handleAnalysisCvInfo(applyInfo.getCvTemplateJson());
                    applyInfo.setCvInfoDto(cvInfoDto);
                }
                if(CollectionUtil.isNotEmpty(applyOperRecords)) {
                    List<HotakeRolesApplyOperRecord> operRecordList = applyOperRecords.stream().filter(e->e.getRoleApplyId()!= null &&
                            e.getRoleApplyId().longValue() == applyInfo.getId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(operRecordList)) {
                        applyInfo.setApplyOperRecords(operRecordList);
                    }
                }
                if(CollectionUtil.isNotEmpty(screQuestionsReplyRecordInfoList)) {
                    List<HotakeInitScreQuestionsReplyRecordInfo> screQuestionsReplyRecordInfos = screQuestionsReplyRecordInfoList.stream().
                            filter(e->e.getRoleApplyId() != null && e.getRoleApplyId().longValue() == applyInfo.getId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(screQuestionsReplyRecordInfos)) {
                        for(HotakeInitScreQuestionsReplyRecordInfo recordInfo : screQuestionsReplyRecordInfos){
                            List<HotakeInitialScreeningQuestionsInfo> screeningQuestionsInfos = screeningQuestionsInfoList.stream().
                                    filter(e->e.getId().longValue() == recordInfo.getQuestionId().longValue()).toList();
                            if(CollectionUtil.isNotEmpty(screeningQuestionsInfos)) {
                                recordInfo.setQuestionText(screeningQuestionsInfos.get(0).getQuestionTitle());
                            }
                        }
                    }
                    applyInfo.setScreQuestionsReplyRecordInfoList(screQuestionsReplyRecordInfos);
                }
                if(applyInfo.getCandidateId() != null){
                    List<SysUser> userList = sysUserList.stream().filter(e->e.getUserId().longValue() == applyInfo.getCandidateId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(userList)) {
                        applyInfo.setCandidateUSer(userList.get(0));
                    }
                }
            }
        }
        return applyInfoList;
    }

    /**
     * 新增候选人岗位申请信息
     * 
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public HotakeRolesApplyInfo insertHotakeRolesApplyInfo(HotakeRolesApplyInfo hotakeRolesApplyInfo)
    {
        fill(FillTypeEnum.INSERT.getCode(), hotakeRolesApplyInfo);
        Long candidateId = SecurityUtils.getUserId();
        hotakeRolesApplyInfo.setCandidateId(candidateId);
        
        // 校验候选人和岗位的唯一性
        if (hotakeRolesApplyInfo.getRoleId() != null) {
            HotakeRolesApplyInfo existInfo = hotakeRolesApplyInfoMapper.selectByCandidateIdAndRoleId(candidateId, hotakeRolesApplyInfo.getRoleId());
            if (existInfo != null) {
                throw new ServiceException(MessageUtils.messageCustomize("HotakeRolesApplyInfoServiceImpl10001"));
            }
        }
        
        String fileSuffix = FileUtil.getSuffix(hotakeRolesApplyInfo.getCvFile());
        if (StrUtil.isNotEmpty(fileSuffix)) {
            fileSuffix = fileSuffix.toLowerCase();
        }
        hotakeRolesApplyInfo.setCvFileSuffix(fileSuffix);
        int resultNum = hotakeRolesApplyInfoMapper.insertHotakeRolesApplyInfo(hotakeRolesApplyInfo);
        return hotakeRolesApplyInfo;
    }

    /**
     * 修改候选人岗位申请信息
     * 
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public HotakeRolesApplyInfo updateHotakeRolesApplyInfo(HotakeRolesApplyInfo hotakeRolesApplyInfo)
    {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeRolesApplyInfo);
        Long candidateId = SecurityUtils.getUserId();
        hotakeRolesApplyInfo.setCandidateId(candidateId);
        
        // 校验候选人和岗位的唯一性（排除自身）
        if (hotakeRolesApplyInfo.getRoleId() != null && hotakeRolesApplyInfo.getId() != null) {
            HotakeRolesApplyInfo existInfo = hotakeRolesApplyInfoMapper.selectByCandidateIdAndRoleId(candidateId, hotakeRolesApplyInfo.getRoleId());
            if (existInfo != null && !existInfo.getId().equals(hotakeRolesApplyInfo.getId())) {
                throw new ServiceException(MessageUtils.messageCustomize("HotakeRolesApplyInfoServiceImpl10001"));
            }
        }
        
        String fileSuffix = FileUtil.getSuffix(hotakeRolesApplyInfo.getCvFile());
        if (StrUtil.isNotEmpty(fileSuffix)) {
            fileSuffix = fileSuffix.toLowerCase();
        }
        hotakeRolesApplyInfo.setCvFileSuffix(fileSuffix);
        int resultNum =  hotakeRolesApplyInfoMapper.updateHotakeRolesApplyInfo(hotakeRolesApplyInfo);
        HotakeRolesApplyOperRecord query = new HotakeRolesApplyOperRecord();
        query.setRoleApplyId(hotakeRolesApplyInfo.getId()).setApplyStage(hotakeRolesApplyInfo.getStage());
        List<HotakeRolesApplyOperRecord> applyOperRecords = rolesApplyOperRecordService.selectHotakeRolesApplyOperRecordList(query);
        if(CollectionUtil.isEmpty(applyOperRecords)) {
            StageEnum stageEnum = StageEnum.getByInfo(hotakeRolesApplyInfo.getStage());
            if(stageEnum != null){
                HotakeRolesApplyOperRecord hotakeRolesApplyOperRecord = new HotakeRolesApplyOperRecord();
                hotakeRolesApplyOperRecord.setApplyStage(hotakeRolesApplyInfo.getStage());
                hotakeRolesApplyOperRecord.setRoleApplyId(hotakeRolesApplyInfo.getId());
                hotakeRolesApplyOperRecord.setRoleId(hotakeRolesApplyInfo.getRoleId());
                rolesApplyOperRecordService.insertHotakeRolesApplyOperRecord(hotakeRolesApplyOperRecord);
            }
        }
        return hotakeRolesApplyInfo;
    }

    /**
     * 批量删除候选人岗位申请信息
     * 
     * @param ids 需要删除的候选人岗位申请信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeRolesApplyInfoByIds(Long[] ids)
    {
        return hotakeRolesApplyInfoMapper.deleteHotakeRolesApplyInfoByIds(ids);
    }

    /**
     * 删除候选人岗位申请信息信息
     * 
     * @param id 候选人岗位申请信息主键
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int deleteHotakeRolesApplyInfoById(Long id)
    {
        return hotakeRolesApplyInfoMapper.deleteHotakeRolesApplyInfoById(id);
    }
    /**
     * 批量新增候选人岗位申请信息
     *
     * @param hotakeRolesApplyInfoList 候选人岗位申请信息列表
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int batchInsertHotakeRolesApplyInfo(List<HotakeRolesApplyInfo> hotakeRolesApplyInfoList){
        return hotakeRolesApplyInfoMapper.batchInsertHotakeRolesApplyInfo(hotakeRolesApplyInfoList);
    }


    /**
     * 简历解析数据
     *
     * @param contents
     * @return
     */
    private HotakeCvInfoDto handleAnalysisCvInfo(String contents) {
        try {
            HotakeCvInfoDto cvInfoDto = JSONUtil.toBean(contents, HotakeCvInfoDto.class);
            //数据添加默认值
            if(cvInfoDto != null){
                if(StrUtil.isEmpty(cvInfoDto.getName())){
                    cvInfoDto.setName("-");
                }
                if(StrUtil.isEmpty(cvInfoDto.getPhone())){
                    cvInfoDto.setPhone("-");
                }
                if(StrUtil.isEmpty(cvInfoDto.getEmail())){
                    cvInfoDto.setEmail("-");
                }
                if(StrUtil.isEmpty(cvInfoDto.getPosition())){
                    cvInfoDto.setPosition("-");
                }
                if(StrUtil.isEmpty(cvInfoDto.getLocation())){
                    cvInfoDto.setLocation("-");
                }
                if (StrUtil.isEmpty(cvInfoDto.getExperienceYear())){
                    cvInfoDto.setExperienceYear("-");
                }
                if(StrUtil.isEmpty(cvInfoDto.getAbout())){
                    cvInfoDto.setAbout("-");
                }
                if (StrUtil.isEmpty(cvInfoDto.getCurrentWork())){
                    cvInfoDto.setCurrentWork("-");
                }
                if(CollectionUtil.isNotEmpty(cvInfoDto.getSkillsTools())){
                    for (VcSkillsToolsDto toolsDto :cvInfoDto.getSkillsTools()) {
                        if(StrUtil.isEmpty(toolsDto.getContent())){
                            toolsDto.setContent("-");
                        }
                    }
                }
                if(CollectionUtil.isNotEmpty(cvInfoDto.getLinks())){
                    for (VcLinksDto linksDto :cvInfoDto.getLinks()) {
                        if(StrUtil.isEmpty(linksDto.getContent())){
                            linksDto.setContent("-");
                        }
                    }
                }

                if(CollectionUtil.isNotEmpty(cvInfoDto.getLanguages())){
                    for (VcLanguagesDto languagesDto :cvInfoDto.getLanguages()) {
                        if(StrUtil.isEmpty(languagesDto.getContent())){
                            languagesDto.setContent("-");
                        }
                    }
                }

                if(CollectionUtil.isNotEmpty(cvInfoDto.getExperience())){
                    for (VcExperienceDto experienceDto :cvInfoDto.getExperience()) {
                        if (StrUtil.isEmpty(experienceDto.getTitle())){
                            experienceDto.setTitle("-");
                        }
                        if(StrUtil.isEmpty(experienceDto.getCompany())){
                            experienceDto.setCompany("-");
                        }
                        if(StrUtil.isEmpty(experienceDto.getLocation())){
                            experienceDto.setLocation("-");
                        }
                        if(StrUtil.isEmpty(experienceDto.getDurationStart())){
                            experienceDto.setDurationStart("-");
                        }
                        if(StrUtil.isEmpty(experienceDto.getDurationEnd())){
                            experienceDto.setDurationEnd("-");
                        }
                        if (CollectionUtil.isNotEmpty(experienceDto.getDescription())){
                            for(VcExperienceDescriptionDto descriptionDto :experienceDto.getDescription()){
                                if(StrUtil.isEmpty(descriptionDto.getContent())){
                                    descriptionDto.setContent("-");
                                }
                            }
                        }
                    }
                }

                if(CollectionUtil.isNotEmpty(cvInfoDto.getEducation())){
                    for (VcEducationDto educationDto :cvInfoDto.getEducation()) {
                        if (StrUtil.isEmpty(educationDto.getDegree())){
                            educationDto.setDegree("-");
                        }
                        if(StrUtil.isEmpty(educationDto.getInstitution())){
                            educationDto.setInstitution("-");
                        }
                        if(StrUtil.isEmpty(educationDto.getDurationStart())){
                            educationDto.setDurationStart("-");
                        }
                        if(StrUtil.isEmpty(educationDto.getDurationEnd())){
                            educationDto.setDurationEnd("-");
                        }
                        if(educationDto.getCertificate() != null){
                            if(StrUtil.isEmpty(educationDto.getCertificate().getFileName())){
                                educationDto.getCertificate().setFileName("-");
                            }
                            if (StrUtil.isEmpty(educationDto.getCertificate().getFileSuffix())){
                                educationDto.getCertificate().setFileSuffix("-");
                            }
                            if (StrUtil.isEmpty(educationDto.getCertificate().getFileUrl())){
                                educationDto.getCertificate().setFileUrl("-");
                            }
                            if (StrUtil.isEmpty(educationDto.getCertificate().getFileSizeShow())){
                                educationDto.getCertificate().setFileSizeShow("-");
                            }
                        }
                    }
                }
            }

            return cvInfoDto;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HotakeCvInfoDto();
    }

    /**
     * 修改候选人岗位申请信息
     *
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateBatchEditStage(List<HotakeRolesApplyStageVo> stageVoList)
    {
        int resultNum = 0;
        if(CollectionUtil.isNotEmpty(stageVoList)){
            for(HotakeRolesApplyStageVo stageVo : stageVoList){
                HotakeRolesApplyInfo hotakeRolesApplyInfo = new HotakeRolesApplyInfo();
                hotakeRolesApplyInfo.setId(stageVo.getRoleApplyId());
                hotakeRolesApplyInfo.setStage(stageVo.getStage());
                fill(FillTypeEnum.UPDATE.getCode(), hotakeRolesApplyInfo);
                resultNum =  hotakeRolesApplyInfoMapper.updateHotakeRolesApplyInfo(hotakeRolesApplyInfo);
                HotakeRolesApplyOperRecord query = new HotakeRolesApplyOperRecord();
                query.setRoleApplyId(hotakeRolesApplyInfo.getId()).setApplyStage(hotakeRolesApplyInfo.getStage());
                List<HotakeRolesApplyOperRecord> applyOperRecords = rolesApplyOperRecordService.selectHotakeRolesApplyOperRecordList(query);
                if(CollectionUtil.isEmpty(applyOperRecords)) {
                    StageEnum stageEnum = StageEnum.getByInfo(hotakeRolesApplyInfo.getStage());
                    if(stageEnum != null){
                        HotakeRolesApplyOperRecord hotakeRolesApplyOperRecord = new HotakeRolesApplyOperRecord();
                        hotakeRolesApplyOperRecord.setApplyStage(hotakeRolesApplyInfo.getStage());
                        hotakeRolesApplyOperRecord.setRoleApplyId(hotakeRolesApplyInfo.getId());
                        hotakeRolesApplyOperRecord.setRoleId(hotakeRolesApplyInfo.getRoleId());
                        rolesApplyOperRecordService.insertHotakeRolesApplyOperRecord(hotakeRolesApplyOperRecord);
                    }
                }
            }
        }
        return resultNum;
    }

    /**
     * AI简历评分和排名系统
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return
     */
    @Override
    public List<HotakeRolesApplyInfo> handleAiCvScoringRankingList(HotakeRolesApplyInfo hotakeRolesApplyInfo) {
        if(CollectionUtil.isEmpty(hotakeRolesApplyInfo.getApplyRoleIdList())){
            throw new ServiceException("Please select at least one piece of business data");
        }
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoRankingList(hotakeRolesApplyInfo);
        if(CollectionUtil.isNotEmpty(applyInfoList)) {
            //查询岗位数据集合
            HotakeRolesInfo query = new HotakeRolesInfo();
            List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);

            List<SysUser> sysUserList = userService.selectUserList(new SysUser());

            for(HotakeRolesApplyInfo applyInfo : applyInfoList) {
                if(applyInfo.getRoleId() != null){
                    List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(rolesInfos)) {
                        applyInfo.setRolesInfo(rolesInfos.get(0));
                    }
                }
                if(StrUtil.isNotEmpty(applyInfo.getCvTemplateJson())){
                    HotakeCvInfoDto cvInfoDto = handleAnalysisCvInfo(applyInfo.getCvTemplateJson());
                    applyInfo.setCvInfoDto(cvInfoDto);
                }
                if(applyInfo.getCandidateId() != null){
                    List<SysUser> userList = sysUserList.stream().filter(e->e.getUserId().longValue() == applyInfo.getCandidateId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(userList)) {
                        applyInfo.setCandidateUSer(userList.get(0));
                    }
                }
                if(StrUtil.isNotEmpty(applyInfo.getAiCvScoringRankingJson())){
                    HotakeAiCvScoringRankingDto rankingDto = JSONUtil.toBean(applyInfo.getAiCvScoringRankingJson(), HotakeAiCvScoringRankingDto.class);
                    applyInfo.setRankingDto(rankingDto);
                }
            }
        }
        return applyInfoList;
    }

    /**
     * 招聘者查看候选人匹配度
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return
     */
    @Override
    public HotakeRolesApplyInfo handleCandidateCompatibilityInfo(HotakeRolesApplyInfo hotakeRolesApplyInfo) {
        if(CollectionUtil.isEmpty(hotakeRolesApplyInfo.getApplyRoleIdList())){
            throw new ServiceException("Please select at least one piece of business data");
        }
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoCompatibilityScoreList(hotakeRolesApplyInfo);
        if(CollectionUtil.isNotEmpty(applyInfoList)) {

            HotakeRolesApplyInfo applyInfo = applyInfoList.get(0);

            HotakeRolesInfo rolesInfo = hotakeRolesInfoMapper.selectHotakeRolesInfoById(applyInfo.getRoleId());
            applyInfo.setRolesInfo(rolesInfo);

            SysUser user = userService.selectUserById(applyInfo.getCandidateId());
            applyInfo.setCandidateUSer(user);
            if(StrUtil.isNotEmpty(applyInfo.getCandidateCompatibilityJson())){
                HotakeCandidateCompatibilityDto compatibilityDto = JSONUtil.toBean(applyInfo.getCandidateCompatibilityJson(), HotakeCandidateCompatibilityDto.class);
                applyInfo.setCompatibilityDto(compatibilityDto);
            }
            return applyInfo;
        }
        return new HotakeRolesApplyInfo();
    }

    /**
     * 岗位申请数据根据当前阶段进行统计
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return
     */
    @Override
    public HotakeRolesApplyStageStatisticalDto handleHotakeRolesApplyStageStatistical(HotakeRolesApplyInfo hotakeRolesApplyInfo) {
        HotakeRolesApplyStageStatisticalDto dto = new HotakeRolesApplyStageStatisticalDto();
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoCompatibilityScoreList(hotakeRolesApplyInfo);
         Integer appliedNum = 0;
         Integer shortlistedNum = 0;
         Integer interviewNum = 0;
         Integer offerNum = 0;
         Integer hiredNum = 0;
         Integer refuseOfferNum = 0;
         Integer interviewFailNum = 0;
         Integer totalNum = 0;
        if(CollectionUtil.isNotEmpty(applyInfoList)) {
            totalNum = applyInfoList.size();
            List<HotakeRolesApplyInfo> applyInfoList1 = applyInfoList.stream().filter(e->StageEnum.APPLIED.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList1)) {
                appliedNum = applyInfoList1.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList2 = applyInfoList.stream().filter(e->StageEnum.SHORTLISTED.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList2)) {
                shortlistedNum = applyInfoList2.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList3 = applyInfoList.stream().filter(e->StageEnum.INTERVIEW.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList3)) {
                interviewNum = applyInfoList3.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList4 = applyInfoList.stream().filter(e->StageEnum.OFFER.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList4)) {
                offerNum = applyInfoList4.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList5 = applyInfoList.stream().filter(e->StageEnum.HIRED.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList5)) {
                hiredNum = applyInfoList5.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList6 = applyInfoList.stream().filter(e->StageEnum.REFUSE_OFFER.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList6)) {
                refuseOfferNum = applyInfoList6.size();
            }
            List<HotakeRolesApplyInfo> applyInfoList7 = applyInfoList.stream().filter(e->StageEnum.INTERVIEW_FAIL.getCode().equals(e.getStage())).toList();
            if(CollectionUtil.isNotEmpty(applyInfoList7)) {
                interviewFailNum = applyInfoList7.size();
            }

        }

        dto.setTotalNum(totalNum);
        dto.setAppliedNum(appliedNum);
        dto.setShortlistedNum(shortlistedNum);
        dto.setInterviewNum(interviewNum);
        dto.setOfferNum(offerNum);
        dto.setHiredNum(hiredNum);
        dto.setRefuseOfferNum(refuseOfferNum);
        dto.setInterviewFailNum(interviewFailNum);
        return dto;
    }

    /**
     * 候选人面试综合评估
     * @param hotakeRolesApplyInfo 候选人岗位申请信息
     * @return
     */
    @Override
    public HotakeRolesApplyInfo handleCandidateInterviewEvaluation(HotakeRolesApplyInfo hotakeRolesApplyInfo) {
        if(CollectionUtil.isEmpty(hotakeRolesApplyInfo.getApplyRoleIdList())){
            throw new ServiceException("Please select at least one piece of business data");
        }
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoCompatibilityScoreOneList(hotakeRolesApplyInfo);
        if(CollectionUtil.isNotEmpty(applyInfoList)) {

            HotakeRolesApplyInfo applyInfo = applyInfoList.get(0);
            if(StrUtil.isNotEmpty(applyInfo.getEvaluationJson())){
                HotakeCandidateInterviewEvaluationDto evaluationDto = JSONUtil.toBean(applyInfo.getEvaluationJson(), HotakeCandidateInterviewEvaluationDto.class);
                applyInfo.setEvaluationDto(evaluationDto);
            }
            HotakeRolesInfo rolesInfo = hotakeRolesInfoMapper.selectHotakeRolesInfoById(applyInfo.getRoleId());
            applyInfo.setRolesInfo(rolesInfo);

            SysUser user = userService.selectUserById(applyInfo.getCandidateId());
            applyInfo.setCandidateUSer(user);
            if(StrUtil.isNotEmpty(applyInfo.getCandidateCompatibilityJson())){
                HotakeCandidateCompatibilityDto compatibilityDto = JSONUtil.toBean(applyInfo.getCandidateCompatibilityJson(), HotakeCandidateCompatibilityDto.class);
                applyInfo.setCompatibilityDto(compatibilityDto);
            }
            return applyInfo;
        }
        return new HotakeRolesApplyInfo();
    }


}
