定时任务

This commit is contained in:
tongfei
2024-01-15 17:56:07 +08:00
parent c4da0c2626
commit cbefd35401
8 changed files with 187 additions and 21 deletions

View File

@@ -52,11 +52,13 @@ namespace WMS.Web.Api
services.AddControllers();
//<2F>Ƴ<EFBFBD><C6B3><EFBFBD>http<74><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>ַhttps://docs.microsoft.com/zh-cn/dotnet/core/compatibility/aspnet-core/5.0/http-httpclient-instances-log-integer-status-codes<65><73>
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
services.AddTimedJob();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseTimedJob();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

View File

@@ -5171,6 +5171,20 @@
<param name="date"></param>
<returns>DateTime</returns>
</member>
<member name="M:WMS.Web.Core.Help.DateTimeUtil.GetTotalMinutesTimeSpan(System.DateTime,System.DateTime)">
<summary>
两个时间的差:分钟
</summary>
<param name="beginTime"></param>
<param name="endTime"></param>
<returns></returns>
</member>
<member name="M:WMS.Web.Core.Help.DateTimeUtil.GetTotalMinutesTimeSpan">
<summary>
当前时间和今晚0点之间的时间的差分钟
</summary>
<returns></returns>
</member>
<member name="M:WMS.Web.Core.Help.RedisClient.SetStringKey(System.String,System.String,System.Nullable{System.TimeSpan})">
<summary>
保存单个key value

View File

@@ -5121,6 +5121,11 @@
<param name="dto"></param>
<returns></returns>
</member>
<member name="T:WMS.Web.Domain.TimedJob.ErpBaseDataSyncJob">
<summary>
erp基础数据-同步定时任务
</summary>
</member>
<member name="T:WMS.Web.Domain.Values.BackRecordType">
<summary>
出入库回退类型

View File

@@ -164,5 +164,40 @@ namespace WMS.Web.Core.Help
return "";
}
}
/// <summary>
/// 两个时间的差:分钟
/// </summary>
/// <param name="beginTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
public static double GetTotalMinutesTimeSpan(DateTime beginTime, DateTime endTime)
{
string dateDiff = null;//获取当前时间
TimeSpan ts1 = new TimeSpan(beginTime.Ticks);
TimeSpan ts2 = new TimeSpan(endTime.Ticks);
//时间比较,得出差值
TimeSpan ts = ts1.Subtract(ts2).Duration();//结果
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
return ts.TotalMinutes;
}
/// <summary>
/// 当前时间和今晚0点之间的时间的差分钟
/// </summary>
/// <returns></returns>
public static double GetTotalMinutesTimeSpan()
{
string dateDiff = null;//获取当前时间
DateTime DateTime1 = DateTime.Now;
//第二天的0点00分00秒
DateTime DateTime2 = DateTime.Now.AddDays(1).Date;//把2个时间转成TimeSpan,方便计算
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
//时间比较,得出差值
TimeSpan ts = ts1.Subtract(ts2).Duration();//结果
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
return ts.TotalMinutes;
}
}
}

View File

@@ -28,5 +28,12 @@ namespace WMS.Web.Domain.Options
/// erp-Id
/// </summary>
public string ErpId { get; set; }
public readonly string cache_materail_key = "erp_materials_list";
public readonly string cache_org_key = "erp_org_list";
public readonly string cache_supplier_key = "erp_supplier_list";
public readonly string cache_customer_key = "erp_customer_list";
public readonly string cache_stock_key = "erp_stock_list";
public readonly string cache_substock_key = "erp_substock_list";
}
}

View File

@@ -41,5 +41,43 @@ namespace WMS.Web.Domain.Services.Public
});
return Task.CompletedTask;
}
public static Task SyncMaterial(IErpService sercice)
{
Task.Run(() =>
{
//异步-同步下物料数据
sercice.BillQueryForMaterial().GetAwaiter().GetResult();
});
return Task.CompletedTask;
}
public static Task SyncOrg(IErpService sercice)
{
Task.Run(() =>
{
//异步-同步下组织数据
sercice.BillQueryForOrg().GetAwaiter().GetResult();
});
return Task.CompletedTask;
}
public static Task SyncCustomer(IErpService sercice)
{
Task.Run(() =>
{
//异步-同步下客户数据
sercice.BillQueryForCustomer().GetAwaiter().GetResult();
});
return Task.CompletedTask;
}
public static Task SyncSupplier(IErpService sercice)
{
Task.Run(() =>
{
//异步-同步下供应商数据
sercice.BillQueryForSupplier().GetAwaiter().GetResult();
});
return Task.CompletedTask;
}
}
}

View File

