1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.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;
@Service
public class ThemeManager {
@Resource
private ThemeService themeService;
@Autowired
private CollectionService collectionService;
@Autowired
private CommentService commentService;
@Autowired
private FansRelService fansRelService;
@Autowired
private BlackListService blackListService;
@Autowired
private RedisService redisService;
//返回推荐主题文章
public List<ThemeDTO> selectHotThemes(){
//TODO:根据算法计算推荐主题
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<ThemeDTO> selectInterestThemes(String userId){
List<String> fansList = fansRelService.queryFansByFollowerId(userId).stream().map(FansRelEntity::getIdolId).collect(Collectors.toList());
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"));
}
//点赞
public void like(String themeId, String userId) {
collectionService.addCollection(themeId, userId, CollectionTypeEnum.LIKE);
}
public void unlike(String themeId, String userId) {
}
// 评论(对主题)
public void commentToTheme(String themeId, String userId, String comment) {
CommentEntity commentEntity = new CommentEntity();
commentEntity.setTargetId(themeId);
commentEntity.setAuthorId(userId);
commentEntity.setCommentType(CommentTypeEnum.THEME.getCode());
commentEntity.setContent(comment);
commentService.insertComment(commentEntity);
}
//转发
public void forward(String themeId, String user) {
//TODO
}
//分享
public void share(String themeId, String user) {
//TODO
}
//收藏
public void favorite(String themeId, String userId) {
collectionService.addCollection(themeId, userId, CollectionTypeEnum.BOOK);
}
public void unFavorite(String themeId, String userId) {
}
//投诉(主题)
public void complaint(String themeId, String user) {
//TODO
}
//屏蔽(用户)
public void blockUser(String themeId, String userId) {
String blockId = themeService.selectTheme(themeId).getAuthorId();
blackListService.addBlock(blockId, userId, BlockTypeEnum.USER);
}
// 解除屏蔽(用户)
public void unblockUser(String themeId, String userId) {
}
// 屏蔽(主题)
public void blockTheme(String themeId, String userId) {
blackListService.addBlock(themeId, userId, BlockTypeEnum.THEME);
BlackListEntity selectOne = blackListService.selectOne(userId, BlockTypeEnum.USER.getCode(), themeService.selectTheme(themeId).getAuthorId());
if (selectOne==null){
blackListService.addBlock(themeService.selectTheme(themeId).getAuthorId(), userId, BlockTypeEnum.USER);
}
}
// 解除屏蔽(主题)
public void unblockTheme(String themeId, String userId) {
blackListService.removeBlackList(themeId, userId, BlockTypeEnum.USER);
}
//屏蔽(内容)
// public void blockContent(String themeId, String userId) {
// BlackListEntity selectOne = blackListService.selectOne(userId, BlockTypeEnum.USER.getCode(), themeId);
// if (selectOne==null){
// blackListService.insertBlackList(userId, BlockTypeEnum.USER.getCode(), themeId);
// }
// }
public void insert(ThemeEntity themeEntity) {
themeService.insertTheme(themeEntity);
}
}