目录
1:首页推荐
1.1:接口地址
1.2:流程分析
1.3:代码实现
2:今日佳人
1.1:接口地址
1.2:流程分析
1.3:代码实现
3:佳人信息
1.1:接口地址
1.2:流程分析
1.3:代码实现
1:首页推荐
1.1:接口地址
1.2:流程分析
1.3:代码实现
与前端交互的app-server模块
controller层实现
@GetMapping("/recommendation")
public ResponseEntity findAllrecommendation(RecommendPageVo recommendPageVo){
ResultList allrecommendation = recommendService.findAllrecommendation(recommendPageVo, UserHolder.getUserId());
return ResponseEntity.ok(allrecommendation);
}
service层实现
@DubboReference
private RecommendUserApi recommendUserApi;
@DubboReference
private UserInfoApi userInfoApi;
public ResultList findAllrecommendation(RecommendPageVo recommendPageVo, Long userId) {
ResultList allrecommendation = recommendUserApi.findAllrecommendation(recommendPageVo, userId);
List<RecommendUser> items = (List<RecommendUser>)allrecommendation.getItems();
//根据ids查询详情
List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
//构建条件
UserInfo userInfo = new UserInfo();
userInfo.setGender(recommendPageVo.getGender());
userInfo.setAge(Integer.valueOf(recommendPageVo.getAge()));
System.out.println(userInfo);
Map<Long, UserInfo> map = userInfoApi.findByids(ids, userInfo);
List<UserInfoVo> userInfoList = new ArrayList<>();
for (RecommendUser item : items) {
UserInfo userInfo1 = map.get(item.getUserId());
UserInfoVo userInfoVo = new UserInfoVo();
if (userInfo1 !=null){
userInfoVo.setId(userInfo1.getId());
userInfoVo.setAge(userInfo1.getAge());
userInfoVo.setAvatar(userInfo1.getAvatar());
userInfoVo.setGender(userInfo1.getGender());
userInfoVo.setFateValue(item.getScore().intValue());
userInfoVo.setNickname(userInfo1.getNickname());
String[] split = userInfo1.getTags().split(",");
userInfoVo.setTags(split);
userInfoList.add(userInfoVo);
}
}
allrecommendation.setItems(userInfoList);
return allrecommendation;
}
统一封装接口的模块
/**
* @Author 爱吃豆的土豆、
* @Date 2023/4/7 16:09
*/
public interface RecommendUserApi {
ResultList findAllrecommendation(RecommendPageVo recommendPageVo, Long userId);
UserInfoVo findtodayBest(Long userId);
UserInfoVo findUserInfo(Long id, Long userId);
}
mongo提供者模块(提供相关接口的实现)
@Override
public ResultList findAllrecommendation(RecommendPageVo recommendPageVo, Long userId) {
Criteria criteria = Criteria.where("toUserId").is(userId);
//查询总数
Query query = new Query(criteria);
long count = mongoTemplate.count(query, RecommendUser.class);
//查询当前页的数据列表
Query querylimit = new Query(criteria).limit(recommendPageVo.getPagesize()).skip((recommendPageVo.getPage() - 1)*recommendPageVo.getPagesize()).with(Sort.by(Sort.Order.desc("score")));
List<RecommendUser> recommendUserList = mongoTemplate.find(querylimit, RecommendUser.class);
ResultList resultList = new ResultList<>();
resultList.setItems(recommendUserList);
//设置列表
//设置当前页码
resultList.setPage(recommendPageVo.getPage());
//设置总记录数
resultList.setCounts((int) count);
//设置页大小
resultList.setPagesize(recommendPageVo.getPagesize());
//设置总页数
resultList.setPages((int) (count/recommendPageVo.getPagesize()));
return resultList;
}
2:今日佳人
1.1:接口地址
1.2:流程分析
缘分值是根据平台算法推算出的缘分值得分,将用户的缘分值存放到mongodb数据库中,这里通过当前登录用户的id,在系统中找到该用户的所有推荐用户,将推荐用户的缘分值进行降序找到最高缘分值用户,进行展示今日佳人。
1.3:代码实现
与前端交互的app-server模块
controller层实现
@GetMapping("/todayBest")
public ResponseEntity findtodayBest(){
UserInfoVo userInfoVo = recommendService.findtodayBest(UserHolder.getUserId());
return ResponseEntity.ok(userInfoVo);
}
service层实现
public UserInfoVo findtodayBest(Long userId) {
UserInfoVo userInfoVo = recommendUserApi.findtodayBest(userId);
return userInfoVo;
}
统一封装接口的模块
UserInfoVo findtodayBest(Long userId);
提供者模块(提供相关接口的实现)
@Override
public UserInfoVo findtodayBest(Long userId) {
Query query = new Query();
//创建等值判断
Criteria criteria = Criteria.where("toUserId").is(userId);
Sort by = Sort.by(Sort.Order.desc("score"));
query.addCriteria(criteria);
query.with(by);
RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
//查询数据
UserInfo userInfo = userInfoMapper.selectById(one.getUserId());
UserInfoVo userInfoVo = new UserInfoVo();
userInfoVo.setId(userInfo.getId());
userInfoVo.setTags(userInfo.getTags().split(","));
userInfoVo.setAge(userInfo.getAge());
userInfoVo.setNickname(userInfo.getNickname());
userInfoVo.setGender(userInfo.getGender());
userInfoVo.setAvatar(userInfo.getAvatar());
userInfoVo.setFateValue(one.getScore().intValue());
return userInfoVo;
}
3:佳人信息
1.1:接口地址
1.2:流程分析
通过接收到前端传入的用户id,在MySQL数据库中查询找到对应的用户信息封装返回。
1.3:代码实现
与前端交互的app-server模块
controller层实现
@GetMapping("/{id}/personalInfo")
public ResponseEntity personalInfo(@PathVariable("id") Long id){
UserInfoVo userInfoVo = recommendService.findUserInfo(id,UserHolder.getUserId());
return ResponseEntity.ok(userInfoVo);
}
service层实现
public UserInfoVo findUserInfo(Long id, Long userId) {
if (id == userId){
UserInfo user = userInfoApi.findUser(userId);
UserInfoVo userInfoVo = UserInfoVo.init(user, 100);
return userInfoVo;
}
return recommendUserApi.findUserInfo(id,userId);
}
UserInfoVo.init()静态方法:在对象内部处理信息的封装
public static UserInfoVo init(UserInfo userInfo,Integer fateValue) {
UserInfoVo userInfoVo = new UserInfoVo();
BeanUtils.copyProperties(userInfo,userInfoVo);
String[] split = userInfo.getTags().split(",");
userInfoVo.tags = split;
userInfoVo.setFateValue(fateValue);
return userInfoVo;
}
统一封装接口的模块文章来源:https://www.toymoban.com/news/detail-419853.html
public UserInfo findUser(Long id);
提供者模块(提供相关接口的实现)文章来源地址https://www.toymoban.com/news/detail-419853.html
@Override
public UserInfo findUser(Long id) {
UserInfo userInfo = this.userInfoMapper.selectById(id);
return userInfo;
}
到了这里,关于交友项目【首页推荐,今日佳人,佳人信息】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!