This commit is contained in:
2026-06-08 16:34:31 +08:00
parent 3e9264ebc6
commit aea34dc5e4
7 changed files with 122 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import cn.hutool.json.JSONObject;
import cn.wepact.dfm.training.dto.SyncRequest;
import cn.wepact.dfm.training.dto.entity.SyncTaskLog;
import cn.wepact.dfm.training.dto.entity.SyncUserInfoLog;
import cn.wepact.dfm.training.dto.entity.TUser;
import cn.wepact.dfm.training.mapper.SyncUserInfoLogMapper;
import cn.wepact.dfm.training.mapper.TUserMapper;
import cn.wepact.dfm.training.util.HttpClientUtil;
@@ -33,8 +34,10 @@ import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@@ -199,6 +202,26 @@ public class SyncUserInfoService {
return;
}
// 第一遍收集所有用户名批量查询用户ID替代 N+1 逐条查询)
List<String> usernames = new ArrayList<>(dataArray.size());
for (int i = 0; i < dataArray.size(); i++) {
JSONObject dataObject = dataArray.getJSONObject(i);
if (dataObject == null) continue;
String u = getStringValue(dataObject, "username");
if (!u.isEmpty()) {
usernames.add(u);
}
}
Map<String, Long> userIdCache = new HashMap<>();
if (!usernames.isEmpty()) {
List<TUser> tUsers = userMapper.batchFindUserIdByUsernames(usernames);
for (TUser tUser : tUsers) {
if (tUser.getUserNo() != null) {
userIdCache.put(tUser.getUserNo(), tUser.getId());
}
}
}
for (int i = 0; i < dataArray.size(); i++) {
JSONObject dataObject = dataArray.getJSONObject(i);
if (dataObject == null) {
@@ -213,8 +236,8 @@ public class SyncUserInfoService {
continue;
}
// 获取系统内部的user_id
Long userId = userMapper.findUserIdByUsername(username);
// 从批量查询的缓存中获取系统内部的user_id
Long userId = userIdCache.get(username);
if (userId == null) {
logger.warn("未找到用户: {}", username);
continue;