package com.yem.wm.es.shippingdetails.op;

import com.yem.wm.es.shippingdetails.validator.ShippingDetailSubmitValidator;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.List;

/**
 * 单据操作
 */
public class ShippingDetailSubmitOp extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
    }


    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new ShippingDetailSubmitValidator());
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] eDataEntities = e.getDataEntities();
        for (DynamicObject eDataEntity : eDataEntities) {
            if ("yem_xshippingdetails".equals( eDataEntity.getDynamicObjectType().getName())) {
                OperationResult Operate = OperationServiceHelper.executeOperate("checkcompleteno",
                        eDataEntity.getDynamicObjectType().getName(),
                        new DynamicObject[]{eDataEntity}, OperateOption.create());
                if (!Operate.isSuccess()) {
                    throw new KDBizException("整机编号校验,调用失败");
                }
            }
        }
    }
}