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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.shr.preentry.PreEntryPersonFactory;
import com.kingdee.shr.preentry.PreEntryPersonInfo;
import com.kingdee.shr.recuritment.RecRegisterFactory;
import com.kingdee.shr.recuritment.RecRegisterInfo;
import com.kingdee.shr.recuritment.RecRegisterStateEnum;
import com.kingdee.shr.recuritment.ResumeBaseRecInfo;
import com.kingdee.shr.recuritment.app.util.convert.ResumeConvertUtil;
import org.apache.log4j.Logger;

/**
 * @Description 简历转化工具类
 * @Date 2025/9/18 19:58
 * @Created by 59279
 */
public class ResumeConvertUtilEx extends ResumeConvertUtil {
    private static final Logger logger = Logger.getLogger(ResumeConvertUtilEx.class);

    /**
     * 根据简历信息添加推荐注册记录。
     * <p>
     * 该方法会根据传入的简历信息创建预入职人员信息和推荐注册信息，并将这些信息保存到系统中。
     * 同时还会调用服务同步简历数据到预入职人员信息中。
     * </p>
     *
     * @param ctx    上下文对象，用于获取业务服务实例
     * @param resume 简历基本信息对象，包含待处理的简历数据
     * @throws BOSException 当业务操作过程中发生异常时抛出
     */
    public static void addRecRegisterByResume(Context ctx, ResumeBaseRecInfo resume) throws BOSException {
        // 创建推荐注册信息和预入职人员信息对象
        RecRegisterInfo newRecRegister = new RecRegisterInfo();
        PreEntryPersonInfo personInfo = new PreEntryPersonInfo();

        // 设置预入职人员基本信息
        personInfo.setName(resume.getName());
        personInfo.setIdCardNO(resume.getIdentityCardNo());
        personInfo.setPassportNO(resume.getPassportNo());
        personInfo.setBirthday(resume.getBirthday());
        personInfo.setGender(resume.getGender());
        personInfo.setNativePlace(resume.getNativePlace());
        personInfo.setHighestDegree(resume.getHDegree());
        personInfo.setNationality(resume.getNational());
        personInfo.setFolk(resume.getFolk());
        personInfo.setWed(resume.getWed());
        personInfo.setPoliticalFace(resume.getPoliticalFace());
        personInfo.setJobStartDate(resume.getWorkDate());

        // 创建并设置人员信息主键
        PersonInfo person = new PersonInfo();
        String personId = BOSUuid.create(person.getBOSType()).toString();
        person.setId(BOSUuid.read(personId));
        personInfo.setPerson(person);

        try {
            // 保存预入职人员信息并获取主键
            IObjectPK personPK = PreEntryPersonFactory.getLocalInstance(ctx).addnew(personInfo);
            personInfo.setId(BOSUuid.read(personPK.toString()));

            // 设置推荐注册信息的基本字段
            String cellPhone = resume.getMobilePhone();
            String email = resume.getEmail();
            newRecRegister.setCellPhone(cellPhone);
            newRecRegister.setEmail(email);
            newRecRegister.setTalent(personInfo);
            newRecRegister.setRecRegisterState(RecRegisterStateEnum.NOREGISTER);
            newRecRegister.setName(resume.getName());

            // 设置推荐注册信息的其他属性
            BOSUuid id = BOSUuid.create((new RecRegisterInfo()).getBOSType());
            newRecRegister.setId(id);
            newRecRegister.setResumeBaseRec(resume);
            newRecRegister.setIsLatest(true);
            newRecRegister.setIsSendLogin(false);
            newRecRegister.setNeedSendEmail(false);
            newRecRegister.setNeedSendSms(false);
            newRecRegister.setNumber(System.currentTimeMillis() + "");
            newRecRegister.setRecRegisterState(RecRegisterStateEnum.NOREGISTER);

            // 保存推荐注册信息
            RecRegisterFactory.getLocalInstance(ctx).addnew(newRecRegister);

            // 调用服务同步简历信息到预入职人员信息
            ResumeSyncToPEPersonServiceEx resumeSyncToPEPersonServiceEx = new ResumeSyncToPEPersonServiceEx();
            resumeSyncToPEPersonServiceEx.syncResumeToPEPerson(ctx, newRecRegister.getTalent(), resume);
        } catch (EASBizException e) {
            logger.error(e.getMessage());
            throw new BOSException(e.getMessage());
        }
    }

}
