commitafdd9e6f14Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:48:05 2024 +0800 fix 价格默认获取菜单中的设置 commita3cfc472c8Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:40:04 2024 +0800 fix 时间范围逻辑 commit83f8906f12Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:32:55 2024 +0800 fix 时间范围逻辑 commit35660b1d0cAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:29:36 2024 +0800 fix 数据范围罗逻辑修改 commit7407955bc5Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:14:06 2024 +0800 fix 分类树增加营业点参数 commited5196d1a1Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 21:02:52 2024 +0800 feat 用户创建 commitd25909a235Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 20:51:20 2024 +0800 fix 菜单明细调整 commit8915f9ff0fAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 20:42:08 2024 +0800 fix 判断提前 commit45aa52aa2aAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 20:34:22 2024 +0800 fix 逻辑取反 commit89904fead1Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 20:23:20 2024 +0800 fix 菜单时效范围 commit573911f907Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 19:13:33 2024 +0800 fix 订单聚合返回状态 commit93c38be18aAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 18:40:12 2024 +0800 fix 订单查询 commite256d18d36Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 18:14:33 2024 +0800 fix 订单取消接口,分页权限控制 commit1218aec811Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 17:32:45 2024 +0800 fix 营业点权限控制优化 commit226c61ee41Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 17:09:42 2024 +0800 fix tenantId赋值 commit28d2c1d8d4Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 17:05:37 2024 +0800 fix ignoretenant commit5ae6dd97b4Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 16:53:40 2024 +0800 fix ignoretenant commit4c866eaf3fAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 16:40:24 2024 +0800 feat 获取所有地址 commit2ca8f2db49Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 16:22:47 2024 +0800 fix 优化查询 commit212a1fa073Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 16:01:49 2024 +0800 fix 临时数据模拟 commit0eb76e878cAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 15:48:59 2024 +0800 fix 处理删除缺少判断 commitc582fb40c0Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 15:40:22 2024 +0800 fix 查询优化 commit6d3a19b283Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 11:48:45 2024 +0800 fix 错误值修改 commitc39f77008dAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 11:31:50 2024 +0800 fix 营业点控制(admin),unit返回 commita5dcab1d07Author: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 11:16:23 2024 +0800 fix 营业点分页控制 commit02084be82bAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 11:01:14 2024 +0800 fix 分类控制菜品获取 commit96c5c5febaAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 10:39:58 2024 +0800 fix DI commit367ede2eebAuthor: liguoyang <Ligy.97@foxmail.com> Date: Thu Mar 21 10:35:24 2024 +0800 fix 订餐返回当前信息,增加数量入参和响应 commit0e1f6cb4a0Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 16:40:10 2024 +0800 fix 餐补查询代码调整 commit21fc2f78d3Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 15:52:46 2024 +0800 fix 支付批量操作 commit878b44a146Merge:a90d4e6bf5c937Author: cheche <717840555@qq.com> Date: Wed Mar 20 15:27:39 2024 +0800 Merge branch 'development' of https://git.dlhis.com/liguoyang/jinko.yikatong into development commita90d4e66a7Author: cheche <717840555@qq.com> Date: Wed Mar 20 15:27:37 2024 +0800 fix 餐补查询及日志删除 commitbf5c937037Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 15:12:30 2024 +0800 fix 补充营业点分配权限接口 commit07672f1d32Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 13:56:33 2024 +0800 fix 优化changeDb commit7090ee4a33Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 13:41:25 2024 +0800 fix 敏感信息混淆 commit4366b68831Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 12:42:48 2024 +0800 fix 代码优化 commit03b73515b5Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Mar 20 10:59:08 2024 +0800 fix tree返回营业点 commit1ce60bedc2Merge:aba4fa72d83930Author: cheche <717840555@qq.com> Date: Wed Mar 20 09:55:17 2024 +0800 Merge branch 'development' of https://git.dlhis.com/liguoyang/jinko.yikatong into development commitaba4fa7ee9Author: cheche <717840555@qq.com> Date: Wed Mar 20 09:55:13 2024 +0800 fix 删除日志优化 commit2d8393068fAuthor: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 20:53:16 2024 +0800 feat 订单聚合接口 commit0cde1d3d92Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 20:03:06 2024 +0800 fix 表名称调整 commitf003e30169Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 19:51:56 2024 +0800 fix 订餐null异常处理 commitf427338715Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 18:43:19 2024 +0800 feat 获取菜品 commit3a65d95701Merge:9a7091b7432faeAuthor: cheche <717840555@qq.com> Date: Tue Mar 19 18:36:16 2024 +0800 Merge branch 'development' of https://git.dlhis.com/liguoyang/jinko.yikatong into development commit9a7091b4e0Author: cheche <717840555@qq.com> Date: Tue Mar 19 18:36:14 2024 +0800 fix 删除日志try commit7432fae103Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 18:02:31 2024 +0800 fix 配送点增加 commitecd162c7afAuthor: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 17:51:15 2024 +0800 fix 预定就餐送货地点 commit1c7a054ed4Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 15:53:22 2024 +0800 feat 获取所有营业点接口 commit84398a566eMerge:217d9f784b4025Author: cheche <717840555@qq.com> Date: Tue Mar 19 15:26:42 2024 +0800 Merge branch 'development' of https://git.dlhis.com/liguoyang/jinko.yikatong into development commit217d9f7770Author: cheche <717840555@qq.com> Date: Tue Mar 19 15:26:40 2024 +0800 feat 配送点管理 commit84b402538eAuthor: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 14:59:21 2024 +0800 fix 菜单分页响应调整,增加详情接口 commit098d7c7e9dAuthor: liguoyang <Ligy.97@foxmail.com> Date: Tue Mar 19 14:26:24 2024 +0800 fix 代码优化
496 lines
18 KiB
C#
496 lines
18 KiB
C#
using Dlhis.Basic.Api;
|
|
using Dlhis.Canteen.Dishes.Request;
|
|
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 Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.AspNetCore.Utils;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.CustomClass;
|
|
using Kurisu.Core.DataAccess.Dto;
|
|
using Kurisu.Core.User.Abstractions;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
using Dlhis.Canteen.Order.Response;
|
|
using Dlhis.Canteen.Dishes;
|
|
using Dlhis.Canteen.Order.Request;
|
|
|
|
namespace Dlhis.Canteen.Order.Services.Implements;
|
|
|
|
public class OrderService : BaseOrderService, IOrderService
|
|
{
|
|
public OrderService(ICanteenSettingService canteenSettingService, IDbContext dbContext,
|
|
ICurrentUser currentUser, IDevicePermissionApi devicePermissionApi,
|
|
ILogger<OrderService> logger)
|
|
: base(canteenSettingService, dbContext, currentUser, devicePermissionApi, logger)
|
|
{
|
|
}
|
|
|
|
public async Task<OrderResultResponse> DeviceConsume(OrderAddRequest request)
|
|
{
|
|
return await AddAsync(request);
|
|
}
|
|
|
|
public async Task<OrderResultResponse> AddAsync(OrderAddRequest request)
|
|
{
|
|
//尝试匹配userId
|
|
_ = long.TryParse(request.UserUniqueNo, out var userId);
|
|
|
|
//根据档案号
|
|
var gainfo = await DbContext.Queryable<GainfoEntity>()
|
|
.Where(x => x.No == request.UserUniqueNo || x.Code == request.UserUniqueNo || x.Id == userId || x.CardNo == request.UserUniqueNo)
|
|
.FirstAsync();
|
|
|
|
gainfo.ThrowIfNull($"无消费权限");
|
|
//消费权限控制
|
|
await CheckPermissionAsync(gainfo.No, request.DeviceUniqueNo);
|
|
|
|
var canteenUserExists = await DbContext.Queryable<GainfoMealEntity>()
|
|
.Where(x => x.UserNo == gainfo.No)
|
|
.AnyAsync();
|
|
|
|
if (!canteenUserExists)
|
|
{
|
|
await DbContext.InsertAsync(new GainfoMealEntity
|
|
{
|
|
UserNo = gainfo.No,
|
|
Balance = 0,
|
|
BalanceOfRecharge = 0,
|
|
BalanceOfSubsidies = 0,
|
|
TenantId = gainfo.Factory,
|
|
Version = 0,
|
|
|
|
CreatedBy = 1,
|
|
CreateTime = DateTime.Now,
|
|
ModifiedBy = 1,
|
|
ModifiedTime = DateTime.Now,
|
|
});
|
|
}
|
|
|
|
//消费
|
|
if (request.Type == BalanceRecordTypeEnumType.Out)
|
|
{
|
|
await ConsumeAsync(request.Adapt<ConsumeRequest>(), gainfo);
|
|
}
|
|
//充值
|
|
else
|
|
{
|
|
await RechargeAsync(request.Adapt<RechargeRequest>(), gainfo);
|
|
}
|
|
|
|
//获取人员余额
|
|
return await DbContext.Queryable<GainfoEntity>()
|
|
.LeftJoin<DepartmentEntity>((ga, department) => ga.DepartmentNo == department.No)
|
|
.LeftJoin<GainfoMealEntity>((ga, department, mealUser) => ga.No == mealUser.UserNo)
|
|
.Where(ga => ga.Id == gainfo.Id)
|
|
.Select((ga, department, mealUser) => new OrderResultResponse
|
|
{
|
|
UserCode = ga.Code,
|
|
UserName = ga.Name,
|
|
DepartmentNo = ga.DepartmentNo,
|
|
DepartmentName = department.Name,
|
|
CurrentBalance = mealUser.Balance
|
|
})
|
|
.SingleAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消费
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="gainfo"></param>
|
|
/// <returns></returns>
|
|
private async Task ConsumeAsync(ConsumeRequest request, GainfoEntity gainfo)
|
|
{
|
|
var device = await DbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == request.DeviceUniqueNo).Where(x => x.Enable).FirstAsync();
|
|
device.ThrowIfNull("设备不存在或者尚未启用");
|
|
var registry = await DbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo).FirstAsync();
|
|
registry.ThrowIfNull("设备尚未注册");
|
|
var businessPlaceInfo = await DbContext.Queryable<BusinessPlaceEntity>()
|
|
.LeftJoin<BusinessPlaceShiftRelationshipEntity>((x, sr) => x.Code == sr.PlaceCode)
|
|
.Where(x => x.Code == registry.BusinessPlaceCode)
|
|
.Select((x, sr) => new
|
|
{
|
|
x.Code,
|
|
sr.ShiftCode,
|
|
x.TenantId
|
|
})
|
|
.FirstAsync();
|
|
businessPlaceInfo.ThrowIfNull("营业点尚未设置");
|
|
|
|
//获取当前消费餐次
|
|
var shifts = await DbContext.Queryable<BusinessShiftMealRelationshipEntity>().Where(x => x.ShiftCode == businessPlaceInfo.ShiftCode).ToListAsync();
|
|
|
|
var shift = shifts.FirstOrDefault(x => new TimePeriod()
|
|
{
|
|
Start = x.Start,
|
|
End = x.End
|
|
|
|
}.IsPresent1(request.CostDateTime));
|
|
|
|
var switchTime = await GetSettingAsync<TimeOnly?>(CanteenSettingItemEnumType.BusinessSwitchTime);
|
|
|
|
var shiftTimePeriod = new TimePeriod()
|
|
{
|
|
Start = shift?.Start,
|
|
End = shift?.End
|
|
};
|
|
|
|
//计算成本日期 todo 优化
|
|
var costDate = DateTime.Now;
|
|
if (shift == null)
|
|
{
|
|
costDate = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
if (switchTime.HasValue)
|
|
{
|
|
var beginOfDay = DateTime.Today;
|
|
|
|
var setTime = beginOfDay.Add(switchTime.Value.ToTimeSpan());
|
|
if (DateTime.Now < setTime)
|
|
costDate = DateTime.Now.AddDays(-1);
|
|
}
|
|
}
|
|
|
|
var order = new CanteenOrderEntity
|
|
{
|
|
RequestJson = request.ToJson(),
|
|
ThirdNo = request.ThirdNo,
|
|
Type = BalanceRecordTypeEnumType.Out,
|
|
ApplicationType = CanteenApplicaitonTypeEnumType.Device,
|
|
Name = gainfo.Name,
|
|
Source = request.Source,
|
|
UserNo = gainfo.No,
|
|
UserCode = gainfo.Code,
|
|
Amount = request.Amount,
|
|
BusinessPlaceCode = businessPlaceInfo.Code,
|
|
BusinessShiftCode = shift?.ShiftCode,
|
|
ShiftType = shift?.ShiftType,
|
|
CostDate = costDate.Date,
|
|
CostId = gainfo.CostId,
|
|
DepartmentNo = gainfo.DepartmentNo,
|
|
DeviceUniqueNo = request.DeviceUniqueNo,
|
|
|
|
No = SnowFlakeHelper.Instance.NextId().ToString(),
|
|
SubsidiesFromMonth = request.CostDateTime.ToString("yyyyMM"),
|
|
IsOffline = request.IsOffline,
|
|
IsCostCrossTenant = await GetSettingAsync<bool>(CanteenSettingItemEnumType.AllowCrossTenant),
|
|
|
|
OriginTenantId = gainfo.Factory,//人员可能跨基地消费
|
|
|
|
//餐别异常处理
|
|
Status = shift == null ? BalanceRecordStatusEnumType.Doubtful : BalanceRecordStatusEnumType.Valid,
|
|
|
|
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
|
|
|
CostDateTime = request.CostDateTime,
|
|
|
|
//标识当前消费的基地
|
|
TenantId = businessPlaceInfo.TenantId,
|
|
};
|
|
|
|
var orderId = await DbContext.InsertReturnIdentityAsync(order);
|
|
try
|
|
{
|
|
await GenerateConsumeRecordAsync(orderId, shifts, request.CostDateTime);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await DbContext.Updateable<CanteenOrderEntity>()
|
|
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Failure)
|
|
.SetColumns(x => x.Result == ex.Message)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.Id == orderId)
|
|
.ExecuteCommandAsync();
|
|
|
|
throw;
|
|
}
|
|
|
|
await DbContext.Updateable<CanteenOrderEntity>()
|
|
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Success)
|
|
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
|
.Where(x => x.Id == orderId)
|
|
.ExecuteCommandAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 充值
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="gainfo"></param>
|
|
/// <returns></returns>
|
|
private async Task RechargeAsync(RechargeRequest request, GainfoEntity gainfo)
|
|
{
|
|
var order = new CanteenOrderEntity
|
|
{
|
|
RequestJson = request.ToJson(),
|
|
IsOffline = false,
|
|
ThirdNo = request.ThirdNo,
|
|
Type = BalanceRecordTypeEnumType.In,
|
|
Name = gainfo.Name,
|
|
Source = request.Source,
|
|
UserNo = gainfo.No,
|
|
UserCode = gainfo.Code,
|
|
Amount = request.Amount,
|
|
BusinessPlaceCode = null,
|
|
BusinessShiftCode = null,
|
|
CostDate = DateTime.Now.Date,
|
|
CostId = gainfo.CostId,
|
|
DepartmentNo = gainfo.DepartmentNo,
|
|
DeviceUniqueNo = string.Empty,
|
|
|
|
No = SnowFlakeHelper.Instance.NextId().ToString(),
|
|
SubsidiesFromMonth = request.CostDateTime.ToString("yyyyMM"),
|
|
IsCostCrossTenant = await GetSettingAsync<bool>(CanteenSettingItemEnumType.AllowCrossTenant),
|
|
|
|
OriginTenantId = gainfo.Factory,//人员可能跨基地消费
|
|
|
|
Status = BalanceRecordStatusEnumType.Valid,
|
|
|
|
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
|
|
|
CostDateTime = request.CostDateTime,
|
|
|
|
//标识当前消费的基地
|
|
TenantId = gainfo.Factory,
|
|
};
|
|
|
|
var orderId = await DbContext.InsertReturnIdentityAsync(order);
|
|
try
|
|
{
|
|
await GenerateRechargeRecordAsync(orderId);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await DbContext.Updateable<CanteenOrderEntity>()
|
|
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Failure)
|
|
.SetColumns(x => x.Result == ex.Message)
|
|
.Where(x => x.Id == orderId)
|
|
.ExecuteCommandAsync();
|
|
|
|
throw;
|
|
}
|
|
|
|
await DbContext.Updateable<CanteenOrderEntity>()
|
|
.SetColumns(x => x.OrderStatus == CanteenOrderStatusEnumType.Success)
|
|
.Where(x => x.Id == orderId)
|
|
.ExecuteCommandAsync();
|
|
}
|
|
|
|
|
|
public async Task<Pagination<OrderAggregationResponse>> GetOrdersAggregationAsync(PageDto page)
|
|
{
|
|
var subjectId = CurrentUser.GetIntSubjectId();
|
|
var gainfo = await DbContext.Queryable<GainfoEntity>().Where(x => x.IdentityUserId == subjectId).FirstAsync();
|
|
var jinkoCode = CurrentUser.GetUserClaim("jinko-code") ?? string.Empty;
|
|
if (jinkoCode.IsPresent())
|
|
{
|
|
gainfo = await DbContext.Queryable<GainfoEntity>().Where(x => x.Code == jinkoCode).FirstAsync();
|
|
}
|
|
|
|
var pageResult = await DbContext.Queryable<CanteenOrderEntity>().Where(x => x.UserNo == gainfo.No)
|
|
.LeftJoin<BusinessPlaceEntity>((x, bplace) => x.BusinessPlaceCode == bplace.Code)
|
|
.Where(x => x.ApplicationType == CanteenApplicaitonTypeEnumType.Booking)
|
|
.Select((x, bplace) => new OrderAggregationResponse
|
|
{
|
|
No = x.No,
|
|
BusinessPlaceName = bplace.Name,
|
|
CreateTime = x.CreateTime,
|
|
Remark = x.Remark,
|
|
OrderStatus = x.OrderStatus,
|
|
})
|
|
.ToPageListAsync(page);
|
|
|
|
//.LeftJoin<CanteenOrderDetailEntity>((x, bplace, detail) => x.No == detail.OrderNo)
|
|
|
|
var nos = pageResult.Data.Select(x => x.No).ToList();
|
|
var dtos = await DbContext.Queryable<CanteenOrderDetailEntity>()
|
|
.LeftJoin<DishesEntity>((x, d) => x.DishesCode == d.CustomCode)
|
|
.Where(x => nos.Contains(x.OrderNo))
|
|
.Select((x, d) => new DishesDto
|
|
{
|
|
No = x.OrderNo,
|
|
DishesCode = x.DishesCode,
|
|
Name = d.Name,
|
|
ImageUrl = d.ImageUrl,
|
|
Number = x.Number,
|
|
Price = x.Price,
|
|
})
|
|
.ToListAsync();
|
|
|
|
|
|
pageResult.Data.ForEach(item =>
|
|
{
|
|
item.Dishes = dtos.Where(x => x.No == item.No).ToList();
|
|
});
|
|
|
|
return pageResult;
|
|
}
|
|
|
|
|
|
//******************************************************* help methods ************************************************************/
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 验证参数
|
|
/// </summary>
|
|
/// <param name="shifts"></param>
|
|
/// <param name="costDateTime"></param>
|
|
/// <param name="order"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
private async Task Verify(List<BusinessShiftMealRelationshipEntity> shifts, DateTime costDateTime, CanteenOrderEntity order)
|
|
{
|
|
//在线
|
|
if (!order.IsOffline)
|
|
{
|
|
shifts.FirstOrDefault(x => new TimePeriod()
|
|
{
|
|
Start = x.Start,
|
|
End = x.End,
|
|
|
|
}.IsPresent1(costDateTime)).ThrowIfNull("当前非就餐时间,无法消费");
|
|
}
|
|
|
|
var beginOfDay = DateTime.Today;
|
|
var endOfDay = beginOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
|
|
|
|
var balanceRecords = await DbContext.Queryable<BalanceRecordEntity>()
|
|
.Where(x => x.UserNo == order.UserNo)
|
|
.Where(x => x.CreateTime >= beginOfDay && x.CreateTime <= endOfDay) //今天消费记录
|
|
.ToListAsync();
|
|
|
|
var dayMarketCostMax = await GetSettingAsync<decimal>(CanteenSettingItemEnumType.DayMarketCostMax);
|
|
if (dayMarketCostMax != default)
|
|
{
|
|
var businessPlaceCodes = await DbContext.Queryable<BusinessPlaceEntity>()
|
|
.Where(x => x.Catalog == CanteenBusinessPlaceCatalogEnumType.Supermarket)
|
|
.Select(x => x.Code).ToListAsync();
|
|
|
|
var cost = balanceRecords.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 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)
|
|
{
|
|
throw new UserFriendlyException($"超出当天消费限制({dayCostMax}),无法消费");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 生成消费记录
|
|
/// </summary>
|
|
/// <param name="orderId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
/// <exception cref="VersionExceptions"></exception>
|
|
private async Task GenerateRechargeRecordAsync(long orderId)
|
|
{
|
|
var order = await DbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync();
|
|
|
|
await ContinueUpdateAsync(async () =>
|
|
{
|
|
var canteenUser = await DbContext.Queryable<GainfoMealEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
|
|
//金额+-
|
|
canteenUser.Balance += order.Amount;
|
|
|
|
var effect = await DbContext.Updateable<GainfoMealEntity>()
|
|
.SetColumns(x => x.Balance == canteenUser.Balance)
|
|
.SetColumns(x => x.Version == x.Version + 1)
|
|
.Where(x => x.UserNo == order.UserNo && x.Version == canteenUser.Version)
|
|
.ExecuteCommandAsync();
|
|
|
|
if (effect <= 0) throw new VersionExceptions("更新版本冲突");
|
|
|
|
//写入记录
|
|
var balanceRecord = order.Adapt<BalanceRecordEntity>();
|
|
await DbContext.InsertAsync(balanceRecord);
|
|
|
|
}, 5);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成充值记录
|
|
/// </summary>
|
|
/// <param name="orderId"></param>
|
|
/// <param name="shifts"></param>
|
|
/// <param name="costDateTime"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
/// <exception cref="VersionExceptions"></exception>
|
|
private async Task GenerateConsumeRecordAsync(long orderId, List<BusinessShiftMealRelationshipEntity> shifts, DateTime costDateTime)
|
|
{
|
|
var order = await DbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync();
|
|
//校验
|
|
await Verify(shifts, costDateTime, order);
|
|
|
|
var allowNoBalance = await GetSettingAsync<bool>(CanteenSettingItemEnumType.AllowNoBalance);
|
|
|
|
await ContinueUpdateAsync(async () =>
|
|
{
|
|
if (!allowNoBalance)
|
|
{
|
|
var canteenUser = await DbContext.Queryable<GainfoMealEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
|
|
//金额+-
|
|
canteenUser.Balance += order.Amount;
|
|
if (canteenUser.Balance < 0)
|
|
{
|
|
throw new UserFriendlyException("余额不足,无法消费");
|
|
}
|
|
|
|
var effect = await DbContext.Updateable<GainfoMealEntity>()
|
|
.SetColumns(x => x.Balance == canteenUser.Balance)
|
|
.SetColumns(x => x.Version == x.Version + 1)
|
|
.Where(x => x.UserNo == order.UserNo && x.Version == canteenUser.Version)//必须与查询时的version一致
|
|
.ExecuteCommandAsync();
|
|
|
|
if (effect <= 0) throw new VersionExceptions("更新版本冲突");
|
|
}
|
|
|
|
//写入记录
|
|
var balanceRecord = order.Adapt<BalanceRecordEntity>();
|
|
await DbContext.InsertAsync(balanceRecord);
|
|
|
|
}, 5);
|
|
}
|
|
|
|
|
|
}
|