ConfigDeviceStatus.vue 12 KB

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