fix 消费恢复为余额表

This commit is contained in:
2024-11-21 10:27:08 +08:00
parent 2a97798118
commit 484be10fde
8 changed files with 168 additions and 36 deletions

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -26,9 +26,10 @@ public class MiddleService : IMiddleService
if (!await _dbContext.Queryable<BalanceRecordEntity>().AnyAsync(x => x.No == entity.No))
{
await _dbContext.InsertAsync(entity);
await _balanceRepository.CalculateBalanceAsync(entity.UserCode);
}
var balance = await _balanceRepository.GetBalanceAsync(entity.UserCode, string.Empty);
var balance = await _balanceRepository.GetBalanceAsync(entity.UserCode);
//获取人员余额
return await _dbContext.Queryable<GainfoEntity>()

View File

@@ -86,7 +86,7 @@ public class OrderService : IOrderService
var jinkoCode = _currentUser.GetUserClaim("jinko-code") ?? string.Empty;
if (jinkoCode.IsPresent())
{
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == jinkoCode).FirstAsync();
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == jinkoCode).SingleAsync();
}
var pageResult = await _dbContext.Queryable<CanteenOrderEntity>().Where(x => x.UserNo == gainfo.No)
@@ -319,6 +319,7 @@ public class OrderService : IOrderService
await _dbContext.InsertAsync(order);
var balanceRecord = order.Adapt<BalanceRecordEntity>();
await _dbContext.InsertAsync(balanceRecord);
await _balanceRepository.CalculateBalanceAsync(balanceRecord.UserCode);
}
/// <summary>
@@ -347,7 +348,7 @@ public class OrderService : IOrderService
//校验
await Verify(order);
await _balanceRepository.CheckConsumableAsync(order.UserCode, order.TenantId, order.Amount);
await _balanceRepository.ConsumeBalanceAsync(order.UserCode, order.Amount);
//写入记录
var balanceRecord = order.Adapt<BalanceRecordEntity>();
@@ -479,14 +480,7 @@ public class OrderService : IOrderService
}
}
//获取人员信息
var currentFactory = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == request.DeviceUniqueNo)
.Select(x => x.TenantId)
.SingleAsync();
var balance = await _balanceRepository.GetBalanceAsync(gainfo.Code, currentFactory);
var totalBalance = await _balanceRepository.GetBalanceAsync(gainfo.Code, string.Empty);
var balance = await _balanceRepository.GetBalanceAsync(gainfo.Code);
var data = await _dbContext.Queryable<GainfoEntity>()
.LeftJoin<DepartmentEntity>((x, department) => x.DepartmentNo == department.No)
@@ -499,7 +493,7 @@ public class OrderService : IOrderService
DepartmentName = department.Name,
CurrentBalance = balance,
CurrentTotalBalance = totalBalance
CurrentTotalBalance = balance
})
.SingleAsync();

View File

@@ -38,8 +38,12 @@ public class WalletService : IWalletService
public async Task<decimal> GetBalanceAsync(string walletCode)
{
var userCode = _currentUser.GetUserClaim("jinko-code");
var factory = await _dbContext.Queryable<WalletEntity>().Where(x => x.Code == walletCode).Select(x => x.Factory).FirstAsync();
return await _balanceRepository.GetBalanceAsync(userCode, factory);
if (!await _dbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == userCode).AnyAsync())
{
await _balanceRepository.CalculateBalanceAsync(userCode);
}
return await _balanceRepository.GetBalanceAsync(userCode);
}
}

View File

@@ -0,0 +1,27 @@
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar;
using SqlSugar;
namespace Dlhis.Entity.Canteen.Entities;
/// <summary>
/// 用户余额表
/// </summary>
[SugarTable(TableName = "UserBalances", TableDescription = "用户余额表")]
public class UserBalanceEntity : SugarBaseEntity, IVersion
{
/// <summary>
/// 学工号
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "学工号", UniqueGroupNameList = new[] { "idx_UserBalances_useCode" })]
public string UserCode { get; set; }
/// <summary>
/// 余额
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "余额")]
public decimal Balance { get; set; }
/// <inheritdoc />
public int Version { get; set; }
}

View File

@@ -1,3 +1,4 @@
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar;
using SqlSugar;
@@ -7,7 +8,7 @@ namespace Dlhis.Entity.Canteen.Entities;
/// 钱包
/// </summary>
[SugarTable(TableName = "Wallets", TableDescription = "钱包")]
public class WalletEntity : SugarBaseEntity
public class WalletEntity : SugarBaseEntity , ISoftDeleted
{
/// <summary>
/// 钱包编码
@@ -26,4 +27,7 @@ public class WalletEntity : SugarBaseEntity
/// </summary>
[SugarColumn(IsNullable = false, UniqueGroupNameList = new[] { "idx_wallets_factry" })]
public string Factory { get; set; }
/// <inheritdoc />
public bool IsDeleted { get; set; }
}

View File

@@ -6,6 +6,8 @@ using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Request;
using Dlhis.ScheduledTask.Common.Services;
using Hangfire;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.Utils;
namespace Dlhis.ScheduledTask.Jobs;
@@ -119,7 +121,39 @@ public class MealSupplementHandlerJob : BaseJob
IsDeleted = false,
});
if (!await DbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == gainfo.Code).AnyAsync())
{
await DbContext.InsertAsync(new UserBalanceEntity
{
UserCode = gainfo.Code,
Balance = 0,
Version = 0
});
}
await CommonHelper.RetryAsync(async () =>
{
var balance = await DbContext.Queryable<BalanceRecordEntity>()
.Where(x => x.UserCode == gainfo.Code)
.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 == gainfo.Code)
.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 == gainfo.Code && x.Version == version)
.ExecuteCommandThrowVersionAsync();
}, 5);
var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
{
new()