package com.yem.wm.im.debcrednotenew.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
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.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.List;

/**
 * @author zhouc
 * @date 2024/3/28 22:00
 * @className PlaceexaMineNewUnAuditOp
 * @description 下单审批反审核（新）服务插件
 */
public class PlaceexaMineNewUnAuditOp extends AbstractOperationServicePlugIn {
    @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());
            String yem_debcreditid = dataEntitie.getString("yem_debcreditid");
            if (yem_debcreditid.length() > 0) {
                QFilter qFilter = new QFilter("id", QCP.equals, Long.valueOf(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 yem_counstat = dynamicObject.getString("yem_counstat");
                    if (yem_counstat.equals("B")) {
                        throw new KDBizException("当前单据关联的《借/贷记单》已确认，禁止反审核！");
                    }
                    String billstatus = dynamicObject.getString("billstatus");
                    if (billstatus.equals("A")) {
//                    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 {
                            dataEntitie.set("yem_debcreditno", null);
                            dataEntitie.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 {
                                dataEntitie.set("yem_debcreditno", null);
                                dataEntitie.set("yem_debcreditid", null);
                            }
                        } else {
                            String message = unAudit.getMessage();
                            List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                            throw new KDBizException(String.format("《借/贷记单》反审核失败，%s，%s", message, allErrorOrValidateInfo));
                        }
                    }

                } else {
                    dataEntitie.set("yem_debcreditno", null);
                    dataEntitie.set("yem_debcreditid", null);
                }
                SaveServiceHelper.update(new DynamicObject[]{dataEntitie});
            }
        }
    }

}
