package cn.com.poc.human_resources.scheduler;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.utils.DocumentLoad;
import cn.com.poc.human_resources.entity.BizHumanResourceResumeContentEntity;
import cn.com.poc.human_resources.entity.BizHumanResourceResumeEntity;
import cn.com.poc.human_resources.service.BizHumanResourceResumeContentService;
import cn.com.poc.human_resources.service.BizHumanResourceResumeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * AI人力-定时任务
 *
 * @author alex.yao
 * @date 2025/12/24
 */
@Component
public class HumanResourceScheduler {

    @Resource
    private BizHumanResourceResumeService bizHumanResourceResumeService;

    @Resource
    private BizHumanResourceResumeContentService bizHumanResourceResumeContentService;

    /**
     * 简历内容补全
     * 对 biz_human_resource_resume_content 内容补全，30分钟一次，用于第一次发布
     */
    @Scheduled(fixedDelay = 10000 * 30)
    public void resumeContentComplete() throws Exception {
        BizHumanResourceResumeEntity bizHumanResourceResumeEntity = new BizHumanResourceResumeEntity();
        bizHumanResourceResumeEntity.setIsDeleted(CommonConstant.IsDeleted.N);
        List<BizHumanResourceResumeEntity> resourceResumeEntities =
                bizHumanResourceResumeService.findByExample(bizHumanResourceResumeEntity, null);
        if (resourceResumeEntities == null) {
            return;
        }

        BizHumanResourceResumeContentEntity bizHumanResourceResumeContentEntity =
                new BizHumanResourceResumeContentEntity();
        bizHumanResourceResumeContentEntity.setIsDeleted(CommonConstant.IsDeleted.N);
        List<BizHumanResourceResumeContentEntity> resourceResumeContentEntities =
                bizHumanResourceResumeContentService.findByExample(bizHumanResourceResumeContentEntity, null);

        List<BizHumanResourceResumeContentEntity> saveResumeContentEntities = new ArrayList<>();


        for (BizHumanResourceResumeEntity resourceResumeEntity : resourceResumeEntities) {
            Long resumeId = resourceResumeEntity.getId();
            Long positionId = resourceResumeEntity.getPosition();

            boolean match = resourceResumeContentEntities.stream()
                    .anyMatch(v -> v.getResumeId().equals(resumeId) && v.getPositionId().equals(positionId));
            if (!match) {
                String fileUrl = resourceResumeEntity.getFileUrl();
                File file = DocumentLoad.downloadURLDocument(fileUrl);
                String resumeContent = DocumentLoad.documentToText(file);
                BizHumanResourceResumeContentEntity saveEntity = new BizHumanResourceResumeContentEntity();
                saveEntity.setResumeId(resumeId);
                saveEntity.setPositionId(positionId);
                saveEntity.setResumeContent(resumeContent);
                saveResumeContentEntities.add(saveEntity);
            }
        }

        if (CollectionUtils.isNotEmpty(saveResumeContentEntities)) {
            for (BizHumanResourceResumeContentEntity saveResumeContentEntity : saveResumeContentEntities) {
                bizHumanResourceResumeContentService.save(saveResumeContentEntity);
            }
        }
    }

}
