package com.vetti.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.vetti.common.enums.StageEnum;
import com.vetti.common.utils.SecurityUtils;
import com.vetti.hotake.domain.HotakeRolesApplyInfo;
import com.vetti.hotake.domain.HotakeRolesApplyOperRecord;
import com.vetti.hotake.service.IHotakeRolesApplyInfoService;
import com.vetti.hotake.service.IHotakeRolesApplyOperRecordService;
import com.vetti.web.entity.dto.HotakePersonalStatisticalAnalysisDataDto;
import com.vetti.web.service.IHotakeCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

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

/**
 * 通用业务逻辑 信息 服务实现类
 *
 * @author WangXiangShun
 * @since 2025-08-27
 */
@Slf4j
@Service
public class HotakeCommonServiceImpl implements IHotakeCommonService {


    @Autowired
    private IHotakeRolesApplyInfoService applyInfoService;

    @Autowired
    private IHotakeRolesApplyOperRecordService operRecordService;

    /**
     * 个人信息-统计分析数据
     * @return
     */
    public HotakePersonalStatisticalAnalysisDataDto getCandidateStatisticalAnalysisData(){
        HotakePersonalStatisticalAnalysisDataDto dataDto = new HotakePersonalStatisticalAnalysisDataDto();
        HotakeRolesApplyInfo queryApplyInfo = new HotakeRolesApplyInfo();
        queryApplyInfo.setCandidateId(SecurityUtils.getUserId());
        List<HotakeRolesApplyInfo> rolesApplyInfoList = applyInfoService.selectHotakeRolesApplyInfoList(queryApplyInfo);
        dataDto.setJobApplicationsNum(rolesApplyInfoList.size());

        Integer totalInterviewsNum = 0;
        Integer offersReceivedNum = 0;

        //获取岗位申请Id
        if(CollectionUtil.isNotEmpty(rolesApplyInfoList)){
            List<Long> applyIds = rolesApplyInfoList.stream().map(HotakeRolesApplyInfo::getId).toList();
            //通过申请Id查询申请操作记录数据
            HotakeRolesApplyOperRecord queryOperRecord = new HotakeRolesApplyOperRecord();
            queryOperRecord.setApplyIds(applyIds);
            List<HotakeRolesApplyOperRecord> applyOperRecordList = operRecordService.selectHotakeRolesApplyOperRecordList(queryOperRecord);
            if(CollectionUtil.isNotEmpty(applyOperRecordList)){
                List<HotakeRolesApplyOperRecord> applyOperRecords  = applyOperRecordList.stream().filter(e-> StageEnum.INTERVIEW.getCode().
                        equals(e.getApplyStage())).toList();
                if (CollectionUtil.isNotEmpty(applyOperRecords)) {
                    totalInterviewsNum = applyOperRecords.size();
                }
                List<HotakeRolesApplyOperRecord> applyOperRecords1  = applyOperRecordList.stream().filter(e-> StageEnum.OFFER.getCode().
                        equals(e.getApplyStage())).toList();
                if (CollectionUtil.isNotEmpty(applyOperRecords1)) {
                    offersReceivedNum = applyOperRecords1.size();
                }
            }
        }
        dataDto.setTotalInterviewsNum(totalInterviewsNum);
        dataDto.setOffersReceivedNum(offersReceivedNum);
        dataDto.setTimeSavedNum(7);
        dataDto.setVisibilityIncreaseNum(32);
        return dataDto;
    }

//    public static void main(String[] args){
//        String str = "roleId=139&roleApplyId=21&userName=aioteam@qq.com&userType=candidate&token=";
//        String base64 = Base64Utils.encodeToString(str.getBytes());
//        System.out.println(base64);
//
//
//    }
}
