ThemeManager.java 8.64 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2
package com.tanpu.community.manager;

刘基明's avatar
刘基明 committed
3 4
import com.tanpu.community.api.beans.req.CreateCommentReq;
import com.tanpu.community.api.beans.req.CreateThemeReq;
5
import com.tanpu.community.api.beans.ForwardThemeDTO;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.api.beans.qo.ThemeQo;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.api.constants.BlockTypeEnum;
刘基明's avatar
刘基明 committed
8 9
import com.tanpu.community.api.constants.CollectionTypeEnum;
import com.tanpu.community.api.constants.CommentTypeEnum;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.dao.entity.community.*;
11
import com.tanpu.community.dao.entity.user.CurriculumResEntity;
12
import com.tanpu.community.dao.entity.user.OrderFlowEntity;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.service.*;
14
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
15
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
16
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
17 18 19
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

刘基明's avatar
刘基明 committed
20
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
21 22 23
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
刘基明's avatar
刘基明 committed
24
import java.util.List;
刘基明's avatar
刘基明 committed
25
import java.util.Set;
刘基明's avatar
刘基明 committed
26 27
import java.util.stream.Collectors;

刘基明's avatar
刘基明 committed
28 29
@Service
public class ThemeManager {
刘基明's avatar
刘基明 committed
30
    @Resource
刘基明's avatar
刘基明 committed
31
    private ThemeService themeService;
刘基明's avatar
刘基明 committed
32 33 34 35 36 37 38 39 40 41

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private FansRelService fansRelService;

刘基明's avatar
刘基明 committed
42 43 44
    @Autowired
    private BlackListService blackListService;

45 46 47
    @Autowired
    private FinProResService finProResService;

刘基明's avatar
刘基明 committed
48 49 50
    @Autowired
    private RedisService redisService;

51 52 53 54 55 56
    @Autowired
    private ZhiboService zhiboService;

    @Autowired
    private CurriculumResService curriculumResService;

57 58 59
    @Autowired
    private OrderFlowService orderFlowService;

刘基明's avatar
刘基明 committed
60 61 62
    @Autowired
    private ThemeAttachmentService themeAttachmentService;

刘基明's avatar
刘基明 committed
63 64 65 66 67 68 69 70 71 72 73 74 75
    @Autowired
    private HomePageService homePageService;

    public void createTheme(CreateThemeReq req, String userId) {
        ThemeEntity themeEntity = new ThemeEntity();
        BeanUtils.copyProperties(req,themeEntity);
        themeEntity.setAuthorId(userId);

        themeService.insertTheme(themeEntity);
        List<ThemeAttachmentEntity> themeAttachments = ConvertUtil.themeReqToAttachmentList(req,themeEntity.getId());
        themeAttachmentService.insertList(themeAttachments);

    }
刘基明's avatar
刘基明 committed
76
    // 返回推荐主题文章
刘基明's avatar
刘基明 committed
77
    public List<ThemeQo> selectHotThemes(String userId) {
刘基明's avatar
刘基明 committed
78
        // TODO:根据算法计算推荐主题
刘基明's avatar
刘基明 committed
79
        List<ThemeEntity> themeEntities = themeService.selectAll();
刘基明's avatar
刘基明 committed
80 81 82
        List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities);
        for (ThemeQo themeQO : themeQos) {
            fillDTOExtraInfo(themeQO, userId);
刘基明's avatar
刘基明 committed
83
        }
刘基明's avatar
刘基明 committed
84
        return themeQos;
刘基明's avatar
刘基明 committed
85 86
    }

87

刘基明's avatar
刘基明 committed
88
    // 返回关注主题
刘基明's avatar
刘基明 committed
89 90
    public List<ThemeQo> selectInterestThemes(String userId) {
        List<String> fansList = fansRelService.queryFansByFollowerId(userId);
刘基明's avatar
刘基明 committed
91
        List<ThemeEntity> themeEntities = themeService.selectByFans(fansList);
刘基明's avatar
刘基明 committed
92 93 94
        List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities);
        for (ThemeQo themeQO : themeQos) {
            fillDTOExtraInfo(themeQO, userId);
刘基明's avatar
刘基明 committed
95
        }
刘基明's avatar
刘基明 committed
96
        return themeQos;
刘基明's avatar
刘基明 committed
97 98 99 100 101
    }

    private String calUpToNowTime(LocalDateTime start) {
        Duration between = Duration.between(LocalDateTime.now(), start);
        long duration = between.toMinutes();
刘基明's avatar
刘基明 committed
102
        if (duration < 1) {
刘基明's avatar
刘基明 committed
103
            return "刚刚";
刘基明's avatar
刘基明 committed
104 105 106 107 108
        } else if (duration < 60) {
            return duration + "分钟前";
        } else if (duration < 60 * 24) {
            return duration / 60 + "小时前";
        } else if (start.getYear() == LocalDateTime.now().getYear()) {
刘基明's avatar
刘基明 committed
109 110 111
            return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm:ss"));
        }
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
刘基明's avatar
刘基明 committed
112 113
    }

刘基明's avatar
刘基明 committed
114
    // 点赞
刘基明's avatar
刘基明 committed
115
    public void like(String themeId, String userId) {
刘基明's avatar
刘基明 committed
116
        collectionService.addIfNotExist(themeId, userId, CollectionTypeEnum.LIKE);
刘基明's avatar
刘基明 committed
117 118
    }

张辰's avatar
张辰 committed
119 120 121 122
    public void unlike(String themeId, String userId) {
    }

    // 评论(对主题)
刘基明's avatar
刘基明 committed
123
    public void comment(CreateCommentReq req, String userId) {
刘基明's avatar
刘基明 committed
124
        CommentEntity commentEntity = CommentEntity.builder()
刘基明's avatar
刘基明 committed
125
                .targetId(req.getThemeId())
刘基明's avatar
刘基明 committed
126
                .authorId(userId)
刘基明's avatar
刘基明 committed
127 128
                .createBy(userId)
                .content(req.getComment())
刘基明's avatar
刘基明 committed
129 130 131
                .commentType(CommentTypeEnum.THEME.getCode())
                .build();

刘基明's avatar
刘基明 committed
132 133 134 135
        commentService.insertComment(commentEntity);
    }

    //转发
136 137 138 139 140 141 142 143 144 145
    public void forward(ForwardThemeDTO forwardThemeDTO, String userId) {
        ThemeEntity targetTheme = themeService.selectById(forwardThemeDTO.getFormerThemeId());
        ThemeEntity newTheme = ThemeEntity.builder()
                .content(targetTheme.getContent())
                .formerThemeId(targetTheme.getId())
                .authorId(userId)
                .createBy(userId)
                .build();

        themeService.insertTheme(newTheme);
刘基明's avatar
刘基明 committed
146 147 148 149
    }


    //收藏
张辰's avatar
张辰 committed
150
    public void favorite(String themeId, String userId) {
刘基明's avatar
刘基明 committed
151
        collectionService.addIfNotExist(themeId, userId, CollectionTypeEnum.BOOK);
张辰's avatar
张辰 committed
152 153 154 155
    }

    public void unFavorite(String themeId, String userId) {

刘基明's avatar
刘基明 committed
156
    }
张辰's avatar
张辰 committed
157

刘基明's avatar
刘基明 committed
158
    //投诉(主题)
刘基明's avatar
刘基明 committed
159
    public void complaint(String themeId, String user) {
刘基明's avatar
刘基明 committed
160
        //TODO
刘基明's avatar
刘基明 committed
161
    }
张辰's avatar
张辰 committed
162

刘基明's avatar
刘基明 committed
163 164
    //屏蔽(用户)
    public void blockUser(String themeId, String userId) {
165
        String blockId = themeService.selectById(themeId).getAuthorId();
张辰's avatar
张辰 committed
166
        blackListService.addBlock(blockId, userId, BlockTypeEnum.USER);
刘基明's avatar
刘基明 committed
167
    }
刘基明's avatar
刘基明 committed
168

