package com.yem.wm.im.yearlybudget.form;
import com.kingdee.cosmic.ctrl.common.CtrlUtil;
import com.yem.wm.utils.OrgFilterUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.org.utils.OrgBillListCommon;
import kd.bos.org.utils.OrgTreeUtils;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EventObject;
import java.util.List;

/**
 * @author 张立攀
 * @ClassName YearlyBudgetFormPlguin
 * @description: 年度预算单
 * @date 2023年07月20日
 * @version: 1.0
 */
public class YearlyBudgetFormPlguin extends AbstractBillPlugIn implements BeforeF7SelectListener {
    //国外客户分组集合
    private static final  String[] customers = {""};
    @Override
    public void registerListener(EventObject e) {
        BasedataEdit yem_productmodel = this.getView().getControl("yem_productmodel");
        if (yem_productmodel != null) {
            yem_productmodel.addBeforeF7SelectListener(this);
        }
        BasedataEdit yem_salesman = this.getView().getControl("yem_salesman");
        if (yem_salesman != null) {
            yem_salesman.addBeforeF7SelectListener(this);
        }
        BasedataEdit yem_client = this.getView().getControl("yem_client");
        if (yem_client != null) {
            yem_client.addBeforeF7SelectListener(this);
        }

    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        DynamicObject datas = this.getModel().getDataEntity(true);
        String key = e.getProperty().getName();
        int row = e.getRow();
        DynamicObject org = datas.getDynamicObject("org");
        List<Long> orgList = OrgFilterUtils.getOrgList(org);

        switch (key) {
            case "yem_productmodel":
                //产品型号根据产品类型过滤
                DynamicObject types = datas.getDynamicObject("yem_bd_products");
                if (types == null) {
                    this.getView().showMessage("请先选择产品类型");
                    e.setCancel(true);
                } else {
                    QFilter qFilter = new QFilter("group.parent.id", QCP.equals, types.getLong("id"));
                   e.addCustomQFilter(qFilter);
                    //清空数据
                    this.getModel().setValue("yem_salesvolume", 0, row);
                    this.getModel().setValue("yem_price", 0, row);
                    this.getModel().setValue("yem_income", 0, row);
                    //清除当前子单据体
                    this.getModel().deleteEntryData("yem_im_monthlyplan");
                    this.getModel().createNewEntryRow("yem_im_monthlyplan");
                }
                break;
            case "yem_salesman":

                ListShowParameter listShowParameter = (ListShowParameter) e.getFormShowParameter();
                listShowParameter.getListFilterParameter().getQFilters().add(new QFilter("entryentity.dpt.id", QCP.in, orgList));

                if (!orgList.isEmpty()) {
                    e.getFormShowParameter().setCustomParam(OrgTreeUtils.PARAM_INIT_ROOT_ORG_ID, orgList.get(0));
                }
                break;
            case "yem_client":
//                QFilter qFilter = new QFilter("group.parent.number", QCP.equals,"110");
//                ListShowParameter parameter = (ListShowParameter) e.getFormShowParameter();
//                parameter.getListFilterParameter().setFilter(qFilter);
                break;
            default:
                break;
        }

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        //子分录标识
        ChangeData changeData = e.getChangeSet()[0];
        String proName = e.getProperty().getName();
        int row = changeData.getRowIndex();
        int paraRow = changeData.getParentRowIndex();
        DynamicObject dyEntry = this.getModel().getDataEntity(true);

        //根据单价*销量重新计算收入
        if (proName.equals("yem_price")) {
            Object ob = changeData.getNewValue();
            if (ob != null) {
                BigDecimal price = (BigDecimal) ob;
                Object dy = this.getModel().getValue("yem_salesvolume", row);
                if (dy != null) {
                    BigDecimal number = (BigDecimal) dy;
                    this.getModel().setValue("yem_income", price.multiply(number), row);
                }
            }
        }
        //汇总销量
        if (proName.equals("yem_salesvolume")) {
            DynamicObjectCollection dyColl = dyEntry.getDynamicObjectCollection("yem_im_detailedinfor");
            BigDecimal totalCount = BigDecimal.ZERO;
            for (DynamicObject dy : dyColl) {
                BigDecimal decimal = dy.getBigDecimal("yem_salesvolume");
                if (decimal != null) {
                    totalCount = totalCount.add(decimal);
                }

            }
            this.getModel().setValue("yem_salesnumber", totalCount);
        }
        //汇总收入
        if (proName.equals("yem_income")) {
            DynamicObjectCollection dyColl = dyEntry.getDynamicObjectCollection("yem_im_detailedinfor");
            BigDecimal totalCount = BigDecimal.ZERO;
            for (DynamicObject dy : dyColl) {
                BigDecimal decimal = dy.getBigDecimal("yem_income");
                if (decimal != null) {
                    totalCount = totalCount.add(decimal);
                }

            }
            BigDecimal totalIncome = totalCount.divide(new BigDecimal(10000));
            this.getModel().setValue("yem_sumincome", totalIncome);
        }
        //产品类型变化  清空产品型号
        if(proName.equals("yem_bd_products")){
            DynamicObjectCollection detailedinfor = dyEntry.getDynamicObjectCollection("yem_im_detailedinfor");
            if(detailedinfor != null && detailedinfor.size() >0){
                for(int i = 0 ; i < detailedinfor.size() ;i++){
                    this.getModel().setValue("yem_productmodel",null,i);
                }
            }

        }

    }




}