From 843cc2ea1b104ecdf9da61318a4136a5d4096411 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期五, 24 四月 2026 11:07:21 +0800
Subject: [PATCH] 集成Quartz定时任务,支持NG出库自动化及WMS/WCS接口扩展

---
 Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs |  118 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 96 insertions(+), 22 deletions(-)

diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs
index 90c4c55..37283df 100644
--- a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs
@@ -1,24 +1,35 @@
-using WIDESEA_Core.Helper;
-using WIDESEA_Core.Extensions;
-using WIDESEA_Core.Middlewares;
-using System.Reflection;
-using WIDESEA_Core.Filter;
+using Autofac;
+using Autofac.Core;
+using Autofac.Extensions.DependencyInjection;
+using Microsoft.AspNetCore.Mvc.Controllers;
+using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.OpenApi.Models;
+using NewLife.Windows;
+using Newtonsoft.Json;
 using Newtonsoft.Json.Converters;
 using Newtonsoft.Json.Serialization;
-using Newtonsoft.Json;
-using System.Text;
-using Microsoft.AspNetCore.Mvc.Controllers;
-using Microsoft.Extensions.DependencyInjection.Extensions;
-using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
-using WIDESEA_Core;
-using WIDESEA_Core.BaseServices;
-using Autofac.Extensions.DependencyInjection;
-using Autofac;
-using WIDESEA_WMSServer.Filter;
-using WIDESEA_Core.Authorization;
-using WIDESEA_Core.Core;
+using Quartz.Spi;
+using SimpleRedis;
 using Swashbuckle.AspNetCore.SwaggerGen;
-using Microsoft.OpenApi.Models;
+using System.Reflection;
+using System.Text;
+using WIDESEA.QuartzJob;
+using WIDESEA_Cache;
+using WIDESEA_Core;
+using WIDESEA_Core.Authorization;
+using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.Core;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Extensions;
+using WIDESEA_Core.Filter;
+using WIDESEA_Core.Helper;
+using WIDESEA_Core.Middlewares;
+using WIDESEA_DTO.WMS;
+using WIDESEA_StorageTaskServices;
+using WIDESEA_Tasks;
+using WIDESEA_WMSServer.Filter;
+using static NewLife.Remoting.ApiHttpClient;
 
 var builder = WebApplication.CreateBuilder(args);
 
@@ -109,20 +120,63 @@
     }
 });
 
+builder.Services.AddHostedService<MyBackgroundService>();
+
+
+//builder.Services.AddSingleton<IJobFactory, JobFactory>();
+//builder.Services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>();
+
+//builder.Services.AddTransient<OutNGTask>();//Job使用瞬时依赖注入
+
+
+//缓存设置配置转实体
+builder.Services.AddConfigurableOptions<CacheSettingsOptions>();
+//禁止在主机启动时通过 App.GetOptions<TOptions> 获取选项,如需获取配置选项理应通过 App.GetConfig<TOptions>("配置节点", true)。
+var cacheSettings = AppSettings.Configuration.GetSection("CacheSettings").Get<CacheSettingsOptions>();
+//如果有redis连接字符串
+if (cacheSettings.UseRedis)
+{
+    var connectionString =
+        $"server={cacheSettings.RedisSettings.Address};db={cacheSettings.RedisSettings.Db}";
+    //注入redis
+    builder.Services.AddSimpleRedis(connectionString);
+    builder.Services.AddScoped<ISimpleCacheService, RedisCacheService>();
+}
+else
+{
+    builder.Services.AddScoped<ISimpleCacheService, MemoryCacheService>();
+}
+
 
 var app = builder.Build();
+
+// 验证 Redis 服务是否成功注册
+using (var scope = app.Services.CreateScope())
+{
+    var services = scope.ServiceProvider;
+    var redisService = services.GetService<ISimpleCacheService>();
+    if (redisService == null)
+    {
+        WIDESEA_Core.Helper.ConsoleHelper.WriteErrorLine("Redis 服务未成功注册");
+    }
+    else
+    {
+        WIDESEA_Core.Helper.ConsoleHelper.WriteSuccessLine("Redis 服务已成功注册");
+    }
+}
+app.UseQuartzJobMildd();
 
 // 3、配置中间件
 app.UseMiniProfiler();//性能分析器
 app.ConfigureApplication();//配置文件
 app.UseApplicationSetup();//启动配置
 app.UseSession();
-if (app.Environment.IsDevelopment())
-{
+//if (app.Environment.IsDevelopment())
+//{
     //todo
     //app.UseSwaggerAuthorized();
     app.UseSwaggerMiddle(() => Assembly.GetExecutingAssembly().GetManifestResourceStream("WIDESEA_WMSServer.index.html"));
-}//判断是否需要打开swagger
+//}//判断是否需要打开swagger
 
 app.UseIpLimitMiddle();
 app.UseApiLogMiddleware();
@@ -146,8 +200,28 @@
 app.UseAuthorization();
 
 
-
 app.MapControllers();
 
+//通过 App.GetOptions<TOptions> 获取选项
+var cacheSettingsb = App.GetOptions<CacheSettingsOptions>();
+//如果需要清除缓存
+if (cacheSettingsb.UseRedis && cacheSettingsb.RedisSettings.ClearRedis)
+{
+    var redis = app.Services.CreateScope().ServiceProvider.GetService<ISimpleCacheService>(); //获取redis服务
+    // 删除redis的key
+    redis.DelByPattern(CacheConst.Cache_Prefix);
+
+    WIDESEA_Core.Helper.ConsoleHelper.WriteInfoLine("正在缓存库存信息");
+    var stockInfoList = await SqlSugarHelper.DbWMS.Queryable<DtStockInfo>()
+        .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock).IncludesAllFirstLayer().ToListAsync();
+    // 缓存库存信息
+    foreach (var item in stockInfoList)
+    {
+        item.StockInfoDetails = new List<DtStockInfoDetail>() { { item.StockInfoDetails[0] } };
+        redis.HashAdd(CacheConst.Cache_DtStockInfo, item.PalletCode, item);
+    }
+    WIDESEA_Core.Helper.ConsoleHelper.WriteInfoLine("缓存库存信息完成");
+}
+
 app.Run();
 

--
Gitblit v1.9.3