Commit 2b17e820 authored by 刘基明's avatar 刘基明

热点数据相关

parent 754fa8fe
package com.tanpu.community.api.beans;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class ThemeDTO {
private static final long serialVersionUID = 1L;
......@@ -14,13 +16,13 @@ public class ThemeDTO {
@ApiModelProperty(value = "标题")
private String title;
@ApiModelProperty(value = "类型")
@ApiModelProperty(value = "类型 1:讨论无标题 2:常温有标题")
private Integer themeType;
@ApiModelProperty(value = "文本内容")
private String content;
@ApiModelProperty(value = "附件类型")
@ApiModelProperty(value = "附件类型 1:产品 2:直播 3:短视频 4:课程")
private Integer attachType;
@ApiModelProperty(value = "附件内容")
......@@ -29,152 +31,41 @@ public class ThemeDTO {
@ApiModelProperty(value = "作者id")
private String authorId;
@ApiModelProperty(value = "被转发的主题")
private String formerThemeId;
@ApiModelProperty(value = "所属的话题")
private String topicId;
private String createBy;
@ApiModelProperty(value = "用户名")
private String userName;
private LocalDateTime createTime;
@ApiModelProperty(value = "用户头像")
private String userImg;
private String updateBy;
@ApiModelProperty(value = "认证")
private String authLabel;
private LocalDateTime updateTime;
@ApiModelProperty(value = "发表时间")
private String upToNowTime;
private Integer deleteTag;
@ApiModelProperty(value = "被转发的主题")
private String formerThemeId;
@ApiModelProperty(value = "转发量")
private Long forwardAomunt;
@ApiModelProperty(value = "评论量")
private Long commentAmount;
public String getId() {
return id;
}
@ApiModelProperty(value = "点赞量")
private Long likeAmount;
private String createBy;
public void setId(String id) {
this.id = id;
}
private LocalDateTime createTime;
public String getTitle() {
return title;
}
private String updateBy;
public void setTitle(String title) {
this.title = title;
}
private LocalDateTime updateTime;
public Integer getThemeType() {
return themeType;
}
private Integer deleteTag;
public void setThemeType(Integer themeType) {
this.themeType = themeType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getAttachType() {
return attachType;
}
public void setAttachType(Integer attachType) {
this.attachType = attachType;
}
public String getAttachment() {
return attachment;
}
public void setAttachment(String attachment) {
this.attachment = attachment;
}
public String getAuthorId() {
return authorId;
}
public void setAuthorId(String authorId) {
this.authorId = authorId;
}
public String getFormerThemeId() {
return formerThemeId;
}
public void setFormerThemeId(String formerThemeId) {
this.formerThemeId = formerThemeId;
}
public String getTopicId() {
return topicId;
}
public void setTopicId(String topicId) {
this.topicId = topicId;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
public Integer getDeleteTag() {
return deleteTag;
}
public void setDeleteTag(Integer deleteTag) {
this.deleteTag = deleteTag;
}
@Override
public String toString() {
return "ThemeEntity{" +
"id=" + id +
", title=" + title +
", themeType=" + themeType +
", content=" + content +
", attachType=" + attachType +
", attachment=" + attachment +
", authorId=" + authorId +
", formerThemeId=" + formerThemeId +
", topicId=" + topicId +
", createBy=" + createBy +
", createTime=" + createTime +
", updateBy=" + updateBy +
", updateTime=" + updateTime +
", deleteTag=" + deleteTag +
"}";
}
}
package com.tanpu.community.api.constants;
public class RedisKeyConstant {
public static final String TOPIC_VIEW_AMOUNT_="TOPIC_VIEW_AMOUNT_";
public static final String TOPIC_LIKE_AMOUNT_="TOPIC_LIKE_AMOUNT_";
public static final String TOPIC_BOOK_AMOUNT_="TOPIC_BOOK_AMOUNT_";
public static final String TOPIC_COMMENT_AMOUNT_="TOPIC_COMMENT_AMOUNT_";
public static final String TOPIC_USER_AMOUNT_="TOPIC_USER_AMOUNT_";
public static final String THEME_VIEW_AMOUNT_="THEME_VIEW_AMOUNT_";
public static final String THEME_LIKE_AMOUNT_="THEME_LIKE_AMOUNT_";
public static final String THEME_BOOK_AMOUNT_="THEME_BOOK_AMOUNT_";
}
package com.tanpu.community.controller;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.service.RedisService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -14,9 +15,10 @@ public class MetricsController {
@ApiOperation("浏览量埋点")
@RequestMapping("/pv")
public void pageView(@RequestParam String themeId){
redisService.incr("pv"+themeId, 1L);
redisService.incr(RedisKeyConstant.THEME_VIEW_AMOUNT_ +themeId, 1L);
}
}
......@@ -2,17 +2,12 @@ package com.tanpu.community.controller;
import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.controller.convert.ThemeConvert;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.manager.ThemeManager;
import com.tanpu.community.service.FansRelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -28,7 +23,7 @@ public class ThemeController {
private FansRelService fansRelService;
@ApiOperation("新增主题")
@RequestMapping(value = "/add")
@PostMapping(value = "/add")
@ResponseBody
public String insertTheme(@RequestBody ThemeDTO themeDTO){
String userId="liujm";
......@@ -37,17 +32,17 @@ public class ThemeController {
return "success";
}
@ApiOperation("系统推荐主题列表")
@ApiOperation("获取推荐的圈文")
@RequestMapping(value = "/recmend_list")
@ResponseBody
public List<ThemeEntity> selectHotList(){
public List<ThemeDTO> selectHotList(){
return null;
}
@ApiOperation("用户关注主题列表")
@ApiOperation("获取关注的圈文")
@RequestMapping(value = "/interest_list")
@ResponseBody
public List<ThemeEntity> selectInterestList(){
public List<ThemeDTO> selectInterestList(){
String userId="liujm012";
return null;
}
......
......@@ -23,7 +23,7 @@ public class TopicController {
@Autowired
private TopicManager topicManager;
@PostMapping(value = "/queryTopicDetailList")
@PostMapping(value = "/admin/queryTopicDetailList")
@ApiOperation("查询所有的主题详情")
@ResponseBody
public List<TopicDTO> getAllTopicList(@RequestBody String content){
......@@ -34,7 +34,7 @@ public class TopicController {
return topicDTOS;
}
@PostMapping(value = "/queryTopicNameList")
@PostMapping(value = "/admin/queryTopicNameList")
@ApiOperation("查询所有的主题名称列表")
@ResponseBody
public List<String> getTopicNameList(@RequestBody String content){
......@@ -45,7 +45,7 @@ public class TopicController {
return topictitiles;
}
@PostMapping(value="/insertTopic")
@PostMapping(value="/admin/insertTopic")
@ApiOperation("新增主题主题")
@ResponseBody
public String addTopic(@RequestParam String topicTitle){
......@@ -56,7 +56,7 @@ public class TopicController {
}
@ApiOperation("单个话题数据总览")
@PostMapping("/selectOne")
@PostMapping("/admin/selectOne")
@ResponseBody
public String selectOne(@RequestParam String topicId) throws MissingServletRequestParameterException {
if (StringUtils.isEmpty(topicId)){
......@@ -66,7 +66,7 @@ public class TopicController {
return "success";
}
@PostMapping(value = "/setTop")
@PostMapping(value = "/admin/setTop")
@ApiOperation("顶置/取消顶置主题")
@ResponseBody
public String setTopTopic(@RequestParam String topicId,@RequestParam boolean setTop) throws MissingServletRequestParameterException {
......@@ -77,7 +77,7 @@ public class TopicController {
return "success";
}
@PostMapping(value = "/setConceal")
@PostMapping(value = "/admin/setConceal")
@ApiOperation("隐藏/显示主题")
@ResponseBody
public String setConceal(@RequestParam String topicId,@RequestParam boolean setConceal) throws MissingServletRequestParameterException {
......@@ -88,7 +88,7 @@ public class TopicController {
return "success";
}
@PostMapping(value = "/modifyViewNum")
@PostMapping(value = "/admin/modifyViewNum")
@ApiOperation("主题浏览数调整")
@ResponseBody
public String modifyViewNum(@RequestParam String topicId,@RequestParam Long modifyMount) throws MissingServletRequestParameterException {
......@@ -96,6 +96,7 @@ public class TopicController {
throw new MissingServletRequestParameterException("topicId","String");
}
//修改浏览量
topicManager.modifyPV(topicId,modifyMount);
return "success";
}
......
......@@ -12,6 +12,8 @@ public class ThemeConvert {
ThemeDTO themeDTO = new ThemeDTO();
BeanUtils.copyProperties(themeEntity, themeDTO);
themeDTO.setId(themeEntity.getId());
themeDTO.setTitle(themeEntity.getTitle());
themeDTO.setTopicId(themeEntity.getTopicId());
themeDTO.setContent(themeEntity.getContent());
themeDTO.setThemeType(themeEntity.getThemeType());
themeDTO.setAttachment(themeEntity.getAttachment());
......@@ -28,6 +30,8 @@ public class ThemeConvert {
public static ThemeEntity convertToEntity(ThemeDTO themeDTO){
ThemeEntity themeEntity = new ThemeEntity();
themeEntity.setTitle(themeDTO.getTitle());
themeEntity.setTopicId(themeDTO.getTopicId());
themeEntity.setContent(themeDTO.getContent());
themeEntity.setThemeType(themeDTO.getThemeType());
themeEntity.setAttachment(themeDTO.getAttachment());
......
package com.tanpu.community.manager;
import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.api.constants.BlockTypeEnum;
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.api.constants.CommentTypeEnum;
import com.tanpu.community.dao.entity.community.*;
import com.tanpu.community.controller.convert.ThemeConvert;
import com.tanpu.community.dao.entity.community.BlackListEntity;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.FansRelEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
......@@ -29,17 +37,48 @@ public class ThemeManager {
@Autowired
private BlackListService blackListService;
@Autowired
private RedisService redisService;
//返回推荐主题文章
public List<ThemeEntity> selectHotThemes(){
public List<ThemeDTO> selectHotThemes(){
//TODO:根据算法计算推荐主题
return null;
List<ThemeEntity> themeEntities = themeService.selectAll();
List<ThemeDTO> themeDTOS = ThemeConvert.convertToDTOs(themeEntities);
for (ThemeDTO themeDTO : themeDTOS) {
themeDTO.setUpToNowTime(calUpToNowTime(themeDTO.getCreateTime()));
//TODO 添加用户名、头像、认证
}
return themeDTOS;
}
//返回关注主题
public List<ThemeEntity> selectInterestThemes(String userId){
public List<ThemeDTO> selectInterestThemes(String userId){
List<String> fansList = fansRelService.queryFansByFollowerId(userId).stream().map(FansRelEntity::getIdolId).collect(Collectors.toList());
List<ThemeEntity> result = themeService.selectByFans(fansList);
return null;
List<ThemeEntity> themeEntities = themeService.selectByFans(fansList);
List<ThemeDTO> themeDTOS = ThemeConvert.convertToDTOs(themeEntities);
for (ThemeDTO themeDTO : themeDTOS) {
themeDTO.setUpToNowTime(calUpToNowTime(themeDTO.getCreateTime()));
//TODO 添加用户名、头像、认证
}
return themeDTOS;
}
private String calUpToNowTime(LocalDateTime start) {
Duration between = Duration.between(LocalDateTime.now(), start);
long duration = between.toMinutes();
if (duration<1){
return "刚刚";
}else if (duration<60){
return duration+"分钟前";
}else if (duration<60*24){
return duration/60+"小时前";
}else if (start.getYear()==LocalDateTime.now().getYear()){
return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm:ss"));
}
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
//点赞
......@@ -116,4 +155,8 @@ public class ThemeManager {
// }
// }
public void insert(ThemeEntity themeEntity) {
themeService.insertTheme(themeEntity);
}
}
......@@ -2,14 +2,17 @@ package com.tanpu.community.manager;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.TopicDTO;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.api.constants.TopicStatusEnum;
import com.tanpu.community.controller.convert.TopicConverter;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.service.TopicService;
import com.tanpu.community.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class TopicManager {
......@@ -17,15 +20,25 @@ public class TopicManager {
@Autowired
private TopicService topicService;
@Autowired
private RedisService redisService;
@Autowired
private ThemeService themeService;
@Autowired
private CollectionService collectionService;
@Autowired
private CommentService commentService;
public void insertTopic(String topicTitle,String userId){
TopicEntity topicEntity = new TopicEntity();
topicEntity.setCreateBy(userId);
topicEntity.setUpdateBy(userId);
topicEntity.setTopicTitle(topicTitle);
topicEntity.setCreateTime(LocalDateTime.now());
topicEntity.setUpdateTime(LocalDateTime.now());
topicEntity.setIsTop(0);
topicEntity.setIsConceal(0);
topicEntity.setIsTop(TopicStatusEnum.FALSE.getCode());
topicEntity.setIsConceal(TopicStatusEnum.FALSE.getCode());
topicService.addTopic(topicEntity);
}
......@@ -61,7 +74,36 @@ public class TopicManager {
public TopicDTO getDetail(String topicId) {
TopicEntity topicEntity = topicService.queryById(topicId);
TopicDTO topicDTO = TopicConverter.convertToDTO(topicEntity);
//TODO:添加实时数据
//TODO:添加实时数据(浏览量,点赞量,评论量,收藏量)
topicDTO.setViewAmount(redisService.getLong(RedisKeyConstant.TOPIC_VIEW_AMOUNT_+topicId));
topicDTO.setLikeAmount(redisService.getLong(RedisKeyConstant.TOPIC_LIKE_AMOUNT_+topicId));
topicDTO.setUserAmount(redisService.getLong(RedisKeyConstant.TOPIC_USER_AMOUNT_+topicId));
return topicDTO;
}
public void modifyPV(String topicId, Long modifyMount) {
if (modifyMount>0){
redisService.incr(RedisKeyConstant.TOPIC_VIEW_AMOUNT_+topicId,modifyMount);
}else {
redisService.decr(RedisKeyConstant.TOPIC_VIEW_AMOUNT_+topicId,-modifyMount);
}
}
public void calculate(){
List<String> topicIds = topicService.queryTopicList().stream().map(TopicEntity::getId).collect(Collectors.toList());
for (String topicId : topicIds) {
List<ThemeEntity> themeEntities = themeService.selectByTopic(topicId);
List<String> themeIds = themeEntities.stream().map(ThemeEntity::getId).collect(Collectors.toList());
Long likeAmountByThemeIds = collectionService.getLikeAmountByThemeIds(themeIds);
Long bookAmountByThemeIds = collectionService.getBookAmountByThemeIds(themeIds);
Long commentAmountByThemeIds = commentService.getCommentAmountByThemeIds(themeIds);
redisService.set(RedisKeyConstant.TOPIC_LIKE_AMOUNT_+topicId, likeAmountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_BOOK_AMOUNT_+topicId, bookAmountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_COMMENT_AMOUNT_+topicId, commentAmountByThemeIds);
}
}
}
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
......@@ -30,12 +27,46 @@ public class CollectionService {
//根据用户id获取点赞列表
public List<CollectionEntity> getLikeListByUser(String userId){
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>().eq(CollectionEntity::getCollectionType,1));
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()));
}
//根据用户id获取收藏列表
public List<CollectionEntity> getBookListByUser(String userId){
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>().eq(CollectionEntity::getCollectionType,2));
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType,CollectionTypeEnum.BOOK.getCode()));
}
//统计主题的点赞量
public Long getLikeAmountByThemeId(String themeId){
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getTargetId, themeId)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())))
.size();
}
//统计主题集合的点赞量
public Long getLikeAmountByThemeIds(List<String> themeIds){
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.in(CollectionEntity::getTargetId, themeIds)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())))
.size();
}
//统计主题的收藏量
public Long getBookAmountByThemeId(String themeId){
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getTargetId, themeId)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())))
.size();
}
//统计主题集合的收藏量
public Long getBookAmountByThemeIds(List<String> themeIds){
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.in(CollectionEntity::getTargetId, themeIds)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())))
.size();
}
}
......@@ -24,4 +24,11 @@ public class CommentService {
public List<CommentEntity> selectByUserId(String userId){
return commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getAuthorId,userId));
}
//统计主题集合的评论量
public Long getCommentAmountByThemeIds(List<String> themeIds){
return (long) commentMapper.selectList((new LambdaQueryWrapper<CommentEntity>()
.in(CommentEntity::getTargetId, themeIds)))
.size();
}
}
......@@ -46,6 +46,11 @@ public class RedisService {
return v == null ? null : (Integer) v;
}
public Long getLong(String key) {
Object v = redisTemplate.opsForValue().get(key);
return v == null ? null : (Long) v;
}
/**
* incr
*/
......
......@@ -25,10 +25,20 @@ public class ThemeService {
//根据话题查询主题
public List<ThemeEntity> selectByTopic(String topidId){
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>().eq(ThemeEntity::getTopicId,topidId));
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId,topidId));
}
//查询列表中用户的主题
public List<ThemeEntity> selectByFans(List<String> fansList) {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>().in(ThemeEntity::getAuthorId,fansList));
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getAuthorId,fansList)
.orderByDesc(ThemeEntity::getUpdateTime));
}
//查询列表中用户的主题
public List<ThemeEntity> selectAll() {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.orderByDesc(ThemeEntity::getUpdateTime));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment