czh-20260630-优化查询;防止清空日期
This commit is contained in:
+95
-29
@@ -20,12 +20,16 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TaskListDetialServiceImpl extends ServiceImpl<TaskListDetialMapper, TaskListDetial> implements ITaskListDetialService {
|
public class TaskListDetialServiceImpl extends ServiceImpl<TaskListDetialMapper, TaskListDetial> implements ITaskListDetialService {
|
||||||
|
|
||||||
@@ -204,19 +208,16 @@ public class TaskListDetialServiceImpl extends ServiceImpl<TaskListDetialMapper,
|
|||||||
task.setFollowersName("");
|
task.setFollowersName("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean clearStartTime = task.getStartTime() == null && existing.getStartTime() != null;
|
// 前端没传日期则保留原值,避免局部更新时意外清除
|
||||||
boolean clearEndTime = task.getEndTime() == null && existing.getEndTime() != null;
|
if (task.getStartTime() == null) {
|
||||||
|
task.setStartTime(existing.getStartTime());
|
||||||
|
}
|
||||||
|
if (task.getEndTime() == null) {
|
||||||
|
task.setEndTime(existing.getEndTime());
|
||||||
|
}
|
||||||
|
|
||||||
taskListDetialMapper.updateById(task);
|
taskListDetialMapper.updateById(task);
|
||||||
|
|
||||||
if (clearStartTime || clearEndTime) {
|
|
||||||
UpdateWrapper<TaskListDetial> uw = new UpdateWrapper<>();
|
|
||||||
uw.eq("id", task.getId());
|
|
||||||
if (clearStartTime) uw.set("start_time", null);
|
|
||||||
if (clearEndTime) uw.set("end_time", null);
|
|
||||||
taskListDetialMapper.update(null, uw);
|
|
||||||
}
|
|
||||||
|
|
||||||
taskListDetialMapper.appendAssigner(task.getId(), userId, loginUser.getRealname());
|
taskListDetialMapper.appendAssigner(task.getId(), userId, loginUser.getRealname());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -544,43 +545,108 @@ public class TaskListDetialServiceImpl extends ServiceImpl<TaskListDetialMapper,
|
|||||||
if (list == null || list.isEmpty()) {
|
if (list == null || list.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1. 收集所有唯一的用户名和用户ID
|
||||||
|
Set<String> allUsernames = new LinkedHashSet<>();
|
||||||
|
Set<String> allUserIds = new LinkedHashSet<>();
|
||||||
for (TaskListDetial task : list) {
|
for (TaskListDetial task : list) {
|
||||||
if (oConvertUtils.isNotEmpty(task.getCreateBy())) {
|
if (oConvertUtils.isNotEmpty(task.getCreateBy())) {
|
||||||
List<com.alibaba.fastjson.JSONObject> users = sysBaseAPI.queryUsersByUsernames(task.getCreateBy());
|
allUsernames.add(task.getCreateBy());
|
||||||
if (users != null && !users.isEmpty()) {
|
}
|
||||||
task.setCreateByName(users.get(0).getString("realname"));
|
collectUserIds(task.getAssigneeId(), allUserIds);
|
||||||
|
collectUserIds(task.getParticipantId(), allUserIds);
|
||||||
|
collectUserIds(task.getFollowersId(), allUserIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 批量查询,构建映射表
|
||||||
|
Map<String, String> usernameToRealname = new HashMap<>();
|
||||||
|
if (!allUsernames.isEmpty()) {
|
||||||
|
List<JSONObject> users = sysBaseAPI.queryUsersByUsernames(String.join(",", allUsernames));
|
||||||
|
if (users != null) {
|
||||||
|
for (JSONObject u : users) {
|
||||||
|
usernameToRealname.put(u.getString("username"), u.getString("realname"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> idToRealname = new HashMap<>();
|
||||||
|
if (!allUserIds.isEmpty()) {
|
||||||
|
List<JSONObject> users = sysBaseAPI.queryUsersByIds(String.join(",", allUserIds));
|
||||||
|
if (users != null) {
|
||||||
|
for (JSONObject u : users) {
|
||||||
|
idToRealname.put(u.getString("id"), u.getString("realname"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 从映射表填充,无需再查数据库
|
||||||
|
for (TaskListDetial task : list) {
|
||||||
|
if (oConvertUtils.isNotEmpty(task.getCreateBy())) {
|
||||||
|
String name = usernameToRealname.get(task.getCreateBy());
|
||||||
|
if (name != null) {
|
||||||
|
task.setCreateByName(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (oConvertUtils.isNotEmpty(task.getAssigneeId()) && oConvertUtils.isEmpty(task.getAssigneeName())) {
|
if (oConvertUtils.isNotEmpty(task.getAssigneeId()) && oConvertUtils.isEmpty(task.getAssigneeName())) {
|
||||||
task.setAssigneeName(translateUserIdsToNames(task.getAssigneeId()));
|
task.setAssigneeName(buildNamesFromMap(task.getAssigneeId(), idToRealname));
|
||||||
}
|
}
|
||||||
if (oConvertUtils.isNotEmpty(task.getParticipantId()) && oConvertUtils.isEmpty(task.getParticipantName())) {
|
if (oConvertUtils.isNotEmpty(task.getParticipantId()) && oConvertUtils.isEmpty(task.getParticipantName())) {
|
||||||
task.setParticipantName(translateUserIdsToNames(task.getParticipantId()));
|
task.setParticipantName(buildNamesFromMap(task.getParticipantId(), idToRealname));
|
||||||
}
|
}
|
||||||
if (oConvertUtils.isNotEmpty(task.getFollowersId()) && oConvertUtils.isEmpty(task.getFollowersName())) {
|
if (oConvertUtils.isNotEmpty(task.getFollowersId()) && oConvertUtils.isEmpty(task.getFollowersName())) {
|
||||||
task.setFollowersName(translateUserIdsToNames(task.getFollowersId()));
|
task.setFollowersName(buildNamesFromMap(task.getFollowersId(), idToRealname));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void collectUserIds(String idsStr, Set<String> idSet) {
|
||||||
|
if (oConvertUtils.isEmpty(idsStr)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (String id : idsStr.split(",")) {
|
||||||
|
String trimmed = id.trim();
|
||||||
|
if (oConvertUtils.isNotEmpty(trimmed)) {
|
||||||
|
idSet.add(trimmed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildNamesFromMap(String idsStr, Map<String, String> idToRealname) {
|
||||||
|
if (oConvertUtils.isEmpty(idsStr) || idToRealname.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (String id : idsStr.split(",")) {
|
||||||
|
String trimmed = id.trim();
|
||||||
|
if (oConvertUtils.isNotEmpty(trimmed)) {
|
||||||
|
String name = idToRealname.get(trimmed);
|
||||||
|
if (name != null) {
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
sb.append(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.length() > 0 ? sb.toString() : null;
|
||||||
|
}
|
||||||
|
|
||||||
private String translateUserIdsToNames(String ids) {
|
private String translateUserIdsToNames(String ids) {
|
||||||
if (oConvertUtils.isEmpty(ids)) {
|
if (oConvertUtils.isEmpty(ids)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String[] idArr = ids.split(",");
|
List<JSONObject> users = sysBaseAPI.queryUsersByIds(ids);
|
||||||
StringBuilder names = new StringBuilder();
|
if (users == null || users.isEmpty()) {
|
||||||
for (String id : idArr) {
|
return null;
|
||||||
if (oConvertUtils.isNotEmpty(id)) {
|
|
||||||
LoginUser user = sysBaseAPI.getUserById(id.trim());
|
|
||||||
if (user != null) {
|
|
||||||
if (names.length() > 0) {
|
|
||||||
names.append(",");
|
|
||||||
}
|
|
||||||
names.append(user.getRealname());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return names.length() > 0 ? names.toString() : null;
|
StringBuilder names = new StringBuilder();
|
||||||
|
for (JSONObject user : users) {
|
||||||
|
if (names.length() > 0) {
|
||||||
|
names.append(",");
|
||||||
|
}
|
||||||
|
names.append(user.getString("realname"));
|
||||||
|
}
|
||||||
|
return names.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String mergeIds(String existingIds, String newIds) {
|
private String mergeIds(String existingIds, String newIds) {
|
||||||
|
|||||||
+64
-27
@@ -28,9 +28,15 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: 任务清单表
|
* @Description: 任务清单表
|
||||||
@@ -549,42 +555,62 @@ public class TaskListServiceImpl extends ServiceImpl<TaskListMapper, TaskList> i
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<TaskList> enrichListSummaries(List<TaskList> lists, String currentUserId) {
|
private List<TaskList> enrichListSummaries(List<TaskList> lists, String currentUserId) {
|
||||||
|
if (lists == null || lists.isEmpty()) {
|
||||||
|
return lists;
|
||||||
|
}
|
||||||
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
for (TaskList list : lists) {
|
for (TaskList list : lists) {
|
||||||
if (list.getCreateTime() != null) {
|
if (list.getCreateTime() != null) {
|
||||||
list.setCreateTimeStr(sdf.format(list.getCreateTime()));
|
list.setCreateTimeStr(sdf.format(list.getCreateTime()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LambdaQueryWrapper<TaskListPermission> ownerQuery = new LambdaQueryWrapper<>();
|
// 1. 收集所有清单ID,批量查询权限表
|
||||||
ownerQuery.eq(TaskListPermission::getMainId, list.getId());
|
List<String> mainIds = lists.stream().map(TaskList::getId).collect(Collectors.toList());
|
||||||
ownerQuery.eq(TaskListPermission::getPermission, "1");
|
List<TaskListPermission> allPerms = taskListPermissionMapper.selectList(
|
||||||
TaskListPermission owner = taskListPermissionMapper.selectOne(ownerQuery);
|
new LambdaQueryWrapper<TaskListPermission>().in(TaskListPermission::getMainId, mainIds)
|
||||||
if (owner != null) {
|
);
|
||||||
LoginUser ownerUser = sysBaseAPI.getUserById(owner.getUserId());
|
|
||||||
if (ownerUser != null) {
|
// 2. 收集所有用户ID,批量查询用户
|
||||||
list.setOwnerName(ownerUser.getRealname());
|
java.util.Set<String> allUserIds = new java.util.LinkedHashSet<>();
|
||||||
}
|
for (TaskListPermission perm : allPerms) {
|
||||||
if (currentUserId != null && currentUserId.equals(owner.getUserId())) {
|
allUserIds.add(perm.getUserId());
|
||||||
list.setMyPermission("1");
|
}
|
||||||
|
Map<String, String> idToName = new HashMap<>();
|
||||||
|
if (!allUserIds.isEmpty()) {
|
||||||
|
List<JSONObject> users = sysBaseAPI.queryUsersByIds(String.join(",", allUserIds));
|
||||||
|
if (users != null) {
|
||||||
|
for (JSONObject u : users) {
|
||||||
|
idToName.put(u.getString("id"), u.getString("realname"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LambdaQueryWrapper<TaskListPermission> collabQuery = new LambdaQueryWrapper<>();
|
// 3. 按清单分组,从映射表填充
|
||||||
collabQuery.eq(TaskListPermission::getMainId, list.getId());
|
Map<String, List<TaskListPermission>> permByMainId = allPerms.stream()
|
||||||
collabQuery.ne(TaskListPermission::getPermission, "1");
|
.collect(Collectors.groupingBy(TaskListPermission::getMainId));
|
||||||
List<TaskListPermission> collabs = taskListPermissionMapper.selectList(collabQuery);
|
|
||||||
if (!collabs.isEmpty()) {
|
for (TaskList list : lists) {
|
||||||
List<String> collabUserIds = collabs.stream().map(TaskListPermission::getUserId).collect(java.util.stream.Collectors.toList());
|
List<TaskListPermission> perms = permByMainId.getOrDefault(list.getId(), java.util.Collections.emptyList());
|
||||||
List<String> collabNames = new java.util.ArrayList<>();
|
List<String> collabNames = new ArrayList<>();
|
||||||
for (String collabUserId : collabUserIds) {
|
for (TaskListPermission perm : perms) {
|
||||||
LoginUser collabUser = sysBaseAPI.getUserById(collabUserId);
|
String name = idToName.get(perm.getUserId());
|
||||||
if (collabUser != null && collabUser.getRealname() != null) {
|
if (name == null) continue;
|
||||||
collabNames.add(collabUser.getRealname());
|
if ("1".equals(perm.getPermission())) {
|
||||||
|
list.setOwnerName(name);
|
||||||
|
if (currentUserId != null && currentUserId.equals(perm.getUserId())) {
|
||||||
|
list.setMyPermission("1");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
collabNames.add(name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (!collabNames.isEmpty()) {
|
||||||
list.setCollaboratorNames(String.join(", ", collabNames));
|
list.setCollaboratorNames(String.join(", ", collabNames));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lists;
|
return lists;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -595,16 +621,27 @@ public class TaskListServiceImpl extends ServiceImpl<TaskListMapper, TaskList> i
|
|||||||
query.orderByAsc(TaskListPermission::getCreateTime);
|
query.orderByAsc(TaskListPermission::getCreateTime);
|
||||||
List<TaskListPermission> perms = taskListPermissionMapper.selectList(query);
|
List<TaskListPermission> perms = taskListPermissionMapper.selectList(query);
|
||||||
|
|
||||||
List<CollaboratorVO> result = new java.util.ArrayList<>();
|
if (perms.isEmpty()) {
|
||||||
|
return java.util.Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量查询所有协作用户
|
||||||
|
String userIds = perms.stream().map(TaskListPermission::getUserId).collect(Collectors.joining(","));
|
||||||
|
Map<String, String> idToName = new HashMap<>();
|
||||||
|
List<JSONObject> users = sysBaseAPI.queryUsersByIds(userIds);
|
||||||
|
if (users != null) {
|
||||||
|
for (JSONObject u : users) {
|
||||||
|
idToName.put(u.getString("id"), u.getString("realname"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<CollaboratorVO> result = new ArrayList<>();
|
||||||
for (TaskListPermission perm : perms) {
|
for (TaskListPermission perm : perms) {
|
||||||
CollaboratorVO vo = new CollaboratorVO();
|
CollaboratorVO vo = new CollaboratorVO();
|
||||||
vo.setPermissionId(perm.getId());
|
vo.setPermissionId(perm.getId());
|
||||||
vo.setUserId(perm.getUserId());
|
vo.setUserId(perm.getUserId());
|
||||||
vo.setPermission(perm.getPermission());
|
vo.setPermission(perm.getPermission());
|
||||||
LoginUser user = sysBaseAPI.getUserById(perm.getUserId());
|
vo.setUsername(idToName.getOrDefault(perm.getUserId(), null));
|
||||||
if (user != null) {
|
|
||||||
vo.setUsername(user.getRealname());
|
|
||||||
}
|
|
||||||
result.add(vo);
|
result.add(vo);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user