diff --git a/Program.cs b/Program.cs index 60c5983..e126937 100644 --- a/Program.cs +++ b/Program.cs @@ -8,7 +8,7 @@ IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { - services.AddScoped(a => new DbContextLocal("Data Source=127.0.0.1;Initial Catalog=CWSSHG;user=sa;password=123456;")); + services.AddScoped(a => new DbContextLocal("Data Source=172.18.4.130;Initial Catalog=CWSSHG;user=sa;password=123456;")); services.AddScoped(a => new DbContextHttp("Data Source=192.168.202.22;Initial Catalog=CWSSHG;user=sa;password=Lanju@123;")); diff --git a/Worker.cs b/Worker.cs index b6654be..4fa7228 100644 --- a/Worker.cs +++ b/Worker.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using NewLife.Log; using WorkerSynReport.Data; namespace WorkerSynReport @@ -20,11 +21,12 @@ namespace WorkerSynReport protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) - { + { + await SynWetMixer(); - - - _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + await SynDry(); + await SynGel(); + await Task.Delay(1000*30, stoppingToken); } } @@ -32,7 +34,7 @@ namespace WorkerSynReport private async Task SynWetMixer() { - _logger.LogInformation("定时器开始"); + XTrace.WriteLine("开始跑湿混机"); using var scope = _scopeFactory.CreateScope(); var services = scope.ServiceProvider; using IDbContextLocal? dbContextLocal = services.GetService(); @@ -56,13 +58,97 @@ namespace WorkerSynReport } if (lsMain.Any()) - { - _logger.LogInformation("插入数据:{Data}",lsMain.Count); + { + XTrace.WriteLine("插入数据:{0}",lsMain.Count); await dbContextHttp.InsertRangeAsync(lsMain); await dbContextHttp.InsertRangeAsync(lsMainDetail); } } } + + + private async Task SynDry() + { + XTrace.WriteLine("开始跑干混机"); + using var scope = _scopeFactory.CreateScope(); + var services = scope.ServiceProvider; + using IDbContextLocal? dbContextLocal = services.GetService(); + using IDbContextHttp? dbContextHttp = services.GetService(); + + var reportId=await dbContextHttp!.Query() + .OrderByDesc(x => x.objId).Select(x => x.reportId).FirstOrDefaultAsync(); + + var objId=await dbContextLocal.Query().Where(x => x.reportId == reportId) + .Select(x => x.objId).FirstOrDefaultAsync(); + + if (objId > 0) + { + List lsMain = new List(); + List lsMainDetail = new List(); + List lsMixerDetails = new(); + + var list= await dbContextLocal.Query().Where(x => x.objId > objId).ToListAsync(); + + foreach (var dry in list) + { + lsMain.Add(dry); + lsMainDetail.AddRange(await dbContextLocal.Query().Where(x=>x.reportId==dry.reportId).ToListAsync()); + + lsMixerDetails.AddRange(await dbContextLocal.Query().Where(x=>x.reportId==dry.reportId).ToListAsync()); + } + + if (lsMain.Any()) + { + XTrace.WriteLine("插入数据:{0}",lsMain.Count); + await dbContextHttp.InsertRangeAsync(lsMain); + await dbContextHttp.InsertRangeAsync(lsMainDetail); + await dbContextHttp.InsertRangeAsync(lsMixerDetails); + } + } + } + + + + private async Task SynGel() + { + + XTrace.WriteLine("开始跑糊化机"); + using var scope = _scopeFactory.CreateScope(); + var services = scope.ServiceProvider; + using IDbContextLocal? dbContextLocal = services.GetService(); + using IDbContextHttp? dbContextHttp = services.GetService(); + + var reportId=await dbContextHttp!.Query() + .OrderByDesc(x => x.objId).Select(x => x.reportId).FirstOrDefaultAsync(); + + var objId=await dbContextLocal.Query().Where(x => x.reportId == reportId) + .Select(x => x.objId).FirstOrDefaultAsync(); + + if (objId > 0) + { + List lsMain = new List(); + List lsMainDetail = new List(); + List lsMixerDetails = new(); + + var list= await dbContextLocal.Query().Where(x => x.objId > objId).ToListAsync(); + + foreach (var dry in list) + { + lsMain.Add(dry); + lsMainDetail.AddRange(await dbContextLocal.Query().Where(x=>x.reportId==dry.reportId).ToListAsync()); + + lsMixerDetails.AddRange(await dbContextLocal.Query().Where(x=>x.reportId==dry.reportId).ToListAsync()); + } + + if (lsMain.Any()) + { + XTrace.WriteLine("插入数据:{0}",lsMain.Count); + await dbContextHttp.InsertRangeAsync(lsMain); + await dbContextHttp.InsertRangeAsync(lsMainDetail); + await dbContextHttp.InsertRangeAsync(lsMixerDetails); + } + } + } } } diff --git a/WorkerSynReport.csproj b/WorkerSynReport.csproj index e3bf833..29b4d1f 100644 --- a/WorkerSynReport.csproj +++ b/WorkerSynReport.csproj @@ -8,11 +8,12 @@ - - - - - - + + + + + + +