package com.yem.wm.utils;

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

/**
 * @author power
 * @ClassName
 * @description: 礼品资金池计算插件
 * @date 2023/7/29
 */
public class GiftAmount {

    /**
     * @description 通用礼品计算逻辑
     * @author power
     * return
     * @time 2023/9/28
     */
    public static  void Calculate(Long customer,Long org,Long yem_bd_products) {
        DynamicObject data = BusinessDataServiceHelper.loadSingle("yem_im_giftamount",
                new QFilter[]{new QFilter("yem_groupcusfields.id", QCP.equals, customer),
                        new QFilter("org", QCP.equals,org),
                        new QFilter("yem_bd_products", QCP.equals,yem_bd_products)});
        if (data != null){
            //礼品预计本位币金额  可用礼品本位币金额  累计已用金额  剩余可用金额
            BigDecimal sumAmountbase = new BigDecimal(0);
            BigDecimal sumSneycrgiftenable= new BigDecimal(0);
            BigDecimal sumAmountbaseamt= new BigDecimal(0);
            BigDecimal giftuseable= new BigDecimal(0);



            DynamicObjectCollection entry = data.getDynamicObjectCollection("yem_im_giftamount_o");
            DynamicObjectCollection entryt = data.getDynamicObjectCollection("yem_im_giftamount_t");
            if (entry !=  null){
                for (DynamicObject entrys:entry) {

                    //分录为 礼品额度申请 礼品退库申请 整机一次性赠送 时 可用金额 与可用金额本位币
                    BigDecimal sumyem_sney= new BigDecimal(0);
                    BigDecimal sumyem_crsy= new BigDecimal(0);

                    //分录为外销合同时 可用礼品金额与 礼品可用金额本位币
                    BigDecimal sumyem_sneygiftenable= new BigDecimal(0);
                    BigDecimal sumyem_crsygiftenable= new BigDecimal(0);

                    String origintype = entrys.getString("yem_origintype");
                    if ("B".equals(origintype)||"C".equals(origintype)||"D".equals(origintype)){
                        sumyem_sney = entrys.getBigDecimal("yem_entrygiftenable");
                        BigDecimal amountbase = entrys.getBigDecimal("yem_originamt");
                        sumAmountbase = sumAmountbase.add(amountbase);
                        sumSneycrgiftenable=sumSneycrgiftenable.add(sumyem_sney);
                    }
                    if ("A".equals(origintype)){

                        BigDecimal amountbase = entrys.getBigDecimal("yem_originamt");
                        sumAmountbase = sumAmountbase.add(amountbase);

                        DynamicObjectCollection enablegiftinfors = entrys.getDynamicObjectCollection("yem_enablegiftinfor");
                        for (DynamicObject enablegiftinfor:enablegiftinfors) {
                            BigDecimal yem_sneygiftenable = enablegiftinfor.getBigDecimal("yem_sneygiftenable");
                            sumyem_sneygiftenable =sumyem_sneygiftenable.add(yem_sneygiftenable);
                            BigDecimal yem_crsygiftenable = enablegiftinfor.getBigDecimal("yem_crsygiftenable");
                            sumyem_crsygiftenable = sumyem_crsygiftenable.add(yem_crsygiftenable);
                        }
                        entrys.set("yem_entrygiftenable",sumyem_sneygiftenable);
                        entrys.set("yem_sneycrgiftenable",sumyem_crsygiftenable);
                        entrys.set("seq",entrys.getInt("seq"));

                        sumSneycrgiftenable=sumyem_sneygiftenable.add(sumSneycrgiftenable);
                    }
//
//                    BigDecimal sneycrgiftenable = entrys.getBigDecimal("yem_sneycrgiftenable");
//                    sumSneycrgiftenable = sumSneycrgiftenable.add(sneycrgiftenable);

                }
            }
            if (entryt != null){
                for (DynamicObject entryts:entryt) {
                    BigDecimal amountbaseamt = entryts.getBigDecimal("yem_amountbaseamt");
                    sumAmountbaseamt = sumAmountbaseamt.add(amountbaseamt);
                }
            }


            giftuseable = sumSneycrgiftenable.subtract(sumAmountbaseamt);
            //礼品预计总金额 可用礼品总金额 累计已用金额  剩余可用金额
            data.set("yem_sumoriginamt",sumAmountbase);
            data.set("yem_giftenable",sumSneycrgiftenable);
            data.set("yem_giftuse",sumAmountbaseamt);
            data.set("yem_giftuseable",giftuseable);

            SaveServiceHelper.save(new DynamicObject[] {data});
        }
    }
}
