package com.yem.wm.im.giftamount;

import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.GiftAmount;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.kie.internal.ChangeSet;

import java.math.BigDecimal;
import java.util.EventObject;

/**
 * @Description 礼品资金池
 */
public class GiftAmountFormPlugin extends AbstractBillPlugIn {

//    @Override
//    public void afterCreateNewData(EventObject e){
//        calculategiftamount();
//    }
//
//    @Override
//    public void afterBindData(EventObject e) {
//        calculategiftamount();
//    }
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String properName = e.getProperty().getName();
        ChangeData changeSet = e.getChangeSet()[0];
        DynamicObject datas = this.getModel().getDataEntity(true);
        int currentRowIndex = this.getModel().getEntryCurrentRowIndex("yem_im_giftamount_o");
        DynamicObjectCollection giftamount = datas.getDynamicObjectCollection("yem_im_giftamount_o");
        switch (properName){

            case "yem_sneygiftenable":
                if(giftamount != null && giftamount.size() >0){
                    DynamicObject entryDy = giftamount.get(currentRowIndex);
                    if(entryDy != null){
                        BigDecimal sum = BigDecimal.ZERO;
                        DynamicObjectCollection enablegiftinfor = entryDy.getDynamicObjectCollection("yem_enablegiftinfor");
                        for(DynamicObject temp:enablegiftinfor){
                            sum = sum.add(temp.getBigDecimal("yem_sneygiftenable"));
                        }
                        this.getModel().setValue("yem_entrygiftenable",sum,currentRowIndex);
                    }
                }
                break;
            case "yem_crsygiftenable":
                if(giftamount != null && giftamount.size() >0){
                    DynamicObject entryDy = giftamount.get(currentRowIndex);
                    if(entryDy != null){
                        BigDecimal sum = BigDecimal.ZERO;
                        DynamicObjectCollection enablegiftinfor = entryDy.getDynamicObjectCollection("yem_enablegiftinfor");
                        for(DynamicObject temp:enablegiftinfor){
                            sum = sum.add(temp.getBigDecimal("yem_crsygiftenable"));
                        }
                        this.getModel().setValue("yem_sneycrgiftenable",sum,currentRowIndex);
                    }
                }
                break;
            default :
                break;
        }
    }

    /**
     * @description 打开表单子分录可用礼品金额汇总至 分录礼品金额
     * @author power
     * @time 2023/10/9 15:05
     */
    public void calculategiftamount() {
//        DynamicObject datas = this.getModel().getDataEntity(true);
//        DynamicObjectCollection giftamount = datas.getDynamicObjectCollection("yem_im_giftamount_o");
//        Long customer = datas.getLong("yem_customer.id");
//        Long org = datas.getLong("org.id");
//        Long products = datas.getLong("yem_bd_products.id");
////        int i=0;
//        for (DynamicObject entryDy:giftamount) {
//            BigDecimal sum = BigDecimal.ZERO;
//            BigDecimal sums = BigDecimal.ZERO;
//            DynamicObjectCollection enablegiftinfor = entryDy.getDynamicObjectCollection("yem_enablegiftinfor");
//            for(DynamicObject temp:enablegiftinfor){
//                sum = sum.add(temp.getBigDecimal("yem_sneygiftenable"));
//                sums = sums.add(temp.getBigDecimal("yem_crsygiftenable"));
//            }
//            entryDy.set("yem_entrygiftenable",sum);
//            entryDy.set("yem_sneycrgiftenable",sums);
//
//            SaveServiceHelper.save(new DynamicObject[] {entryDy});
//            GiftAmount.Calculate(customer,org,products);
//        }
    }




}
