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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.List;

/**
 * @author ljw
 * @date 2024/11/5 16:51
 * @description confirmsvalidityOp
 */
public class confirmsvalidityOp extends AbstractOperationServicePlugIn {

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


    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e)
    {
        super.beforeExecuteOperationTransaction(e);
        DynamicObject[] eDataEntities = e.getDataEntities();

        for (DynamicObject eDataEntity : eDataEntities) {
            eDataEntity = BusinessDataServiceHelper.loadSingle(eDataEntity.getPkValue(), eDataEntity.getDynamicObjectType().getName());

            DynamicObjectCollection collection = eDataEntity.getDynamicObjectCollection("yem_creditdetail");

            if (!collection.isEmpty()) {
                String creditbackstatus = eDataEntity.getString("yem_creditbackstatus");
                String billno = eDataEntity.getString("billno");
                if (!"A".equals(creditbackstatus)) {
                    e.setCancelMessage("单据 [ "+ billno +" ] 未进行信保回填确认，不允许提交！");
                    e.setCancel(true);
                }
            }
        }
    }
}
