From e52cf0f37ad18f611c72061185632e4bb6519256 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 14 Aug 2024 15:43:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Order/Controllers/OrdersController.cs | 50 +------ .../Order/Services/Implements/OrderService.cs | 138 ++++++++---------- 2 files changed, 68 insertions(+), 120 deletions(-) diff --git a/Src/Dlhis.Canteen/Order/Controllers/OrdersController.cs b/Src/Dlhis.Canteen/Order/Controllers/OrdersController.cs index 51e0027c..59ab64b3 100644 --- a/Src/Dlhis.Canteen/Order/Controllers/OrdersController.cs +++ b/Src/Dlhis.Canteen/Order/Controllers/OrdersController.cs @@ -1,12 +1,10 @@ -using System.Diagnostics; -using Dlhis.Canteen.Dishes.Request; +using Dlhis.Canteen.Dishes.Request; using Dlhis.Canteen.Order.Request; using Dlhis.Canteen.Order.Response; using Dlhis.Canteen.Order.Services; using Kurisu.AspNetCore.DataAccess.Dto; using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; using Kurisu.AspNetCore.UnifyResultAndValidation; -using Kurisu.AspNetCore.Utils.Extensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -22,13 +20,11 @@ public class OrdersController : ControllerBase { private readonly IOrderService _orderService; private readonly IBookingOrderService _bookingOrderService; - private readonly ILogger _logger; - public OrdersController(IOrderService orderService, IBookingOrderService bookingOrderService, ILogger logger) + public OrdersController(IOrderService orderService, IBookingOrderService bookingOrderService) { _orderService = orderService; _bookingOrderService = bookingOrderService; - _logger = logger; } /// @@ -41,27 +37,8 @@ public class OrdersController : ControllerBase [IgnoreTenant] public async Task> AddAsync(OrderAddRequest request) { - var sw = Stopwatch.StartNew(); var result = await _orderService.AddAsync(request); - try - { - return result.Response; - } - finally - { - var ms = sw.ElapsedMilliseconds; - try - { - if (ms > 1000) - { - _logger.LogInformation("消费请求:{json}. 耗时记录:{log}", request.ToJson(), result.Logs.Join("\r\n")); - } - } - catch (Exception) - { - //ignore - } - } + return result.Response; } /// @@ -74,27 +51,8 @@ public class OrdersController : ControllerBase [IgnoreTenant] public async Task> DeviceConsume(OrderAddRequest request) { - var sw = Stopwatch.StartNew(); var result = await _orderService.AddAsync(request); - try - { - return result.Response; - } - finally - { - var ms = sw.ElapsedMilliseconds; - try - { - if (ms > 1000) - { - _logger.LogInformation("消费请求:{json}. 耗时记录:{log}", request.ToJson(), result.Logs.Join("\r\n")); - } - } - catch (Exception) - { - //ignore - } - } + return result.Response; } /// diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs index e94823f6..d0c8b274 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using Dlhis.Canteen.Order.Dto; +using Dlhis.Canteen.Order.Dto; using Dlhis.Canteen.Setting.Services; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Canteen.Entities; @@ -24,8 +23,6 @@ namespace Dlhis.Canteen.Order.Services.Implements; public class OrderService : BaseOrderService, IOrderService { private readonly IServiceProvider _serviceProvider; - private Stopwatch _sw; - private readonly List _logs = new(); public OrderService(ICanteenSettingService canteenSettingService, IDbContext dbContext, @@ -54,44 +51,11 @@ public class OrderService : BaseOrderService, IOrderService /// public async Task AddAsync(OrderAddRequest request) { - _sw = new Stopwatch(); - var gainfo = await GetUserAsync(request); - _logs.Add("获取人员耗时:" + _sw.ElapsedMilliseconds); - _sw.Restart(); - - var message = await ToDoAsync(request, gainfo); - - //获取人员余额 - var data = await DbContext.Queryable() - .LeftJoin((x, ga) => x.UserNo == ga.No) - .LeftJoin((x, ga, department) => ga.DepartmentNo == department.No) - .Where(x => x.UserNo == gainfo.No) - .Select((x, ga, department) => new OrderResultResponse - { - UserCode = ga.Code, - UserName = ga.Name, - DepartmentNo = ga.DepartmentNo, - DepartmentName = department.Name, - CurrentBalance = x.Balance - }) - .SingleAsync(); - - var result = new DefaultApiResult - { - Code = message.IsPresent() ? ApiStateCode.Error : ApiStateCode.Success, - Data = data, - Msg = message - }; - - _logs.Add("获取余额:" + _sw.ElapsedMilliseconds); - _sw.Restart(); - return new OrderResultResponseWithLogs { - Response = result, - Logs = _logs + Response = await ToDoAsync(request, gainfo) }; } @@ -306,8 +270,6 @@ public class OrderService : BaseOrderService, IOrderService var order = await DbContext.Queryable().Where(x => x.Id == orderId).SingleAsync(); //校验 await Verify(order); - _logs.Add("消费请求校验:" + _sw.ElapsedMilliseconds); - _sw.Restart(); await CommonHelper.RetryAsync(async () => { @@ -327,9 +289,6 @@ public class OrderService : BaseOrderService, IOrderService var balanceRecord = order.Adapt(); await DbContext.InsertAsync(balanceRecord); }, 5); - - _logs.Add("消费写入:" + _sw.ElapsedMilliseconds); - _sw.Restart(); } /// @@ -482,9 +441,6 @@ public class OrderService : BaseOrderService, IOrderService }.IsPresent(request.CostDateTime)); shift.ThrowIfNull("非就餐时间,无法消费"); - _logs.Add("检验设备和营业点参数:" + _sw.ElapsedMilliseconds); - _sw.Restart(); - var costDate = await GetCostDateAsync(shift, request.CostDateTime); (request.Source == BalanceRecordSourceEnumType.QrCode && businessPlaceInfo.TenantId != gainfo.Factory).ThrowIfTrue("禁止跨厂区消费"); @@ -527,8 +483,6 @@ public class OrderService : BaseOrderService, IOrderService }; var orderId = await DbContext.InsertReturnIdentityAsync(order); - _logs.Add("临时order写入:" + _sw.ElapsedMilliseconds); - _sw.Restart(); try { @@ -549,9 +503,6 @@ public class OrderService : BaseOrderService, IOrderService .SetColumns(x => x.ModifiedTime == DateTime.Now) .Where(x => x.Id == orderId) .ExecuteCommandAsync(); - - _logs.Add("更新image和订单信息:" + _sw.ElapsedMilliseconds); - _sw.Restart(); } catch (Exception ex) { @@ -573,20 +524,55 @@ public class OrderService : BaseOrderService, IOrderService /// /// /// - private async Task ToDoAsync(OrderAddRequest request, GainfoEntity gainfo) + private async Task> ToDoAsync(OrderAddRequest request, GainfoEntity gainfo) { var message = string.Empty; if (request.Source == BalanceRecordSourceEnumType.EMealVoucher) { + var eMealVoucherQrCode = await DbContext.Queryable().Where(x => x.QrCode == request.UserUniqueNo).SingleAsync(); + if (eMealVoucherQrCode == null) + { + message = "无法识别"; + return new DefaultApiResult + { + Code = ApiStateCode.Error, + Data = null, + Msg = message + }; + } + + var originData = eMealVoucherQrCode.OriginData.Split(':'); + + var no = originData[0]; + var ts = originData[1]; + var idNo = originData[3]; + try { - await EMealVoucherConsumeAsync(request.Adapt(), gainfo); + await EMealVoucherConsumeAsync(request.Adapt(), gainfo, eMealVoucherQrCode.OriginData, no, ts, idNo); } catch (Exception ex) { message = ex.Message; } + + //获取餐券余额 + var data = await DbContext.Queryable() + .LeftJoin((x, d) => x.No == d.No) + .Where((x, d) => d.IdNo == idNo) + .Select((x, d) => new OrderResultResponse + { + CurrentBalance = d.Balance + }) + .SingleAsync(); + + return new DefaultApiResult + { + Code = message.IsPresent() ? ApiStateCode.Error : ApiStateCode.Success, + Data = data, + Msg = message + }; } else { @@ -633,9 +619,29 @@ public class OrderService : BaseOrderService, IOrderService message = ex.Message; } } - } - return message; + //获取人员余额 + var data = await DbContext.Queryable() + .LeftJoin((x, ga) => x.UserNo == ga.No) + .LeftJoin((x, ga, department) => ga.DepartmentNo == department.No) + .Where(x => x.UserNo == gainfo.No) + .Select((x, ga, department) => new OrderResultResponse + { + UserCode = ga.Code, + UserName = ga.Name, + DepartmentNo = ga.DepartmentNo, + DepartmentName = department.Name, + CurrentBalance = x.Balance + }) + .SingleAsync(); + + return new DefaultApiResult + { + Code = message.IsPresent() ? ApiStateCode.Error : ApiStateCode.Success, + Data = data, + Msg = message + }; + } async Task ToCheckAbleOfflineConsumeAsync(string thirdNo) { @@ -669,19 +675,14 @@ public class OrderService : BaseOrderService, IOrderService } - private async Task EMealVoucherConsumeAsync(ConsumeRequest request, GainfoEntity gainfo) + private async Task EMealVoucherConsumeAsync(ConsumeRequest request, GainfoEntity gainfo, string originData, string no, string ts, string idNo) { - var eMealVoucherQrCode = await DbContext.Queryable().Where(x => x.QrCode == request.UserUniqueNo).SingleAsync(); - var originData = eMealVoucherQrCode.OriginData.Split(':'); - - var no = originData[0]; - var idNo = originData[3]; var nowDate = DateTime.Now.Date; var current = await DbContext.Queryable() .Where(x => x.No == no && x.IdNo == idNo).SingleAsync(); (nowDate >= current.Start && nowDate <= current.End).ThrowIfFalse("餐券已过期"); - var time = DateTimeOffset.FromUnixTimeMilliseconds(Convert.ToInt64(originData[1].Reverse().Join(string.Empty))).ToLocalTime().DateTime; + var time = DateTimeOffset.FromUnixTimeMilliseconds(Convert.ToInt64(ts.Reverse().Join(string.Empty))).ToLocalTime().DateTime; (time.AddMinutes(60) >= DateTime.Now).ThrowIfFalse("二维码已过期"); var eMealVoucher = await DbContext.Queryable().Where(x => x.No == no).SingleAsync(); @@ -689,9 +690,6 @@ public class OrderService : BaseOrderService, IOrderService var checkData = await PreCheckAsync(request.DeviceUniqueNo, request.CostDateTime); eMealVoucher.Factories.Contains(checkData.BusinessPlaceTenantId).ThrowIfFalse("禁止在当前厂区消费"); - _logs.Add("检验设备和营业点参数:" + _sw.ElapsedMilliseconds); - _sw.Restart(); - var order = new CanteenOrderEntity { RequestJson = request.ToJson(), @@ -700,7 +698,7 @@ public class OrderService : BaseOrderService, IOrderService ApplicationType = CanteenApplicationTypeEnumType.Device, Name = gainfo.Name, Source = request.Source, - UserNo = eMealVoucherQrCode.OriginData, //记录为当前消费券 + UserNo = originData, //记录为当前消费券 UserCode = gainfo.Code, Amount = request.Amount, BusinessPlaceCode = checkData.BusinessPlaceCode, @@ -725,8 +723,6 @@ public class OrderService : BaseOrderService, IOrderService }; var orderId = await DbContext.InsertReturnIdentityAsync(order); - _logs.Add("临时order写入:" + _sw.ElapsedMilliseconds); - _sw.Restart(); try { @@ -749,17 +745,11 @@ public class OrderService : BaseOrderService, IOrderService await DbContext.InsertAsync(balanceRecord); }, 5); - _logs.Add("消费写入:" + _sw.ElapsedMilliseconds); - _sw.Restart(); - await DbContext.Updateable() .SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Success) .SetColumns(x => x.ModifiedTime == DateTime.Now) .Where(x => x.Id == orderId) .ExecuteCommandAsync(); - - _logs.Add("更新image和订单信息:" + _sw.ElapsedMilliseconds); - _sw.Restart(); } catch (Exception ex) {