互相喜欢,喜欢,粉丝 - 统计
思路分析
代码实现,首先构造vo返回对象,里面封装的是统计的数据数量
package com.tanhua.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CountsVo implements Serializable {
private Long eachLoveCount;//(互相喜欢数);
private Long loveCount;//(喜欢),
private Long fanCount;//(粉丝),
}
Controller
/**
* 互相喜欢,喜欢,粉丝 - 统计
* 请求路径:/users/counts
* 请求方式:get
* 响应数据: Integer eachLoveCount(互相喜欢数);
* Integer loveCount(喜欢),
* Integer fanCount(粉丝),
*/
@GetMapping("/counts")
public ResponseEntity getNumber(){
CountsVo countsVo = service.getNumber();
return ResponseEntity.ok(countsVo);
}
Service
/**
*
*互相喜欢,喜欢,粉丝 - 统计
*/
public CountsVo getNumber() {
//1.先获取当前用户id
Long userId = ThreadLocalUtils.getUserId();
//1. 根据当前用户id查询好友表 互相喜欢数量
Long eachLoveCount = friendApi.findEachLoveCount(userId);
//2 根据当前用户id查询喜欢表 喜欢数量
Long loveCount = userLikeApi.findLoveCount(userId);
//3 根据当前用户id查询喜欢表 粉丝数量
Long fanCount = userLikeApi.findFanCount(userId);
return new CountsVo(eachLoveCount,loveCount,fanCount);
}
FriendApiImpl
好友数量就是互相喜欢数量
/**
*
*查询好友数量
*/
public Long findEachLoveCount(Long userId) {
//构造查询条件
Criteria criteria = Criteria.where("userId").is(userId);
Query query = Query.query(criteria);
//2.
long count = mongoTemplate.count(query, Friend.class);
return count;
}
UserLikeApiImpl
/**
*查询喜欢的数量
*/
public Long findLoveCount(Long userId) {
Boolean b = true;
Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(b);
Query query = Query.query(criteria);
long count = mongoTemplate.count(query, UserLike.class);
return count;
}
/**
* 查询粉丝数量
*
*/
public Long findFanCount(Long userId) {
Criteria criteria = Criteria.where("likeUserId").is(userId).and("idLike").is(true);
Query query = Query.query(criteria);
long count = mongoTemplate.count(query, UserLike.class);
return count;
}
标签:24,count,粉丝,userId,Long,喜欢,query
From: https://www.cnblogs.com/zhangdashuaige/p/16851775.html