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

import com.yem.wm.im.debcrednotenew.utils.DebCredUtils;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.exception.KDException;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author zhouc
 * @date 2024/3/28 16:40
 * @className DebCredUnCountersignPlugin
 * @description 贷记单反回签服务插件
 */
public class DebCredUnCountersignPlugin extends AbstractOperationServicePlugIn {
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEnties = e.getDataEntities();
        for (DynamicObject temp : dataEnties) {
            temp = BusinessDataServiceHelper.loadSingle(temp.getPkValue(), temp.getDynamicObjectType().getName());
            String billtype = temp.getString("yem_billtype.number");
            String yem_counstat = temp.getString("yem_counstat");
            if (yem_counstat.equals("A")) {
                throw new KDBizException("当前单据未确认，禁止再次进行回签反确认");
            }
            long id = temp.getLong("id");
            long yem_spcapitalpoolid = temp.getLong("yem_spcapitalpoolid");//配件资金池id
            long yem_accfunddetailid = temp.getLong("yem_accfunddetailid");//配件资金使用分录行id
            long yem_partsid = temp.getLong("yem_partsid");//来源配件资金使用分录id
            //获取配件资金池
            DynamicObject yem_spcapitalpool = BusinessDataServiceHelper.loadSingle(yem_spcapitalpoolid, "yem_spcapitalpool");
            if (yem_spcapitalpool != null) {
                //配件资金来源
                DynamicObjectCollection yem_spcapitalpool_o = yem_spcapitalpool.getDynamicObjectCollection("yem_spcapitalpool_o");
                //配件资金池去向
                DynamicObjectCollection yem_partsfund = yem_spcapitalpool.getDynamicObjectCollection("yem_partsfund");
                //贷记单 贷记单来源
                DynamicObjectCollection yem_creditnotesource = temp.getDynamicObjectCollection("yem_creditnotesource");
                if (yem_creditnotesource.size() > 0) {
                    for (int i = 0; i < yem_spcapitalpool_o.size(); i++) {
                        DynamicObject object = yem_spcapitalpool_o.get(i);
                        long yem_sourcebillid_o = object.getLong("id");
                        if (yem_sourcebillid_o == yem_partsid) {
                            BigDecimal yem_totalamount = object.getBigDecimal("yem_totalamount");
                            BigDecimal yem_totalamountloc = object.getBigDecimal("yem_totalamountloc");
                            object.set("yem_totalamount", yem_totalamount.subtract(temp.getBigDecimal("yem_amount")));
                            object.set("yem_totalamountloc", yem_totalamountloc.subtract(temp.getBigDecimal("yem_amountbase")));
                        }
                    }
                }
                //贷记单去向
                DynamicObjectCollection yem_creditnotedes = temp.getDynamicObjectCollection("yem_creditnotedes");
                if (yem_creditnotedes.size() > 0) {
                    for (int i = 0; i < yem_partsfund.size(); i++) {
                        DynamicObject object = yem_partsfund.get(i);
                        long yem_sourcebillid_o = object.getLong("id");
                        if (yem_sourcebillid_o == yem_accfunddetailid) {
                            BigDecimal yem_actamount = object.getBigDecimal("yem_actamount");
                            BigDecimal yem_actamountloc = object.getBigDecimal("yem_actamountloc");
                            BigDecimal yem_towardwriteamt = object.getBigDecimal("yem_towardwriteamt");
                            object.set("yem_actamount", yem_actamount.subtract(temp.getBigDecimal("yem_amount_sub")));
                            object.set("yem_actamountloc", yem_actamountloc.subtract(temp.getBigDecimal("yem_amountbase_sub")));
                            object.set("yem_towardsjamt", yem_towardwriteamt.subtract(temp.getBigDecimal("yem_amount_sub")));//预计核销金额
                        }
                    }
                }
                DynamicObject object = DebCredUtils.UpdateSpcapitalpoolAmount(yem_spcapitalpool);
                //清空借记单核销金额
                DebCredUtils.deleteDJJhxje("SJ", yem_creditnotedes);
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        object.getDynamicObjectType().getName(), new DynamicObject[]{object}, OperateOption.create());
                if (Operate.isSuccess()) {
                    temp.set("yem_countuser", null);
                    temp.set("yem_coundate", null);
                    temp.set("yem_counstat", "A");
                    SaveServiceHelper.update(new DynamicObject[]{temp});

                }
            }
        }
    }
}
