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

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * @author zhouc
 * @date 2024/3/27 20:46
 * @className SaleOrdeDelGenerateOp
 * @description 删除贷记单
 */
public class SaleOrdeDelGenerateOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_es_materialinfo");
        fieldKeys.add("yem_intauditbusano");
        fieldKeys.add("yem_intauditbusaid");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            DelGenerate(dataEntitie);
        }
    }

    public void DelGenerate(DynamicObject dataEntitie) {
        DynamicObjectCollection yem_accfunddetail = dataEntitie.getDynamicObjectCollection("yem_accfunddetail");
        for (int i = 0; i < yem_accfunddetail.size(); i++) {
            DynamicObject accfunddetail = yem_accfunddetail.get(i);
            Long yem_debcreditid = accfunddetail.getLong("yem_debcreditid");
            if (yem_debcreditid > 0) {
                QFilter qFilter = new QFilter("id", QCP.equals, yem_debcreditid);
                DynamicObject yem_debcrednote = BusinessDataServiceHelper.loadSingle("yem_debcrednote", "id"
                        , new QFilter[]{qFilter});
                if (yem_debcrednote != null) {
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(yem_debcreditid, "yem_debcrednote");
                    String billstatus = dynamicObject.getString("billstatus");
                    if (billstatus.equals("A")) {
                        OperationResult delete = OperationServiceHelper.executeOperate("delete", "yem_debcrednote", new Object[]{yem_debcreditid}, OperateOption.create());
                        if (!delete.isSuccess()) {
                            String message = delete.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("《借/贷记单》删除失败，%s，%s", message, allErrorOrValidateInfo));
                        } else {
                            accfunddetail.set("yem_debcrednoteno", null);
                            accfunddetail.set("yem_debcreditid", null);

                        }
                    } else {
                        OperationResult unAudit = OperationServiceHelper.executeOperate("unaudit", "yem_debcrednote", new Object[]{yem_debcreditid}, OperateOption.create());
                        if (unAudit.isSuccess()) {
//                        //切换正式需修改
//                        OperationResult sfestimatedAudit = OperationServiceHelper.executeOperate("sfestimated", "yem_debcrednote", new Object[]{yem_debcreditid}, OperateOption.create());
                            OperationResult delete = OperationServiceHelper.executeOperate("delete", "yem_debcrednote", new Object[]{yem_debcreditid}, OperateOption.create());
                            if (!delete.isSuccess()) {
                                String message = delete.getMessage();
                                List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                                throw new KDBizException(String.format("《借/贷记单》删除失败，%s，%s", message, allErrorOrValidateInfo));
                            } else {
                                accfunddetail.set("yem_debcrednoteno", null);
                                accfunddetail.set("yem_debcreditid", null);

                            }
                        } else {
                            String message = unAudit.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("《借/贷记单》反审核失败，%s，%s", message, allErrorOrValidateInfo));
                        }
                    }
                }

            }

        }
        SaveServiceHelper.update(new DynamicObject[]{dataEntitie});
    }
}
