forked from fanchaoo/hahu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentService.java
More file actions
129 lines (104 loc) · 4.04 KB
/
CommentService.java
File metadata and controls
129 lines (104 loc) · 4.04 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
package com.fc.service;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.fc.mapper.AnswerMapper;
import com.fc.mapper.CommentMapper;
import com.fc.mapper.MessageMapper;
import com.fc.mapper.QuestionMapper;
import com.fc.mapper.UserMapper;
import com.fc.model.AnswerComment;
import com.fc.model.Message;
import com.fc.model.Question;
import com.fc.model.QuestionComment;
import com.fc.model.User;
import com.fc.util.MyUtil;
import com.fc.util.RedisKey;
@Service
public class CommentService {
@Autowired
private UserMapper userMapper;
@Autowired
private CommentMapper commentMapper;
@Autowired
private MessageMapper messageMapper;
@Autowired
private QuestionMapper questionMapper;
@Autowired
private AnswerMapper answerMapper;
@Autowired
private JedisPool jedisPool;
public QuestionComment commentQuestion(Integer questionId, String commentContent, Integer userId) {
QuestionComment comment = new QuestionComment();
comment.setLikedCount(0);
comment.setCreateTime(new Date().getTime());
comment.setQuestionCommentContent(commentContent);
comment.setQuestionId(questionId);
comment.setUserId(userId);
commentMapper.insertQuestionComment(comment);
User user = userMapper.selectUserInfoByUserId(userId);
comment.setUser(user);
return comment;
}
public QuestionComment replyQuestionComment(QuestionComment comment, Integer userId) {
comment.setLikedCount(0);
comment.setCreateTime(new Date().getTime());
comment.setUserId(userId);
commentMapper.insertQuestionCommentReply(comment);
User user = userMapper.selectUserInfoByUserId(userId);
comment.setUser(user);
return comment;
}
// 评论回答
public AnswerComment commentAnswer(Integer answerId, String commentContent, Integer userId) {
AnswerComment comment = new AnswerComment();
comment.setLikedCount(0);
comment.setCreateTime(new Date().getTime());
comment.setAnswerCommentContent(commentContent);
comment.setAnswerId(answerId);
comment.setUserId(userId);
commentMapper.insertAnswerComment(comment);
User user = userMapper.selectUserInfoByUserId(userId);
comment.setUser(user);
// 插入一条评论消息
Message message = new Message();
message.setType(Message.TYPE_COMMENT);
message.setSecondType(1);
Date date = new Date();
message.setMessageDate(MyUtil.formatDate(date));
message.setMessageTime(date.getTime());
message.setFromUserId(userId);
message.setFromUserName(userMapper.selectUsernameByUserId(userId));
Question question = questionMapper.selectQuestionByAnswerId(answerId);
message.setQuestionId(question.getQuestionId());
message.setQuestionTitle(question.getQuestionTitle());
message.setAnswerId(answerId);
message.setCommentId(comment.getAnswerCommentId());
message.setUserId(answerMapper.selectUserIdByAnswerId(answerId));
messageMapper.insertTypeComment(message);
return comment;
}
public AnswerComment replyAnswerComment(AnswerComment comment, Integer userId) {
comment.setLikedCount(0);
comment.setCreateTime(new Date().getTime());
comment.setUserId(userId);
commentMapper.insertAnswerCommentReply(comment);
User user = userMapper.selectUserInfoByUserId(userId);
comment.setUser(user);
return comment;
}
public void likeQuestionComment(Integer userId, Integer questionCommentId) {
Jedis jedis = jedisPool.getResource();
jedis.zadd(userId + RedisKey.LIKE_QUESTION_COMMENT, new Date().getTime(), String.valueOf(questionCommentId));
jedis.zadd(questionCommentId + RedisKey.LIKED_QUESTION_COMMENT, new Date().getTime(), String.valueOf(userId));
jedisPool.returnResource(jedis);
}
public void likeAnswerComment(Integer userId, Integer answerCommentId) {
Jedis jedis = jedisPool.getResource();
jedis.zadd(userId + RedisKey.LIKE_ANSWER_COMMENT, new Date().getTime(), String.valueOf(answerCommentId));
jedis.zadd(answerCommentId + RedisKey.LIKED_ANSWER_COMMENT, new Date().getTime(), String.valueOf(userId));
jedisPool.returnResource(jedis);
}
}