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;
}
///
public async Task> GetListAsync()
{
return await _dbContext.Queryable()
.GroupBy(x => new { x.Code, x.Name })
.Select(x => new WalletResponse
{
Code = x.Code,
Name = x.Name + "钱包",
Factories = SqlFunc.Subqueryable()
.Where(p => p.Code == x.Code)
.Where(p => p.IsDeleted == false)
.ToList(p => p.Factory)
})
.ToListAsync();
}
///
public async Task GetBalanceAsync(string walletCode)
{
var userCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
if (!await _dbContext.Queryable().Where(x => x.UserCode == userCode).AnyAsync())
{
await _balanceRepository.CalculateBalanceAsync(userCode);
}
return await _balanceRepository.GetBalanceAsync(userCode);
}
}