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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.api.constants.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.mapper.community.CollectionMapper;
import com.tanpu.community.util.ConvertUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class CollectionService {
@Resource
private CollectionMapper collectionMapper;
// 若不存在则新增,若存在则修改deleteTag
public void addIfNotExist(String themeId, String userId, CollectionTypeEnum type) {
// 判断是否存在
CollectionEntity queryCollection = getTargetCollection(themeId, userId, type);
if (queryCollection != null) {
Integer oldDeleteTag = queryCollection.getDeleteTag();
queryCollection.setDeleteTag(ConvertUtil.deleteTagShift(oldDeleteTag));
collectionMapper.updateById(queryCollection);
} else {
CollectionEntity entity = CollectionEntity.builder()
.collectionType(type.getCode())
.authorId(userId)
.targetId(themeId)
.createBy(userId)
.build();
collectionMapper.insert(entity);
}
}
//根据用户、主题、类型查询
public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) {
return collectionMapper.selectOne(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type)
.eq(CollectionEntity::getAuthorId, userId)
.eq(CollectionEntity::getTargetId, themeId));
}
// 根据用户id获取点赞列表
public List<CollectionEntity> getLikeListByUser(String userId) {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
// 根据用户id获取收藏列表
public List<CollectionEntity> getBookListByUser(String userId) {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
// 统计单个主题的点赞量
public Long getLikeAmountByThemeId(String themeId) {
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getTargetId, themeId)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())))
.size();
}
// 统计多个主题的点赞量
public Long getLikeAmountByThemeIds(List<String> themeIds) {
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.in(CollectionEntity::getTargetId, themeIds)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE.getCode()))
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
.size();
}
//统计单个主题的收藏量
public Long getBookAmountByThemeId(String themeId) {
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getTargetId, themeId)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode()))
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
.size();
}
//统多个主题的收藏量
public Long getBookAmountByThemeIds(List<String> themeIds) {
return (long) collectionMapper.selectList((new LambdaQueryWrapper<CollectionEntity>()
.in(CollectionEntity::getTargetId, themeIds)
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.BOOK.getCode()))
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
.size();
}
}