divisionDialog.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <!-- 节点划分 -->
  2. <template>
  3. <hc-new-dialog v-model="linkModal" is-table widths="1200px" @close="closeModal">
  4. <template #header>
  5. <div class="flex justify-between">
  6. <div class="text-lg">节点划分</div>
  7. <div class="mr-8 w-100 flex">
  8. <el-input
  9. v-model="searchTreeVal"
  10. clearable
  11. block
  12. placeholder="请输入名称关键词检索"
  13. @keyup="searchTreeKeyUp"
  14. @clear="searchTreeClick"
  15. >
  16. <template #suffix>
  17. <HcIcon
  18. name="search-2"
  19. ui="text-xl iscusor"
  20. @click="searchTreeClick"
  21. />
  22. </template>
  23. </el-input>
  24. <el-button type="primary" class="ml-2" @click="searchTreeClick">搜索</el-button>
  25. </div>
  26. </div>
  27. </template>
  28. <div class="link-data-box">
  29. <div class="link-data-left">
  30. <div class="relative h-full flex">
  31. <div v-loading="treeLoading" class="hc_tree_card_border relative w-full">
  32. <hc-body scrollbar padding="0px">
  33. <HcLazyTree
  34. ref="leftTree"
  35. show-checkbox
  36. is-type
  37. check-strictly
  38. :h-props="leftTreeProps"
  39. :is-color="false"
  40. :accordion="false"
  41. :auto-expand-keys="treeAutoExpandKeys"
  42. @check="leftTreeNodeCheckChange"
  43. @load="treeLoadNode"
  44. @node-loading="treeNodeLoading"
  45. />
  46. </hc-body>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="link-data-icon">
  51. <HcIcon type="primary" name="links" style="font-size: 22px;" />
  52. </div>
  53. <div class="link-data-right">
  54. <div class="relative h-full flex">
  55. <div v-loading="treeLoadingData" class="hc_tree_card_border relative w-full">
  56. <hc-body scrollbar padding="0px">
  57. <HcLazyTree
  58. v-if="!isSearchTree"
  59. ref="rightTree"
  60. v-model:loading="isLoading"
  61. tree-key="id"
  62. show-checkbox
  63. is-type
  64. :is-color="false"
  65. :default-checked-keys="defaultCheckedKeys"
  66. :is-auto-check="true"
  67. :accordion="false"
  68. :h-props="treeProps"
  69. @check="addTreeNodeCheckChange"
  70. @load="rightTreeLoadNode"
  71. @node-tap="rightElTreeClick"
  72. >
  73. <template #name="{ node, data }">
  74. <div class="custom-tree-node">
  75. <div class="label" @dblclick="ElTreeDblClick(data)">
  76. <el-input v-if="data.isInputName" v-model="data.title" size="small" @blur="ElTreeBtnClick(data)" @keyup="keyUpEvent($event, data)">
  77. <template #append>
  78. <el-button plain size="small" type="primary" @click="ElTreeBtnClick(data)">
  79. <HcIcon name="check" />
  80. </el-button>
  81. </template>
  82. </el-input>
  83. <span v-else>{{ data.title }}</span>
  84. </div>
  85. </div>
  86. </template>
  87. </HcLazyTree>
  88. <HcDataTree
  89. v-if="isSearchTree"
  90. :datas="searchTreeData"
  91. show-checkbox
  92. is-type
  93. :auto-expand-keys="treeAutoExpandKeys"
  94. default-expand-all
  95. @node-tap="wbsElTreeClick"
  96. />
  97. </hc-body>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. <template #footer>
  103. <div class="dialog-footer">
  104. <el-button hc-btn style="border: 1px solid var(--el-button-border-color);" @click="closeModal">
  105. <HcIcon name="close" />
  106. <span>取消</span>
  107. </el-button>
  108. <el-button hc-btn type="primary" :loading="sureLoading" :disabled="isLoading" @click="sureClick(1)">
  109. <HcIcon name="check" />
  110. <span>关联并退出</span>
  111. </el-button>
  112. <el-button hc-btn type="primary" :loading="sureLoading" :disabled="isLoading" @click="sureClick(2)">
  113. <HcIcon name="check" />
  114. <span>关联并继续</span>
  115. </el-button>
  116. </div>
  117. </template>
  118. </hc-new-dialog>
  119. </template>
  120. <script setup>
  121. import { nextTick, ref, watch } from 'vue'
  122. import { getArrValue, getObjValue } from 'js-fast-way'
  123. import { useAppStore } from '~src/store'
  124. import samplingApi from '~api/tentative/material/sampling'
  125. import queryApi from '~api/data-fill/query'
  126. import wbsApi from '~api/data-fill/wbs'
  127. import { getStoreValue } from '~uti/storage'
  128. const props = defineProps({
  129. linkModal: {
  130. type: Boolean,
  131. default: false,
  132. },
  133. classifyType:{
  134. type: String,
  135. default: '1',
  136. },
  137. treeItemInfo:{
  138. type: Object,
  139. default: () => ({}),
  140. },
  141. firstTree: {
  142. type: Object,
  143. default: () => ({}),
  144. },
  145. autoExpandKeys: {
  146. type: Array,
  147. default: () => [],
  148. },
  149. })
  150. //事件
  151. const emit = defineEmits(['close', 'save'])
  152. const useAppState = useAppStore()
  153. const contractId = ref(useAppState.getContractId)
  154. const projectId = ref(useAppState.getProjectId)
  155. const treeItemInfo = ref(props.treeItemInfo)
  156. const classifyType = ref(props.classifyType)
  157. const firstTree = ref(props.firstTree)
  158. const treeAutoExpandKeys = ref(props.autoExpandKeys)
  159. const linkModal = defineModel('modelValue', {
  160. default: false,
  161. })
  162. const closeModal = ()=>{
  163. linkModal.value = false
  164. leftPid.value = ''
  165. allSelectedList.value = []
  166. halfSelectedList.value = []
  167. emit('close')
  168. }
  169. //获取两棵树的数据
  170. const treeLoading = ref(true)
  171. const treeNodeLoading = () => {
  172. treeLoading.value = false
  173. }
  174. const rightTree = ref(null)
  175. const treeProps = ref({
  176. label: 'title',
  177. children: 'children',
  178. isLeaf: (data) => !data.hasChildren,
  179. // disabled: (data) => {
  180. // // 当节点类型为6时才能选择
  181. // return data.nodeType !== 6
  182. // },
  183. })
  184. const leftTreeProps = ref({
  185. label: 'title',
  186. children: 'children',
  187. isLeaf: (data) =>!data.hasChildren,
  188. disabled: (data) => {
  189. // 当节点类型为6时不能能选择
  190. return data.nodeType === 6
  191. },
  192. })
  193. //监听
  194. const isLoading = ref(false)
  195. watch(isLoading, (newVal) => {
  196. console.log('加载状态变化:', newVal)
  197. })
  198. watch(() => [
  199. props.linkModal,
  200. props.classifyType,
  201. props.treeItemInfo,
  202. props.firstTree,
  203. props.autoExpandKeys,
  204. ], ([link, type, treeItemIn, first, keys]) => {
  205. linkModal.value = link
  206. classifyType.value = type
  207. treeItemInfo.value = treeItemIn
  208. firstTree.value = first
  209. treeAutoExpandKeys.value = keys
  210. console.log(keys, 'keys')
  211. }, { immediate: true })
  212. //选中的节点
  213. const defaultCheckedKeys = ref([])
  214. const allSelectedList = ref([])
  215. const halfSelectedList = ref([])
  216. const addTreeNodeCheckChange = async (_, { checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys }) => {
  217. await nextTick()
  218. //全选数据
  219. const keys = checkedNodes || []
  220. console.log('key22222222s', keys)
  221. const NodesArr = keys
  222. .filter(node => node.nodeType === 6)
  223. .map(node => ({
  224. nodeName: node.title,
  225. primaryKeyId: node.primaryKeyId,
  226. }))
  227. // 更新选中状态
  228. defaultCheckedKeys.value = checkedNodes.map(node => node.id)
  229. console.log( defaultCheckedKeys.value, ' defaultCheckedKeys.value')
  230. allSelectedList.value = NodesArr
  231. //半选数据
  232. const halfNodes = halfCheckedNodes || []
  233. const halfArr = halfNodes
  234. .filter(node => node.nodeType === 6)
  235. .map(node => ({
  236. nodeName: node.title,
  237. primaryKeyId: node.primaryKeyId,
  238. }))
  239. halfSelectedList.value = halfArr
  240. }
  241. const sureLoading = ref(false)
  242. const sureClick = async (type) => {
  243. await nextTick() // 等待下一个 DOM
  244. const keys = allSelectedList.value || []
  245. console.log('keys', keys)
  246. if (keys.length <= 0 || leftPid.value.length <= 0) {
  247. window?.$message?.warning('请先选择节点')
  248. return
  249. } else {
  250. //发起请求
  251. sureLoading.value = true
  252. const { error, code, msg } = await wbsApi.saveContractTreeNode({
  253. projectId: projectId.value,
  254. contractId: contractId.value,
  255. saveType: 4, //节点划分,多对多
  256. allSelectedList: allSelectedList.value,
  257. halfSelectedList: halfSelectedList.value,
  258. currentNodePrimaryKeyId: leftPid.value,
  259. isImportTree: 1,
  260. templateType:useAppState.contractInfo?.templateType,
  261. })
  262. //判断状态
  263. sureLoading.value = false
  264. if (!error && code === 200) {
  265. window?.$message?.success(msg)
  266. // type=1时关闭弹窗,type=2时保持打开
  267. if (type === 1) {
  268. closeModal()
  269. emit('save')
  270. } else {
  271. allSelectedList.value = []
  272. halfSelectedList.value = []
  273. leftPid.value = ''
  274. leftTree.value?.treeRef.setCheckedKeys([])
  275. rightTree.value?.treeRef.setCheckedKeys([])
  276. }
  277. }
  278. }
  279. }
  280. const leftTree = ref(null)
  281. const treeLoadNode = async ({ node, item, level }, resolve) => {
  282. let contractIdRelation = '',
  283. parentId = '',
  284. primaryKeyId = ''
  285. if (level !== 0) {
  286. const nodeData = getObjValue(item)
  287. contractIdRelation = nodeData?.contractIdRelation || ''
  288. parentId = contractIdRelation ? nodeData?.primaryKeyId : nodeData?.id
  289. primaryKeyId = nodeData?.id || ''
  290. }
  291. //获取数据
  292. const { data } = await queryApi.queryWbsTreeData({
  293. contractId: contractId.value || '',
  294. contractIdRelation,
  295. primaryKeyId,
  296. parentId,
  297. classifyType: classifyType.value,
  298. tableOwner: classifyType.value,
  299. dataTime:new Date(),
  300. })
  301. resolve(getArrValue(data))
  302. treeLoading.value = false
  303. }
  304. const leftPid = ref('')
  305. const leftTreeNodeCheckChange = (_, { checkedNodes }) => {
  306. if (checkedNodes && checkedNodes.length > 0) {
  307. // 获取所有选中节点的 primaryKeyId 并用逗号拼接
  308. leftPid.value = checkedNodes.map(node => node.primaryKeyId).filter(Boolean).join(',')
  309. } else {
  310. leftPid.value = ''
  311. }
  312. }
  313. //更改节点名称
  314. const ElTreeDblClick = (item) => {
  315. item.isInputName = true
  316. }
  317. //回车
  318. const keyUpEvent = (e, item) => {
  319. if (e.key === 'Enter') {
  320. ElTreeBtnClick(item)
  321. }
  322. }
  323. //更改节点名称完成
  324. const ElTreeBtnClick = (item) => {
  325. if (!item?.title) {
  326. window?.$message?.warning('节点名称不能为空')
  327. } else {
  328. item.isInputName = false
  329. // ElTreeCheckedKeys()
  330. }
  331. }
  332. const rightTreeLoadNode = async ({ node, item, level }, resolve) => {
  333. let parentId = '0'
  334. if (level !== 0) {
  335. parentId = item?.id
  336. }
  337. //获取数据
  338. const { data } = await samplingApi.queryLazyTree({
  339. wbsId: firstTree.value?.wbsId,
  340. tenantId: getStoreValue('tenantId') || '',
  341. projectId: projectId.value,
  342. parentId,
  343. wbsType: firstTree.value.wbsType,
  344. type:1,
  345. })
  346. resolve(getArrValue(data))
  347. // 确保在数据加载完成后,重新设置选中状态
  348. await nextTick()
  349. if (defaultCheckedKeys.value.length > 0) {
  350. rightTree.value?.treeRef.setCheckedKeys(defaultCheckedKeys.value)
  351. }
  352. }
  353. const rightObj = ref(null)
  354. const rightElTreeClick = ({ data }) => {
  355. rightObj.value = data
  356. }
  357. //搜索右边的节点树
  358. const searchTreeVal = ref('')
  359. const searchTreeData = ref([])
  360. const isSearchTree = ref(false)
  361. //回车
  362. const treeLoadingData = ref(false)
  363. const getSearchTreeData = async () => {
  364. treeLoadingData.value = true
  365. const { error, code, data } = await wbsApi.getQueryValueByType({
  366. contractId: contractId.value,
  367. queryValue: searchTreeVal.value,
  368. projectId:projectId.value,
  369. type:1,
  370. wbsId:firstTree.value?.wbsId,
  371. })
  372. //判断状态
  373. if (!error && code === 200) {
  374. let treedata = getArrValue(data)
  375. searchTreeData.value = treedata
  376. treeLoadingData.value = false
  377. } else {
  378. treeLoadingData.value = false
  379. searchTreeData.value = []
  380. }
  381. }
  382. const searchTreeKeyUp = (e) => {
  383. if (e.key === 'Enter') {
  384. searchTreeClick()
  385. }
  386. }
  387. const searchTreeClick = async () => {
  388. if (searchTreeVal.value) {
  389. isSearchTree.value = true
  390. //treeLoading.value = true
  391. getSearchTreeData().then()
  392. } else {
  393. isSearchTree.value = false
  394. }
  395. }
  396. </script>
  397. <style lang='scss' scoped>
  398. .title-box{
  399. position: relative;
  400. display: flex;
  401. // padding: 40px;
  402. font-weight: 400;
  403. font-size: 18px;
  404. width: 100%;
  405. margin-bottom: 10px;
  406. margin-top: 10px;
  407. .title-box-left{
  408. width: 50%;
  409. text-align: center;
  410. }
  411. .title-box-right{
  412. width: 50%;
  413. text-align: center;
  414. }
  415. }
  416. .link-data-box{
  417. position: relative;
  418. display: flex;
  419. height: calc(100% - 45px);
  420. align-items: center;
  421. // border: 1px solid rgb(187, 187, 187);
  422. padding: 0px 40px 0px 40px;
  423. .link-data-left{
  424. width: 50%;
  425. position: relative;
  426. height: 100%;
  427. background: #ffffff;
  428. border: 1px solid #273044;
  429. }
  430. .link-data-icon{
  431. display: inline-block;
  432. vertical-align: middle;
  433. padding: 0 16px;
  434. }
  435. .link-data-right{
  436. width: 50%;
  437. position: relative;
  438. height: 100%;
  439. background: #ffffff;
  440. border: 1px solid #273044;
  441. }
  442. }
  443. </style>
  444. <style lang="scss">
  445. .link-data-right .el-tree.hc-tree-node-v2 .data-custom-tree-node .label, .hc-tree-node .data-custom-tree-node .label{
  446. display: flex;
  447. }
  448. </style>