@@ -18,6 +18,7 @@ using WMS.Web.Core.Dto.Erp.Org;
using WMS.Web.Core.Dto.Erp.OutStock;
using WMS.Web.Core.Dto.Erp.Supplier;
using WMS.Web.Core.Dto.Erp.TakeStock;
using WMS.Web.Core.Help;
using WMS.Web.Core.Internal.Results;
using WMS.Web.Domain.Entitys;
using WMS.Web.Domain.Infrastructure;
@@ -41,12 +42,6 @@ namespace WMS.Web.Domain.Services.Public
private ERPGWSoapClient _client;
private readonly IMemoryCache _memoryCache;
private readonly IBasicsRepositories _basicsRepositories;
public readonly string cache_materail_key = "erp_materials_list";
public readonly string cache_org_key = "erp_org_list";
public readonly string cache_supplier_key = "erp_supplier_list";
public readonly string cache_customer_key = "erp_customer_list";
public readonly string cache_stock_key = "erp_stock_list";
public readonly string cache_substock_key = "erp_substock_list";
public ErpService(
IMapper mapper,
IOptions<ErpOptions> erpOptions,
@@ -798,7 +793,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的物料数据;
var materials = _memoryCache.Get<List<ErpMaterialDto>>(cache_materail_key);
var materials = _memoryCache.Get<List<ErpMaterialDto>>(_erpOptions.cache_materail_key);
if (materials == null || materials.Count == 0)
{
return await this.BillQueryForMaterialPagedList();
@@ -825,7 +820,7 @@ namespace WMS.Web.Domain.Services.Public
public async Task<Result<ErpMaterialDto>> BillQueryForMaterial(int id)
{
//1.获取缓存中的物料数据
var materials = _memoryCache.Get<List<ErpMaterialDto>>(cache_materail_key);
var materials = _memoryCache.Get<List<ErpMaterialDto>>(_erpOptions.cache_materail_key);
if (materials == null || materials.Count == 0)
return Result<ErpMaterialDto>.ReSuccess(null);
//2.通过ID取当前物料列表中的
@@ -838,7 +833,7 @@ namespace WMS.Web.Domain.Services.Public
{
//把取到的数据放集合中并重新给缓存
materials.Add(mater);
_memoryCache.Set(cache_materail_key, materials, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_materail_key, materials, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
return Result<ErpMaterialDto>.ReSuccess(mater);
}
else
@@ -910,8 +905,8 @@ namespace WMS.Web.Domain.Services.Public
}
var endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
_logger.LogInformation($"物料拉取-结束时间:{endTime}");
//5.物料集合进行缓存
_memoryCache.Set(cache_materail_key, erp_materials_list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
//5.物料集合进行缓存-当前缓存时间到当天晚上的0点后面定时任务执行重新放入缓存
_memoryCache.Set(_erpOptions.cache_materail_key, erp_materials_list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
_logger.LogInformation($"物料拉取-总条数:{erp_materials_list.Count}");
return ResultList<ErpMaterialDto>.ReSuccess(erp_materials_list);
}
@@ -983,7 +978,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的组织数据;
var orgs = _memoryCache.Get<List<ErpOrgDto>>(cache_org_key);
var orgs = _memoryCache.Get<List<ErpOrgDto>>(_erpOptions.cache_org_key);
if (orgs == null || orgs.Count == 0)
{
@@ -1025,7 +1020,7 @@ namespace WMS.Web.Domain.Services.Public
}
//5.组织集合进行缓存
_memoryCache.Set(cache_org_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_org_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
_logger.LogInformation($"组织拉取-总条数:{list.Count}");
return ResultList<ErpOrgDto>.ReSuccess(list);
@@ -1051,7 +1046,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的供应商数据;
var suppliers = _memoryCache.Get<List<ErpSupplierDto>>(cache_supplier_key);
var suppliers = _memoryCache.Get<List<ErpSupplierDto>>(_erpOptions.cache_supplier_key);
if (suppliers == null || suppliers.Count == 0)
{
@@ -1093,7 +1088,7 @@ namespace WMS.Web.Domain.Services.Public
}
//5.供应商集合进行缓存
_memoryCache.Set(cache_supplier_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_supplier_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
_logger.LogInformation($"供应商拉取-总条数:{list.Count}");
return ResultList<ErpSupplierDto>.ReSuccess(list);
@@ -1118,7 +1113,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的供应商数据;
var customers = _memoryCache.Get<List<ErpCustomerDto>>(cache_customer_key);
var customers = _memoryCache.Get<List<ErpCustomerDto>>(_erpOptions.cache_customer_key);
if (customers == null || customers.Count == 0)
{
@@ -1171,7 +1166,7 @@ namespace WMS.Web.Domain.Services.Public
var endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
_logger.LogInformation($"客户拉取-结束时间:{endTime}");
//5.供应商集合进行缓存
_memoryCache.Set(cache_customer_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_customer_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
_logger.LogInformation($"客户拉取-总条数:{list.Count}");
return ResultList<ErpCustomerDto>.ReSuccess(list);
@@ -1196,7 +1191,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的仓库数据;
var stocks = _memoryCache.Get<List<ErpStockDto>>(cache_stock_key);
var stocks = _memoryCache.Get<List<ErpStockDto>>(_erpOptions.cache_stock_key);
if (stocks == null || stocks.Count == 0)
{
//2.先登录金蝶-拿到token
@@ -1233,7 +1228,7 @@ namespace WMS.Web.Domain.Services.Public
}
//5.供应商集合进行缓存
_memoryCache.Set(cache_stock_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_stock_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_logger.LogInformation($"仓库拉取-总条数:{list.Count}");
return ResultList<ErpStockDto>.ReSuccess(list);
}
@@ -1258,7 +1253,7 @@ namespace WMS.Web.Domain.Services.Public
try
{
//1.获取缓存中的仓库数据;
var stocks = _memoryCache.Get<List<Erp_SubStockDto>>(cache_substock_key);
var stocks = _memoryCache.Get<List<Erp_SubStockDto>>(_erpOptions.cache_substock_key);
if (stocks == null || stocks.Count == 0)
{
var token_result = await this.Init();
@@ -1289,7 +1284,7 @@ namespace WMS.Web.Domain.Services.Public
}
//5.供应商集合进行缓存 只有查询全部的时候才缓存
_memoryCache.Set(cache_substock_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_memoryCache.Set(_erpOptions.cache_substock_key, list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(3)));
_logger.LogInformation($"子仓库拉取-总条数:{list.Count}");
return ResultList<Erp_SubStockDto>.ReSuccess(list);
}

View File

@@ -0,0 +1,70 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Pomelo.AspNetCore.TimedJob;
using System;
using System.Collections.Generic;
using System.Text;
using WMS.Web.Core.Dto.Erp;
using WMS.Web.Core.Dto.Erp.Customer;
using WMS.Web.Core.Dto.Erp.Org;
using WMS.Web.Core.Dto.Erp.Supplier;
using WMS.Web.Domain.IService.Public;
using WMS.Web.Domain.Options;
using WMS.Web.Domain.Services.Public;
namespace WMS.Web.Domain.TimedJob
{
/// <summary>
/// erp基础数据-同步定时任务
/// </summary>
public class ErpBaseDataSyncJob : Job
{
private readonly IMemoryCache _memoryCache;
private readonly ErpOptions _erpOptions;
private ILogger<ErpBaseDataSyncJob> _logger;
private readonly IServiceProvider _serviceProvider;
public ErpBaseDataSyncJob(IServiceProvider serviceProvider,IMemoryCache memoryCache, IOptions<ErpOptions> erpOptions, ILogger<ErpBaseDataSyncJob> logger)
{
_serviceProvider = serviceProvider;
_memoryCache = memoryCache;
this._erpOptions = erpOptions?.Value;
this._logger = logger;
}
//[Invoke(Begin = "2022-03-02 01:01", Interval = 1000 * 60 * 60 * 24, SkipWhileExecuting = true)]
//30分钟执行一次
[Invoke(Begin = "2024-01-15 17:43", Interval = 1000 * 60 * 30, SkipWhileExecuting = true)]
public void Run()
{
string tip = "";
_logger.LogInformation($"----------异步:定时任务拉取---------");
var sercice = _serviceProvider.GetRequiredService<IErpService>();
var materials = _memoryCache.Get<List<ErpMaterialDto>>(_erpOptions.cache_materail_key);
if (materials == null || materials.Count == 0)
ErpBaseDataSync.SyncMaterial(sercice);
else
tip=tip + "物料缓存未失效;";
var orgs = _memoryCache.Get<List<ErpOrgDto>>(_erpOptions.cache_org_key);
if (orgs == null || orgs.Count == 0)
ErpBaseDataSync.SyncOrg(sercice);
else
tip = tip + "组织缓存未失效;";
var suppliers = _memoryCache.Get<List<ErpSupplierDto>>(_erpOptions.cache_supplier_key);
if (suppliers == null || suppliers.Count == 0)
ErpBaseDataSync.SyncSupplier(sercice);
else
tip = tip + "供应商缓存未失效;";
var customers = _memoryCache.Get<List<ErpCustomerDto>>(_erpOptions.cache_customer_key);
if (customers == null || customers.Count == 0)
ErpBaseDataSync.SyncCustomer(sercice);
else
tip = tip + "客户缓存未失效;";
if (!string.IsNullOrEmpty(tip))
_logger.LogInformation($"异步:定时任务拉取-->" + tip);
}
}
}