project.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { defineStore } from 'pinia'
  2. import {
  3. fetchProject,
  4. uploadProjectBimScene,
  5. relaceProjectScenes,
  6. SceneType,
  7. deleteProject,
  8. finishProject,
  9. updateProject,
  10. deleteProjectBimScene,
  11. BimStatus,
  12. deleteProjectScene,
  13. updateProjectBimSceneName
  14. } from '@/api'
  15. import type {
  16. Project,
  17. Scene,
  18. BimUploadData,
  19. UpdateProjectData,
  20. Bim
  21. } from '@/api'
  22. export const BinType = Symbol('bin')
  23. export const binTypeDesc = 'bin'
  24. export type { Project, BimUploadData, Bim }
  25. export type ProjectScene = Omit<Scene, 'type' | 'num' | 'status'> &
  26. (
  27. | {
  28. type: Scene['type']
  29. status: Scene['status']
  30. num: Scene['num']
  31. }
  32. | {
  33. type: typeof BinType
  34. status: BimStatus
  35. }
  36. )
  37. export type SelectTypeScenes = {
  38. [SceneType.SWKJ]: Scene['num'][]
  39. [SceneType.SWKK]: Scene['num'][]
  40. [SceneType.SWSS]: Scene['num'][]
  41. }
  42. type CurrentState = null | Project
  43. export const useProject = defineStore('project', {
  44. state: () => ({
  45. current: null as CurrentState
  46. }),
  47. getters: {
  48. scenes: state => {
  49. const scenes: ProjectScene[] = state.current?.sceneList || []
  50. const bimData = state.current?.bimData
  51. if (bimData) {
  52. return [
  53. {
  54. id: bimData.bimId,
  55. name: bimData.bimName,
  56. title: bimData.bimName,
  57. sceneName: bimData.bimName,
  58. thumb: bimData.bimOssFilePath,
  59. createTime: bimData.createTime,
  60. type: BinType,
  61. status: bimData.bimStatus,
  62. phone: ''
  63. },
  64. ...scenes
  65. ]
  66. } else {
  67. return scenes
  68. }
  69. },
  70. isSelf: state => (id?: Project['projectId']) =>
  71. state.current?.projectId === id
  72. },
  73. actions: {
  74. async setCurrent(id: Project['projectId']) {
  75. this.current = await fetchProject(id)
  76. },
  77. async updateCurrent(id?: Project['projectId']) {
  78. if ((id = id || this.current?.projectId)) {
  79. this.isSelf(id) && (await this.setCurrent(id))
  80. }
  81. },
  82. async delete(id?: Project['projectId']) {
  83. if ((id = id || this.current?.projectId)) {
  84. await deleteProject(id)
  85. await this.updateCurrent(id)
  86. }
  87. },
  88. async update(data: PartialPart<UpdateProjectData, 'projectId'>) {
  89. const id = data.projectId || this.current?.projectId
  90. if (id) {
  91. await updateProject({
  92. ...data,
  93. projectId: id
  94. })
  95. await this.updateCurrent(id)
  96. }
  97. },
  98. async finish(id?: Project['projectId']) {
  99. if ((id = id || this.current?.projectId)) {
  100. await finishProject(id)
  101. await this.updateCurrent(id)
  102. }
  103. },
  104. async addBim(data: BimUploadData, id?: Project['projectId']) {
  105. if ((id = id || this.current?.projectId)) {
  106. await uploadProjectBimScene(id, data)
  107. await this.updateCurrent(id)
  108. }
  109. },
  110. async updateBimName(bimName: Bim['bimName'], bimId?: Bim['bimId']) {
  111. if ((bimId = bimId || this.current?.bimData?.bimId)) {
  112. await updateProjectBimSceneName({ bimId, bimName })
  113. this.current?.bimData?.bimId === bimId && (await this.updateCurrent())
  114. }
  115. },
  116. async deleteBim(bimId?: Bim['bimId']) {
  117. if ((bimId = bimId || this.current?.bimData?.bimId)) {
  118. await deleteProjectBimScene(bimId)
  119. this.current?.bimData?.bimId === bimId && (await this.updateCurrent())
  120. }
  121. },
  122. async replaceScenes(
  123. typeScenes: SelectTypeScenes,
  124. id?: Project['projectId']
  125. ) {
  126. if ((id = id || this.current?.projectId)) {
  127. const sceneNumParam = Object.entries(typeScenes).map(
  128. ([type, nums]) => ({
  129. type: Number(type) as SceneType,
  130. numList: nums
  131. })
  132. )
  133. await relaceProjectScenes({ id, sceneNumParam })
  134. await this.updateCurrent(id)
  135. }
  136. },
  137. async deleteScene(num: Scene['num'], id?: Project['projectId']) {
  138. if ((id = id || this.current?.projectId)) {
  139. await deleteProjectScene({ projectId: id, num })
  140. await this.updateCurrent(id)
  141. }
  142. }
  143. }
  144. })
  145. export {
  146. ProjectStatus,
  147. ProjectStatusDesc,
  148. BimStatus,
  149. BimStatusDesc
  150. } from '@/api'