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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.AbstractOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.hr.hbp.common.util.HRObjectUtils;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hr.sdm.business.SdmBusinessService;
import nckd.jxccl.hr.sdm.common.SanDingConstant;

/**
 * 三定计划列表插件
 * @entity: nckd_sandingplan
 * @author: jtd
 * @date: 2025/12/12 16:52
 */
public class SanDingPlanListPlugin extends AbstractListPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);

        String operateKey = ((AbstractOperate) args.getSource()).getOperateKey();
        if (HRStringUtils.equals(SanDingConstant.NEW_OP, operateKey)) {
            DynamicObject lastSanDingPlanDyo = SdmBusinessService.getLastSanDingPlanDyo(0L);
            if (!HRObjectUtils.isEmpty(lastSanDingPlanDyo) && !lastSanDingPlanDyo.getBoolean(SanDingConstant.NCKD_ISCOMPLATED_KEY)) {
                getView().showTipNotification("您有一个进行中的三定管理数据，无法新增。");
                // 取消
                args.setCancel(true);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);

        String operateKey = afterDoOperationEventArgs.getOperateKey();
        if (HRStringUtils.equals(operateKey, SanDingConstant.SENDTASK_OP)) {
            // 刷新
            getView().invokeOperation(SanDingConstant.REFRESH_OP);
        }
    }
}
