Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/MealSupplementHandlerJob.cs
2024-12-05 13:44:03 +08:00

190 lines
7.5 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Jip.Api;
using Dlhis.Jip.Api.Requests;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.Utils;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 餐补核算
/// </summary>
[RecurringJob(nameof(MealSupplementHandlerJob), "0 0/30 * * * ?", Name = "餐补核算")]
public class MealSupplementHandlerJob : BaseJob
{
private readonly IJinkoJipApi _jinkoJipApi;
public MealSupplementHandlerJob(IServiceProvider serviceProvider,
IJinkoJipApi jinkoJipApi) : base(serviceProvider)
{
_jinkoJipApi = jinkoJipApi;
}
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//查询待处理餐补
var mealSupplements = await DbContext.Queryable<MealSupplementRecordEntity>()
.Where(x => x.Status == MealSupplementStatusEnumtype.Wait || x.Status == MealSupplementStatusEnumtype.Error)
.Where(x => !x.Sync)
.ToListAsync();
foreach (var item in mealSupplements)
{
var gainfo = await DbContext.Queryable<GainfoEntity>().Where(x => x.Code == item.Code).SingleAsync();
if (gainfo == null)
{
gainfo = await DbContext.Queryable<GainfoEntity>().AS("GainfoLeave").Where(x => x.Code == item.Code).SingleAsync();
if (gainfo == null)
{
await DbContext.Updateable<MealSupplementRecordEntity>()
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Error)
.SetColumns(x => x.Result == "人员档案不存在")
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == item.UniqueId)
.ExecuteCommandAsync();
//后台任务获取人员 todo
//BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByCodeAsync(item.Code));
continue;
}
if (gainfo.ResignationDate < item.Date)
{
try
{
var r = await _jinkoJipApi.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
{
new()
{
UniqueId = item.UniqueId,
Status = "FAIL",
Msg = "人员离职"
}
});
await DbContext.Updateable<MealSupplementRecordEntity>()
.SetColumns(x => x.Result == r)
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Success)
.SetColumns(x => x.Sync == true)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == item.UniqueId)
.ExecuteCommandAsync();
}
catch
{
//ignore
}
continue;
}
}
await SetMealSupplementAsync(gainfo, item);
}
}
private async Task SetMealSupplementAsync(GainfoEntity gainfo, MealSupplementRecordEntity mealSupplement)
{
await DbContext.Client.Ado.BeginTranAsync();
try
{
await DbContext.InsertAsync(new BalanceRecordEntity
{
Amount = mealSupplement.Amount,
ApplicationType = CanteenApplicationTypeEnumType.MealSupplement,
Type = BalanceRecordTypeEnumType.In,
BusinessPlaceCode = null,
BusinessShiftCode = null,
ShiftType = null,
CostDate = mealSupplement.Date,
CostDateTime = DateTime.Now,
UserNo = gainfo.No,
No = mealSupplement.UniqueId, //使用DHR流水号
UserCode = gainfo.Code,
TenantId = mealSupplement.Factory,
Status = BalanceRecordStatusEnumType.Valid,
Source = BalanceRecordSourceEnumType.LocalTenantSubsidies,
Remark = mealSupplement.UniqueId + ":" + mealSupplement.Remark,
CostId = gainfo.CostId,
OriginTenantId = gainfo.Factory,
Werks = gainfo.Werks,
Name = gainfo.Name,
IsCostCrossTenant = false,
DepartmentNo = gainfo.DepartmentNo,
DeviceUniqueNo = null,
IsDeleted = false,
});
if (!await DbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == gainfo.Code).AnyAsync())
{
await DbContext.InsertAsync(new UserBalanceEntity
{
UserCode = gainfo.Code,
Balance = 0,
Version = 0
});
}
await CommonHelper.RetryAsync(async () =>
{
var balance = await DbContext.Queryable<BalanceRecordEntity>()
.Where(x => x.UserCode == gainfo.Code)
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
.Where(x => x.Source != BalanceRecordSourceEnumType.EMealVoucher)
.SumAsync(x => x.Amount);
var version = await DbContext.Queryable<UserBalanceEntity>()
.Where(x => x.UserCode == gainfo.Code)
.Select(x => x.Version)
.SingleAsync();
await DbContext.Updateable<UserBalanceEntity>()
.SetColumns(x => x.Balance == balance)
.SetColumns(x => x.Version == version + 1)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UserCode == gainfo.Code && x.Version == version)
.ExecuteCommandThrowVersionAsync();
}, 5);
var r = await _jinkoJipApi.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
{
new()
{
UniqueId = mealSupplement.UniqueId,
Status = "SUCCESS",
Msg = "操作成功"
}
});
//更新餐补信息为成功(同步成功)
await DbContext.Updateable<MealSupplementRecordEntity>()
.SetColumns(x => x.Result == r)
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Success)
.SetColumns(x => x.Sync == true)
.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 + " " + ex.StackTrace)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == mealSupplement.UniqueId)
.ExecuteCommandAsync();
}
}
}