From b28e6955429a4d303fcd92aa0615f85c1d8a320d Mon Sep 17 00:00:00 2001 From: liguoyang Date: Tue, 18 Jun 2024 10:00:31 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=8B=86=E5=88=86=E6=B5=B7=E5=AE=81?= =?UTF-8?q?=E9=A4=90=E8=A1=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Jobs/MealChunengSupplementHandlerJob.cs | 165 ++++++++++++++++++ .../Jobs/MealHainingSupplementHandlerJob.cs | 165 ++++++++++++++++++ 2 files changed, 330 insertions(+) create mode 100644 Src/Dlhis.ScheduledTask/Jobs/MealChunengSupplementHandlerJob.cs create mode 100644 Src/Dlhis.ScheduledTask/Jobs/MealHainingSupplementHandlerJob.cs diff --git a/Src/Dlhis.ScheduledTask/Jobs/MealChunengSupplementHandlerJob.cs b/Src/Dlhis.ScheduledTask/Jobs/MealChunengSupplementHandlerJob.cs new file mode 100644 index 00000000..9d652ee3 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Jobs/MealChunengSupplementHandlerJob.cs @@ -0,0 +1,165 @@ +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.Attributes; +using Dlhis.ScheduledTask.Common.Request; +using Dlhis.ScheduledTask.Common.Services; +using Hangfire; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions; +using Kurisu.AspNetCore.Utils; +using Kurisu.AspNetCore.Utils.Extensions; + +namespace Dlhis.ScheduledTask.Jobs; + +/// +/// 餐补核算 +/// +[RecurringJob(nameof(MealChunengSupplementHandlerJob), "0 0/30 * * * ?", Name = "储能餐补核算")] +public class MealChunengSupplementHandlerJob : BaseJob +{ + private readonly IHainingMealSupplementApi _hainingMealSupplementApi; + private readonly IJinkoJIPService _jinkoJIPService; + + public MealChunengSupplementHandlerJob(IServiceProvider serviceProvider, + IHainingMealSupplementApi hainingMealSupplementApi, + IJinkoJIPService jinkoJIPService) : base(serviceProvider) + { + _hainingMealSupplementApi = hainingMealSupplementApi; + _jinkoJIPService = jinkoJIPService; + } + + protected override async Task RunAsync(IServiceProvider serviceProvider) + { + //查询待处理餐补 + var mealSupplements = await DbContext.Queryable() + .Where(x => x.Status == MealSupplementStatusEnumtype.Wait || x.Status == MealSupplementStatusEnumtype.Error) + .Where(x => x.Workplace == "5" && x.Factory == "501")//储能 + .Where(x => !x.Sync) + .ToListAsync(); + + foreach (var item in mealSupplements) + { + var gainfo = await DbContext.Queryable().Where(x => x.Code == item.Code).SingleAsync(); + if (gainfo == null) + { + gainfo = await DbContext.Queryable().AS("GainfoLeave").Where(x => x.Code == item.Code).SingleAsync(); + + if (gainfo == null) + { + await DbContext.Updateable() + .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(x => x.TodoByCodeAsync(item.Code)); + + continue; + } + else if (gainfo.ResignationDate < item.Date) + { + try + { + var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List + { + new() + { + UniqueId = item.UniqueId, + Status = "FAIL", + Msg = "人员离职" + } + }); + + await DbContext.Updateable() + .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 + { + } + + continue; + } + } + + + //创建余额数据 + var mealUser = await DbContext.Queryable().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, + }; + + await DbContext.InsertAsync(mealUser); + } + + await SetHainingMealSupplementAsync(item); + } + } + + private async Task SetHainingMealSupplementAsync(MealSupplementRecordEntity mealSupplement) + { + await DbContext.Client.Ado.BeginTranAsync(); + try + { + var hnResponse = await _hainingMealSupplementApi.AddHsChuNengMealSupplementAsync(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 + { + new() + { + UniqueId = mealSupplement.UniqueId, + Status = "SUCCESS", + Msg = "操作成功" + } + }); + + //更新餐补信息为成功(同步成功) + await DbContext.Updateable() + .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() + .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(); + } + } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Jobs/MealHainingSupplementHandlerJob.cs b/Src/Dlhis.ScheduledTask/Jobs/MealHainingSupplementHandlerJob.cs new file mode 100644 index 00000000..5639f42c --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Jobs/MealHainingSupplementHandlerJob.cs @@ -0,0 +1,165 @@ +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.Attributes; +using Dlhis.ScheduledTask.Common.Request; +using Dlhis.ScheduledTask.Common.Services; +using Hangfire; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions; +using Kurisu.AspNetCore.Utils; +using Kurisu.AspNetCore.Utils.Extensions; + +namespace Dlhis.ScheduledTask.Jobs; + +/// +/// 餐补核算 +/// +[RecurringJob(nameof(MealHainingSupplementHandlerJob), "0 0/30 * * * ?", Name = "海宁餐补核算")] +public class MealHainingSupplementHandlerJob : BaseJob +{ + private readonly IHainingMealSupplementApi _hainingMealSupplementApi; + private readonly IJinkoJIPService _jinkoJIPService; + + public MealHainingSupplementHandlerJob(IServiceProvider serviceProvider, + IHainingMealSupplementApi hainingMealSupplementApi, + IJinkoJIPService jinkoJIPService) : base(serviceProvider) + { + _hainingMealSupplementApi = hainingMealSupplementApi; + _jinkoJIPService = jinkoJIPService; + } + + protected override async Task RunAsync(IServiceProvider serviceProvider) + { + //查询待处理餐补 + var mealSupplements = await DbContext.Queryable() + .Where(x => x.Status == MealSupplementStatusEnumtype.Wait || x.Status == MealSupplementStatusEnumtype.Error) + .Where(x => x.Workplace == "5" && x.Factory != "501") //非储能 + .Where(x => !x.Sync) + .ToListAsync(); + + foreach (var item in mealSupplements) + { + var gainfo = await DbContext.Queryable().Where(x => x.Code == item.Code).SingleAsync(); + if (gainfo == null) + { + gainfo = await DbContext.Queryable().AS("GainfoLeave").Where(x => x.Code == item.Code).SingleAsync(); + + if (gainfo == null) + { + await DbContext.Updateable() + .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(x => x.TodoByCodeAsync(item.Code)); + + continue; + } + else if (gainfo.ResignationDate < item.Date) + { + try + { + var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List + { + new() + { + UniqueId = item.UniqueId, + Status = "FAIL", + Msg = "人员离职" + } + }); + + await DbContext.Updateable() + .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 + { + } + + continue; + } + } + + + //创建余额数据 + var mealUser = await DbContext.Queryable().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, + }; + + await DbContext.InsertAsync(mealUser); + } + + await SetHainingMealSupplementAsync(item); + } + } + + 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 + { + new() + { + UniqueId = mealSupplement.UniqueId, + Status = "SUCCESS", + Msg = "操作成功" + } + }); + + //更新餐补信息为成功(同步成功) + await DbContext.Updateable() + .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() + .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(); + } + } +} \ No newline at end of file