Browse Source

redis

master
gwbvipvip 7 months ago
parent
commit
2f1dfec3e5
5 changed files with 179 additions and 37 deletions
  1. +131
    -5
      BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs
  2. +2
    -1
      BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj
  3. +39
    -0
      BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs
  4. +4
    -29
      BPA.KitChen.GroupMealOrder/appsettings.json
  5. +3
    -2
      BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs

+ 131
- 5
BPA.KitChen.GroupMealOrder.Application/Service/WeighOrder/Service/WeighOrderService.cs View File

@@ -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
/// <returns></returns>
public async Task<WeighOrderDto> 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<BPA_WeighOrderDiningPlateUnbindRecord>();


foreach (var item in inputDto)
{
//查询订单
var weighOrder = await db.Queryable<BPA_WeighOrder>().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
/// <returns></returns>
public async Task<WeighOrderDto> GetWeighOrderByOpenId(GetWeighOrderByOpenIdInputDto inputDto)
{
var res = await db.Queryable<BPA_WeighOrder>()
var res = new WeighOrderDto();
var orderRedis = RedisHelper.RedisConn.GetDatabase().StringGet("order" + inputDto.OpenId);
if (!string.IsNullOrEmpty(orderRedis))
{
res = JsonConvert.DeserializeObject<WeighOrderDto>(orderRedis);
return res;
}

res = await db.Queryable<BPA_WeighOrder>()
.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;
}





/// <summary>
/// 称重订单分页
/// </summary>
@@ -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<BPA_WeighOrder>()
.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<BPA_WeighOrderGoods>()
.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<List<GoogsAttribute>>(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<BPA_WeighOrderDiningPlate>()
.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<BPA_WeighOrderDiningPlateUnbindRecord>()
.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));
}
}
}

+ 2
- 1
BPA.KitChen.GroupMealOrder.Core/BPA.KitChen.GroupMealOrder.Core.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@@ -12,6 +12,7 @@
<PackageReference Include="Furion" Version="4.9.1.20" />
<PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.20" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.134" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.7" />
</ItemGroup>



+ 39
- 0
BPA.KitChen.GroupMealOrder.Core/Cache/RedisHelper.cs View File

@@ -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;

/// <summary>
/// 单例获取
/// </summary>
public static ConnectionMultiplexer RedisConn
{
get
{
if (_redisConn == null)
{
// 锁定某一代码块,让同一时间只有一个线程访问该代码块
lock (Locker)
{
if (_redisConn == null || !_redisConn.IsConnected)
{
_redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions);
}
}
}
return _redisConn;
}
}
}
}

+ 4
- 29
BPA.KitChen.GroupMealOrder/appsettings.json View File

@@ -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"
}
}

+ 3
- 2
BPA.KitChen.WeChat/WechatServer/Service/WechatNotityService.cs View File

@@ -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;
}
}


Loading…
Cancel
Save