ThemeService.java 12.7 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.common.exception.BizException;
张辰's avatar
张辰 committed
6
import com.tanpu.common.redis.RedisHelper;
刘基明's avatar
刘基明 committed
7
import com.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.enums.DeleteTagEnum;
9
import com.tanpu.community.api.enums.ReportStatusEnum;
刘基明'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.Collections;
刘基明's avatar
刘基明 committed
23
import java.util.HashMap;
刘基明's avatar
刘基明 committed
24
import java.util.List;
刘基明's avatar
刘基明 committed
25
import java.util.Map;
刘基明's avatar
刘基明 committed
26
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
27

刘基明's avatar
刘基明 committed
28 29 30
@Service
public class ThemeService {

刘基明's avatar
刘基明 committed
31
    @Resource
刘基明's avatar
刘基明 committed
32
    private ThemeMapper themeMapper;
刘基明's avatar
刘基明 committed
33

刘基明's avatar
刘基明 committed
34 35 36
    @Autowired
    private UuidGenHelper uuidGenHelper;

张辰's avatar
张辰 committed
37 38 39
    @Autowired
    private RedisHelper redisHelper;

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

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

张辰's avatar
张辰 committed
51
    //n天内发表的所有主题
52
    public List<ThemeEntity> queryRecentdays(Integer days) {
刘基明's avatar
刘基明 committed
53
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
54
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
55 56 57 58 59 60 61 62 63 64 65 66
                .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
67 68 69 70

        return themeMapper.selectList(queryWrapper);
    }

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

刘基明's avatar
刘基明 committed
78
    public List<ThemeEntity> queryThemesByUserId(String userId) {
刘基明's avatar
刘基明 committed
79
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
80 81
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
刘基明's avatar
刘基明 committed
82 83 84
                .orderByDesc(ThemeEntity::getId));
    }

85
    //根据用户id查询主题list
刘基明's avatar
刘基明 committed
86
    public List<ThemeEntity> queryThemesByUserId(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
87 88 89 90 91 92
        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
93
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
94 95
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
        }
刘基明's avatar
刘基明 committed
96 97
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
98 99 100 101
        }
        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
102
    //根据ids返回主题详情,带分页
刘基明's avatar
刘基明 committed
103
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) {
104
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
105 106
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
107 108 109 110 111
        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
112
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
113 114
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
        }
刘基明's avatar
刘基明 committed
115 116
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
117 118
        }
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
119 120
    }

刘基明's avatar
刘基明 committed
121 122
    /**
     * 根据主题Id查询列表
123
     *
刘基明's avatar
刘基明 committed
124 125 126
     * @param themeIds
     * @return
     */
刘基明's avatar
刘基明 committed
127
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds) {
128
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
129 130
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
131 132 133 134 135
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());

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

刘基明's avatar
刘基明 committed
138

刘基明's avatar
刘基明 committed
139
    /**
刘基明's avatar
刘基明 committed
140
     * 查询非传入作者的主题(可分页)
141
     *
刘基明's avatar
刘基明 committed
142 143 144 145 146
     * @param lastId
     * @param pageSize
     * @param userId
     * @return
     */
147
    public List<ThemeEntity> selectExcludeUser(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
148
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<>();
149 150
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.ne(ThemeEntity::getAuthorId, userId);
刘基明's avatar
刘基明 committed
151
        }
刘基明's avatar
刘基明 committed
152

刘基明's avatar
刘基明 committed
153 154
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
155
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
刘基明's avatar
刘基明 committed
156
            queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime());
刘基明's avatar
刘基明 committed
157
        }
158
        if (pageSize != null) {
刘基明's avatar
刘基明 committed
159 160 161 162 163 164 165 166 167 168 169
            queryWrapper.last("limit " + pageSize);
        }

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

    /**
     * 查询非传入作者的主题(可分页)
170
     *
刘基明's avatar
刘基明 committed
171 172 173 174 175
     * @param lastId
     * @param pageSize
     * @param userId
     * @return
     */
176 177
    public List<ThemeEntity> queryByThemeIdsExcludeUser(List<String> themeIds, String userId, String lastId, Integer pageSize) {
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
178 179 180 181
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds);
182 183
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.ne(ThemeEntity::getAuthorId, userId);
刘基明's avatar
刘基明 committed
184 185 186 187 188 189 190
        }

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

刘基明's avatar
刘基明 committed
195
        queryWrapper.orderByDesc(ThemeEntity::getId);
刘基明's avatar
刘基明 committed
196
        queryWrapper.orderByDesc(ThemeEntity::getCreateTime);
刘基明's avatar
刘基明 committed
197
        queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
198 199 200
        return themeMapper.selectList(queryWrapper);
    }

201

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

刘基明's avatar
刘基明 committed
221 222
    //根据话题查询所有的主题Id
    public List<String> queryThemeIdsByTopic(String topidId) {
刘基明's avatar
刘基明 committed
223
        if (StringUtils.isEmpty(topidId)) {
刘基明's avatar
刘基明 committed
224 225
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
226 227 228 229 230 231
        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
232
    /**
刘基明's avatar
刘基明 committed
233
     * 根据作者查询主题分页列表
刘基明's avatar
刘基明 committed
234
     * @param userIds
刘基明's avatar
刘基明 committed
235
     * @param pageStart
刘基明's avatar
刘基明 committed
236 237 238
     * @param pageSize
     * @return
     */
张辰's avatar
张辰 committed
239
    public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize) {
刘基明's avatar
刘基明 committed
240 241 242
        if (CollectionUtils.isEmpty(userIds)){
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
243
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
244 245 246 247
                .in(ThemeEntity::getAuthorId, userIds)
                .last("limit " + pageStart + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
248
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
249 250 251

    }

刘基明's avatar
刘基明 committed
252 253
    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
254
                .eq(ThemeEntity::getFormerThemeId, themeId)
刘基明's avatar
刘基明 committed
255 256
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
257

刘基明's avatar
刘基明 committed
258
    public Integer getForwardCountByUser(String themeId, String userId) {
刘基明's avatar
刘基明 committed
259 260 261 262 263
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
264

刘基明's avatar
刘基明 committed
265
    @Transactional
刘基明's avatar
刘基明 committed
266
    public void deleteById(String themeId) {
刘基明's avatar
刘基明 committed
267 268
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
269 270
        if (themeEntity == null) {
            throw new BizException("主题未找到,id:" + themeId);
刘基明's avatar
刘基明 committed
271 272 273 274
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
275

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


297 298 299 300 301 302 303 304
    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
305 306 307

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
刘基明's avatar
刘基明 committed
308 309 310
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashMap<String, Integer>();
        }
刘基明's avatar
刘基明 committed
311 312 313 314 315 316 317 318
        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
319
}