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


import com.yem.em.utils.EmptyUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.RefreshNodeEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.list.IListView;
import kd.bos.list.ITreeListView;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.list.plugin.AbstractTreeListPlugin;
import kd.bos.mvc.list.ListView;
import kd.bos.mvc.list.TreeListModel;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.basedata.BaseDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * @author 夜子
 * @ClassName DevicefilesList
 * @description: 设备档案表单插件
 * @date 2023年07月12日
 * @version: 1.0
 */
public class DevicefilesList extends AbstractTreeListPlugin {

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
        IListView listview = (IListView) this.getView();
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        if ("writein".equals(operateKey)) {
            ListSelectedRowCollection selectedRows = listview.getSelectedRows();
            Object[] primaryKeyValues = selectedRows.getPrimaryKeyValues();
            for (Object pkvalue : primaryKeyValues) {
                DynamicObject bill = BusinessDataServiceHelper.loadSingle(pkvalue, "yem_em_devicefiles");
                DynamicObject ecatalog = bill.getDynamicObject("yem_model");
                if (ecatalog != null) {
                    DynamicObject etcmanual = BusinessDataServiceHelper.loadSingle(ecatalog.getPkValue(),
                            "yem_em_etcmanual");
                    String picturefield = etcmanual.getString("yem_picture");
                    if (EmptyUtils.isNotEmpty(picturefield)) {
                        bill.set("yem_picture", picturefield);
                    }
                }
                SaveServiceHelper.save(new DynamicObject[]{bill});
            }
        }
        if ("syncpic".equals(operateKey)) {
            this.getView().invokeOperation("refresh");
        }
    }

    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);
        QFilter qFilters = new QFilter("yem_businesssort", QCP.equals, "Z");
        ListShowParameter listShowParameter = (ListShowParameter) e.getFormShowParameter();
        listShowParameter.getTreeFilterParameter().getQFilters().add(qFilters);
    }

    @Override
    public void refreshNode(RefreshNodeEvent e) {
        super.refreshNode(e);
        if (this.getControlFilters() != null) {
            List<QFilter> filter = this.getTreeModel().getTreeFilter();
            filter.clear();
            QFilter sourceFilter = new QFilter("yem_businesssort","=","Z");
            filter.add(sourceFilter);
        }
    }
}
