forked from fanchaoo/hahu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionService.java
More file actions
162 lines (136 loc) · 5.54 KB
/
CollectionService.java
File metadata and controls
162 lines (136 loc) · 5.54 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
package com.fc.service;
import java.util.ArrayList;
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.fc.mapper.AnswerMapper;
import com.fc.mapper.CollectionMapper;
import com.fc.mapper.UserMapper;
import com.fc.model.Answer;
import com.fc.model.Collection;
import com.fc.util.MyUtil;
import com.fc.util.RedisKey;
@Service
public class CollectionService {
@Autowired
private CollectionMapper collectionMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private AnswerMapper answerMapper;
@Autowired
private JedisPool jedisPool;
// 创建收藏夹
public void addCollection(Collection collection, Integer userId) {
collection.setUserId(userId);
collection.setCreateTime(new Date().getTime());
collection.setUpdateTime(new Date().getTime());
collectionMapper.insertCollection(collection);
}
// 列出自己创建的收藏夹
public List<Collection> listCreatingCollection(Integer userId) {
// 获取收藏夹列表
List<Collection> list = collectionMapper.listCreatingCollectionByUserId(userId);
// 获取每个列表中的答案条数
Jedis jedis = jedisPool.getResource();
for (Collection collection : list) {
Long answerCount = jedis.zcard(collection.getCollectionId() + RedisKey.COLLECT);
collection.setAnswerCount(Integer.parseInt(answerCount + ""));
System.out.println(answerCount);
}
jedisPool.returnResource(jedis);
return list;
}
// 收藏答案
public void collectAnswer(Integer collectionId, Integer answerId) {
// 更新用户被收藏数量
userMapper.updateCollectedCountByAnswerId(answerId);
Jedis jedis = jedisPool.getResource();
jedis.zadd(collectionId + RedisKey.COLLECT, new Date().getTime(), String.valueOf(answerId));
jedis.zadd(answerId + RedisKey.COLLECTED, new Date().getTime(), String.valueOf(collectionId));
jedisPool.returnResource(jedis);
}
// 取消收藏答案
public void uncollectAnswer(Integer collectionId, Integer answerId) {
Jedis jedis = jedisPool.getResource();
jedis.zrem(collectionId + RedisKey.COLLECT, String.valueOf(answerId));
jedis.zrem(answerId + RedisKey.COLLECTED, String.valueOf(collectionId));
jedisPool.returnResource(jedis);
}
public boolean collectionContainAnswer(Integer collectionId, Integer answerId) {
Jedis jedis = jedisPool.getResource();
Long rank = jedis.zrank(collectionId + RedisKey.COLLECT, String.valueOf(answerId));
jedisPool.returnResource(jedis);
return rank == null ? false : true;
}
// 获取收藏夹内容详情
public Map<String, Object> getCollectionContent(Integer collectionId, Integer localUserId) {
Map<String, Object> map = new HashMap<String, Object>();
Jedis jedis = jedisPool.getResource();
// 获取收藏夹内的答案id列表
Set<String> idSet = jedis.zrange(collectionId + RedisKey.COLLECT, 0, -1);
List<Integer> idList = MyUtil.StringSetToIntegerList(idSet);
if (idList.size() > 0) {
List<Answer> answerList = answerMapper.listAnswerByAnswerId(idList);
map.put("answerList", answerList);
}
// 判断是不是在获取自己的收藏夹信息
Integer userId = collectionMapper.selectUserIdByCollectionId(collectionId);
if (userId.equals(localUserId)) {
map.put("isSelf", true);
}
// 获取收藏夹信息
Collection collection = collectionMapper.selectCollectionByCollectionId(collectionId);
Long answerCount = jedis.zcard(collectionId + RedisKey.COLLECT);
collection.setAnswerCount(Integer.parseInt(answerCount + ""));
map.put("collection", collection);
jedisPool.returnResource(jedis);
return map;
}
// 判断某人是否关注了某收藏夹
public boolean judgePeopleFollowCollection(Integer userId, Integer collectionId) {
Jedis jedis = jedisPool.getResource();
Long rank = jedis.zrank(userId + RedisKey.FOLLOW_COLLECTION, String.valueOf(collectionId));
jedisPool.returnResource(jedis);
System.out.println(rank);
return rank == null ? false : true;
}
// 关注收藏夹
public void followCollection(Integer userId, Integer collectionId) {
Jedis jedis = jedisPool.getResource();
jedis.zadd(userId + RedisKey.FOLLOW_COLLECTION, new Date().getTime(), String.valueOf(collectionId));
jedis.zadd(collectionId + RedisKey.FOLLOWED_COLLECTION, new Date().getTime(), String.valueOf(userId));
jedisPool.returnResource(jedis);
}
// 取消关注收藏夹
public void unfollowCollection(Integer userId, Integer collectionId) {
Jedis jedis = jedisPool.getResource();
jedis.zrem(userId + RedisKey.FOLLOW_COLLECTION, String.valueOf(collectionId));
jedis.zrem(collectionId + RedisKey.FOLLOWED_COLLECTION, String.valueOf(userId));
jedisPool.returnResource(jedis);
}
// 列出某用户所关注的收藏夹
public List<Collection> listFollowingCollection(Integer userId) {
Jedis jedis = jedisPool.getResource();
// 获取所关注的收藏夹的id集合
Set<String> idSet = jedis.zrange(userId + RedisKey.FOLLOW_COLLECTION, 0, -1);
List<Integer> idList = MyUtil.StringSetToIntegerList(idSet);
List<Collection> list = new ArrayList<Collection>();
if (idList.size() > 0) {
list = collectionMapper.listCollectionByCollectionId(idList);
for (Collection collection : list) {
Long answerCount = jedis.zcard(collection.getCollectionId() + RedisKey.COLLECT);
collection.setAnswerCount(Integer.parseInt(answerCount + ""));
System.out.println(answerCount);
}
}
jedisPool.returnResource(jedis);
return list;
}
}