ThemeService.java 12.8 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2
package com.tanpu.community.service;

刘基明's avatar
刘基明 committed
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
刘基明's avatar
刘基明 committed
4
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
张辰's avatar
张辰 committed
5
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
刘基明's avatar
刘基明 committed
6
import com.tanpu.common.exception.BizException;
张辰's avatar
张辰 committed
7
import com.tanpu.common.redis.RedisHelper;
刘基明's avatar
刘基明 committed
8
import com.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.dao.entity.community.TimesCountEntity;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.dao.mapper.community.ThemeMapper;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.util.TimeUtils;
刘基明's avatar
刘基明 committed
14
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
15
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
16
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
17
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
18
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
19

刘基明's avatar
刘基明 committed
20
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
21
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
22
import java.util.*;
刘基明's avatar
刘基明 committed
23
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
24

刘基明's avatar
刘基明 committed
25 26 27
@Service
public class ThemeService {

刘基明's avatar
刘基明 committed
28
    @Resource
刘基明's avatar
刘基明 committed
29
    private ThemeMapper themeMapper;
刘基明's avatar
刘基明 committed
30

刘基明's avatar
刘基明 committed
31 32 33
    @Autowired
    private UuidGenHelper uuidGenHelper;

张辰's avatar
张辰 committed
34 35 36
    @Autowired
    private RedisHelper redisHelper;

刘基明's avatar
刘基明 committed
37
    @Transactional
刘基明's avatar
刘基明 committed
38
    public void insertTheme(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
39
        themeEntity.setThemeId(uuidGenHelper.getUuidStr());
刘基明's avatar
刘基明 committed
40 41 42
        themeMapper.insert(themeEntity);
    }

刘基明's avatar
刘基明 committed
43
    @Transactional
刘基明's avatar
刘基明 committed
44 45
    public void update(ThemeEntity themeEntity, String themeId) {
        themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
46 47
    }

张辰's avatar
张辰 committed
48
    //n天内发表的所有主题
49
    public List<ThemeEntity> queryRecentdays(Integer days) {
刘基明's avatar
刘基明 committed
50
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
51
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
52 53 54 55 56 57 58 59 60 61 62 63
                .gt(ThemeEntity::getCreateTime, TimeUtils.getDaysBefore(days))
                .orderByDesc(ThemeEntity::getId);

        return themeMapper.selectList(queryWrapper);
    }

    //最新的n条主题
    public List<ThemeEntity> queryLatestThemes(Integer n) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .last("limit " + n)
                .orderByDesc(ThemeEntity::getId);
刘基明's avatar
刘基明 committed
64 65 66 67

        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
68
    //根据id返回主题详情(未删)
刘基明's avatar
刘基明 committed
69
    public ThemeEntity queryByThemeId(String themeId) {
刘基明's avatar
刘基明 committed
70
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
71 72
                .eq(ThemeEntity::getThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
73 74
    }

75
    //根据用户id查询主题list
刘基明's avatar
刘基明 committed
76
    public List<ThemeEntity> queryThemesByUserId(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
77 78 79 80 81 82
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(ThemeEntity::getId);
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
83
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
84 85
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
        }
刘基明's avatar
刘基明 committed
86 87
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
88 89 90 91
        }
        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
92
    //根据ids返回主题详情,带分页
刘基明's avatar
刘基明 committed
93
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) {
94
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
95 96
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
97 98 99 100 101
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
102
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
103 104
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
        }
刘基明's avatar
刘基明 committed
105 106
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
107 108
        }
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
109 110
    }

刘基明's avatar
刘基明 committed
111 112
    /**
     * 根据主题Id查询列表
113
     *
刘基明's avatar
刘基明 committed
114 115 116
     * @param themeIds
     * @return
     */
刘基明's avatar
刘基明 committed
117
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds) {
118
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
119 120
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
121 122 123 124 125
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());

        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
126 127
    }

刘基明's avatar
刘基明 committed
128

刘基明's avatar
刘基明 committed
129
    /**
刘基明's avatar
刘基明 committed
130
     * 查询非传入作者的主题(可分页)
131
     *
刘基明's avatar
刘基明 committed
132 133 134 135 136
     * @param lastId
     * @param pageSize
     * @param userId
     * @return
     */
137
    public List<ThemeEntity> selectExcludeUser(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
138
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<>();
139 140
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.ne(ThemeEntity::getAuthorId, userId);
刘基明's avatar
刘基明 committed
141
        }
刘基明's avatar
刘基明 committed
142

刘基明's avatar
刘基明 committed
143 144
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
145
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
146
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
刘基明's avatar
刘基明 committed
147
        }
148
        if (pageSize != null) {
刘基明's avatar
刘基明 committed
149 150 151 152 153 154 155 156 157 158 159
            queryWrapper.last("limit " + pageSize);
        }

        queryWrapper.orderByDesc(ThemeEntity::getId);
        queryWrapper.orderByDesc(ThemeEntity::getCreateTime);
        queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectList(queryWrapper);
    }

    /**
     * 查询非传入作者的主题(可分页)
160
     *
刘基明's avatar
刘基明 committed
161 162 163 164 165
     * @param lastId
     * @param pageSize
     * @param userId
     * @return
     */
166 167
    public List<ThemeEntity> queryByThemeIdsExcludeUser(List<String> themeIds, String userId, String lastId, Integer pageSize) {
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
168 169 170 171
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds);
172 173
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.ne(ThemeEntity::getAuthorId, userId);
刘基明's avatar
刘基明 committed
174 175 176 177 178 179 180
        }

        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
        }
