package com.yem.wm.bc.change.op;


import java.util.List;

import com.yem.wm.bc.change.business.ChangeModelHelper;
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.metadata.entity.validation.ConditionValidation;
import kd.bos.service.operation.validate.ConditionValidator;

/**
 * @author 夜子
 * @ClassName XBillAuditOp
 * @description: TODO
 * @date 2023年08月03日
 * @version: 1.0
 */
public class XBillAuditOp extends AbstractOperationServicePlugIn {
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        if (this.billEntityType != null) {
            String opkey = (String) this.operateMeta.get("key");
            List<ConditionValidation> vallist = ChangeModelHelper.getValidConditionList((String) this.billEntityType.getName(),
                    opkey);
            if (vallist != null && vallist.size() > 0) {
                for (ConditionValidation o : vallist) {
                    ConditionValidator XBillCV = new ConditionValidator();
                    XBillCV.setValidation(o.createValidate());
                    e.addValidator(XBillCV);
                }
            }
        }
    }

    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            DynamicObject changeModel = ChangeModelHelper
                    .getChangeModel4XBill((String) bills[0].getDataEntityType().getName());
            if (changeModel == null) {
                return;
            }
            for (DynamicObject bill : bills) {
                ChangeModelHelper.excutePluginMethod((DynamicObject) changeModel, (String) "beforeXBillAudit",
                        (DynamicObject) bill);
            }
        }
    }

    public void endOperationTransaction(EndOperationTransactionArgs e) {
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            String billtype = bills[0].getDataEntityType().getName();
            DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill((String) billtype);
            if (changeModel == null) {
                return;
            }
            for (DynamicObject bill : bills) {
                ChangeModelHelper.excutePluginMethod((DynamicObject) changeModel, (String) "afterXBillAudit",
                        (DynamicObject) bill);
            }
        }
    }
}
