From 690cabfd965249a7b79425b3f690fb0e65d44e9c Mon Sep 17 00:00:00 2001 From: chenchun Date: Thu, 6 Nov 2025 16:59:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=85=AC=E5=91=8A?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/Announcement/AnnouncementCacheDto.cs | 17 ++++ .../Dtos/Announcement/AnnouncementLogDto.cs | 22 +++++ .../Dtos/Announcement/AnnouncementOutput.cs | 17 ++++ .../IServices/IAnnouncementService.cs | 15 ++++ .../Services/AnnouncementService.cs | 81 +++++++++++++++++++ .../Entities/AnnouncementLogAggregateRoot.cs | 44 ++++++++++ Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 4 +- 7 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementCacheDto.cs create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementLogDto.cs create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementOutput.cs create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IAnnouncementService.cs create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AnnouncementService.cs create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/AnnouncementLogAggregateRoot.cs diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementCacheDto.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementCacheDto.cs new file mode 100644 index 00000000..5b8cd6c7 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementCacheDto.cs @@ -0,0 +1,17 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement; + +/// +/// 公告缓存 DTO +/// +public class AnnouncementCacheDto +{ + /// + /// 版本号 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 公告日志列表 + /// + public List Logs { get; set; } = new List(); +} diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementLogDto.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementLogDto.cs new file mode 100644 index 00000000..0a7ef430 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementLogDto.cs @@ -0,0 +1,22 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement; + +/// +/// 公告日志 DTO +/// +public class AnnouncementLogDto +{ + /// + /// 日期 + /// + public string Date { get; set; } = string.Empty; + + /// + /// 标题 + /// + public string Title { get; set; } = string.Empty; + + /// + /// 内容列表 + /// + public List Content { get; set; } = new List(); +} diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementOutput.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementOutput.cs new file mode 100644 index 00000000..02129c67 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementOutput.cs @@ -0,0 +1,17 @@ +namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement; + +/// +/// 公告输出 DTO +/// +public class AnnouncementOutput +{ + /// + /// 版本号 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 公告日志列表 + /// + public List Logs { get; set; } = new List(); +} diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IAnnouncementService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IAnnouncementService.cs new file mode 100644 index 00000000..77d4d29a --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IAnnouncementService.cs @@ -0,0 +1,15 @@ +using Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement; + +namespace Yi.Framework.AiHub.Application.Contracts.IServices; + +/// +/// 公告服务接口 +/// +public interface IAnnouncementService +{ + /// + /// 获取公告信息 + /// + /// 公告信息 + Task GetAsync(); +} diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AnnouncementService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AnnouncementService.cs new file mode 100644 index 00000000..8914cd25 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AnnouncementService.cs @@ -0,0 +1,81 @@ +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Configuration; +using Volo.Abp.Application.Services; +using Volo.Abp.Caching; +using Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement; +using Yi.Framework.AiHub.Application.Contracts.IServices; +using Yi.Framework.AiHub.Domain.Entities; +using Yi.Framework.SqlSugarCore.Abstractions; + +namespace Yi.Framework.AiHub.Application.Services; + +/// +/// 公告服务 +/// +public class AnnouncementService : ApplicationService, IAnnouncementService +{ + private readonly ISqlSugarRepository _announcementRepository; + private readonly IConfiguration _configuration; + private readonly IDistributedCache _announcementCache; + private const string AnnouncementCacheKey = "AiHub:Announcement"; + + public AnnouncementService( + ISqlSugarRepository announcementRepository, + IConfiguration configuration, + IDistributedCache announcementCache) + { + _announcementRepository = announcementRepository; + _configuration = configuration; + _announcementCache = announcementCache; + } + + /// + /// 获取公告信息 + /// + public async Task GetAsync() + { + // 使用 GetOrAddAsync 从缓存获取或添加数据,缓存1小时 + var cacheData = await _announcementCache.GetOrAddAsync( + AnnouncementCacheKey, + async () => await LoadAnnouncementDataAsync(), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1) + } + ); + + return new AnnouncementOutput + { + Version = cacheData?.Version ?? "v1.0.0", + Logs = cacheData?.Logs ?? new List() + }; + } + + /// + /// 从数据库加载公告数据 + /// + private async Task LoadAnnouncementDataAsync() + { + // 从配置文件读取版本号,如果没有配置则使用默认值 + var version = _configuration["AiHubVersion"] ?? "v1.0.0"; + + // 查询所有公告日志,按日期降序排列 + var logs = await _announcementRepository._DbQueryable + .OrderByDescending(x => x.Date) + .ToListAsync(); + + // 转换为 DTO + var logDtos = logs.Select(log => new AnnouncementLogDto + { + Date = log.Date.ToString("yyyy-MM-dd"), + Title = log.Title, + Content = log.Content + }).ToList(); + + return new AnnouncementCacheDto + { + Version = version, + Logs = logDtos + }; + } +} diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/AnnouncementLogAggregateRoot.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/AnnouncementLogAggregateRoot.cs new file mode 100644 index 00000000..ec890394 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/AnnouncementLogAggregateRoot.cs @@ -0,0 +1,44 @@ +using SqlSugar; +using Volo.Abp.Domain.Entities.Auditing; + +namespace Yi.Framework.AiHub.Domain.Entities; + +/// +/// 公告日志 +/// +[SugarTable("Ai_AnnouncementLog")] +[SugarIndex($"index_{nameof(Date)}", nameof(Date), OrderByType.Desc)] +public class AnnouncementLogAggregateRoot : FullAuditedAggregateRoot +{ + public AnnouncementLogAggregateRoot() + { + } + + public AnnouncementLogAggregateRoot(DateTime date, string title, List content) + { + Date = date; + Title = title; + Content = content; + } + + /// + /// 日期 + /// + public DateTime Date { get; set; } + + /// + /// 标题 + /// + public string Title { get; set; } = string.Empty; + + /// + /// 内容列表(JSON格式存储) + /// + [SugarColumn(IsJson = true, IsNullable = false)] + public List Content { get; set; } = new List(); + + /// + /// 备注 + /// + public string? Remark { get; set; } +} diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index 1ffa409e..1e701178 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -354,9 +354,7 @@ namespace Yi.Abp.Web var app = context.GetApplicationBuilder(); app.UseRouting(); - // app.ApplicationServices.GetRequiredService().SqlSugarClient.CodeFirst.InitTables(); - // app.ApplicationServices.GetRequiredService().SqlSugarClient.CodeFirst.InitTables(); - // app.ApplicationServices.GetRequiredService().SqlSugarClient.CodeFirst.InitTables(); + //app.ApplicationServices.GetRequiredService().SqlSugarClient.CodeFirst.InitTables(); //跨域 app.UseCors(DefaultCorsPolicyName);