Browse Source

pms 设备责任人 功能优化

zhangcl 2 months ago
parent
commit
edcea01048
1 changed files with 43 additions and 68 deletions
  1. 43 68
      src/views/pms/device/ConfigDevicePerson.vue

+ 43 - 68
src/views/pms/device/ConfigDevicePerson.vue

@@ -19,17 +19,17 @@
             />
             />
           </div>
           </div>
           <el-scrollbar height="400px">
           <el-scrollbar height="400px">
-            <el-radio-group v-model="selectedDevice">
+            <el-checkbox-group v-model="selectedDevices">
               <div
               <div
                 v-for="device in simpleDevices"
                 v-for="device in simpleDevices"
                 :key="device.id"
                 :key="device.id"
                 class="radio-item"
                 class="radio-item"
               >
               >
-                <el-radio :label="device.id">
+                <el-checkbox :label="device.id">
                   {{ device.deviceCode }} ({{ device.deviceName }}) - {{ device.devicePersons }}
                   {{ device.deviceCode }} ({{ device.deviceName }}) - {{ device.devicePersons }}
-                </el-radio>
+                </el-checkbox>
               </div>
               </div>
-            </el-radio-group>
+            </el-checkbox-group>
           </el-scrollbar>
           </el-scrollbar>
         </div>
         </div>
       </el-col>
       </el-col>
@@ -80,7 +80,7 @@
           type="textarea"
           type="textarea"
           :rows="3"
           :rows="3"
           @input="handleReasonInput"
           @input="handleReasonInput"
-          :disabled="!selectedDevice || selectedUsers.length === 0"
+          :disabled="selectedDevices.length === 0 || selectedUsers.length === 0"
         />
         />
       </div>
       </div>
       <div class="temp-list card">
       <div class="temp-list card">
@@ -159,6 +159,8 @@ const emit = defineEmits(['success', 'node-click']) // 定义 success 树点击
 
 
 // 响应式数据
 // 响应式数据
 const selectedDevice = ref<number>(0)
 const selectedDevice = ref<number>(0)
