using Dlhis.Canteen.Order.Dto; using Dlhis.Canteen.Setting.Services; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Canteen.Entities; using Dlhis.Entity.Canteen.Enums; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Kurisu.AspNetCore.CustomClass; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; using Microsoft.Extensions.Primitives; namespace Dlhis.Canteen.Order.Services.ConsumeHandlers; /// /// 订单通用处理 /// public class OrderCommonHandler : IOrderCommonHandler { private readonly IDbContext _dbContext; private readonly ICanteenSettingService _canteenSettingService; private readonly ILogger _logger; private readonly IHttpContextAccessor _httpContextAccessor; public OrderCommonHandler(IDbContext dbContext, IHttpContextAccessor httpContextAccessor, ICanteenSettingService canteenSettingService, ILogger logger) { _dbContext = dbContext; _canteenSettingService = canteenSettingService; _httpContextAccessor = httpContextAccessor; _logger = logger; } /// /// 设置设备ip /// /// public async Task SetDeviceIpInfoAsync(string deviceNo) { try { StringValues ip = ""; if (_httpContextAccessor.HttpContext?.Request.Headers.TryGetValue("X-Requested-DeviceIp", out ip) == true) { var device = await _dbContext.Queryable().Where(x => x.UniqueNo == deviceNo).SingleAsync(); device.ConnectionInfo = new { ip = ip.ToString() }.ToJson(); await _dbContext.UpdateAsync(device); } _logger.LogInformation("设备ip获取:{deviceNo} {ip}", deviceNo, ip); } catch (Exception) { //ignore } } /// /// 消费前预检 /// /// 设备号 /// 消费时间 /// public async Task PreCheckAsync(string deviceUniqueNo, DateTime costDateTime) { var device = await _dbContext.Queryable().Where(x => x.UniqueNo == deviceUniqueNo).SingleAsync(); device.ThrowIfNull("设备尚未登记"); device.Enable.ThrowIfFalse("设备已被停用"); (device.Type != DeviceTypeEnumType.Meal).ThrowIfTrue("设备类型有误"); var bindingInfo = await _dbContext.Queryable() .LeftJoin((x, b) => x.BusinessPlaceCode == b.Code) .LeftJoin((x, b, bs) => bs.PlaceCode == b.Code) .Where(x => x.DeviceUniqueNo == deviceUniqueNo && x.BusinessPlaceCode.HasValue) .Select((x, b, bs) => new { b.Code, bs.ShiftCode, Factory = b.TenantId }) .SingleAsync(); bindingInfo.ThrowIfNull("设备尚未登记营业点"); //获取当前消费餐次 var shifts = await _dbContext.Queryable() .Where(x => x.ShiftCode == bindingInfo.ShiftCode) .Select(x => new { x.ShiftType, x.Start, x.End }) .ToListAsync(); shifts.ThrowIfEmpty("营业点餐次尚未设置"); var shift = shifts.FirstOrDefault(x => new TimePeriod { Start = x.Start, End = x.End }.IsPresent(costDateTime)); shift.ThrowIfNull("非就餐时间"); var costDate = await _canteenSettingService.GetCostDateAsync(bindingInfo.Factory, costDateTime); return new OrderCheckResultDto { Factory = bindingInfo.Factory, CostDate = costDate, ShiftType = shift!.ShiftType, BusinessPlaceCode = bindingInfo.Code, BusinessShiftCode = bindingInfo.ShiftCode }; } /// /// 消费更新处理 /// /// /// public async Task TryConsumeAsync(long orderId, Func action) { try { await action(); await _dbContext.Updateable() .SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Success) .SetColumns(x => x.ModifiedTime == DateTime.Now) .Where(x => x.Id == orderId) .ExecuteCommandAsync(); } catch (Exception ex) { await _dbContext.Updateable() .SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Failure) .SetColumns(x => x.ModifiedTime == DateTime.Now) .SetColumns(x => x.Result == ex.Message) .Where(x => x.Id == orderId) .ExecuteCommandAsync(); throw; } } }