using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Volo.Abp; using Volo.Abp.Application.Services; using Volo.Abp.Users; using Yi.Framework.AiHub.Application.Contracts.Dtos.ActivationCode; using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge; using Yi.Framework.AiHub.Application.Contracts.IServices; using Yi.Framework.AiHub.Domain.Managers; using Yi.Framework.AiHub.Domain.Shared.Enums; namespace Yi.Framework.AiHub.Application.Services; /// /// 激活码服务 /// public class ActivationCodeService : ApplicationService, IActivationCodeService { private readonly ActivationCodeManager _activationCodeManager; private readonly IRechargeService _rechargeService; private readonly PremiumPackageManager _premiumPackageManager; public ActivationCodeService( ActivationCodeManager activationCodeManager, IRechargeService rechargeService, PremiumPackageManager premiumPackageManager) { _activationCodeManager = activationCodeManager; _rechargeService = rechargeService; _premiumPackageManager = premiumPackageManager; } /// /// 批量生成激活码 /// [Authorize] [HttpPost("activationCode/Batch")] public async Task CreateBatchAsync(ActivationCodeCreateInput input) { var entities = await _activationCodeManager.CreateBatchAsync( input.GoodsType, input.Count, input.IsReusable, input.IsSameTypeOnce, input.Remark); return new ActivationCodeCreateOutput { GoodsType = input.GoodsType, Count = input.Count, Codes = entities.Select(x => x.Code).ToList() }; } /// /// 兑换激活码 /// [Authorize] [HttpPost("activationCode/Redeem")] public async Task RedeemAsync(ActivationCodeRedeemInput input) { var userId = CurrentUser.GetId(); var redeemContext = await _activationCodeManager.RedeemAsync(userId, input.Code); var goodsType = redeemContext.ActivationCode.GoodsType; var goods = redeemContext.Goods; var packageName = redeemContext.PackageName; var totalAmount = goods.Price; if (goods.TokenAmount > 0) { await _premiumPackageManager.CreatePremiumPackageAsync( userId, goods.TokenAmount, packageName, totalAmount, "激活码兑换", expireMonths: null, isCreateRechargeRecord: !goods.IsCombo); } if (goods.VipMonths > 0) { await _rechargeService.RechargeVipAsync(new RechargeCreateInput { UserId = userId, RechargeAmount = totalAmount, Content = packageName, Months = goods.VipMonths, Remark = "激活码兑换", ContactInfo = null }); } return new ActivationCodeRedeemOutput { GoodsType = goodsType, PackageName = packageName, Content = packageName }; } }