|
|
@@ -10,6 +10,7 @@ import com.cyksj.mapper.UserMapper;
|
|
|
import com.cyksj.mapper.mini.ArticleMapper;
|
|
|
import com.cyksj.mapper.mini.ArticleUserMarkMapper;
|
|
|
import com.cyksj.model.dto.MiniProgramSession;
|
|
|
+import com.cyksj.model.entity.Article;
|
|
|
import com.cyksj.model.entity.ArticleUserMark;
|
|
|
import com.cyksj.model.entity.User;
|
|
|
import com.cyksj.model.request.MiniArticleReq;
|
|
|
@@ -22,6 +23,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@@ -83,6 +85,7 @@ public class MiniServiceImpl implements MiniService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Throwable.class)
|
|
|
public Result<String> markArticle(MiniArticleReq miniArticleReq) {
|
|
|
Long userId = miniArticleReq.getUserId();
|
|
|
Long articleId = miniArticleReq.getId();
|
|
|
@@ -98,6 +101,7 @@ public class MiniServiceImpl implements MiniService {
|
|
|
if (type == 2) mark.setIsCol(true);
|
|
|
try {
|
|
|
articleUserMarkMapper.insert(mark);
|
|
|
+ updateArticleMarkNum(type, articleId, 1);
|
|
|
} catch (DuplicateKeyException e) {
|
|
|
|
|
|
}
|
|
|
@@ -110,8 +114,10 @@ public class MiniServiceImpl implements MiniService {
|
|
|
mark.setIsDz(!isDz);
|
|
|
articleUserMarkMapper.updateById(mark);
|
|
|
if (!isDz) {
|
|
|
+ updateArticleMarkNum(type, articleId, 1);
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult("点赞成功");
|
|
|
}
|
|
|
+ updateArticleMarkNum(type, articleId, -1);
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult("取消成功");
|
|
|
}
|
|
|
if (type == 2) {
|
|
|
@@ -120,8 +126,10 @@ public class MiniServiceImpl implements MiniService {
|
|
|
mark.setIsCol(!isCol);
|
|
|
articleUserMarkMapper.updateById(mark);
|
|
|
if (!isCol) {
|
|
|
+ updateArticleMarkNum(type, articleId, 1);
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult("收藏成功");
|
|
|
}
|
|
|
+ updateArticleMarkNum(type, articleId, -1);
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult("取消成功");
|
|
|
}
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
@@ -144,4 +152,22 @@ public class MiniServiceImpl implements MiniService {
|
|
|
}
|
|
|
return detailView;
|
|
|
}
|
|
|
+
|
|
|
+ public void updateArticleMarkNum(Integer type, Long articleId, Integer offset) {
|
|
|
+ Integer success = 0;
|
|
|
+ while (success != 1) {
|
|
|
+ //文章累计点赞数、收藏数
|
|
|
+ Article article = articleMapper.selectById(articleId);
|
|
|
+ if (article == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Integer dzNum = article.getDzNum();
|
|
|
+ Integer colNum = article.getColNum();
|
|
|
+ if (type == 1) {
|
|
|
+ success = articleMapper.updateDzNum(articleId, dzNum, offset);
|
|
|
+ } else {
|
|
|
+ success = articleMapper.updateColNum(articleId, colNum, offset);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|