增加周播放量
This commit is contained in:
parent
732ee1df4b
commit
5e4aaf78ce
|
|
@ -1,6 +1,7 @@
|
|||
package com.sqx.common.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -114,8 +115,13 @@ public class ApiAccessLimitUtil {
|
|||
String redisKey = generateRedisKey(key, id);
|
||||
Object countObj = redisUtils.get(redisKey);
|
||||
if (countObj == null) {
|
||||
// 根据不同时间周期设置过期时间并初始化访问次数为1
|
||||
long expireAt = calculateExpireAt(timeFormat);
|
||||
long expireAt;
|
||||
if (StrUtil.isBlank(timeFormat)) {
|
||||
expireAt = count;
|
||||
}else {
|
||||
// 根据不同时间周期设置过期时间并初始化访问次数为1
|
||||
expireAt = calculateExpireAt(timeFormat);
|
||||
}
|
||||
redisUtils.set(redisKey, 1, expireAt);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,6 @@ public class AppCourseController extends AbstractController {
|
|||
if (week != null) {
|
||||
return Result.success().put("data", week);
|
||||
}
|
||||
// courseService.asyncIncrView(courseDetailsId);
|
||||
Result result = courseDetailsService.selectCourseDetailsById(id, token, courseDetailsId);
|
||||
if (result.get("code").equals(0)) {
|
||||
redisUtils.set(redisKey, result.get("data"), 3);
|
||||
|
|
|
|||
|
|
@ -332,6 +332,7 @@ public class Course implements Serializable {
|
|||
|
||||
@TableField(exist = false)
|
||||
private Integer isCollect;
|
||||
private Integer weekView;
|
||||
|
||||
public Course() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.sqx.common.exception.SqxException;
|
||||
import com.sqx.common.utils.ApiAccessLimitUtil;
|
||||
import com.sqx.common.utils.DateUtils;
|
||||
import com.sqx.common.utils.PageUtils;
|
||||
import com.sqx.common.utils.Result;
|
||||
|
|
@ -130,6 +131,14 @@ public class CourseDetailsServiceImpl extends ServiceImpl<CourseDetailsDao, Cour
|
|||
|
||||
}
|
||||
|
||||
private void setCourseView(Course course) {
|
||||
if (ApiAccessLimitUtil.isAccessAllowed("setCourseView:" + course.getCourseId(), "updateAuthCertInfo", 1, 600)) {
|
||||
int courseViewCount = redisServiceImpl.getCourseWeekViewCount(course.getCourseId());
|
||||
course.setWeekView(courseViewCount);
|
||||
courseDao.updateById(course);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result selectCourseDetailsById(Long id, String token, String courseDetailsId) {
|
||||
Course bean = courseDao.selectById(id);
|
||||
|
|
@ -139,6 +148,9 @@ public class CourseDetailsServiceImpl extends ServiceImpl<CourseDetailsDao, Cour
|
|||
bean.setViewCounts(bean.getViewCounts() + 1);
|
||||
}
|
||||
courseDao.updateById(bean);
|
||||
|
||||
setCourseView(bean);
|
||||
|
||||
Long userId = null;
|
||||
if (StringUtils.isNotEmpty(token)) {
|
||||
Claims claims = jwtUtils.getClaimByToken(token);
|
||||
|
|
|
|||
|
|
@ -21,4 +21,6 @@ public interface RedisService {
|
|||
boolean isRecordUserOnLineTime(long userId);
|
||||
|
||||
boolean isSetUserState(long userId);
|
||||
|
||||
int getCourseWeekViewCount(long courseId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -291,4 +291,23 @@ public class RedisServiceImpl implements RedisService {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCourseWeekViewCount(long courseId) {
|
||||
String key = "course:viewCount:" + courseId;
|
||||
String info = redisUtils.get(key);
|
||||
if (StrUtil.isBlank(info)) {
|
||||
// 获取当前时间
|
||||
DateTime now = DateUtil.date();
|
||||
// 获取下周一的日期
|
||||
DateTime nextMonday = DateUtil.nextWeek();
|
||||
// 计算距离下周一还有多少秒
|
||||
long seconds = nextMonday.getTime() - now.getTime();
|
||||
redisUtils.set(key, 1, seconds);
|
||||
return 1;
|
||||
}
|
||||
int i = Integer.parseInt(info) + 1;
|
||||
redisUtils.set(key, i, -1);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue