using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_DTO.BasicInfo;
|
using WIDESEA_IBasicInfoServices;
|
using WIDESEA_Model.Models;
|
|
namespace WIDESEA_BasicInfoServices
|
{
|
public class ProductsService : ServiceBase<Dt_Products, IRepository<Dt_Products>>, IProductsService
|
{
|
private readonly IRepository<Dt_RuntimeCode> _runtimeCodeRepository;
|
private readonly IRepository<Dt_ActivationCode> _activationCodeRepository;
|
private readonly IActivationCodeService _activationCodeService;
|
|
public ProductsService(IRepository<Dt_Products> BaseDal, IRepository<Dt_RuntimeCode> runtimeCodeRepository, IRepository<Dt_ActivationCode> activationCodeRepository, IActivationCodeService activationCodeService) : base(BaseDal)
|
{
|
_runtimeCodeRepository = runtimeCodeRepository;
|
_activationCodeRepository = activationCodeRepository;
|
_activationCodeService = activationCodeService;
|
}
|
|
/// <summary>
|
/// 验证产品激活信息
|
/// </summary>
|
/// <param name="activationModel">包含产品激活所需信息的DTO对象,包括产品名称、运行码、激活码和设备ID</param>
|
/// <returns>返回一个WebResponseContent对象,包含验证结果信息:
|
/// - 成功时返回OK状态
|
/// - 失败时返回具体的错误信息,如"无效的运行码"、"无效的激活码"、"激活码已过期"或"激活码验证失败"</returns>
|
/// <remarks>
|
/// 验证流程:
|
/// 1. 根据产品名称获取产品ID
|
/// 2. 验证运行码是否存在且匹配
|
/// 3. 验证激活码是否存在且匹配
|
/// 4. 检查激活码是否过期
|
/// 5. 重新生成激活码进行验证
|
/// 6. 更新激活状态为已使用
|
/// </remarks>
|
public WebResponseContent ValidateActivation(ActivationDTO activationModel)
|
{
|
int productId = -1;
|
Dt_Products product = BaseDal.QueryFirst(x => x.ProductName == activationModel.ProductName);
|
if (product != null)
|
{
|
productId = product.Id;
|
}
|
|
// 获取运行码记录
|
Dt_RuntimeCode runtimeCode = _runtimeCodeRepository.QueryFirst(rc => rc.RuntimeCode == activationModel.RuntimeCode && rc.DeviceId == activationModel.DeviceId && rc.ProductId == productId);
|
|
if (runtimeCode == null)
|
return WebResponseContent.Instance.Error("无效的运行码");
|
|
// 获取对应的激活码
|
Dt_ActivationCode activation = _activationCodeRepository.QueryFirst(ac => ac.ActivationCode == activationModel.ActivationCode && ac.RuntimeCode == activationModel.RuntimeCode && ac.DeviceId == activationModel.DeviceId && ac.ProductId == productId);
|
|
if (activation == null)
|
return WebResponseContent.Instance.Error("无效的激活码");
|
|
if (activation.ExpiresTime != null && activation.ExpiresTime < DateTime.Now)
|
return WebResponseContent.Instance.Error("激活码已过期");
|
|
ActivationCodeDTO activationCodeDTO = new ActivationCodeDTO
|
{
|
RuntimeCode = runtimeCode.RuntimeCode,
|
DeviceId = activation.DeviceId,
|
ProductName = activationModel.ProductName
|
};
|
|
// 重新生成映射进行验证
|
string regenerated = _activationCodeService.GenerateActivationCode(activationCodeDTO);
|
if (regenerated != activationModel.ActivationCode)
|
return WebResponseContent.Instance.Error("激活码验证失败");
|
|
// 更新激活状态
|
activation.IsUsed = true;
|
activation.UsedTime = DateTime.Now;
|
_activationCodeRepository.UpdateData(activation);
|
|
return WebResponseContent.Instance.OK();
|
}
|
|
/// <summary>
|
/// 获取设备激活信息
|
/// </summary>
|
/// <param name="deviceId">设备ID</param>
|
/// <param name="productName">产品名称</param>
|
/// <returns>
|
/// 返回WebResponseContent对象,包含以下情况:
|
/// 1. 产品不存在时返回错误
|
/// 2. 运行码不存在时返回错误
|
/// 3. 激活码不存在时返回运行码、空激活码和未激活状态
|
/// 4. 激活码已使用时返回运行码、激活码和已激活状态
|
/// 5. 激活码未使用时返回运行码、激活码和未激活状态
|
/// 6. 发生异常时返回错误信息
|
/// </returns>
|
public WebResponseContent GetActivationInfo(string deviceId, string productName)
|
{
|
try
|
{
|
Dt_Products product = BaseDal.QueryFirst(x => x.ProductName == productName);
|
if (product == null)
|
{
|
return WebResponseContent.Instance.Error();
|
}
|
|
Dt_RuntimeCode runtimeCode = _runtimeCodeRepository.QueryFirst(x => x.DeviceId == deviceId && x.ProductId == product.Id);
|
if (runtimeCode == null)
|
{
|
return WebResponseContent.Instance.Error();
|
}
|
|
Dt_ActivationCode activationCode = _activationCodeRepository.QueryFirst(x => x.RuntimeCode == runtimeCode.RuntimeCode && x.ProductId == product.Id && x.DeviceId == deviceId);
|
if (activationCode == null)
|
{
|
return WebResponseContent.Instance.OK(data: new { runtimeCode.RuntimeCode, ActivationCode = "", IsActivated = false });
|
}
|
|
if (activationCode.IsUsed && activationCode.UsedTime != null)
|
{
|
return WebResponseContent.Instance.OK(data: new { runtimeCode.RuntimeCode, activationCode.ActivationCode, IsActivated = true });
|
}
|
|
return WebResponseContent.Instance.OK(data: new { runtimeCode.RuntimeCode, activationCode.ActivationCode, IsActivated = false });
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error(message: "获取激活码信息失败");
|
}
|
}
|
}
|
}
|