ThemeManager.java 5.49 KB
package com.tanpu.community.manager;

import com.tanpu.community.api.beans.ThemeDTO;
import com.tanpu.community.api.constants.BlockTypeEnum;
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.api.constants.CommentTypeEnum;
import com.tanpu.community.controller.convert.ThemeConvert;
import com.tanpu.community.dao.entity.community.BlackListEntity;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.FansRelEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ThemeManager {
    @Resource
    private ThemeService themeService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private FansRelService fansRelService;

    @Autowired
    private BlackListService blackListService;

    @Autowired
    private RedisService redisService;

    //返回推荐主题文章
    public List<ThemeDTO> selectHotThemes(){
        //TODO:根据算法计算推荐主题
        List<ThemeEntity> themeEntities = themeService.selectAll();
        List<ThemeDTO> themeDTOS = ThemeConvert.convertToDTOs(themeEntities);
        for (ThemeDTO themeDTO : themeDTOS) {

            themeDTO.setUpToNowTime(calUpToNowTime(themeDTO.getCreateTime()));
            //TODO 添加用户名、头像、认证
        }
        return themeDTOS;
    }

    //返回关注主题
    public List<ThemeDTO> selectInterestThemes(String userId){
        List<String> fansList = fansRelService.queryFansByFollowerId(userId).stream().map(FansRelEntity::getIdolId).collect(Collectors.toList());
        List<ThemeEntity> themeEntities = themeService.selectByFans(fansList);
        List<ThemeDTO> themeDTOS = ThemeConvert.convertToDTOs(themeEntities);
        for (ThemeDTO themeDTO : themeDTOS) {

            themeDTO.setUpToNowTime(calUpToNowTime(themeDTO.getCreateTime()));
            //TODO 添加用户名、头像、认证
        }
        return themeDTOS;
    }

    private String calUpToNowTime(LocalDateTime start) {
        Duration between = Duration.between(LocalDateTime.now(), start);
        long duration = between.toMinutes();
        if (duration<1){
            return "刚刚";
        }else if (duration<60){
            return duration+"分钟前";
        }else if (duration<60*24){
            return duration/60+"小时前";
        }else if (start.getYear()==LocalDateTime.now().getYear()){
            return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm:ss"));
        }
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    //点赞
    public void like(String themeId, String userId) {
        collectionService.addCollection(themeId, userId, CollectionTypeEnum.LIKE);
    }

    public void unlike(String themeId, String userId) {
    }

    // 评论(对主题)
    public void commentToTheme(String themeId, String userId, String comment) {
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setTargetId(themeId);
        commentEntity.setAuthorId(userId);
        commentEntity.setCommentType(CommentTypeEnum.THEME.getCode());
        commentEntity.setContent(comment);
        commentService.insertComment(commentEntity);
    }

    //转发
    public void forward(String themeId, String user) {
        //TODO
    }

    //分享
    public void share(String themeId, String user) {
        //TODO
    }

    //收藏
    public void favorite(String themeId, String userId) {
        collectionService.addCollection(themeId, userId, CollectionTypeEnum.BOOK);
    }

    public void unFavorite(String themeId, String userId) {

    }

    //投诉(主题)
    public void complaint(String themeId, String user) {
        //TODO
    }

    //屏蔽(用户)
    public void blockUser(String themeId, String userId) {
        String blockId = themeService.selectTheme(themeId).getAuthorId();
        blackListService.addBlock(blockId, userId, BlockTypeEnum.USER);
    }

    // 解除屏蔽(用户)
    public void unblockUser(String themeId, String userId) {
    }

    // 屏蔽(主题)
    public void blockTheme(String themeId, String userId) {
        blackListService.addBlock(themeId, userId, BlockTypeEnum.THEME);
        BlackListEntity selectOne = blackListService.selectOne(userId, BlockTypeEnum.USER.getCode(), themeService.selectTheme(themeId).getAuthorId());
        if (selectOne==null){
            blackListService.addBlock(themeService.selectTheme(themeId).getAuthorId(), userId, BlockTypeEnum.USER);
        }
    }

    // 解除屏蔽(主题)
    public void unblockTheme(String themeId, String userId) {
        blackListService.removeBlackList(themeId, userId, BlockTypeEnum.USER);
    }

    //屏蔽(内容)
//    public void blockContent(String themeId, String userId) {
//        BlackListEntity selectOne = blackListService.selectOne(userId, BlockTypeEnum.USER.getCode(), themeId);
//        if (selectOne==null){
//            blackListService.insertBlackList(userId, BlockTypeEnum.USER.getCode(), themeId);
//        }
//    }

    public void insert(ThemeEntity themeEntity) {
        themeService.insertTheme(themeEntity);
    }

}