From 99aa2a71a288b2bd2840b12f88b9620b32b973c3 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 22 May 2024 16:39:08 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E9=A4=90=E8=A1=A5=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MealSupplementsRecordController.cs | 13 ++- .../MealSupplementInitImportRequest.cs | 15 ++++ .../Request/MealSupplementRecordMyRequest.cs | 7 +- .../Services/IMealSupplementRecordService.cs | 2 + .../Implements/MealSupplementRecordService.cs | 83 +++++++++++++++++-- 5 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementInitImportRequest.cs diff --git a/Src/Dlhis.Canteen/MealSupplementRecord/Controller/MealSupplementsRecordController.cs b/Src/Dlhis.Canteen/MealSupplementRecord/Controller/MealSupplementsRecordController.cs index 173cff3b..fcf64442 100644 --- a/Src/Dlhis.Canteen/MealSupplementRecord/Controller/MealSupplementsRecordController.cs +++ b/Src/Dlhis.Canteen/MealSupplementRecord/Controller/MealSupplementsRecordController.cs @@ -1,6 +1,7 @@ using Dlhis.Canteen.MealSupplementRecord.Request; using Dlhis.Canteen.MealSupplementRecord.Response; using Dlhis.Canteen.MealSupplementRecord.Services; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; using Kurisu.Core.DataAccess.Dto; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -44,4 +45,14 @@ public class MealSupplementsRecordController { return await _mealSupplementRecordService.GetMyMealSupplementRecords(request); } -} + + /// + /// 期初餐补余额导入 + /// + [HttpPost("init/import")] + [Transactional] + public async Task InitImportAsync(List requests) + { + await _mealSupplementRecordService.InitImportAsync(requests); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementInitImportRequest.cs b/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementInitImportRequest.cs new file mode 100644 index 00000000..efe33828 --- /dev/null +++ b/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementInitImportRequest.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace Dlhis.Canteen.MealSupplementRecord.Request; + +public class MealSupplementInitImportRequest +{ + [Required(ErrorMessage = "工号不能为空")] + public string Code { get; set; } + + /// + /// 余额 + /// + [Range(1, int.MaxValue, ErrorMessage = "余额有误")] + public decimal Balance { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementRecordMyRequest.cs b/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementRecordMyRequest.cs index b5bf1d15..38cbf6ca 100644 --- a/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementRecordMyRequest.cs +++ b/Src/Dlhis.Canteen/MealSupplementRecord/Request/MealSupplementRecordMyRequest.cs @@ -9,8 +9,11 @@ public class MealSupplementRecordMyRequest : IValidatableObject public IEnumerable Validate(ValidationContext validationContext) { - SearchDateTimePeriod.End = SearchDateTimePeriod.End.Value.AddDays(1).AddSeconds(-1); + if (SearchDateTimePeriod.End.HasValue) + { + SearchDateTimePeriod.End = SearchDateTimePeriod.End.Value.AddDays(1).AddSeconds(-1); + } yield break; } -} +} \ No newline at end of file diff --git a/Src/Dlhis.Canteen/MealSupplementRecord/Services/IMealSupplementRecordService.cs b/Src/Dlhis.Canteen/MealSupplementRecord/Services/IMealSupplementRecordService.cs index a14785b9..0ca74ff9 100644 --- a/Src/Dlhis.Canteen/MealSupplementRecord/Services/IMealSupplementRecordService.cs +++ b/Src/Dlhis.Canteen/MealSupplementRecord/Services/IMealSupplementRecordService.cs @@ -9,4 +9,6 @@ public interface IMealSupplementRecordService : IScopeDependency Task> GetPageAsync(MealSupplementRecordRequest request); Task GetMyMealSupplementRecords(MealSupplementRecordMyRequest request); + + Task InitImportAsync(List requests); } diff --git a/Src/Dlhis.Canteen/MealSupplementRecord/Services/Implements/MealSupplementRecordService.cs b/Src/Dlhis.Canteen/MealSupplementRecord/Services/Implements/MealSupplementRecordService.cs index 1cb3ec82..35b8c7ba 100644 --- a/Src/Dlhis.Canteen/MealSupplementRecord/Services/Implements/MealSupplementRecordService.cs +++ b/Src/Dlhis.Canteen/MealSupplementRecord/Services/Implements/MealSupplementRecordService.cs @@ -3,7 +3,9 @@ using Dlhis.Canteen.MealSupplementRecord.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Canteen.Entities; +using Dlhis.Entity.Canteen.Enums; using Dlhis.Sys.Api; +using Kurisu.AspNetCore.Utils; using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.Core.DataAccess.Dto; using Kurisu.Core.User.Abstractions; @@ -15,7 +17,6 @@ namespace Dlhis.Canteen.MealSupplementRecord.Services.Implements; public class MealSupplementRecordService : IMealSupplementRecordService { - private readonly IDbContext _dbContext; private readonly ICurrentUser _currentUser; private readonly ITenantApi _tenantApi; @@ -36,8 +37,8 @@ public class MealSupplementRecordService : IMealSupplementRecordService .WhereIF(request.Workplace.IsPresent(), x => x.Workplace == request.Workplace) .WhereIF(request.Factory.IsPresent(), x => x.Factory == request.Factory) .WhereIF(request.DateTimePeriod?.Start.HasValue == true - && request.DateTimePeriod?.End.HasValue == true, - x => x.Date >= request.DateTimePeriod.Start && x.Date <= request.DateTimePeriod.End) + && request.DateTimePeriod?.End.HasValue == true, + x => x.Date >= request.DateTimePeriod.Start && x.Date <= request.DateTimePeriod.End) .WhereIF(request.Search.IsPresent(), (x, ga) => x.Code == request.Search || ga.Name == request.Search) .Select((x, ga) => new MealSupplementRecordResponse { @@ -70,7 +71,6 @@ public class MealSupplementRecordService : IMealSupplementRecordService } catch (Exception) { - } @@ -110,4 +110,77 @@ public class MealSupplementRecordService : IMealSupplementRecordService Items = data, }; } -} + + public async Task InitImportAsync(List requests) + { + var today = DateTime.Now.Date; + foreach (var request in requests) + { + var gainfo = await _dbContext.Queryable().Where(x => x.Code == request.Code).FirstAsync(); + if (gainfo == null) + { + continue; + } + + await CommonHelper.RetryAsync(async () => + { + var mealUser = await _dbContext.Queryable().Where(x => x.UserCode == request.Code).FirstAsync(); + if (mealUser == null) + { + mealUser = new GainfoMealEntity + { + UserNo = gainfo.No, + UserCode = gainfo.Code, + Balance = 0, + BalanceOfRecharge = 0, + BalanceOfSubsidies = 0, + Version = 0 + }; + + await _dbContext.InsertAsync(mealUser); + } + + var b = mealUser.Balance += request.Balance; + + await _dbContext.Updateable() + .SetColumns(x => x.Version == x.Version + 1) + .SetColumns(x => x.Balance == b) + .SetColumns(x => x.ModifiedTime == DateTime.Now) + .Where(x => x.UserCode == mealUser.UserCode && x.Version == mealUser.Version) + .ExecuteCommandThrowVersionAsync(); + }, 5); + + await _dbContext.InsertAsync(new BalanceRecordEntity + { + Amount = request.Balance, + ApplicationType = CanteenApplicaitonTypeEnumType.MealSupplement, + Type = BalanceRecordTypeEnumType.In, + BusinessPlaceCode = null, + BusinessShiftCode = null, + ShiftType = null, + CostDate = today, + CostDateTime = DateTime.Now, + UserNo = gainfo.No, + No = SnowFlakeHelper.Instance.NextId().ToString(), + UserCode = gainfo.Code, + TenantId = gainfo.Factory, + Status = BalanceRecordStatusEnumType.Valid, + Source = BalanceRecordSourceEnumType.LocalTenantSubsidies, + Remark = "期初导入", + CostId = gainfo.CostId, + SubsidiesFromMonth = today.ToString("yyyyMM"), + OriginTenantId = gainfo.Factory, + Werks = gainfo.Werks, + Name = gainfo.Name, + IsCostCrossTenant = false, + DepartmentNo = gainfo.DepartmentNo, + DeviceUniqueNo = null, + + CreatedBy = 1, + CreateTime = DateTime.Now, + ModifiedBy = 1, + ModifiedTime = DateTime.Now, + }); + } + } +} \ No newline at end of file