From 2f1dfec3e583d76c8e3501d547a43cff5485b941 Mon Sep 17 00:00:00 2001 From: gwbvipvip Date: Tue, 19 Mar 2024 17:39:54 +0800 Subject: [PATCH] redis --- .../WeighOrder/Service/WeighOrderService.cs | 136 +++++++++++++++++- .../BPA.KitChen.GroupMealOrder.Core.csproj | 3 +- .../Cache/RedisHelper.cs | 39 +++++ BPA.KitChen.GroupMealOrder/appsettings.json | 33 +---- .../Service/WechatNotityService.cs | 5 +- 5 files changed, 179 insertions(+), 37 deletions(-) create mode 100644 BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs diff --git a/BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs b/BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs index b605d17..123f1b6 100644 --- a/BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs +++ b/BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs @@ -24,6 +24,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Org.BouncyCastle.Crypto; +using BPA.KitChen.GroupMealOrder.Core.Cache; +using NPOI.XSSF.Streaming.Values; +using StackExchange.Redis; namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service { @@ -43,6 +46,8 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service /// public async Task CreateWeighOrder(WeighOrderCreteDto inputDto) { + + var diningPlateInfo = await GetDiningPlateInfoByQRCode(inputDto.QRCode); if (diningPlateInfo == null || diningPlateInfo.Status != 0) { @@ -111,6 +116,7 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service OrderId = order.Id, }); + RedisHelper.RedisConn.GetDatabase().StringSet("order" + inputDto.CreateId, JsonConvert.SerializeObject(result)); return result; } @@ -192,6 +198,11 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service } + //写入redis + Task.Run(async () => + { + await AddRedis(ids); + }); return res > 0; } @@ -264,6 +275,13 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service }); var res = await db.Insertable(diningPlate).ExecuteCommandAsync(); + + ///写入redis + + Task.Run(async () => + { + await AddRedis(inputDto.OrderId); + }); return res > 0; } @@ -468,6 +486,8 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service { throw Oops.Oh("请求错误"); } + + RedisHelper.RedisConn.GetDatabase().KeyDelete("order" + result.CreateId); return res > 0; } catch (Exception e) @@ -486,14 +506,19 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service { var list = new List(); + + foreach (var item in inputDto) { + //查询订单 + var weighOrder = await db.Queryable().FirstAsync(x => x.Id == item.WeighOrderId); list.Add(new BPA_WeighOrderDiningPlateUnbindRecord() { CreateAt = DateTime.Now, DiningPlateId = item.DiningPlateId, WeighOrderId = item.WeighOrderId, }); + RedisHelper.RedisConn.GetDatabase().KeyDelete("order" + weighOrder.CreateId); } if (list.Count > 0) @@ -501,7 +526,6 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service var res = await db.Insertable(list).ExecuteCommandAsync(); return res > 0; } - return false; } @@ -611,7 +635,15 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service /// public async Task GetWeighOrderByOpenId(GetWeighOrderByOpenIdInputDto inputDto) { - var res = await db.Queryable() + var res = new WeighOrderDto(); + var orderRedis = RedisHelper.RedisConn.GetDatabase().StringGet("order" + inputDto.OpenId); + if (!string.IsNullOrEmpty(orderRedis)) + { + res = JsonConvert.DeserializeObject(orderRedis); + return res; + } + + res = await db.Queryable() .Where(x => x.CreateId == inputDto.OpenId) .OrderBy(x => x.CreateAt, OrderByType.Desc) .Select(t => new WeighOrderDto @@ -700,14 +732,12 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service }) .ToList(); } - + RedisHelper.RedisConn.GetDatabase().StringSet("order" + res.CreateId, JsonConvert.SerializeObject(res)); return res; } - - /// /// 称重订单分页 /// @@ -919,5 +949,101 @@ namespace BPA.KitChen.GroupMealOrder.Application.Service.WeighOrder.Service } + + public async Task AddRedis( string orderId) + { + var res = new WeighOrderDto(); + + res = await db.Queryable() + .Where(x => x.Id == orderId) + .OrderBy(x => x.CreateAt, OrderByType.Desc) + .Select(t => new WeighOrderDto + { + Id = t.Id.SelectAll(), + }).FirstAsync(); + if (res == null || res.States == 1) + { + throw Oops.Oh("无数据"); + } + + + + //查询订单商品 + var orderGoods = await db.Queryable() + .Where(x => x.OrderId == res.Id) + .ToListAsync(); + if (orderGoods != null) + { + res.GoodsInfo = orderGoods.Select(x => new WeighOrderGoodsDto() + { + GoodsId = x.GoodsId, + GoodsAttribute = string.IsNullOrEmpty(x.GoodsAttribute) ? null : JsonConvert.DeserializeObject>(x.GoodsAttribute), + GoodsName = x.GoodsName, + GoodsWeight = x.GoodsWeight, + GroupId = x.GroupId, + Id = x.Id, + OrderId = x.OrderId, + DiningPlateId = x.DiningPlateId, + TotalAmount = x.TotalAmount, + }).ToList(); + } + + + + //查询订单餐盘 + var diningPlate = await db.Queryable() + .Where(x => x.WeighOrderId == res.Id) + .Select(x => new WeighOrderDiningPlateDto() + { + DiningPlateId = x.DiningPlateId, + OrderId = x.WeighOrderId, + ChipCode = x.ChipCode, + QRCode = x.QRCode, + }).ToListAsync(); + + var ids = diningPlate.Select(x => x.DiningPlateId).ToList(); + //查询解绑产盘 + var unbindRed = db.Queryable() + .Where(x => x.WeighOrderId == res.Id && ids.Contains(x.DiningPlateId)) + .OrderBy(x => x.CreateAt, OrderByType.Desc) + .Select(t => new WeighOrderDto + { + Id = t.Id.SelectAll(), + }).ToList(); + if (unbindRed.Count > 0) + { + throw Oops.Oh("没有餐盘或已用被解绑"); + } + + + if (diningPlate != null) + { + foreach (var item in diningPlate) + { + var goods = res.GoodsInfo.Where(x => x.DiningPlateId == item.DiningPlateId).ToList(); + item.GoodsInfo = goods; + } + res.DiningPlateInfo = diningPlate; + } + + + if (res.GoodsInfo != null) + { + res.GoodsInfo = res.GoodsInfo + .GroupBy(x => x.GoodsId) + .Select(x => new WeighOrderGoodsDto() + { + GoodsAttribute = x.FirstOrDefault()?.GoodsAttribute, + GoodsId = x.Key, + GoodsName = x.FirstOrDefault()?.GoodsName, + GoodsWeight = x.Sum(p => p.GoodsWeight), + GroupId = x.FirstOrDefault()?.GroupId, + OrderId = x.FirstOrDefault()?.OrderId, + TotalAmount = x.Sum(p => p.TotalAmount), + }) + .ToList(); + } + RedisHelper.RedisConn.GetDatabase().StringSet("order" + res.CreateId, JsonConvert.SerializeObject(res)); + } } } diff --git a/BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj b/BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj index 09bb971..d2a8e3c 100644 --- a/BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj +++ b/BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -12,6 +12,7 @@ + diff --git a/BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs b/BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs new file mode 100644 index 0000000..51cb528 --- /dev/null +++ b/BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs @@ -0,0 +1,39 @@ +using Furion; +using StackExchange.Redis; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPA.KitChen.GroupMealOrder.Core.Cache +{ + public class RedisHelper + { + private static readonly ConfigurationOptions ConfigurationOptions = ConfigurationOptions.Parse($"{App.Configuration["RedisConfig:IP"]},password={App.Configuration["RedisConfig:Pwd"]}"); + private static readonly object Locker = new object(); + private static ConnectionMultiplexer _redisConn; + + /// + /// 单例获取 + /// + public static ConnectionMultiplexer RedisConn + { + get + { + if (_redisConn == null) + { + // 锁定某一代码块,让同一时间只有一个线程访问该代码块 + lock (Locker) + { + if (_redisConn == null || !_redisConn.IsConnected) + { + _redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions); + } + } + } + return _redisConn; + } + } + } +} diff --git a/BPA.KitChen.GroupMealOrder/appsettings.json b/BPA.KitChen.GroupMealOrder/appsettings.json index d2a53d3..b3635aa 100644 --- a/BPA.KitChen.GroupMealOrder/appsettings.json +++ b/BPA.KitChen.GroupMealOrder/appsettings.json @@ -12,33 +12,8 @@ "DBConnectionStr": "server=10.2.1.21;Port=3306;Database=bpa_kitchen_groupmealorder;Uid=root;Pwd=cygadmin;", "DBConnectionLogStr": "server=10.2.1.21;Port=3306;Database=bpa_kitchen_groupmeallog;Uid=root;Pwd=cygadmin;", "AllowedHosts": "*", - "wechat": [ - { - "Key": "wx37fa34ae664500d2", - "WechatConfig": { - "IsProduction": true, - "Appid": "wx37fa34ae664500d2", - "AppSecret": "36d33b57b9c3cc009fbda02f70ff9000", - "AppMessagetTakeAway": "kubHTVDra8KA356lGFpb10H8j2PzXfvjNbb-gLbPW0Y", - "AppMessageActivity": "SeB1D4uOblgaL5mdICfoeBs4ImRT3uv9grOur3PT5qk", - "MchId": "1610675942", - "MchKey": "bS2kT9aN1yJ5qH1zM1qR3bF8rU4gK4mM", - "MchIP": "1.14.74.54", - "WeChatPayNotifyUrl": "https://bpa.black-pa.com:21527/wechat/wechat/paynotity", - "WeChatRefundUrl": "https://bpa.black-pa.com:21527/wechat/wechat/refundnotity", - "WeChatOrderNotifyUrl": "https://bpa.black-pa.com:21527/wechat/api/order/notity", - "OrderAddress": "http://order", - "FranchiseeAddress": "http://kitchen" - }, - "WechatPayConfig": { - "AppId": "wx37fa34ae664500d2", - "MchId": "1610675942", - "APIKey": "bS2kT9aN1yJ5qH1zM1qR3bF8rU4gK4mM", - "APIv3Key": "", - "Certificate": "", - "RsaPublicKey": "" - } - } - - ] + "RedisConfig": { + "IP": "10.2.1.21:6379", + "Pwd": "1178661552398188544" + } } diff --git a/BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs b/BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs index af58d7f..bd4c4b5 100644 --- a/BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs +++ b/BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs @@ -1,4 +1,5 @@ -using BPA.KitChen.GroupMealOrder.Core.CacheOption; +using BPA.KitChen.GroupMealOrder.Core.Cache; +using BPA.KitChen.GroupMealOrder.Core.CacheOption; using BPA.KitChen.GroupMealOrder.Core.Entity; using BPA.KitChen.GroupMealOrder.SqlSugar; using BPA.KitChen.WeChat.WechatServer.Dtos; @@ -81,7 +82,7 @@ namespace BPA.KitChen.WeChat.WechatServer.Service { return WeChatPayNotifyResult.Failure; } - + RedisHelper.RedisConn.GetDatabase().KeyDelete("order" + order.CreateId); return WeChatPayNotifyResult.Success; } }