package nckd.jxccl.swc.stm.plugin.operate.unitst;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.operate.result.IOperateInfo;
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.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.exception.ValidationException;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.swc.constants.StmConstant;

import java.util.StringJoiner;

/**
* 单位工资总额预算申报调整-提交生效插件
* 实体标识：nckd_unitstbudgetadjust
* @author W.Y.C
* @date 2025/12/11 16:15
* @version 1.0
*/
public class UnitStBudgetAdjustSubmitEffectOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().addAll(this.billEntityType.getAllFields().keySet());
    }


    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        for (DynamicObject dataEntity : e.getDataEntities()) {
            OperateOption option = OperateOption.create();
            OperationResult result = OperationServiceHelper.executeOperate(
                    "backunitstbudget",
                    StmConstant.UNITSTBUDGETADJUST_ENTITYID,
                    new DynamicObject[]{dataEntity},
                    option
            );

            if (result != null && !result.isSuccess()) {
                StringJoiner errorMsg = new StringJoiner("；");
                for (IOperateInfo error : result.getAllErrorOrValidateInfo()) {
                    errorMsg.add(error.getMessage());
                }
                if (!ObjectUtils.isEmpty(result.getMessage())) {
                    errorMsg.add(result.getMessage());
                }
                throw new ValidationException(StrFormatter.format("回写【{}】失败，原因：{}","单位工资总额预算申报",errorMsg.toString()));
            }
        }
    }
}