提交 4f6eaa0c authored 作者: 吴德鹏's avatar 吴德鹏

评论优化

上级 7fe49d9d
......@@ -7,6 +7,7 @@ import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.example.afrishop_v3.models.*;
import com.example.afrishop_v3.repository.*;
import com.example.afrishop_v3.security.services.AuthenticationUser;
import com.google.common.util.concurrent.AtomicDouble;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -16,6 +17,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static org.springframework.data.domain.Sort.Order.*;
......@@ -239,7 +241,7 @@ public class ItemController {
result.setMessage("The itemId parameter cannot be empty");
return result;
}
Map<String, Object> param = new HashMap();
try {
String userId = user.userId();
......@@ -247,21 +249,28 @@ public class ItemController {
Page<TbCfItemComment> allByItemId = commentRepository.findAllByItemId(itemId, PageRequest.of(pageNum, pageSize, Sort.by(Sort.Order.desc("createTime"))));
List<TbCfItemComment> comments = allByItemId.toList();
AtomicDouble totalScore = new AtomicDouble(0d);
if (comments.size() > 0 && comments != null) {
if (limit != null) {
comments.stream().limit(limit);
comments = comments.stream().limit(limit).collect(Collectors.toList());
}
comments.forEach(c -> {
String liked = (String) redisCache.get(key + userId + "_" + c.getId());
c.setLike(liked == null ? false : true);
if (!StringUtils.isBlank(userId)) {
String liked = (String) redisCache.get(key + userId + "_" + c.getId());
c.setLike(liked == null ? false : true);
}
Integer count = (Integer) redisCache.get(key + c.getId());
c.setLikeCount(count == null ? 0 : count);
totalScore.addAndGet(c.getScore());
});
double v = totalScore.get() / comments.size();
param.put("totalScore", Double.parseDouble(String.format("%.1f", v)));
}
result.setData(comments);
param.put("comments", comments);
result.setData(param);
} catch (Exception e) {
result.setCode(ResultCodeEnum.SERVICE_ERROR.getCode());
result.setMessage(ResultCodeEnum.SERVICE_ERROR.getDesc());
......
......@@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import javax.annotation.Nullable;
import javax.persistence.*;
......@@ -22,6 +23,7 @@ import java.util.List;
*/
@Entity
@Data
@Where(clause = "del_flag=1")
public class TbCfItemComment {
/**
......@@ -107,12 +109,24 @@ public class TbCfItemComment {
private boolean isLike;
// @Formula(value = "(SELECT IFNULL(COUNT(*),0) FROM tb_cf_item_like lk where lk.comment_id = id)")
@Transient
private int likeCount;
@Transient
private double score;
@Transient
private double totalScore;
public double getTotalScore() {
return totalScore;
}
public void setTotalScore(double totalScore) {
this.totalScore = totalScore;
}
public double getScore() {
return (double) (itemScore + serviceScore + logisticsScore + priceScore) / 4;
score = (double) (itemScore + serviceScore + logisticsScore + priceScore) / 4;
return Double.parseDouble(String.format("%.1f", score));
}
public void setScore(double score) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论