ThemeService.java 13.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;
5
import com.tanpu.biz.common.enums.RelTypeEnum;
张辰's avatar
张辰 committed
6
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
刘基明's avatar
刘基明 committed
7
import com.tanpu.common.exception.BizException;
张辰's avatar
张辰 committed
8
import com.tanpu.common.redis.RedisHelper;
9
import com.tanpu.common.util.JsonUtil;
刘基明's avatar
刘基明 committed
10
import com.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
11 12
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.qo.TopicFollowQo;
13 14
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.api.enums.ThemeTypeEnum;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
18
import com.tanpu.community.dao.entity.community.TimesCountEntity;
刘基明's avatar
刘基明 committed
19
import com.tanpu.community.dao.entity.community.VisitLogEntity;
刘基明's avatar
刘基明 committed
20
import com.tanpu.community.dao.mapper.community.ThemeMapper;
刘基明's avatar
刘基明 committed
21 22
import com.tanpu.community.dao.mapper.community.VisitLogMapper;
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
23
import com.tanpu.community.util.TimeUtils;
刘基明's avatar
刘基明 committed
24
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
25
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
26
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
27
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
28

刘基明's avatar
刘基明 committed
29
import javax.annotation.Resource;
30
import java.util.Arrays;
31 32 33 34 35 36
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
刘基明's avatar
刘基明 committed
37
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
38

刘基明's avatar
刘基明 committed
39 40 41
@Service
public class ThemeService {

刘基明's avatar
刘基明 committed
42
    @Resource
刘基明's avatar
刘基明 committed
43
    private ThemeMapper themeMapper;
刘基明's avatar
刘基明 committed
44

刘基明's avatar
刘基明 committed
45
    @Resource
刘基明's avatar
刘基明 committed
46 47
    private UuidGenHelper uuidGenHelper;

刘基明's avatar
刘基明 committed
48
    @Resource
张辰's avatar
张辰 committed
49
    private RedisHelper redisHelper;
刘基明's avatar
刘基明 committed
50 51
    @Resource
    private VisitLogMapper visitLogMapper;
张辰's avatar
张辰 committed
52

刘基明's avatar
刘基明 committed
53
    @Transactional
刘基明's avatar
刘基明 committed
54
    public void insertTheme(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
55
        if (StringUtils.isBlank(themeEntity.getThemeId())) {
刘基明's avatar
刘基明 committed
56 57 58
            themeEntity.setThemeId(uuidGenHelper.getUuidStr());
        }

刘基明's avatar
刘基明 committed
59 60 61
        themeMapper.insert(themeEntity);
    }

刘基明's avatar
刘基明 committed
62
    @Transactional
刘基明's avatar
刘基明 committed
63 64
    public void update(ThemeEntity themeEntity, String themeId) {
        themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
65 66
    }

张辰's avatar
张辰 committed
67
    //n天内发表的所有主题
68
    public List<ThemeEntity> queryRecentdays(Integer days) {
刘基明's avatar
刘基明 committed
69
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
70
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
71 72 73 74 75 76 77 78 79 80 81 82
                .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
83 84 85 86

        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
87
    //根据id返回主题详情(未删)
刘基明's avatar
刘基明 committed
88
    public ThemeEntity queryByThemeId(String themeId) {
刘基明's avatar
刘基明 committed
89
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
90 91
                .eq(ThemeEntity::getThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
92 93
    }

张辰's avatar
张辰 committed
94 95
    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
刘基明's avatar
刘基明 committed
96
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
张辰's avatar
张辰 committed
97
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
98
        return themeEntity;
张辰's avatar
张辰 committed
99 100
    }

101
    //根据用户id查询主题list
张辰's avatar
张辰 committed
102
    public List<ThemeEntity> queryThemesByUserIdCreateDesc(String userId, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
103 104
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getAuthorId, userId)
105 106
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(ThemeEntity::getCreateTime);
刘基明's avatar
刘基明 committed
107 108
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
刘基明's avatar
刘基明 committed
109
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
110
            queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
刘基明's avatar
刘基明 committed
111
        }
刘基明's avatar
刘基明 committed
112
        if (pageSize != null) {
张辰's avatar
张辰 committed
113
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
114 115 116 117
        }
        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
118
    //根据ids返回主题详情,带分页
刘基明's avatar
刘基明 committed
119
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) {
120
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
121 122
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
123 124 125 126 127
        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
128
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
129
            queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
刘基明's avatar
刘基明 committed
130
        }
