2025-07-05 17:43:48 +08:00
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Volo.Abp.Application.Services;
|
2025-08-10 11:53:28 +08:00
|
|
|
|
using Volo.Abp.Users;
|
2025-07-05 17:43:48 +08:00
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge;
|
2025-08-11 15:51:59 +08:00
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
2025-07-05 17:43:48 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities;
|
2025-08-11 15:51:59 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Managers;
|
2025-08-10 11:53:28 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
2025-10-20 10:18:24 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
2025-07-05 17:43:48 +08:00
|
|
|
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
|
|
|
|
|
2025-08-10 11:53:28 +08:00
|
|
|
|
namespace Yi.Framework.AiHub.Application.Services
|
2025-07-05 17:43:48 +08:00
|
|
|
|
{
|
2025-08-13 22:19:31 +08:00
|
|
|
|
public class RechargeService : ApplicationService, IRechargeService
|
2025-07-05 17:43:48 +08:00
|
|
|
|
{
|
2025-08-10 11:53:28 +08:00
|
|
|
|
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _repository;
|
|
|
|
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
|
|
private readonly IUserService _userService;
|
2025-08-11 15:51:59 +08:00
|
|
|
|
private readonly IRoleService _roleService;
|
|
|
|
|
|
private readonly AiRechargeManager _aiMessageManager;
|
2025-07-05 17:43:48 +08:00
|
|
|
|
|
2025-08-10 11:53:28 +08:00
|
|
|
|
public RechargeService(
|
|
|
|
|
|
ISqlSugarRepository<AiRechargeAggregateRoot> repository,
|
|
|
|
|
|
ICurrentUser currentUser,
|
2025-08-11 15:51:59 +08:00
|
|
|
|
IUserService userService, IRoleService roleService, AiRechargeManager aiMessageManager)
|
2025-08-10 11:53:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
|
|
|
|
|
_currentUser = currentUser;
|
|
|
|
|
|
_userService = userService;
|
2025-08-11 15:51:59 +08:00
|
|
|
|
_roleService = roleService;
|
|
|
|
|
|
_aiMessageManager = aiMessageManager;
|
2025-08-10 11:53:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询已登录的账户充值记录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Route("recharge/account")]
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
public async Task<List<RechargeGetListOutput>> GetListByAccountAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var userId = CurrentUser.Id;
|
|
|
|
|
|
var entities = await _repository._DbQueryable.Where(x => x.UserId == userId)
|
|
|
|
|
|
.OrderByDescending(x => x.CreationTime)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
var output = entities.Adapt<List<RechargeGetListOutput>>();
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给用户充值VIP
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">充值输入参数</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost("recharge/vip")]
|
|
|
|
|
|
public async Task RechargeVipAsync(RechargeCreateInput input)
|
|
|
|
|
|
{
|
2025-08-13 22:19:31 +08:00
|
|
|
|
DateTime? expireDateTime = null;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果传入了月数,计算过期时间
|
|
|
|
|
|
if (input.Months.HasValue && input.Months.Value > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 直接查询该用户最大的过期时间
|
|
|
|
|
|
var maxExpireTime = await _repository._DbQueryable
|
2025-10-20 10:18:24 +08:00
|
|
|
|
.Where(x => x.RechargeType == RechargeTypeEnum.Vip)
|
2025-08-13 22:19:31 +08:00
|
|
|
|
.Where(x => x.UserId == input.UserId && x.ExpireDateTime.HasValue)
|
|
|
|
|
|
.MaxAsync(x => x.ExpireDateTime);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果最大过期时间大于现在时间,从最大过期时间开始计算
|
|
|
|
|
|
// 否则从当天开始计算
|
|
|
|
|
|
DateTime baseDateTime = maxExpireTime.HasValue && maxExpireTime.Value > DateTime.Now
|
|
|
|
|
|
? maxExpireTime.Value
|
|
|
|
|
|
: DateTime.Now;
|
|
|
|
|
|
// 计算新的过期时间
|
|
|
|
|
|
expireDateTime = baseDateTime.AddMonths(input.Months.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果月数为空或0,表示永久VIP,ExpireDateTime保持为null
|
|
|
|
|
|
|
2025-08-10 11:53:28 +08:00
|
|
|
|
// 创建充值记录
|
|
|
|
|
|
var rechargeRecord = new AiRechargeAggregateRoot
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = input.UserId,
|
|
|
|
|
|
RechargeAmount = input.RechargeAmount,
|
|
|
|
|
|
Content = input.Content,
|
2025-08-13 22:19:31 +08:00
|
|
|
|
ExpireDateTime = expireDateTime,
|
2025-08-10 11:53:28 +08:00
|
|
|
|
Remark = input.Remark,
|
2025-10-20 10:18:24 +08:00
|
|
|
|
ContactInfo = input.ContactInfo,
|
|
|
|
|
|
RechargeType = RechargeTypeEnum.Vip
|
2025-08-10 11:53:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 保存充值记录到数据库
|
|
|
|
|
|
await _repository.InsertAsync(rechargeRecord);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用UserService给用户添加VIP角色
|
|
|
|
|
|
await _userService.AddUserRoleByRoleCodeAsync(input.UserId,
|
|
|
|
|
|
new List<string>() { AiHubConst.VipRole, "default" });
|
|
|
|
|
|
}
|
2025-08-11 15:51:59 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除用户vip及角色
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RemoteService(isEnabled: false)]
|
|
|
|
|
|
public async Task RemoveVipRoleByExpireAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var expiredUserIds = await _aiMessageManager.RemoveVipByExpireAsync();
|
|
|
|
|
|
if (expiredUserIds is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 17:43:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|