张辰's avatar
张辰 committed
169 170 171 172 173 174 175
    // 解除屏蔽(用户)
    public void unblockUser(String themeId, String userId) {
    }

    // 屏蔽(主题)
    public void blockTheme(String themeId, String userId) {
        blackListService.addBlock(themeId, userId, BlockTypeEnum.THEME);
176
        BlackListEntity selectOne = blackListService.selectOne(userId, BlockTypeEnum.USER.getCode(), themeService.selectById(themeId).getAuthorId());
刘基明's avatar
刘基明 committed
177
        if (selectOne == null) {
178
            blackListService.addBlock(themeService.selectById(themeId).getAuthorId(), userId, BlockTypeEnum.USER);
刘基明's avatar
刘基明 committed
179
        }
刘基明's avatar
刘基明 committed
180
    }
刘基明's avatar
刘基明 committed
181

张辰's avatar
张辰 committed
182 183 184
    // 解除屏蔽(主题)
    public void unblockTheme(String themeId, String userId) {
        blackListService.removeBlackList(themeId, userId, BlockTypeEnum.USER);
刘基明's avatar
刘基明 committed
185
    }
张辰's avatar
张辰 committed
186 187 188 189 190 191 192 193 194

    //屏蔽(内容)
//    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);
//        }
//    }

刘基明's avatar
刘基明 committed
195

196 197

    //查询话题所需关联信息
刘基明's avatar
刘基明 committed
198 199
    private void fillDTOExtraInfo(ThemeQo themeQO, String userId) {
        List<ThemeAttachmentEntity> attachments = themeAttachmentService.selectByThemeId(themeQO.getId());
刘基明's avatar
刘基明 committed
200 201 202 203
        if (CollectionUtils.isEmpty(attachments)){
            return;
        }
        List<Object> obs = attachments.stream().map(themeAttachmentEntity -> transferAttachment(themeAttachmentEntity, userId)).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
204
        themeQO.setAttachment(obs);
刘基明's avatar
刘基明 committed
205
        //是否关注
刘基明's avatar
刘基明 committed
206 207 208 209 210
        themeQO.setUpToNowTime(calUpToNowTime(themeQO.getCreateTime()));
        String authorId = themeQO.getAuthorId();
        Set<String> fansSet = fansRelService.queryFansByFollowerId(userId).stream().collect(Collectors.toSet());
        if (fansSet.contains(authorId)) {
            themeQO.setFollow(true);
刘基明's avatar
刘基明 committed
211
        }else {
刘基明's avatar
刘基明 committed
212
            themeQO.setFollow(false);
刘基明's avatar
刘基明 committed
213
        }
刘基明's avatar
刘基明 committed
214 215 216 217
        HomePageEntity userEntity = homePageService.selectByUser(userId);
        themeQO.setUserImg(userEntity.getHeadImg());
        themeQO.setNickName(userEntity.getNickName());

刘基明's avatar
刘基明 committed
218 219 220 221
    }

    public Object transferAttachment(ThemeAttachmentEntity themeAttachment,String userId){
        switch (themeAttachment.getAttachType()) {
222 223 224
            //附件类型 1:产品 2:直播 3:短视频 4:课程
            //TODO ENTITY 转 DTO
            case 1:
刘基明's avatar
刘基明 committed
225
                return finProResService.selectById(themeAttachment.getAttchId());
226
            case 2:
刘基明's avatar
刘基明 committed
227
                return zhiboService.selectByid(themeAttachment.getAttchId());
228
            case 3:
刘基明's avatar
刘基明 committed
229 230
                CurriculumResEntity curriculumResEntity = curriculumResService.selectShortVideo(themeAttachment.getAttchId());
                return curriculumResEntity;
231
            case 4:
刘基明's avatar
刘基明 committed
232
                curriculumResEntity = curriculumResService.selectById(themeAttachment.getAttchId());
233
                //todo 栏目详情,需要判断当前用户是否购买
刘基明's avatar
刘基明 committed
234 235
                OrderFlowEntity orderFlowEntity = orderFlowService.queryByUserAndProId(themeAttachment.getThemeId(), userId);
                return curriculumResEntity;
236
            default:
刘基明's avatar
刘基明 committed
237
               return null;
238 239 240 241 242


        }
    }

刘基明's avatar
刘基明 committed
243

刘基明's avatar
刘基明 committed
244
}