ThemeService.java 10.9 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;
21 22 23 24 25 26
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
27
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
28

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

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

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

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

刘基明's avatar
刘基明 committed
41
    @Transactional
刘基明's avatar
刘基明 committed
42
    public void insertTheme(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
43 44 45 46
        if (StringUtils.isBlank(themeEntity.getThemeId())){
            themeEntity.setThemeId(uuidGenHelper.getUuidStr());
        }

刘基明's avatar
刘基明 committed
47 48 49
        themeMapper.insert(themeEntity);
    }

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

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

        return themeMapper.selectList(queryWrapper);
    }

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

张辰's avatar
张辰 committed
82 83 84 85 86 87
    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
    }

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

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

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

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

刘基明's avatar
刘基明 committed
141

刘基明's avatar
刘基明 committed
142

143

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

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

    }

刘基明's avatar
刘基明 committed
194 195
    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
刘基明's avatar
刘基明 committed
196
                .eq(ThemeEntity::getFormerThemeId, themeId)
刘基明's avatar
刘基明 committed
197 198
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }
刘基明's avatar
刘基明 committed
199

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

    public Set<String> getForwardUsers(List<String> themeIds) {
刘基明's avatar
刘基明 committed
208 209 210
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashSet<>();
        }
刘基明's avatar
刘基明 committed
211 212 213 214
        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
215
    }
刘基明's avatar
刘基明 committed
216

刘基明's avatar
刘基明 committed
217 218


刘基明's avatar
刘基明 committed
219
    @Transactional
刘基明's avatar
刘基明 committed
220
    public void deleteById(String themeId,String userId) {
刘基明's avatar
刘基明 committed
221 222
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
刘基明's avatar
刘基明 committed
223 224
        if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
            throw new BizException("主题与用户不匹配,id:" + themeId+",userId"+userId);
刘基明's avatar
刘基明 committed
225 226 227 228
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }
刘基明's avatar
刘基明 committed
229

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


249 250 251 252 253 254 255 256
    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
257 258 259

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
刘基明's avatar
刘基明 committed
260 261 262
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashMap<String, Integer>();
        }
刘基明's avatar
刘基明 committed
263 264 265 266 267 268 269 270
        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
271
}