探花功能
一、查询探花卡片推荐用户列表
TanHuaController
/**
* 左滑右滑之:查询推荐列表卡片列表
* 请求路径:/tanhua/cards
* 请求方式:get
* 响应数据:List<TodayBest>
*/
@GetMapping("/cards")
public ResponseEntity lookCardsList(){
List<TodayBest> list = tanHuaService.lookCardsList();
return ResponseEntity.ok(list);
}
TanHuaService
/**
* 左滑右滑之查看推荐卡片列表
* @return
*/
public List<TodayBest> lookCardsList() {
//1.每次查询10条推荐用户,排除已喜欢或已不喜欢的用户
List<RecommendUser> recommendUsers =recommendUserApi.lookCardsList(ThreadLocalUtils.getUserId(),10);
//2.如果推荐的用户为空,构造默认推荐用户列表
if(CollUtil.isEmpty(recommendUsers)){
recommendUsers = new ArrayList<>();
String[] userIds = defaultRecomment.split(",");
for (String userId : userIds) {
RecommendUser recommendUser = new RecommendUser();
recommendUser.setUserId(Convert.toLong(userId));
recommendUser.setToUserId(ThreadLocalUtils.getUserId());
recommendUser.setScore(RandomUtil.randomDouble(60, 90));
recommendUsers.add(recommendUser);
}
}
//3.批量获取推荐好友的用户id查询用户详情
List<Long> userIds = CollUtil.getFieldValues(recommendUsers, "userId", Long.class);
Map<Long, UserInfo> userInfoMap = userInfoApi.batchQueryUserInfo(userIds, null);
//4.遍历users集合,构造vo对象
List<TodayBest> todayBests = new ArrayList<>();
for (RecommendUser recommendUser : recommendUsers) {
Long id = recommendUser.getUserId();
UserInfo userInfo = userInfoMap.get(id);
if(userInfo != null){
TodayBest todayBest = TodayBest.init(userInfo, recommendUser);
todayBests.add(todayBest);
}
}
return todayBests;
}
api层
/**
*
*卡片的推荐好友数据列表
* 1.排除已喜欢或已不喜欢的用户
* 2.构建查询推荐好友的条件
* 3.
*/
public List<RecommendUser> lookCardsList(Long userId, int count) {
//1.根据用户的id查询用户已喜欢或不喜欢的用户
Criteria criteria = Criteria.where("userId").is(userId);
Query query = Query.query(criteria);
List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
//2.提取里面已喜欢或已不喜欢的用户id,用户已喜欢或已不喜欢的用户的id在User_like表是likeUserId字段
//在好友推荐表里是userId字段
List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
//3.构造查询推荐好友的条件,排除已喜欢或不喜欢的用户
Criteria nin = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);
//4.统计函数,随机推荐好友
TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(RecommendUser.class,
Aggregation.match(nin),//查询条件,注意,这个查询条件不能写成上面那个criteria
Aggregation.sample(count));//
AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
List<RecommendUser> mappedResults = results.getMappedResults();
return mappedResults;
}
二、右滑喜欢
思路:
代码实现
TanHuaController
/**
* 右滑,喜欢:
* 请求路径:/tanhua/:id/love
* 请求方式:get
* 请求参数:路径参数id,喜欢的用户id
* 响应数据:null
*
*/
@GetMapping("/{id}/love")
public ResponseEntity love(@PathVariable("id") Long likeUserId){
tanHuaService.love(likeUserId);//喜欢的用户id
return ResponseEntity.ok(null);
}
TanHuaService
@Autowired
private MessagesService messagesService;
/**
*右滑,喜欢
*/
public void love(Long likeUserId) {
//1.操作UserLike表,保存喜欢的数据到MongoDB
Boolean result = userLikeApi.saveOrUpdate(ThreadLocalUtils.getUserId(),likeUserId,true);
//2.判断是否保存成功
if(!result){
throw new BusinessException(ErrorResult.error());
}
//3.操作redis,首先将喜欢的人从不喜欢集合中删除,再将喜欢的的人添加到喜欢的集合数据
redisTemplate.opsForSet().remove(Constants.USER_NOT_LIKE_KEY + ThreadLocalUtils.getUserId(), likeUserId.toString());
redisTemplate.opsForSet().add(Constants.USER_LIKE_KEY + ThreadLocalUtils.getUserId(), likeUserId.toString());
//4.判断是否互相喜欢
Boolean like = isLike(likeUserId, ThreadLocalUtils.getUserId());
if(like){
//互相喜欢,添加为好友
messagesService.addFriend(likeUserId);
}
}
public Boolean isLike(Long userId,Long likeUserId ){
//
String key = Constants.USER_LIKE_KEY +userId;
//判断对方的喜欢集合里是否有我的id
return redisTemplate.opsForSet().isMember(key, likeUserId.toString());
}
UserLikeApiImpl
/**
*保存或或更新喜欢的用户信息
*/
public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
try {
//1.先查询UserLike表是否有数据
Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
Query query = Query.query(criteria);
UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
if(userLike == null){
//没有,保存数据
userLike = new UserLike();
userLike.setUserId(userId);
userLike.setLikeUserId(likeUserId);
userLike.setCreated(System.currentTimeMillis());
userLike.setIsLike(isLike);
mongoTemplate.save(userLike);
}else{
//有数据,更新数据
Update update = Update.update("isLike", isLike).set("updated", System.currentTimeMillis());
mongoTemplate.updateFirst(query, update, UserLike.class);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
三、左滑不喜欢
TanHuaController
/**
* 左滑,不喜欢
* 请求路径:/tanhua/:id/unlove
* 请求方式:get
* 请求参数:路径参数id,不喜欢的人的用户id
*/
@GetMapping("/{id}/unlove")
public ResponseEntity unLove(@PathVariable("id") Long unLikeUserId){
tanHuaService.unLove(unLikeUserId);
return ResponseEntity.ok(null);
}
TanHuaService
**
* 左滑不喜欢
* @param unLikeUserId
*/
public void unLove(Long unLikeUserId) {
//1.保存不喜欢的记录到数据库,操作的是mongoDb
Boolean result = userLikeApi.saveOrUpdate(ThreadLocalUtils.getUserId(), unLikeUserId, false);
if(!result){
throw new BusinessException(ErrorResult.error());
}
//2.操作redis,在喜欢的集合中剔除名单,在不喜欢的集合中添加名单
redisTemplate.opsForSet().remove(Constants.USER_LIKE_KEY+ThreadLocalUtils.getUserId(), unLikeUserId.toString());
redisTemplate.opsForSet().add(Constants.USER_NOT_LIKE_KEY+ThreadLocalUtils.getUserId(), unLikeUserId.toString());
标签:功能,likeUserId,22,userId,用户,Long,喜欢,探花,id
From: https://www.cnblogs.com/zhangdashuaige/p/16850918.html