fix 优化消费充值代码
This commit is contained in:
@@ -34,12 +34,12 @@ public class OrdersController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加充值消费订单
|
/// 添加充值/消费订单
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<OrderConsumeResponse> AddAsync(OrderAddRequest request)
|
public async Task<OrderResultResponse> AddAsync(OrderAddRequest request)
|
||||||
{
|
{
|
||||||
return await _orderService.AddAsync(request);
|
return await _orderService.AddAsync(request);
|
||||||
}
|
}
|
||||||
|
|||||||
41
Src/Dlhis.Canteen/Order/Request/ConsumeRequest.cs
Normal file
41
Src/Dlhis.Canteen/Order/Request/ConsumeRequest.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using Dlhis.Entity.Canteen.Enums;
|
||||||
|
|
||||||
|
namespace Dlhis.Canteen.Order.Request;
|
||||||
|
|
||||||
|
public class ConsumeRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否离线数据
|
||||||
|
/// </summary>
|
||||||
|
public bool IsOffline { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 消费机器唯一信息
|
||||||
|
/// </summary>
|
||||||
|
public string DeviceUniqueNo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 第三方订单号
|
||||||
|
/// </summary>
|
||||||
|
public string ThirdNo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 档案号/QrCode/学工号
|
||||||
|
/// </summary>
|
||||||
|
public string UserUniqueNo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 来源
|
||||||
|
/// </summary>
|
||||||
|
public BalanceRecordSourceEnumType Source { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 金额
|
||||||
|
/// </summary>
|
||||||
|
public decimal Amount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 产生时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CostDateTime { get; set; }
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Dlhis.Entity.Canteen.Enums;
|
using Dlhis.Entity.Canteen.Enums;
|
||||||
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
|
|
||||||
namespace Dlhis.Canteen.Order.Request;
|
namespace Dlhis.Canteen.Order.Request;
|
||||||
|
|
||||||
public class OrderAddRequest
|
public class OrderAddRequest : IValidatableObject
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否离线数据
|
/// 是否离线数据
|
||||||
@@ -23,6 +24,7 @@ public class OrderAddRequest
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 档案号/QrCode/学工号
|
/// 档案号/QrCode/学工号
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Required(ErrorMessage = "人员档案号/QrCode/学工号不能为空")]
|
||||||
public string UserUniqueNo { get; set; }
|
public string UserUniqueNo { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -44,7 +46,41 @@ public class OrderAddRequest
|
|||||||
public decimal Amount { get; set; }
|
public decimal Amount { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消费时间,由机器提供,否则获取当前时间
|
/// 产生时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime CostDateTime { get; set; } = DateTime.Now;
|
public DateTime? CostDateTime { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||||
|
{
|
||||||
|
//处理金额
|
||||||
|
Amount = Type == BalanceRecordTypeEnumType.In ? Math.Abs(Amount) : -Math.Abs(Amount);
|
||||||
|
|
||||||
|
if (Type == BalanceRecordTypeEnumType.Out)
|
||||||
|
{
|
||||||
|
//处理消费类型
|
||||||
|
if ((int)Source >= (int)BalanceRecordSourceEnumType.Wechat)
|
||||||
|
{
|
||||||
|
yield return new ValidationResult($"不支持来源{Source.GetDescription()}", new[] { nameof(Source) });
|
||||||
|
}
|
||||||
|
|
||||||
|
//在线就以API时间为准
|
||||||
|
if (!IsOffline)
|
||||||
|
{
|
||||||
|
CostDateTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DeviceUniqueNo.IsEmpty())
|
||||||
|
{
|
||||||
|
yield return new ValidationResult("设备唯一号不能为空", new[] { nameof(DeviceUniqueNo) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Type == BalanceRecordTypeEnumType.In)
|
||||||
|
{
|
||||||
|
if ((int)Source < (int)BalanceRecordSourceEnumType.Wechat)
|
||||||
|
{
|
||||||
|
yield return new ValidationResult($"不支持来源{Source.GetDescription()}", new[] { nameof(Source) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
Src/Dlhis.Canteen/Order/Request/RechargeRequest.cs
Normal file
34
Src/Dlhis.Canteen/Order/Request/RechargeRequest.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using Dlhis.Entity.Canteen.Enums;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Dlhis.Canteen.Order.Request;
|
||||||
|
|
||||||
|
public class RechargeRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 第三方订单号
|
||||||
|
/// </summary>
|
||||||
|
public string ThirdNo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 档案号/QrCode/学工号
|
||||||
|
/// </summary>
|
||||||
|
public string UserUniqueNo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 来源
|
||||||
|
/// </summary>
|
||||||
|
[EnumDataType(typeof(BalanceRecordSourceEnumType))]
|
||||||
|
public BalanceRecordSourceEnumType Source { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 金额
|
||||||
|
/// </summary>
|
||||||
|
[Range(0.01, double.MaxValue)]
|
||||||
|
public decimal Amount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 产生时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CostDateTime { get; set; } = DateTime.Now;
|
||||||
|
}
|
||||||
@@ -109,4 +109,9 @@ public class BalanceRecordResponse
|
|||||||
/// 数据状态描述
|
/// 数据状态描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string StatusDesc => Status.GetDescription();
|
public string StatusDesc => Status.GetDescription();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录产生时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CostDateTime { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,4 +44,9 @@ public class BalanceRecordUserResponse
|
|||||||
/// 数据状态描述
|
/// 数据状态描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string StatusDesc => Status.GetDescription();
|
public string StatusDesc => Status.GetDescription();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录产生日期
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CostDateTime { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Dlhis.Canteen.Order.Response;
|
namespace Dlhis.Canteen.Order.Response;
|
||||||
|
|
||||||
public class OrderConsumeResponse
|
public class OrderResultResponse
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户工号
|
/// 用户工号
|
||||||
@@ -8,5 +8,5 @@ public interface IOrderService : IScopeDependency
|
|||||||
{
|
{
|
||||||
Task<Pagination<OrderResponse>> GetPageAsync(OrderPageRequest request);
|
Task<Pagination<OrderResponse>> GetPageAsync(OrderPageRequest request);
|
||||||
|
|
||||||
Task<OrderConsumeResponse> AddAsync(OrderAddRequest request);
|
Task<OrderResultResponse> AddAsync(OrderAddRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ public class BalanceService : IBalanceService
|
|||||||
DepartmentName = department.Name,
|
DepartmentName = department.Name,
|
||||||
BusinessPlaceName = bplace.Name,
|
BusinessPlaceName = bplace.Name,
|
||||||
BusinessShiftName = bshift.Name,
|
BusinessShiftName = bshift.Name,
|
||||||
|
CostDateTime = record.CreateTime // todo
|
||||||
})
|
})
|
||||||
.ToPageListAsync(request);
|
.ToPageListAsync(request);
|
||||||
|
|
||||||
@@ -121,6 +122,7 @@ public class BalanceService : IBalanceService
|
|||||||
DepartmentName = department.Name,
|
DepartmentName = department.Name,
|
||||||
BusinessPlaceName = bplace.Name,
|
BusinessPlaceName = bplace.Name,
|
||||||
BusinessShiftName = bshift.Name,
|
BusinessShiftName = bshift.Name,
|
||||||
|
CostDateTime = record.CreateTime // todo
|
||||||
})
|
})
|
||||||
.ToPageListAsync(request);
|
.ToPageListAsync(request);
|
||||||
|
|
||||||
@@ -151,6 +153,7 @@ public class BalanceService : IBalanceService
|
|||||||
No = record.No,
|
No = record.No,
|
||||||
BusinessPlaceName = bplace.Name,
|
BusinessPlaceName = bplace.Name,
|
||||||
BusinessShiftName = bshift.Name,
|
BusinessShiftName = bshift.Name,
|
||||||
|
CostDateTime = record.CreateTime // todo
|
||||||
})
|
})
|
||||||
.ToPageListAsync(request);
|
.ToPageListAsync(request);
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ public class OrderService : IOrderService
|
|||||||
private readonly ICanteenSettingService _canteenSettingService;
|
private readonly ICanteenSettingService _canteenSettingService;
|
||||||
private readonly ILogger<OrderService> _logger;
|
private readonly ILogger<OrderService> _logger;
|
||||||
|
|
||||||
public OrderService(IDbContext dbContext, ICanteenSettingService canteenSettingService, ILogger<OrderService> logger)
|
public OrderService(IDbContext dbContext,
|
||||||
|
ICanteenSettingService canteenSettingService,
|
||||||
|
ILogger<OrderService> logger)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_canteenSettingService = canteenSettingService;
|
_canteenSettingService = canteenSettingService;
|
||||||
@@ -41,40 +43,73 @@ public class OrderService : IOrderService
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IgnoreTenant]
|
[IgnoreTenant]
|
||||||
public async Task<OrderConsumeResponse> AddAsync(OrderAddRequest request)
|
public async Task<OrderResultResponse> AddAsync(OrderAddRequest request)
|
||||||
|
{
|
||||||
|
//根据档案号
|
||||||
|
var canteenUserExists = await _dbContext.Queryable<CanteenUserEntity>()
|
||||||
|
.Where(x => x.UserNo == request.UserUniqueNo || x.UserCode == request.UserUniqueNo)
|
||||||
|
.AnyAsync();
|
||||||
|
canteenUserExists.ThrowIfFalse($"就餐用户{request.UserUniqueNo}不存在,无法操作");
|
||||||
|
|
||||||
|
var gainfo = await _dbContext.Queryable<GainfoEntity>()
|
||||||
|
.Where(x => x.No == request.UserUniqueNo || x.Code == request.UserUniqueNo)
|
||||||
|
.FirstAsync();
|
||||||
|
gainfo.ThrowIfNull($"用户{request.UserUniqueNo}不存在,无法操作");
|
||||||
|
|
||||||
|
//消费
|
||||||
|
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<CanteenUserEntity>((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,
|
||||||
|
Balance = 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();
|
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == request.DeviceUniqueNo).Where(x => x.Enable).FirstAsync();
|
||||||
device.ThrowIfNull("设备不存在或者尚未启用");
|
device.ThrowIfNull("设备不存在或者尚未启用");
|
||||||
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo).FirstAsync();
|
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.DeviceUniqueNo).FirstAsync();
|
||||||
registry.ThrowIfNull("设备尚未注册");
|
registry.ThrowIfNull("设备尚未注册");
|
||||||
|
|
||||||
var businessPlace = await _dbContext.Queryable<CanteenBusinessPlaceEntity>().Where(x => x.Code == registry.BusinessPlaceCode).FirstAsync();
|
var businessPlace = await _dbContext.Queryable<CanteenBusinessPlaceEntity>().Where(x => x.Code == registry.BusinessPlaceCode).FirstAsync();
|
||||||
businessPlace.ThrowIfNull("营业点尚未设置");
|
businessPlace.ThrowIfNull("营业点尚未设置");
|
||||||
var shifts = await _dbContext.Queryable<CanteenBusinessShiftEntity>().Where(x => x.BusinessPlaceCode == registry.BusinessPlaceCode).ToListAsync();
|
|
||||||
|
|
||||||
//todo
|
//获取当前消费餐次
|
||||||
|
var shifts = await _dbContext.Queryable<CanteenBusinessShiftEntity>().Where(x => x.BusinessPlaceCode == businessPlace.Code).ToListAsync();
|
||||||
|
//todo
|
||||||
var shift = shifts.FirstOrDefault(x => new TimePeriod()
|
var shift = shifts.FirstOrDefault(x => new TimePeriod()
|
||||||
{
|
{
|
||||||
Start = x.Start,
|
Start = x.Start,
|
||||||
End = x.End,
|
End = x.End
|
||||||
|
|
||||||
}.IsPresent1(request.CostDateTime));
|
}.IsPresent1(request.CostDateTime));
|
||||||
|
|
||||||
//根据档案号
|
var settings = await _canteenSettingService.GetUserSettingAsync(request.UserUniqueNo);
|
||||||
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>()
|
|
||||||
.Where(x => x.UserNo == request.UserUniqueNo || x.UserCode == request.UserUniqueNo)
|
|
||||||
.FirstAsync();
|
|
||||||
|
|
||||||
canteenUser.ThrowIfNull($"{request.DeviceUniqueNo}不存在,无法就餐");
|
|
||||||
|
|
||||||
var gafinfo = await _dbContext.Queryable<GainfoEntity>()
|
|
||||||
.Where(x => x.No == canteenUser.UserNo)
|
|
||||||
.FirstAsync();
|
|
||||||
|
|
||||||
var settings = await _canteenSettingService.GetUserSettingAsync(gafinfo.No);
|
|
||||||
var switchTime = GetSetting<TimeOnly?>(settings, CanteenSettingItemEnumType.BusinessSwitchTime);
|
var switchTime = GetSetting<TimeOnly?>(settings, CanteenSettingItemEnumType.BusinessSwitchTime);
|
||||||
//金额处理
|
|
||||||
request.Amount = request.Type == BalanceRecordTypeEnumType.In ? Math.Abs(request.Amount) : -Math.Abs(request.Amount);
|
|
||||||
|
|
||||||
var shiftTimePeriod = new TimePeriod()
|
var shiftTimePeriod = new TimePeriod()
|
||||||
{
|
{
|
||||||
@@ -82,7 +117,7 @@ public class OrderService : IOrderService
|
|||||||
End = shift?.End
|
End = shift?.End
|
||||||
};
|
};
|
||||||
|
|
||||||
//成本日期 todo 优化
|
//计算成本日期 todo 优化
|
||||||
var costDate = DateTime.Now;
|
var costDate = DateTime.Now;
|
||||||
if (shift == null)
|
if (shift == null)
|
||||||
{
|
{
|
||||||
@@ -106,136 +141,141 @@ public class OrderService : IOrderService
|
|||||||
{
|
{
|
||||||
RequestJson = request.ToJson(),
|
RequestJson = request.ToJson(),
|
||||||
ThirdNo = request.ThirdNo,
|
ThirdNo = request.ThirdNo,
|
||||||
Type = request.Type.Value,
|
Type = BalanceRecordTypeEnumType.Out,
|
||||||
Name = gafinfo.Name,
|
Name = gainfo.Name,
|
||||||
Source = request.Source,
|
Source = request.Source,
|
||||||
UserNo = canteenUser.UserNo,
|
UserNo = gainfo.No,
|
||||||
UserCode = canteenUser.UserCode,
|
UserCode = gainfo.Code,
|
||||||
Amount = request.Amount,
|
Amount = request.Amount,
|
||||||
BusinessPlaceCode = businessPlace.Code,
|
BusinessPlaceCode = businessPlace.Code,
|
||||||
BusinessShiftCode = shift?.Code,
|
BusinessShiftCode = shift?.Code,
|
||||||
CostDate = costDate.Date,
|
CostDate = costDate.Date,
|
||||||
CostId = canteenUser.CostId,
|
CostId = gainfo.CostId,
|
||||||
DepartmentNo = gafinfo.DepartmentNo,
|
DepartmentNo = gainfo.DepartmentNo,
|
||||||
DeviceInfo = device.Code.ToString(),
|
DeviceInfo = request.DeviceUniqueNo,
|
||||||
|
|
||||||
No = "O" + SnowFlakeHelper.Instance.NextId(),
|
No = "O" + SnowFlakeHelper.Instance.NextId(),
|
||||||
SubsidiesFromMonth = DateTime.Now.ToString("yyyyMM"),
|
SubsidiesFromMonth = DateTime.Now.ToString("yyyyMM"),
|
||||||
IsOffline = request.IsOffline,
|
IsOffline = request.IsOffline,
|
||||||
IsCostCrossTenant = GetSetting<bool>(settings, CanteenSettingItemEnumType.AllowCrossTenant),
|
IsCostCrossTenant = GetSetting<bool>(settings, CanteenSettingItemEnumType.AllowCrossTenant),
|
||||||
|
|
||||||
OriginTenantId = canteenUser.TenantId,//人员可能跨基地消费
|
OriginTenantId = gainfo.Werks,//人员可能跨基地消费
|
||||||
|
|
||||||
//餐别异常处理
|
//餐别异常处理
|
||||||
Status = shift == null ? BalanceRecordStatusEnumType.Doubtful : BalanceRecordStatusEnumType.Valid,
|
Status = shift == null ? BalanceRecordStatusEnumType.Doubtful : BalanceRecordStatusEnumType.Valid,
|
||||||
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
|
||||||
//设备才能标识当前消费的基地
|
|
||||||
TenantId = device.TenantId,
|
|
||||||
|
|
||||||
//默认为admin
|
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
||||||
CreatedBy = 1,
|
|
||||||
CreateTime = DateTime.Now,
|
CostDateTime = request.CostDateTime,
|
||||||
ModifiedBy = 1,
|
|
||||||
ModifiedTime = DateTime.Now,
|
//标识当前消费的基地
|
||||||
|
TenantId = businessPlace.TenantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
var id = await _dbContext.InsertReturnIdentityAsync(order);
|
var orderId = await _dbContext.InsertReturnIdentityAsync(order);
|
||||||
var result = await _dbContext.UseTransactionAsync(async () =>
|
//await _dbContext.Client.Ado.BeginTranAsync();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
try
|
await GenerateConsumeRecordAsync(orderId, settings, shifts, request.CostDateTime);
|
||||||
{
|
order.OrderStatus = CanteenOrderStatusEnumType.Successful;
|
||||||
var r = await GenerateChargeRecordAsync(id, settings, shifts, request.CostDateTime);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
//订单消费成功
|
|
||||||
order.OrderStatus = CanteenOrderStatusEnumType.Successful;
|
|
||||||
await _dbContext.UpdateAsync(order);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result.IsSuccess)
|
//await _dbContext.Client.Ado.CommitTranAsync();
|
||||||
{
|
|
||||||
throw new UserFriendlyException(result.ErrorMessage);
|
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
return result.Data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<OrderConsumeResponse> GenerateChargeRecordAsync(long orderId,
|
|
||||||
Dictionary<CanteenSettingItemEnumType, string> settings,
|
|
||||||
List<CanteenBusinessShiftEntity> shifts,
|
|
||||||
DateTime costDateTime)
|
|
||||||
{
|
|
||||||
var order = await _dbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync();
|
|
||||||
|
|
||||||
//校验
|
|
||||||
await Verify(settings, shifts, costDateTime, order);
|
|
||||||
|
|
||||||
|
|
||||||
return await ContinueUpdateAsync(async () =>
|
|
||||||
{
|
{
|
||||||
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
|
order.OrderStatus = CanteenOrderStatusEnumType.Failure;
|
||||||
//金额+-
|
//await _dbContext.Client.Ado.RollbackTranAsync();
|
||||||
canteenUser.Balance += order.Amount;
|
throw;
|
||||||
|
}
|
||||||
if (order.Type == BalanceRecordTypeEnumType.Out)
|
finally
|
||||||
{
|
{
|
||||||
if (canteenUser.Balance < 0)
|
await _dbContext.UpdateAsync(order);
|
||||||
{
|
}
|
||||||
throw new UserFriendlyException("余额不足,无法消费");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var effect = await _dbContext.Updateable<CanteenUserEntity>()
|
|
||||||
.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);
|
|
||||||
|
|
||||||
return await _dbContext.Queryable<GainfoEntity>()
|
|
||||||
.LeftJoin<DepartmentEntity>((ga, department) => ga.DepartmentNo == department.No)
|
|
||||||
.LeftJoin<CanteenUserEntity>((ga, department, mealUser) => ga.No == mealUser.UserNo)
|
|
||||||
.Select((ga, department, mealUser) => new OrderConsumeResponse
|
|
||||||
{
|
|
||||||
UserCode = ga.Code,
|
|
||||||
UserName = ga.Name,
|
|
||||||
DepartmentNo = ga.DepartmentNo,
|
|
||||||
DepartmentName = department.Name,
|
|
||||||
Balance = mealUser.Balance
|
|
||||||
})
|
|
||||||
.FirstAsync();
|
|
||||||
}, 5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 充值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="gainfo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task RechargeAsync(RechargeRequest request, GainfoEntity gainfo)
|
||||||
|
{
|
||||||
|
var settings = await _canteenSettingService.GetUserSettingAsync(request.UserUniqueNo);
|
||||||
|
|
||||||
|
var order = new CanteenOrderEntity
|
||||||
|
{
|
||||||
|
RequestJson = request.ToJson(),
|
||||||
|
ThirdNo = request.ThirdNo,
|
||||||
|
Type = BalanceRecordTypeEnumType.Out,
|
||||||
|
Name = gainfo.Name,
|
||||||
|
Source = request.Source,
|
||||||
|
UserNo = gainfo.No,
|
||||||
|
UserCode = gainfo.Code,
|
||||||
|
Amount = request.Amount,
|
||||||
|
BusinessPlaceCode = Guid.Empty,
|
||||||
|
BusinessShiftCode = null,
|
||||||
|
CostDate = DateTime.Now.Date,
|
||||||
|
CostId = gainfo.CostId,
|
||||||
|
DepartmentNo = gainfo.DepartmentNo,
|
||||||
|
DeviceInfo = string.Empty,
|
||||||
|
|
||||||
|
No = "O" + SnowFlakeHelper.Instance.NextId(),
|
||||||
|
SubsidiesFromMonth = DateTime.Now.ToString("yyyyMM"),
|
||||||
|
IsOffline = false,
|
||||||
|
IsCostCrossTenant = GetSetting<bool>(settings, CanteenSettingItemEnumType.AllowCrossTenant),
|
||||||
|
|
||||||
|
OriginTenantId = gainfo.Werks,//人员可能跨基地消费
|
||||||
|
|
||||||
|
Status = BalanceRecordStatusEnumType.Valid,
|
||||||
|
|
||||||
|
OrderStatus = CanteenOrderStatusEnumType.Wait,
|
||||||
|
|
||||||
|
CostDateTime = request.CostDateTime,
|
||||||
|
|
||||||
|
//标识当前消费的基地
|
||||||
|
TenantId = gainfo.Werks,
|
||||||
|
};
|
||||||
|
|
||||||
|
var orderId = await _dbContext.InsertReturnIdentityAsync(order);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await GenerateRechargeRecordAsync(orderId);
|
||||||
|
order.OrderStatus = CanteenOrderStatusEnumType.Successful;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
order.OrderStatus = CanteenOrderStatusEnumType.Failure;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await _dbContext.UpdateAsync(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//******************************************************* help methods ************************************************************/
|
//******************************************************* help methods ************************************************************/
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备并发更新
|
/// 设备并发更新 需要抛出VersionExceptions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="func"></param>
|
/// <param name="func"></param>
|
||||||
/// <param name="retry"></param>
|
/// <param name="retry">并发冲突处理次数</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="UserFriendlyException"></exception>
|
/// <exception cref="UserFriendlyException"></exception>
|
||||||
private async Task<T> ContinueUpdateAsync<T>(Func<Task<T>> func, int retry)
|
private async Task ContinueUpdateAsync(Func<Task> func, int retry)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < retry; i++)
|
for (int i = 0; i < retry; i++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await func();
|
await func();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is VersionExceptions)
|
catch (Exception ex) when (ex is VersionExceptions)
|
||||||
{
|
{
|
||||||
@@ -247,7 +287,7 @@ public class OrderService : IOrderService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UserFriendlyException("消费失败,请重试");
|
throw new UserFriendlyException("操作失败,请重试");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -270,7 +310,6 @@ public class OrderService : IOrderService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证参数
|
/// 验证参数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -342,4 +381,80 @@ public class OrderService : IOrderService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <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<CanteenUserEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
|
||||||
|
//金额+-
|
||||||
|
canteenUser.Balance += order.Amount;
|
||||||
|
|
||||||
|
var effect = await _dbContext.Updateable<CanteenUserEntity>()
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成充值记录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="orderId"></param>
|
||||||
|
/// <param name="settings"></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,
|
||||||
|
Dictionary<CanteenSettingItemEnumType, string> settings,
|
||||||
|
List<CanteenBusinessShiftEntity> shifts,
|
||||||
|
DateTime costDateTime)
|
||||||
|
{
|
||||||
|
var order = await _dbContext.Queryable<CanteenOrderEntity>().Where(x => x.Id == orderId).FirstAsync();
|
||||||
|
//校验
|
||||||
|
await Verify(settings, shifts, costDateTime, order);
|
||||||
|
|
||||||
|
await ContinueUpdateAsync(async () =>
|
||||||
|
{
|
||||||
|
var canteenUser = await _dbContext.Queryable<CanteenUserEntity>().Where(x => x.UserNo == order.UserNo).FirstAsync();
|
||||||
|
//金额+-
|
||||||
|
canteenUser.Balance += order.Amount;
|
||||||
|
if (canteenUser.Balance < 0)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("余额不足,无法消费");
|
||||||
|
}
|
||||||
|
|
||||||
|
var effect = await _dbContext.Updateable<CanteenUserEntity>()
|
||||||
|
.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user