刘基明's avatar
刘基明 committed
131 132
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
刘基明's avatar
刘基明 committed
133 134
        }
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
135 136
    }

刘基明's avatar
刘基明 committed
137 138
    /**
     * 根据主题Id查询列表
139
     *
刘基明's avatar
刘基明 committed
140 141 142
     * @param themeIds
     * @return
     */
刘基明's avatar
刘基明 committed
143
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds) {
144
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
145 146
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
147 148 149 150
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());

刘基明's avatar
刘基明 committed
151 152
        List<ThemeEntity> themeEntities = themeMapper.selectList(queryWrapper);
        return themeEntities;
刘基明's avatar
刘基明 committed
153 154
    }

刘基明's avatar
刘基明 committed
155

刘基明's avatar
刘基明 committed
156
    /**
刘基明's avatar
刘基明 committed
157
     * 根据话题查询最新主题(可分页)
刘基明's avatar
刘基明 committed
158 159
     *
     * @param topidId  话题Id
刘基明's avatar
刘基明 committed
160 161 162
     * @param pageSize 查询数量
     * @return
     */
刘基明's avatar
刘基明 committed
163
    public List<ThemeEntity> queryNewestByTopic(String topidId, Integer pageStart, Integer pageSize, List<String> excludeIds) {
刘基明's avatar
刘基明 committed
164
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
张辰's avatar
张辰 committed
165 166 167 168
                .eq(ThemeEntity::getTopicId, topidId);
        if (!excludeIds.isEmpty()) {
            queryWrapper.notIn(ThemeEntity::getThemeId, excludeIds);
        }
刘基明's avatar
刘基明 committed
169
        queryWrapper.last("limit " + pageStart + ", " + pageSize)
170 171
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
172
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
173
    }
刘基明's avatar
刘基明 committed
174

刘基明's avatar
刘基明 committed
175
    //根据话题查询所有的主题Id,包括已删除的主题
刘基明's avatar
刘基明 committed
176
    public List<String> queryThemeIdsByTopic(String topidId) {
刘基明's avatar
刘基明 committed
177
        if (StringUtils.isEmpty(topidId)) {
刘基明's avatar
刘基明 committed
178 179
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
180 181 182 183 184 185
        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
186
    /**
刘基明's avatar
刘基明 committed
187
     * 根据作者查询主题分页列表
刘基明's avatar
刘基明 committed
188
     *
刘基明's avatar
刘基明 committed
189
     * @param userIds
刘基明's avatar
刘基明 committed
190
     * @param pageStart
刘基明's avatar
刘基明 committed
191 192 193
     * @param pageSize
     * @return
     */
张辰's avatar
张辰 committed
194
    public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize) {
刘基明's avatar
刘基明 committed
195
        if (CollectionUtils.isEmpty(userIds)) {
刘基明's avatar
刘基明 committed
196 197
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
198
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
199 200 201 202
                .in(ThemeEntity::getAuthorId, userIds)
                .last("limit " + pageStart + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
刘基明's avatar
刘基明 committed
203
        return themeMapper.selectList(queryWrapper);
刘基明's avatar
刘基明 committed
204 205 206

    }

刘基明's avatar
刘基明 committed
207 208
    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
209
                .eq(ThemeEntity::getFormerThemeId, themeId)
刘基明's avatar
刘基明 committed
210 211
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
212

刘基明's avatar
刘基明 committed
213
    public boolean judgeForwardByUser(String themeId, String userId) {
刘基明's avatar
刘基明 committed
214 215 216
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
刘基明's avatar
刘基明 committed
217
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)) > 0;
刘基明's avatar
刘基明 committed
218 219 220
    }

    public Set<String> getForwardUsers(List<String> themeIds) {
刘基明's avatar
刘基明 committed
221
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
222 223
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
224 225 226 227
        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
228
    }
刘基明's avatar
刘基明 committed
229

刘基明's avatar
刘基明 committed
230

刘基明's avatar
刘基明 committed
231
    @Transactional
刘基明's avatar
刘基明 committed
232
    public void deleteById(String themeId, String userId) {
刘基明's avatar
刘基明 committed
233 234
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
235
        if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
刘基明's avatar
刘基明 committed
236
            throw new BizException("主题与用户不匹配,id:" + themeId + ",userId" + userId);
刘基明's avatar
刘基明 committed
237 238 239 240
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
241

刘基明's avatar
刘基明 committed
242 243
    /**
     * 查询更新节点后的用户新建主题数
244
     *
刘基明's avatar
刘基明 committed
245 246
     * @param userIds 用户ids
     * @param lastId  更新时间节点
刘基明's avatar
刘基明 committed
247 248
     * @return
     */
张辰's avatar
张辰 committed
249
    public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
刘基明's avatar
刘基明 committed
250 251 252 253 254
        if (CollectionUtils.isEmpty(userIds)) {
            return 0;
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, userIds)
张辰's avatar
张辰 committed
255
                .gt(ThemeEntity::getId, lastId)
刘基明's avatar
刘基明 committed
256 257 258
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectCount(queryWrapper);
    }
刘基明's avatar
刘基明 committed
259 260


261 262 263
    public void updateReportStatus(String themeId) {
        ThemeEntity themeEntity = queryByThemeId(themeId);
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
264
            throw new BizException("主题未找到,id:" + themeId);
265 266 267 268
        }
        themeEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
269 270 271

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
刘基明's avatar
刘基明 committed
272
        if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
273 274
            return new HashMap<String, Integer>();
        }
