package com.yem.wm.bd.basemodelib.from;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.Control;
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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author zhouc
 * @date 2023/7/27 19:48
 * @className basemodelibEdit
 * @description
 */
public class basemodelibEdit extends AbstractBillPlugIn implements BeforeF7SelectListener {
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String propertyName = e.getProperty().getName();
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int rowIndex = changeData[0].getRowIndex();
        switch (propertyName) {
            case "yem_products":
                DynamicObject yem_products = (DynamicObject) this.getModel().getValue("yem_products");
                if (yem_products != null) {
                    DynamicObject group = yem_products.getDynamicObject("group");
                    if (group != null) {
                        String longnumber = group.getString("longnumber");
                        if (!longnumber.equals("")) {
                            String[] groupnumber = longnumber.split("\\.");
                            if(groupnumber.length>0){
                                QFilter qFilter = new QFilter("number", QCP.equals, groupnumber[0]);
                                DynamicObject[] productsgroup = BusinessDataServiceHelper.load("yem_bd_productsgroup",
                                        "id,name,number", new QFilter[]{qFilter}, null);
                                if(productsgroup.length>0){
                                    this.getView().getModel().setValue("yem_productsgroup",productsgroup[0].getLong("id"));
                                }
                            }
                        }
                        else{
                            this.getView().getModel().setValue("yem_productsgroup",group);
                        }
                    }
                }
                break;
        }
    }


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        //产品机型beforef7Select事件监听
        BasedataEdit supplies = this.getControl("yem_products");//产品机型
        supplies.addBeforeF7SelectListener(this);

    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        Control control = (Control) e.getSource();
        String key = control.getKey();
//        if ("".equals(key)) {
//            DynamicObject yem_products = (DynamicObject) this.getModel().getValue("yem_products");
//            if (yem_products != null) {
//                DynamicObject group = yem_products.getDynamicObject("group");
//                if (group != null) {
//                    String longnumber = group.getString("longnumber");
//
//                }
//            }
//        }
    }
}
