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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.Save;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
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.DateUtil;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;

import java.util.Date;

/**
* 评价规则表单插件
* @author W.Y.C
* @date 2025/6/25 15:40
* @version 1.0
*/
public class EvaluationRuleFormPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        if (args.getSource() instanceof Save) {
            DynamicObject dataEntity = this.getView().getModel().getDataEntity();
            Date date = dataEntity.getDate(HonorStudentConstant.NCKD_BIZYEAR);
            QFilter qFilter = new QFilter(HonorStudentConstant.NCKD_BIZYEAR, QCP.equals, date);
            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.EVALUATIONRULE_ENTITYID, new QFilter[]{qFilter});
            if(exists){
                args.setCancel(true);
                this.getView().showErrorNotification(StrFormatter.format("所属年份【{}】的规则已存在，不能重复添加", DateUtil.getYear(date)));
            }
        }
    }
}