|
|
@@ -1,16 +1,25 @@
|
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.his.enums.FsUserIntegralLogTypeEnum;
|
|
|
import com.fs.his.param.FsArticleListUParam;
|
|
|
+import com.fs.his.param.FsUserAddIntegralTemplateParam;
|
|
|
+import com.fs.his.service.IFsUserIntegralLogsService;
|
|
|
import com.fs.his.vo.FsArticleListUVO;
|
|
|
import com.fs.his.vo.FsArticleListVO;
|
|
|
-import com.fs.his.vo.FsArticleVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.his.mapper.FsArticleMapper;
|
|
|
import com.fs.his.domain.FsArticle;
|
|
|
import com.fs.his.service.IFsArticleService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
/**
|
|
|
* 文章Service业务层处理
|
|
|
@@ -23,6 +32,10 @@ public class FsArticleServiceImpl implements IFsArticleService
|
|
|
{
|
|
|
@Autowired
|
|
|
private FsArticleMapper fsArticleMapper;
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+ @Autowired
|
|
|
+ private IFsUserIntegralLogsService userIntegralLogsService;
|
|
|
|
|
|
/**
|
|
|
* 查询文章
|
|
|
@@ -112,4 +125,48 @@ public class FsArticleServiceImpl implements IFsArticleService
|
|
|
public List<FsArticleListVO> selectFsArticleListVO(FsArticle fsArticle) {
|
|
|
return fsArticleMapper.selectFsArticleListVO(fsArticle);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R finishView(Long userId, Long articleId) {
|
|
|
+ //1.添加积分
|
|
|
+ // 先检查今天是否已领取
|
|
|
+ String today = DateUtils.getDate(); // 格式:2026-03-02
|
|
|
+ String finishViewKey = String.format("article:integral:%s:%s:%s", userId, articleId, today);
|
|
|
+ boolean success = redisCache.setIfAbsent(finishViewKey, "1", getSecondsUntilMidnight(), TimeUnit.SECONDS);
|
|
|
+ // 检查今天是否已领取
|
|
|
+ if (!success) {
|
|
|
+ return R.error("今日已领取该文章积分,明天再来吧");
|
|
|
+ }
|
|
|
+ FsUserAddIntegralTemplateParam param = new FsUserAddIntegralTemplateParam();
|
|
|
+ param.setUserId(userId);
|
|
|
+ param.setLogType(FsUserIntegralLogTypeEnum.TYPE_30.getValue());
|
|
|
+ param.setBusinessId(articleId.toString());
|
|
|
+ try {
|
|
|
+ R r = userIntegralLogsService.addIntegralTemplate(param);
|
|
|
+ if (!"200".equals(r.get("code").toString())) {
|
|
|
+ redisCache.deleteObject(finishViewKey);
|
|
|
+ return R.error("阅读文章领取积分失败:" + r.get("msg"));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 异常时删除缓存,允许用户重试
|
|
|
+ redisCache.deleteObject(finishViewKey);
|
|
|
+ return R.error("阅读文章领取积分失败");
|
|
|
+ }
|
|
|
+ return R.ok("今日已领取奖励!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R queryFinishViewStatus(Long userId, Long articleId) {
|
|
|
+ String today = DateUtils.getDate(); // 格式:2026-03-02
|
|
|
+ String finishViewKey = String.format("article:integral:%s:%s:%s", userId, articleId, today);
|
|
|
+ boolean isView = redisCache.hasKey(finishViewKey);
|
|
|
+ return R.ok().put("isView", isView ? 1 : 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private long getSecondsUntilMidnight() {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ LocalDateTime midnight = now.toLocalDate().plusDays(1).atStartOfDay();
|
|
|
+ return ChronoUnit.SECONDS.between(now, midnight);
|
|
|
+ }
|
|
|
}
|