+const selectedDevices = ref<number[]>([])
+
 const selectedDept = ref('')
 const selectedDept = ref('')
 const selectedUsers = ref<number[]>([])
 const selectedUsers = ref<number[]>([])
 const tempRelations = ref<Array<{
 const tempRelations = ref<Array<{
@@ -169,66 +171,38 @@ const tempRelations = ref<Array<{
   reason: string
   reason: string
 }>>([])
 }>>([])
 
 
-// 添加设备选择监听
-watch(selectedDevice, (newVal) => {
-  if (newVal) {
-    // 切换设备时清空人员选择
+watch(selectedDevices, (newVal, oldVal) => {
+  // 找出取消选择的设备
+  const removedDevices = oldVal.filter(id => !newVal.includes(id))
+
+  // 移除对应设备的关联记录
+  tempRelations.value = tempRelations.value.filter(
+    r => !removedDevices.includes(r.deviceIds[0])
+  )
+
+  // 当没有选中设备时清空其他选项
+  if (newVal.length === 0) {
     selectedUsers.value = []
     selectedUsers.value = []
-    // 可选:清空部门选择
-    formData.value.deptId = undefined
     formData.value.reason = ''
     formData.value.reason = ''
-    simpleUsers.value = []
   }
   }
 })
 })
 
 
-// 处理人员选择变化
-const handleUserSelectionChange = (value: number[]) => {
-  if (!selectedDevice.value) {
+const handleUserSelectionChange = (userIds: number[]) => {
+  if (selectedDevices.value.length === 0) {
     ElMessage.warning('请先选择设备')
     ElMessage.warning('请先选择设备')
     selectedUsers.value = []
     selectedUsers.value = []
     return
     return
   }
   }
 
 
-  // 获取当前选择的设备
-  const device = simpleDevices.value.find(d => d.id === selectedDevice.value)
-  if (!device) return
-
-  // 新增或更新关联关系
-  updateDeviceRelation(device, value)
-  // 自动同步原因(如果已输入)
-  if (formData.value.reason) {
-    handleReasonInput(formData.value.reason)
-  }
-}
-
-// 修改 暂时 保存关联方法
-const saveTempRelation = () => {
-  if (!selectedDevice.value || selectedUsers.value.length === 0) return
-
-  const device = simpleDevices.value.find(d => d.id === selectedDevice.value)
-  const users = simpleUsers.value.filter(u => selectedUsers.value.includes(u.id))
-
-  if (!device) return
-
-  const newRelation = {
-    deviceIds: [selectedDevice.value], // 保持数组结构但只包含单个设备
-    deviceNames: `${device.deviceCode} (${device.deviceName})`,
-    userIds: users.map(u => u.id),
-    userNames: users.map(u => u.nickname).join(', ')
-  }
-
-  // 覆盖已存在的设备关联
-  const existIndex = tempRelations.value.findIndex(
-    r => r.deviceIds[0] === selectedDevice.value
+  // 获取所有选中设备
+  const devices = simpleDevices.value.filter(d =>
+    selectedDevices.value.includes(d.id)
   )
   )
 
 
-  if (existIndex > -1) {
-    tempRelations.value[existIndex] = newRelation
-  } else {
-    tempRelations.value.push(newRelation)
-  }
-
-  clearSelection()
+  // 更新所有设备的关联关系
+  devices.forEach(device => {
+    updateDeviceRelation(device, userIds)
+  })
 }
 }
 
 
 /** 处理 部门-设备 树 被点击 */
 /** 处理 部门-设备 树 被点击 */
@@ -275,10 +249,15 @@ const getUserList = async () => {
 // 新增输入处理方法
 // 新增输入处理方法
 const handleReasonInput = (value: string) => {
 const handleReasonInput = (value: string) => {
   formData.value.reason = value
   formData.value.reason = value
-  if (selectedDevice.value && selectedUsers.value.length > 0) {
-    const device = simpleDevices.value.find(d => d.id === selectedDevice.value)
-    device && updateDeviceRelation(device, selectedUsers.value)
-  }
+  // 同步到所有已选设备的记录
+  selectedDevices.value.forEach(deviceId => {
+    const relation = tempRelations.value.find(
+      r => r.deviceIds[0] === deviceId
+    )
+    if (relation) {
+      relation.reason = value
+    }
+  })
 }
 }
 
 
 // 更新设备关联关系
 // 更新设备关联关系
@@ -294,21 +273,13 @@ const updateDeviceRelation = (device: IotDeviceVO, userIds: number[]) => {
     reason: formData.value.reason
     reason: formData.value.reason
   }
   }
 
 
-  // 查找是否已存在该设备的关联
   const existIndex = tempRelations.value.findIndex(
   const existIndex = tempRelations.value.findIndex(
     r => r.deviceIds[0] === device.id
     r => r.deviceIds[0] === device.id
   )
   )
 
 
   if (existIndex > -1) {
   if (existIndex > -1) {
-    // 如果没有选择人员,移除该关联
-    if (userIds.length === 0) {
-      tempRelations.value.splice(existIndex, 1)
-    } else {
-      // 更新关联
-      tempRelations.value[existIndex] = newRelation
-    }
-  } else if (userIds.length > 0) {
-    // 添加新关联
+    tempRelations.value[existIndex] = newRelation
+  } else {
     tempRelations.value.push(newRelation)
     tempRelations.value.push(newRelation)
   }
   }
 }
 }
@@ -319,10 +290,14 @@ const clearSelection = () => {
   selectedDept.value = ''
   selectedDept.value = ''
 }
 }
 
 
-const removeTempRelation = (deviceIds: string[]) => {
+const removeTempRelation = (deviceIds: number[]) => {
   tempRelations.value = tempRelations.value.filter(
   tempRelations.value = tempRelations.value.filter(
     r => r.deviceIds.join() !== deviceIds.join()
     r => r.deviceIds.join() !== deviceIds.join()
   )
   )
+  // 同步取消勾选设备
+  selectedDevices.value = selectedDevices.value.filter(
+    id => !deviceIds.includes(id)
+  )
 }
 }
 
 
 const submitRelations = async () => {
 const submitRelations = async () => {