From b3f96013d48e25afffb4cc7726055464a8ad2b24 Mon Sep 17 00:00:00 2001
From: pycook <pycook@126.com>
Date: Thu, 28 Nov 2019 21:17:06 +0800
Subject: [PATCH] relation view [doing]

---
 api/lib/cmdb/preference.py                 |  4 +-
 api/views/cmdb/preference.py               |  8 ++--
 ui/src/config/router.config.js             |  2 +-
 ui/src/views/cmdb/relation_views/index.vue | 56 ++++++++++++++--------
 4 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/api/lib/cmdb/preference.py b/api/lib/cmdb/preference.py
index 91d0970..3597ea6 100644
--- a/api/lib/cmdb/preference.py
+++ b/api/lib/cmdb/preference.py
@@ -114,8 +114,10 @@ class PreferenceManager(object):
     def get_relation_view():
         views = PreferenceRelationView.get_by(to_dict=True)
         result = dict()
+        name2id = list()
         for view in views:
             result.setdefault(view['name'], []).extend(json.loads(view['cr_ids']))
+            name2id.append([view['name'], view['id']])
 
         id2type = dict()
         for view_name in result:
@@ -129,7 +131,7 @@ class PreferenceManager(object):
         for type_id in id2type:
             id2type[type_id] = CITypeCache.get(type_id).to_dict()
 
-        return result, id2type
+        return result, id2type, sorted(name2id, key=lambda x: x[1])
 
     @classmethod
     def create_or_update_relation_view(cls, name, cr_ids):
diff --git a/api/views/cmdb/preference.py b/api/views/cmdb/preference.py
index 3a39717..3bb0f43 100644
--- a/api/views/cmdb/preference.py
+++ b/api/views/cmdb/preference.py
@@ -71,18 +71,18 @@ class PreferenceRelationApiView(APIView):
     url_prefix = "/preference/relation/view"
 
     def get(self):
-        views, id2type = PreferenceManager.get_relation_view()
+        views, id2type, name2id = PreferenceManager.get_relation_view()
 
-        return self.jsonify(views=views, id2type=id2type)
+        return self.jsonify(views=views, id2type=id2type, name2id=name2id)
 
     @role_required(RoleEnum.CONFIG)
     @args_required("name")
     def post(self):
         name = request.values.get("name")
         cr_ids = request.values.get("cr_ids")
-        views, id2type = PreferenceManager.create_or_update_relation_view(name, cr_ids)
+        views, id2type, name2id = PreferenceManager.create_or_update_relation_view(name, cr_ids)
 
-        return self.jsonify(views, id2type)
+        return self.jsonify(views=views, id2type=id2type, name2id=name2id)
 
     def put(self):
         return self.post()
