附近商户和用户签到

ops/2025/3/4 11:59:46/

附近商户

当点击美食按钮时,发送请求:
http://127.0.0.1:8080/api/shop/of/type?&typeId=1&current=1&x=120.149993&y=30.334229
其中,typeId=1表示美食类型,current=1表示页码为1,x=120.149993&y=30.334229即为经纬坐标

导入数据

redis 提供了一种数据结构,即地理空间(Geospatial):用于存储地理位置信息,适合于地理位置查询、距离计算等场景。

java">GEOADD places 116.403963 39.915119 "Beijing"
GEODIST places "Beijing" "Shanghai" km  # 计算两地距离

在这里插入图片描述
按照商户类型做分组,类型相同的商户作为同一组,以typeId为key存入同一个GEO集合

java">@Test
void loadShopData() {// 1.查询店铺信息List<Shop> list = shopService.list();// 2.把店铺分组,按照typeId分组,typeId一致的放到一个集合Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));// 3.分批完成写入Redisfor (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {// 3.1.获取类型idLong typeId = entry.getKey();String key = SHOP_GEO_KEY + typeId;// 3.2.获取同类型的店铺的集合List<Shop> value = entry.getValue();List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());// 3.3.写入redis GEOADD key 经度 纬度 memberfor (Shop shop : value) {// stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shop.getId().toString());locations.add(new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getX(), shop.getY())));}stringRedisTemplate.opsForGeo().add(key, locations);}
}

实现附近商户

修改 ShopController 的 queryShopByType 函数:

java">@GetMapping("/of/type")
public Result queryShopByType(@RequestParam("typeId") Integer typeId,@RequestParam(value = "current", defaultValue = "1") Integer current,@RequestParam(value = "x", required = false) Double x,@RequestParam(value = "y", required = false) Double y
) {return shopService.queryShopByType(typeId, current, x, y);
}

ShopService 的具体实现 ShopServiceImpl 中完成 queryShopByType
如果传入的参数有经纬坐标,先根据类型查询 redis ,否则直接查询数据库

java">@Overridepublic Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {// 1.判断是否需要根据坐标查询if (x == null || y == null) {// 不需要坐标查询,按数据库查询Page<Shop> page = query().eq("type_id", typeId).page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));// 返回数据return Result.ok(page.getRecords());}// 2.计算分页参数int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;int end = current * SystemConstants.DEFAULT_PAGE_SIZE;// 3.查询redis、按照距离排序、分页。结果:shopId、distanceString key = SHOP_GEO_KEY + typeId;GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo() // GEOSEARCH key BYLONLAT x y BYRADIUS 10 WITHDISTANCE.search(key,GeoReference.fromCoordinate(x, y),new Distance(5000),RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));// 4.解析出idif (results == null) {return Result.ok(Collections.emptyList());}List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();if (list.size() <= from) {// 没有下一页了,结束return Result.ok(Collections.emptyList());}// 4.1.截取 from ~ end的部分List<Long> ids = new ArrayList<>(list.size());Map<String, Distance> distanceMap = new HashMap<>(list.size());list.stream().skip(from).forEach(result -> {// 4.2.获取店铺idString shopIdStr = result.getContent().getName();ids.add(Long.valueOf(shopIdStr));// 4.3.获取距离Distance distance = result.getDistance();distanceMap.put(shopIdStr, distance);});// 5.根据id查询ShopString idStr = StrUtil.join(",", ids);List<Shop> shops = query().in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list();for (Shop shop : shops) {shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());}// 6.返回return Result.ok(shops);}

用户签到

统计用户签到,可以选择建立一张表,签到一次插入一次数据,但这样非常占空间
用0和1标示业务状态,这种思路就称为位图(BitMap)。Redis中是利用string类型数据结构实现BitMap,因此最大上限是512M,转换为bit则是 2^32个bit位。
在这里插入图片描述
示例:

# 将 user:online 这个 Bitmap 中,第 0 位设置为 1(在线)
SETBIT user:online 0 1

实现签到

UserController 中添加 sign 签到功能函数

java">@PostMapping("/sign")public Result sign(){return userService.sign();}

UerService 的具体实现 UserServiceImpl 中完成 sign 函数,将 userID 作为 key,然后设置对应索引位置为 1,表示当天签到

java">@Override
public Result sign() {// 1.获取当前登录用户Long userId = UserHolder.getUser().getId();// 2.获取日期LocalDateTime now = LocalDateTime.now();// 3.拼接keyString keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));String key = USER_SIGN_KEY + userId + keySuffix;// 4.获取今天是本月的第几天int dayOfMonth = now.getDayOfMonth();// 5.写入Redis SETBIT key offset 1stringRedisTemplate.opsForValue().setBit(key, dayOfMonth - 1, true);return Result.ok();
}

连续签到统计

