package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.tanpu.common.exception.BizException; import com.tanpu.common.redis.RedisHelper; import com.tanpu.common.uuid.UuidGenHelper; import com.tanpu.community.api.enums.DeleteTagEnum; import com.tanpu.community.api.enums.ReportStatusEnum; import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.mapper.community.ThemeMapper; import com.tanpu.community.util.TimeUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @Service public class ThemeService { @Resource private ThemeMapper themeMapper; @Autowired private UuidGenHelper uuidGenHelper; @Autowired private RedisHelper redisHelper; @Transactional public void insertTheme(ThemeEntity themeEntity) { themeEntity.setThemeId(uuidGenHelper.getUuidStr()); themeMapper.insert(themeEntity); } @Transactional public void update(ThemeEntity themeEntity, String themeId) { themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId)); } //n天内所有主题 public List<ThemeEntity> queryRecentdays(Integer days) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()) .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); return themeMapper.selectList(queryWrapper); } //根据id返回主题详情(未删) public ThemeEntity queryByThemeId(String themeId) { return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getThemeId, themeId) .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())); } public List<ThemeEntity> queryThemesByUserId(String userId) { return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getAuthorId, userId) .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()) .orderByDesc(ThemeEntity::getId)); } //根据用户id查询主题list public List<ThemeEntity> queryThemesByUserId(String userId, String lastId, Integer pageSize) { 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); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } return themeMapper.selectList(queryWrapper); } //根据ids返回主题详情,带分页 public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) { if (CollectionUtils.isEmpty(themeIds)) { return Collections.emptyList(); } 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); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } return themeMapper.selectList(queryWrapper); } /** * 根据主题Id查询列表 * * @param themeIds * @return */ public List<ThemeEntity> queryByThemeIds(List<String> themeIds) { if (CollectionUtils.isEmpty(themeIds)) { return Collections.emptyList(); } LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .in(ThemeEntity::getThemeId, themeIds) .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } /** * 查询非传入作者的主题(可分页) * * @param lastId * @param pageSize * @param userId * @return */ public List<ThemeEntity> selectExcludeUser(String userId, String lastId, Integer pageSize) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<>(); if (!StringUtils.isEmpty(userId)) { queryWrapper.ne(ThemeEntity::getAuthorId, userId); } if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } queryWrapper.orderByDesc(ThemeEntity::getId); queryWrapper.orderByDesc(ThemeEntity::getCreateTime); queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } /** * 查询非传入作者的主题(可分页) * * @param lastId * @param pageSize * @param userId * @return */ public List<ThemeEntity> queryByThemeIdsExcludeUser(List<String> themeIds, String userId, String lastId, Integer pageSize) { if (CollectionUtils.isEmpty(themeIds)) { return Collections.emptyList(); } LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .in(ThemeEntity::getThemeId, themeIds); if (!StringUtils.isEmpty(userId)) { queryWrapper.ne(ThemeEntity::getAuthorId, userId); } if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } queryWrapper.orderByDesc(ThemeEntity::getId); queryWrapper.orderByDesc(ThemeEntity::getCreateTime); queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } /** * 根据话题查询最新主题(可分页) * * @param topidId 话题Id * @param lastId 查询此主题Id之前的主题 * @param pageSize 查询数量 * @return */ public List<ThemeEntity> queryByTopic(String topidId, String lastId, Integer pageSize) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getTopicId, topidId); if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } if (pageSize != null) { queryWrapper.last("limit " + pageSize); } queryWrapper.orderByDesc(ThemeEntity::getCreateTime); queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } //根据话题查询所有的主题Id public List<String> queryThemeIdsByTopic(String topidId) { if (StringUtils.isEmpty(topidId)) { return Collections.emptyList(); } 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()); } /** * 根据作者查询主题列表(可分页) * * @param userIds * @param lastId * @param pageSize * @return */ public List<ThemeEntity> queryByUserIds(List<String> userIds, String lastId, Integer pageSize) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .in(ThemeEntity::getAuthorId, userIds); if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } queryWrapper.last("limit " + pageSize); queryWrapper.orderByDesc(ThemeEntity::getCreateTime); queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } /** * 根据关键字搜索 * * @param keyword * @param lastId * @param pageSize * @return */ public List<ThemeEntity> search(String keyword, String lastId, Integer pageSize) { if (StringUtils.isEmpty(keyword)) { return Collections.emptyList(); } LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .like(ThemeEntity::getTitle, keyword); if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getCreateTime()); } queryWrapper.last("limit " + pageSize); queryWrapper.orderByDesc(ThemeEntity::getCreateTime); queryWrapper.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()); return themeMapper.selectList(queryWrapper); } //查询对应话题的发表用户集合 public Set<String> getPostUserCount(List<String> themeIds) { return themeMapper.selectBatchIds(themeIds).stream() .map(ThemeEntity::getAuthorId) .collect(Collectors.toSet()); } public Integer getForwardCountById(String themeId) { return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getFormerThemeId, themeId) .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)); } public Integer getForwardCountByUser(String themeId, String userId) { return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getFormerThemeId, themeId) .eq(ThemeEntity::getAuthorId, userId) .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)); } @Transactional public void deleteById(String themeId) { ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getThemeId, themeId)); if (themeEntity == null) { throw new BizException("主题未找到,id:" + themeId); } themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode()); themeMapper.updateById(themeEntity); } /** * 查询更新节点后的用户新建主题数 * * @param userIds 用户ids * @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); } 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); } }