|
|
@@ -846,8 +846,52 @@ public class FsUserServiceImpl implements IFsUserService {
|
|
|
return fsUserMapper.selectFsUserByUserIds(userIds, companyUserId);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public FsUserSummaryCountVO userSummaryCount(Long userId) {
|
|
|
+ // 判断是否是管理员
|
|
|
+ Long companyId;
|
|
|
+ CompanyUser companyUser = companyUserMapper.selectCompanyUserById(userId);
|
|
|
+ if (companyUser != null && companyUser.isAdmin()) {
|
|
|
+ userId = 0L;
|
|
|
+ companyId = companyUser.getCompanyId();
|
|
|
+ } else {
|
|
|
+ companyId = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 CompletableFuture 异步执行两个查询
|
|
|
+
|
|
|
+ Long finalUserId = userId;
|
|
|
+ CompletableFuture<Integer> userTotalFuture = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.getUserTotal(finalUserId, companyId));
|
|
|
+
|
|
|
+ CompletableFuture<Integer> todayNewUserFuture = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.getTodayNewUser(finalUserId, companyId));
|
|
|
+
|
|
|
+ CompletableFuture<List<FsUserSummaryCountTagVO>> countTagList = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.countTag(finalUserId, companyId));
|
|
|
+
|
|
|
+ // 等待查询结果并构建返回对象
|
|
|
+ FsUserSummaryCountVO fsUserSummaryCountVO = new FsUserSummaryCountVO();
|
|
|
+ try {
|
|
|
+ fsUserSummaryCountVO.setUserTotal(userTotalFuture.get());
|
|
|
+ fsUserSummaryCountVO.setTodayNewUser(todayNewUserFuture.get());
|
|
|
+ fsUserSummaryCountVO.setTagList(countTagList.get());
|
|
|
+
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ logger.error("异步查询用户统计数据失败", e);
|
|
|
+ // 设置默认值
|
|
|
+ fsUserSummaryCountVO.setUserTotal(0);
|
|
|
+ fsUserSummaryCountVO.setTodayNewUser(0);
|
|
|
+ fsUserSummaryCountVO.setTagList(new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ return fsUserSummaryCountVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ public FsUserSummaryCountVO userSummaryCountOld(Long userId) {
|
|
|
+ // 开始时间
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
// 判断是否是管理员
|
|
|
Long companyId = null;
|
|
|
CompanyUser companyUser = companyUserMapper.selectCompanyUserById(userId);
|
|
|
@@ -855,9 +899,24 @@ public class FsUserServiceImpl implements IFsUserService {
|
|
|
userId = 0L;
|
|
|
companyId = companyUser.getCompanyId();
|
|
|
}
|
|
|
+ long start2 = System.currentTimeMillis();
|
|
|
+ logger.info("countUserSummary 执行耗时: {}ms", start2 - start);
|
|
|
+
|
|
|
+ // Integer getUserTotal(@Param("userId") Long userId, @Param("companyId") Long companyId);
|
|
|
+ //
|
|
|
+ // Integer getTodayNewUser(@Param("userId") Long userId, @Param("companyId") Long companyId);
|
|
|
+
|
|
|
+
|
|
|
FsUserSummaryCountVO fsUserSummaryCountVO = fsUserMapper.countUserSummary(userId, companyId);
|
|
|
+ long start3 =System.currentTimeMillis();
|
|
|
+ logger.info("countTag 执行耗时: {}ms", start3 - start2);
|
|
|
List<FsUserSummaryCountTagVO> countTagList = fsUserMapper.countTag(userId, companyId);
|
|
|
fsUserSummaryCountVO.setTagList(countTagList);
|
|
|
+
|
|
|
+ // 结束时间
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("countUserAnswerStats 执行耗时: {}ms", end - start3);
|
|
|
+
|
|
|
return fsUserSummaryCountVO;
|
|
|
}
|
|
|
|
|
|
@@ -1201,7 +1260,95 @@ public class FsUserServiceImpl implements IFsUserService {
|
|
|
return fsUserMapper.selectFsUserListByJointUserNameKey(userNameKey);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 该方法原方法是 getUserStatisticsOld
|
|
|
+ * 目的为了优化sql查询 fsUserMapper.countUserStats 现拆分成三次异步查询
|
|
|
+ * @Param:
|
|
|
+ * @Return:
|
|
|
+ * @Author xgb
|
|
|
+ * @Date 2025/10/29 15:34
|
|
|
+ */
|
|
|
private FsUserStatisticsVO getUserStatistics(UserStatisticsCommonParam param) {
|
|
|
+
|
|
|
+ FsUserStatisticsVO fsUserStatisticsVO = new FsUserStatisticsVO();
|
|
|
+
|
|
|
+ // 判断是否是管理员
|
|
|
+ CompanyUser companyUser = companyUserMapper.selectCompanyUserById(param.getUserId());
|
|
|
+ if (companyUser != null && companyUser.isAdmin()){
|
|
|
+ param.setUserId(0L);
|
|
|
+ param.setCompanyId(companyUser.getCompanyId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 异步调用三个查询方法
|
|
|
+ CompletableFuture<Map<String, Object>> watchStatsFuture = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.countUserWatchStats(param));
|
|
|
+
|
|
|
+ CompletableFuture<Map<String, Object>> answerStatsFuture = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.countUserAnswerStats(param));
|
|
|
+
|
|
|
+ CompletableFuture<Map<String, Object>> redPacketStatsFuture = CompletableFuture.supplyAsync(() ->
|
|
|
+ fsUserMapper.countUserRedPacketStats(param));
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 等待所有查询完成
|
|
|
+ Map<String, Object> watchMap = watchStatsFuture.get();
|
|
|
+ Map<String, Object> answerMap = answerStatsFuture.get();
|
|
|
+ Map<String, Object> redPacketMap = redPacketStatsFuture.get();
|
|
|
+
|
|
|
+ // 处理观看和完成人数统计
|
|
|
+ if (watchMap != null) {
|
|
|
+ fsUserStatisticsVO.setCourseWatchNum(Integer.valueOf(watchMap.getOrDefault("courseWatchNum", 0L).toString()))
|
|
|
+ .setCourseCompleteNum(Integer.valueOf(watchMap.getOrDefault("courseCompleteNum", 0L).toString()));
|
|
|
+
|
|
|
+ Long completeNum = (Long) watchMap.get("courseCompleteNum");
|
|
|
+ Long watchNum = (Long) watchMap.get("courseWatchNum");
|
|
|
+ if (completeNum != null && watchNum != null && watchNum > 0) {
|
|
|
+ int courseCompleteRate = BigDecimal.valueOf(completeNum)
|
|
|
+ .divide(BigDecimal.valueOf(watchNum), 2, RoundingMode.HALF_UP)
|
|
|
+ .multiply(BigDecimal.valueOf(100))
|
|
|
+ .intValue();
|
|
|
+ fsUserStatisticsVO.setCourseCompleteRate(courseCompleteRate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理答题统计
|
|
|
+ if (answerMap != null) {
|
|
|
+ Long answerRightNum = (Long) answerMap.get("answerRightNum");
|
|
|
+ Long answerNum = (Long) answerMap.get("answerNum");
|
|
|
+
|
|
|
+ fsUserStatisticsVO.setAnswerNum(answerNum.intValue()).setAnswerRightNum(answerRightNum.intValue());
|
|
|
+
|
|
|
+ if (answerRightNum != null && answerNum != null && answerNum > 0) {
|
|
|
+ int answerCompleteRate = BigDecimal.valueOf(answerRightNum)
|
|
|
+ .divide(BigDecimal.valueOf(answerNum), 2, RoundingMode.HALF_UP)
|
|
|
+ .multiply(BigDecimal.valueOf(100))
|
|
|
+ .intValue();
|
|
|
+ fsUserStatisticsVO.setAnswerRightRate(answerCompleteRate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理红包统计
|
|
|
+ if (redPacketMap != null) {
|
|
|
+ Object numObj = redPacketMap.get("redPacketNum");
|
|
|
+ Object amtObj = redPacketMap.get("redPacketAmount");
|
|
|
+ if (numObj != null && amtObj != null) {
|
|
|
+ fsUserStatisticsVO.setRedPacketNum(Integer.parseInt(numObj.toString()))
|
|
|
+ .setRedPacketAmount(new BigDecimal(amtObj.toString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ // 异常处理
|
|
|
+ logger.error("异步查询用户统计数据失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return fsUserStatisticsVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private FsUserStatisticsVO getUserStatisticsNew(UserStatisticsCommonParam param) {
|
|
|
FsUserStatisticsVO fsUserStatisticsVO = new FsUserStatisticsVO();
|
|
|
|
|
|
// 判断是否是管理员
|