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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.hr.psms.business.JobLevelCalculatorService;
import nckd.jxccl.hr.psms.common.ContributionConstant;
import nckd.jxccl.hr.psms.common.PerfRankMgmtConstant;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* 升保降级条件配置
* 实体标识：nckd_levelconditionconf
* @author W.Y.C
* @date 2025/11/2 21:01
* @version 1.0
*/
public class LevelConditionConfFormPlugin extends AbstractFormPlugin implements Plugin, BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        // 为分录中的A字段添加值变化监听
        BasedataEdit minJobLevelEdit = this.getView().getControl(PerfRankMgmtConstant.NCKD_MINJOBLEVEL);
        if (minJobLevelEdit != null) {
            minJobLevelEdit.addBeforeF7SelectListener(this);
        }

        BasedataEdit maxJobLevelEdit = this.getView().getControl(PerfRankMgmtConstant.NCKD_MAXJOBLEVEL);
        if (maxJobLevelEdit != null) {
            maxJobLevelEdit.addBeforeF7SelectListener(this);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String fieldKey = e.getProperty().getName();

        //基础资料联动
        // 仅处理B字段的F7事件
        if (PerfRankMgmtConstant.NCKD_MINJOBLEVEL.equalsIgnoreCase(fieldKey) || PerfRankMgmtConstant.NCKD_MAXJOBLEVEL.equalsIgnoreCase(fieldKey)) {
            DynamicObject jobSeq = ConvertUtil.toDynamicObjectOrNull(this.getModel().getValue(PositionStructureConstant.NCKD_JOBSEQ));
            if(jobSeq != null){
                DynamicObject[] jobLevelByJobSeq = JobLevelCalculatorService.getJobLevelByJobSeq(jobSeq);
                List<Long> jobLevelIds = Arrays.stream(jobLevelByJobSeq).map(dynamicObject -> dynamicObject.getLong(FormConstant.ID_KEY)).collect(Collectors.toList());
                QFilter filter = new QFilter(ContributionConstant.ID_KEY, QCP.in, jobLevelIds);
                e.addCustomQFilter(filter);

            }else{
                this.getView().showTipNotification("请先选择“职位序列”");
                e.setCancel( true);
            }

        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        if(FormConstant.NCKD_JOBSEQ.equalsIgnoreCase(name)){
            ChangeData[] changeSet = e.getChangeSet();
            Object newValue = changeSet[0].getNewValue();
            Object oldValue = changeSet[0].getOldValue();
            if(newValue != null && !Objects.equals(newValue, oldValue)) {
                this.getModel().setValue(PerfRankMgmtConstant.NCKD_MINJOBLEVEL, null);
                this.getModel().setValue(PerfRankMgmtConstant.NCKD_MAXJOBLEVEL, null);
                this.getView().updateView(PerfRankMgmtConstant.NCKD_MINJOBLEVEL);
                this.getView().updateView(PerfRankMgmtConstant.NCKD_MAXJOBLEVEL);
            }
        }
    }
}