181
        if (pageSize != null) {
刘基明's avatar
刘基明 committed
182 183
            queryWrapper.last("limit " + pageSize);
        }
刘基明's avatar
刘基明 committed
184

刘基明's avatar
刘基明 committed
185
        queryWrapper.orderByDesc(ThemeEntity::getId);
刘基明's avatar
刘基明 committed
186
        queryWrapper.orderByDesc(ThemeEntity::getCreateTime);
刘基明's avatar
刘基明 committed
187
        queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
188 189 190
        return themeMapper.selectList(queryWrapper);
    }

191

刘基明's avatar
刘基明 committed
192
    /**
刘基明's avatar
刘基明 committed
193
     * 根据话题查询最新主题(可分页)
刘基明's avatar
刘基明 committed
194 195
     *
     * @param topidId  话题Id
刘基明's avatar
刘基明 committed
196 197 198
     * @param pageSize 查询数量
     * @return
     */
张辰's avatar
张辰 committed
199
    public List<ThemeEntity> queryNewestByTopic(String topidId, Integer pageNo, Integer pageSize, List<String> excludeIds) {
刘基明's avatar
刘基明 committed
200
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
张辰's avatar
张辰 committed
201 202 203 204 205
                .eq(ThemeEntity::getTopicId, topidId);
        if (!excludeIds.isEmpty()) {
            queryWrapper.notIn(ThemeEntity::getThemeId, excludeIds);
        }
        queryWrapper.last("limit " + pageNo + ", " + pageSize)
206 207
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
208
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
209
    }
刘基明's avatar
刘基明 committed
210

刘基明's avatar
刘基明 committed
211
    //根据话题查询所有的主题Id,包括已删除的主题
刘基明's avatar
刘基明 committed
212
    public List<String> queryThemeIdsByTopic(String topidId) {
刘基明's avatar
刘基明 committed
213
        if (StringUtils.isEmpty(topidId)) {
刘基明's avatar
刘基明 committed
214 215
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
216 217 218 219 220 221
        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());
    }

刘基明's avatar
刘基明 committed
222
    /**
刘基明's avatar
刘基明 committed
223
     * 根据作者查询主题分页列表
刘基明's avatar
刘基明 committed
224
     * @param userIds
刘基明's avatar
刘基明 committed
225
     * @param pageStart
刘基明's avatar
刘基明 committed
226 227 228
     * @param pageSize
     * @return
     */
张辰's avatar
张辰 committed
229
    public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize) {
刘基明's avatar
刘基明 committed
230 231 232
        if (CollectionUtils.isEmpty(userIds)){
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
233
        //TODO 索引优化
刘基明's avatar
刘基明 committed
234
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
235 236 237 238
                .in(ThemeEntity::getAuthorId, userIds)
                .last("limit " + pageStart + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
239
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
240 241 242

    }

刘基明's avatar
刘基明 committed
243 244
    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
245
                .eq(ThemeEntity::getFormerThemeId, themeId)
刘基明's avatar
刘基明 committed
246 247
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
248

刘基明's avatar
刘基明 committed
249
    public boolean judgeForwardByUser(String themeId, String userId) {
刘基明's avatar
刘基明 committed
250 251 252
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
刘基明's avatar
刘基明 committed
253 254 255 256
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))>0;
    }

    public Set<String> getForwardUsers(List<String> themeIds) {
刘基明's avatar
刘基明 committed
257 258 259
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
260 261 262 263
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getFormerThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
                .stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
264
    }
刘基明's avatar
刘基明 committed
265

刘基明's avatar
刘基明 committed
266 267


刘基明's avatar
刘基明 committed
268
    @Transactional
刘基明's avatar
刘基明 committed
269
    public void deleteById(String themeId,String userId) {
刘基明's avatar
刘基明 committed
270 271
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
272 273
        if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
            throw new BizException("主题与用户不匹配,id:" + themeId+",userId"+userId);
刘基明's avatar
刘基明 committed
274 275 276 277
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
278

刘基明's avatar
刘基明 committed
279 280
    /**
     * 查询更新节点后的用户新建主题数
281 282
     *
     * @param userIds      用户ids
刘基明's avatar
刘基明 committed
283 284 285
     * @param lastViewTime 更新时间节点
     * @return
     */
张辰's avatar
张辰 committed
286
    public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
刘基明's avatar
刘基明 committed
287 288 289 290 291
        if (CollectionUtils.isEmpty(userIds)) {
            return 0;
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, userIds)
张辰's avatar
张辰 committed
292
                .gt(ThemeEntity::getId, lastId)
刘基明's avatar
刘基明 committed
293 294 295
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectCount(queryWrapper);
    }
刘基明's avatar
刘基明 committed
296 297


298 299 300 301 302 303 304 305
    public void updateReportStatus(String themeId) {
        ThemeEntity themeEntity = queryByThemeId(themeId);
        if (themeEntity == null) {
            throw new BizException("主题未找到,id:"+themeId);
        }
        themeEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
306 307 308

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
刘基明's avatar
刘基明 committed
309 310 311
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashMap<String, Integer>();
        }
刘基明's avatar
刘基明 committed
312 313 314 315 316 317 318 319
        LambdaQueryWrapper<ThemeEntity> wrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
                .in(ThemeEntity::getFormerThemeId,themeIds)
                .groupBy(ThemeEntity::getFormerThemeId);
        return themeMapper.selectCountByThemeIds(wrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
    }

刘基明's avatar
刘基明 committed
320
}