z8018
2 天以前 d8dc91f9c1fece5711e38edd1b1274cb9e579015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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: "获取激活码信息失败");
            }
        }
    }
}