feat: 新增公告功能

This commit is contained in:
chenchun
2025-11-06 16:59:29 +08:00
parent 771ecd9d81
commit 690cabfd96
7 changed files with 197 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
/// <summary>
/// 公告缓存 DTO
/// </summary>
public class AnnouncementCacheDto
{
/// <summary>
/// 版本号
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// 公告日志列表
/// </summary>
public List<AnnouncementLogDto> Logs { get; set; } = new List<AnnouncementLogDto>();
}

View File

@@ -0,0 +1,22 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
/// <summary>
/// 公告日志 DTO
/// </summary>
public class AnnouncementLogDto
{
/// <summary>
/// 日期
/// </summary>
public string Date { get; set; } = string.Empty;
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; } = string.Empty;
/// <summary>
/// 内容列表
/// </summary>
public List<string> Content { get; set; } = new List<string>();
}

View File

@@ -0,0 +1,17 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
/// <summary>
/// 公告输出 DTO
/// </summary>
public class AnnouncementOutput
{
/// <summary>
/// 版本号
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// 公告日志列表
/// </summary>
public List<AnnouncementLogDto> Logs { get; set; } = new List<AnnouncementLogDto>();
}

View File

@@ -0,0 +1,15 @@
using Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
/// <summary>
/// 公告服务接口
/// </summary>
public interface IAnnouncementService
{
/// <summary>
/// 获取公告信息
/// </summary>
/// <returns>公告信息</returns>
Task<AnnouncementOutput> GetAsync();
}

View File

@@ -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;
/// <summary>
/// 公告服务
/// </summary>
public class AnnouncementService : ApplicationService, IAnnouncementService
{
private readonly ISqlSugarRepository<AnnouncementLogAggregateRoot> _announcementRepository;
private readonly IConfiguration _configuration;
private readonly IDistributedCache<AnnouncementCacheDto> _announcementCache;
private const string AnnouncementCacheKey = "AiHub:Announcement";
public AnnouncementService(
ISqlSugarRepository<AnnouncementLogAggregateRoot> announcementRepository,
IConfiguration configuration,
IDistributedCache<AnnouncementCacheDto> announcementCache)
{
_announcementRepository = announcementRepository;
_configuration = configuration;
_announcementCache = announcementCache;
}
/// <summary>
/// 获取公告信息
/// </summary>
public async Task<AnnouncementOutput> 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<AnnouncementLogDto>()
};
}
/// <summary>
/// 从数据库加载公告数据
/// </summary>
private async Task<AnnouncementCacheDto> 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
};
}
}

View File

@@ -0,0 +1,44 @@
using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;
namespace Yi.Framework.AiHub.Domain.Entities;
/// <summary>
/// 公告日志
/// </summary>
[SugarTable("Ai_AnnouncementLog")]
[SugarIndex($"index_{nameof(Date)}", nameof(Date), OrderByType.Desc)]
public class AnnouncementLogAggregateRoot : FullAuditedAggregateRoot<Guid>
{
public AnnouncementLogAggregateRoot()
{
}
public AnnouncementLogAggregateRoot(DateTime date, string title, List<string> content)
{
Date = date;
Title = title;
Content = content;
}
/// <summary>
/// 日期
/// </summary>
public DateTime Date { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; } = string.Empty;
/// <summary>
/// 内容列表JSON格式存储
/// </summary>
[SugarColumn(IsJson = true, IsNullable = false)]
public List<string> Content { get; set; } = new List<string>();
/// <summary>
/// 备注
/// </summary>
public string? Remark { get; set; }
}

View File

@@ -354,9 +354,7 @@ namespace Yi.Abp.Web
var app = context.GetApplicationBuilder();
app.UseRouting();
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<CardFlipTaskAggregateRoot>();
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<InvitationRecordAggregateRoot>();
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<InviteCodeAggregateRoot>();
//app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<AnnouncementLogAggregateRoot>();
//跨域
app.UseCors(DefaultCorsPolicyName);