在每天定时任务中, 同时更新user_info 表中的数据。

This commit is contained in:
haotianmingyue 2024-11-06 17:37:04 +08:00
parent f0c632960c
commit 3ab7f5787f
3 changed files with 48 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package com.ruoyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 启动程序
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* @author ruoyi
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableScheduling
public class RuoYiApplication
{
public static void main(String[] args)

View File

@ -92,11 +92,11 @@ public class UserInfo extends BaseEntity
private String phonenumber;
/** MR在线总长单位分钟 */
@Excel(name = "MR在线总长", readConverterExp = "=位分钟")
@Excel(name = "MR在线总长", readConverterExp = "位秒")
private Long tMrTime;
/** 在线总时长(单位分钟) */
@Excel(name = "在线总时长", readConverterExp = "=位分钟")
@Excel(name = "在线总时长", readConverterExp = "位秒")
private Long tOnlineTime;
/** 消费总额 */

View File

@ -3,14 +3,20 @@ package com.ruoyi.system.service.impl;
import com.ruoyi.system.domain.DailyUser;
import com.ruoyi.system.domain.StUser;
import com.ruoyi.system.domain.SysUserOnlineTime;
import com.ruoyi.system.domain.UserInfo;
import com.ruoyi.system.mapper.DailyUserMapper;
import com.ruoyi.system.mapper.StUserMapper;
import com.ruoyi.system.service.ISysUserOnlineTimeService;
import com.ruoyi.system.service.IUserInfoService;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Service;
//import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Year;
import java.time.ZoneId;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@ -29,6 +35,9 @@ public class SysUserOnlineTimeServiceImpl implements ISysUserOnlineTimeService {
@Autowired
private StUserMapper stUserMapper;
@Autowired
private IUserInfoService userInfoService;
@Override
public void recordUserLogin(Long userId){
@ -176,6 +185,14 @@ public class SysUserOnlineTimeServiceImpl implements ISysUserOnlineTimeService {
}
//统计用户7/30天在线时长, 可以用定时任务,每天设置一个时间点计算
// @Scheduled(cron="0 */5 * * * *")
// public void test(){
// Date date = new Date();
// System.out.println("定时任务输出"+date);
// }
// 每天凌晨 1 点计算近7/30天的在线时长
// @Scheduled(cron="0 0 1 * * ?")
@Scheduled(cron="0 * * * * ?")
@Override
public void calculateUserOnlineStats() {
@ -184,27 +201,35 @@ public class SysUserOnlineTimeServiceImpl implements ISysUserOnlineTimeService {
//获取所有用户, 假设用户在创建账号时就在st_user表中创建数据
List<Long> userIds = stUserMapper.getAllUserIds();
System.out.println("用户总数:"+userIds.size());
//计算每个用户的7/30天在线时长, 每天计算前7天的在线时间
/*
* 近7天时间 = 当前近7天时间-7天前的时间+前1天时间
* 今天 8月9日---计算8月8日的近7天时间------8月7日的近7天时间+8月8日的在线时间-8月1日的在线时间
* 近30天时间同理
* */
for(Long userId : userIds){
// 获取当前日期. 默认到天吧
LocalDate now = LocalDate.now();
// 计算前1天前2天前8天的日期
// 计算前1天前2天前8天前30天的日期,然后根据日期查找在线时间
LocalDate oneDayAgo = now.minusDays(1);
// LocalDate twoDaysAgo = now.minusDays(2);
LocalDate eightDaysAgo = now.minusDays(8);
LocalDate thirtyDaysAgo = now.minusDays(31);
DailyUser oneDayAgoUser = dailyUserMapper.selectByUserIdAndDate(userId, oneDayAgo);
Long oneDayAgoOnlineTime = oneDayAgoUser != null ? oneDayAgoUser.getOnlineTime() : 0L;
DailyUser eightDaysAgoUser = dailyUserMapper.selectByUserIdAndDate(userId, eightDaysAgo);
Long eightDaysAgoOnlineTime = eightDaysAgoUser != null ? eightDaysAgoUser.getOnlineTime() : 0L;
DailyUser thirtyDaysAgoUser = dailyUserMapper.selectByUserIdAndDate(userId, thirtyDaysAgo);
Long thirtyDaysAgoOnlineTime = thirtyDaysAgoUser != null ? thirtyDaysAgoUser.getOnlineTime() : 0L;
// st_user表中每个用户应该只有一行数据,所以直接通过userId来获取
StUser stUser = stUserMapper.selectStUserByUserId(userId);
// 应该一定不会为null吧
@ -212,13 +237,29 @@ public class SysUserOnlineTimeServiceImpl implements ISysUserOnlineTimeService {
Long new7daysTime = last7daysTime + oneDayAgoOnlineTime - eightDaysAgoOnlineTime;
Long last30daysTime = stUser.getThOnlineTime();
Long new30daysTime = last30daysTime + oneDayAgoOnlineTime - thirtyDaysAgoOnlineTime;
//重写数据
stUser.setSeOnlineTime(new7daysTime);
stUser.setThOnlineTime(new30daysTime);
stUser.setUpdateTime(new Date());
stUser.setDate(oneDayAgo);
//更新数据库
stUserMapper.updateStUser(stUser);
int back = stUserMapper.updateStUser(stUser);
if(back == 0){
System.out.println(userId+"用户更新数据失败");
}
// 更新在线总时长
UserInfo userInfo = userInfoService.selectUserInfoByUserId(userId);
userInfo.settOnlineTime(userInfo.gettOnlineTime()+oneDayAgoOnlineTime);
userInfo.setsOnlineTime(new7daysTime);
int back2 = userInfoService.updateUserInfo(userInfo);
if(back2 == 0){
System.out.println(userId+"user_info更新在线总时长失败");
}
}
}