Selaa lähdekoodia

普通作业票提交问题修复

pm 3 kuukautta sitten
vanhempi
sitoutus
648afa23cf
1 muutettua tiedostoa jossa 117 lisäystä ja 204 poistoa
  1. 117 204
      src/views/jobTicket/job/UserView/CardView.vue

+ 117 - 204
src/views/jobTicket/job/UserView/CardView.vue

@@ -8,7 +8,7 @@
           <template #header>可选择的锁定人</template>
           <div class="user-list">
             <div
-              v-for="user in jtlocker"
+              v-for="user in allLockUsers"
               :key="user.id"
               class="user-card"
               :class="{ selected: isUserInSelectedGroup(user) }"
@@ -54,9 +54,9 @@
                 <template #header>{{ group.groupName }}</template>
                 <div class="user-list">
                   <div
-                    class="user-card"
                     v-for="user in addedLockUsers[group.id] || []"
-                    :key="user.id"
+                    :key="user.userId"
+                    class="user-card"
                     @click.stop="removeLockUser(user, group.id)"
                   >
                     <img src="@/assets/images/UserBlack.png" />
@@ -74,7 +74,7 @@
           <div class="user-list">
             <div
               v-for="user in addedCoLockUsers"
-              :key="user.id"
+              :key="user.userId"
               class="user-card"
               @click="removeCoLockUser(user)"
             >
@@ -89,24 +89,19 @@
     <!-- 底部按钮 -->
     <div class="bottom-btn">
       <el-button type="primary" @click="submit">
-        <el-icon>
-          <Check />
-        </el-icon>
+        <el-icon><Check /></el-icon>
         确 定
       </el-button>
       <el-button @click="cancel">
-        <el-icon>
-          <Close />
-        </el-icon>
+        <el-icon><Close /></el-icon>
         取 消
       </el-button>
     </div>
   </div>
 </template>
-
 <script setup lang="ts">
-import { ref, onMounted } from 'vue'
-import { useRoute } from 'vue-router'
+import { ref, computed, onMounted } from 'vue'
+import { useRoute, useRouter } from 'vue-router'
 import { ElMessage } from 'element-plus'
 import * as RoleUser from '@/api/system/user'
 import * as JobUserApi from '@/api/job/jobUser'
@@ -117,268 +112,186 @@ import { Check, Close } from '@element-plus/icons-vue'
 
 const route = useRoute()
 const router = useRouter()
+
 const allGroup = ref<any[]>([])
-const jtlocker = ref<any[]>([])
-const jtcolocker = ref<any[]>([])
+const allLockUsers = ref<any[]>([]) // 全量锁定人
+const allCoLockUsers = ref<any[]>([]) // 全量共锁人
 
 const addedLockUsers = ref<Record<string, any[]>>({})
 const addedCoLockUsers = ref<any[]>([])
+
 const selectedGroupId = ref<string>('')
-const toDeleteUserIds = ref<number[]>([]) //删除的人员
-// 新增:保存当前票已存在的用户
 const existingUserList = ref<any[]>([])
