262 lines
10 KiB
C#
262 lines
10 KiB
C#
using Dlhis.Canteen.Api;
|
|
using Dlhis.Canteen.Api.Request;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Basic.Enums;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Dlhis.ScheduledTask.Common.Request;
|
|
using Dlhis.ScheduledTask.Common.Services;
|
|
using Hangfire;
|
|
using Kurisu.AspNetCore.Utils;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Scheduled;
|
|
using Kurisu.Scheduled.Attributes;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
|
|
namespace Dlhis.ScheduledTask.Jobs;
|
|
|
|
/// <summary>
|
|
/// 餐补核算
|
|
/// </summary>
|
|
[RecurringJob(nameof(MealSupplementHandlerJob), "0 0/30 * * * ?", Name = "餐补核算")]
|
|
public class MealSupplementHandlerJob : BaseJob
|
|
{
|
|
private readonly IHainingMealSupplementApi _hainingMealSupplementApi;
|
|
private readonly IJinkoJIPService _jinkoJIPService;
|
|
|
|
public MealSupplementHandlerJob(IServiceProvider serviceProvider,
|
|
IHainingMealSupplementApi hainingMealSupplementApi,
|
|
IJinkoJIPService jinkoJIPService) : base(serviceProvider)
|
|
{
|
|
_hainingMealSupplementApi = hainingMealSupplementApi;
|
|
_jinkoJIPService = jinkoJIPService;
|
|
}
|
|
|
|
public 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)
|
|
{
|
|
GainfoMealEntity mealUser = null;
|
|
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 && gainfo.ResignationDate < item.Date)
|
|
{
|
|
try
|
|
{
|
|
var r = await _jinkoJIPService.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
|
|
{
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
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();
|
|
|
|
//后台任务获取人员
|
|
BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByCodeAsync(item.Code));
|
|
}
|
|
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
//创建余额数据
|
|
mealUser = await DbContext.Queryable<GainfoMealEntity>().Where(x => x.UserCode == gainfo.Code).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);
|
|
}
|
|
}
|
|
|
|
if (item.Workplace == "5")
|
|
{
|
|
//todo 海宁餐补数据
|
|
//await SetHainingMealSupplementAsync(item);
|
|
}
|
|
else
|
|
{
|
|
await SetMealSupplementAsync(gainfo, item);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task SetMealSupplementAsync(GainfoEntity gainfo, MealSupplementRecordEntity mealSupplement)
|
|
{
|
|
await DbContext.Client.Ado.BeginTranAsync();
|
|
try
|
|
{
|
|
await CommonHelper.RetryAsync(async () =>
|
|
{
|
|
var mealUser = await DbContext.Queryable<GainfoMealEntity>().Where(x => x.UserCode == mealSupplement.Code).FirstAsync();
|
|
var b = mealUser.Balance += mealSupplement.Amount;
|
|
|
|
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)
|
|
.ExecuteCommandThrowVersionAsync();
|
|
|
|
}, 5);
|
|
|
|
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.Status == MealSupplementStatusEnumtype.Success)
|
|
.SetColumns(x => x.Sync == true)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.UniqueId == mealSupplement.UniqueId)
|
|
.ExecuteCommandAsync();
|
|
|
|
await DbContext.InsertAsync(new BalanceRecordEntity
|
|
{
|
|
Amount = mealSupplement.Amount,
|
|
ApplicationType = CanteenApplicaitonTypeEnumType.MealSupplement,
|
|
Type = BalanceRecordTypeEnumType.In,
|
|
BusinessPlaceCode = null,
|
|
BusinessShiftCode = null,
|
|
ShiftType = null,
|
|
CostDate = mealSupplement.Date,
|
|
CostDateTime = DateTime.Now,
|
|
UserNo = gainfo.No,
|
|
No = SnowFlakeHelper.Instance.NextId().ToString(),
|
|
UserCode = gainfo.Code,
|
|
TenantId = mealSupplement.Factory,
|
|
Status = BalanceRecordStatusEnumType.Valid,
|
|
Source = BalanceRecordSourceEnumType.LocalTenantSubsidies,
|
|
Remark = mealSupplement.UniqueId + ":" + mealSupplement.Remark,
|
|
CostId = gainfo.CostId,
|
|
SubsidiesFromMonth = mealSupplement.Date.ToString("yyyyMM"),
|
|
OriginTenantId = gainfo.Factory,
|
|
Name = gainfo.Name,
|
|
IsCostCrossTenant = false,
|
|
DepartmentNo = gainfo.DepartmentNo,
|
|
DeviceUniqueNo = null,
|
|
|
|
IsDeleted = false,
|
|
|
|
CreatedBy = 1,
|
|
CreateTime = DateTime.Now,
|
|
ModifiedBy = 1,
|
|
ModifiedTime = DateTime.Now,
|
|
});
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
private async Task SetHainingMealSupplementAsync(MealSupplementRecordEntity mealSupplement)
|
|
{
|
|
await DbContext.Client.Ado.BeginTranAsync();
|
|
try
|
|
{
|
|
var hnResponse = await _hainingMealSupplementApi.AddMealSupplementAsync(new AddHainingMealSupplementRequest
|
|
{
|
|
Code = mealSupplement.Code,
|
|
Amount = mealSupplement.Amount,
|
|
Date = mealSupplement.Date.Date,
|
|
Remark = mealSupplement.Remark,
|
|
UniqueId = mealSupplement.UniqueId,
|
|
});
|
|
|
|
(hnResponse == null || hnResponse.Code != 200).ThrowIfTrue("海宁餐补写入失败:" + hnResponse?.Message);
|
|
|
|
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.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();
|
|
}
|
|
}
|
|
}
|