package yem.wm.bd.createbasedata.operation;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;

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

/**
 * @Description 删除核算维度数据
 * 合同单据在点击删除时，同步删除核算维度资料
 * @Author: zhangh
 * @CreateTime: 2025-02-14  18:22
 */
public class DeleteInsuranceOpPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        for (DynamicObject dataEntity : e.getDataEntities()) {
            //需要判断是否是变更单，如果是变更单据，则跳出
            String name = dataEntity.getDataEntityType().getName();
            dataEntity = BusinessDataServiceHelper.loadSingleFromCache(dataEntity.getLong("id"), name);

            DynamicObject fieldmap = BusinessDataServiceHelper.loadSingle("yem_bd_fieldmap", "id,yem_targetentity", new QFilter[]{new QFilter("yem_sourceentity.number", QCP.equals, name)});//查询《核算维度字段映射配置》
            if (fieldmap != null) {
                List<QFilter> qFilters = new ArrayList<>();
                qFilters.add(new QFilter("yem_billkey", QCP.equals, name));
                qFilters.add(new QFilter("yem_billid", QCP.equals, dataEntity.getLong("id")));
                DeleteServiceHelper.delete(fieldmap.getDynamicObject("yem_targetentity").getString("number"), qFilters.toArray(new QFilter[]{}));
            }
        }
    }
}
