using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Application.Services; using Volo.Abp.Users; using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge; using Yi.Framework.AiHub.Application.Contracts.IServices; using Yi.Framework.AiHub.Domain.Entities; using Yi.Framework.AiHub.Domain.Managers; using Yi.Framework.AiHub.Domain.Shared.Consts; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Application.Services { public class RechargeService : ApplicationService,IRechargeService { private readonly ISqlSugarRepository _repository; private readonly ICurrentUser _currentUser; private readonly IUserService _userService; private readonly IRoleService _roleService; private readonly AiRechargeManager _aiMessageManager; public RechargeService( ISqlSugarRepository repository, ICurrentUser currentUser, IUserService userService, IRoleService roleService, AiRechargeManager aiMessageManager) { _repository = repository; _currentUser = currentUser; _userService = userService; _roleService = roleService; _aiMessageManager = aiMessageManager; } /// /// 查询已登录的账户充值记录 /// /// [Route("recharge/account")] [Authorize] public async Task> GetListByAccountAsync() { var userId = CurrentUser.Id; var entities = await _repository._DbQueryable.Where(x => x.UserId == userId) .OrderByDescending(x => x.CreationTime) .ToListAsync(); var output = entities.Adapt>(); return output; } /// /// 给用户充值VIP /// /// 充值输入参数 /// [HttpPost("recharge/vip")] public async Task RechargeVipAsync(RechargeCreateInput input) { // 创建充值记录 var rechargeRecord = new AiRechargeAggregateRoot { UserId = input.UserId, RechargeAmount = input.RechargeAmount, Content = input.Content, ExpireDateTime = input.ExpireDateTime, Remark = input.Remark, ContactInfo = input.ContactInfo }; // 保存充值记录到数据库 await _repository.InsertAsync(rechargeRecord); // 使用UserService给用户添加VIP角色 await _userService.AddUserRoleByRoleCodeAsync(input.UserId, new List() { AiHubConst.VipRole, "default" }); } /// /// 移除用户vip及角色 /// [RemoteService(isEnabled: false)] public async Task RemoveVipRoleByExpireAsync() { var expiredUserIds = await _aiMessageManager.RemoveVipByExpireAsync(); if (expiredUserIds is not null) { await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole); } } } }