fix 消费恢复为余额表
This commit is contained in:
@@ -8,9 +8,8 @@ public interface IBalanceRepository : IScopeDependency
|
||||
/// 获取余额
|
||||
/// </summary>
|
||||
/// <param name="userCode"></param>
|
||||
/// <param name="factory"></param>
|
||||
/// <returns></returns>
|
||||
Task<decimal> GetBalanceAsync(string userCode, string factory);
|
||||
Task<decimal> GetBalanceAsync(string userCode);
|
||||
|
||||
/// <summary>
|
||||
/// 获取消费明细
|
||||
@@ -29,12 +28,19 @@ public interface IBalanceRepository : IScopeDependency
|
||||
/// <returns></returns>
|
||||
Task<List<string>> GetWalletFactoriesAsync(string factory);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检测余额是否足够
|
||||
/// 消费余额
|
||||
/// </summary>
|
||||
/// <param name="userCode"></param>
|
||||
/// <param name="factory"></param>
|
||||
/// <param name="cost"></param>
|
||||
/// <returns></returns>
|
||||
Task CheckConsumableAsync(string userCode, string factory, decimal cost);
|
||||
Task ConsumeBalanceAsync(string userCode, decimal cost);
|
||||
|
||||
/// <summary>
|
||||
/// 重算余额
|
||||
/// </summary>
|
||||
/// <param name="userCode"></param>
|
||||
/// <returns></returns>
|
||||
Task CalculateBalanceAsync(string userCode);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using Dlhis.Entity.Canteen.Entities;
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Repository.Implements;
|
||||
@@ -14,19 +16,21 @@ public class BalanceRepository : IBalanceRepository
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<decimal> GetBalanceAsync(string userCode, string factory)
|
||||
public async Task<decimal> GetBalanceAsync(string userCode)
|
||||
{
|
||||
var queryable = _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.UserCode == userCode && x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.Where(x => x.Source != BalanceRecordSourceEnumType.EMealVoucher);
|
||||
// var queryable = _dbContext.Queryable<BalanceRecordEntity>()
|
||||
// .Where(x => x.UserCode == userCode && x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
// .Where(x => x.Source != BalanceRecordSourceEnumType.EMealVoucher);
|
||||
//
|
||||
// if (!factory.IsPresent())
|
||||
// {
|
||||
// return await queryable.SumAsync(x => x.Amount);
|
||||
// }
|
||||
//
|
||||
// var factories = await GetWalletFactoriesAsync(factory);
|
||||
// return await queryable.Where(x => factories.Contains(x.TenantId)).SumAsync(x => x.Amount);
|
||||
|
||||
if (!factory.IsPresent())
|
||||
{
|
||||
return await queryable.SumAsync(x => x.Amount);
|
||||
}
|
||||
|
||||
var factories = await GetWalletFactoriesAsync(factory);
|
||||
return await queryable.Where(x => factories.Contains(x.TenantId)).SumAsync(x => x.Amount);
|
||||
return await _dbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == userCode).Select(x => x.Balance).SingleAsync();
|
||||
}
|
||||
|
||||
public async Task<List<BalanceRecordEntity>> GetBalanceRecordsAsync(string userCode, string factory, DateTime startTime, DateTime endTime)
|
||||
@@ -51,10 +55,68 @@ public class BalanceRepository : IBalanceRepository
|
||||
}
|
||||
|
||||
|
||||
public async Task CheckConsumableAsync(string userCode, string factory, decimal cost)
|
||||
/// <inheritdoc />
|
||||
public async Task ConsumeBalanceAsync(string userCode, decimal cost)
|
||||
{
|
||||
var balance = await GetBalanceAsync(userCode, factory);
|
||||
var isEnough = balance - Math.Abs(cost) >= 0;
|
||||
isEnough.ThrowIfFalse($"钱包余额不足({balance}元)");
|
||||
await CommonHelper.RetryAsync(async () =>
|
||||
{
|
||||
if (!await _dbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == userCode).AnyAsync())
|
||||
{
|
||||
await CalculateBalanceAsync(userCode);
|
||||
}
|
||||
|
||||
var data = await _dbContext.Queryable<UserBalanceEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Balance, x.Version
|
||||
})
|
||||
.SingleAsync();
|
||||
|
||||
var currentBalance = data.Balance - Math.Abs(cost);
|
||||
var isEnough = currentBalance >= 0;
|
||||
isEnough.ThrowIfFalse($"钱包余额不足({data.Balance}元)");
|
||||
|
||||
await _dbContext.Updateable<UserBalanceEntity>()
|
||||
.SetColumns(x => x.Balance == currentBalance)
|
||||
.SetColumns(x => x.Version == data.Version + 1)
|
||||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||||
.Where(x => x.UserCode == userCode && x.Version == data.Version)
|
||||
.ExecuteCommandThrowVersionAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task CalculateBalanceAsync(string userCode)
|
||||
{
|
||||
if (!await _dbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == userCode).AnyAsync())
|
||||
{
|
||||
await _dbContext.InsertAsync(new UserBalanceEntity
|
||||
{
|
||||
UserCode = userCode,
|
||||
Balance = 0,
|
||||
Version = 0
|
||||
});
|
||||
}
|
||||
|
||||
await CommonHelper.RetryAsync(async () =>
|
||||
{
|
||||
var balance = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.Where(x => x.Source != BalanceRecordSourceEnumType.EMealVoucher)
|
||||
.SumAsync(x => x.Amount);
|
||||
|
||||
var version = await _dbContext.Queryable<UserBalanceEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.Select(x => x.Version)
|
||||
.SingleAsync();
|
||||
|
||||
await _dbContext.Updateable<UserBalanceEntity>()
|
||||
.SetColumns(x => x.Balance == balance)
|
||||
.SetColumns(x => x.Version == version + 1)
|
||||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||||
.Where(x => x.UserCode == userCode && x.Version == version)
|
||||
.ExecuteCommandThrowVersionAsync();
|
||||
}, 5);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user