diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/MiddleService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/MiddleService.cs index de70a148..418eafae 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/MiddleService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/MiddleService.cs @@ -1,20 +1,19 @@ using Dlhis.Canteen.Order.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Canteen.Entities; -using Dlhis.Entity.Canteen.Enums; -using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; -using Kurisu.AspNetCore.Utils; namespace Dlhis.Canteen.Order.Services.Implements; public class MiddleService : IMiddleService { private readonly IDbContext _dbContext; + private readonly IBalanceService _balanceService; - public MiddleService(IDbContext dbContext) + public MiddleService(IDbContext dbContext, IBalanceService balanceService) { _dbContext = dbContext; + _balanceService = balanceService; } /// @@ -26,38 +25,7 @@ public class MiddleService : IMiddleService if (!await _dbContext.Queryable().AnyAsync(x => x.No == entity.No)) { await _dbContext.InsertAsync(entity); - - await CommonHelper.RetryAsync(async () => - { - var mealUser = await _dbContext.Queryable().Where(x => x.UserCode == entity.UserCode).FirstAsync(); - if (mealUser == null) - { - mealUser = new GainfoMealEntity - { - UserNo = entity.No, - UserCode = entity.UserCode, - Balance = 0, - BalanceOfRecharge = 0, - BalanceOfSubsidies = 0, - Version = 0 - }; - - await _dbContext.InsertAsync(mealUser); - } - - //重新核算 - var balance = await _dbContext.Queryable() - .Where(x => x.UserCode == entity.UserCode) - .Where(x => x.Status == BalanceRecordStatusEnumType.Valid) - .SumAsync(x => x.Amount); - - await _dbContext.Updateable() - .SetColumns(x => x.Version == x.Version + 1) - .SetColumns(x => x.Balance == balance) - .SetColumns(x => x.ModifiedTime == DateTime.Now) - .Where(x => x.UserCode == mealUser.UserCode && x.Version == mealUser.Version) - .ExecuteCommandThrowVersionAsync(); - }, 5); + await _balanceService.ReCalcAsync(entity.UserCode); } //获取人员余额 diff --git a/Src/Dlhis.Middle/Services/IDataDownloadService.cs b/Src/Dlhis.Middle/Services/IDataDownloadService.cs index 2fb4ddad..0efb64d4 100644 --- a/Src/Dlhis.Middle/Services/IDataDownloadService.cs +++ b/Src/Dlhis.Middle/Services/IDataDownloadService.cs @@ -7,6 +7,4 @@ public interface IDataDownloadService : IScopeDependency Task DownloadAsync(string factory) where T : class, new(); Task TruncateAsync(); - - Task DownloadGainfosAsync(string factory); } \ No newline at end of file diff --git a/Src/Dlhis.Middle/Services/Implements/BalanceRecordCheckBackgroundService.cs b/Src/Dlhis.Middle/Services/Implements/BalanceRecordCheckBackgroundService.cs index 019762de..70810c52 100644 --- a/Src/Dlhis.Middle/Services/Implements/BalanceRecordCheckBackgroundService.cs +++ b/Src/Dlhis.Middle/Services/Implements/BalanceRecordCheckBackgroundService.cs @@ -46,72 +46,73 @@ public class BalanceRecordCheckBackgroundService : BackgroundService { var now = DateTime.Now; - using (var scope = _serviceProvider.CreateScope()) + using var scope = _serviceProvider.CreateScope(); + var db = scope.ServiceProvider.GetService(); + var ds = scope.ServiceProvider.GetService(); + + foreach (var factory in _factory.Split(',')) { - var db = scope.ServiceProvider.GetService(); - var ds = scope.ServiceProvider.GetService(); - - foreach (var factory in _factory.Split(',')) + try { - try + var balanceRecords = await db.Queryable().Where(x => x.TenantId == factory).Where(x => !x.IsSync).ToListAsync(stoppingToken); + if (balanceRecords.Any()) { - var balanceRecords = await db.Queryable().Where(x => x.TenantId == factory).Where(x => !x.IsSync).ToListAsync(stoppingToken); - if (balanceRecords.Any()) + _logger.LogInformation("上传离线订单"); + foreach (var balanceRecord in balanceRecords) { - _logger.LogInformation("上传离线订单"); - foreach (var balanceRecord in balanceRecords) + //事务 + await db.Client.Ado.BeginTranAsync(); + try { - await db.Client.Ado.BeginTranAsync(); - try - { - balanceRecord.IsSync = true; - await db.UpdateAsync(balanceRecord); + //设置为已同步 + balanceRecord.IsSync = true; + await db.UpdateAsync(balanceRecord); - var r = await UploadAsync(balanceRecord); - if (r.Code == ApiStateCode.Success) - { - var d = r.Data; - await db.Updateable() - .SetColumns(x => x.Balance == d.Balance) - .Where(x => x.UserCode == d.UserCode) - .ExecuteCommandAsync(stoppingToken); - - await db.Client.Ado.CommitTranAsync(); - } - } - catch (Exception e) + var r = await UploadAsync(balanceRecord); + if (r.Code == ApiStateCode.Success) { - _logger.LogError(e, "{factory}上传离线消费失败:{message}", factory, e.Message); - await db.Client.Ado.RollbackTranAsync(); + var d = r.Data; + //更新本地余额表 + await db.Updateable() + .SetColumns(x => x.Balance == d.Balance) + .Where(x => x.UserCode == d.UserCode) + .ExecuteCommandAsync(stoppingToken); + + await db.Client.Ado.CommitTranAsync(); } } + catch (Exception e) + { + _logger.LogError(e, "{factory}上传离线消费失败:{message}", factory, e.Message); + await db.Client.Ado.RollbackTranAsync(); + } } - - - if (now.Hour == 4 && now.Minute == 1 - && !_healthCheckService.GetIsOffline() - && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) - { - await ds.DownloadGainfosAsync(factory); - } - else if (now.Hour == 15 && now.Minute == 1 - && !_healthCheckService.GetIsOffline() - && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) - { - await ds.DownloadGainfosAsync(factory); - } - else if (now.Hour == 20 && now.Minute == 1 - && !_healthCheckService.GetIsOffline() - && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) - { - await ds.DownloadGainfosAsync(factory); - } - } - catch (Exception e) - { - _logger.LogError(e, "{factory}同步离线数据失败:{message}", factory, e.Message); } } + catch (Exception e) + { + _logger.LogError(e, "{factory}同步离线数据失败:{message}", factory, e.Message); + } + } + + + if (now is { Hour: 4, Minute: 1 } + && !_healthCheckService.GetIsOffline() + && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) + { + await ds.InitAsync(_factory); + } + else if (now is { Hour: 15, Minute: 1 } + && !_healthCheckService.GetIsOffline() + && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) + { + await ds.InitAsync(_factory); + } + else if (now is { Hour: 20, Minute: 1 } + && !_healthCheckService.GetIsOffline() + && !await db.Queryable().Where(x => !x.IsSync).AnyAsync()) + { + await ds.InitAsync(_factory); } } diff --git a/Src/Dlhis.Middle/Services/Implements/DataDownloadService.cs b/Src/Dlhis.Middle/Services/Implements/DataDownloadService.cs index 5c2f73ff..9cf94ea8 100644 --- a/Src/Dlhis.Middle/Services/Implements/DataDownloadService.cs +++ b/Src/Dlhis.Middle/Services/Implements/DataDownloadService.cs @@ -18,13 +18,15 @@ public class DataDownloadService : IDataDownloadService private readonly IHttpClientFactory _httpClientFactory; private readonly IMemoryCache _memoryCache; private readonly IDbContext _dbContext; + private readonly ILogger _logger; private readonly string _hostName; - public DataDownloadService(IHttpClientFactory httpClientFactory, IMemoryCache memoryCache, IConfiguration configuration, IDbContext dbContext) + public DataDownloadService(IHttpClientFactory httpClientFactory, IMemoryCache memoryCache, IConfiguration configuration, IDbContext dbContext, ILogger logger) { _httpClientFactory = httpClientFactory; _memoryCache = memoryCache; _dbContext = dbContext; + _logger = logger; _hostName = configuration.GetSection("HostName").Value; } @@ -36,6 +38,7 @@ public class DataDownloadService : IDataDownloadService foreach (var item in factories) { + _logger.LogInformation("正在下载{factory}", item); if (!await _dbContext.Queryable().AnyAsync()) { await DownloadAsync(item); @@ -63,6 +66,8 @@ public class DataDownloadService : IDataDownloadService } } } + + _logger.LogInformation("下载完成"); } public async Task DownloadAsync(string factory) where T : class, new() @@ -97,38 +102,8 @@ public class DataDownloadService : IDataDownloadService await _dbContext.Client.Deleteable().ExecuteCommandAsync(); await _dbContext.Client.Deleteable().ExecuteCommandAsync(); - //await _dbContext.Client.Deleteable().ExecuteCommandAsync(); - //await _dbContext.Client.Deleteable().ExecuteCommandAsync(); - } - - /// - public async Task DownloadGainfosAsync(string factory) - { - await _dbContext.Client.Ado.BeginTranAsync(); - try - { - await _dbContext.Client.Deleteable().ExecuteCommandAsync(); - await _dbContext.Client.Deleteable().ExecuteCommandAsync(); - - await DownloadAsync(factory); - await DownloadAsync(factory); - - - await DownloadAsync(factory); - await DownloadAsync(factory); - - await DownloadAsync(factory); - await DownloadAsync(factory); - await DownloadAsync(factory); - - await DownloadAsync(factory); - - await _dbContext.Client.Ado.CommitTranAsync(); - } - catch (Exception) - { - await _dbContext.Client.Ado.RollbackTranAsync(); - } + await _dbContext.Client.Deleteable().ExecuteCommandAsync(); + await _dbContext.Client.Deleteable().ExecuteCommandAsync(); } private async Task AuthAsync()