-// 加载数据
+
+// 共锁人动态可选
+const jtcolocker = computed(() => {
+  return allCoLockUsers.value.filter(u => {
+    return !addedCoLockUsers.value.some(a => a.userId === u.id)
+  })
+})
+
 onMounted(async () => {
   const groupData = await JobPointGroupApi.getJobTicketGroupPage({
     pageNo: 1,
     pageSize: -1,
     ticketId: route.query.ticketId
   })
-
   allGroup.value = groupData.list
-  jtlocker.value = await RoleUser.getRoleUser('jtlocker')
-  if(route.query.groupId&&route.query.groupId!=='0'){
-    selectedGroupId.value=route.query.groupId
-  }else if(route.query.groupId=='0'){
-    // 作业通过sop创建之后从设置点进来此时默认选中第一个分组
-    selectedGroupId.value=groupData.list[0].id
-  }else{
-    selectedGroupId.value = ''
-  }
 
-  const JobUserData = await JobApi.selectJobTicketById(route.query.ticketId)
-  console.log(JobUserData,'JobUserData')
+  allLockUsers.value = await RoleUser.getRoleUser('jtlocker')
+  allCoLockUsers.value = await RoleUser.getRoleUser('jtcolocker')
 
-  if (Array.isArray(JobUserData.ticketUserList) && JobUserData.ticketUserList.length > 0) {
-    const userList = JobUserData.ticketUserList
-    existingUserList.value = JobUserData.ticketUserList//给新增用来判断后端已有数据
-    // 1. 渲染锁定人
-    userList
-      .filter((user) => user.userRole == 'jtlocker')
-      .forEach((user) => {
-        const groupId = user.groupId
-        if (!groupId) return // 没分组就跳过
+  if (route.query.groupId && route.query.groupId !== '0') {
+    selectedGroupId.value = route.query.groupId
+  } else {
+    selectedGroupId.value = groupData.list?.[0]?.id || ''
+  }
 
+  const jobData = await JobApi.selectJobTicketById(route.query.ticketId)
+  if (Array.isArray(jobData.ticketUserList)) {
+    existingUserList.value = jobData.ticketUserList
+    jobData.ticketUserList.forEach(user => {
+      if (user.userRole === 'jtlocker') {
+        const groupId = user.groupId
         if (!addedLockUsers.value[groupId]) {
           addedLockUsers.value[groupId] = []
-
         }
-
         addedLockUsers.value[groupId].push({
-          id: user.id,
           userId: user.userId,
           nickname: user.userName
         })
-      })
-
-
-    // 2. 渲染共锁人(groupId 为 null)
-    userList
-      .filter((user) => user.userRole === 'jtcolocker')
-      .forEach((user) => {
+      } else if (user.userRole === 'jtcolocker') {
         addedCoLockUsers.value.push({
-          id: user.id,
           userId: user.userId,
           nickname: user.userName
         })
-
-        // 从左侧移除已分配的共锁人
-        jtcolocker.value = jtcolocker.value.filter((u) => u.id !== user.userId)
-      })
-  } else {
-
-    // 渲染初始化的
-
-    jtcolocker.value = await RoleUser.getRoleUser('jtcolocker')
+      }
+    })
   }
 })
 
-// 切换分
+// 选组
 const selectGroup = (groupId: string) => {
-  console.log('选中分组:', groupId)
   selectedGroupId.value = groupId
 }
 
-// 是否在选中的分组中(只检查当前选中的分组)
+// 判断选中
 const isUserInSelectedGroup = (user) => {
-
-  if (!selectedGroupId.value) return false
-  const groupList = addedLockUsers.value[selectedGroupId.value] || []
-  return groupList.some((u) => u.id == user.id)
+  const list = addedLockUsers.value[selectedGroupId.value] || []
+  return list.some(u => u.userId === user.id)
 }
 
-// 添加 or 移除
+// 添加/移除
 const selectUser = (user, type: 'lock' | 'coLock') => {
   if (type === 'lock') {
     if (!selectedGroupId.value) {
       ElMessage.warning('请先选择一个分组')
       return
     }
-
-    const groupList = addedLockUsers.value[selectedGroupId.value] || []
-    const existingIndex = groupList.findIndex((u) => u.id === user.id)
-
-    if (existingIndex !== -1) {
-      groupList.splice(existingIndex, 1)
+    const list = addedLockUsers.value[selectedGroupId.value] || []
+    const exists = list.findIndex(u => u.userId === user.id)
+    if (exists >= 0) {
+      list.splice(exists, 1)
     } else {
-      if (!addedLockUsers.value[selectedGroupId.value]) {
-        addedLockUsers.value[selectedGroupId.value] = []
-      }
-      addedLockUsers.value[selectedGroupId.value].push(user)
+      list.push({
+        userId: user.id,
+        nickname: user.nickname
+      })
     }
+    addedLockUsers.value[selectedGroupId.value] = list
   } else {
-    const index = jtcolocker.value.findIndex((u) => u.id === user.id)
-    if (index !== -1) {
-      addedCoLockUsers.value.push(user)
-      jtcolocker.value.splice(index, 1)
+    const idx = addedCoLockUsers.value.findIndex(u => u.userId === user.id)
+    if (idx >= 0) {
+      addedCoLockUsers.value.splice(idx, 1)
+    } else {
+      addedCoLockUsers.value.push({
+        userId: user.id,
+        nickname: user.nickname
+      })
     }
   }
 }
 
-// 移除锁定人
 const removeLockUser = (user, groupId) => {
   const list = addedLockUsers.value[groupId]
-  if (!list) return
-  addedLockUsers.value[groupId] = list.filter((u) => u.id !== user.id)
-
-  // 记录待删除
-  toDeleteUserIds.value.push(user.id)
+  addedLockUsers.value[groupId] = list.filter(u => u.userId !== user.userId)
 }
 
-// 移除共锁人(同时回到左侧)
 const removeCoLockUser = (user) => {
-  addedCoLockUsers.value = addedCoLockUsers.value.filter((u) => u.id !== user.id)
-  jtcolocker.value.push(user)
-
-  // 记录待删除
-  toDeleteUserIds.value.push(user.id)
-  console.log(user,'user')
+  addedCoLockUsers.value = addedCoLockUsers.value.filter(u => u.userId !== user.userId)
 }
 
-// 确认按钮事件
 const submit = async () => {
-  try {
-    const ticketId = route.query.ticketId
-    const userList: any[] = []
-
-    // 【1】锁定人:同组去重
-    Object.keys(addedLockUsers.value).forEach((groupId) => {
-      const users = addedLockUsers.value[groupId] || []
-      const uniqueIds = new Set()
-      const uniqueUsers = []
-
-      users.forEach((user) => {
-        if (!uniqueIds.has(user.id)) {
-          uniqueIds.add(user.id)
-          uniqueUsers.push(user)
-        }
+  const ticketId = route.query.ticketId
+  const finalList: any[] = []
+
+  Object.entries(addedLockUsers.value).forEach(([groupId, users]) => {
+    users.forEach(u => {
+      finalList.push({
+        ticketId,
+        groupId,
+        userId: u.userId,
+        userName: u.nickname,
+        userType: 0,
+        userRole: 'jtlocker'
       })
-
-      uniqueUsers.forEach((user) => {
-        userList.push({
-          ticketId,
-          groupId,
-          userId: user.id,
-          userName: user.nickname,
-          userType: 0,
-          userRole: 'jtlocker'
-        })
-      })
-    })
-
-    // 【2】共锁人:全局去重
-    const coLockIds = new Set()
-    addedCoLockUsers.value.forEach((user) => {
-      if (!coLockIds.has(user.id)) {
-        coLockIds.add(user.id)
-        userList.push({
-          ticketId,
-          groupId: null,
-          userId: user.id,
-          userName: user.nickname,
-          userType: 0,
-          userRole: 'jtcolocker'
-        })
-      }
     })
+  })
 
-    // 【3】过滤现有用户数据
-    const finalUserList = existingUserList.value.filter((existingUser) => {
-      // 检查是否在删除列表中
-      if (toDeleteUserIds.value.includes(existingUser.id)) {
-        return false // 被删除的用户,过滤掉
-      }
-
-      // 检查是否在新增列表中(说明有变化)
-      const isInAddedList = userList.some((addedUser) =>
-        addedUser.userId === existingUser.userId &&
-        addedUser.userRole === existingUser.userRole &&
-        addedUser.groupId === existingUser.groupId
-      )
-
-      // 如果不在新增列表中,说明没有变化,过滤掉
-      if (!isInAddedList) {
-        return false
-      }
-
-      return true // 保留有变化的用户
+  addedCoLockUsers.value.forEach(u => {
+    finalList.push({
+      ticketId,
+      groupId: null,
+      userId: u.userId,
+      userName: u.nickname,
+      userType: 0,
+      userRole: 'jtcolocker'
     })
+  })
 
-    console.log('最终要新增的:', finalUserList)
+  const toAdd = finalList.filter(newU =>
+    !existingUserList.value.some(e =>
+      e.userId === newU.userId &&
+      e.userRole === newU.userRole &&
+      e.groupId === newU.groupId
+    )
+  )
+
+  const toDelete = existingUserList.value.filter(e =>
+    !finalList.some(newU =>
+      newU.userId === e.userId &&
+      newU.userRole === e.userRole &&
+      newU.groupId === e.groupId
+    )
+  ).map(e => e.id)
+
+  if (!toAdd.length && !toDelete.length) {
+    ElMessage.warning('无更改')
+    return
+  }
 
-    // 【4】提示
-    if (finalUserList.length === 0 && toDeleteUserIds.value.length === 0) {
-      ElMessage.warning('请至少选择一个人员或移除一个人员')
-      return
-    }
-    console.log(jtcolocker.value,jtlocker.value,addedCoLockUsers.value,addedLockUsers.value,'jjjj')
-    // 【5】先删
-    if (toDeleteUserIds.value.length > 0) {
-      const deleteIds = toDeleteUserIds.value.join(',')
-      await JobUserApi.deleteJobTicketUserList(deleteIds)
-      console.log('删除成功:', toDeleteUserIds.value)
-    }
+  if (toDelete.length) {
+    await JobUserApi.deleteJobTicketUserList(toDelete.join(','))
+  }
 
-    // 【6】再新增 - 只有当有新增数据时才调用接口
-    if (finalUserList.length > 0) {
-      console.log(finalUserList,'finalUserList')
-      const result = await JobUserApi.insertJobTicketUser(finalUserList)
-      if (result) {
-        ElMessage.success('保存成功')
-        toDeleteUserIds.value = []
-      } else {
-        ElMessage.error(result?.msg || '保存失败')
-      }
-    } else {
-      // 如果只有删除操作,也提示成功
-      ElMessage.success('保存成功')
-      toDeleteUserIds.value = []
-    }
-  } catch (error) {
-    console.error('保存失败:', error)
+  if (toAdd.length) {
+    await JobUserApi.insertJobTicketUser(toAdd)
   }
+
+  ElMessage.success('保存成功')
 }
 
 const cancel = () => {
-  if (route.query.type == 'update') {
-    router.push({
-      path: '/jobTicket/jobTicket/job/UpdateJob',
-      query: {
-        id: route.query.ticketId,
-        type: route.query.type
-      }
-    })
-  } else {
-    router.push({
-      path: '/jobTicket/jobTicket/job/UpdateJob',
-      query: {
-        id: route.query.ticketId,
-        type: route.query.type
-      }
-    })
-  }
+  router.push({
+    path: '/jobTicket/jobTicket/job/UpdateJob',
+    query: {
+      id: route.query.ticketId,
+      type: route.query.type
+    }
+  })
 }
 </script>
+
 <style lang="scss" scoped>
 .page-container {
   padding: 20px;