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

import com.yem.wm.utils.YunzhijiaUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 合同提交云之家特殊处理，事务结束之后在进行同步操作
 * @Date: 2024/12/25 17:34
 * @Created: by ZZSLL
 */

public class YemBillSubmitToYZJEndTransactionOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);

    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] entities = e.getDataEntities();
        List<DynamicObject> passDataEntity = new ArrayList<>();
        OperationResult result = this.operationResult;
        if (!result.isSuccess()) {
            return;
        }
        StringBuilder errMsg = new StringBuilder();
        for (DynamicObject entity : entities) {
            String billType = entity.getDataEntityType().getName();
            if (!"yem_es_salesorder".equals(billType)) {
                return;
            }

            YunzhijiaUtils.submit2YZJ(result, passDataEntity, entity);

            if (result.isSuccess()) {
                passDataEntity.add(entity);
            } else {
                String message = result.getMessage();
                errMsg.append(message).append("\n ");
            }
        }

        if (errMsg.length() > 0) {
            throw new KDBizException("提交云之家失败！错误信息：" + errMsg);
        }
    }
}
