146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 订单通用处理
|
|
/// </summary>
|
|
public class OrderCommonHandler : IOrderCommonHandler
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICanteenSettingService _canteenSettingService;
|
|
private readonly ILogger<OrderCommonHandler> _logger;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public OrderCommonHandler(IDbContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
ICanteenSettingService canteenSettingService,
|
|
ILogger<OrderCommonHandler> logger)
|
|
{
|
|
_dbContext = dbContext;
|
|
_canteenSettingService = canteenSettingService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置设备ip
|
|
/// </summary>
|
|
/// <param name="deviceNo"></param>
|
|
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<DeviceEntity>().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
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消费前预检
|
|
/// </summary>
|
|
/// <param name="deviceUniqueNo">设备号</param>
|
|
/// <param name="costDateTime">消费时间</param>
|
|
/// <returns></returns>
|
|
public async Task<OrderCheckResultDto> PreCheckAsync(string deviceUniqueNo, DateTime costDateTime)
|
|
{
|
|
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == deviceUniqueNo).SingleAsync();
|
|
device.ThrowIfNull("设备尚未登记");
|
|
device.Enable.ThrowIfFalse("设备已被停用");
|
|
(device.Type != DeviceTypeEnumType.Meal).ThrowIfTrue("设备类型有误");
|
|
|
|
var bindingInfo = await _dbContext.Queryable<DeviceRegistryEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, b) => x.BusinessPlaceCode == b.Code)
|
|
.LeftJoin<BusinessPlaceShiftRelationshipEntity>((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<BusinessShiftMealRelationshipEntity>()
|
|
.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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消费更新处理
|
|
/// </summary>
|
|
/// <param name="orderId"></param>
|
|
/// <param name="action"></param>
|
|
public async Task TryConsumeAsync(long orderId, Func<Task> action)
|
|
{
|
|
try
|
|
{
|
|
await action();
|
|
|
|
await _dbContext.Updateable<CanteenOrderEntity>()
|
|
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Success)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.Id == orderId)
|
|
.ExecuteCommandAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await _dbContext.Updateable<CanteenOrderEntity>()
|
|
.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;
|
|
}
|
|
}
|
|
} |