package com.kingdee.shr.recuritment.service.offer;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.base.attachment.AttachmentInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.beisen.utils.HRAttachmentUploadUtil;
import com.kingdee.eas.custom.recuritment.utils.AttachmentUtils;
import com.kingdee.shr.attachment.ISHRAttachmentExt;
import com.kingdee.shr.attachment.SHRAttachmentExtCollection;
import com.kingdee.shr.attachment.SHRAttachmentExtFactory;
import com.kingdee.shr.attachment.SHRAttachmentExtInfo;
import com.kingdee.shr.preentry.*;
import com.kingdee.shr.recuritment.*;
import com.kingdee.shr.recuritment.app.service.ResumeSyncToPEPersonService;
import com.kingdee.shr.recuritment.utils.RecBaseUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 简历同步预入职档案
 * @Date 2025/9/18 19:53
 * @Created by 59279
 */
public class ResumeSyncToPEPersonServiceEx extends ResumeSyncToPEPersonService {
    private static Logger logger = LoggerFactory.getLogger(ResumeSyncToPEPersonServiceEx.class);

    /**
     * 同步简历信息到预入职人员信息中
     *
     * @param ctx    上下文对象
     * @param person 预入职人员信息对象
     * @param resume 简历基本信息对象
     */
    @Override
    public void syncResumeToPEPerson(Context ctx, PreEntryPersonInfo person, ResumeBaseRecInfo resume) {
        super.syncResumeToPEPerson(ctx, person, resume);
        //简历id
        try {
            String resumeId = resume.getId().toString();
            IRecApproval iRecApproval = RecApprovalFactory.getLocalInstance(ctx);
            RecApprovalCollection recApprovalCollection =
                    iRecApproval.getRecApprovalCollection("where resumeBaseRec = '" + resumeId + "'");
            if (!recApprovalCollection.isEmpty()) {
                //获取招聘审批信息
                RecApprovalInfo recApprovalInfo = recApprovalCollection.get(0);
                //获取背调信息
                String personalScreening = recApprovalInfo.getPersonalScreening();
                if (StringUtils.isNotBlank(personalScreening)) {
                    person.put("personalScreening", personalScreening);
                    SelectorItemCollection updateSic = new SelectorItemCollection();
                    updateSic.add("personalScreening");
                    //更新背调信息
                    PreEntryPersonFactory.getLocalInstance(ctx).updatePartial(person, updateSic);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        if (person != null) {
            logger.error("***************************同步简历附件到预入职开始***************************");
            //预入职id
            String id = person.getId().toString();
            //简历id
            String resumeId = resume.getId().toString();
            try {
                //删除简历附件
                String filter = "where boid = '" + id + "' and (attachment.name like '%标准简历%' or attachment.name like '%面试评价%' or attachment.name like '%原始简历%' or attachment.name like '%背景调查审批表%' )";
                AttachmentUtils.deleteAttachment(ctx, filter);
                ISHRAttachmentExt ishrAttachmentExt = SHRAttachmentExtFactory.getLocalInstance(ctx);
                SHRAttachmentExtCollection shrAttachmentExtCollection =
                        ishrAttachmentExt.getSHRAttachmentExtCollection("select attachment.* where boID = '" + resumeId + "'");
                String uipk = "hr.preentry.resume.base";//预入职人员_基本信息
                for (int i = 0; i < shrAttachmentExtCollection.size(); i++) {
                    SHRAttachmentExtInfo shrAttachmentExtInfo = shrAttachmentExtCollection.get(i);
                    AttachmentInfo attachmentInfo = shrAttachmentExtInfo.getAttachment();
                    if (attachmentInfo == null || attachmentInfo.getFile() == null) {
                        continue;
                    }
                    String type = attachmentInfo.getSimpleName();
                    String fileName = attachmentInfo.getName();
                    byte[] file = attachmentInfo.getFile();
                    //上传附件
                    HRAttachmentUploadUtil.uploadAttachment(ctx, file, type, fileName, id, uipk, "null0");
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            logger.error("***************************同步简历附件到预入职结束***************************");
        }
    }
}
