This commit is contained in:
670788339
2025-04-18 09:16:47 +08:00
parent 95312696f6
commit 076b1828ad
4 changed files with 55 additions and 21 deletions

View File

@@ -660,8 +660,8 @@ public class StudyCourseApi extends ApiBaseController{
/**
* appendtime 于 study-video-time 合并
* */
@RequestMapping(value="/updateStudyVideoTime",method = {RequestMethod.GET,RequestMethod.POST})
public JsonResponse<String> updateStudyVideoTime(StudyTimeVo studyTime, HttpServletRequest request){
/*@RequestMapping(value="/updateStudyVideoTime1",method = {RequestMethod.GET,RequestMethod.POST})
public JsonResponse<String> updateStudyVideoTime1(StudyTimeVo studyTime, HttpServletRequest request){
// 0 study-video-time , 1 appendtime
if (studyTime.getType() == 0){
@@ -698,7 +698,7 @@ public class StudyCourseApi extends ApiBaseController{
token = request.getHeader("token");
}
try {
studyService.updateStudyDuration(studyTime.getStudyId(),null,studyTime.getContentId(),studyTime.getDuration(),studyTime.getCourseId());
studyService.updateStudyDuration(studyTime.getStudyId(),null,studyTime.getContentId(),studyTime.getVideoTime(),studyTime.getCourseId());
List<StudyCourse> allUserList = thirdApi.getStudyCourseList(studyTime.getStudyId() ,studyTime.getCourseId(), token);
log.info("updateStudyVideoTime type =1 在线课学习记录 = " + allUserList);
return success(studyTime.getId());
@@ -709,9 +709,38 @@ public class StudyCourseApi extends ApiBaseController{
}else{
return error("type不能为空");
}
}
}*/
@RequestMapping(value="/updateStudyVideoTime",method = {RequestMethod.GET,RequestMethod.POST})
public JsonResponse<String> updateStudyVideoTime(StudyTimeVo studyTime, HttpServletRequest request){
try {
if(StringUtils.isBlank(studyTime.getItemId())){
return error("参数错误");
}
if(studyTime.getVideoTime()==null){
return error("无时间点");
}
if(StringUtils.isBlank(studyTime.getStudyId())){
return error("参数错误");
}
if(StringUtils.isBlank(studyTime.getCourseId())){
return error("未指定课程");
}
if(StringUtils.isBlank(studyTime.getContentId())){
return error("未指定资源内容");
}
studyService.updateStudyDuration(studyTime.getStudyId(),null,studyTime.getContentId(),studyTime.getDuration(),studyTime.getCourseId());
if (studyTime.getContentId() != null && studyTime.getCourseId() != null && studyTime.getProgressVideo() != null){
contentService.updateProcessVideo(studyTime.getContentId(), studyTime.getCourseId(), studyTime.getProgressVideo());
}
} catch (Exception e) {
log.error("updateStudyVideoTime",e);
return error("学习时长记录失败",e.getMessage(),null);
}
return success("true");
}
/**获取最后一次的学习内容*/
@GetMapping("/last-study")
public JsonResponse<Map<String,Object>> lastStudy(){

View File

@@ -103,9 +103,11 @@ public class StudyCourseTask {
}
public void saveStudyCourseItemLastTime1() {
@XxlJob("saveStudyCourseItemLastTime")
public void saveStudyCourseItemLastTime() {
// 1. 定义匹配模式匹配所有目标key
final String KEY_PATTERN = "studyId:*:courseId:*:courseContentId:*";
final String KEY_PATTERN = "studyId:*:courseId:*:courseContentId:*:studyItemId:*";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
// 2. 使用SCAN安全遍历避免阻塞
ScanOptions options = ScanOptions.scanOptions()
@@ -128,27 +130,30 @@ public class StudyCourseTask {
try {
// 6. 提取studyContentId
String[] parts = redisKey.split(":");
if (parts.length < 6) continue;
if (parts.length < 7) continue;
String studyId = parts[1];
String courseId = parts[3];
String courseContentId = parts[5];
String studyItemId = parts[7];
// 7. 获取存储的时间点(示例逻辑)
String redisValue = redisTemplate.opsForValue().get(redisKey);
if (redisValue == null) continue;
String[] partValues = redisValue.split("&");
int duration = Integer.parseInt(partValues[0]);
int studyVideoTtime = Integer.parseInt(partValues[0]);
int appendtime = Integer.parseInt(partValues[1]);
LocalDateTime timestamp = null;
if (partValues.length >= 2){
timestamp = LocalDateTime.parse(partValues[1], formatter);
timestamp = LocalDateTime.parse(partValues[2], formatter);
}
// 8. 更新数据库(调用已有服务方法)
studyService.newAppendStudyDuration(studyId,null,courseContentId,duration,timestamp);
studyService.newAppendStudyDuration(studyId,null,courseContentId,appendtime,timestamp);
// 8. 更新数据库(调用已有服务方法)
studyService.updateStudyCourseItemLastTime(studyItemId, studyVideoTtime, timestamp);
List<StudyCourse> allUserList = thirdApi.getStudyCourseList(studyId , courseId, null);
log.info("处理成功 allUserList: {}", allUserList);
// 9. 删除Redis键原子操作
redisTemplate.delete(redisKey);
log.info("处理成功 key: {}, lastStudyTime: {}", redisKey, duration);
log.info("处理成功 key: {}, lastStudyTime: {}", redisKey, appendtime);
} catch (Exception e) {
log.error("处理失败 key: {}", redisKey, e);
}
@@ -163,8 +168,8 @@ public class StudyCourseTask {
}
@XxlJob("saveStudyCourseItemLastTime")
public void saveStudyCourseItemLastTime() {
@XxlJob("saveStudyCourseItemLastTime1")
public void saveStudyCourseItemLastTime1() {
// 定义需要处理的键模式集合
processKeys("studyContentId:*:last_active", this::handleLastActiveKey);
processKeys("studyId:*:courseId:*:courseContentId:*", this::handleDurationKey);

View File

@@ -102,7 +102,7 @@ public interface IStudyService {
void updateStudyCourseItemLastTime(String studyContentId, int lastStudyTime, LocalDateTime timestamp);
void updateStudyDuration(String studyId,String studyItemId, String contentId, Integer duration,String courseId);
void updateStudyDuration(String studyId,String studyItemId, String contentId, Integer videoTime,String courseId);
void newAppendStudyDuration(String studyId, String studyItemId, String courseContentId, int duration, LocalDateTime timestamp);
}

View File

@@ -149,22 +149,22 @@ public class StudyServiceImpl implements IStudyService{
// 更新 前端传输已学习时长
@Override
public void updateStudyDuration(String studyId,String studyItemId,String courseContentId, Integer duration,String courseId) {
public void updateStudyDuration(String studyId,String studyItemId,String courseContentId, Integer videoTime,String courseId) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
String key = "studyId:" + studyId + ":courseId:" + courseId + ":courseContentId:" + courseContentId;
String key = "studyId:" + studyId + ":courseId:" + courseId + ":courseContentId:" + courseContentId + ":studyItemId:" + studyItemId;
String currentValue = redisTemplate.opsForValue().get(key);
int lastDuration = 0;
int sum = duration;
int sum = 10; // 原appendtime改为固定10秒调用一次接口
if (currentValue != null) {
String[] partValues = currentValue.split("&");
lastDuration = Integer.parseInt(partValues[0]);
lastDuration = Integer.parseInt(partValues[1]);
sum += lastDuration;
};
String value = sum + "&" + now.format(formatter); // 使用ISO8601时间格式
String value = videoTime + "&" + sum + "&" + now.format(formatter); // study_video_time & appendtime & time
log.info("-study-video-time-----value = " + value);
// 20250303 优化 多次更新改一次更新