This commit is contained in:
2025-03-05 12:42:51 +08:00
parent 4573ac55bc
commit 72f040487a
2 changed files with 3 additions and 139 deletions

View File

@@ -80,7 +80,7 @@ public class OrderCommonHandler : IOrderCommonHandler
{ {
b.Code, b.Code,
bs.ShiftCode, bs.ShiftCode,
Factory = x.TenantId Factory = b.TenantId
}) })
.SingleAsync(); .SingleAsync();

View File

@@ -488,142 +488,6 @@ public class OrderService : IOrderService
return await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == gainfoQrCode.UserCode).SingleAsync(); return await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == gainfoQrCode.UserCode).SingleAsync();
} }
/// <summary>
/// 电子餐券消费
/// </summary>
/// <param name="request"></param>
/// <param name="gainfo"></param>
/// <param name="originData"></param>
/// <param name="no"></param>
/// <param name="idNo"></param>
private async Task EMealVoucherConsumeAsync(ConsumeRequest request, GainfoEntity gainfo, string originData, string no, string idNo)
{
var now = DateTime.Now;
var current = await _dbContext.Queryable<EMealVoucherDetailEntity>()
.Where(x => x.No == no && x.IdNo == idNo)
.Select(x => new { x.Name, x.Start, x.End })
.SingleAsync();
(now >= current.Start && now <= current.End).ThrowIfFalse("餐券已过期");
var eMealVoucher = await _dbContext.Queryable<EMealVoucherEntity>().Where(x => x.No == no).SingleAsync();
var checkData = await PreCheckAsync(request.DeviceUniqueNo, request.CostDateTime);
var isSupermarket = await _dbContext.Queryable<BusinessPlaceEntity>().Where(x => x.Code == checkData.BusinessPlaceCode)
.Select(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.SingleAsync();
isSupermarket.ThrowIfTrue("禁止在超市消费");
eMealVoucher.Factories.Contains(checkData.Factory).ThrowIfFalse("禁止在当前厂区消费");
gainfo.No = originData;
var order = await _orderRepository.CreateDeviceConsumeOrderAsync(checkData, request, gainfo);
await TryConsumeAsync(order.Id, async () =>
{
await CommonHelper.RetryAsync(async () =>
{
var detail = await _dbContext.Queryable<EMealVoucherDetailEntity>().Where(x => x.No == no && x.IdNo == idNo).SingleAsync();
//金额+-
detail.Balance += order.Amount;
(detail.Balance < 0).ThrowIfTrue("余额不足,无法消费");
await _dbContext.Updateable<EMealVoucherDetailEntity>()
.SetColumns(x => x.Balance == detail.Balance)
.SetColumns(x => x.Version == x.Version + 1)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.No == no && x.IdNo == idNo && x.Version == detail.Version) //必须与查询时的version一致
.ExecuteCommandThrowVersionAsync();
//写入记录
var balanceRecord = order.Adapt<BalanceRecordEntity>();
balanceRecord.Remark = $"({no})" + current.Name;
await _dbContext.InsertAsync(balanceRecord);
}, 5);
});
}
/// <summary>
/// 消费前预检
/// </summary>
/// <param name="deviceUniqueNo"></param>
/// <param name="costDateTime"></param>
/// <returns></returns>
private async Task<OrderPreCheckDto> 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 OrderPreCheckDto
{
Factory = bindingInfo.Factory,
CostDate = costDate,
ShiftType = shift!.ShiftType,
BusinessPlaceCode = bindingInfo.Code,
BusinessShiftCode = bindingInfo.ShiftCode
};
}
/// <summary>
/// 设置设备ip
/// </summary>
/// <param name="deviceNo"></param>
private async Task SetDeviceIpInfoAsync(string deviceNo)
{
try
{
if (_httpContextAccessor.HttpContext!.Request.Headers.TryGetValue("X-Requested-DeviceIp", out var ip))
{
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>
/// 检查是否能够进行离线消费,处理hpt重复提交 /// 检查是否能够进行离线消费,处理hpt重复提交
/// </summary> /// </summary>