POJO
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = “visitors”)
public class Visitors implements java.io.Serializable{
private static final long serialVersionUID = 2811682148052386573L;
private ObjectId id;
private Long userId; //我的id
private Long visitorUserId; //来访用户id
private String from; //来源,如首页、圈子等
private Long date; //来访时间
private Double score; //得分
}
====================================================
API
import com.tanhua.dubbo.server.pojo.Visitors;
import java.util.List;
public interface VisitorsApi {
/**
*
* @param userId 我的id
* @param visitorUserId 访客id
* @param from 来源
* @return
*/
String saveVisitor(Long userId, Long visitorUserId, String from);
/**
* 查询我的访客数据,存在2种情况:
* 1. 我没有看过我的访客数据,返回前5个访客信息
* 2. 之前看过我的访客,从上一次查看的时间点往后查询5个访客数据
*
* @param userId
* @return
*/
List queryMyVisitor(Long userId);
}
==============================================================
API IMPL
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.pojo.Visitors;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@Service(version = “1.0.0”)
public class VisitorsApiImpl implements VisitorsApi {
@Autowired
private MongoTemplate mongoTemplate;
private static final String VISITOR_REDIS_KEY = “VISITOR_USER”;
@Autowired
private RedisTemplate redisTemplate;
@Override
public String saveVisitor(Long userId, Long visitorUserId, String from) {
//校验
if (!ObjectUtil.isAllNotEmpty(userId, visitorUserId, from)) {
return null;
}
//查询访客用户在今天是否已经记录过,如果已经记录过,不再记录
String today = DateUtil.today();
Long minDate = DateUtil.parseDateTime(today + ” 00:00:00″).getTime();
Long maxDate = DateUtil.parseDateTime(today + ” 23:59:59″).getTime();
Query query = Query.query(Criteria.where(“userId”).is(userId)
.and(“visitorUserId”).is(visitorUserId)
.andOperator(Criteria.where(“date”).gte(minDate),
Criteria.where(“date”).lte(maxDate)
)
);
long count = this.mongoTemplate.count(query, Visitors.class);
if (count > 0) {
//今天已经记录过的
return null;
}
Visitors visitors = new Visitors();
visitors.setFrom(from);
visitors.setVisitorUserId(visitorUserId);
visitors.setUserId(userId);
visitors.setDate(System.currentTimeMillis());
visitors.setId(ObjectId.get());
//存储数据
this.mongoTemplate.save(visitors);
return visitors.getId().toHexString();
}
@Override
public List queryMyVisitor(Long userId) {
// 查询前5个访客数据,按照访问时间倒序排序
// 如果用户已经查询过列表,记录查询时间,后续查询需要按照这个时间往后查询
// 上一次查询列表的时间
Long date = Convert.toLong(this.redisTemplate.opsForHash().get(VISITOR_REDIS_KEY, String.valueOf(userId)));
PageRequest pageRequest = PageRequest.of(0, 5, Sort.by(Sort.Order.desc(“date”)));
Query query = Query.query(Criteria.where(“userId”).is(userId))
.with(pageRequest);
if (ObjectUtil.isNotEmpty(date)) {
query.addCriteria(Criteria.where(“date”).gte(date));
}
List visitorsList = this.mongoTemplate.find(query, Visitors.class);
//查询每个来访用户的得分
for (Visitors visitors : visitorsList) {
Query queryScore = Query.query(Criteria.where(“toUserId”)
.is(userId).and(“userId”).is(visitors.getVisitorUserId())
);
RecommendUser recommendUser = this.mongoTemplate.findOne(queryScore, RecommendUser.class);
if(ObjectUtil.isNotEmpty(recommendUser)){
visitors.setScore(recommendUser.getScore());
}else {
//默认得分
visitors.setScore(90d);
}
}
return visitorsList;
}
}
==============================================================
APP VO API
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VisitorsVo {
private Long id;
private String avatar;
private String nickname;
private String gender;
private Integer age;
private String[] tags;
private Integer fateValue;
}
============================================================
controller
/**
* 谁看过我
*
* @return
*/
@GetMapping(“visitors”)
public ResponseEntity queryVisitorsList(){
try {
List list = this.quanZiService.queryVisitorsList();
return ResponseEntity.ok(list);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
==============================================================
service 核心
public List queryVisitorsList() {
User user = UserThreadLocal.get();
List visitorsList = this.visitorsApi.queryMyVisitor(user.getId());
if (CollUtil.isEmpty(visitorsList)) {
return Collections.emptyList();
}
List userIds = CollUtil.getFieldValues(visitorsList, “visitorUserId”);
List userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);
List visitorsVoList = new ArrayList();
for (Visitors visitor : visitorsList) {
for (UserInfo userInfo : userInfoList) {
if (ObjectUtil.equals(visitor.getVisitorUserId(), userInfo.getUserId())) {
VisitorsVo visitorsVo = new VisitorsVo();
visitorsVo.setAge(userInfo.getAge());
visitorsVo.setAvatar(userInfo.getLogo());
visitorsVo.setGender(userInfo.getSex().name().toLowerCase());
visitorsVo.setId(userInfo.getUserId());
visitorsVo.setNickname(userInfo.getNickName());
visitorsVo.setTags(StringUtils.split(userInfo.getTags(), ‘,’));
visitorsVo.setFateValue(visitor.getScore().intValue());
visitorsVoList.add(visitorsVo);
break;
}
}
}
return visitorsVoList;
}
=============================================================
service 记录来访者 数据
public TodayBest queryUserInfo(Long userId) {
UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(userId);
if(ObjectUtil.isEmpty(userInfo)){
return null;
}
TodayBest todayBest = new TodayBest();
todayBest.setId(userId);
todayBest.setAge(userInfo.getAge());
todayBest.setGender(userInfo.getSex().name().toLowerCase());
todayBest.setNickname(userInfo.getNickName());
todayBest.setTags(Convert.toStrArray(StrUtil.split(userInfo.getTags(),’,’)));
todayBest.setAvatar(userInfo.getLogo());
//缘分值
User user = UserThreadLocal.get();
todayBest.setFateValue(this.recommendUserService.queryScore(userId, user.getId()).longValue());
//记录来访用户
this.visitorsApi.saveVisitor(userId, user.getId(), “个人主页”);
return todayBest;
}
=========================================================