刘基明's avatar
刘基明 committed
275 276
        LambdaQueryWrapper<ThemeEntity> wrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
刘基明's avatar
刘基明 committed
277
                .in(ThemeEntity::getFormerThemeId, themeIds)
刘基明's avatar
刘基明 committed
278 279 280 281 282
                .groupBy(ThemeEntity::getFormerThemeId);
        return themeMapper.selectCountByThemeIds(wrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
    }

刘基明's avatar
刘基明 committed
283 284 285
    public List<ThemeEntity> queryAllForward() {
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeType, ThemeTypeEnum.FORWARD.getCode())
刘基明's avatar
刘基明 committed
286
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
刘基明's avatar
刘基明 committed
287 288
                .orderByAsc(ThemeEntity::getCreateTime));
    }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    public String commentSyncForward(CreateCommentReq req, String userId) {
        // 评论构造theme content
        List<ThemeContentReq> themeContentReqs = Arrays.asList(ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build());
        ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build();
        ThemeEntity themeEntity = ThemeEntity.builder()
                .content(JsonUtil.toJson(themeContentReqs))
                .topicId("")
                .formerThemeId(req.getThemeId())
                .authorId(userId)
                .themeType(ThemeTypeEnum.FORWARD.getCode())
                .build();
        this.insertTheme(themeEntity);
        return themeEntity.getThemeId();
    }
刘基明's avatar
刘基明 committed
304 305 306 307 308 309 310 311 312 313 314


    public void queryCommentForTopic(List<TopicFollowQo> topicQos, String userId) {


        for (TopicFollowQo topic : topicQos) {
            String topicId = topic.getTopicId();
            // 最近的一条讨论
            ThemeEntity themeEntity = themeMapper.queryOneByTopicIdOrderByUpdateTimeDesc(topicId);
            if (themeEntity != null) {
                ThemeQo themeQo = ConvertUtil.themeEntityToQo(themeEntity);
刘基明's avatar
刘基明 committed
315
                topic.setLastTheme(getUserName(themeQo.getAuthorId()) + ":" + themeQo.content.get(0).getValue());
刘基明's avatar
刘基明 committed
316
                topic.setLastThemeTime(TimeUtils.formatTopicListTime(themeEntity.getUpdateTime()));
刘基明's avatar
刘基明 committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

            }

            // 查询更新条数
            VisitLogEntity visitLogEntity = visitLogMapper.queryLastByVisitorIdAndRefId(userId, topicId);
            if (visitLogEntity != null) {

                Integer updates = themeMapper.countByTopicIdAndCreateTimeAfter(topicId, visitLogEntity.getUpdateTime());
                topic.setUpdateCount(updates);
            } else {
                topic.setUpdateCount(0);
            }

        }

    }
刘基明's avatar
刘基明 committed
333 334 335 336 337

    private String getUserName(String authorId) {

        return "理财师Jack";
    }
刘基明's avatar
刘基明 committed
338
}