You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.5 KiB
C#

5 months ago
7 months ago
using NewLife.Log;
11 months ago
using WorkerSynReport.Data;
namespace WorkerSynReport
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
11 months ago
private readonly IServiceScopeFactory _scopeFactory;
public Worker(ILogger<Worker> logger, IServiceScopeFactory scopeFactory)
{
_logger = logger;
11 months ago
this._scopeFactory = scopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
5 months ago
{
try
{
await SynDry();
await SynWetMixer();
6 months ago
5 months ago
await SynGel();
}
catch (Exception e)
{
XTrace.WriteException(e);
}
7 months ago
5 months ago
await Task.Delay(1000*30, stoppingToken);
}
}
11 months ago
private async Task SynWetMixer()
{
7 months ago
XTrace.WriteLine("开始跑湿混机");
11 months ago
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();
10 months ago
var objId=await dbContextLocal!.Query<Report_WetMixer>().Where(x => x.reportId == reportId)
11 months ago
.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())
7 months ago
{
6 months ago
XTrace.WriteLine("湿混机插入数据:{0}",lsMain.Count);
11 months ago
await dbContextHttp.InsertRangeAsync(lsMain);
await dbContextHttp.InsertRangeAsync(lsMainDetail);
}
}
}
10 months ago
private async Task SynDry()
{
XTrace.WriteLine("开始跑干混机");
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_Dry>()
.OrderByDesc(x => x.objId).Select(x => x.reportId).FirstOrDefaultAsync();
10 months ago
var objId=await dbContextLocal!.Query<Report_Dry>().Where(x => x.reportId == reportId)
10 months ago
.Select(x => x.objId).FirstOrDefaultAsync();
if (objId > 0)
{
List<Report_Dry> lsMain = new List<Report_Dry>();
List<Report_DryDos_Detail> lsMainDetail = new List<Report_DryDos_Detail>();
List<Report_DryMixer_Detail> lsMixerDetails = new();
var list= await dbContextLocal.Query<Report_Dry>().Where(x => x.objId > objId).ToListAsync();
foreach (var dry in list)
{
lsMain.Add(dry);
lsMainDetail.AddRange(await dbContextLocal.Query<Report_DryDos_Detail>().Where(x=>x.reportId==dry.reportId).ToListAsync());
lsMixerDetails.AddRange(await dbContextLocal.Query<Report_DryMixer_Detail>().Where(x=>x.reportId==dry.reportId).ToListAsync());
}
if (lsMain.Any())
{
6 months ago
XTrace.WriteLine("干混机插入数据:{0}",lsMain.Count);
10 months ago
await dbContextHttp.InsertRangeAsync(lsMain);
await dbContextHttp.InsertRangeAsync(lsMainDetail);
await dbContextHttp.InsertRangeAsync(lsMixerDetails);
}
}
}
7 months ago
7 months ago
7 months ago
7 months ago
private async Task SynGel()
{
XTrace.WriteLine("开始跑糊化机");
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_Gel>()
.OrderByDesc(x => x.objId).Select(x => x.reportId).FirstOrDefaultAsync();
var objId=await dbContextLocal.Query<Report_Gel>().Where(x => x.reportId == reportId)
.Select(x => x.objId).FirstOrDefaultAsync();
if (objId > 0)
{
List<Report_Gel> lsMain = new List<Report_Gel>();
List<Report_GelDoser_Detail> lsMainDetail = new List<Report_GelDoser_Detail>();
List<Report_GelMixing_Detail> lsMixerDetails = new();
var list= await dbContextLocal.Query<Report_Gel>().Where(x => x.objId > objId).ToListAsync();
foreach (var dry in list)
{
lsMain.Add(dry);
lsMainDetail.AddRange(await dbContextLocal.Query<Report_GelDoser_Detail>().Where(x=>x.reportId==dry.reportId).ToListAsync());
lsMixerDetails.AddRange(await dbContextLocal.Query<Report_GelMixing_Detail>().Where(x=>x.reportId==dry.reportId).ToListAsync());
}
if (lsMain.Any())
{
6 months ago
XTrace.WriteLine("糊化机插入数据:{0}",lsMain.Count);
7 months ago
await dbContextHttp.InsertRangeAsync(lsMain);
await dbContextHttp.InsertRangeAsync(lsMainDetail);
await dbContextHttp.InsertRangeAsync(lsMixerDetails);
}
}
}
}
}