package nckd.jimin.jyyy.fi.plugin.form;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;

/**
 * 标准单据列表插件
 */
public class VoucherListPlugin extends AbstractListPlugin implements Plugin {


    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        ListSelectedRowCollection selectedRows = this.getSelectedRows();
        String key = e.getOperateKey();
        StringBuffer errorInfo = new StringBuffer();

        MainEntityType entityType= EntityMetadataCache.getDataEntityType("gl_voucher");
        DynamicObject[] vouchers = BusinessDataServiceHelper.load(selectedRows.getPrimaryKeyValues(), entityType);

        /***
         * 审核后，调用SAP同步操作
         */
        if ("audit".equals(key)) {
            OperationResult operationResult = e.getOperationResult();
            if (operationResult.isSuccess()) {
                OperationResult opResult = OperationServiceHelper.executeOperate("synsap", "gl_voucher", vouchers, OperateOption.create());
                if (opResult != null && opResult.isSuccess()){
                    errorInfo.append(opResult.getMessage());
                }else{
                    errorInfo.append(opResult.getMessage());
                }

                if (errorInfo.length() > 0) {
                    this.getView().showTipNotification(errorInfo.toString(), 2500);
                }
            }
        }
    }
}