Files
jinko.yikatong/Src/Dlhis.Canteen/Setting/Services/Implements/WalletService.cs

53 lines
1.8 KiB
C#

using Dlhis.Canteen.Order.Repository;
using Dlhis.Canteen.Setting.Response;
using Dlhis.Constants;
using Dlhis.Entity.Canteen.Entities;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using SqlSugar;
namespace Dlhis.Canteen.Setting.Services.Implements;
public class WalletService : IWalletService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly IBalanceRepository _balanceRepository;
public WalletService(IDbContext dbContext, ICurrentUser currentUser, IBalanceRepository balanceRepository)
{
_dbContext = dbContext;
_currentUser = currentUser;
_balanceRepository = balanceRepository;
}
/// <inheritdoc />
public async Task<List<WalletResponse>> GetListAsync()
{
return await _dbContext.Queryable<WalletEntity>()
.GroupBy(x => new { x.Code, x.Name })
.Select(x => new WalletResponse
{
Code = x.Code,
Name = x.Name + "钱包",
Factories = SqlFunc.Subqueryable<WalletEntity>()
.Where(p => p.Code == x.Code)
.Where(p => p.IsDeleted == false)
.ToList(p => p.Factory)
})
.ToListAsync();
}
/// <inheritdoc />
public async Task<decimal> GetBalanceAsync(string walletCode)
{
var userCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
if (!await _dbContext.Queryable<UserBalanceEntity>().Where(x => x.UserCode == userCode).AnyAsync())
{
await _balanceRepository.CalculateBalanceAsync(userCode);
}
return await _balanceRepository.GetBalanceAsync(userCode);
}
}