redis模块

usertest
cjy 5 months ago
parent 969bcc3913
commit fb4746a351

@ -10,6 +10,7 @@
<PackageReference Include="Autofac" Version="8.0.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="CSRedisCore" Version="3.8.803" />
<PackageReference Include="DotNetCore.CAP" Version="8.0.0" />
<PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="8.0.0" />
<PackageReference Include="Enums.NET" Version="4.0.2" />

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DS.Module.Core\DS.Module.Core.csproj" />
<ProjectReference Include="..\DS.Module.Nuget\DS.Module.Nuget.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DS.Module.RedisModule
{
public interface IRedisService
{
/// <summary>
/// 设置长时间存在的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool SetLongValue(string key, string value);
/// <summary>
/// 设置值,并设置清除时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="outSecond"></param>
/// <returns></returns>
bool SetValue(string key, string value, int outSecond);
/// <summary>
/// 设置值,存在则覆盖,并沿用之前的清除时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool SetValue(string key, string value);
/// <summary>
/// 是否存在key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Exists(string key);
// <summary>
/// 更新Key把自动注销时间设置为原来的key的时间,不存在返回false
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool UpdateValue(string key, string value);
/// <summary>
/// 获取值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
string? GetValue(string key);
/// <summary>
/// 获得json序列化后的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T? GetValue<T>(string key);
/// <summary>
/// 获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T? GetEntity<T>(string key);
/// <summary>
/// 获取列表 模糊匹配
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
List<T>? GetLike<T>(string key);
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
void DeleteKey(string key);
/// <summary>
/// 模糊删除
/// </summary>
/// <param name="key"></param>
void DeleteLike(string key);
}
}

@ -0,0 +1,21 @@
namespace DS.Module.RedisModule;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// 注入Redis服务
/// </summary>
public static class RedisModuleInstall
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <exception cref="ArgumentNullException"></exception>
public static void AddRedisModuleInstall(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddScoped<IRedisService, RedisService>();
}
}

