Files
jinko.yikatong/Src/Dlhis.Canteen/Setting/Services/Implements/WalletService.cs
2024-11-14 14:14:47 +08:00

45 lines
1.6 KiB
C#

using Dlhis.Canteen.Order.Repository;
using Dlhis.Canteen.Setting.Response;
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).ToList(p => p.Factory)
})
.ToListAsync();
}
/// <inheritdoc />
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);
}
}