diff --git a/ui/src/config/router.config.js b/ui/src/config/router.config.js
index 1408881..e9d9997 100644
--- a/ui/src/config/router.config.js
+++ b/ui/src/config/router.config.js
@@ -21,7 +21,7 @@ const cmdbRouter = [
     hideChildrenInMenu: true,
     children: [
       {
-        path: '/relation_views/:id',
+        path: '/relation_views/:viewId',
         name: 'cmdb_relation_views_item',
         component: () => import('@/views/cmdb/relation_views'),
         meta: { title: '关系视图', keepAlive: true },
diff --git a/ui/src/views/cmdb/relation_views/index.vue b/ui/src/views/cmdb/relation_views/index.vue
index 69c92c6..c7cb06f 100644
--- a/ui/src/views/cmdb/relation_views/index.vue
+++ b/ui/src/views/cmdb/relation_views/index.vue
@@ -1,13 +1,13 @@
 <template>
   <a-card :bordered="false">
-    <a-menu v-model="current" mode="horizontal" v-if="ciTypes.length">
-      <a-menu-item :key="ciType.id" v-for="ciType in ciTypes">
+    <a-menu v-model="current" mode="horizontal" v-if="relationViews.name2id && relationViews.name2id.length">
+      <a-menu-item :key="item[1]" v-for="item in relationViews.name2id">
         <router-link
-          :to="{name: 'cmdb_tree_views_item', params: {typeId: ciType.id}}"
-        >{{ ciType.alias || ciTypes.name }}</router-link>
+          :to="{name: 'cmdb_relation_views_item', params: { viewId: item[1]} }"
+        >{{ item[0] }}</router-link>
       </a-menu-item>
     </a-menu>
-    <a-alert message="请先到 我的订阅 页面完成订阅!" banner v-else></a-alert>
+    <a-alert message="管理员 还未配置关系视图!" banner v-else></a-alert>
     <div style="clear: both; margin-top: 20px"></div>
     <template>
       <a-row :gutter="8">
@@ -37,7 +37,7 @@
 <script>
 import { STable } from '@/components'
 
-import { getSubscribeTreeView, getSubscribeAttributes } from '@/api/cmdb/preference'
+import { getRelationView, getSubscribeAttributes } from '@/api/cmdb/preference'
 import { searchCI } from '@/api/cmdb/ci'
 export default {
   components: { STable },
@@ -47,8 +47,11 @@ export default {
       triggerSelect: false,
       treeNode: null,
       ciTypes: [],
+      relationViews: {},
       levels: [],
       typeId: null,
+      viewId: null,
+      viewName: null,
       current: [],
       instanceList: [],
       treeKeys: [],
@@ -59,9 +62,11 @@ export default {
       scrollY: 0,
 
       loadInstances: parameter => {
+        console.log(parameter, 'load instances')
         const params = parameter || {}
         // const params = Object.assign(parameter, this.$refs.search.queryParam)
         let q = `q=_type:${this.typeId}`
+        console.log(params, 'params')
         Object.keys(params).forEach(key => {
           if (!['pageNo', 'pageSize', 'sortField', 'sortOrder'].includes(key) && params[key] + '' !== '') {
             if (typeof params[key] === 'object' && params[key].length > 1) {
@@ -121,20 +126,21 @@ export default {
   },
 
   created () {
-    this.getCITypes()
+    this.getRelationViews()
   },
 
   inject: ['reload'],
   watch: {
     '$route.path': function (newPath, oldPath) {
-      this.typeId = this.$route.params.typeId
-      this.getCITypes()
+      this.viewId = this.$route.params.viewId
+      this.getRelationViews()
       this.reload()
     }
   },
 
   methods: {
     onSelect (keys) {
+      console.log('onSelect')
       this.triggerSelect = true
       if (keys.length) {
         this.treeKeys = keys[0].split('-').filter(item => item !== '')
@@ -143,6 +149,7 @@ export default {
       this.$refs.table.refresh(true)
     },
     wrapTreeData (facet) {
+      console.log('wrapTreeData')
       if (this.triggerSelect) {
         return
       }
@@ -169,7 +176,7 @@ export default {
         rows = document.querySelector('.ant-table-body').childNodes[0].childNodes[1].childNodes[0].childNodes
       }
       let scrollX = 0
-
+      console.log(rows, 'rows')
       const columns = Object.assign([], this.columns)
       for (let i = 0; i < rows.length; i++) {
         columns[i].width = rows[i].offsetWidth < 80 ? 80 : rows[i].offsetWidth
@@ -182,6 +189,7 @@ export default {
     },
 
     onLoadData (treeNode) {
+      console.log(treeNode, 'load data')
       this.triggerSelect = false
       return new Promise(resolve => {
         if (treeNode.dataRef.children) {
@@ -195,20 +203,28 @@ export default {
       })
     },
 
-    getCITypes () {
-      getSubscribeTreeView().then(res => {
-        this.ciTypes = res
-        if (this.ciTypes.length) {
-          this.typeId = this.$route.params.typeId || this.ciTypes[0].id
-          this.current = [this.typeId]
-          this.loadColumns()
-          this.levels = res.find(item => item.id === this.typeId).levels
-          this.$refs.table && this.$refs.table.refresh(true)
+    getRelationViews () {
+      getRelationView().then(res => {
+        this.relationViews = res
+
+        if ((Object.keys(this.relationViews.views) || []).length) {
+          this.viewId = parseInt(this.$route.params.viewId) || this.relationViews.name2id[0][1]
+          this.relationViews.name2id.forEach(item => {
+            if (item[1] === this.viewId) {
+              this.viewName = item[0]
+            }
+          })
+          this.levels = this.relationViews.views[this.viewName]
+          this.current = [this.levels[0]]
+          this.typeId = this.levels[0]
+          console.log(this.levels, 'levels')
+          // this.loadColumns()
+          // this.$refs.table && this.$refs.table.refresh(true)
         }
       })
     },
     loadColumns () {
-      getSubscribeAttributes(this.typeId).then(res => {
+      getSubscribeAttributes(this.levels[this.levels.length - 1]).then(res => {
         const prefAttrList = res.attributes
 
         const columns = []