package fi.cas.opplugin;

import com.alibaba.druid.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

//付款处理单付款反写
public class PaymentProcessOpPlugin extends AbstractOperationServicePlugIn {
    private static String nckd_cas_arbill_gz_l = "ar_finarbill"; //应收挂帐单标识
    private static String nckd_er_prepaybill_ext = "er_prepaybill"; //预付借款单标识
    private static String nckd_er_publicreimbur_ext = "er_publicreimbursebill"; //报销单标识
    private static String er_contractbill = "er_contractbill"; //合同台账单标识
    private static String nckd_ap_finapbill_gz = "ap_finapbill"; //应付挂账单标识
    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        //获取付款处理单据信息
        DynamicObject[] dynamicObjects = e.getDataEntities();
        for (int i = 0; i < dynamicObjects.length; i++) {
            //当前单头数据包
            DynamicObject info = BusinessDataServiceHelper.loadSingle(dynamicObjects[i].getPkValue(), dynamicObjects[i].getDynamicObjectType().getName());
            //付款金额
            BigDecimal actpayamt = info.getBigDecimal("actpayamt");
            //单据编号
            String billno = info.getString("billno");
            //付款处理id
            long id = info.getLong("id");
            //付款日期
            Date paydate = info.getDate("paydate");
            //获取源单id
            Long sourcebillid = info.getLong("sourcebillid");
            //存储源单分录id和付款处理分录实付金额
            HashMap<Long,BigDecimal> amountMap = new HashMap<>();
            //循环付款明细分录
            for (DynamicObject entryentity : info.getDynamicObjectCollection("entry")) {
                //取当前循环付款明细分录的实付金额
                BigDecimal expenseamount = entryentity.getBigDecimal("e_actamt");
                //取当前循环付款明细分录的源单分录id
                Long sourcebillentryid = entryentity.getLong("e_sourcebillentryid");
                amountMap.put(sourcebillentryid,expenseamount);
            }
            //查询应收挂账单
            QFilter gzFilter = new QFilter("id", QCP.equals,sourcebillid);
            DynamicObject[] gzDynamicObjects = BusinessDataServiceHelper.load(nckd_cas_arbill_gz_l,"id",new QFilter[] {gzFilter});

            //查询报销单
            QFilter bxFilter = new QFilter("id", QCP.equals,sourcebillid);
            DynamicObject[] bxDynamicObjects = BusinessDataServiceHelper.load(nckd_er_publicreimbur_ext,"id",new QFilter[] {bxFilter});
            //应收方向的回写
            if (gzDynamicObjects.length > 0) {
                //循环数组获取结算状态，已结算金额，未结算金额
                for (int c=0;c<gzDynamicObjects.length;c++) {
                    //应收挂帐单头数据包
                    DynamicObject gzInfo = BusinessDataServiceHelper.loadSingle(gzDynamicObjects[c].getPkValue(), gzDynamicObjects[c].getDynamicObjectType().getName());
                    //获取预付借款单id
                    String yfjkId = gzInfo.getString("sourcebillid");
                    if (!StringUtils.isEmpty(yfjkId)) {
                        //存储预付借款单分录id和付款处理分录实付金额
                        HashMap<Long,BigDecimal> yfjkMap = new HashMap<>();
                        //循环应收挂帐单付款明细分录
                        for (DynamicObject entryentity : gzInfo.getDynamicObjectCollection("entry")) {
                            //获取分录实付金额
                            BigDecimal nckd_amountfield5 = amountMap.get(entryentity.getLong("id")) == null ? BigDecimal.ZERO:amountMap.get(entryentity.getLong("id"));
                            //获取未结算金额
                            BigDecimal nckd_amountfield7 = entryentity.getBigDecimal("nckd_amountfield7");
                            nckd_amountfield7 = nckd_amountfield7.subtract(nckd_amountfield5);
                            //反写分录已付金额，未付金额
                            entryentity.set("nckd_amountfield5",nckd_amountfield5);
                            entryentity.set("nckd_amountfield7",nckd_amountfield7);
                            Long yfjkSourceBillEntryId = entryentity.getLong("e_srcentryid");
                            yfjkMap.put(yfjkSourceBillEntryId,nckd_amountfield5);
                        }
                        SaveServiceHelper.save(new DynamicObject[]{gzInfo});

                        //查询预付借款单
                        Long prebillId = Long.valueOf(yfjkId);
                        QFilter nckd_yfjkFilter = new QFilter("id", QCP.equals,prebillId);
                        DynamicObject[] yfjkDynamicObjects = BusinessDataServiceHelper.load(nckd_er_prepaybill_ext,"id",new QFilter[] {nckd_yfjkFilter});
                        //循环数组获取结算状态，已结算金额，未结算金额
                        for (int d=0;d<yfjkDynamicObjects.length;d++) {
                            BigDecimal nckd_amountfield5 = BigDecimal.ZERO;
                            BigDecimal nckd_amountfield6 = BigDecimal.ZERO;
                            //预付借款单头数据包
                            DynamicObject yfjkInfo = BusinessDataServiceHelper.loadSingle(yfjkDynamicObjects[d].getPkValue(), yfjkDynamicObjects[d].getDynamicObjectType().getName());
                            //循环预付借款单预付借款信息分录
                            for (DynamicObject entryentity : yfjkInfo.getDynamicObjectCollection("expenseentryentity")) {
                                //获取分录实付金额
                                BigDecimal nckd_amountfield4 = yfjkMap.get(entryentity.getLong("id")) == null ? BigDecimal.ZERO:yfjkMap.get(entryentity.getLong("id"));
                                //获取未结算金额
                                BigDecimal nckd_amountfield3 = entryentity.getBigDecimal("nckd_amountfield3");
                                nckd_amountfield3 = nckd_amountfield3.subtract(nckd_amountfield4);
                                //反写分录已结算金额，未结算金额
                                entryentity.set("nckd_amountfield4",nckd_amountfield4);
                                entryentity.set("nckd_amountfield3",nckd_amountfield3);
                                nckd_amountfield5 = nckd_amountfield5.add(nckd_amountfield4);
                                nckd_amountfield6 = nckd_amountfield6.add(nckd_amountfield3);
                            }
                            yfjkInfo.set("nckd_amountfield5", nckd_amountfield5);
                            yfjkInfo.set("nckd_amountfield6", nckd_amountfield6);
                            yfjkInfo.set("billstatus", "G");
                            SaveServiceHelper.save(new DynamicObject[]{yfjkInfo});
                        }
                    }
                }
            }//应付方向的回写
            else if (bxDynamicObjects.length > 0) {
                //循环数组获取结算状态，已结算金额，未结算金额
                for (int c=0;c<bxDynamicObjects.length;c++) {
                    //报销单头数据包
                    DynamicObject bxInfo = BusinessDataServiceHelper.loadSingle(bxDynamicObjects[c].getPkValue(), bxDynamicObjects[c].getDynamicObjectType().getName());
                    String nckd_sourcebillid = bxInfo.getString("nckd_sourcebillid");
                    //报账类型等于工程报账回写合同台账单
                    if (!StringUtils.isEmpty(nckd_sourcebillid)) {
                        //获取合同台账单id
                        Long contractbillId = Long.parseLong(nckd_sourcebillid);
                        //查询合同台账单
                        QFilter htFilter = new QFilter("id", QCP.equals,contractbillId);
                        DynamicObject[] htDynamicObjects = BusinessDataServiceHelper.load(er_contractbill,"id",new QFilter[] {htFilter});
                        for (int j=0;j<htDynamicObjects.length;j++) {
                            //合同台账单头数据包
                            DynamicObject htInfo = BusinessDataServiceHelper.loadSingle(htDynamicObjects[j].getPkValue(), htDynamicObjects[j].getDynamicObjectType().getName());
                            //反写付款明细
                            DynamicObjectCollection entryEntity = htInfo.getDynamicObjectCollection("nckd_payentry");
                            DynamicObject entry1 = entryEntity.addNew();
                            entry1.set("nckd_pay_billno1", billno);//付款处理单据编号
                            entry1.set("nckd_pay_id1", id);//付款处理单据id
                            entry1.set("nckd_amountfield1", actpayamt);//付款金额
                            entry1.set("nckd_timefield2", paydate);//付款日期
                            SaveServiceHelper.save(new DynamicObject[]{htInfo});
                        }
                    }
                }
            }
        }
    }
}
