ThemeService.java 17 KB
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.qo.TopicFollowQo;
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.StatusEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.entity.community.VisitLogEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import com.tanpu.community.dao.mapper.community.TopicMapper;
import com.tanpu.community.dao.mapper.community.VisitLogMapper;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ThemeService {

    @Resource
    private ThemeMapper themeMapper;

    @Resource
    private UuidGenHelper uuidGenHelper;

    @Resource
    private TopicMapper topicMapper;
    @Resource
    private VisitLogMapper visitLogMapper;
    @Resource
    private FeignService feignService;

    @Transactional
    public void insertTheme(ThemeEntity themeEntity) {
        if (StringUtils.isBlank(themeEntity.getThemeId())) {
            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> queryRecentdaysOrHasTopic(Integer days) {

        return themeMapper.queryRecentdaysOrHasTopic(DeleteTagEnum.NOT_DELETED.getCode(), TimeUtils.getDaysBefore(days), "");
    }

    //最新的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()));
    }

    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
        return themeEntity;
    }

    //根据用户id查询主题list
    public List<ThemeEntity> queryThemesByUserIdCreateDesc(String userId, String lastId, Integer pageSize, Set<String> userPermitTopics) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .in(ThemeEntity::getTopicId, userPermitTopics)
                .orderByDesc(ThemeEntity::getCreateTime);
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
            queryWrapper.lt(ThemeEntity::getCreateTime, 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::getCreateTime, 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, Set<String> userPermitTopics) {
        if (CollectionUtils.isEmpty(themeIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .in(ThemeEntity::getTopicId, userPermitTopics)
                .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::getCreateTime, 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());

        List<ThemeEntity> themeEntities = themeMapper.selectList(queryWrapper);
        return themeEntities;
    }


    /**
     * 根据话题查询最新主题(可分页)
     *
     * @param topidId  话题Id
     * @param pageSize 查询数量
     * @return
     */
    public List<ThemeEntity> queryNewestByTopic(String topidId, Integer pageStart, Integer pageSize, List<String> excludeIds) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        if (!excludeIds.isEmpty()) {
            queryWrapper.notIn(ThemeEntity::getThemeId, excludeIds);
        }
        queryWrapper.last("limit " + pageStart + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectList(queryWrapper);
    }

    // 根据话题查询所有的主题Id,
    // 不包括已删除的主题 v2.3.1
    public List<String> queryThemeIdsByTopic(String topidId) {
        if (StringUtils.isEmpty(topidId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId)
                .eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED);
        queryWrapper.select(ThemeEntity::getThemeId);
        return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
    }

    public List<String> queryThemeIdsByTopic(String topidId, Date startDate, Date endDate) {
        if (StringUtils.isEmpty(topidId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId).gt(ThemeEntity::getCreateTime, startDate).lt(ThemeEntity::getCreateTime, endDate);
        queryWrapper.select(ThemeEntity::getThemeId);
        return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
    }

    /**
     * 根据作者查询主题分页列表
     *
     * @param userIds
     * @param pageStart
     * @param pageSize
     * @param userFollowTopics
     * @return
     */
    public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize, Set<String> userFollowTopics) {
        if (CollectionUtils.isEmpty(userIds) && CollectionUtils.isEmpty(userFollowTopics)) {
            return Collections.emptyList();
        }
        userFollowTopics.remove("");
        return themeMapper.queryFollowList(userIds, userFollowTopics, pageStart, pageSize);

    }

    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }

    public boolean judgeForwardByUser(String themeId, String userId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)) > 0;
    }

    public Set<String> getForwardUsers(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)) {
            return new HashSet<>();
        }
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getFormerThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
                .stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet());
    }


    @Transactional
    public void deleteById(String themeId, String userId) {
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
        if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
            throw new BizException("主题与用户不匹配,id:" + themeId + ",userId" + userId);
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }

    /**
     * 查询更新节点后的用户新建主题数
     *
     * @param userIds 用户ids
     * @param lastId  更新时间节点
     * @return
     */
    public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
        if (CollectionUtils.isEmpty(userIds)) {
            return 0;
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, userIds)
                .gt(ThemeEntity::getId, lastId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        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);
    }

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)) {
            return new HashMap<String, Integer>();
        }
        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));
    }

    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));
    }

    public String commentSyncForward(CreateCommentReq req, String userId) {

        checkForwardSpecialPermission(req.getThemeId());
        // 评论构造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();
    }

    /**
     * 专属话题不允许转发
     */
    public void checkForwardSpecialPermission(String themeId) {
        ThemeEntity themeEntity = queryByThemeId(themeId);
        if (themeEntity!=null && StringUtils.isNotBlank(themeEntity.getTopicId())){
            TopicEntity topicEntity = topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>().eq(TopicEntity::getTopicId, themeEntity.getTopicId()));
            if (topicEntity!=null && topicEntity.getSpecialPermission()==1){
                throw new BizException(ErrorCodeConstant.TOPIC_FORWARD_ABORT.getCode(),"专属话题不允许转发");
            }
        }
    }


    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);

                topic.setLastTheme(getUserName(themeQo.getAuthorId()) + ":" + themeQo.content.get(0).getValue());
                topic.setLastThemeTime(TimeUtils.formatTopicListTime(themeEntity.getUpdateTime()));
                topic.setLastThemeSecond(themeEntity.getUpdateTime().toEpochSecond(ZoneOffset.of("+8")));

            }

            // 查询更新条数
            VisitLogEntity visitLogEntity = visitLogMapper.queryLastByVisitorIdAndRefId(userId, topicId);
            Integer updates;
            if (visitLogEntity != null) {
                updates = themeMapper.countByTopicIdAndCreateTimeAfter(topicId, visitLogEntity.getUpdateTime());
            } else {
                updates = themeMapper.countByTopicIdAndCreateTimeAfter(topicId, null);
            }
            topic.setUpdateCount(updates);

        }

    }

    private String getUserName(String authorId) {
        UserInfoResp userInfoById = feignService.getUserInfoById(authorId);
        if (StringUtils.isNotBlank(userInfoById.getNickName())) {
            return userInfoById.getNickName();
        }
        return "理财师";
    }

    public List<ThemeEntity> queryTopByTopic(String topicId) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topicId)
                .eq(ThemeEntity::getDeleteTag, StatusEnum.FALSE.getCode())
                .eq(ThemeEntity::getIsTop, StatusEnum.TRUE.getCode())
                .orderByDesc(ThemeEntity::getSetTopTime);
        return themeMapper.selectList(queryWrapper);
    }
}