fix:日志添加租户id

This commit is contained in:
2026-01-21 19:34:52 +08:00
parent 566955ede7
commit 924d370eff
4 changed files with 28 additions and 11 deletions

View File

@@ -17,20 +17,23 @@ import java.util.Map;
public interface SysLogMapper extends BaseMapper<SysLog> {
List<LoginStateResp> countDailyLoginUsers(@Param("startTime") String startTime,
@Param("endTime") String endTime);
@Param("endTime") String endTime,
@Param("tenantId") Long tenantId);
List<HourlyOnlineDto> getHourlyOnlineUsers(@Param("date") String date);
/**
* 查找在指定时间之前最后登录的用户
*/
List<UserLastLoginDto> findLastLoginBeforeDate(@Param("beforeTime") LocalDateTime beforeTime);
List<UserLastLoginDto> findLastLoginBeforeDate(@Param("beforeTime") LocalDateTime beforeTime,
@Param("tenantId") Long tenantId);
/**
* 查找用户在指定时间之后的下一次退出时间
*/
List<UserLogoutDto> findNextLogoutTimes(@Param("userIds") List<String> userIds, @Param("afterTime") LocalDateTime afterTime);
List<UserLogoutDto> findNextLogoutTimes(@Param("userIds") List<String> userIds,
@Param("afterTime") LocalDateTime afterTime,
@Param("tenantId") Long tenantId);
List<Map<String, Object>> findActiveSessionsOnDate(@Param("targetDate") String targetDate);
}

View File

@@ -123,6 +123,13 @@ public class SysLog implements Serializable {
@TableField("serviceId")
private String serviceId;
/**
* 租户编号
*/
@Schema(description = "租户编号")
@TableField("tenantId")
private Long tenantId;
/**
* 删除标记
*/

View File

@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdm.common.common.SdmResponse;
import com.sdm.common.common.ThreadLocalContext;
import com.sdm.common.entity.req.system.UserQueryReq;
import com.sdm.common.entity.resp.PageDataResp;
import com.sdm.common.entity.resp.system.CIDUserResp;
@@ -63,6 +64,7 @@ public class ISysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implem
wrapper.ge(SysLog::getCreateTime, sysLog.getCreateTimeArr()[0])
.le(SysLog::getCreateTime, sysLog.getCreateTimeArr()[1]);
}
wrapper.eq(SysLog::getTenantId, sysLog.getTenantId());
wrapper.orderByDesc(SysLog::getCreateTime);
PageHelper.startPage(sysLog.getCurrent(), sysLog.getSize());
List<SysLog> sysLogList = this.list(wrapper);
@@ -86,7 +88,8 @@ public class ISysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implem
public SdmResponse<List<LoginStateResp>> getUserLoginStatistics(SysLogDTO sysLog) {
String startDate = sysLog.getCreateTimeArr()[0];
String endDate = sysLog.getCreateTimeArr()[1];
List<LoginStateResp> respList = baseMapper.countDailyLoginUsers(startDate + " 00:00:00", endDate + " 23:59:59");
Long tenantId = ThreadLocalContext.getTenantId();
List<LoginStateResp> respList = baseMapper.countDailyLoginUsers(startDate + " 00:00:00", endDate + " 23:59:59", tenantId);
// 将结果转换为Map方便查找
Map<String, Integer> resultMap = respList.stream()
.collect(Collectors.toMap(
@@ -122,13 +125,14 @@ public class ISysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implem
// result.setDate(dateStr);
// result.setHourlyStats(hourlyStats);
// return SdmResponse.success(result);
return SdmResponse.success(getHourlyOnlineUsers(LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
Long tenantId = ThreadLocalContext.getTenantId();
return SdmResponse.success(getHourlyOnlineUsers(LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")), tenantId));
}
/**
* 统计指定日期每小时的在线用户数(处理任意时间登录未退出的情况)
*/
public Map<Integer, Long> getHourlyOnlineUsers(LocalDate targetDate) {
public Map<Integer, Long> getHourlyOnlineUsers(LocalDate targetDate, Long tenantId) {
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
@@ -143,12 +147,13 @@ public class ISysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implem
LocalDateTime targetDayEnd = targetDate.plusDays(1).atStartOfDay();
// 2. 获取所有在目标日期之前登录的用户及其最后登录时间
List<UserLastLoginDto> UserLastLoginDtos = baseMapper.findLastLoginBeforeDate(targetDayEnd);
List<UserLastLoginDto> UserLastLoginDtos = baseMapper.findLastLoginBeforeDate(targetDayEnd, tenantId);
// 3. 获取这些用户在目标日期及之后的退出时间
Map<String, LocalDateTime> userLogoutTimes = getUserNextLogoutTimes(
UserLastLoginDtos.stream().map(UserLastLoginDto::getUserId).collect(Collectors.toList()),
targetDayStart
targetDayStart,
tenantId
);
// 4. 构建用户会话
@@ -235,12 +240,12 @@ public class ISysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implem
session.getEffectiveLogoutTime().isAfter(periodStart);
}
private Map<String, LocalDateTime> getUserNextLogoutTimes(List<String> userIds, LocalDateTime afterTime) {
private Map<String, LocalDateTime> getUserNextLogoutTimes(List<String> userIds, LocalDateTime afterTime, Long tenantId) {
Map<String, LocalDateTime> logoutTimes = new HashMap<>();
// 批量查询用户的下一次退出时间
List<UserLogoutDto> logouts = baseMapper.findNextLogoutTimes(userIds, afterTime);
List<UserLogoutDto> logouts = baseMapper.findNextLogoutTimes(userIds, afterTime, tenantId);
for (UserLogoutDto logout : logouts) {
logoutTimes.put(logout.getUserId(), logout.getLogoutTime());

View File

@@ -64,6 +64,7 @@
sys_log
WHERE title = '登录成功'
AND createTime &lt; #{beforeTime}
AND tenantId = #{tenantId}
GROUP BY createBy
</select>
@@ -81,6 +82,7 @@
<foreach collection='userIds' item='userId' open='(' separator=',' close=')'>
#{userId}
</foreach>
AND l1.tenantId = #{tenantId}
GROUP BY l1.createBy, l1.createTime
</select>