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.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.workflow.engine.msg.info.MessageInfo;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhouc
 * @date 2024/3/28 16:37
 * @className DebCredCountersignPlugin
 * @description 贷记单回签确认服务插件
 */
public class DebCredCountersignPlugin 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");
//            if ("yem_debcrednote_d".equals(billtype)) {
            String yem_counstat = temp.getString("yem_counstat");
            if (yem_counstat.equals("B")) {
                throw new KDBizException("当前单据已确认，禁止再次进行回签确认");
            }
            List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments(temp.getDynamicObjectType().getName(), temp.getPkValue(), "attachmentpanel");
            if (attachments.size() == 0) {
                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");
                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) {
                        if (billtype.equals("yem_debcrednote_a")) {
                            object.set("yem_totalamount", temp.getBigDecimal("yem_sumgoalamt"));
                            object.set("yem_totalamountloc", temp.getBigDecimal("yem_sumgoalamtloc"));
                        } else {
                            object.set("yem_totalamount", temp.getBigDecimal("yem_amount"));
                            object.set("yem_totalamountloc", temp.getBigDecimal("yem_amountbase"));
                        }

                    }
                }
                //贷记单去向 yem_creditnotedes
                DynamicObjectCollection yem_creditnotedes = temp.getDynamicObjectCollection("yem_creditnotedes");
                //配件资金池去向
                DynamicObjectCollection yem_partsfund = yem_spcapitalpool.getDynamicObjectCollection("yem_partsfund");
                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) {
                        if (billtype.equals("yem_debcrednote_a")) {
                            object.set("yem_actamount", temp.getBigDecimal("yem_sumoriginalamtloc"));
                            object.set("yem_actamountloc", temp.getBigDecimal("yem_sumoriginalamt"));
                        } else {
                            object.set("yem_actamount", temp.getBigDecimal("yem_amount_sub"));
                            object.set("yem_actamountloc", temp.getBigDecimal("yem_amountbase_sub"));
                        }
                        Map<Long, BigDecimal> map = new HashMap<>();
                        List yem_towardwritebillno = new ArrayList<>();
                        for (int j = 0; j < yem_creditnotedes.size(); j++) {
                            DynamicObject creditnotedes = yem_creditnotedes.get(j);
                            DynamicObject yem_debcrednoteno = creditnotedes.getDynamicObject("yem_debcrednoteno");
                            if (yem_debcrednoteno != null) {
                                String billno = yem_debcrednoteno.getString("billno");
                                yem_towardwritebillno.add(billno);
                                long debcrednotenoid = yem_debcrednoteno.getLong("id");
                                BigDecimal yem_amount_d = creditnotedes.getBigDecimal("yem_amount_d");
                                if (map.containsKey(debcrednotenoid)) {
                                    BigDecimal bigDecimal = map.get(debcrednotenoid);
                                    map.put(debcrednotenoid, bigDecimal.add(yem_amount_d));
                                } else {
                                    map.put(debcrednotenoid, yem_amount_d);
                                }
                            }
                        }
                        if (yem_towardwritebillno.size() > 0) {
                            if (billtype.equals("yem_debcrednote_a")) {
                                object.set("yem_towardsjamt", temp.getBigDecimal("yem_sumoriginalamtloc"));//预计核销金额
                            } else {
                                object.set("yem_towardsjamt", temp.getBigDecimal("yem_amount_sub"));//预计核销金额
                            }
                        }
                        if (map.size() > 0) {
                            DebCredUtils.updateJJD(map, "SJ");
                        }
                    }
                }
                DynamicObject object = DebCredUtils.UpdateSpcapitalpoolAmount(yem_spcapitalpool);
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        object.getDynamicObjectType().getName(), new DynamicObject[]{object}, OperateOption.create());
                if (Operate.isSuccess()) {
                    temp.set("yem_countuser", YEM.getCurrentUserId());
                    temp.set("yem_coundate", new Date());
                    temp.set("yem_counstat", "B");
                    SaveServiceHelper.update(new DynamicObject[]{temp});


            }
            }
        }
    }
}