@ -0,0 +1,266 @@
using CSRedis;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using DS.Module.Core;
namespace DS.Module.RedisModule
{
public class RedisService: IRedisService
{
CSRedisClient csRedis;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly string redisConfig;
public RedisService(IServiceProvider serviceProvider)
{
redisConfig = AppSetting.app(new string[] { "RedisInfo", "RedisConfig" });
csRedis = new CSRedisClient(redisConfig);
RedisHelper.Initialization(csRedis);
}
/// <summary>
/// 设置长时间存在的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetLongValue(string key, string value)
{
try
{
csRedis.Set(key, value);
return true;
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 设置值,并设置清除时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="outSecond"></param>
/// <returns></returns>
public bool SetValue(string key, string value, int outSecond)
{
try
{
csRedis.Set(key, value, outSecond);
return true;
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 设置值,存在则覆盖,并沿用之前的清除时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetValue(string key, string value)
{
try
{
if (csRedis.Exists(key))
{
long time = csRedis.Ttl(key);
csRedis.Set(key, value, Convert.ToInt32(time));
}
else
csRedis.Set(key, value);
return true;
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
/// <summary>
/// 是否存在key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
{
try
{
return csRedis.Exists(key);
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-KeyExists" + ex.Message);
return false;
}
}
/// <summary>
/// 更新Key把自动注销时间设置为原来的key的时间,不存在返回false
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool UpdateValue(string key, string value)
{
try
{
if (csRedis.Exists(key))
{
long time = csRedis.Ttl(key);
csRedis.Set(key, value, Convert.ToInt32(time));
return true;
}
return false;
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
public string? GetValue(string key)
{
try
{
return csRedis.Get(key);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetValue[{key}]" + ex.Message);
return null;
}
}
/// <summary>
/// 获得json序列化后的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T? GetValue<T>(string key)
{
try
{
var data = csRedis.Get(key);
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetValue[{key}]" + ex.Message);
return default;
}
}
public T? GetEntity<T>(string key)
{
try
{
var data = csRedis.Get(key);
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public List<T>? GetLike<T>(string key)
{
try
{
var dataList = csRedis.Keys(key + "*");
List<T> list = new List<T>();
foreach (string item in dataList)
{
var data = GetEntity<T>(item);
if (data != null)
{
list.Add(data);
}
}
return list;
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public void DeleteKey(string key)
{
try
{
csRedis.Del(key);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-DeleteKey[{key}]" + ex.Message);
}
}
public void DeleteLike(string key)
{
try
{
var dataList = csRedis.Keys(key + "*");
foreach (string item in dataList)
{
DeleteKey(item);
}
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-DeleteLike[{key}]" + ex.Message);
}
}
private bool AcquireLock(string lockKey, string lockValue, int lockTimeoutSeconds)
{
// 尝试获取锁
bool lockAcquired = csRedis.SetNx(lockKey, lockValue);
// 如果成功获取锁,设置锁的超时时间
if (lockAcquired)
{
csRedis.Expire(lockKey, lockTimeoutSeconds);
}
return lockAcquired;
}
private void ReleaseLock(string lockKey, string lockValue)
{
// 释放锁
// 使用 Lua 脚本确保只有持有锁的客户端才能释放锁
string luaScript = @"
if redis.call('get', KEYS[1]) == ARGV[1] then
return redis.call('del', KEYS[1])
else
return 0
end";
csRedis.Eval(luaScript, lockKey, new[] { lockValue });
}
}
}

@ -2,6 +2,6 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
<NameOfLastUsedPublishProfile>D:\Code\DS\ds8-solution\ds-wms-service\DS.WMS.AdminApi\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
<NameOfLastUsedPublishProfile>D:\Code\DS\ds8-solution-pro\ds-wms-service\DS.WMS.AdminApi\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
</PropertyGroup>
</Project>

@ -852,3 +852,10 @@
2024-05-28 11:15:08.5385 Info Validating config: TargetNames=console, ownFile-web, ConfigItems=54, FilePath=D:\Source\Repos\DS8\ds-wms-service\DS.WMS.AdminApi\bin\Debug\net8.0\nlog.config
2024-05-28 11:15:08.5385 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: allfile
2024-05-28 11:15:08.5522 Info Configuration initialized.
2024-07-10 14:02:54.7297 Info Registered target NLog.Targets.FileTarget(Name=allfile)
2024-07-10 14:02:54.7610 Info Registered target NLog.Targets.FileTarget(Name=ownFile-web)
2024-07-10 14:02:54.7610 Info Registered target NLog.Targets.ColoredConsoleTarget(Name=console)
2024-07-10 14:02:54.7963 Info NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 5.2.8.2366. Product version: 5.2.8+f586f1341c46fa38aaaff4c641e7f0fa7e813943. GlobalAssemblyCache: False
2024-07-10 14:02:54.8120 Info Validating config: TargetNames=console, ownFile-web, ConfigItems=54, FilePath=D:\Code\DS\ds8-solution-pro\ds-wms-service\DS.WMS.AdminApi\bin\Debug\net8.0\nlog.config
2024-07-10 14:02:54.8196 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: allfile
2024-07-10 14:02:54.8343 Info Configuration initialized.

@ -49,5 +49,18 @@ namespace DS.WMS.Core.Check.Interface
/// <param name="req"></param>
/// <returns></returns>
public Task<DataResult> CreateCheckBillAutoByExcel(string id, List<CheckBillAutoExcelReq> req);
/// <summary>
/// 对账明细批量删除
/// </summary>
/// <param name="req">主表Id及明细业务Ids</param>
/// <returns></returns>
public DataResult BatchDelCheckBillAutoDetail(IdModel req);
/// <summary>
/// 自动对账批量删除
/// </summary>
/// <param name="req">主表Ids</param>
/// <returns></returns>
public DataResult BatchDelCheckBillAuto(IdModel req);
}
}

@ -282,9 +282,83 @@ namespace DS.WMS.Core.Check.Method
var businessList = tenantDb.Queryable<CheckBillAutoDetail>().Where(x => x.CheckId == checkId && req.Ids.Contains(x.Id)).ToList();
if (businessList.Count > 0)
tenantDb.Deleteable(businessList).ExecuteCommand();
tenantDb.Deleteable(businessList).ExecuteCommand();
var details = tenantDb.Queryable<CheckBillAutoDetail>().Where(x => x.CheckId == checkId && !req.Ids.Contains(x.Id)).ToList();
if (details.Count > 0)
{
if (details.Where(x => x.IsEqual == false).Any())
{
info.CheckStatus = 2;
info.CheckStatusName = "对账不一致";
}
else
{
info.CheckStatus = 1;
info.CheckStatusName = "对账一致";
}
tenantDb.Updateable(info).ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
/// <summary>
/// 自动对账批量删除
/// </summary>
/// <param name="req">主表Ids</param>
/// <returns></returns>
public DataResult BatchDelCheckBillAuto(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Length == 0)
return DataResult.Failed("业务Ids不能为空");
if (tenantDb.Queryable<CheckBillAuto>().Where(x => req.Ids.Contains(x.Id) && (x.BillNo != null && x.BillNo != "")).Any())
{
return DataResult.Failed("存在已生成的对账信息!");
}
var billLists = tenantDb.Queryable<CheckBillAuto>().Where(x => req.Ids.Contains(x.Id)).ToList();
if (billLists.Count > 0)
{
var details = tenantDb.Queryable<CheckBillAutoDetail>().Where(x => req.Ids.Contains(x.CheckId)).ToList();
if (details.Count > 0)
{
tenantDb.Deleteable(details).ExecuteCommand();
}
tenantDb.Deleteable(billLists).ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
/// <summary>
/// 自动对账重新匹配
/// </summary>
/// <param name="req">主表Ids</param>
/// <returns></returns>
public DataResult RecountCheckBillAuto(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Length == 0)
return DataResult.Failed("业务Ids不能为空");
var billLists = tenantDb.Queryable<CheckBillAuto>().Where(x => req.Ids.Contains(x.Id)).ToList();
if (billLists.Count > 0)
{
foreach (var bill in billLists)
{
}
}
else {
return DataResult.Failed("无匹配的自动对账信息");
}
return DataResult.Successed("重新匹配成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
}

@ -1,63 +1,66 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"JwtSettings": {
"Issuer": "vol.core.owner",
"Audience": "vol.core",
"User": "C5ABA9E202D94C43A3CA66002BF77FAF",
"SecretKey": "sdfsdfsrty45634kkhllghtdgdfss345t678fs"
},
"Cors": {
"PolicyName": "WMSCore.API",
"Url": "http://localhost:8000,http://localhost:5999,http://localhost:8088,http://localhost:5173,http://0.0.0.0:5999,http://0.0.0.0:9995,http://localhost:9995,http://60.209.125.238:9995,http://localhost:3000,https://localhost:3100,http://47.104.255.182:3100,http://47.104.255.182:3110,https://localhost:3110,http://localhost:8080,http://localhost:8081,http://localhost:8082,http://localhost:8083,http://localhost:8084"
},
"DBInfo": {
"DefaultDbConnId": "1288018625843826688",
"DefaultDbType": 0,
"DefaultDbString": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev",
"DBS": [
{
"ConnId": "1288018625843826680",
"DBType": 0,
"Enabled": false,
"HitRate": 40,
"Connection": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_log"
}
]
},
"SwaggerDoc": {
"ContactName": "WmsMainAPI",
"ContactEmail": "Wms API.Core@xxx.com",
"ContactUrl": "https://www.xxx.com",
"Version": "1.0",
"Title": "Wms API",
"Description": "Wms Web API"
},
"Middleware": {
"RecordAccessLogs": {
"Enabled": true,
"IgnoreApis": "/api/permission/getnavigationbar,/api/monitor/getids4users,/api/monitor/getaccesslogs,/api/monitor/server,/api/monitor/getactiveusers,/api/monitor/server,"
}
},
"FileSettings": {
"BasePath": "", //使
"RelativePath": "LinkAttach",
"FileType": [ ".xls", ".xlsx", ".pdf", ".jpg", ".png", ".txt", ".pms" ]
},
"PrintService": {
"IP": "60.209.125.238",
//"IP": "localhost",
"Port": "3009",
"AccessKey": "1777229107311022080",
"AccessSecret": "d816e6fe938f24e2f205db129d31286a",
"GetModuleListUrl": "/printApi/OpenPrint/GetPrintModuleList",
"GetTemplateListUrl": "/printApi/OpenPrint/GetPrintTemplateList",
"GetJsonPrintInfoUrl": "/printApi/OpenPrint/GetOpenJsonPrintInfo",
"GetJsonPrintStreamUrl": "/printApi/OpenPrint/GetOpenJsonPrintStream"
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"JwtSettings": {
"Issuer": "vol.core.owner",
"Audience": "vol.core",
"User": "C5ABA9E202D94C43A3CA66002BF77FAF",
"SecretKey": "sdfsdfsrty45634kkhllghtdgdfss345t678fs"
},
"Cors": {
"PolicyName": "WMSCore.API",
"Url": "http://localhost:8000,http://localhost:5999,http://localhost:8088,http://localhost:5173,http://0.0.0.0:5999,http://0.0.0.0:9995,http://localhost:9995,http://60.209.125.238:9995,http://localhost:3000,https://localhost:3100,http://47.104.255.182:3100,http://47.104.255.182:3110,https://localhost:3110,http://localhost:8080,http://localhost:8081,http://localhost:8082,http://localhost:8083,http://localhost:8084"
},
"DBInfo": {
"DefaultDbConnId": "1288018625843826688",
"DefaultDbType": 0,
"DefaultDbString": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev",
"DBS": [
{
"ConnId": "1288018625843826680",
"DBType": 0,
"Enabled": false,
"HitRate": 40,
"Connection": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_log"
}
]
},
"SwaggerDoc": {
"ContactName": "WmsMainAPI",
"ContactEmail": "Wms API.Core@xxx.com",
"ContactUrl": "https://www.xxx.com",
"Version": "1.0",
"Title": "Wms API",
"Description": "Wms Web API"
},
"Middleware": {
"RecordAccessLogs": {
"Enabled": true,
"IgnoreApis": "/api/permission/getnavigationbar,/api/monitor/getids4users,/api/monitor/getaccesslogs,/api/monitor/server,/api/monitor/getactiveusers,/api/monitor/server,"
}
},
"FileSettings": {
"BasePath": "", //使
"RelativePath": "LinkAttach",
"FileType": [ ".xls", ".xlsx", ".pdf", ".jpg", ".png", ".txt", ".pms" ]
},
"PrintService": {
"IP": "60.209.125.238",
//"IP": "localhost",
"Port": "3009",
"AccessKey": "1777229107311022080",
"AccessSecret": "d816e6fe938f24e2f205db129d31286a",
"GetModuleListUrl": "/printApi/OpenPrint/GetPrintModuleList",
"GetTemplateListUrl": "/printApi/OpenPrint/GetPrintTemplateList",
"GetJsonPrintInfoUrl": "/printApi/OpenPrint/GetOpenJsonPrintInfo",
"GetJsonPrintStreamUrl": "/printApi/OpenPrint/GetOpenJsonPrintStream"
},
"RedisInfo": {
"RedisConfig": "127.0.0.1:6379,password=,defaultDatabase=15"
}
}

@ -53,7 +53,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.Module.DjyServiceStatus"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.WMS.CheckApi", "DS.WMS.CheckApi\DS.WMS.CheckApi.csproj", "{4EE319A5-22DC-43F8-BA3C-A34494B4BB8D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DS.Module.DjyRulesEngine", "DS.Module.DjyRulesEngine\DS.Module.DjyRulesEngine.csproj", "{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.Module.DjyRulesEngine", "DS.Module.DjyRulesEngine\DS.Module.DjyRulesEngine.csproj", "{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DS.Module.RedisModule", "DS.Module.RedisModule\DS.Module.RedisModule.csproj", "{CF36AACB-6405-4F5E-9494-72D8BD1FF414}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -153,6 +155,10 @@ Global
{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1}.Release|Any CPU.Build.0 = Release|Any CPU
{CF36AACB-6405-4F5E-9494-72D8BD1FF414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF36AACB-6405-4F5E-9494-72D8BD1FF414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF36AACB-6405-4F5E-9494-72D8BD1FF414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF36AACB-6405-4F5E-9494-72D8BD1FF414}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -181,6 +187,7 @@ Global
{86AF9895-D98D-4BFD-BEB9-CE291A382426} = {518DB9B5-80A8-4B2C-8570-52BD406458DE}
{4EE319A5-22DC-43F8-BA3C-A34494B4BB8D} = {65D75DB2-12D5-4D1F-893D-9750905CE5E4}
{C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1} = {518DB9B5-80A8-4B2C-8570-52BD406458DE}
{CF36AACB-6405-4F5E-9494-72D8BD1FF414} = {518DB9B5-80A8-4B2C-8570-52BD406458DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {66115F23-94B4-43C0-838E-33B5CF77F788}

Loading…
Cancel
Save