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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class CollectionService {
@Resource
private CollectionMapper collectionMapper;
// 若不存在则新增,若存在则修改deleteTag
@Transactional
public void saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) {
// 判断记录是否存在,无论是否删除
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getTargetId, themeId);
CollectionEntity queryCollection = collectionMapper.selectOne(queryWrapper);
if (queryCollection != null) {
queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
queryCollection.setCollectionTime(LocalDateTime.now());
collectionMapper.updateById(queryCollection);
} else {
CollectionEntity entity = CollectionEntity.builder()
.collectionType(type.getCode())
.userId(userId)
.targetId(themeId)
.collectionTime(LocalDateTime.now())
.build();
collectionMapper.insert(entity);
}
}
//根据用户、主题、类型查询未删除对象
public CollectionEntity queryCollection(String targetId, String userId, CollectionTypeEnum type) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getTargetId, targetId)
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
return collectionMapper.selectOne(queryWrapper);
}
//根据用户、主题、类型查询未删除对象
public CollectionEntity queryIncludeDelete(String targetId, String userId, CollectionTypeEnum type) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getTargetId, targetId);
return collectionMapper.selectOne(queryWrapper);
}
//根据用户、主题、类型查询未删除对象
public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) {
if (CollectionUtils.isEmpty(targetIds)){
return new HashSet<>();
}
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.in(CollectionEntity::getTargetId, targetIds)
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
return collectionMapper.selectList(queryWrapper).stream().map(CollectionEntity::getTargetId)
.collect(Collectors.toSet());
}
// 根据用户id和行为type获取target_id列表
public Set<String> getSetByUser(String userId, CollectionTypeEnum type) {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
.stream().map(CollectionEntity::getTargetId).collect(Collectors.toSet());
}
// 根据用户id和行为type获取target_id列表
public List<String> getListByUser(String userId, CollectionTypeEnum type) {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByDesc(CollectionEntity::getCollectionTime))
.stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());
}
// 根据用户id和行为type获取target_id列表
public List<String> getListByUser(String userId, CollectionTypeEnum type,String lastId,Integer pageSize) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByDesc(CollectionEntity::getCollectionTime);
if (StringUtils.isNotEmpty(lastId)) {
CollectionEntity target = queryIncludeDelete(lastId, userId, type);
if (target == null) return Collections.emptyList();
queryWrapper.lt(CollectionEntity::getCollectionType, target.getCollectionTime());
}
if (pageSize != null) {
queryWrapper.last("limit " + pageSize);
}
return collectionMapper.selectList(queryWrapper)
.stream().map(CollectionEntity::getTargetId).collect(Collectors.toList());
}
// 统计单个对象(主题、评论)的数量(点赞、收藏)
public Integer getCountByTypeAndId(String targetId, CollectionTypeEnum type) {
return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getTargetId, targetId)
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())));
}
// 统计多个对象(主题、评论)的数量(点赞、收藏)
public Map<String, Integer> getCountMapByType(List<String> targetIds, CollectionTypeEnum type) {
if (CollectionUtils.isEmpty(targetIds)) {
return new HashMap<>();
}
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>().eq(CollectionEntity::getCollectionType, 1)
.in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId);
return collectionMapper.selectCountByTargetIds(queryWrapper).stream()
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
}
// 统计多个对象(主题、评论)的数量(点赞、收藏)
public Integer getCountByTypeAndIds(List<String> targetIds, CollectionTypeEnum type) {
return collectionMapper.selectCount((new LambdaQueryWrapper<CollectionEntity>()
.in(CollectionEntity::getTargetId, targetIds)
.eq(CollectionEntity::getCollectionType, type.getCode()))
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
//逻辑删除,修改delete_tag
@Transactional
public void delete(String themeId, String userId, CollectionTypeEnum type) {
CollectionEntity queryCollection = queryCollection(themeId, userId, type);
if (queryCollection != null) {
queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode());
queryCollection.setUncollectionTime(LocalDateTime.now());
collectionMapper.updateById(queryCollection);
} else {
// throw new BizException("Collection对象未找到,themeId:" + themeId + ",userId:" + userId + ",type:" + type.getCode());
return;
}
}
}