ThemeService.java 12.3 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;
11 12
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.api.enums.ThemeTypeEnum;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.dao.entity.community.TimesCountEntity;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.dao.mapper.community.ThemeMapper;
刘基明's avatar
刘基明 committed
18
import com.tanpu.community.util.TimeUtils;
刘基明's avatar
刘基明 committed
19
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
20
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
21
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
22
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
23
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
24

刘基明's avatar
刘基明 committed
25
import javax.annotation.Resource;
26
import java.util.Arrays;
27 28 29 30 31 32
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
33
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
34

刘基明's avatar
刘基明 committed
35 36 37
@Service
public class ThemeService {

刘基明's avatar
刘基明 committed
38
    @Resource
刘基明's avatar
刘基明 committed
39
    private ThemeMapper themeMapper;
刘基明's avatar
刘基明 committed
40

刘基明's avatar
刘基明 committed
41 42 43
    @Autowired
    private UuidGenHelper uuidGenHelper;

张辰's avatar
张辰 committed
44 45 46
    @Autowired
    private RedisHelper redisHelper;

刘基明's avatar
刘基明 committed
47
    @Transactional
刘基明's avatar
刘基明 committed
48
    public void insertTheme(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
49 50 51 52
        if (StringUtils.isBlank(themeEntity.getThemeId())){
            themeEntity.setThemeId(uuidGenHelper.getUuidStr());
        }

刘基明's avatar
刘基明 committed
53 54 55
        themeMapper.insert(themeEntity);
    }

刘基明's avatar
刘基明 committed
56
    @Transactional
刘基明's avatar
刘基明 committed
57 58
    public void update(ThemeEntity themeEntity, String themeId) {
        themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
59 60
    }

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

        return themeMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
81
    //根据id返回主题详情(未删)
刘基明's avatar
刘基明 committed
82
    public ThemeEntity queryByThemeId(String themeId) {
刘基明's avatar
刘基明 committed
83
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
84 85
                .eq(ThemeEntity::getThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
刘基明's avatar
刘基明 committed
86 87
    }

张辰's avatar
张辰 committed
88 89
    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
刘基明's avatar
刘基明 committed
90
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
张辰's avatar
张辰 committed
91
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
92
        return themeEntity;
张辰's avatar
张辰 committed
93 94
    }

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

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

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

刘基明's avatar
刘基明 committed
145 146
        List<ThemeEntity> themeEntities = themeMapper.selectList(queryWrapper);
        return themeEntities;
刘基明's avatar
刘基明 committed
147 148
    }

刘基明's avatar
刘基明 committed
149

刘基明's avatar
刘基明 committed
150

151

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

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

    }

刘基明's avatar
刘基明 committed
202 203
    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
204
                .eq(ThemeEntity::getFormerThemeId, themeId)
刘基明's avatar
刘基明 committed
205 206
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
207

刘基明's avatar
刘基明 committed
208
    public boolean judgeForwardByUser(String themeId, String userId) {
刘基明's avatar
刘基明 committed
209 210 211
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
刘基明's avatar
刘基明 committed
212 213 214 215
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))>0;
    }

    public Set<String> getForwardUsers(List<String> themeIds) {
刘基明's avatar
刘基明 committed
216 217 218
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
219 220 221 222
        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
223
    }
刘基明's avatar
刘基明 committed
224

刘基明's avatar
刘基明 committed
225 226


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

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


257 258 259 260 261 262 263 264
    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
265 266 267

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
刘基明's avatar
刘基明 committed
268 269 270
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashMap<String, Integer>();
        }
刘基明's avatar
刘基明 committed
271 272 273 274 275 276 277 278
        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
279 280 281 282 283 284
    public List<ThemeEntity> queryAllForward() {
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeType, ThemeTypeEnum.FORWARD.getCode())
                .eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
                .orderByAsc(ThemeEntity::getCreateTime));
    }
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    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
300
}