forked from fanchaoo/hahu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionService.java
More file actions
276 lines (239 loc) · 9.88 KB
/
QuestionService.java
File metadata and controls
276 lines (239 loc) · 9.88 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.fc.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.alibaba.fastjson.JSON;
import com.fc.mapper.AnswerMapper;
import com.fc.mapper.CommentMapper;
import com.fc.mapper.QuestionMapper;
import com.fc.mapper.TopicMapper;
import com.fc.mapper.UserMapper;
import com.fc.model.Answer;
import com.fc.model.AnswerComment;
import com.fc.model.PageBean;
import com.fc.model.Question;
import com.fc.model.QuestionComment;
import com.fc.model.Topic;
import com.fc.model.User;
import com.fc.util.MyUtil;
import com.fc.util.RedisKey;
@Service
public class QuestionService {
@Autowired
private QuestionMapper questionMapper;
@Autowired
private AnswerMapper answerMapper;
@Autowired
private TopicMapper topicMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private CommentMapper commentMapper;
@Autowired
private JedisPool jedisPool;
public Integer ask(Question question, String topicNameString, Integer userId) {
String[] topicNames = topicNameString.split(",");
System.out.println(Arrays.toString(topicNames));
Map<Integer, String> map = new HashMap<>();
List<Integer> topicIdList = new ArrayList<>();
for (String topicName : topicNames) {
Topic topic = new Topic();
Integer topicId = topicMapper.selectTopicIdByTopicName(topicName);
if (topicId == null) {
topic.setTopicName(topicName);
topic.setParentTopicId(1);
topicMapper.insertTopic(topic);
topicId = topic.getTopicId();
}
map.put(topicId, topicName);
topicIdList.add(topicId);
}
String topicKvList = JSON.toJSONString(map);
question.setTopicKvList(topicKvList);
question.setCreateTime(new Date().getTime());
question.setUserId(userId);
questionMapper.insertQuestion(question);
// 向关联表插入数据
for (Integer topicId : topicIdList) {
questionMapper.insertIntoQuestionTopic(question.getQuestionId(), topicId);
}
return question.getQuestionId();
}
// 获得问题页详情
@SuppressWarnings("unchecked")
public Map<String, Object> getQuestionDetail(Integer questionId, Integer userId) {
Map<String, Object> map = new HashMap<>();
// 获取问题信息
Question question = questionMapper.selectQuestionByQuestionId(questionId);
if (question == null) {
throw new RuntimeException("该问题id不存在~");
}
// 获取该问题被浏览次数
Jedis jedis = jedisPool.getResource();
jedis.zincrby(RedisKey.QUESTION_SCANED_COUNT, 1, questionId + "");
question.setScanedCount((int) jedis.zscore(RedisKey.QUESTION_SCANED_COUNT, questionId + "").doubleValue());
// 获取该问题被关注人数
Long followedCount = jedis.zcard(questionId + RedisKey.FOLLOWED_QUESTION);
question.setFollowedCount(Integer.parseInt(followedCount + ""));
// 获取10个关注该问题的人
Set<String> userIdSet = jedis.zrange(questionId + RedisKey.FOLLOWED_QUESTION, 0, 9);
List<Integer> userIdList = MyUtil.StringSetToIntegerList(userIdSet);
List<User> followedUserList = new ArrayList<>();
if (userIdList.size() > 0) {
followedUserList = userMapper.listUserInfoByUserId(userIdList);
}
// 获取5个该话题下的问题
List<Question> relatedQuestionList = questionMapper.listRelatedQuestion(questionId);
System.out.println("relatedQuestionList:" + relatedQuestionList);
// 获取提问用户信息
User askUser = userMapper.selectUserInfoByUserId(question.getUserId());
question.setUser(askUser);
// 获取问题评论列表
List<QuestionComment> questionCommentList = commentMapper.listQuestionCommentByQuestionId(questionId);
// 为每条问题评论绑定用户信息
for (QuestionComment comment : questionCommentList) {
User commentUser = userMapper.selectUserInfoByUserId(comment.getUserId());
comment.setUser(commentUser);
// 判断用户是否赞过该评论
Long rank = jedis.zrank(userId + RedisKey.LIKE_QUESTION_COMMENT, comment.getQuestionCommentId() + "");
comment.setLikeState(rank == null ? "false" : "true");
// 获取该评论被点赞次数
Long likedCount = jedis.zcard(comment.getQuestionCommentId() + RedisKey.LIKED_QUESTION_COMMENT);
comment.setLikedCount(Integer.valueOf(likedCount + ""));
}
question.setQuestionCommentList(questionCommentList);
// 获取答案列表信息
List<Answer> answerList = answerMapper.selectAnswerByQuestionId(questionId);
for (Answer answer : answerList) {
User answerUser = userMapper.selectUserInfoByUserId(answer.getUserId());
answer.setUser(answerUser);
// 获取答案评论列表
List<AnswerComment> answerCommentList = commentMapper.listAnswerCommentByAnswerId(answer.getAnswerId());
for (AnswerComment comment : answerCommentList) {
// 为评论绑定用户信息
User commentUser = userMapper.selectUserInfoByUserId(comment.getUserId());
comment.setUser(commentUser);
// 判断用户是否赞过该评论
Long rank = jedis.zrank(userId + RedisKey.LIKE_ANSWER_COMMENT, comment.getAnswerCommentId() + "");
comment.setLikeState(rank == null ? "false" : "true");
// 获取该评论被点赞次数
Long likedCount = jedis.zcard(comment.getAnswerCommentId() + RedisKey.LIKED_ANSWER_COMMENT);
comment.setLikedCount(Integer.valueOf(likedCount + ""));
}
answer.setAnswerCommentList(answerCommentList);
// 获取用户点赞状态
Long rank = jedis.zrank(answer.getAnswerId() + RedisKey.LIKED_ANSWER, String.valueOf(userId));
answer.setLikeState(rank == null ? "false" : "true");
// 获取该回答被点赞次数
Long likedCount = jedis.zcard(answer.getAnswerId() + RedisKey.LIKED_ANSWER);
answer.setLikedCount(Integer.valueOf(likedCount + ""));
}
// 获取话题信息
Map<Integer, String> topicMap = (Map<Integer, String>) JSON.parse(question.getTopicKvList());
map.put("topicMap", topicMap);
map.put("question", question);
map.put("answerList", answerList);
map.put("followedUserList", followedUserList);
map.put("relatedQuestionList", relatedQuestionList);
jedisPool.returnResource(jedis);
return map;
}
public PageBean<Question> listQuestionByUserId(Integer userId, Integer curPage) {
// 当请求页数为空时
curPage = curPage == null ? 1 : curPage;
// 每页记录数,从哪开始
int limit = 8;
int offset = (curPage - 1) * limit;
// 获得总记录数,总页数
int allCount = questionMapper.selectQuestionCountByUserId(userId);
int allPage = 0;
if (allCount <= limit) {
allPage = 1;
} else if (allCount / limit == 0) {
allPage = allCount / limit;
} else {
allPage = allCount / limit + 1;
}
// 构造查询map
Map<String, Object> map = new HashMap<>();
map.put("offset", offset);
map.put("limit", limit);
map.put("userId", userId);
// 得到某页数据列表
List<Question> questionList = questionMapper.listQuestionByUserId(map);
// 构造PageBean
PageBean<Question> pageBean = new PageBean<>(allPage, curPage);
pageBean.setList(questionList);
return pageBean;
}
public List<Question> listQuestionByPage(Integer curPage) {
// 当请求页数为空时
curPage = curPage == null ? 1 : curPage;
// 每页记录数,从哪开始
int limit = 3;
int offset = (curPage - 1) * limit;
Jedis jedis = jedisPool.getResource();
Set<String> idSet = jedis.zrange(RedisKey.QUESTION_SCANED_COUNT, offset, offset + limit - 1);
List<Integer> idList = MyUtil.StringSetToIntegerList(idSet);
System.out.println(idList);
List<Question> questionList = new ArrayList<Question>();
if (idList.size() > 0) {
questionList = questionMapper.listQuestionByQuestionId(idList);
for (Question question : questionList) {
question.setAnswerCount(answerMapper.selectAnswerCountByQuestionId(question.getQuestionId()));
question.setFollowedCount(Integer.parseInt(jedis.zcard(question.getQuestionId() + RedisKey.FOLLOWED_QUESTION) + ""));
}
}
jedisPool.returnResource(jedis);
return questionList;
}
// 判断某人是否关注了某问题
public boolean judgePeopleFollowQuestion(Integer userId, Integer questionId) {
Jedis jedis = jedisPool.getResource();
Long rank = jedis.zrank(userId + RedisKey.FOLLOW_QUESTION, String.valueOf(questionId));
jedisPool.returnResource(jedis);
return rank == null ? false : true;
}
// 关注收藏夹
public void followQuestion(Integer userId, Integer questionId) {
Jedis jedis = jedisPool.getResource();
jedis.zadd(userId + RedisKey.FOLLOW_QUESTION, new Date().getTime(), String.valueOf(questionId));
jedis.zadd(questionId + RedisKey.FOLLOWED_QUESTION, new Date().getTime(), String.valueOf(userId));
jedisPool.returnResource(jedis);
}
// 取消关注收藏夹
public void unfollowQuestion(Integer userId, Integer questionId) {
Jedis jedis = jedisPool.getResource();
jedis.zrem(userId + RedisKey.FOLLOW_QUESTION, String.valueOf(questionId));
jedis.zrem(questionId + RedisKey.FOLLOWED_QUESTION, String.valueOf(userId));
jedisPool.returnResource(jedis);
}
// 列出所关注的问题
public List<Question> listFollowingQuestion(Integer userId) {
Jedis jedis = jedisPool.getResource();
// 获取所关注的问题的id集合
Set<String> idSet = jedis.zrange(userId + RedisKey.FOLLOW_QUESTION, 0, -1);
List<Integer> idList = MyUtil.StringSetToIntegerList(idSet);
List<Question> list = new ArrayList<Question>();
if (idList.size() > 0) {
list = questionMapper.listQuestionByQuestionId(idList);
for (Question question : list) {
// 设置回答数目
int answerCount = answerMapper.selectAnswerCountByQuestionId(question.getQuestionId());
question.setAnswerCount(answerCount);
Long followedCount = jedis.zcard(question.getQuestionId() + RedisKey.FOLLOWED_QUESTION);
question.setFollowedCount(Integer.parseInt(followedCount + ""));
}
}
jedisPool.returnResource(jedis);
return list;
}
}