fix 餐饮消费控制和设备统计优化

This commit is contained in:
2024-04-09 15:58:12 +08:00
parent 22208fc1d9
commit a528d92012
6 changed files with 220 additions and 141 deletions

View File

@@ -516,66 +516,62 @@ public class OrderService : BaseOrderService, IOrderService
}
var beginOfDay = DateTime.Today;
var endOfDay = beginOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
var endOfDay = beginOfDay.AddDays(1).AddSeconds(-1);
var balanceRecords = await DbContext.Queryable<BalanceRecordEntity>()
//今天所有消费记录
var todayAllBalanceRecords = await DbContext.Queryable<BalanceRecordEntity>()
.Where(x => x.UserNo == order.UserNo)
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay) //今天消费记录
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay)
.ToListAsync();
var dayMarketCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayMarketCostMax);
if (dayMarketCostMax != default)
{
if (order.BusinessPlaceCode.HasValue && await DbContext.Queryable<BusinessPlaceEntity>()
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.Where(x => x.Code == order.BusinessPlaceCode.Value)
.AnyAsync())
{
var businessPlaceCodes = await DbContext.Queryable<BusinessPlaceEntity>()
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.Select(x => x.Code).ToListAsync();
var cost = balanceRecords.Where(x => x.BusinessPlaceCode.HasValue)
.Where(x => businessPlaceCodes.Contains(x.BusinessPlaceCode.Value))
.Sum(x => x.Amount);
if (cost + Math.Abs(order.Amount) > dayMarketCostMax)
{
throw new UserFriendlyException($"超出当天超市消费限制({dayMarketCostMax}),无法消费");
}
}
}
var allowRepeatInOneShift = await GetSettingAsync(CanteenSettingItemEnumType.AllowRepeatInOneShift, true);
if (!allowRepeatInOneShift)
{
//todo 餐次问题
var costCount = balanceRecords.Where(x => x.BusinessShiftCode == order.BusinessShiftCode).Count();
if (costCount > 1)
{
throw new UserFriendlyException($"当前餐次不允许多次消费");
}
}
var onceCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.OnceCostMax);
if (onceCostMax != default)
{
if (Math.Abs(order.Amount) > onceCostMax)
{
throw new UserFriendlyException($"一次消费金额不能超出{onceCostMax}");
}
}
//超市营业点
var supermarketCodes = await DbContext.Queryable<BusinessPlaceEntity>()
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
.Select(x => x.Code)
.ToListAsync();
//一天消费金额
var dayCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayCostMax);
if (dayCostMax != default)
{
var cost = balanceRecords.Where(x => x.Type == BalanceRecordTypeEnumType.Out).Sum(x => Math.Abs(x.Amount));
if (cost + Math.Abs(order.Amount) > dayCostMax)
//must has business place code, use .Value is safe
var cost = todayAllBalanceRecords
.Where(x => !supermarketCodes.Contains(x.BusinessPlaceCode.Value)) //filter supermarket
.Sum(x => Math.Abs(x.Amount));
(cost + Math.Abs(order.Amount) > dayCostMax).ThrowIfTrue($"超出当天消费限制{dayCostMax}元,无法消费");
}
//一次消费金额
var onceCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.OnceCostMax);
if (onceCostMax != default)
{
(Math.Abs(order.Amount) > onceCostMax).ThrowIfTrue($"一次消费金额不能超出{onceCostMax}");
}
//超时消费限额
var dayMarketCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayMarketCostMax);
if (dayMarketCostMax != default)
{
//current businessplace is supermarket
if (order.BusinessPlaceCode.HasValue && supermarketCodes.Contains(order.BusinessPlaceCode.Value))
{
throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费");
var cost = todayAllBalanceRecords.Where(x => x.BusinessPlaceCode.HasValue)
.Where(x => supermarketCodes.Contains(x.BusinessPlaceCode.Value))
.Sum(x => Math.Abs(x.Amount));
(cost + Math.Abs(order.Amount) > dayMarketCostMax).ThrowIfTrue($"超出当天超市消费限制{dayMarketCostMax},无法消费");
}
}
//重复餐次消费
var allowRepeatInOneShift = await GetSettingAsync(CanteenSettingItemEnumType.AllowRepeatInOneShift, true);
if (!allowRepeatInOneShift)
{
var costCount = todayAllBalanceRecords.Where(x => x.ShiftType == order.ShiftType).Count();
(costCount > 1).ThrowIfTrue($"{order?.ShiftType?.GetDescription()}不允许多次消费");
}
}