60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
|
|
namespace Dlhis.Canteen.Order.Repository.Implements;
|
|
|
|
public class BalanceRepository : IBalanceRepository
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public BalanceRepository(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<decimal> GetBalanceAsync(string userCode, string factory)
|
|
{
|
|
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);
|
|
}
|
|
|
|
public async Task<List<BalanceRecordEntity>> GetBalanceRecordsAsync(string userCode, string factory, DateTime startTime, DateTime endTime)
|
|
{
|
|
var factories = await GetWalletFactoriesAsync(factory);
|
|
|
|
return await _dbContext.Queryable<BalanceRecordEntity>()
|
|
.Where(x => x.UserCode == userCode)
|
|
.Where(x => factories.Contains(x.TenantId))
|
|
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
|
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid)
|
|
.Where(x => x.CostDateTime >= startTime && x.CostDateTime <= endTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<string>> GetWalletFactoriesAsync(string factory)
|
|
{
|
|
var wallets = await _dbContext.Queryable<WalletEntity>().ToListAsync();
|
|
var code = wallets.First(x => x.Factory == factory).Code;
|
|
|
|
return wallets.Where(x => x.Code == code).Select(x => x.Factory).ToList();
|
|
}
|
|
|
|
|
|
public async Task CheckConsumableAsync(string userCode, string factory, decimal cost)
|
|
{
|
|
var balance = await GetBalanceAsync(userCode, factory);
|
|
var isEnough = balance - Math.Abs(cost) >= 0;
|
|
isEnough.ThrowIfFalse($"钱包余额不足({balance}元)");
|
|
}
|
|
} |