use-child-node.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { ref, onMounted } from 'vue'
  18. import { useI18n } from 'vue-i18n'
  19. import { uniqBy } from 'lodash'
  20. import {
  21. querySimpleList,
  22. queryProcessDefinitionByCode
  23. } from '@/service/modules/process-definition'
  24. import type { IJsonItem } from '../types'
  25. import { number } from 'echarts'
  26. export function useChildNode({
  27. model,
  28. projectCode,
  29. isCreate,
  30. from,
  31. processName,
  32. code
  33. }: {
  34. model: { [field: string]: any }
  35. projectCode: number
  36. isCreate: boolean
  37. from?: number
  38. processName?: number
  39. code?: number
  40. }): IJsonItem {
  41. const { t } = useI18n()
  42. const options = ref([] as { label: string; value: string }[])
  43. const loading = ref(false)
  44. const getProcessList = async () => {
  45. if (loading.value) return
  46. loading.value = true
  47. try {
  48. const res = await querySimpleList(projectCode)
  49. options.value = res.map((option: { name: string; code: number }) => ({
  50. label: option.name,
  51. value: option.code
  52. }))
  53. loading.value = false
  54. } catch (err) {
  55. loading.value = false
  56. }
  57. }
  58. const getProcessListByCode = async (processCode: number) => {
  59. if (!processCode) return
  60. try {
  61. const res = await queryProcessDefinitionByCode(processCode, projectCode)
  62. getTaskOptions(res)
  63. } catch (err) {}
  64. }
  65. const getTaskOptions = (processDefinition: {
  66. processTaskRelationList: []
  67. taskDefinitionList: []
  68. }) => {
  69. const { processTaskRelationList = [], taskDefinitionList = [] } =
  70. processDefinition
  71. const preTaskOptions: { code: number; name: string }[] = []
  72. const tasks: { [field: number]: string } = {}
  73. taskDefinitionList.forEach(
  74. (task: { code: number; taskType: string; name: string }) => {
  75. tasks[task.code] = task.name
  76. if (task.code === code) return
  77. if (
  78. task.taskType === 'CONDITIONS' &&
  79. processTaskRelationList.filter(
  80. (relation: { preTaskCode: number }) =>
  81. relation.preTaskCode === task.code
  82. ).length >= 2
  83. ) {
  84. return
  85. }
  86. preTaskOptions.push({
  87. code: task.code,
  88. name: task.name
  89. })
  90. }
  91. )
  92. model.preTaskOptions = uniqBy(preTaskOptions, 'code')
  93. if (!code) return
  94. const preTasks: number[] = []
  95. const postTaskOptions: { code: number; name: string }[] = []
  96. processTaskRelationList.forEach(
  97. (relation: { preTaskCode: number; postTaskCode: number }) => {
  98. if (relation.preTaskCode === code) {
  99. postTaskOptions.push({
  100. code: relation.postTaskCode,
  101. name: tasks[relation.postTaskCode]
  102. })
  103. }
  104. if (relation.postTaskCode === code && relation.preTaskCode !== 0) {
  105. preTasks.push(relation.preTaskCode)
  106. }
  107. }
  108. )
  109. model.preTasks = preTasks
  110. model.postTaskOptions = postTaskOptions
  111. }
  112. const onChange = (code: number) => {
  113. getProcessListByCode(code)
  114. }
  115. onMounted(() => {
  116. if (from === 1 && processName) {
  117. getProcessListByCode(processName)
  118. }
  119. getProcessList()
  120. })
  121. return {
  122. type: 'select',
  123. field: 'processName',
  124. span: 24,
  125. name: t('project.node.child_node'),
  126. props: {
  127. loading: loading,
  128. disabled: !isCreate,
  129. 'on-update:value': onChange
  130. },
  131. options: options
  132. }
  133. }