湿混机
parent
2a19784b03
commit
1919d1a6ee
@ -1,10 +1,26 @@
|
||||
using NewLife.Extensions.Hosting.AgentService;
|
||||
using NewLife.Log;
|
||||
using WorkerSynReport;
|
||||
|
||||
XTrace.UseConsole();
|
||||
|
||||
IHost host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
|
||||
services.AddScoped<IDbContextLocal>(a => new DbContextLocal("Data Source=127.0.0.1;Initial Catalog=CWSSHG;user=sa;password=123456;"));
|
||||
|
||||
services.AddScoped<IDbContextHttp>(a => new DbContextHttp("Data Source=192.168.202.22;Initial Catalog=CWSSHG;user=sa;password=Lanju@123;"));
|
||||
|
||||
|
||||
services.AddHostedService<Worker>();
|
||||
})
|
||||
.UseAgentService(options =>
|
||||
{
|
||||
options.ServiceName = "报表数据";
|
||||
options.DisplayName = "Worker服务测试";
|
||||
options.Description = "Worker服务的应用";
|
||||
})
|
||||
.Build();
|
||||
|
||||
await host.RunAsync();
|
||||
|
@ -1,21 +1,68 @@
|
||||
using System.Collections.Immutable;
|
||||
using WorkerSynReport.Data;
|
||||
|
||||
namespace WorkerSynReport
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
public Worker(ILogger<Worker> logger, IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
this._scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await SynWetMixer();
|
||||
|
||||
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
await Task.Delay(1000*30, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SynWetMixer()
|
||||
{
|
||||
|
||||
_logger.LogInformation("定时器开始");
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var services = scope.ServiceProvider;
|
||||
using IDbContextLocal? dbContextLocal = services.GetService<IDbContextLocal>();
|
||||
using IDbContextHttp? dbContextHttp = services.GetService<IDbContextHttp>();
|
||||
|
||||
var reportId=await dbContextHttp!.Query<Report_WetMixer>()
|
||||
.OrderByDesc(x => x.objId).Select(x => x.reportId).FirstOrDefaultAsync();
|
||||
|
||||
|
||||
var objId=await dbContextLocal.Query<Report_WetMixer>().Where(x => x.reportId == reportId)
|
||||
.Select(x => x.objId).FirstOrDefaultAsync();
|
||||
if (objId>0)
|
||||
{
|
||||
List<Report_WetMixer> lsMain = new List<Report_WetMixer>();
|
||||
List<Report_WetMixer_Detail> lsMainDetail = new List<Report_WetMixer_Detail>();
|
||||
var list= await dbContextLocal.Query<Report_WetMixer>().Where(x => x.objId > objId).ToListAsync();
|
||||
foreach (var eMixer in list)
|
||||
{
|
||||
lsMain.Add(eMixer);
|
||||
lsMainDetail.AddRange(await dbContextLocal.Query<Report_WetMixer_Detail>().Where(x => x.reportId==eMixer.reportId).ToListAsync());
|
||||
}
|
||||
|
||||
if (lsMain.Any())
|
||||
{
|
||||
_logger.LogInformation("插入数据:{Data}",lsMain.Count);
|
||||
await dbContextHttp.InsertRangeAsync(lsMain);
|
||||
await dbContextHttp.InsertRangeAsync(lsMainDetail);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue