relationClsAttrEditor.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <div class="flex-col">
  3. <h3 style="font-weight: 400;color: #1f2f3d; margin: 0px 0 20px;">
  4. <a-icon type="info-circle" class="warnIcons" theme="filled" />
  5. 关联关系
  6. </h3>
  7. <div style="margin-bottom: 10px">
  8. <a-button type="primary" @click="addEnt" style="margin-right: 10px">新增关联实体</a-button>
  9. <a-button type="primary" @click="selectEnt" style="margin-right: 10px">选择关联实体</a-button>
  10. <a-button type="primary" @click="dels">删除</a-button>
  11. </div>
  12. <a-table :pagination="false" :loading="loading" :columns="columns" :data-source="data" :row-selection="{ onChange: onTableSelect }">
  13. <span slot="entName" slot-scope="text, record">
  14. {{text}}
  15. <!-- <a @click="openNewPage(record)">{{text}}</a> -->
  16. </span>
  17. <span slot="action" slot-scope="text, record">
  18. <!-- <a @click="edit(record)" style="margin-right: 5px">编辑</a> -->
  19. <a-popconfirm title="此操作无法恢复,请确认是否删除?"
  20. @confirm="() => del(record)">
  21. <a href="javascript:;" class="spanMarginl">删除</a>
  22. </a-popconfirm>
  23. </span>
  24. </a-table>
  25. <a-row style="margin-top: 30px;">
  26. <a-col>
  27. <a-pagination
  28. v-model="pagination.current"
  29. :total="pagination.total"
  30. :pageSize="pagination.pageSize"
  31. @change="onChangePagination"/>
  32. </a-col>
  33. </a-row>
  34. </div>
  35. </template>
  36. <script>
  37. import { getRelationAttr } from '@/api/graph/entity'
  38. import { addRelBatch, deleteEntityRelation } from '@/api/graph/relationClass'
  39. export default {
  40. name: "relationClsAttrEditor",
  41. props: ['entId', 'relClsId', 'direction', 'isModel', 'clsId', 'otherVID'],
  42. data() {
  43. return {
  44. data: [],
  45. columns: [
  46. {
  47. title: '实体名称',
  48. dataIndex: 'entName',
  49. key: 'entName',
  50. scopedSlots: {customRender: 'entName'},
  51. },
  52. {
  53. title: '操作',
  54. key: 'action',
  55. scopedSlots: {customRender: 'action'},
  56. },
  57. ],
  58. loading: false,
  59. selectRows: [],
  60. columnsIdCache: [],
  61. columnsCache: [],
  62. pagination: {
  63. pageSize: 9,
  64. total: 100,
  65. current: 1
  66. }
  67. }
  68. },
  69. mounted() {
  70. this.getAttr()
  71. },
  72. methods: {
  73. // 获取属性
  74. getAttr() {
  75. const vm = this
  76. this.loading = true
  77. getRelationAttr(this.entId, this.relClsId, this.direction, this.isModel, this.pagination.current, this.pagination.pageSize).then(res => {
  78. const attrClsNameList = {}
  79. this.pagination.total = res.total
  80. // 组装表格columns
  81. res.headers.reverse().forEach(item => {
  82. if (vm.columnsIdCache.indexOf(item.attrClsId) === -1) {
  83. attrClsNameList[item.attrClsId] = item.attrClsName
  84. vm.columnsIdCache.push(item.attrClsId)
  85. vm.columnsCache.push({
  86. attrClsId: item.attrClsId,
  87. attrClsName: item.attrClsName
  88. })
  89. vm.columns.splice(1, 0, {
  90. dataIndex: item.attrClsId,
  91. key: item.attrClsId,
  92. title: item.attrClsName
  93. })
  94. }
  95. })
  96. res.data.forEach(item => {
  97. item.key = item.entID
  98. // 新建的关系类没有属性,给一个空的属性列表,方便后面操作
  99. item.attrList ? item.attrList.forEach(attr => {
  100. item[attr.attrClsId] = attr.attrValue
  101. item[attr.attrClsId + '_name'] = attrClsNameList[attr.attrClsId]
  102. }) : item.attrList = []
  103. })
  104. vm.data = res.data
  105. vm.loading = false
  106. })
  107. },
  108. openNewPage(record) {
  109. const params = this.$route.params
  110. const query = this.$route.query
  111. const location = window.location
  112. const url = location.origin + location.pathname + '#/model/' + params.classId + '/entity_model/' + record.entID + '?page=' + query.page + '&clsName=' + query.clsName + '&entName=' + record.entName + '&isModel=' + query.isModel
  113. window.open(url)
  114. },
  115. // 表格选中行后的回调
  116. onTableSelect(selectedRowKeys, selectedRows) {
  117. this.selectRows = selectedRows
  118. },
  119. // 分页变更
  120. onChangePagination(current) {
  121. this.pagination.current = current
  122. this.getAttr()
  123. },
  124. // 新增实体
  125. addEnt() {
  126. this.$open(
  127. 'addEntityModal',
  128. {
  129. title: '新增实体',
  130. paramsId: this.otherVID, // 实体类id
  131. isModel: this.isModel
  132. },
  133. function (data) {
  134. if (data) {
  135. this.addRelEnt(data)
  136. }
  137. }
  138. )
  139. },
  140. // 新增关系类实体
  141. addRelEnt(data) {
  142. let entityRelations = []
  143. let startEntId;
  144. let endEntId;
  145. if (this.direction === 'START') {
  146. startEntId = this.entId
  147. endEntId = data
  148. } else {
  149. startEntId = data
  150. endEntId = this.entId
  151. }
  152. let entityRelation = {
  153. startEntId,
  154. endEntId,
  155. isModel: this.isModel,
  156. relClsId: this.relClsId
  157. }
  158. entityRelations.push(entityRelation)
  159. addRelBatch(entityRelations).then(res => {
  160. if (res) {
  161. this.getAttr()
  162. }
  163. })
  164. },
  165. // 选择实体
  166. selectEnt() {
  167. this.$open(
  168. 'addRelBatchModal',
  169. {
  170. relClsId: this.relClsId, // 实体类id
  171. isModel: this.isModel,
  172. direction: this.direction,
  173. entId: this.entId
  174. },
  175. function (data) {
  176. if (data) {
  177. this.getAttr()
  178. }
  179. }
  180. )
  181. },
  182. // 编辑
  183. edit(record) {
  184. // 如果新增实体,没有属性类
  185. if (record.attrList.length === 0) {
  186. this.columnsCache.forEach(cls => {
  187. record.attrList.push({
  188. attrClsId: cls.attrClsId,
  189. attrValue: ''
  190. })
  191. record[cls.attrClsId] = ''
  192. record[cls.attrClsId + '_name'] = cls.attrClsName
  193. })
  194. }
  195. this.columnsCache.forEach(cls => {
  196. let hasCls = false;
  197. record.attrList.forEach(attr => {
  198. if (attr.attrClsId === cls.attrClsId) hasCls = true
  199. })
  200. if (!hasCls) {
  201. record.attrList.push({
  202. attrClsId: cls.attrClsId,
  203. attrValue: ''
  204. })
  205. record[cls.attrClsId] = ''
  206. record[cls.attrClsId + '_name'] = cls.attrClsName
  207. }
  208. })
  209. this.$open(
  210. 'editTemplateRelAttrbatchModal',
  211. {
  212. record: record, // 实体类id
  213. isModel: this.isModel
  214. },
  215. function (data) {
  216. if (data) {
  217. this.getAttr()
  218. }
  219. }
  220. )
  221. },
  222. // 批量删除
  223. dels() {
  224. const ids = []
  225. this.selectRows.forEach(row => {
  226. ids.push(row.relId)
  227. })
  228. deleteEntityRelation(ids, this.isModel).then(res => {
  229. if (res) {
  230. this.$notification["success"]({
  231. message: "删除成功"
  232. });
  233. this.getAttr()
  234. }
  235. })
  236. },
  237. // 表格删除
  238. del(record) {
  239. const relIds = [record.relId]
  240. deleteEntityRelation(relIds, this.isModel).then(res => {
  241. if (res) {
  242. this.$notification["success"]({
  243. message: "删除成功"
  244. });
  245. this.getAttr()
  246. }
  247. })
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped>
  253. </style>