package com.tanpu.community.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tanpu.common.exception.BizException; import com.tanpu.common.uuid.UuidGenHelper; import com.tanpu.community.api.enums.DeleteTagEnum; import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.mapper.community.ThemeMapper; 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.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; @Transactional public void insertTheme(ThemeEntity themeEntity) { themeEntity.setThemeId(uuidGenHelper.getUuidStr()); themeMapper.insert(themeEntity); } @Transactional public void update(ThemeEntity themeEntity,String themeId) { themeEntity.setThemeId(themeId); themeMapper.updateById(themeEntity); } //根据id返回主题详情 public ThemeEntity queryByThemeId(String themeId) { return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getThemeId,themeId)); } public List<ThemeEntity> queryThemeIdsByUserId(String userId) { return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>() .eq(ThemeEntity::getAuthorId,userId) .eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())); } //根据ids返回主题详情 public List<ThemeEntity> queryByThemeIds(List<String> themeIds) { return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>() .in(ThemeEntity::getThemeId,themeIds) .eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())); } //分页倒叙lastId之前的主题 public List<ThemeEntity> selectAll(String lastId,Integer pageSize) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .orderByDesc(ThemeEntity::getUpdateTime); if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime()); } queryWrapper.last("limit "+pageSize); queryWrapper.orderByDesc(ThemeEntity::getId); 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.getUpdateTime()); } if (pageSize!=null){ queryWrapper.last("limit "+pageSize); } queryWrapper.orderByDesc(ThemeEntity::getId); 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()); } //关注的主题列表 public List<ThemeEntity> queryByUserIds(List<String> userIds, String lastId, Integer pageSize) { LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>() .in(ThemeEntity::getAuthorId, userIds) .orderByDesc(ThemeEntity::getUpdateTime); if (StringUtils.isNotEmpty(lastId)) { ThemeEntity lastEntity = queryByThemeId(lastId); if (lastEntity==null) throw new BizException("主题未找到,id:"+lastId); queryWrapper.lt(ThemeEntity::getUpdateTime, lastEntity.getUpdateTime()); } queryWrapper.last("limit "+pageSize); 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::getThemeId, 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.selectById(themeId); if (themeEntity==null){ throw new BizException("主题未找到,id:"+themeId); } themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode()); themeMapper.updateById(themeEntity); } }