课程说明
- 实现探花功能
- MongoDB geo
- 搜附近
- 上报地理位置
- 搜附近
1、探花
探花功能是将推荐的好友随机的通过卡片的形式展现出来,用户可以选择左滑、右滑操作,左滑:“不喜欢”,右滑:“喜欢”。
喜欢:如果双方喜欢,那么就会成为好友。
如果已经喜欢或不喜欢的用户在列表中不再显示。
1.1、查询推荐列表dubbo服务
1.1.1、实体对象
package com.tanhua.dubbo.server.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "user_like")
public class UserLike implements java.io.Serializable {
private static final long serialVersionUID = 6739966698394686523L;
private ObjectId id;
@Indexed
private Long userId; //用户id,自己
@Indexed
private Long likeUserId; //喜欢的用户id,对方
private Boolean isLike; // 是否喜欢
private Long created; //创建时间
private Long updated; // 更新时间
}
1.1.2、定义接口
RecommendUserApi
/**
* 查询探花列表,查询时需要排除喜欢和不喜欢的用户
*/
List<RecommendUser> queryCardsList(Long userId, int count);
1.1.3、编写实现
RecommendUserApiImpl
/**
* 查询探花列表,查询时需要排除喜欢和不喜欢的用户
* 1、排除喜欢,不喜欢的用户
* 2、随机展示
* 3、指定数量
*/
public List<RecommendUser> queryCardsList(Long userId, int counts) {
//1、查询喜欢不喜欢的用户ID
List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
List<Long> likeUserIdS = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);
//2、构造查询推荐用户的条件
Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIdS);
//3、使用统计函数,随机获取推荐的用户列表
TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
Aggregation.match(criteria),//指定查询条件
Aggregation.sample(counts)
);
AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
//4、构造返回
return results.getMappedResults();
}
1.1.4、单元测试
//com.tanhua.dubbo.server.api.TestRecommendUserApi
@Test
public void testQueryCardList(){
this.recommendUserApi.queryCardsList(106l, 10)
.forEach(recommendUser -> System.out.println(recommendUser));
}
1.2、查询推荐列表APP接口实现
接口文档:https://mock-java.itheima.net/project/35/interface/api/593
1.2.1、TanHuaController
/**
* 探花-推荐用户列表
*/
@GetMapping("/cards")
public ResponseEntity queryCardsList() {
List<TodayBest> list = this.tanhuaService.queryCardsList();
return ResponseEntity.ok(list);
}
1.2.2、TanHuaService
#默认推荐列表
tanhua:
default:
recommend:
users: 2,3,8,10,18,20,24,29,27,32,36,37,56,64,75,88
@Value("${tanhua.default.recommend.users}")
private String recommendUser;
//探花-推荐用户列表
public List<TodayBest> queryCardsList() {
//1、调用推荐API查询数据列表(排除喜欢/不喜欢的用户,数量限制)
List<RecommendUser> users = recommendUserApi.queryCardsList(UserHolder.getUserId(),10);
//2、判断数据是否存在,如果不存在,构造默认数据 1,2,3
if(CollUtil.isEmpty(users)) {
users = new ArrayList<>();
String[] userIdS = recommendUser.split(",");
for (String userId : userIdS) {
RecommendUser recommendUser = new RecommendUser();
recommendUser.setUserId(Convert.toLong(userId));
recommendUser.setToUserId(UserHolder.getUserId());
recommendUser.setScore(RandomUtil.randomDouble(60, 90));
users.add(recommendUser);
}
}
//3、构造VO
List<Long> ids = CollUtil.getFieldValues(users, "userId", Long.class);
Map<Long, UserInfo