From eb8a48c6427115e40202642cd19e62a287162a65 Mon Sep 17 00:00:00 2001
From: lq1405 <2769838458@qq.com>
Date: Fri, 7 Feb 2025 15:58:00 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AF=95=E7=94=A8=20?=
=?UTF-8?q?=E6=9B=B4=E5=85=B7=E8=AE=BE=E7=BD=AE=E7=9A=84=E8=AF=95=E7=94=A8?=
=?UTF-8?q?=E6=97=B6=E9=97=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ModifySoftwareControlValidityModel.cs | 2 +
LMS.Tools/Extensions/ConvertExtension.cs | 17 +++++++
LMS.service/Controllers/SoftWareController.cs | 12 +----
.../SoftwareService/SoftwareControlService.cs | 48 +++++++++++++++----
4 files changed, 60 insertions(+), 19 deletions(-)
diff --git a/LMS.Repository/Software/ModifySoftwareControlValidityModel.cs b/LMS.Repository/Software/ModifySoftwareControlValidityModel.cs
index 596d9cd..4e2c08f 100644
--- a/LMS.Repository/Software/ModifySoftwareControlValidityModel.cs
+++ b/LMS.Repository/Software/ModifySoftwareControlValidityModel.cs
@@ -7,5 +7,7 @@ namespace LMS.Repository.Software
public SoftwareControlEnum.SoftwareControlValidityEnum? ExpirationTime { get; set; }
public bool? IsForever { get; set; }
+
+ public bool? IsTry { get; set; }
}
}
diff --git a/LMS.Tools/Extensions/ConvertExtension.cs b/LMS.Tools/Extensions/ConvertExtension.cs
index cce56c3..b9fb380 100644
--- a/LMS.Tools/Extensions/ConvertExtension.cs
+++ b/LMS.Tools/Extensions/ConvertExtension.cs
@@ -40,5 +40,22 @@
return 0; // 默认返回0
}
+
+ ///
+ /// 将字符串转换为int,默认或者是转换错误返回0
+ ///
+ ///
+ ///
+ public static int ConvertStringToIntOrDefault(string input)
+ {
+ if (int.TryParse(input, out int result))
+ {
+ return result;
+ }
+ else
+ {
+ return 0;
+ }
+ }
}
}
diff --git a/LMS.service/Controllers/SoftWareController.cs b/LMS.service/Controllers/SoftWareController.cs
index df6c314..b24da28 100644
--- a/LMS.service/Controllers/SoftWareController.cs
+++ b/LMS.service/Controllers/SoftWareController.cs
@@ -1,16 +1,10 @@
-using LMS.Repository.DB;
-using LMS.Repository.DTO;
+using LMS.Repository.DTO;
using LMS.Repository.DTO.Software;
-using LMS.Repository.Models.User;
using LMS.Repository.Software;
-using LMS.Repository.User;
using LMS.service.Service.SoftwareService;
-using LMS.service.Service.UserService;
using LMS.Tools.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.RazorPages;
-using Microsoft.IdentityModel.Tokens;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using static LMS.Common.Enums.ResponseCodeEnum;
@@ -55,10 +49,6 @@ namespace LMS.service.Controllers
{
return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError);
}
- if (model.IsForever == null && model.ExpirationTime == null)
- {
- return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError);
- }
long requestUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
return await _softwareControlService.ModifySoftwareControlValidity(id, model, requestUserId);
}
diff --git a/LMS.service/Service/SoftwareService/SoftwareControlService.cs b/LMS.service/Service/SoftwareService/SoftwareControlService.cs
index 6a3c90f..14f3781 100644
--- a/LMS.service/Service/SoftwareService/SoftwareControlService.cs
+++ b/LMS.service/Service/SoftwareService/SoftwareControlService.cs
@@ -117,18 +117,18 @@ namespace LMS.service.Service.SoftwareService
{
try
{
+ if (model.IsForever == null && model.ExpirationTime == null && model.IsTry == null)
+ {
+ return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError);
+ }
+
+ model.IsTry ??= false;
+
+ // 检查用户是不是管理员
if (!await _userBasicDao.CheckUserIsAdminOrSuperAdmin(requestUserId))
{
return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
}
- if (model.ExpirationTime != null)
- {
- // 判断model传入的枚举值是不是再对应的enum中是有效的
- if (!Enum.IsDefined(typeof(SoftwareControlEnum.SoftwareControlValidityEnum), model.ExpirationTime))
- {
- return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "传入的到期时间不在有效范围内");
- }
- }
// 判断指定ID的软件控制项是否存在
SoftwareControl? softwareControl = await _dbContext.SoftwareControl.FirstOrDefaultAsync(x => x.Id == id);
@@ -137,6 +137,38 @@ namespace LMS.service.Service.SoftwareService
return APIResponseModel.CreateErrorResponseModel(ResponseCode.IdDateNotFound);
}
+ // 试用
+ if (model.IsTry == true)
+ {
+ // 设置更新时间和更新人
+ softwareControl.UpdatedTime = BeijingTimeExtension.GetBeijingTime();
+ softwareControl.UpdatedUserId = requestUserId;
+
+ double trialDays = 1;
+ Options? options = await _dbContext.Options.FirstOrDefaultAsync(x => x.Key == "LaiToolTrialDays");
+ if (options != null)
+ {
+ double s = options.GetValueObject();
+ trialDays = s;
+ }
+
+ softwareControl.ExpirationTime = BeijingTimeExtension.GetBeijingTime().AddDays(trialDays);
+ softwareControl.IsForever = false;
+ // 更新数据库
+ _dbContext.SoftwareControl.Update(softwareControl);
+ await _dbContext.SaveChangesAsync();
+ return APIResponseModel.CreateSuccessResponseModel("修改成功");
+ }
+
+ if (model.ExpirationTime != null)
+ {
+ // 判断model传入的枚举值是不是再对应的enum中是有效的
+ if (!Enum.IsDefined(typeof(SoftwareControlEnum.SoftwareControlValidityEnum), model.ExpirationTime))
+ {
+ return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "传入的到期时间不在有效范围内");
+ }
+ }
+
// 判断当前数据是不是永久,如果是永久,就不能修改
if (softwareControl.IsForever && model.IsForever == true)
{