package com.yem.wm.im.dealeintimeledge.form;

import com.grapecity.documents.excel.Q;
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.RefBillEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * @author 张立攀
 * @version 1.0
 * @description   经销商实时台账
 * @date 2023-10-10 14:15
 */
public class DealeInTimeLedge extends AbstractBillPlugIn implements BeforeF7SelectListener {
    @Override
    public void registerListener(EventObject e) {
        RefBillEdit products = this.getView().getControl("yem_contractnumber");
        if (products != null) {
            products.addBeforeF7SelectListener(this);
        }
        BasedataEdit  model = this.getView().getControl("yem_model");
        if (model != null) {
            model.addBeforeF7SelectListener(this);
        }
        BasedataEdit  orderno = this.getView().getControl("yem_orderno");
        if (orderno != null) {
            orderno.addBeforeF7SelectListener(this);
        }


    }
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        ChangeData changeData = e.getChangeSet()[0];
        String name = e.getProperty().getName();
        int rowIndex = changeData.getRowIndex();
        switch (name){
            //选择合同号时，携带该合同的贸易国别、销售员字段
            case "yem_contractnumber":
                DynamicObject yemContractnumber = (DynamicObject) this.getModel().getValue("yem_contractnumber", rowIndex);
                if(yemContractnumber != null){
                    DynamicObject object = BusinessDataServiceHelper.loadSingle("yem_es_salesorder","yem_clientcountry,yem_operator",new QFilter[]{
                       new QFilter("billstatus", QCP.equals,"C"),
                            new QFilter("yem_cancelstatus",QCP.equals,"A"),
                            new QFilter("yem_closestatus",QCP.equals,"A"),
                            new QFilter("billno",QCP.equals,yemContractnumber.getString("billno"))
                    });
                    if(object != null){
                        DynamicObject clientcountry = (DynamicObject) object.get("yem_clientcountry");
                        DynamicObject operator = (DynamicObject) object.get("yem_operator");
                        if(clientcountry != null){
                            this.getModel().setValue("yem_country",clientcountry.getPkValue(),rowIndex);
                        }
                        if(operator != null){
                            this.getModel().setValue("yem_salesman",operator.getPkValue(),rowIndex);
                        }
                    }
                }

                break;
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        DynamicObject datas = this.getModel().getDataEntity(true);
        int row = e.getRow();
        String key = e.getProperty().getName();
        switch (key) {
            case "yem_contractnumber":
                DynamicObject yemCustomername = datas.getDynamicObject("yem_customername");
                if(yemCustomername != null){
                    e.addCustomQFilter(
                            new QFilter("yem_customer",QCP.equals,yemCustomername.getPkValue())
                    );
                }
                break;
            case "yem_model":
                DynamicObject contractnumber  = (DynamicObject) this.getModel().getValue("yem_contractnumber",row);
                List<Long> list = new ArrayList<>();
                if(contractnumber != null){
                    DynamicObject object = BusinessDataServiceHelper.loadSingle(contractnumber.getPkValue(), contractnumber.getDynamicObjectType().getName());
                    if(object != null){
                        DynamicObjectCollection dynamicObjectCollection1 = object.getDynamicObjectCollection("yem_es_materialinfo");
                        for(DynamicObject dc:dynamicObjectCollection1){
                            DynamicObject yemProductmodel = dc.getDynamicObject("yem_productmodel");
                            if(yemProductmodel != null){
                                DynamicObject yemProducts = yemProductmodel.getDynamicObject("yem_products");
                                if(yemProducts != null){
                                    list.add(yemProducts.getLong("id"));
                                }
                            }
                        }
                        e.addCustomQFilter(new QFilter("id",QCP.in,list));
                    }
                }
                break;
            case "yem_orderno":
                DynamicObject contractnumber1  = (DynamicObject) this.getModel().getValue("yem_contractnumber",row);
                if(contractnumber1 != null){
                    DynamicObject object = BusinessDataServiceHelper.loadSingle(contractnumber1.getPkValue(), contractnumber1.getDynamicObjectType().getName());
                    if(object != null){
                        DynamicObjectCollection dynamicObjectCollection1 = object.getDynamicObjectCollection("yem_es_materialinfo");
                        List<Object> lists = new ArrayList<>();
                        for(DynamicObject dt:dynamicObjectCollection1){
                            DynamicObject yemProductmodel = dt.getDynamicObject("yem_productmodel");
                            if(yemProductmodel != null){
                                lists.add(yemProductmodel.getPkValue());
                            }
                        }
                        e.addCustomQFilter(
                                new QFilter("id",QCP.in,lists)
                        );
                    }
                }
                break;
        }
    }
}
