需求简述
1)每个房屋详情点击一次,当前房屋ID就累加1次;
2)统计热门房屋通过Redis统计房屋访问量,统计热门房屋;
/**
* 查询房屋详情
* 查询关联经纪人
* @param id
* @return
*/
@RequestMapping("house/detail")
public String houseDetail(Long id,ModelMap modelMap){
House house = houseService.queryOneHouse(id);
HouseUser houseUser = houseService.getHouseUser(id);
recommendService.increase(id); // 房屋访问量增加
List comments = commentService.getHouseComments(id,8);
if (houseUser.getUserId() != null && !houseUser.getUserId().equals(0)) {
modelMap.put("agent", agencyService.getAgentDeail(houseUser.getUserId()));
}
List rcHouses = recommendService.getHotHouse(CommonConstants.RECOM_SIZE);
modelMap.put("recomHouses", rcHouses);
modelMap.put("house", house);
modelMap.put("commentList", comments);
return "/house/detail";
}
public void increase(Long id) {
try {
Jedis jedis = new Jedis("127.0.0.1");
jedis.zincrby(HOT_HOUSE_KEY, 1.0D, id + "");
jedis.zremrangeByRank(HOT_HOUSE_KEY, 0, -11);// 0代表第一个元素,-1代表最后一个元素,保留热度由低到高末尾10个房产
jedis.close();
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
说明:
1)访问房屋详情,此房屋推荐次数+1;
/**
* 1.实现分页
* 2.支持小区搜索、类型搜索
* 3.支持排序
* 4.支持展示图片、价格、标题、地址等信息
* @return
*/
@RequestMapping("/house/list")
public String houseList(Integer pageSize,Integer pageNum,House query,ModelMap modelMap){
PageData ps = houseService.queryHouse(query,PageParams.build(pageSize, pageNum));
List hotHouses = recommendService.getHotHouse(CommonConstants.RECOM_SIZE);
modelMap.put("recomHouses", hotHouses);
modelMap.put("ps", ps);
modelMap.put("vo", query);
return "house/listing";
}
说明:recommendService.getHotHouse()获取热门房屋集合信息;
public List getHot() {
try {
Jedis jedis = new Jedis("127.0.0.1");
Set idSet = jedis.zrevrange(HOT_HOUSE_KEY, 0, -1);
jedis.close();
List ids = idSet.stream().map(Long::parseLong).collect(Collectors.toList());
return ids;
} catch (Exception e) {
logger.error(e.getMessage(), e);//有同学反应在未安装redis时会报500,在这里做下兼容,
return Lists.newArrayList();
}
}
说明:
1)从Redis获取统计热门房屋ID;
2)jedis.zrevrange()降序排列获取所有统计的房屋访问次数;
public List getHotHouse(Integer size) {
House query = new House();
List list = getHot();
list = list.subList(0, Math.min(list.size(), size));
if (list.isEmpty()) {
return Lists.newArrayList();
}
query.setIds(list);
final List order = list;
List houses = houseService.queryAndSetImg(query, PageParams.build(size, 1));
Ordering houseSort = Ordering.natural().onResultOf(hs -> { 根据房屋ID在order中的索引大小进行排序
return order.indexOf(hs.getId());
});
return houseSort.sortedCopy(houses);
}
说明:
1)取一定数量的热门房屋ID;
2)根据order中的集合进行排序;
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================