live-forum/server/webapi/LiveForum/LiveForum.Service/Cdk/CdkService.cs
2026-03-24 11:27:37 +08:00

150 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FreeSql;
using LiveForum.IService.Cdk;
using LiveForum.IService.Others;
using LiveForum.Model;
using LiveForum.Model.Dto.Cdk;
namespace LiveForum.Service.Cdk
{
/// <summary>
/// CDK服务实现
/// </summary>
public class CdkService : ICdkService
{
private readonly IBaseRepository<T_CDKs> _cdkRepository;
private readonly IBaseRepository<T_Users> _userRepository;
private readonly ISystemSettingsService _systemSettingsService;
/// <summary>
/// CDK功能开关配置键
/// </summary>
public const string CDK_ENABLED_KEY = "cdk_enabled";
/// <summary>
/// 构造函数
/// </summary>
public CdkService(
IBaseRepository<T_CDKs> cdkRepository,
IBaseRepository<T_Users> userRepository,
ISystemSettingsService systemSettingsService)
{
_cdkRepository = cdkRepository;
_userRepository = userRepository;
_systemSettingsService = systemSettingsService;
}
/// <summary>
/// 检查CDK状态
/// </summary>
public async Task<CdkStatusDto> CheckCdkStatusAsync(long userId)
{
// 获取CDK功能是否启用
var cdkEnabled = await _systemSettingsService.GetSettingAsync(CDK_ENABLED_KEY);
var isCdkEnabled = string.Equals(cdkEnabled, "true", StringComparison.OrdinalIgnoreCase);
// 获取用户CDK激活状态
var user = await _userRepository.Select
.Where(x => x.Id == userId)
.FirstAsync();
var isActivated = user?.IsCdkActivated ?? false;
return new CdkStatusDto
{
CdkEnabled = isCdkEnabled,
IsActivated = isActivated
};
}
/// <summary>
/// 激活CDK
/// </summary>
public async Task<string> ActivateCdkAsync(long userId, string code)
{
if (string.IsNullOrWhiteSpace(code))
{
return "请输入有效 CDK";
}
// 查找CDK
var cdk = await _cdkRepository.Select
.Where(x => x.Code == code.Trim())
.FirstAsync();
// CDK不存在
if (cdk == null)
{
return "请输入有效 CDK";
}
// CDK已被使用
if (cdk.Status == 1)
{
return "该 CDK 已被激活";
}
// 获取用户
var user = await _userRepository.Select
.Where(x => x.Id == userId)
.FirstAsync();
if (user == null)
{
return "用户不存在";
}
// 用户已激活
if (user.IsCdkActivated)
{
return "您已激活过 CDK";
}
var now = DateTime.Now;
// 更新CDK状态
cdk.Status = 1;
cdk.UsedByUserId = userId;
cdk.UsedAt = now;
await _cdkRepository.UpdateAsync(cdk);
// 更新用户CDK激活状态
user.IsCdkActivated = true;
user.CdkActivatedAt = now;
await _userRepository.UpdateAsync(user);
return "CDK 激活成功";
}
/// <summary>
/// 检查用户是否需要CDK激活才能进行互动操作
/// </summary>
public async Task<bool> RequiresCdkActivationAsync(long userId)
{
// 获取CDK功能是否启用
var cdkEnabled = await _systemSettingsService.GetSettingAsync(CDK_ENABLED_KEY);
var isCdkEnabled = string.Equals(cdkEnabled, "true", StringComparison.OrdinalIgnoreCase);
// 如果CDK功能未启用不需要激活
if (!isCdkEnabled)
{
return false;
}
// 获取用户CDK激活状态
var user = await _userRepository.Select
.Where(x => x.Id == userId)
.FirstAsync();
// 如果用户已激活,不需要激活
if (user?.IsCdkActivated == true)
{
return false;
}
// 需要激活但未激活
return true;
}
}
}