ConfigDeviceStatus.vue 10 KB

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