首先获得今天是该月第几天,然后将对应索引位置之前的返回,然后循环与 1 做 与(&) 运算,结果为 0 时结束循环,计算循环次数

UserController 中添加 signCount 签到统计功能函数

java">@GetMapping("/sign/count")
public Result signCount(){return userService.signCount();
}

UerService 的具体实现 UserServiceImpl 中完成 signCount 函数

java">@Override
public Result signCount() {// 1.获取当前登录用户Long userId = UserHolder.getUser().getId();// 2.获取日期LocalDateTime now = LocalDateTime.now();// 3.拼接keyString keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));String key = USER_SIGN_KEY + userId + keySuffix;// 4.获取今天是本月的第几天int dayOfMonth = now.getDayOfMonth();// 5.获取本月截止今天为止的所有的签到记录,返回的是一个十进制的数字 BITFIELD sign:5:202203 GET u14 0List<Long> result = stringRedisTemplate.opsForValue().bitField(key,BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0));if (result == null || result.isEmpty()) {// 没有任何签到结果return Result.ok(0);}Long num = result.get(0);if (num == null || num == 0) {return Result.ok(0);}// 6.循环遍历int count = 0;while (true) {// 6.1.让这个数字与1做与运算,得到数字的最后一个bit位  // 判断这个bit位是否为0if ((num & 1) == 0) {// 如果为0,说明未签到,结束break;}else {// 如果不为0,说明已签到,计数器+1count++;}// 把数字右移一位,抛弃最后一个bit位,继续下一个bit位num >>>= 1;}return Result.ok(count);
}

http://www.ppmy.cn/ops/163035.html

相关文章

element UI => element Plus 差异化整理

注&#xff1a;文章由deepSeek生成&#xff1b; 以下是 Element UI 和 Element Plus 中 有变化的组件属性差异 的详细对比。这些变化主要集中在 Vue 3 的适配、API 优化以及新特性的引入。 1. Button 组件 (el-button) 属性名Element UIElement Plus差异说明iconicon"el-…

跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围

一 查看springboot自带的tomcat版本&#xff1a; 可直接在项目中找到Maven Dependencies中找到tomcat版本 二、查看SpringBoot内置tomcat版本的支持范围 我这边是跟据maven仓库查看的 首先跟据链接打开maven仓库&#xff1a;https://mvnrepository.com/ 然后搜索&#xff1a…

使用DeepSeek+KIMI生成高质量PPT

一、使用DeepSeek DeepSeek官网&#xff1a;DeepSeek 点击“开始对话”&#xff0c;进入交互页面。 在上图中&#xff0c;输入问题&#xff0c;即可获取AI生成的结果。 基础模型&#xff08;V3&#xff09;&#xff1a;通用模型&#xff08;2024.12&#xff09;&#xff0c;高…

本地部署 DeepSeek:从 Ollama 配置到 Spring Boot 集成

前言 随着人工智能技术的迅猛发展&#xff0c;越来越多的开发者希望在本地环境中部署和调用 AI 模型&#xff0c;以满足特定的业务需求。本文将详细介绍如何在本地环境中使用 Ollama 配置 DeepSeek 模型&#xff0c;并在 IntelliJ IDEA 中创建一个 Spring Boot 项目来调用该模型…

8295智能座舱弹窗点击问题,点击window之外的区域,window不消失的问题。touchableRegion的问题分析(android 13)

1.问题描述 在项目开发过程中&#xff0c;遇到input的问题。用户点击status bar的Wifi图标之后&#xff0c;会弹出wifi列表的window&#xff0c;而点击这个window之外的区域&#xff0c;wifi列表的窗口不会消失的问题。 2. 问题分析定位 分析触摸问题&#xff0c;必不可少的会…

攻防世界WEB(新手模式)18-easyphp

打开题目&#xff0c;直接开始代码审计 条件1&#xff1a;$a 必须存在&#xff0c;且 intval($a) 必须大于 6000000&#xff0c;同时 strlen($a) 必须小于等于 3。 这意味着 $a 必须是一个字符串&#xff0c;且它的整数值大于 6000000&#xff0c;但字符串长度不能超过 3。这看…

JS宏进阶:数据分类之逻辑回归

一、逻辑回归介绍 逻辑回归(Logistic Regression)是一种用于解决分类问题的统计学习方法,特别是适用于二分类问题。 1、原理 线性模型与逻辑函数:逻辑回归基于线性回归的概念,但通过使用逻辑函数(也称为 sigmoid 函数)将线性模型的输出映射到 [0, 1] 的概率范围内。这…

机器学习:特征提取

介绍 &#xff08;一&#xff09;原理 特征提取的核心概念是将高维、复杂的原始数据转换为低维且具有代表性的特征集合。原始数据往往包含大量冗余或无关信息&#xff0c;直接使用这些数据进行模型训练不仅会增加计算成本&#xff0c;还可能导致模型性能下降。通过特征提取&a…