163 lines
5.8 KiB
C#
163 lines
5.8 KiB
C#
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Basic.Enums;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.ScheduledTask.SyncSapInfo;
|
|
using Dlhis.ScheduledTask.SyncSapInfo.Services;
|
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.Core.DataAccess.Entity;
|
|
using Kurisu.SqlSugar.Services;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.ScheduledTask.CommonJobs;
|
|
|
|
public class MealSupplementHandlerJob
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ILogger<MealSupplementHandlerJob> _logger;
|
|
private readonly IJinkoJIPService _jinkoJIPService;
|
|
|
|
public MealSupplementHandlerJob(IDbContext dbContext, ILogger<MealSupplementHandlerJob> logger, IJinkoJIPService jinkoJIPService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
_jinkoJIPService = jinkoJIPService;
|
|
}
|
|
|
|
public async Task RunAsync()
|
|
{
|
|
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
|
try
|
|
{
|
|
//查询待处理餐补
|
|
var mealSupplements = await _dbContext.Queryable<MealSupplementRecordEntity>()
|
|
.Where(x => (x.Status == MealSupplementStatusEnumtype.Wait || x.Status == MealSupplementStatusEnumtype.Error) && !x.Sync)
|
|
.ToListAsync();
|
|
|
|
foreach (var item in mealSupplements)
|
|
{
|
|
GainfoMealEntity mealUser = null;
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == item.Code).SingleAsync();
|
|
//档案不存在
|
|
if (gainfo == null)
|
|
{
|
|
//todo
|
|
//_logger.LogWarning("档案code:{code}不存在.flowno:{flowno}", item.Code, item.UniqueId);
|
|
//continue;
|
|
|
|
gainfo = new GainfoEntity() { No = Guid.NewGuid().ToString() };
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
|
|
mealUser = await _dbContext.Queryable<GainfoMealEntity>().Where(x => x.UserNo == gainfo.No).SingleAsync();
|
|
if (mealUser == null)
|
|
{
|
|
mealUser = new GainfoMealEntity
|
|
{
|
|
Balance = 0,
|
|
BalanceOfRecharge = 0,
|
|
BalanceOfSubsidies = 0,
|
|
UserNo = gainfo.No,
|
|
UserCode = gainfo.Code,
|
|
Version = 0,
|
|
TenantId = gainfo.Factory,
|
|
};
|
|
|
|
await _dbContext.InsertAsync(mealUser);
|
|
}
|
|
|
|
await SetMealSupplementAsync(mealUser, item);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
_dbContext.Client.QueryFilter.Restore();
|
|
}
|
|
}
|
|
|
|
private async Task SetMealSupplementAsync(GainfoMealEntity mealUser, MealSupplementRecordEntity mealSupplement)
|
|
{
|
|
await _dbContext.Client.Ado.BeginTranAsync();
|
|
try
|
|
{
|
|
await ContinueUpdateAsync(async () =>
|
|
{
|
|
mealUser = await _dbContext.Queryable<GainfoMealEntity>().Where(x => x.UserCode == mealUser.UserCode).FirstAsync();
|
|
var b = mealUser.Balance += mealSupplement.Amount;
|
|
|
|
var effect = await _dbContext.Updateable<GainfoMealEntity>()
|
|
.SetColumns(x => x.Balance == b)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.UserCode == mealUser.UserCode && x.Version == mealUser.Version)
|
|
.ExecuteCommandAsync();
|
|
|
|
if (effect <= 0)
|
|
{
|
|
throw new VersionExceptions("餐补写入错误");
|
|
}
|
|
|
|
}, 5);
|
|
|
|
await _dbContext.Updateable<MealSupplementRecordEntity>()
|
|
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Sucess)
|
|
.SetColumns(x => x.Sync == true)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.UniqueId == mealSupplement.UniqueId)
|
|
.ExecuteCommandAsync();
|
|
|
|
var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
|
|
{
|
|
new()
|
|
{
|
|
UniqueId = mealSupplement.UniqueId,
|
|
Status = "SUCCESS",
|
|
Msg = "操作成功"
|
|
}
|
|
});
|
|
|
|
await _dbContext.Updateable<MealSupplementRecordEntity>()
|
|
.SetColumns(x => x.Result == r)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.UniqueId == mealSupplement.UniqueId)
|
|
.ExecuteCommandAsync();
|
|
|
|
await _dbContext.Client.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await _dbContext.Client.Ado.RollbackTranAsync();
|
|
|
|
await _dbContext.Updateable<MealSupplementRecordEntity>()
|
|
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Error)
|
|
.SetColumns(x => x.Result == ex.Message)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.UniqueId == mealSupplement.UniqueId)
|
|
.ExecuteCommandAsync();
|
|
}
|
|
}
|
|
|
|
private async Task ContinueUpdateAsync(Func<Task> func, int retry, string errorMessage = null)
|
|
{
|
|
for (int i = 0; i < retry; i++)
|
|
{
|
|
try
|
|
{
|
|
await func();
|
|
return;
|
|
}
|
|
catch (Exception ex) when (ex is VersionExceptions)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
throw new UserFriendlyException(errorMessage ?? "操作失败,请重试");
|
|
}
|
|
}
|