update
This commit is contained in:
@@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@Mapper
|
||||
public interface SyncUserInfoLogMapper extends MyBatisBaseMapper<SyncUserInfoLog, Long> {
|
||||
|
||||
@@ -22,4 +23,6 @@ public interface SyncUserInfoLogMapper extends MyBatisBaseMapper<SyncUserInfoLog
|
||||
|
||||
void updateLastSyncTime(Date date);
|
||||
SyncUserInfoLog findUserInfoByStuUserId(@Param("stuUserId") String stuUserId);
|
||||
|
||||
List<SyncUserInfoLog> batchFindUserInfoByStuUserIds(@Param("stuUserIds") List<String> stuUserIds);
|
||||
}
|
||||
|
||||
@@ -150,4 +150,11 @@
|
||||
<select id="findUserInfoByStuUserId" resultMap="UserResultMap">
|
||||
SELECT user_id, username FROM sync_user_info_log WHERE user_id = #{stuUserId}
|
||||
</select>
|
||||
|
||||
<select id="batchFindUserInfoByStuUserIds" resultMap="UserResultMap">
|
||||
SELECT user_id, username FROM sync_user_info_log WHERE user_id IN
|
||||
<foreach collection="stuUserIds" item="stuUserId" open="(" separator="," close=")">
|
||||
#{stuUserId}
|
||||
</foreach>
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -22,4 +22,11 @@ public interface TUserMapper extends MyBatisBaseMapper<TUser, Long> {
|
||||
*/
|
||||
|
||||
Long findUserIdByUsername(@Param("username") String username);
|
||||
|
||||
/**
|
||||
* 批量查询用户ID(替代 N+1 循环查询)
|
||||
* @param usernames 用户名列表
|
||||
* @return username -> userId 映射列表
|
||||
*/
|
||||
List<TUser> batchFindUserIdByUsernames(@Param("usernames") List<String> usernames);
|
||||
}
|
||||
|
||||
@@ -189,5 +189,12 @@
|
||||
|
||||
<select id="findUserIdByUsername" resultType="java.lang.Long">
|
||||
SELECT id FROM t_user WHERE user_no = #{username}
|
||||
</select>
|
||||
</select>
|
||||
|
||||
<select id="batchFindUserIdByUsernames" resultType="cn.wepact.dfm.training.dto.entity.TUser">
|
||||
SELECT id, user_no AS userNo FROM t_user WHERE user_no IN
|
||||
<foreach collection="usernames" item="username" open="(" separator="," close=")">
|
||||
#{username}
|
||||
</foreach>
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -304,21 +304,51 @@ public class SyncTaskService {
|
||||
responseJson = new JSONObject(responseData);
|
||||
dataArray = responseJson.getJSONArray("data");
|
||||
|
||||
// 第一遍:收集所有 stuUserId,批量查询用户信息(替代 N+1)
|
||||
List<String> stuUserIds = new ArrayList<>(dataArray.size());
|
||||
for (int i = 0; i < dataArray.size(); i++) {
|
||||
JSONObject taskData = dataArray.getJSONObject(i);
|
||||
String stuUserId = getStringValue(taskData, "stuUserId");
|
||||
if (!stuUserId.isEmpty()) {
|
||||
stuUserIds.add(stuUserId);
|
||||
}
|
||||
}
|
||||
// 批量查询:sync_user_info_log(stuUserId → username)
|
||||
Map<String, String> stuUserIdToUsername = new HashMap<>();
|
||||
if (!stuUserIds.isEmpty()) {
|
||||
List<SyncUserInfoLog> userInfos = syncUserInfoLogMapper.batchFindUserInfoByStuUserIds(stuUserIds);
|
||||
for (SyncUserInfoLog info : userInfos) {
|
||||
if (info.getUserId() != null && info.getUsername() != null) {
|
||||
stuUserIdToUsername.put(info.getUserId(), info.getUsername());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 批量查询:t_user(username → userId)
|
||||
List<String> usernames = new ArrayList<>(stuUserIdToUsername.values());
|
||||
Map<String, Long> usernameToUserId = new HashMap<>();
|
||||
if (!usernames.isEmpty()) {
|
||||
List<TUser> tUsers = userMapper.batchFindUserIdByUsernames(usernames);
|
||||
for (TUser tUser : tUsers) {
|
||||
if (tUser.getUserNo() != null) {
|
||||
usernameToUserId.put(tUser.getUserNo(), tUser.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < dataArray.size(); i++) {
|
||||
JSONObject taskData = dataArray.getJSONObject(i);
|
||||
// 从任务数据中获取学生用户ID
|
||||
String stuUserId = getStringValue(taskData, "stuUserId");
|
||||
if (stuUserId.isEmpty()) continue;
|
||||
|
||||
|
||||
|
||||
// 1. 获取用户信息:根据云学堂的学生用户ID查询本地用户信息
|
||||
SyncUserInfoLog userInfo = syncUserInfoLogMapper.findUserInfoByStuUserId(stuUserId);
|
||||
if (userInfo == null) {
|
||||
// 1. 从缓存中获取用户信息
|
||||
String username = stuUserIdToUsername.get(stuUserId);
|
||||
if (username == null) {
|
||||
continue; // 如果在本地数据库中找不到对应的用户信息,跳过当前记录的处理
|
||||
}
|
||||
|
||||
// 2. 获取系统内部的user_id:通过用户名查找对应的用户ID
|
||||
Long userId = userMapper.findUserIdByUsername(userInfo.getUsername());
|
||||
// 2. 从缓存中获取系统内部的user_id
|
||||
Long userId = usernameToUserId.get(username);
|
||||
if (userId == null) {
|
||||
continue; // 如果找不到对应的用户ID,跳过当前记录的处理
|
||||
}
|
||||
@@ -535,15 +565,42 @@ public class SyncTaskService {
|
||||
}
|
||||
|
||||
public void processTaskScores(List<TaskScoreSummary> taskScoreSummaries) {
|
||||
// 批量查询用户信息缓存(替代 N+1)
|
||||
List<String> stuUserIds = new ArrayList<>(taskScoreSummaries.size());
|
||||
for (TaskScoreSummary summary : taskScoreSummaries) {
|
||||
// 1. 获取用户信息
|
||||
SyncUserInfoLog userInfo = syncUserInfoLogMapper.findUserInfoByStuUserId(summary.getStuUserId());
|
||||
if (userInfo == null) {
|
||||
if (summary.getStuUserId() != null) {
|
||||
stuUserIds.add(summary.getStuUserId());
|
||||
}
|
||||
}
|
||||
Map<String, String> stuUserIdToUsername = new HashMap<>();
|
||||
if (!stuUserIds.isEmpty()) {
|
||||
List<SyncUserInfoLog> userInfos = syncUserInfoLogMapper.batchFindUserInfoByStuUserIds(stuUserIds);
|
||||
for (SyncUserInfoLog info : userInfos) {
|
||||
if (info.getUserId() != null && info.getUsername() != null) {
|
||||
stuUserIdToUsername.put(info.getUserId(), info.getUsername());
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> usernames = new ArrayList<>(stuUserIdToUsername.values());
|
||||
Map<String, Long> usernameToUserId = new HashMap<>();
|
||||
if (!usernames.isEmpty()) {
|
||||
List<TUser> tUsers = userMapper.batchFindUserIdByUsernames(usernames);
|
||||
for (TUser tUser : tUsers) {
|
||||
if (tUser.getUserNo() != null) {
|
||||
usernameToUserId.put(tUser.getUserNo(), tUser.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (TaskScoreSummary summary : taskScoreSummaries) {
|
||||
// 1. 从缓存中获取用户信息
|
||||
String username = stuUserIdToUsername.get(summary.getStuUserId());
|
||||
if (username == null) {
|
||||
continue; // 如果没有找到用户信息,跳过
|
||||
}
|
||||
|
||||
// 2. 获取 user_id
|
||||
Long userId = userMapper.findUserIdByUsername(userInfo.getUsername());
|
||||
// 2. 从缓存中获取 user_id
|
||||
Long userId = usernameToUserId.get(username);
|
||||
if (userId == null) {
|
||||
continue; // 如果没有找到用户 ID,跳过
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -79,12 +79,12 @@
|
||||
</root>
|
||||
|
||||
|
||||
<!-- 增加SQL打印 -->
|
||||
<logger name="org.apache.ibatis" level="debug">
|
||||
<!-- 增加SQL打印(additivity=false 防止向上传播到 root 导致重复打印) -->
|
||||
<logger name="org.apache.ibatis" level="debug" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
|
||||
<logger name="cn.wepact.dfm.training.mapper" level="debug">
|
||||
<logger name="cn.wepact.dfm.training.mapper" level="debug" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<!-- 增加SQL打印 end-->
|
||||
|
||||
Reference in New Issue
Block a user