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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ThemeService {
@Resource
private ThemeMapper themeMapper;
@Autowired
private UuidGenHelper uuidGenHelper;
public void insertTheme(ThemeEntity themeEntity) {
themeEntity.setThemeId(uuidGenHelper.getUuidStr());
themeMapper.insert(themeEntity);
}
//根据id返回主题详情
public ThemeEntity queryByThemeId(String themeId) {
return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId,themeId));
}
//分页倒叙lastId之前的主题
public List<ThemeEntity> selectAll(String lastId,Integer pageSize) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.orderByDesc(ThemeEntity::getUpdateTime);
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
}
queryWrapper.last("limit "+pageSize);
queryWrapper.orderByDesc(ThemeEntity::getUpdateTime);
return themeMapper.selectList(queryWrapper);
}
/**
* 根据条件查询主题
* @param topidId 话题Id
* @param lastId 查询此主题Id之前的主题
* @param pageSize 查询数量
* @return
*/
public List<ThemeEntity> queryByTopic(String topidId, String lastId,Integer pageSize) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topidId);
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
}
if (pageSize!=null){
queryWrapper.last("limit "+pageSize);
}
return themeMapper.selectList(queryWrapper);
}
//根据话题查询所有的主题Id
public List<String> queryThemeIdsByTopic(String topidId) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topidId);
queryWrapper.select(ThemeEntity::getThemeId);
return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
}
//关注的主题列表
public List<ThemeEntity> queryByFans(List<String> fansList, String lastId,Integer pageSize) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getAuthorId, fansList)
.orderByDesc(ThemeEntity::getUpdateTime);
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId);
queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime());
}
queryWrapper.last("limit "+pageSize);
return themeMapper.selectList(queryWrapper);
}
//查询对应话题的发表用户集合
public Set<String> getPostUserCount(List<String> themeIds) {
return themeMapper.selectBatchIds(themeIds).stream()
.map(ThemeEntity::getAuthorId)
.collect(Collectors.toSet());
}
public Integer getForwardCountById(String themeId) {
return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeId, themeId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
}
public Integer getForwardCountByUser(String themeId,String userId) {
return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getFormerThemeId, themeId)
.eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
}
}