ConfigDeviceStatus.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <div class="container">
  3. <el-row :gutter="20" class="equal-height-row">
  4. <!-- 左侧设备列表 -->
  5. <el-col :span="12" class="col-wrapper">
  6. <div class="card">
  7. <h3>设备列表</h3>
  8. <div class="dept-select">
  9. <el-tree-select
  10. clearable
  11. v-model="formData.deptId1"
  12. :data="deptList"
  13. :props="defaultProps"
  14. check-strictly
  15. node-key="id"
  16. filterable
  17. placeholder="请选择设备所属部门"
  18. @node-click="handleDeptDeviceTreeNodeClick"
  19. />
  20. </div>
  21. <el-scrollbar height="400px">
  22. <el-checkbox-group v-model="selectedDevices" @change="handleDeviceChange">
  23. <div
  24. v-for="device in simpleDevices"
  25. :key="device.id"
  26. class="checkbox-item"
  27. >
  28. <el-checkbox :label="device.id">
  29. {{ device.deviceCode }} ({{ device.deviceName }})
  30. </el-checkbox>
  31. </div>
  32. </el-checkbox-group>
  33. </el-scrollbar>
  34. </div>
  35. </el-col>
  36. <!-- 右侧设备状态 -->
  37. <el-col :span="12" class="col-wrapper">
  38. <div class="card">
  39. <h3>设备状态</h3>
  40. <div class="dept-select">
  41. <el-select
  42. v-model="formData.deviceStatus"
  43. @change="handleStatusChange"
  44. placeholder="请选择"
  45. clearable
  46. :disabled="selectedDevices.length === 0"
  47. >
  48. <el-option
  49. v-for="dict in getStrDictOptions(DICT_TYPE.PMS_DEVICE_STATUS)"
  50. :key="dict.label"
  51. :label="dict.label"
  52. :value="dict.value"
  53. />
  54. </el-select>
  55. </div>
  56. <el-input
  57. v-model="formData.reason"
  58. placeholder="请输入调整原因"
  59. :disabled="!formData.deviceStatus"
  60. class="reason-input"
  61. type="textarea"
  62. :rows="3"
  63. @blur="saveCurrentRelation"
  64. />
  65. <div class="action-bar">
  66. </div>
  67. </div>
  68. </el-col>
  69. </el-row>
  70. <!-- 暂存关联列表 -->
  71. <div class="temp-list card">
  72. <h3>待提交的关联关系</h3>
  73. <el-table :data="tempRelations" style="width: 100%">
  74. <el-table-column prop="deviceNames" label="设备" width="200" >
  75. <template #default="{ row }">
  76. {{ row.deviceNames }}
  77. </template>
  78. </el-table-column>
  79. <el-table-column prop="status" label="状态" />
  80. <el-table-column prop="reason" label="调整原因" />
  81. <el-table-column label="操作" width="120">
  82. <template #default="{ row }">
  83. <el-button
  84. type="danger"
  85. size="small"
  86. @click="removeTempRelation(row.deviceId)"
  87. >
  88. 删除
  89. </el-button>
  90. </template>
  91. </el-table-column>
  92. </el-table>
  93. <div class="submit-area">
  94. <el-button
  95. type="primary"
  96. size="large"
  97. @click="submitRelations"
  98. :disabled="tempRelations.length === 0"
  99. >
  100. 提交全部关联关系
  101. </el-button>
  102. </div>
  103. </div>
  104. </div>
  105. </template>
  106. <script setup lang="ts">
  107. import { ref, computed } from 'vue'
  108. import { ElMessage } from 'element-plus'
  109. import {defaultProps, handleTree} from "@/utils/tree";
  110. import * as DeptApi from "@/api/system/dept";
  111. import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
  112. import {IotDevicePersonApi, IotDevicePersonVO} from "@/api/pms/iotdeviceperson";
  113. import * as UserApi from "@/api/system/user";
  114. import {simpleUserList, UserVO} from "@/api/system/user";
  115. import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";
  116. defineOptions({ name: 'ConfigDeviceStatus' })
  117. const simpleDevices = ref<IotDeviceVO[]>([])
  118. const simpleUsers = ref<UserVO[]>([])
  119. const currentStatus = ref<string>('')
  120. const adjustReason = ref<string>('')
  121. const deptList = ref<Tree[]>([]) // 树形结构
  122. const selectedDevices = ref<number[]>([]) // 改为数组存储多选
  123. // 获取当前设备对象
  124. const currentDevice = computed(() => {
  125. return simpleDevices.value.find(d => d.id === selectedDevice.value)
  126. })
  127. const formData = ref({
  128. id: undefined,
  129. deviceCode: undefined,
  130. deviceName: undefined,
  131. brand: undefined,
  132. model: undefined,
  133. deptId: undefined as string | undefined,
  134. deptId1: undefined as string | undefined,
  135. deviceStatus: undefined,
  136. reason: '',
  137. assetProperty: undefined,
  138. picUrl: undefined,
  139. })
  140. const queryParams = reactive({
  141. deptId1: formData.value.deptId1,
  142. deptId: formData.value.deptId,
  143. })
  144. const emit = defineEmits(['success', 'node-click']) // 定义 success 树点击 事件,用于操作成功后的回调
  145. // 响应式数据
  146. const selectedDevice = ref<number>(0)
  147. const selectedDept = ref('')
  148. const selectedUsers = ref<number[]>([])
  149. const tempRelations = ref<Array<{
  150. deviceId: number
  151. deviceNames: string
  152. status: string
  153. statusLabel: string
  154. reason: string
  155. }>>([])
  156. // 设备切换时清空状态
  157. const handleDeviceChange = () => {
  158. currentStatus.value = ''
  159. adjustReason.value = ''
  160. // 尝试从暂存记录恢复数据
  161. const existRecord = tempRelations.value.find(r => r.deviceId === selectedDevice.value)
  162. if (existRecord) {
  163. currentStatus.value = existRecord.deviceStatus
  164. adjustReason.value = existRecord.reason
  165. }
  166. }
  167. // 修改watch监听
  168. watch(selectedDevices, (newVal, oldVal) => {
  169. // 删除取消选择的设备
  170. const removedDevices = oldVal.filter(id => !newVal.includes(id))
  171. removedDevices.forEach(deviceId => {
  172. const index = tempRelations.value.findIndex(r => r.deviceId === deviceId)
  173. if (index > -1) tempRelations.value.splice(index, 1)
  174. })
  175. // 自动应用当前状态到新选设备
  176. if (formData.value.deviceStatus && formData.value.reason) {
  177. saveCurrentRelation()
  178. }
  179. })
  180. // 修改保存方法
  181. const saveCurrentRelation = () => {
  182. if (!formData.value.deviceStatus || !formData.value.reason) return
  183. const statusDict = getStrDictOptions(DICT_TYPE.PMS_DEVICE_STATUS)
  184. .find(d => d.value === formData.value.deviceStatus)
  185. selectedDevices.value.forEach(deviceId => {
  186. const device = simpleDevices.value.find(d => d.id === deviceId)
  187. if (!device) return
  188. const newRelation = {
  189. deviceId,
  190. deviceNames: `${device.deviceCode} (${device.deviceName})`,
  191. status: formData.value.deviceStatus!,
  192. statusLabel: statusDict?.label || '未知状态',
  193. reason: formData.value.reason
  194. }
  195. const existIndex = tempRelations.value.findIndex(r => r.deviceId === deviceId)
  196. if (existIndex > -1) {
  197. tempRelations.value[existIndex] = newRelation
  198. } else {
  199. tempRelations.value.push(newRelation)
  200. }
  201. })
  202. }
  203. /** 处理 部门-设备 树 被点击 */
  204. const handleDeptDeviceTreeNodeClick = async (row: { [key: string]: any }) => {
  205. emit('node-click', row)
  206. formData.value.deptId1 = row.id
  207. await getDeviceList()
  208. }
  209. /** 获得 部门下的设备 列表 */
  210. const getDeviceList = async () => {
  211. try {
  212. const params = { deptId: formData.value.deptId1 }
  213. const data = await IotDeviceApi.simpleDevices(params)
  214. simpleDevices.value = data || []
  215. } catch (error) {
  216. simpleDevices.value = []
  217. console.error('获取设备列表失败:', error)
  218. }
  219. }
  220. // 添加状态变更处理
  221. const handleStatusChange = () => {
  222. if (selectedDevices.value.length > 0) {
  223. saveCurrentRelation()
  224. }
  225. }
  226. // 添加多设备判断方法
  227. const isMultiDevice = (row: any) => {
  228. return selectedDevices.value.includes(row.deviceId) && selectedDevices.value.length > 1
  229. }
  230. // 修改删除方法
  231. const removeTempRelation = (deviceId: number) => {
  232. // 从暂存列表删除
  233. tempRelations.value = tempRelations.value.filter(r => r.deviceId !== deviceId)
  234. // 从选中设备中移除
  235. selectedDevices.value = selectedDevices.value.filter(id => id !== deviceId)
  236. // 如果当前表单状态为空,尝试恢复其他设备的状态
  237. if (!formData.value.deviceStatus && tempRelations.value.length > 0) {
  238. const firstRelation = tempRelations.value[0]
  239. formData.value.deviceStatus = firstRelation.status
  240. formData.value.reason = firstRelation.reason
  241. }
  242. }
  243. // 提交时处理数据
  244. const submitRelations = async () => {
  245. try {
  246. // 转换数据结构
  247. const submitData = tempRelations.value.map(r => ({
  248. deviceId: r.deviceId,
  249. status: r.status,
  250. reason: r.reason
  251. }))
  252. await IotDeviceApi.saveDeviceStatuses(submitData)
  253. ElMessage.success('提交成功')
  254. tempRelations.value = []
  255. } catch (error) {
  256. ElMessage.error('提交失败')
  257. }
  258. }
  259. /** 初始化 */
  260. onMounted(async () => {
  261. // 初始化部门树 根据选择的部门 查询设备列表 部门人员列表
  262. deptList.value = handleTree(await DeptApi.getSimpleDeptList())
  263. // getList()
  264. })
  265. </script>
  266. <style scoped>
  267. .container {
  268. padding: 20px;
  269. }
  270. .card {
  271. flex: 1;
  272. display: flex;
  273. flex-direction: column;
  274. overflow: hidden;
  275. }
  276. .list-item {
  277. padding: 8px 12px;
  278. border-bottom: 1px solid #f0f0f0;
  279. }
  280. .col-wrapper {
  281. display: flex;
  282. flex-direction: column;
  283. }
  284. .dept-select {
  285. margin-bottom: 20px;
  286. }
  287. .user-list {
  288. margin-bottom: 20px;
  289. border: 1px solid #ebeef5;
  290. border-radius: 4px;
  291. padding: 10px;
  292. }
  293. .action-bar {
  294. text-align: right;
  295. }
  296. .temp-list {
  297. margin-top: 20px;
  298. }
  299. .submit-area {
  300. margin-top: 20px;
  301. text-align: center;
  302. }
  303. h3 {
  304. margin: 0 0 15px 0;
  305. color: #303133;
  306. }
  307. .radio-item {
  308. width: 100%;
  309. padding: 8px 12px;
  310. border-bottom: 1px solid #f0f0f0;
  311. }
  312. .radio-item .el-radio {
  313. width: 100%;
  314. height: 100%;
  315. }
  316. .radio-item .el-radio__label {
  317. display: block;
  318. white-space: nowrap;
  319. overflow: hidden;
  320. text-overflow: ellipsis;
  321. }
  322. .equal-height-row {
  323. display: flex;
  324. align-items: stretch;
  325. }
  326. .el-scrollbar {
  327. flex: 1;
  328. }
  329. .reason-input {
  330. margin-top: 20px;
  331. }
  332. /* 添加多设备标签样式 */
  333. .el-tag {
  334. margin-left: 8px;
  335. vertical-align: middle;
  336. }
  337. </style>