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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import nckd.jxccl.hr.psms.common.bo.PositionAppointmentBO;
import nckd.jxccl.hr.psms.helper.PositionFileHelper;

import java.util.Date;
import java.util.EventObject;
import java.util.Map;
import java.util.Objects;

/**
* 在职人员初定（弹窗）-弹窗页面插件
* @author W.Y.C
* @date 2025/9/15 14:47
* @version 1.0
*/
public class ServingInitialFormPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        //获取列表选择的人员
        Object personId = showParameter.getCustomParam("personId");
        if(personId != null) {
            this.getModel().setValue(FormConstant.NCKD_PERSON, personId);
            this.getView().updateView(FormConstant.NCKD_PERSON);

            initValue(ConvertUtil.toLong(personId),null);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String fieldKey = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();

        //【员工】或【调整日期】发生变更则重新获取员工任职信息
        if (PositionStructureConstant.NCKD_BEGINDATE.equalsIgnoreCase(fieldKey) || FormConstant.NCKD_PERSON.equalsIgnoreCase(fieldKey)) {
            if (changeSet != null && changeSet.length > 0) {
                ChangeData changeData = changeSet[0];
                Object oldValue = changeData.getOldValue();
                Object newValue = changeData.getNewValue();
                if (!Objects.equals(oldValue, newValue)) {
                    Date beginDate = ConvertUtil.toDate(this.getModel().getValue(PositionStructureConstant.NCKD_BEGINDATE));
                    DynamicObject person = ConvertUtil.toDynamicObject(this.getModel().getValue(PositionStructureConstant.NCKD_PERSON));
                    if(person.getDataEntityType() != null) {
                        initValue(person.getLong(FormConstant.ID_KEY), beginDate);
                    }
                }
            }
        }
    }

    /**
     * 初始化表单数据
     * @param personId 人员ID
     * @param date 日期
     * @return: void
     * @author W.Y.C
     * @date: 2025/09/25 21:39
     */
    private void initValue(Long personId, Date date) {
        if (date == null) {
            date = new Date();
        }
        PositionAppointmentBO positionAppointMen = PositionFileHelper.positionAppointmentQuery(personId, date);
        if (positionAppointMen != null) {
            // 学历
            DynamicObject perEduExp = positionAppointMen.getPerEduExp();
            if (perEduExp != null) {
                long diplomaId = perEduExp.getLong(String.join(".", FormConstant.EDUCATION_KEY, FormConstant.ID_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_DIPLOMA, diplomaId == 0 ? null : diplomaId);
            }

            // 职称信息
            DynamicObject perProTitle = positionAppointMen.getPerProTitle();
            if (perProTitle != null) {
                // 职称名称
                this.getModel().setValue(PositionStructureConstant.NCKD_RANKNAME,
                        perProTitle.getString(String.join(".", FormConstant.PROFESSIONAL_KEY, FormConstant.NAME_KEY)));

                // 职称等级
                long proLevelId = perProTitle.getLong(String.join(".", FormConstant.PROLEVEL_KEY, FormConstant.ID_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_PROTITLELEVEL, proLevelId == 0 ? null : proLevelId);
            }

            // 技能信息
            DynamicObject perOcpQual = positionAppointMen.getPerOcpQual();
            if (perOcpQual != null) {
                // 技能名称
                this.getModel().setValue(PositionStructureConstant.NCKD_JOBSTATUSNAME,
                        perOcpQual.getString(String.join(".", FormConstant.QUALIFICATION_KEY, FormConstant.NAME_KEY)));

                // 技能等级
                long quaLevelId = perOcpQual.getLong(String.join(".", FormConstant.QUALEVEL_KEY, FormConstant.ID_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_OCPQUALLEVEL, quaLevelId == 0 ? null : quaLevelId);
            }

            // 任职信息
            DynamicObject empPosOrgRel = positionAppointMen.getEmpPosOrgRel();
            if (empPosOrgRel != null) {
                // 岗位
                long positionId = empPosOrgRel.getLong(String.join(".", FormConstant.POSITION_KEY, FormConstant.ID_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_POSITIONHR, positionId == 0 ? null : positionId);

                // 职位序列
                long jobSeqId = empPosOrgRel.getLong(String.join(".", FormConstant.HBPM_POSITIONHR, FormConstant.NCKD_JOBSEQ, FormConstant.ID_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_JOBSEQ, jobSeqId == 0 ? null : jobSeqId);

                // 本次加入集团日期
                Date joinComDate = empPosOrgRel.getDate(String.join(".", FormConstant.HRPI_PERSERLEN, FormConstant.JOINCOMDATE_KEY));
                this.getModel().setValue(PositionStructureConstant.NCKD_JOINCOMDATE, joinComDate);

            }

            if(positionAppointMen.getExcellent() != null && positionAppointMen.getExcellent()){
                this.getModel().setValue(PositionStructureConstant.NCKD_EXCELLENT, true);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        boolean success = afterDoOperationEventArgs.getOperationResult() != null && afterDoOperationEventArgs.getOperationResult().isSuccess();
        if(success && PositionStructureConstant.OP_CONFIRMINITIAL.equalsIgnoreCase(operateKey)){
            Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
            String jobLeveStr = customData.get(PositionStructureConstant.NCKD_JOBLEVELHR);
            this.getView().returnDataToParent("true");
            this.getView().showConfirm("提示","初定成功，职位档案已生成。",jobLeveStr, MessageBoxOptions.OK,null,null,null,null);

        }
    }
}