From 61c4e0f977baa22071b69165aa2c25eb3fadba34 Mon Sep 17 00:00:00 2001 From: lq1405 <2769838458@qq.com> Date: Fri, 28 Mar 2025 22:13:41 +0800 Subject: [PATCH] =?UTF-8?q?v=201.0.7=20=E6=96=B0=E5=A2=9E=E8=BD=AF?= =?UTF-8?q?=E4=BB=B6=E6=8E=88=E6=9D=83=E5=92=8C=E6=95=B0=E6=8D=AE=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LMS.Common/Attributes/DescriptionAttribute.cs | 2 +- LMS.Common/Enums/DataInfoEnum.cs | 10 + LMS.Common/Enums/MachineAuthorizationEnum.cs | 10 + LMS.DAO/ApplicationDbContext.cs | 4 + LMS.Repository/DB/DataInfo.cs | 30 ++ LMS.Repository/DB/MachineAuthorization.cs | 64 ++++ .../DTO/OtherDto/MachineAuthorizationDto.cs | 66 ++++ LMS.Repository/Other/AddDataInfo.cs | 20 ++ .../Other/AddMachineAuthorization.cs | 38 +++ .../QuartzTaskSchedulerConfig.cs | 2 +- .../Configuration/ServiceConfiguration.cs | 5 +- LMS.service/Controllers/OtherController.cs | 132 +++++++ LMS.service/Service/Other/DataInfoService.cs | 141 ++++++++ .../Other/MachineAuthorizationService.cs | 323 ++++++++++++++++++ LMS.service/appsettings.json | 2 +- SQL/v1.0.7/DataInfo.sql | 32 ++ SQL/v1.0.7/MachineAuthorization.sql | 38 +++ 17 files changed, 914 insertions(+), 5 deletions(-) create mode 100644 LMS.Common/Enums/DataInfoEnum.cs create mode 100644 LMS.Common/Enums/MachineAuthorizationEnum.cs create mode 100644 LMS.Repository/DB/DataInfo.cs create mode 100644 LMS.Repository/DB/MachineAuthorization.cs create mode 100644 LMS.Repository/DTO/OtherDto/MachineAuthorizationDto.cs create mode 100644 LMS.Repository/Other/AddDataInfo.cs create mode 100644 LMS.Repository/Other/AddMachineAuthorization.cs create mode 100644 LMS.service/Controllers/OtherController.cs create mode 100644 LMS.service/Service/Other/DataInfoService.cs create mode 100644 LMS.service/Service/Other/MachineAuthorizationService.cs create mode 100644 SQL/v1.0.7/DataInfo.sql create mode 100644 SQL/v1.0.7/MachineAuthorization.sql diff --git a/LMS.Common/Attributes/DescriptionAttribute.cs b/LMS.Common/Attributes/DescriptionAttribute.cs index f73aebf..cb22652 100644 --- a/LMS.Common/Attributes/DescriptionAttribute.cs +++ b/LMS.Common/Attributes/DescriptionAttribute.cs @@ -10,4 +10,4 @@ Description = description; } } -} +} \ No newline at end of file diff --git a/LMS.Common/Enums/DataInfoEnum.cs b/LMS.Common/Enums/DataInfoEnum.cs new file mode 100644 index 0000000..8f62ad2 --- /dev/null +++ b/LMS.Common/Enums/DataInfoEnum.cs @@ -0,0 +1,10 @@ +using LMS.Common.Attributes; + +namespace LMS.Common.Enums +{ + public enum DataInfoTypeEnum + { + [Description("Discord")] + Discord = 0, + } +} diff --git a/LMS.Common/Enums/MachineAuthorizationEnum.cs b/LMS.Common/Enums/MachineAuthorizationEnum.cs new file mode 100644 index 0000000..e68a3cd --- /dev/null +++ b/LMS.Common/Enums/MachineAuthorizationEnum.cs @@ -0,0 +1,10 @@ +using System.ComponentModel; + +namespace LMS.Common.Enums +{ + public enum MachineAuthorizationEnum + { + [Description("NanFengAI")] + NanFengAI = 0, + } +} diff --git a/LMS.DAO/ApplicationDbContext.cs b/LMS.DAO/ApplicationDbContext.cs index 178a037..4331606 100644 --- a/LMS.DAO/ApplicationDbContext.cs +++ b/LMS.DAO/ApplicationDbContext.cs @@ -41,6 +41,10 @@ namespace LMS.DAO public DbSet SoftwareControl { get; set; } + public DbSet MachineAuthorization { get; set; } + + public DbSet DataInfo { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); diff --git a/LMS.Repository/DB/DataInfo.cs b/LMS.Repository/DB/DataInfo.cs new file mode 100644 index 0000000..1cc7519 --- /dev/null +++ b/LMS.Repository/DB/DataInfo.cs @@ -0,0 +1,30 @@ +using LMS.Common.Enums; +using System.ComponentModel.DataAnnotations; + +namespace LMS.Repository.DB +{ + public class DataInfo + { + /// + /// ID + /// + [Required] + public required string ID { get; set; } + /// + /// 数据的类型 + /// + [Required] + public required DataInfoTypeEnum Type { get; set; } + + /// + /// 数据的字符串 + /// + [Required] + public required string DataString { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreatedTime { get; set; } + } +} diff --git a/LMS.Repository/DB/MachineAuthorization.cs b/LMS.Repository/DB/MachineAuthorization.cs new file mode 100644 index 0000000..050af2b --- /dev/null +++ b/LMS.Repository/DB/MachineAuthorization.cs @@ -0,0 +1,64 @@ +using LMS.Common.Enums; +using System.ComponentModel.DataAnnotations; + +namespace LMS.Repository.DB +{ + public class MachineAuthorization + { + /// + /// ID + /// + [Required] + public required string ID { get; set; } + + /// + /// 机器码或者是授权码 + /// + [Required] + public required string MachineID { get; set; } + + /// + /// 授权软件类型 + /// + [Required] + public required MachineAuthorizationEnum Type { get; set; } + + /// + /// 授权日期 + /// + [Required] + public DateTime AuthorizedDate { get; set; } + + /// + /// 过期日期 + /// + [Required] + public DateTime ExpiryDate { get; set; } + + /// + /// 授权码 + /// + [Required] + public required string AuthorizationCode { get; set; } + + /// + /// 创建用户ID + /// + public long CreatedUserID { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreatedDate { get; set; } + + /// + /// 更新用户ID + /// + public long UpdatedUserID { get; set; } + + /// + /// 更新时间 + /// + public DateTime UpdatedDate { get; set; } + } +} diff --git a/LMS.Repository/DTO/OtherDto/MachineAuthorizationDto.cs b/LMS.Repository/DTO/OtherDto/MachineAuthorizationDto.cs new file mode 100644 index 0000000..31ac378 --- /dev/null +++ b/LMS.Repository/DTO/OtherDto/MachineAuthorizationDto.cs @@ -0,0 +1,66 @@ +using LMS.Common.Enums; +using LMS.Repository.DTO.UserDto; +using System.ComponentModel.DataAnnotations; + +namespace LMS.Repository.DTO.OtherDto +{ + public class MachineAuthorizationDto + { + + /// + /// ID + /// + [Required] + public required string ID { get; set; } + + /// + /// 机器码或者是授权码 + /// + [Required] + public required string MachineID { get; set; } + + /// + /// 授权软件类型 + /// + [Required] + public required MachineAuthorizationEnum Type { get; set; } + + /// + /// 授权日期 + /// + [Required] + public DateTime AuthorizedDate { get; set; } + + /// + /// 过期日期 + /// + [Required] + public DateTime ExpiryDate { get; set; } + + /// + /// 授权码 + /// + [Required] + public required string AuthorizationCode { get; set; } + + /// + /// 创建用户 + /// + public UserBaseDto? CreatedUser { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreatedDate { get; set; } + + /// + /// 更新用户ID + /// + public UserBaseDto? UpdatedUser { get; set; } + + /// + /// 更新时间 + /// + public DateTime UpdatedDate { get; set; } + } +} diff --git a/LMS.Repository/Other/AddDataInfo.cs b/LMS.Repository/Other/AddDataInfo.cs new file mode 100644 index 0000000..dcbab3d --- /dev/null +++ b/LMS.Repository/Other/AddDataInfo.cs @@ -0,0 +1,20 @@ +using LMS.Common.Enums; +using System.ComponentModel.DataAnnotations; + +namespace LMS.Repository.Other +{ + public class AddDataInfo + { + /// + /// 数据的类型 + /// + [Required] + public required DataInfoTypeEnum Type { get; set; } + + /// + /// 数据的字符串 + /// + [Required] + public required string DataString { get; set; } + } +} diff --git a/LMS.Repository/Other/AddMachineAuthorization.cs b/LMS.Repository/Other/AddMachineAuthorization.cs new file mode 100644 index 0000000..6dabe98 --- /dev/null +++ b/LMS.Repository/Other/AddMachineAuthorization.cs @@ -0,0 +1,38 @@ +using LMS.Common.Enums; +using System.ComponentModel.DataAnnotations; + +namespace LMS.Repository.Other +{ + public class AddMachineAuthorization + { + /// + /// 机器码或者是授权码 + /// + [Required] + public required string MachineID { get; set; } + + /// + /// 授权日期 + /// + [Required] + public DateTime AuthorizedDate { get; set; } + + /// + /// 过期日期 + /// + [Required] + public DateTime ExpiryDate { get; set; } + + /// + /// 授权码 + /// + [Required] + public required string AuthorizationCode { get; set; } + + /// + /// 授权软件类型 + /// + [Required] + public required MachineAuthorizationEnum Type { get; set; } + } +} diff --git a/LMS.service/Configuration/QuartzTaskSchedulerConfig.cs b/LMS.service/Configuration/QuartzTaskSchedulerConfig.cs index 9996b6d..7d16d2f 100644 --- a/LMS.service/Configuration/QuartzTaskSchedulerConfig.cs +++ b/LMS.service/Configuration/QuartzTaskSchedulerConfig.cs @@ -46,7 +46,7 @@ namespace LMS.service.Configuration q.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity("MonthlyTaskTrigger", "DefaultGroup") - .WithCronSchedule("0 0 18 24 * ?")); // 每月1号凌晨0点 + .WithCronSchedule("0 0 0 1 * ?")); // 每月1号凌晨0点 }); // 添加 Quartz 托管服务 diff --git a/LMS.service/Configuration/ServiceConfiguration.cs b/LMS.service/Configuration/ServiceConfiguration.cs index a587beb..945d580 100644 --- a/LMS.service/Configuration/ServiceConfiguration.cs +++ b/LMS.service/Configuration/ServiceConfiguration.cs @@ -5,6 +5,7 @@ using LMS.DAO.UserDAO; using LMS.service.Configuration.InitConfiguration; using LMS.service.Extensions.Mail; using LMS.service.Service; +using LMS.service.Service.Other; using LMS.service.Service.PermissionService; using LMS.service.Service.PromptService; using LMS.service.Service.RoleService; @@ -35,8 +36,8 @@ namespace Lai_server.Configuration services.AddScoped(); services.AddScoped(); services.AddScoped(); - - + services.AddScoped(); + services.AddScoped(); // 注入 DAO services.AddScoped(); diff --git a/LMS.service/Controllers/OtherController.cs b/LMS.service/Controllers/OtherController.cs new file mode 100644 index 0000000..a986221 --- /dev/null +++ b/LMS.service/Controllers/OtherController.cs @@ -0,0 +1,132 @@ +using LMS.Common.Extensions; +using LMS.Repository.DB; +using LMS.Repository.DTO; +using LMS.Repository.DTO.OtherDto; +using LMS.Repository.Other; +using LMS.service.Service.Other; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System.ComponentModel.DataAnnotations; +using static LMS.Common.Enums.ResponseCodeEnum; + +namespace LMS.service.Controllers +{ + [Route("lms/[controller]/[action]")] + [ApiController] + public class OtherController(MachineAuthorizationService machineAuthorizationService, DataInfoService dataInfoService) : Controller + { + private readonly MachineAuthorizationService _machineAuthorizationService = machineAuthorizationService; + private readonly DataInfoService _dataInfoService = dataInfoService; + + #region 新增机器码/指定授权码 + + /// + /// 新增机器码/指定授权码 + /// + /// + /// + [HttpPost] + [Authorize] + public async Task>> AddMachineAuthorization(AddMachineAuthorization addMachineAuthorization) + { + if (!ModelState.IsValid) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError); + } + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _machineAuthorizationService.AddMachineAuthorization(addMachineAuthorization, userId); + } + + #endregion + + #region 修改机器码/指定授权码 + [HttpPost("{id}")] + [Authorize] + public async Task>> ModifyMachineAuthorization(string id, AddMachineAuthorization addMachineAuthorization) + { + if (!ModelState.IsValid) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError); + } + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _machineAuthorizationService.ModifyMachineAuthorization(id, addMachineAuthorization, userId); + } + + #endregion + + #region 获取指定的机器授权码 + + [HttpGet("{id}")] + [Authorize] + public async Task>> GetMachineAuthorization(string id) + { + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _machineAuthorizationService.GetMachineAuthorization(id, userId); + } + + #endregion + + #region 获取指定的机器码授权码列表 + + [HttpGet] + [Authorize] + public async Task>>> QueryMachineAuthorizationCollection([Required] int page, [Required] int pageSize, string? machineId, string? AuthorizationCode, int? type) + { + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _machineAuthorizationService.QueryMachineAuthorizationCollection(page, pageSize, machineId, AuthorizationCode, type, userId); + } + + #endregion + + #region 删除指定的机器码授权码 + + [HttpDelete("{id}")] + [Authorize] + public async Task>> DeleteMachineAuthorization(string id) + { + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _machineAuthorizationService.DeleteMachineAuthorization(id, userId); + } + + #endregion + + + #region 新增数据信息 + + [HttpPost] + public async Task>> AddDataInfo(AddDataInfo addDataInfo) + { + if (!ModelState.IsValid) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError); + } + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _dataInfoService.AddDataInfo(addDataInfo); + } + + #endregion + + #region 查询指定类型的数据信息 + + [HttpGet] + [Authorize] + public async Task>>> QueryDataInfoCollection([Required] int page, [Required] int pageSize, int? type) + { + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _dataInfoService.QueryDataInfoCollection(page, pageSize, type, userId); + } + + #endregion + + #region 删除指定的数据信息 + + [HttpDelete("{id}")] + [Authorize] + public async Task>> DeleteDataInfo(string id) + { + long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _dataInfoService.DeleteDataInfo(id, userId); + } + #endregion + } +} diff --git a/LMS.service/Service/Other/DataInfoService.cs b/LMS.service/Service/Other/DataInfoService.cs new file mode 100644 index 0000000..d2b0d9a --- /dev/null +++ b/LMS.service/Service/Other/DataInfoService.cs @@ -0,0 +1,141 @@ +using LMS.Common.Enums; +using LMS.Common.Extensions; +using LMS.DAO; +using LMS.DAO.UserDAO; +using LMS.Repository.DB; +using LMS.Repository.DTO; +using LMS.Repository.DTO.UserDto; +using LMS.Repository.Other; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using static LMS.Common.Enums.ResponseCodeEnum; + +namespace LMS.service.Service.Other +{ + public class DataInfoService(ApplicationDbContext dbContext, UserBasicDao userBasicDao) + { + private readonly ApplicationDbContext _context = dbContext; + private readonly UserBasicDao _userBasicDao = userBasicDao; + + #region 新增数据信息 + /// + /// 新增数据信息 + /// + /// + /// + /// + public async Task>> AddDataInfo(AddDataInfo addDataInfo) + { + try + { + // 判断类型是不是存在 + if (!Enum.IsDefined(typeof(DataInfoTypeEnum), addDataInfo.Type)) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "授权类型不存在"); + } + // 直接开始添加 + DataInfo dataInfo = new DataInfo + { + ID = Guid.NewGuid().ToString(), + Type = addDataInfo.Type, + DataString = addDataInfo.DataString, + CreatedTime = BeijingTimeExtension.GetBeijingTime() + }; + await _context.DataInfo.AddAsync(dataInfo); + await _context.SaveChangesAsync(); + return APIResponseModel.CreateSuccessResponseModel("数据新增成功"); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + #region 获取数据信息 + /// + /// 获取数据信息 + /// + /// + /// + /// + /// + /// + public async Task>>> QueryDataInfoCollection(int page, int pageSize, int? type, long requestUserId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId); + if (!isSuperAdmin) + { + return APIResponseModel>.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + IQueryable query = _context.DataInfo; + + if (type.HasValue) + { + query = query.Where(x => x.Type == (DataInfoTypeEnum)type); + } + + // 总数 + int total = await query.CountAsync(); + + // 分页 + query = query.OrderByDescending(x => x.CreatedTime).Skip((page - 1) * pageSize).Take(pageSize); + + List dataInfos = await query.ToListAsync(); + + CollectionResponse collectionResponse = new CollectionResponse + { + Total = total, + Collection = dataInfos, + Current = page, + }; + return APIResponseModel>.CreateSuccessResponseModel(collectionResponse); + } + catch (Exception ex) + { + return APIResponseModel>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + #region 删除指定的数据信息 + /// + /// 删除指定的数据信息 + /// + /// + /// + /// + public async Task>> DeleteDataInfo(string id, long userId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(userId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + DataInfo? dataInfo = await _context.DataInfo.FirstOrDefaultAsync(x => x.ID == id); + if (dataInfo == null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.IdDateNotFound); + } + + _context.DataInfo.Remove(dataInfo); + await _context.SaveChangesAsync(); + return APIResponseModel.CreateSuccessResponseModel("删除成功"); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + } +} diff --git a/LMS.service/Service/Other/MachineAuthorizationService.cs b/LMS.service/Service/Other/MachineAuthorizationService.cs new file mode 100644 index 0000000..636b3eb --- /dev/null +++ b/LMS.service/Service/Other/MachineAuthorizationService.cs @@ -0,0 +1,323 @@ +using AutoMapper; +using LMS.Common.Enums; +using LMS.Common.Extensions; +using LMS.DAO; +using LMS.DAO.UserDAO; +using LMS.Repository.DB; +using LMS.Repository.DTO; +using LMS.Repository.DTO.OtherDto; +using LMS.Repository.DTO.UserDto; +using LMS.Repository.Models.DB; +using LMS.Repository.Other; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using static LMS.Common.Enums.ResponseCodeEnum; + +namespace LMS.service.Service.Other +{ + public class MachineAuthorizationService(UserBasicDao userBasicDao, ApplicationDbContext dbContext, IMapper mapper) + { + private readonly UserBasicDao _userBasicDao = userBasicDao; + private readonly ApplicationDbContext _dbContext = dbContext; + private readonly IMapper _mapper = mapper; + + + #region 新增机器码/指定授权码 + /// + /// 新增机器码/指定授权码 + /// + /// + /// + /// + public async Task>> AddMachineAuthorization(AddMachineAuthorization addMachineAuthorization, long requestUserId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + // 判断类型是不是存在 + if (!Enum.IsDefined(typeof(MachineAuthorizationEnum), addMachineAuthorization.Type)) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "授权类型不存在"); + } + + // 判断相同的机器码和对应的类型的授权的是不是存在 + MachineAuthorization? machineAuthorizationExist = await _dbContext.MachineAuthorization.FirstOrDefaultAsync(x => x.MachineID == addMachineAuthorization.MachineID && x.Type == addMachineAuthorization.Type); + if (machineAuthorizationExist != null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "相同的机器码和对应的类型的授权已经存在"); + } + + // 业务逻辑 + MachineAuthorization machineAuthorization = new MachineAuthorization + { + ID = Guid.NewGuid().ToString(), + MachineID = addMachineAuthorization.MachineID, + AuthorizationCode = addMachineAuthorization.AuthorizationCode, + AuthorizedDate = addMachineAuthorization.AuthorizedDate, + ExpiryDate = addMachineAuthorization.ExpiryDate, + Type = addMachineAuthorization.Type, + CreatedUserID = requestUserId, + CreatedDate = BeijingTimeExtension.GetBeijingTime(), + UpdatedUserID = requestUserId, + UpdatedDate = BeijingTimeExtension.GetBeijingTime() + }; + + await _dbContext.MachineAuthorization.AddAsync(machineAuthorization); + await _dbContext.SaveChangesAsync(); + + return APIResponseModel.CreateSuccessResponseModel("新增成功"); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + #endregion + + #region 修改机器码/指定授权码 + + /// + /// 修改机器码/指定授权码 + /// + /// + /// + /// + /// + /// + public async Task>> ModifyMachineAuthorization(string id, AddMachineAuthorization addMachineAuthorization, long userId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(userId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + // 判断类型是不是存在 + if (!Enum.IsDefined(typeof(MachineAuthorizationEnum), addMachineAuthorization.Type)) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "授权类型不存在"); + } + + MachineAuthorization? machineAuthorization = await _dbContext.MachineAuthorization.FirstOrDefaultAsync(x => x.ID == id); + if (machineAuthorization == null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.IdDateNotFound); + } + + + // 排除对应的机器码和类型,排除自己 + MachineAuthorization? machineAuthorizationExist = await _dbContext.MachineAuthorization.FirstOrDefaultAsync(x => x.MachineID == addMachineAuthorization.MachineID && x.Type == addMachineAuthorization.Type && x.ID != id); + if (machineAuthorizationExist != null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "相同的机器码和对应的类型的授权已经存在"); + } + + // 开始修改 + machineAuthorization.MachineID = addMachineAuthorization.MachineID; + machineAuthorization.AuthorizationCode = addMachineAuthorization.AuthorizationCode; + machineAuthorization.Type = addMachineAuthorization.Type; + machineAuthorization.UpdatedUserID = userId; + machineAuthorization.UpdatedDate = BeijingTimeExtension.GetBeijingTime(); + machineAuthorization.AuthorizedDate = addMachineAuthorization.AuthorizedDate; + machineAuthorization.ExpiryDate = addMachineAuthorization.ExpiryDate; + + _dbContext.MachineAuthorization.Update(machineAuthorization); + await _dbContext.SaveChangesAsync(); + return APIResponseModel.CreateSuccessResponseModel("修改成功"); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + #region 获取指定的机器授权码 + /// + /// 获取指定的机器授权码 + /// + /// + /// + /// + /// + public async Task>> GetMachineAuthorization(string id, long userId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(userId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + MachineAuthorization? machineAuthorization = await _dbContext.MachineAuthorization.FirstOrDefaultAsync(x => x.ID == id); + if (machineAuthorization == null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.IdDateNotFound); + } + + User? createdUser = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == machineAuthorization.CreatedUserID); + User? updatedUser = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == machineAuthorization.UpdatedUserID); + UserBaseDto createdUserBasic = _mapper.Map(createdUser); + UserBaseDto updatedUserBasic = _mapper.Map(updatedUser); + if (!isSuperAdmin) + { + createdUserBasic.HideContactInfo(); + updatedUserBasic.HideContactInfo(); + } + MachineAuthorizationDto machineAuthorizationDto = new MachineAuthorizationDto + { + ID = machineAuthorization.ID, + MachineID = machineAuthorization.MachineID, + AuthorizationCode = machineAuthorization.AuthorizationCode, + AuthorizedDate = machineAuthorization.AuthorizedDate, + ExpiryDate = machineAuthorization.ExpiryDate, + Type = machineAuthorization.Type, + CreatedUser = createdUserBasic, + CreatedDate = machineAuthorization.CreatedDate, + UpdatedUser = updatedUserBasic, + UpdatedDate = machineAuthorization.UpdatedDate + }; + return APIResponseModel.CreateSuccessResponseModel(ResponseCode.Success, machineAuthorizationDto); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + #region 获取指定的机器码授权码列表 + + /// + /// 获取指定的机器码授权码列表 + /// + /// + /// + /// + /// + /// + /// + public async Task>>> QueryMachineAuthorizationCollection(int page, int pageSize, string? machineId, string? authorizationCode, int? type, long requestUserId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId); + if (!isSuperAdmin) + { + return APIResponseModel>.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + IQueryable query = _dbContext.MachineAuthorization; + if (!string.IsNullOrWhiteSpace(machineId)) + { + query = query.Where(x => x.MachineID.Contains(machineId)); + } + + if (!string.IsNullOrWhiteSpace(authorizationCode)) + { + query = query.Where(x => x.AuthorizationCode.Contains(authorizationCode)); + } + + if (type != null) + { + query = query.Where(x => x.Type == (MachineAuthorizationEnum)type); + } + + // 总数 + int total = await query.CountAsync(); + + // 降序/分页 + List machineAuthorizations = await query.OrderByDescending(x => x.CreatedDate).Skip((page - 1) * pageSize).Take(pageSize).ToListAsync(); + + List machineAuthorizationDtos = new List(); + for (int i = 0; i < machineAuthorizations.Count; i++) + { + var machineAuthorization = machineAuthorizations[i]; + User? createdUser = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == machineAuthorization.CreatedUserID); + User? updatedUser = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == machineAuthorization.UpdatedUserID); + UserBaseDto createdUserBasic = _mapper.Map(createdUser); + UserBaseDto updatedUserBasic = _mapper.Map(updatedUser); + if (!isSuperAdmin) + { + createdUserBasic.HideContactInfo(); + updatedUserBasic.HideContactInfo(); + } + MachineAuthorizationDto machineAuthorizationDto = new MachineAuthorizationDto + { + ID = machineAuthorization.ID, + MachineID = machineAuthorization.MachineID, + AuthorizationCode = machineAuthorization.AuthorizationCode, + AuthorizedDate = machineAuthorization.AuthorizedDate, + ExpiryDate = machineAuthorization.ExpiryDate, + Type = machineAuthorization.Type, + CreatedUser = createdUserBasic, + CreatedDate = machineAuthorization.CreatedDate, + UpdatedUser = updatedUserBasic, + UpdatedDate = machineAuthorization.UpdatedDate + }; + machineAuthorizationDtos.Add(machineAuthorizationDto); + } + + CollectionResponse collectionResponse = new CollectionResponse + { + Total = total, + Collection = machineAuthorizationDtos, + Current = page, + }; + return APIResponseModel>.CreateSuccessResponseModel(ResponseCode.Success, collectionResponse); + } + catch (Exception ex) + { + return APIResponseModel>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + #region 删除指定的机器码授权码 + /// + /// 删除指定的机器码授权码 + /// + /// + /// + /// + public async Task>> DeleteMachineAuthorization(string id, long requestUserId) + { + try + { + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + MachineAuthorization? machineAuthorization = await _dbContext.MachineAuthorization.FirstOrDefaultAsync(x => x.ID == id); + if (machineAuthorization == null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.IdDateNotFound); + } + + _dbContext.MachineAuthorization.Remove(machineAuthorization); + await _dbContext.SaveChangesAsync(); + return APIResponseModel.CreateSuccessResponseModel("删除成功"); + } + catch (Exception ex) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message); + } + } + + #endregion + + + } +} diff --git a/LMS.service/appsettings.json b/LMS.service/appsettings.json index d0c9600..a9fe443 100644 --- a/LMS.service/appsettings.json +++ b/LMS.service/appsettings.json @@ -26,6 +26,6 @@ ], "Enrich": [ "FromLogContext" ] }, - "Version": "1.0.6", + "Version": "1.0.7", "AllowedHosts": "*" } diff --git a/SQL/v1.0.7/DataInfo.sql b/SQL/v1.0.7/DataInfo.sql new file mode 100644 index 0000000..4ad9c07 --- /dev/null +++ b/SQL/v1.0.7/DataInfo.sql @@ -0,0 +1,32 @@ +/* + Navicat Premium Dump SQL + + Source Server : 亿速云(国内) + Source Server Type : MySQL + Source Server Version : 80018 (8.0.18) + Source Host : yisurds-66dc0b453c05d4.rds.ysydb1.com:14080 + Source Schema : LMS_TEST + + Target Server Type : MySQL + Target Server Version : 80018 (8.0.18) + File Encoding : 65001 + + Date: 28/03/2025 20:54:04 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for DataInfo +-- ---------------------------- +DROP TABLE IF EXISTS `DataInfo`; +CREATE TABLE `DataInfo` ( + `ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'ID', + `Type` int(11) NOT NULL COMMENT '数据的类型', + `DataString` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '数据的字符串', + `CreatedTime` datetime NOT NULL COMMENT '创建时间', + PRIMARY KEY (`ID`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '数据信息表' ROW_FORMAT = Dynamic; + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/SQL/v1.0.7/MachineAuthorization.sql b/SQL/v1.0.7/MachineAuthorization.sql new file mode 100644 index 0000000..5d8c051 --- /dev/null +++ b/SQL/v1.0.7/MachineAuthorization.sql @@ -0,0 +1,38 @@ +/* + Navicat Premium Dump SQL + + Source Server : 亿速云(国内) + Source Server Type : MySQL + Source Server Version : 80018 (8.0.18) + Source Host : yisurds-66dc0b453c05d4.rds.ysydb1.com:14080 + Source Schema : LMS_TEST + + Target Server Type : MySQL + Target Server Version : 80018 (8.0.18) + File Encoding : 65001 + + Date: 28/03/2025 20:53:54 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for MachineAuthorization +-- ---------------------------- +DROP TABLE IF EXISTS `MachineAuthorization`; +CREATE TABLE `MachineAuthorization` ( + `ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'ID', + `MachineID` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '机器码或者是授权码', + `AuthorizedDate` datetime NOT NULL COMMENT '授权日期', + `ExpiryDate` datetime NOT NULL COMMENT '过期日期', + `AuthorizationCode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '授权码', + `CreatedUserID` bigint(20) NOT NULL COMMENT '创建用户ID', + `CreatedDate` datetime NOT NULL COMMENT '创建时间', + `UpdatedUserID` bigint(20) NOT NULL COMMENT '更新用户ID', + `UpdatedDate` datetime NOT NULL COMMENT '更新时间', + `Type` int(10) NOT NULL COMMENT '授权类型', + PRIMARY KEY (`ID`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '机器授权表' ROW_FORMAT = Dynamic; + +SET FOREIGN_KEY_CHECKS = 1;