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

import com.yem.wm.im.debcrednotenew.utils.DebCredUtils;
import com.yem.wm.utils.FunctionalCommon;
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.exception.KDException;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouc
 * @date 2024/3/28 14:32
 * @className DebCredNewUnAuditPlugin
 * @description 贷记单反审核（新）服务插件
 */
public class DebCredNewUnAuditPlugin 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");
            temp.set("yem_remainingamount",0);
            temp.set("yem_remainingusdamount",0);
//            if ("yem_debcrednote_d".equals(billtype)) {
            String yem_counstat = temp.getString("yem_counstat");
            if (yem_counstat.equals("B")) {
                throw new KDBizException("当前单据【" + temp.getString("billno") + "】已确认，禁止反审核！");
            }
            long yem_spcapitalpoolid = temp.getLong("yem_spcapitalpoolid");//配件资金池id
            if (yem_spcapitalpoolid == 0) {
                return;
            }
            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) {
                        yem_spcapitalpool_o.remove(object);
                        temp.set("yem_partsid", null);
                        break;
                    }
                }
                //配件资金池去向
                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) {
                        yem_partsfund.remove(object);
                        temp.set("yem_accfunddetailid", null);
                        break;
                    }
                }
                yem_accfunddetailid = temp.getLong("yem_accfunddetailid");//去向配件资金使用分录行id
                yem_partsid = temp.getLong("yem_partsid");//来源配件资金使用分录id
                if (yem_accfunddetailid == 0 && yem_partsid == 0) {
                    temp.set("yem_spcapitalpoolid", null);
                }
                DynamicObject object = DebCredUtils.UpdateSpcapitalpoolAmount(yem_spcapitalpool);
                OperationResult Operate = OperationServiceHelper.executeOperate("save", object.getDynamicObjectType().getName(), new DynamicObject[]{object}, OperateOption.create());
                if (Operate.isSuccess()) {
                    //清空借记单核销金额
                    DebCredUtils.deleteDJJhxje("YJ", temp.getDynamicObjectCollection("yem_creditnotedes"));
                }
                SaveServiceHelper.update(temp);
            }
            backwriting(temp);
        }
    }

    /**
     * 反写收款单、销售出库单
     *
     * @param temp
     */
    public void backwriting(DynamicObject temp) {
        String yem_partscredit = temp.getString("yem_partscredit");
        if (yem_partscredit.equals("B")) {
            //更新销售出库单
            QFilter qFilter3 = new QFilter("billno", QCP.equals, temp.getString("yem_source"));
            DynamicObject yem_es_delivernotice = BusinessDataServiceHelper.loadSingle("yem_es_delivernotice", "id", qFilter3.toArray());
            if (yem_es_delivernotice != null) {
                DynamicObject object = BusinessDataServiceHelper.loadSingle(yem_es_delivernotice.getPkValue(), yem_es_delivernotice.getDynamicObjectType().getName());
                object.set("yem_debcreditid", null);
                object.set("yem_debcreditno", null);
                SaveServiceHelper.update(new DynamicObject[]{object});
            }
        }
        //全额收款
        if (yem_partscredit.equals("C")) {
            //更新销售出库单
//            QFilter qFilter3 = new QFilter("billno", QCP.equals, temp.getString("yem_source"));
//            DynamicObject yem_es_delivernotice = BusinessDataServiceHelper.loadSingle("yem_gathering", "id", qFilter3.toArray());
//            if (yem_es_delivernotice != null) {
//                DynamicObject object = BusinessDataServiceHelper.loadSingle(yem_es_delivernotice.getPkValue(), yem_es_delivernotice.getDynamicObjectType().getName());
//                object.set("yem_debcreditid", null);
//                object.set("yem_debcreditno", null);
//                SaveServiceHelper.update(new DynamicObject[]{object});
//            }
            //来源合同号
            String yem_sourcebillno = temp.getString("yem_sourcebillno");
            QFilter qFilter3 = new QFilter("yem_receiptdetail.yem_claimdetail.yem_salcontractno.billno", QCP.equals, yem_sourcebillno);
            DynamicObject[] yem_es_delivernotice = BusinessDataServiceHelper.load("yem_gathering", "id", qFilter3.toArray());
            if (yem_es_delivernotice.length > 0) {
                for (int i = 0; i < yem_es_delivernotice.length; i++) {
                    DynamicObject dynamicObject = yem_es_delivernotice[i];
                    DynamicObject object = BusinessDataServiceHelper.loadSingle(dynamicObject.getPkValue(), dynamicObject.getDynamicObjectType().getName());
                    if (object != null) {
                        DynamicObjectCollection yem_receiptdetail = object.getDynamicObjectCollection("yem_receiptdetail");
                        for (int j = 0; j < yem_receiptdetail.size(); j++) {
                            DynamicObject dynamicObject1 = yem_receiptdetail.get(j);
                            DynamicObjectCollection yem_claimdetail = dynamicObject1.getDynamicObjectCollection("yem_claimdetail");
                            for (DynamicObject claimdetail : yem_claimdetail) {
                                DynamicObject yem_salcontractno = claimdetail.getDynamicObject("yem_salcontractno");
                                if (yem_salcontractno != null) {
                                    if (yem_sourcebillno.equals(yem_salcontractno.getString("billno"))) {
                                        claimdetail.set("yem_creditslipid", null);
                                        claimdetail.set("yem_creditslipno", null);
                                    }
                                }
                            }
                        }
                    }
                    SaveServiceHelper.update(new DynamicObject[]{object});
                }
            }
        }
    }
}
