1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.FollowRelEntity;
import com.tanpu.community.dao.mapper.community.FollowRelMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@EnableCaching
@Service
public class FollowRelService {
@Resource
private FollowRelMapper followRelMapper;
public List<String> queryIdolsByFansId(String followerId) {
return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getFansId, followerId)
.eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
.stream().map(FollowRelEntity::getIdolId)
.collect(Collectors.toList());
}
public Page<String> queryIdolsByFansId(String fansId, Integer pageNum, Integer pageSize) {
Integer pageStart = (pageNum - 1) * pageSize;
List<FollowRelEntity> idols = followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getFansId, fansId)
.last("limit " + pageStart + ", " + pageSize)
.eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
List<String> list = idols.stream().map(FollowRelEntity::getIdolId)
.collect(Collectors.toList());
Integer totalSize = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFansId,fansId)
.eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
Page<String> tPage = new Page<>(pageNum, pageSize, (long) totalSize, 0, list);
int totalPage = list.size() / pageSize;
if (list.size() % pageSize == 0) {
tPage.setTotalPages(totalPage);
} else {
tPage.setTotalPages(totalPage + 1);
}
return tPage;
}
public Page<String> queryFansByIdolId(String idolId, Integer pageNum, Integer pageSize) {
Integer pageStart = (pageNum - 1) * pageSize;
LambdaQueryWrapper<FollowRelEntity> queryWrapper = new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getIdolId, idolId)
.last("limit " + pageStart + ", " + pageSize)
.eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
List<String> list = followRelMapper.selectList(queryWrapper)
.stream().map(FollowRelEntity::getFansId).collect(Collectors.toList());
// 分页
Integer totalSize = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getIdolId, idolId)
.eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
Page<String> tPage = new Page<>(pageNum, pageSize, (long) totalSize, 0, list);
int totalPage = list.size() / pageSize;
if (list.size() % pageSize == 0) {
tPage.setTotalPages(totalPage);
} else {
tPage.setTotalPages(totalPage + 1);
}
return tPage;
}
@Transactional
public boolean addFollowRel(String idolId, String followerId) {
FollowRelEntity searchResult = queryRecord(idolId, followerId);
if (searchResult == null) {
FollowRelEntity entity = FollowRelEntity.builder()
.idolId(idolId)
.fansId(followerId)
.followTime(LocalDateTime.now())
.build();
followRelMapper.insert(entity);
return true;
} else {
searchResult.setFollowTime(LocalDateTime.now());
searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
followRelMapper.updateById(searchResult);
return false;
}
}
/**
* 逻辑删除关注关系
*
* @param idolId
* @param followerId
*/
@Transactional
public void deleteFollowRel(String idolId, String followerId) {
FollowRelEntity searchResult = queryRecord(idolId, followerId);
if (searchResult == null) {
throw new BizException("未找到关注关系");
}
searchResult.setUnfollowTime(LocalDateTime.now());
searchResult.setDeleteTag(DeleteTagEnum.DELETED.getCode());
followRelMapper.updateById(searchResult);
}
public FollowRelEntity queryRecord(String idolId, String followerId) {
return followRelMapper.selectOne(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getIdolId, idolId)
.eq(FollowRelEntity::getFansId, followerId));
}
public boolean checkFollow(String idolId, String followerId) {
return followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getIdolId, idolId)
.eq(FollowRelEntity::getFansId, followerId)
.eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
> 0;
}
}