package nckd.jxccl.hrmp.hbpm.plugin.operate.hr;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import nckd.jxccl.hrmp.hbpm.business.hr.service.impl.PositionBillSaveHelper;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;
import nckd.jxccl.hrmp.hbpm.plugin.operate.hr.validator.PositionBillSaveAndSubmitValidator;
import nckd.jxccl.hrmp.hbpm.plugin.operate.hr.validator.PositionBillSaveValidator;

/**
 * 岗位申请单保存操作插件
 * @entity: nckd_positionbill
 * @operate: save
 * @from: kd.hr.homs.business.domain.orgbatch.service.impl.OrgBatchBillSaveHelper
 * @author: jtd
 * @date: 2026/1/1 17:57
 */
public class PositionBillSaveOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(PositionBillSaveOpPlugin.class);

    public void onAddValidators(AddValidatorsEventArgs e) {
        logger.info("PositionBillSaveOpPlugin.onAddValidators start.");
        super.onAddValidators(e);
        e.addValidator(new PositionBillSaveAndSubmitValidator());
        e.addValidator(new PositionBillSaveValidator());
    }

    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        logger.info("PositionBillSaveOpPlugin.beginOperationTransaction start.");
        if (e.getDataEntities().length != 0) {
            DynamicObject dataEntity = e.getDataEntities()[0];
            PositionBillSaveHelper.getInstance().positionBillSave(dataEntity);
        }
    }

    public void endOperationTransaction(EndOperationTransactionArgs e) {
        Long billId = e.getDataEntities()[0].getLong(PositionBillConstant.ID_KEY);
        HRBaseServiceHelper positionBillEntityHelper = new HRBaseServiceHelper(PositionBillConstant.NCKD_POSITIONBILLENTRY_ENTITY);
        QFilter positionIdFilter = new QFilter(PositionBillConstant.CREATOR_KEY, QCP.equals, 0);
        QFilter billIdFilter = new QFilter(PositionBillConstant.NCKD_BILLID, QCP.equals, billId);
        positionIdFilter.and(billIdFilter);
        positionBillEntityHelper.deleteByFilter(positionIdFilter.toArray());
    }

}
