package com.yem.em.bd.electronicgroup.list;

import com.yem.em.utils.AtlasUtils;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import static com.yem.em.utils.AtlasUtils.seelctDyn;

/**
 * @author zhouc
 * @date 2023/10/24 20:15
 * @className ElectronicGroupListEdit
 * @description 配件图册分组列表插件
 */
public class ElectronicGroupListEdit extends AbstractListPlugin {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        //获取产品类型
        if (operateKey.equals("getproductsgroup")) {
            productsgroup();
//            Addelectronicgroup(null);
            this.getView().showSuccessNotification("获取产品类型操作完成！");
        }
        //获取产品机型
        if (operateKey.equals("getproductstree")) {
            productstree();
            this.getView().showSuccessNotification("获取产品机型操作完成！");
        }
        //修复配件图册
        if ("assequipment".equals(operateKey)) {
            QFilter qFilter = new QFilter("yem_brandmodel", QCP.equals, null);
//            qFilter.and(new QFilter("yem_eqctype", QCP.equals, null));
            DynamicObjectCollection collection = QueryServiceHelper.query("yem_em_assequipment",
                    "id,number,name,yem_brandmodel,yem_eqctype", new QFilter[]{qFilter}, null);
            for (int i = 0; i < collection.size(); i++) {
                DynamicObject object = collection.get(i);
                QFilter qFilter1 = new QFilter("number", QCP.equals, object.getString("number"));
                DynamicObject yem_bd_basemodelib = BusinessDataServiceHelper.loadSingle("yem_bd_basemodelib",
                        "id,number,name,yem_products.number,yem_productsgroup", qFilter1.toArray());
                if (yem_bd_basemodelib != null) {
                    DynamicObject object1 = BusinessDataServiceHelper.loadSingle(object.getString("id"), "yem_em_assequipment");
                    if (object1 != null) {
                        object1.set("yem_brandmodel", AtlasUtils.seelctDyn("yem_em_electronicgroup", "number", yem_bd_basemodelib.getString("yem_products.number")));
                        object1.set("yem_eqctype", yem_bd_basemodelib.getDynamicObject("yem_productsgroup"));//设备类别
                    }
                    OperationResult Operate = OperationServiceHelper.executeOperate("save",
                            object1.getDynamicObjectType().getName(), new DynamicObject[]{object1}, OperateOption.create());
                    if (Operate.isSuccess()) {

                    }
                }
            }
            this.getView().showSuccessNotification("修复配件图册完成！");
        }
        //修复整机档案
        if (operateKey.equals("devicefiles")) {
            QFilter qFilter = new QFilter("group", QCP.equals, null);
            DynamicObjectCollection collection = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,number,name,yem_model.yem_products.number", new QFilter[]{qFilter}, null);
            for (int i = 0; i < collection.size(); i++) {
                DynamicObject object = collection.get(i);
                DynamicObject object1 = BusinessDataServiceHelper.loadSingle(object.getString("id"), "yem_em_devicefiles");
                if (object1 != null) {
                    object1.set("group", seelctDyn("yem_em_electronicgroup", "number",
                            object.getString("yem_model.yem_products.number")));
                }
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        object1.getDynamicObjectType().getName(), new DynamicObject[]{object1}, OperateOption.create());
                if (Operate.isSuccess()) {

                }
            }
            this.getView().showSuccessNotification("修复整机档案完成！");
        }
    }

    public void productsgroup() {
        QFilter qFilter = new QFilter("enable", QCP.equals, "1");
        DynamicObjectCollection collection = QueryServiceHelper.query("yem_bd_productsgroup",
                "id,number,name,yem_businesssort,parent.number", new QFilter[]{qFilter}, "longnumber");
        if (collection.size() > 0) {
            for (int i = 0; i < collection.size(); i++) {
                AtlasUtils.Addelectronicgroup(collection.get(i), "productsgroup");
            }
        }
    }

    public void productstree() {
        QFilter qFilter = new QFilter("enable", QCP.equals, "1");
        DynamicObjectCollection collection = QueryServiceHelper.query("yem_bd_productstree",
                "id,number,name,yem_classification,group.number", new QFilter[]{qFilter}, null);
        if (collection.size() > 0) {
            for (int i = 0; i < collection.size(); i++) {
                AtlasUtils.Addelectronicgroup(collection.get(i), "productstree");
            }
        }
    }
}
