package nckd.jxccl.hr.hstu.plugin.form;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.Save;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;

/**
* 地区差异分插件
* @author W.Y.C
* @date 2025/6/19 16:12
* @version 1.0
*/
public class OrgDifferenceFormPlugin extends AbstractBasePlugIn implements Plugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        if (args.getSource() instanceof Save) {
            DynamicObject dataEntity = this.getView().getModel().getDataEntity();
            DynamicObject org = dataEntity.getDynamicObject(HonorStudentConstant.NCKD_ORG);
            QFilter qFilter = new QFilter(HonorStudentConstant.NCKD_ORG, QCP.equals, org.getPkValue());
            Long pkValue = (Long) dataEntity.getPkValue();
            if(pkValue != null && pkValue > 0L){
                qFilter.and(new QFilter(HonorStudentConstant.ID_KEY, QCP.not_equals, dataEntity.getPkValue()));
            }
            boolean exists = QueryServiceHelper.exists(HonorStudentConstant.ORGDIFFERENCE_ENTITYID, new QFilter[]{qFilter});
            if(exists){
                args.setCancel(true);
                this.getView().showErrorNotification(StrFormatter.format("【{}】组织已存在差异分，不能重复添加",org.getString("name")));
            }
        }
    }

}