feat 生日券功能,自动上传消费数据,提醒访客到期

This commit is contained in:
2025-12-09 11:35:05 +08:00
parent 323eb367d0
commit c1462a03dc
60 changed files with 1451 additions and 409 deletions

View File

@@ -1,8 +1,10 @@
using Dlhis.Basic.Api;
using Dlhis.Canteen.Order.Repository;
using Dlhis.Canteen.Order.Request;
using Dlhis.Canteen.Order.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Entities.Voucher;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
@@ -22,64 +24,78 @@ public class BirthdayConsumeHandler : IConsumeHandler
private readonly IDbContext _dbContext;
private readonly IOrderCommonHandler _orderCommonHandler;
private readonly IOrderRepository _orderRepository;
private readonly IFactorySettingApi _factorySettingApi;
public BirthdayConsumeHandler(IDbContext dbContext,
IOrderCommonHandler orderCommonHandler,
IOrderRepository orderRepository)
IOrderRepository orderRepository,
IFactorySettingApi factorySettingApi)
{
_dbContext = dbContext;
_orderCommonHandler = orderCommonHandler;
_orderRepository = orderRepository;
_factorySettingApi = factorySettingApi;
}
public async Task<DefaultApiResult<OrderResultResponse>> ConsumeAsync(OrderAddRequest request, GainfoEntity gainfo)
{
var msg = string.Empty;
var ticketNo = request.UserUniqueNo;
var qrCode = request.UserUniqueNo;
var exists = await _dbContext.Queryable<TicketEntity>()
.Where(x => x.Type == TicketEnumType.Birthday)
.Where(x => x.QrCode == ticketNo)
.AnyAsync();
exists.ThrowIfFalse("生日券无效");
var voucherQrCode = await _dbContext.Queryable<VoucherQrCodeEntity>()
.LeftJoin<VoucherUserInfoEntity>((x, u) => x.VoucherNo == u.VoucherNo)
.Where(x => x.QrCode == qrCode)
.Select((x, u) => new
{
x.VoucherNo,
u.UserCode,
u.Factory
})
.SingleAsync();
voucherQrCode.ThrowIfNull("生日券无效");
try
{
var checkData = await _orderCommonHandler.PreCheckAsync(request.DeviceUniqueNo, request.CostDateTime!.Value);
var isSupermarket = await _dbContext.Queryable<BusinessPlaceEntity>()
var businessPlace = await _dbContext.Queryable<BusinessPlaceEntity>()
.Where(x => x.Code == checkData.BusinessPlaceCode)
.Select(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
//.Select(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.SingleAsync();
businessPlace.ThrowIfNull("营业点不存在");
var isSupermarket = businessPlace.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket;
isSupermarket.ThrowIfFalse("请在超市消费");
var allowFactories = await _factorySettingApi.GetEnableUseBirthdayVoucherFactoriesAsync(voucherQrCode.Factory);
allowFactories.Contains(businessPlace.TenantId).ThrowIfFalse($"该厂区({businessPlace.TenantId})不可使用此生日券");
var order = await _orderRepository.CreateDeviceConsumeOrderAsync(checkData, request.Adapt<ConsumeRequest>(), gainfo);
await _orderCommonHandler.TryConsumeAsync(order.Id, async () =>
{
await CommonHelper.RetryAsync(async () =>
{
var ticket = await _dbContext.Queryable<TicketEntity>()
.Where(x => x.Type == TicketEnumType.Birthday)
.Where(x => x.QrCode == ticketNo)
var voucher = await _dbContext.Queryable<VoucherEntity>()
.Where(x => x.Type == VoucherEnumType.Birthday)
.Where(x => x.No == voucherQrCode.VoucherNo)
.SingleAsync();
//金额+-
ticket.Balance += order.Amount;
(ticket.Balance < 0).ThrowIfTrue("余额不足,无法消费");
voucher.Balance += order.Amount;
(voucher.Balance < 0).ThrowIfTrue("余额不足,无法消费");
await _dbContext.Updateable<TicketEntity>()
.SetColumns(x => x.Balance == ticket.Balance)
await _dbContext.Updateable<VoucherEntity>()
.SetColumns(x => x.Balance == voucher.Balance)
.SetColumns(x => x.Version == x.Version + 1)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.QrCode == ticketNo && x.Version == ticket.Version) //必须与查询时的version一致
.Where(x => x.No == voucher.No && x.Version == voucher.Version) //必须与查询时的version一致
.ExecuteCommandThrowVersionAsync();
//写入记录
var balanceRecord = order.Adapt<BalanceRecordEntity>();
balanceRecord.Remark = $"{ticket.QrCode}(生日券消费)";
balanceRecord.Remark = $"{voucher.No}(生日券消费)";
await _dbContext.InsertAsync(balanceRecord);
}, 5);
});
}
@@ -87,14 +103,15 @@ public class BirthdayConsumeHandler : IConsumeHandler
{
msg = ex.Message;
}
//获取餐券余额
var data = await _dbContext.Queryable<TicketEntity>()
.LeftJoin<GainfoEntity>((x, ga) => x.UserCode == ga.Code)
.Where(x => x.Type == TicketEnumType.Birthday && x.QrCode == ticketNo)
.Select((x, ga) => new OrderResultResponse
var data = await _dbContext.Queryable<VoucherEntity>()
.LeftJoin<VoucherUserInfoEntity>((x, u) => x.No == u.VoucherNo)
.LeftJoin<GainfoEntity>((x, u, ga) => u.UserCode == ga.Code)
.Where(x => x.Type == VoucherEnumType.Birthday && x.No == voucherQrCode.VoucherNo)
.Select((x, u, ga) => new OrderResultResponse
{
UserCode = x.UserCode,
UserCode = u.UserCode,
UserName = ga.Name,
CurrentBalance = x.Balance
})

View File

@@ -3,6 +3,7 @@ using Dlhis.Canteen.Order.Request;
using Dlhis.Canteen.Order.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Entities.EMealVoucher;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;

View File

@@ -8,6 +8,7 @@ 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;
@@ -40,7 +41,8 @@ public class OrderCommonHandler : IOrderCommonHandler
{
try
{
if (_httpContextAccessor.HttpContext!.Request.Headers.TryGetValue("X-Requested-DeviceIp", out var ip))
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

View File

@@ -6,6 +6,7 @@ using Dlhis.Canteen.Setting.Services;
using Dlhis.Constants;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Entities.EMealVoucher;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Entity.Device;
using Dlhis.Entity.Device.Entities;

View File

@@ -6,6 +6,8 @@ using Dlhis.Canteen.Setting.Services;
using Dlhis.Constants;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Entities.EMealVoucher;
using Dlhis.Entity.Canteen.Entities.Voucher;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Entity.Device;
using Dlhis.Entity.Device.Entities;
@@ -160,27 +162,35 @@ public class OrderService : IOrderService
switch (flag)
{
case QrCodeConstants.User:
{
gainfo = await GetUserByQrCodeAsync(request.UserUniqueNo);
await CheckPermissionAsync(gainfo.Code, request.DeviceUniqueNo);
}
{
gainfo = await GetUserByQrCodeAsync(request.UserUniqueNo);
await CheckPermissionAsync(gainfo.Code, request.DeviceUniqueNo);
}
break;
case QrCodeConstants.EMealVoucher:
{
request.Source = BalanceRecordSourceEnumType.EMealVoucher;
var eMealVoucherQrCode = await _dbContext.Queryable<EMealVoucherDetailQrCodeEntity>().Where(x => x.QrCode == request.UserUniqueNo).SingleAsync();
eMealVoucherQrCode.ThrowIfNull("无法识别");
var code = eMealVoucherQrCode.OriginData.Split(':')[2];
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).SingleAsync();
}
{
request.Source = BalanceRecordSourceEnumType.EMealVoucher;
var eMealVoucherQrCode = await _dbContext.Queryable<EMealVoucherDetailQrCodeEntity>().Where(x => x.QrCode == request.UserUniqueNo).SingleAsync();
eMealVoucherQrCode.ThrowIfNull("无法识别");
var code = eMealVoucherQrCode.OriginData.Split(':')[2];
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).SingleAsync();
}
break;
case QrCodeConstants.Birthday:
{
request.Source = BalanceRecordSourceEnumType.Birthday;
var ticket = await _dbContext.Queryable<TicketEntity>().Where(x => x.Type == TicketEnumType.Birthday).Where(x => x.QrCode == request.UserUniqueNo).SingleAsync();
ticket.ThrowIfNull("无法识别");
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == ticket.UserCode).SingleAsync();
}
{
request.Source = BalanceRecordSourceEnumType.Birthday;
var qrcode = await _dbContext.Queryable<VoucherQrCodeEntity>()
.Where(x => x.QrCode == request.UserUniqueNo)
.SingleAsync();
qrcode.ThrowIfNull("券码无法识别");
var vourcher = await _dbContext.Queryable<VoucherUserInfoEntity>()
.Where(x => x.VoucherNo == qrcode.VoucherNo)
.SingleAsync();
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == vourcher.UserCode).SingleAsync();
}
break;
default: throw new UserFriendlyException($"无法识别{request.UserUniqueNo}");
@@ -201,7 +211,7 @@ public class OrderService : IOrderService
//在线
if (!request.IsOffline)
{
gainfo.ThrowIfNull("无法识别");
gainfo.ThrowIfNull("无法识别人员信息");
}
else
{
@@ -213,6 +223,7 @@ public class OrderService : IOrderService
.FirstAsync();
}
gainfo.ThrowIfNull("无法识别人员信息");
return gainfo;
}
@@ -292,7 +303,9 @@ public class OrderService : IOrderService
.Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo && x.BusinessPlaceCode.HasValue)
.Select((x, b, bs) => new
{
b.Code, bs.ShiftCode, Factory = b.TenantId
b.Code,
bs.ShiftCode,
Factory = b.TenantId
})
.SingleAsync();
@@ -485,7 +498,7 @@ public class OrderService : IOrderService
return await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == gainfoQrCode.UserCode).SingleAsync();
}
/// <summary>
/// 检查是否能够进行离线消费,处理hpt重复提交
/// </summary>