fix 补充日志输出
This commit is contained in:
@@ -1,20 +1,19 @@
|
|||||||
using Dlhis.Canteen.Order.Response;
|
using Dlhis.Canteen.Order.Response;
|
||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
using Dlhis.Entity.Canteen.Entities;
|
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.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils;
|
|
||||||
|
|
||||||
namespace Dlhis.Canteen.Order.Services.Implements;
|
namespace Dlhis.Canteen.Order.Services.Implements;
|
||||||
|
|
||||||
public class MiddleService : IMiddleService
|
public class MiddleService : IMiddleService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
|
private readonly IBalanceService _balanceService;
|
||||||
|
|
||||||
public MiddleService(IDbContext dbContext)
|
public MiddleService(IDbContext dbContext, IBalanceService balanceService)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
|
_balanceService = balanceService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -26,38 +25,7 @@ public class MiddleService : IMiddleService
|
|||||||
if (!await _dbContext.Queryable<BalanceRecordEntity>().AnyAsync(x => x.No == entity.No))
|
if (!await _dbContext.Queryable<BalanceRecordEntity>().AnyAsync(x => x.No == entity.No))
|
||||||
{
|
{
|
||||||
await _dbContext.InsertAsync(entity);
|
await _dbContext.InsertAsync(entity);
|
||||||
|
await _balanceService.ReCalcAsync(entity.UserCode);
|
||||||
await CommonHelper.RetryAsync(async () =>
|
|
||||||
{
|
|
||||||
var mealUser = await _dbContext.Queryable<GainfoMealEntity>().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<BalanceRecordEntity>()
|
|
||||||
.Where(x => x.UserCode == entity.UserCode)
|
|
||||||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
|
||||||
.SumAsync(x => x.Amount);
|
|
||||||
|
|
||||||
await _dbContext.Updateable<GainfoMealEntity>()
|
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取人员余额
|
//获取人员余额
|
||||||
|
|||||||
@@ -7,6 +7,4 @@ public interface IDataDownloadService : IScopeDependency
|
|||||||
Task DownloadAsync<T>(string factory) where T : class, new();
|
Task DownloadAsync<T>(string factory) where T : class, new();
|
||||||
|
|
||||||
Task TruncateAsync();
|
Task TruncateAsync();
|
||||||
|
|
||||||
Task DownloadGainfosAsync(string factory);
|
|
||||||
}
|
}
|
||||||
@@ -46,72 +46,73 @@ public class BalanceRecordCheckBackgroundService : BackgroundService
|
|||||||
{
|
{
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
|
|
||||||
using (var scope = _serviceProvider.CreateScope())
|
using var scope = _serviceProvider.CreateScope();
|
||||||
|
var db = scope.ServiceProvider.GetService<IDbContext>();
|
||||||
|
var ds = scope.ServiceProvider.GetService<IDataDownloadService>();
|
||||||
|
|
||||||
|
foreach (var factory in _factory.Split(','))
|
||||||
{
|
{
|
||||||
var db = scope.ServiceProvider.GetService<IDbContext>();
|
try
|
||||||
var ds = scope.ServiceProvider.GetService<IDataDownloadService>();
|
|
||||||
|
|
||||||
foreach (var factory in _factory.Split(','))
|
|
||||||
{
|
{
|
||||||
try
|
var balanceRecords = await db.Queryable<LocalBalanceRecordEntity>().Where(x => x.TenantId == factory).Where(x => !x.IsSync).ToListAsync(stoppingToken);
|
||||||
|
if (balanceRecords.Any())
|
||||||
{
|
{
|
||||||
var balanceRecords = await db.Queryable<LocalBalanceRecordEntity>().Where(x => x.TenantId == factory).Where(x => !x.IsSync).ToListAsync(stoppingToken);
|
_logger.LogInformation("上传离线订单");
|
||||||
if (balanceRecords.Any())
|
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);
|
var r = await UploadAsync(balanceRecord);
|
||||||
if (r.Code == ApiStateCode.Success)
|
if (r.Code == ApiStateCode.Success)
|
||||||
{
|
|
||||||
var d = r.Data;
|
|
||||||
await db.Updateable<LocalGainfoMealEntity>()
|
|
||||||
.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);
|
var d = r.Data;
|
||||||
await db.Client.Ado.RollbackTranAsync();
|
//更新本地余额表
|
||||||
|
await db.Updateable<LocalGainfoMealEntity>()
|
||||||
|
.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<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
|
|
||||||
{
|
|
||||||
await ds.DownloadGainfosAsync(factory);
|
|
||||||
}
|
|
||||||
else if (now.Hour == 15 && now.Minute == 1
|
|
||||||
&& !_healthCheckService.GetIsOffline()
|
|
||||||
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
|
|
||||||
{
|
|
||||||
await ds.DownloadGainfosAsync(factory);
|
|
||||||
}
|
|
||||||
else if (now.Hour == 20 && now.Minute == 1
|
|
||||||
&& !_healthCheckService.GetIsOffline()
|
|
||||||
&& !await db.Queryable<LocalBalanceRecordEntity>().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<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
|
||||||
|
{
|
||||||
|
await ds.InitAsync(_factory);
|
||||||
|
}
|
||||||
|
else if (now is { Hour: 15, Minute: 1 }
|
||||||
|
&& !_healthCheckService.GetIsOffline()
|
||||||
|
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
|
||||||
|
{
|
||||||
|
await ds.InitAsync(_factory);
|
||||||
|
}
|
||||||
|
else if (now is { Hour: 20, Minute: 1 }
|
||||||
|
&& !_healthCheckService.GetIsOffline()
|
||||||
|
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
|
||||||
|
{
|
||||||
|
await ds.InitAsync(_factory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,15 @@ public class DataDownloadService : IDataDownloadService
|
|||||||
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly IHttpClientFactory _httpClientFactory;
|
||||||
private readonly IMemoryCache _memoryCache;
|
private readonly IMemoryCache _memoryCache;
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
|
private readonly ILogger<DataDownloadService> _logger;
|
||||||
private readonly string _hostName;
|
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<DataDownloadService> logger)
|
||||||
{
|
{
|
||||||
_httpClientFactory = httpClientFactory;
|
_httpClientFactory = httpClientFactory;
|
||||||
_memoryCache = memoryCache;
|
_memoryCache = memoryCache;
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
|
_logger = logger;
|
||||||
_hostName = configuration.GetSection("HostName").Value;
|
_hostName = configuration.GetSection("HostName").Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +38,7 @@ public class DataDownloadService : IDataDownloadService
|
|||||||
|
|
||||||
foreach (var item in factories)
|
foreach (var item in factories)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("正在下载{factory}", item);
|
||||||
if (!await _dbContext.Queryable<LocalDepartmentEntity>().AnyAsync())
|
if (!await _dbContext.Queryable<LocalDepartmentEntity>().AnyAsync())
|
||||||
{
|
{
|
||||||
await DownloadAsync<LocalDepartmentEntity>(item);
|
await DownloadAsync<LocalDepartmentEntity>(item);
|
||||||
@@ -63,6 +66,8 @@ public class DataDownloadService : IDataDownloadService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("下载完成");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DownloadAsync<T>(string factory) where T : class, new()
|
public async Task DownloadAsync<T>(string factory) where T : class, new()
|
||||||
@@ -97,38 +102,8 @@ public class DataDownloadService : IDataDownloadService
|
|||||||
await _dbContext.Client.Deleteable<LocalBusinessPlaceShiftRelationshipEntity>().ExecuteCommandAsync();
|
await _dbContext.Client.Deleteable<LocalBusinessPlaceShiftRelationshipEntity>().ExecuteCommandAsync();
|
||||||
await _dbContext.Client.Deleteable<LocalBusinessShiftMealRelationshipEntity>().ExecuteCommandAsync();
|
await _dbContext.Client.Deleteable<LocalBusinessShiftMealRelationshipEntity>().ExecuteCommandAsync();
|
||||||
|
|
||||||
//await _dbContext.Client.Deleteable<LocalGainfoEntity>().ExecuteCommandAsync();
|
await _dbContext.Client.Deleteable<LocalGainfoEntity>().ExecuteCommandAsync();
|
||||||
//await _dbContext.Client.Deleteable<LocalGainfoMealEntity>().ExecuteCommandAsync();
|
await _dbContext.Client.Deleteable<LocalGainfoMealEntity>().ExecuteCommandAsync();
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task DownloadGainfosAsync(string factory)
|
|
||||||
{
|
|
||||||
await _dbContext.Client.Ado.BeginTranAsync();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _dbContext.Client.Deleteable<LocalGainfoEntity>().ExecuteCommandAsync();
|
|
||||||
await _dbContext.Client.Deleteable<LocalGainfoMealEntity>().ExecuteCommandAsync();
|
|
||||||
|
|
||||||
await DownloadAsync<LocalGainfoEntity>(factory);
|
|
||||||
await DownloadAsync<LocalGainfoMealEntity>(factory);
|
|
||||||
|
|
||||||
|
|
||||||
await DownloadAsync<LocalDeviceEntity>(factory);
|
|
||||||
await DownloadAsync<LocalDeviceRegistryEntity>(factory);
|
|
||||||
|
|
||||||
await DownloadAsync<LocalBusinessPlaceEntity>(factory);
|
|
||||||
await DownloadAsync<LocalBusinessShiftEntity>(factory);
|
|
||||||
await DownloadAsync<LocalBusinessPlaceShiftRelationshipEntity>(factory);
|
|
||||||
|
|
||||||
await DownloadAsync<LocalBusinessShiftMealRelationshipEntity>(factory);
|
|
||||||
|
|
||||||
await _dbContext.Client.Ado.CommitTranAsync();
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
await _dbContext.Client.Ado.RollbackTranAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<string> AuthAsync()
|
private async Task<string> AuthAsync()
|
||||||
|
|||||||
Reference in New Issue
Block a user