diff --git a/Src/Dlhis.Canteen/Order/Services/BaseOrderService.cs b/Src/Dlhis.Canteen/Order/Services/BaseOrderService.cs index 73c719d2..16ed5fee 100644 --- a/Src/Dlhis.Canteen/Order/Services/BaseOrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/BaseOrderService.cs @@ -37,25 +37,14 @@ public abstract class BaseOrderService /// 获取参数配置 /// /// + /// /// /// /// /// - protected async Task GetSettingAsync(string tenantId, CanteenSettingItemEnumType item, T defaultValue = default) + protected async Task GetSettingAsync(string userNo, string tenantId, CanteenSettingItemEnumType item, T defaultValue = default) { - if (_settings == null) - { - var subjectId = _currentUser.GetIntSubjectId(); - var gainfo = await _dbContext.Queryable().Where(x => x.IdentityUserId == subjectId).FirstAsync(); - var jinkoCode = _currentUser.GetUserClaim("jinko-code") ?? string.Empty; - if (jinkoCode.IsPresent()) - { - gainfo = await _dbContext.Queryable().Where(x => x.Code == jinkoCode).FirstAsync(); - } - - _settings = await _canteenSettingService.GetUserSettingAsync(gainfo.No, tenantId); - } - + _settings ??= await _canteenSettingService.GetUserSettingAsync(userNo, tenantId); if (_settings.TryGetValue(item, out var v)) { diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/BookingOrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/BookingOrderService.cs index 0c42aed6..17054b7b 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/BookingOrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/BookingOrderService.cs @@ -41,7 +41,7 @@ public class BookingOrderService : BaseOrderService, IBookingOrderService //预定时间 var now = DateTime.Now; - var bookingDeadline = await GetSettingAsync(businessPlace.TenantId, CanteenSettingItemEnumType.BookingDeadline) ?? new TimeOnly(20, 30, 00); + var bookingDeadline = await GetSettingAsync(gainfo.No, businessPlace.TenantId, CanteenSettingItemEnumType.BookingDeadline) ?? new TimeOnly(20, 30, 00); var dealLine = DateTime.Today.Add(bookingDeadline.ToTimeSpan()); (now >= dealLine).ThrowIfTrue("超出订餐时间"); @@ -73,7 +73,7 @@ public class BookingOrderService : BaseOrderService, IBookingOrderService No = SnowFlakeHelper.Instance.NextId().ToString(), SubsidiesFromMonth = now.ToString("yyyyMM"), IsOffline = false, - IsCostCrossTenant = await GetSettingAsync(businessPlace.TenantId, CanteenSettingItemEnumType.AllowCrossTenant), + IsCostCrossTenant = await GetSettingAsync(gainfo.No, businessPlace.TenantId, CanteenSettingItemEnumType.AllowCrossTenant), OriginTenantId = gainfo.Factory, //人员可能跨基地消费 @@ -213,7 +213,7 @@ public class BookingOrderService : BaseOrderService, IBookingOrderService var order = await DbContext.Queryable().Where(x => x.No == request.No).FirstAsync(); order.ThrowIfNull("订单不存在"); - var bookingDeadline = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.BookingDeadline) ?? new TimeOnly(20, 30, 00); + var bookingDeadline = await GetSettingAsync(order.No, order.TenantId, CanteenSettingItemEnumType.BookingDeadline) ?? new TimeOnly(20, 30, 00); var dealLine = DateTime.Today.Add(bookingDeadline.ToTimeSpan()); (DateTime.Now >= dealLine).ThrowIfTrue("不在订单取消允许范围内,不可操作"); diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs index 703badda..69a3093a 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs @@ -21,16 +21,16 @@ namespace Dlhis.Canteen.Order.Services.Implements; public class OrderService : BaseOrderService, IOrderService { - private readonly RedisCache _redisCache; + private readonly IServiceProvider _serviceProvider; public OrderService(ICanteenSettingService canteenSettingService, IDbContext dbContext, ICurrentUser currentUser, - RedisCache redisCache, + IServiceProvider serviceProvider, ILogger logger) : base(canteenSettingService, dbContext, currentUser, logger) { - _redisCache = redisCache; + _serviceProvider = serviceProvider; } public async Task> DeviceConsume(OrderAddRequest request) @@ -49,7 +49,7 @@ public class OrderService : BaseOrderService, IOrderService .WhereIF(request.Source == BalanceRecordSourceEnumType.Card, x => x.CardNo == request.UserUniqueNo) .WhereIF(request.Source != BalanceRecordSourceEnumType.Card && !request.IsUserId, x => x.Code == request.UserUniqueNo) .WhereIF(request.Source != BalanceRecordSourceEnumType.Card && request.IsUserId && s, x => x.Id == userId) - .FirstAsync(); + .SingleAsync(); if (gainfo == null && request.IsOffline) { @@ -66,20 +66,20 @@ public class OrderService : BaseOrderService, IOrderService { if (request.ThirdNo.IsPresent()) { - using (var handler = await _redisCache.LockAsync($"consume:device:{request.ThirdNo}")) + var redisCache = _serviceProvider.GetService(); + + await using var handler = await redisCache.LockAsync($"consume:device:{request.ThirdNo}"); + if (handler.Acquired) { - if (handler.Acquired) + if (await ToCheckAsync(request.ThirdNo)) { - if (await ToCheckAsync(request.ThirdNo)) + try { - try - { - await HandleOfflineAsync(request.Adapt(), gainfo); - } - catch (Exception ex) - { - Logger.LogError(ex, "离线入库错误: 订单号:{no} .{message}", request.ThirdNo, ex.Message); - } + await HandleOfflineAsync(request.Adapt(), gainfo); + } + catch (Exception ex) + { + Logger.LogError(ex, "离线入库错误: 订单号:{no} .{message}", request.ThirdNo, ex.Message); } } } @@ -99,8 +99,6 @@ public class OrderService : BaseOrderService, IOrderService else { gainfo.ThrowIfNull("无消费权限"); - //todo - //await CheckPermissionAsync(gainfo.No, request.DeviceUniqueNo); try { @@ -113,7 +111,6 @@ public class OrderService : BaseOrderService, IOrderService } } - //获取人员余额 var data = await DbContext.Queryable() .LeftJoin((x, ga) => x.UserNo == ga.No) @@ -175,7 +172,6 @@ public class OrderService : BaseOrderService, IOrderService }) .FirstAsync(); - //获取当前消费餐次 var shifts = await DbContext.Queryable().Where(x => x.ShiftCode == businessPlaceInfo.ShiftCode).ToListAsync(); @@ -209,7 +205,7 @@ public class OrderService : BaseOrderService, IOrderService No = SnowFlakeHelper.Instance.NextId().ToString(), SubsidiesFromMonth = request.CostDateTime.ToString("yyyyMM"), IsOffline = request.IsOffline, - IsCostCrossTenant = await GetSettingAsync(businessPlaceInfo.TenantId, CanteenSettingItemEnumType.AllowCrossTenant), + IsCostCrossTenant = businessPlaceInfo!.TenantId != gainfo.Factory, OriginTenantId = gainfo.Factory, Werks = gainfo.Werks, @@ -221,21 +217,21 @@ public class OrderService : BaseOrderService, IOrderService CostDateTime = request.CostDateTime, //标识当前消费的基地 - TenantId = businessPlaceInfo.TenantId, + TenantId = businessPlaceInfo!.TenantId, }; await DbContext.InsertReturnIdentityAsync(order); - var allowNoBalance = await GetSettingAsync(businessPlaceInfo.TenantId, CanteenSettingItemEnumType.AllowNoBalance); + var allowNoBalance = await GetSettingAsync(gainfo.No, businessPlaceInfo.TenantId, CanteenSettingItemEnumType.AllowNoBalance); await CommonHelper.RetryAsync(async () => { if (!allowNoBalance) { GainfoMealEntity gainfoMeal = null; - - using (var handler = await _redisCache.LockAsync($"gainfo:meal:{order.UserNo}", retryInterval: TimeSpan.FromSeconds(1))) + var redisCache = _serviceProvider.GetService(); + using (var handler = await redisCache.LockAsync($"gainfo:meal:{order.UserNo}", retryInterval: TimeSpan.FromSeconds(1))) { if (handler.Acquired) { @@ -298,14 +294,15 @@ public class OrderService : BaseOrderService, IOrderService sr.ShiftCode, x.TenantId }) - .FirstAsync(); + .SingleAsync(); + businessPlaceInfo.ThrowIfNull("设备尚未登记营业点"); //获取当前消费餐次 var shifts = await DbContext.Queryable().Where(x => x.ShiftCode == businessPlaceInfo.ShiftCode).ToListAsync(); shifts.ThrowIfEmpty("营业点餐次尚未设置"); - var shift = shifts.FirstOrDefault(x => new TimePeriod() + var shift = shifts.FirstOrDefault(x => new TimePeriod { Start = x.Start, End = x.End @@ -336,7 +333,7 @@ public class OrderService : BaseOrderService, IOrderService No = SnowFlakeHelper.Instance.NextId().ToString(), SubsidiesFromMonth = request.CostDateTime.ToString("yyyyMM"), IsOffline = request.IsOffline, - IsCostCrossTenant = await GetSettingAsync(businessPlaceInfo.TenantId, CanteenSettingItemEnumType.AllowCrossTenant), + IsCostCrossTenant = await GetSettingAsync(gainfo.No, businessPlaceInfo!.TenantId, CanteenSettingItemEnumType.AllowCrossTenant), OriginTenantId = gainfo.Factory, Werks = gainfo.Werks, @@ -396,13 +393,13 @@ public class OrderService : BaseOrderService, IOrderService } var pageResult = await DbContext.Queryable().Where(x => x.UserNo == gainfo.No) - .LeftJoin((x, bplace) => x.BusinessPlaceCode == bplace.Code) + .LeftJoin((x, bPlace) => x.BusinessPlaceCode == bPlace.Code) .Where(x => x.ApplicationType == CanteenApplicationTypeEnumType.Booking) .OrderBy(x => x.CreateTime, OrderByType.Desc) - .Select((x, bplace) => new OrderAggregationResponse + .Select((x, bPlace) => new OrderAggregationResponse { No = x.No, - BusinessPlaceName = bplace.Name, + BusinessPlaceName = bPlace.Name, CreateTime = x.CreateTime, Remark = x.Remark, OrderStatus = x.OrderStatus, @@ -440,7 +437,7 @@ public class OrderService : BaseOrderService, IOrderService /// private async Task GetCostDateAsync(BusinessShiftMealRelationshipEntity shift, DateTime costDateTime) { - var switchTime = await GetSettingAsync(shift.TenantId, CanteenSettingItemEnumType.BusinessSwitchTime); + var switchTime = await GetSettingAsync(string.Empty, shift.TenantId, CanteenSettingItemEnumType.BusinessSwitchTime); var costDate = costDateTime.Date; @@ -483,7 +480,7 @@ public class OrderService : BaseOrderService, IOrderService .ToListAsync(); //一天消费金额 - var dayCostMax = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.DayCostMax); + var dayCostMax = await GetSettingAsync(order.UserNo, order.TenantId, CanteenSettingItemEnumType.DayCostMax); if (dayCostMax != default) { //must has business place code, use .Value is safe @@ -494,14 +491,14 @@ public class OrderService : BaseOrderService, IOrderService } //一次消费金额 - var onceCostMax = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.OnceCostMax); + var onceCostMax = await GetSettingAsync(order.UserNo, order.TenantId, CanteenSettingItemEnumType.OnceCostMax); if (onceCostMax != default) { (Math.Abs(order.Amount) > onceCostMax).ThrowIfTrue($"单次消费金额不能超出{onceCostMax}元"); } - //超时消费限额 - var dayMarketCostMax = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.DayMarketCostMax); + //超市消费限额 + var dayMarketCostMax = await GetSettingAsync(order.UserNo, order.TenantId, CanteenSettingItemEnumType.DayMarketCostMax); if (dayMarketCostMax != default) { //current businessplace is supermarket @@ -516,11 +513,11 @@ public class OrderService : BaseOrderService, IOrderService } //重复餐次消费 - var allowRepeatInOneShift = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.AllowRepeatInOneShift, true); + var allowRepeatInOneShift = await GetSettingAsync(order.UserNo, order.TenantId, CanteenSettingItemEnumType.AllowRepeatInOneShift, true); if (!allowRepeatInOneShift) { var costCount = todayAllBalanceRecords.Count(x => x.ShiftType == order.ShiftType); - (costCount > 1).ThrowIfTrue($"{order?.ShiftType?.GetDescription()}不允许多次消费"); + (costCount > 1).ThrowIfTrue($"{order.ShiftType?.GetDescription()}不允许多次消费"); } } @@ -534,16 +531,12 @@ public class OrderService : BaseOrderService, IOrderService /// private async Task GenerateConsumeRecordAsync(long orderId) { - var order = await DbContext.Queryable().Where(x => x.Id == orderId).FirstAsync(); + var order = await DbContext.Queryable().Where(x => x.Id == orderId).SingleAsync(); //校验 await Verify(order); - var allowNoBalance = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.AllowNoBalance); + var allowNoBalance = await GetSettingAsync(order.UserNo, order.TenantId, CanteenSettingItemEnumType.AllowNoBalance); - ////降级到读已提交 - // await DbContext.Client.Ado.BeginTranAsync(IsolationLevel.ReadCommitted); - // try - // { await CommonHelper.RetryAsync(async () => { if (!allowNoBalance) @@ -565,14 +558,6 @@ public class OrderService : BaseOrderService, IOrderService var balanceRecord = order.Adapt(); await DbContext.InsertAsync(balanceRecord); }, 5); - - // await DbContext.Client.Ado.CommitTranAsync(); - // } - // catch (Exception) - // { - // await DbContext.Client.Ado.RollbackTranAsync(); - // throw; - // } } /// diff --git a/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs b/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs index 14d2f23c..39300afe 100644 --- a/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs +++ b/Src/Dlhis.Canteen/Setting/Services/Implements/CanteenSettingService.cs @@ -90,17 +90,7 @@ public class CanteenSettingService : ICanteenSettingService settings = settings.OrderBy(x => x.UniqueType).ToList(); var result = new Dictionary(); - settings.ForEach(x => - { - if (result.ContainsKey(x.Item)) - { - result[x.Item] = x.Value; - } - else - { - result.Add(x.Item, x.Value); - } - }); + settings.ForEach(x => { result[x.Item] = x.Value; }); return result; } diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs index 3fd026eb..9458008d 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs @@ -155,9 +155,9 @@ public class GainfoUpdateService : IGainfoUpdateService var originInserts = persons.Where(x => searchCodes.Contains(x.ZWORKCODE)).Where(x => !existsUpdateWorkCodes.Contains(x.ZWORKCODE)).ToList(); var updates = ToGainfoSap(originUpdates); - _logger.LogWarning("SAP分批更新数:{count}", updates.Count); + // _logger.LogWarning("SAP分批更新数:{count}", updates.Count); var inserts = ToGainfoSap(originInserts); - _logger.LogWarning("SAP分批新增数:{count}", inserts.Count); + //_logger.LogWarning("SAP分批新增数:{count}", inserts.Count); foreach (var item in inserts) { @@ -193,7 +193,7 @@ public class GainfoUpdateService : IGainfoUpdateService else { no = await UpdatePersonAsync(item); - _logger.LogWarning("执行更新成功 工号:{code},姓名:{name}", item.ename, item.gname); + //_logger.LogWarning("执行更新成功 工号:{code},姓名:{name}", item.ename, item.gname); await _gainfoPermissionApi.ResetAsync(new GainfoPermissionResetRequest { UserNo = no