using System.Collections.Immutable; using NewLife.Log; using WorkerSynReport.Data; namespace WorkerSynReport { public class Worker : BackgroundService { private readonly ILogger _logger; private readonly IServiceScopeFactory _scopeFactory; public Worker(ILogger logger, IServiceScopeFactory scopeFactory) { _logger = logger; this._scopeFactory = scopeFactory; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await SynDry(); await SynWetMixer(); await SynGel(); await Task.Delay(1000*60, stoppingToken); } } private async Task SynWetMixer() { 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(); var list= await dbContextLocal.Query().Where(x => x.objId > objId).ToListAsync(); foreach (var eMixer in list) { lsMain.Add(eMixer); lsMainDetail.AddRange(await dbContextLocal.Query().Where(x => x.reportId==eMixer.reportId).ToListAsync()); } if (lsMain.Any()) { 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); } } } } }