package nckd.jxccl.hr.psms.plugin.operate.initial;

import com.google.common.collect.Lists;
import kd.bos.common.enums.EnableEnum;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.enums.AppraisalResultEnum;
import nckd.jxccl.base.common.enums.psms.JobSeqEnum;
import nckd.jxccl.base.common.exception.ValidationException;
import nckd.jxccl.base.common.utils.DateUtil;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;
import nckd.jxccl.base.pm.helper.PerformanceManagerHelper;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import nckd.jxccl.hr.psms.common.bo.PositionAppointmentBO;
import nckd.jxccl.hr.psms.helper.PositionFileHelper;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * 人员初定操作插件基类
 * @author W.Y.C
 * @date 2025/9/15
 * @version 1.0
 */
public abstract class BaseInitialOperationPlugIn extends AbstractOperationServicePlugIn {
    
    protected final static Log logger = LogFactory.getLog(BaseInitialOperationPlugIn.class);

    public List<BaseInitialData> baseInitialData = Lists.newArrayList();

    /**
     * 基本信息数据对象
     */
    protected static class BaseInitialData {
        /** 开始时间 */
        Date beginDate;
        /** 备注 */
        String causeRemark;
        /** 总积分 */
        BigDecimal allSumScore;
        /** 聘任年限 */
        Integer employmentYears;
        /** 职称名称 */
        String rankName;
        /** 技能名称 */
        String jobStatusName;
        /** 员工 */
        DynamicObject person;
        /** 岗位 */
        DynamicObject positionHr;
        /** 所属公司 */
        DynamicObject company;
        /** 部门 */
        DynamicObject dep;
        /** 职位序列 */
        DynamicObject jobSeq;
        /** 职位序列（转换后） */
        DynamicObject convertJobSeq;
        /** 职位序列-编码 */
        String jobSeqNumber;
        /** 职位序列-名称 */
        String jobSeqName;
        /** 职位序列枚举 */
        JobSeqEnum jobSeqEnum;
        /** 职称等级 */
        DynamicObject proTitleLevel;
        /** 技能等级 */
        DynamicObject ocpQualLevel;
        /** 学历 */
        DynamicObject diploma;
        /** 任职经历 */
        DynamicObject empPosOrgRel;
        /** 本次入集团时间 */
        Date joinComDate;
        /** 上年度绩效考核结果*/
        DynamicObject lastYearAppraisalResult;
        /** 降级数 */
        int downgradeNum;
        /** 上年度绩效考核结果枚举 */
        AppraisalResultEnum lastYearAppraisalResultEnum;
        /** 是否优秀生 */
        boolean isExcellent;
    }

    /**
     * 积分数据
     */
    protected static class ScoreData {
        BigDecimal educationScore;
        BigDecimal proTitleScore;
        BigDecimal perOcpQualScore;
        DynamicObject dbProTitleLevel;
        DynamicObject dbOcpQualLevel;
    }

    /**
     * 提取基本信息（支持批量）
     * @param initialData 页面数据
     * @return: java.util.List<nckd.jxccl.hr.psms.plugin.operate.initial.BaseInitialOperationPlugIn.BaseInitialData>
     * @author W.Y.C
     * @date: 2025/10/12 14:20
     */
    protected List<BaseInitialData> extractBasicInfo(DynamicObject initialData) {
        logger.info("【职位体系】-开始提取基本信息");
        List<BaseInitialData> dataList = new ArrayList<>();
        if(initialData.containsProperty(FormConstant.NCKD_ENTRYENTITY)){
            DynamicObjectCollection dynamicObjectCollection = initialData.getDynamicObjectCollection(FormConstant.NCKD_ENTRYENTITY);
            logger.info("【职位体系】-检测到批量初定数据，条数: {}", dynamicObjectCollection.size());
            for (int i = 0; i < dynamicObjectCollection.size(); i++) {
                DynamicObject dynamicObject = dynamicObjectCollection.get(i);
                logger.info("【职位体系】-处理第 {} 条数据", i+1);
                //批量初定
                BaseInitialData data = getBaseInitialData(dynamicObject);
                dataList.add(data);
                baseInitialData.add(data);
            }
        }else{
            logger.info("【职位体系】-处理单条初定数据");
            BaseInitialData data = getBaseInitialData(initialData);
            dataList.add(data);
            baseInitialData.add(data);
        }
        logger.info("【职位体系】-基本信息提取完成，共提取 {} 条数据", dataList.size());
        return dataList;
    }

    @NotNull
    private BaseInitialData getBaseInitialData(DynamicObject initialData) {
        BaseInitialData data = new BaseInitialData();
        logger.info("【职位体系】-开始获取基础数据");

        data.beginDate = initialData.getDate(PositionStructureConstant.NCKD_BEGINDATE);
        data.causeRemark = initialData.getString(PositionStructureConstant.KEY_NCKD_CAUSEREMARK);
        data.person = initialData.getDynamicObject(PositionStructureConstant.NCKD_PERSON);

        PositionAppointmentBO positionAppointment = PositionFileHelper.positionAppointmentQuery(data.person.getLong(FormConstant.ID_KEY), data.beginDate);
        logger.info("【职位体系】-获取人员任职信息完成，personId={}", data.person.getLong(FormConstant.ID_KEY));
        
        // 学历
        DynamicObject perEduExp = positionAppointment.getPerEduExp();
        if (perEduExp != null) {
            // 学历
            long diplomaId = perEduExp.getLong(String.join(".", FormConstant.EDUCATION_KEY, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取学历信息，diplomaId={}", diplomaId);
            if(diplomaId > 0) {
                DynamicObject diploma = BusinessDataServiceHelper.newDynamicObject(PositionStructureConstant.HBSS_DIPLOMA);
                diploma.set(FormConstant.ID_KEY, diplomaId);
                data.diploma = diploma;
            }
        } else {
            logger.info("【职位体系】-无学历信息");
        }
        
        // 职称信息
        DynamicObject perProTitle = positionAppointment.getPerProTitle();
        if (perProTitle != null) {
            data.rankName = perProTitle.getString(String.join(".", FormConstant.PROFESSIONAL_KEY, FormConstant.NAME_KEY));
            logger.info("【职位体系】-获取职称信息，rankName={}", data.rankName);
            
            // 职称等级
            long perProTitleId = perProTitle.getLong(String.join(".", FormConstant.PROLEVEL_KEY, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取职称等级，perProTitleId={}", perProTitleId);
            if(perProTitleId > 0) {
                DynamicObject proTitleLevel = BusinessDataServiceHelper.newDynamicObject(PositionStructureConstant.HBSS_PROTITLELEVEL);
                proTitleLevel.set(FormConstant.ID_KEY, perProTitleId);
                data.proTitleLevel = proTitleLevel;
            }
        } else {
            logger.info("【职位体系】-无职称信息");
        }

        // 技能信息
        DynamicObject perOcpQual = positionAppointment.getPerOcpQual();
        if (perOcpQual != null) {
            data.jobStatusName = perOcpQual.getString(String.join(".", FormConstant.QUALIFICATION_KEY, FormConstant.NAME_KEY));
            logger.info("【职位体系】-获取技能信息，jobStatusName={}", data.jobStatusName);
            
            // 技能等级
            long quaLevelId = perOcpQual.getLong(String.join(".", FormConstant.QUALEVEL_KEY, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取技能等级，quaLevelId={}", quaLevelId);
            if(quaLevelId > 0) {
                DynamicObject ocpQualLevel = BusinessDataServiceHelper.newDynamicObject(PositionStructureConstant.HBSS_OCPQUALLEVEL);
                ocpQualLevel.set(FormConstant.ID_KEY, quaLevelId);
                data.ocpQualLevel = ocpQualLevel;
            }
        } else {
            logger.info("【职位体系】-无技能信息");
        }
        
        // 任职信息
        DynamicObject empPosOrgRel = positionAppointment.getEmpPosOrgRel();
        if (empPosOrgRel != null) {
            data.empPosOrgRel = empPosOrgRel;
            logger.info("【职位体系】-获取任职信息完成");
            
            //岗位
            long positionId = empPosOrgRel.getLong(String.join(".", FormConstant.POSITION_KEY, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取岗位信息，positionId={}", positionId);
            if(positionId > 0) {
                DynamicObject position = BusinessDataServiceHelper.newDynamicObject(FormConstant.HBPM_POSITIONHR);
                position.set(FormConstant.ID_KEY, positionId);
                position.set(FormConstant.NAME_KEY, empPosOrgRel.getString(String.join(".", FormConstant.POSITION_KEY, FormConstant.NAME_KEY)));
                data.positionHr = position;
            }

            long companyId = empPosOrgRel.getLong(String.join(".", FormConstant.COMPANY_KEY, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取公司信息，companyId={}", companyId);
            if(companyId > 0) {
                DynamicObject company = BusinessDataServiceHelper.newDynamicObject(FormConstant.ADMINORG_ENTITYID);
                company.set(FormConstant.ID_KEY, companyId);
                data.company = company;
            }
            
            long adminOrgId = empPosOrgRel.getLong(String.join(".", FormConstant.ADMINORG, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取部门信息，adminOrgId={}", adminOrgId);
            if(adminOrgId > 0) {
                DynamicObject dep = BusinessDataServiceHelper.newDynamicObject(FormConstant.ADMINORG_ENTITYID);
                dep.set(FormConstant.ID_KEY, adminOrgId);
                data.dep = dep;
            }

            // 职位序列
            Long jobSeqId = empPosOrgRel.getLong(String.join(".", FormConstant.HBPM_POSITIONHR, FormConstant.NCKD_JOBSEQ, FormConstant.ID_KEY));
            logger.info("【职位体系】-获取职位序列信息，jobSeqId={}", jobSeqId);
            if(jobSeqId > 0) {
                String jobSeqNumber = empPosOrgRel.getString(String.join(".", FormConstant.HBPM_POSITIONHR, FormConstant.NCKD_JOBSEQ, FormConstant.NUMBER_KEY));
                String jobSeqName = empPosOrgRel.getString(String.join(".", FormConstant.HBPM_POSITIONHR, FormConstant.NCKD_JOBSEQ, FormConstant.NAME_KEY));
                DynamicObject jobSeq = BusinessDataServiceHelper.newDynamicObject(FormConstant.HBJM_JOBSEQHR);
                jobSeq.set(FormConstant.ID_KEY, jobSeqId);
                jobSeq.set(FormConstant.NUMBER_KEY, jobSeqNumber);
                jobSeq.set(FormConstant.NAME_KEY, jobSeqName);
                data.jobSeq = jobSeq;
                data.jobSeqNumber = jobSeqNumber;
                data.jobSeqName = jobSeqName;
                data.jobSeqEnum = JobSeqEnum.getByCode(data.jobSeqNumber);
                logger.info("【职位体系】-职位序列编号={}，名称={}", jobSeqNumber, jobSeqName);
            }

            //本次加入集团时间
            data.joinComDate = empPosOrgRel.getDate(String.join(".", FormConstant.HRPI_PERSERLEN, FormConstant.JOINCOMDATE_KEY));
            logger.info("【职位体系】-本次加入集团时间={}", data.joinComDate);
        } else {
            logger.info("【职位体系】-无任职信息");
        }

        data.isExcellent = Boolean.FALSE;
        if(positionAppointment.getExcellent() != null && positionAppointment.getExcellent()){
            data.isExcellent = Boolean.TRUE;
            logger.info("【职位体系】-该员工为优秀生");
        }
        
        // 特定字段由子类处理
        extractSpecificFields(data, initialData);
        logger.info("【职位体系】-基础数据获取完成");
        return data;
    }

    /**
     * 提取特定字段（由子类实现）
     */
    protected abstract void extractSpecificFields(BaseInitialData data, DynamicObject initialData);

    /**
     * 获取上年度绩效结果
     */
    protected void getPreviousYearPerformance(BaseInitialData data, String logPrefix) {
        LocalDateTime lastYearDateTime = DateUtil.minusYears(DateUtil.toLocalDateTime(data.beginDate), 1);
        data.lastYearAppraisalResult = PerformanceManagerHelper.getPerformanceResult(
                data.person.getLong(FormConstant.ID_KEY), lastYearDateTime);
        
        AppraisalResultEnum lastYearAppraisalResultEnum = AppraisalResultEnum.NONE;
        if(data.lastYearAppraisalResult != null) {
            String lastAppraisalResultNumber = data.lastYearAppraisalResult.getString(FormConstant.NUMBER_KEY);
            lastYearAppraisalResultEnum = AppraisalResultEnum.getByCode(lastAppraisalResultNumber);
        }
        data.lastYearAppraisalResultEnum = lastYearAppraisalResultEnum;
        
        // 降级数
        data.downgradeNum = 0;
        if (lastYearAppraisalResultEnum == AppraisalResultEnum.BASICALLY_QUALIFIED) {
            // "基本合格"降1级
            data.downgradeNum = 1;
        } else if (lastYearAppraisalResultEnum == AppraisalResultEnum.UN_QUALIFIED) {
            // "不合格"降2级
            data.downgradeNum = 2;
        }

        logger.info("{}-上年度考核结果【{}】-降级数【{}】",
                logPrefix, lastYearAppraisalResultEnum.getName(), data.downgradeNum);
    }

    /**
     * 获取积分数据
     */
    protected ScoreData getScoreData(BaseInitialData data, String logPrefix) {
        logger.info("{}-开始获取积分数据", logPrefix);
        ScoreData scoreData = new ScoreData();

        QueryFieldBuilder queryFieldBuilder = QueryFieldBuilder.create().addIdNumberNameWithExtras(FormConstant.NCKD_SCORE);

        // 学历积分
        scoreData.educationScore = BigDecimal.ZERO;
        if (data.diploma != null) {
            logger.info("{}-开始获取学历积分，diplomaId={}", logPrefix, data.diploma.getLong(FormConstant.ID_KEY));
            DynamicObject education = BusinessDataServiceHelper.loadSingle(FormConstant.HBSS_DIPLOMA,
                    queryFieldBuilder.buildSelect(),
                    new QFilter[]{QFilterCommonHelper.getIdEqFilter(data.diploma.getLong(FormConstant.ID_KEY))});
            if (education != null) {
                scoreData.educationScore = education.getBigDecimal(FormConstant.NCKD_SCORE);
                logger.info("{}-学历【{}】-积分【{}】", logPrefix, education.getString(FormConstant.NAME_KEY), scoreData.educationScore);
            } else {
                logger.warn("{}-未找到学历信息", logPrefix);
            }
        } else {
            logger.warn("{}-无学历信息", logPrefix);
        }

        // 职称积分
        scoreData.proTitleScore = BigDecimal.ZERO;
        // 技能积分
        scoreData.perOcpQualScore = BigDecimal.ZERO;
        scoreData.dbProTitleLevel = null;
        scoreData.dbOcpQualLevel = null;

        logger.info("{}-职位序列类型={}, proTitleLevel={}, ocpQualLevel={}", 
                   logPrefix, data.jobSeqEnum, data.proTitleLevel, data.ocpQualLevel);

        if (data.jobSeqEnum != JobSeqEnum.SKILL && data.proTitleLevel != null) {
            // 非技能序列获取职称积分
            logger.info("{}-开始获取职称积分", logPrefix);
            scoreData.dbProTitleLevel = BusinessDataServiceHelper.loadSingle(FormConstant.HBSS_PROTITLELEVEL,
                    queryFieldBuilder.buildSelect(),
                    new QFilter[]{QFilterCommonHelper.getIdEqFilter(data.proTitleLevel.getLong(FormConstant.ID_KEY))});
            if (scoreData.dbProTitleLevel != null) {
                scoreData.proTitleScore = scoreData.dbProTitleLevel.getBigDecimal(FormConstant.NCKD_SCORE);
                data.jobStatusName = null;
                logger.info("{}-职称积分获取完成，积分为{}", logPrefix, scoreData.proTitleScore);
            } else {
                logger.warn("{}-未找到职称等级信息", logPrefix);
            }
        } else if (data.jobSeqEnum == JobSeqEnum.SKILL && data.ocpQualLevel != null) {
            // 技能序列获取技能积分
            logger.info("{}-开始获取技能积分", logPrefix);
            scoreData.dbOcpQualLevel = BusinessDataServiceHelper.loadSingle(FormConstant.HBSS_OCPQUALLEVEL,
                    queryFieldBuilder.buildSelect(),
                    new QFilter[]{QFilterCommonHelper.getIdEqFilter(data.ocpQualLevel.getLong(FormConstant.ID_KEY))});
            if (scoreData.dbOcpQualLevel != null) {
                scoreData.perOcpQualScore = scoreData.dbOcpQualLevel.getBigDecimal(FormConstant.NCKD_SCORE);
                data.rankName = null;
                logger.info("{}-技能积分获取完成，积分为{}", logPrefix, scoreData.perOcpQualScore);
            } else {
                logger.warn("{}-未找到技能等级信息", logPrefix);
            }
        } else {
            logger.info("{}-无需获取职称或技能积分", logPrefix);
        }

        logger.info("{}-初定职位序列【{}】-学历积分【{}】-职称积分【{}】-技能积分【{}】",
                logPrefix, data.jobSeqName, scoreData.educationScore, scoreData.proTitleScore, scoreData.perOcpQualScore);

        return scoreData;
    }

    /**
     * 计算累计积分池
     */
    protected BigDecimal calculateSumScore(BigDecimal allSumScore, ScoreData scoreData, String logPrefix) {
        // 累计积分池 = 累计总积分【前端传入】 - (职称分/技能分) - 学历分
        BigDecimal sumScore = allSumScore
                .subtract(scoreData.proTitleScore != null ? scoreData.proTitleScore : BigDecimal.ZERO)
                .subtract(scoreData.perOcpQualScore != null ? scoreData.perOcpQualScore : BigDecimal.ZERO)
                .subtract(scoreData.educationScore != null ? scoreData.educationScore : BigDecimal.ZERO);

        // 如果为负数则赋值为0
        if (sumScore.compareTo(BigDecimal.ZERO) < 0) {
            sumScore = BigDecimal.ZERO;
        }

        logger.info("{}-累计积分计算结果 = {}", logPrefix, sumScore);
        return sumScore;
    }

    /**
     * 创建并保存职位档案
     */
    protected DynamicObject createAndSavePersonPosFile(BaseInitialData data, ScoreData scoreData,
                                             BigDecimal allSumScore, BigDecimal sumScore, 
                                             DynamicObject jobLeve, String logPrefix, String typeState) {
        logger.info("{}-开始创建并保存职位档案", logPrefix);
        // 优秀生得2分
        BigDecimal excellentScore = data.isExcellent ? new BigDecimal(2) : BigDecimal.ZERO;
        logger.info("{}-优秀生分数: {}", logPrefix, excellentScore);

        // 构建职位档案并存入数据库
        DynamicObject personPosFile = BusinessDataServiceHelper.newDynamicObject(
                PositionStructureConstant.PERSONPOSFILE_ENTITYID);
        logger.info("{}-创建职位档案对象完成", logPrefix);

        personPosFile.set(PositionStructureConstant.NCKD_PERSON, data.person);
        personPosFile.set(PositionStructureConstant.NCKD_FIRSTRANK, EnableEnum.YES.getCode());
        personPosFile.set(PositionStructureConstant.NCKD_BEGINDATE, data.beginDate);
        personPosFile.set(PositionStructureConstant.NCKD_APPRAISALRESULT, data.lastYearAppraisalResult);
        personPosFile.set(PositionStructureConstant.NCKD_JOBSEQHR, data.jobSeq);
        personPosFile.set(PositionStructureConstant.NCKD_POSITIONHR, data.positionHr);
        /*personPosFile.set(PositionStructureConstant.USEORG_KEY, data.company);
        personPosFile.set(PositionStructureConstant.CREATEORG_KEY, data.dep);
        personPosFile.set(PositionStructureConstant.ORG_KEY, data.dep);*/

       /* personPosFile.set(PositionStructureConstant.ORG_KEY, UserServiceHelper.getUserMainOrgId(UserServiceHelper.getCurrentUserId()));
        personPosFile.set(PositionStructureConstant.SRCCREATEORG_KEY, data.company);
        personPosFile.set(PositionStructureConstant.CREATEORG_KEY, data.company);
        personPosFile.set(PositionStructureConstant.USEORG_KEY, data.dep);*/

        personPosFile.set(PositionStructureConstant.NCKD_DEP, data.dep);
        personPosFile.set(PositionStructureConstant.NCKD_ALLSUMSCORE, allSumScore);
        personPosFile.set(PositionStructureConstant.NCKD_SUMSCORE, sumScore);
        logger.info("{}-设置基本字段完成，总积分={}, 累计积分={}", logPrefix, allSumScore, sumScore);

        DynamicObject convertJobSeq = data.convertJobSeq;
        JobSeqEnum jobSeqEnum = JobSeqEnum.getByCode(convertJobSeq.getString(FormConstant.NUMBER_KEY));
        logger.info("{}-转换后的职位序列类型={}", logPrefix, jobSeqEnum);
        
        if(JobSeqEnum.SKILL == jobSeqEnum){
            personPosFile.set(PositionStructureConstant.NCKD_JOBSTATUSNAME, data.jobStatusName);
            personPosFile.set(PositionStructureConstant.NCKD_OCPQUALLEVEL, scoreData.dbOcpQualLevel);
            personPosFile.set(PositionStructureConstant.NCKD_JOBSTATUSSCORE, scoreData.perOcpQualScore);
            logger.info("{}-设置技能序列相关信息完成", logPrefix);
        }else{
            personPosFile.set(PositionStructureConstant.NCKD_RANKNAME, data.rankName);
            personPosFile.set(PositionStructureConstant.NCKD_PROTITLELEVEL, scoreData.dbProTitleLevel);
            personPosFile.set(PositionStructureConstant.NCKD_RANKSCORE, scoreData.proTitleScore);
            logger.info("{}-设置非技能序列相关信息完成", logPrefix);
        }
        
        personPosFile.set(PositionStructureConstant.NCKD_JOBLEVELHR, jobLeve);
        personPosFile.set(PositionStructureConstant.NCKD_DIPLOMA, data.diploma);
        personPosFile.set(PositionStructureConstant.NCKD_TYPESTATE, typeState);
        personPosFile.set(PositionStructureConstant.NCKD_DIPLOMASCORE, scoreData.educationScore);
        personPosFile.set(PositionStructureConstant.NCKD_EXECUTEYEAR, DateUtil.getYear(data.beginDate));
        personPosFile.set(PositionStructureConstant.NCKD_ORGINSSCORE, excellentScore);
        personPosFile.set(PositionStructureConstant.NCKD_EMPLOYMENTYEARS, data.employmentYears);
        personPosFile.set(PositionStructureConstant.KEY_NCKD_CAUSEREMARK, data.causeRemark);
        personPosFile.set(PositionStructureConstant.NCKD_DISABLE, EnableEnum.NO.getCode());
        personPosFile.set(PositionStructureConstant.NCKD_ISCURRENTNEWEST, EnableEnum.YES.getCode());
        personPosFile.set(PositionStructureConstant.STATUS, StatusEnum.C.toString());
        personPosFile.set(PositionStructureConstant.ENABLE, EnableEnum.YES.getCode());
        personPosFile.set(PositionStructureConstant.CREATOR_KEY, RequestContext.get().getCurrUserId());
        logger.info("{}-设置所有字段完成", logPrefix);

//        SaveServiceHelper.save(new DynamicObject[]{personPosFile});
        OperationResult operationResult = SaveServiceHelper.saveOperate(PositionStructureConstant.PERSONPOSFILE_ENTITYID, new DynamicObject[]{personPosFile}, OperateOption.create());
        if (!operationResult.isSuccess()) {
            StringJoiner errorMsg = new StringJoiner("\n");
            for (IOperateInfo error : operationResult.getAllErrorOrValidateInfo()) {
                errorMsg.add(error.getMessage());
            }
            if (!ObjectUtils.isEmpty(operationResult.getMessage())) {
                errorMsg.add(operationResult.getMessage());
            }
            throw new ValidationException("保存职位档案失败，原因：" + errorMsg.toString());
        }
        logger.info("{}-职位档案保存完成，ID={}", logPrefix, personPosFile.getLong(FormConstant.ID_KEY));
        return personPosFile;
    }

    /**
     * 设置操作结果中的职级信息
     */
    protected void setJobLevelResult(DynamicObject person, DynamicObject jobLeve) {
        String jobLeveStr = StrFormatter.format("{}：{}（{}级）", 
                person.getString(FormConstant.NAME_KEY),
                jobLeve.getString(FormConstant.NAME_KEY),
                jobLeve.getString(PositionStructureConstant.NCKD_ALIAS));
        
        Map<String, String> customData = this.getOperationResult().getCustomData();
        if(customData == null) {
            this.getOperationResult().setCustomData(new HashMap<>());
            customData = this.getOperationResult().getCustomData();
        }
        
        String oldJobLeveStr = customData.get(PositionStructureConstant.NCKD_JOBLEVELHR);
        if (oldJobLeveStr == null || oldJobLeveStr.isEmpty()) {
            customData.put(PositionStructureConstant.NCKD_JOBLEVELHR, jobLeveStr);
        } else {
            customData.put(PositionStructureConstant.NCKD_JOBLEVELHR, oldJobLeveStr + StrFormatter.LINE_SEPARATOR + jobLeveStr);
        }
    }
}
