Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AnnouncementService.cs
chenchun b7756e2112 feat: 新增功能
- 概要
  - 重构并扩展公告相关模型、DTO、服务,新增公告类型、图片与时间字段,调整缓存与查询处理。
  - 新增枚举 AnnouncementTypeEnum。

- 主要改动(简要)
  - Yi.Framework.AiHub.Application.Contracts/Dtos/Announcement/AnnouncementLogDto.cs
    - 新增 ImageUrl、StartTime、EndTime、Type 字段,移除 Date 字段,Title 不再默认空串。
  - Yi.Framework.AiHub.Domain/Entities
    - 重命名 AnnouncementLogAggregateRoot -> AnnouncementAggregateRoot
    - 表名由 Ai_AnnouncementLog 改为 Ai_Announcement(SugarTable 标注)
    - 新增 ImageUrl、StartTime、EndTime、Type、Remark 字段(Remark 已存在,保持)
  - Yi.Framework.AiHub.Domain.Shared/Enums/AnnouncementTypeEnum.cs
    - 新增枚举文件(Activity=1, System=2)
  - Yi.Framework.AiHub.Application.Contracts/IServices/IAnnouncementService.cs
    - GetAsync 返回类型由 AnnouncementOutput 改为 List<AnnouncementLogDto>
  - Yi.Framework.AiHub.Application/Services/AnnouncementService.cs
    - 使用 Mapster 进行 DTO 映射
    - 查询按 StartTime 降序,返回 List<AnnouncementLogDto>,缓存结构简化
  - Yi.Abp.Web/YiAbpWebModule.cs
    - 改为初始化 AnnouncementAggregateRoot 的表(Ai_Announcement)
  - Yi.Ai.Vue3/types/import_meta.d.ts
    - 移除 VITE_BUILD_COMPRESS 环境变量声明

- 重要注意/兼容性提示
  - 接口变更:IAnnouncementService.GetAsync 返回类型已改变,调用方需同步更新(之前返回 AnnouncementOutput 的代码需调整)。
  - 数据库表变更:表名从 Ai_AnnouncementLog -> Ai_Announcement,若需保留历史数据,请在部署前做好数据迁移(重命名表或迁移数据到新表结构),或使用 CodeFirst 初始化新表(当前代码在启动时会 InitTables<AnnouncementAggregateRoot>())。
  - 新增 Mapster 适配(确保项目有 Mapster 依赖)。
  - 前端类型声明移除环境变量后,前端构建/运行脚本若依赖 VITE_BUILD_COMPRESS 需同步调整。
  - 若有缓存结构(AnnouncementCacheDto)或序列化相关约定变更,确认兼容性。

- 建议操作
  - 更新所有使用 IAnnouncementService 的代码(API 层/前端适配返回结构)。
  - 在非生产环境先执行数据迁移验证(保留旧表数据或写迁移脚本)。
  - 确认 Mapster 包已安装并编译通过。
  - 前端项目检查并同步 import_meta.d.ts 变更。
2025-11-10 15:03:02 +08:00

69 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Mapster;
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<AnnouncementAggregateRoot> _announcementRepository;
private readonly IConfiguration _configuration;
private readonly IDistributedCache<AnnouncementCacheDto> _announcementCache;
private const string AnnouncementCacheKey = "AiHub:Announcement";
public AnnouncementService(
ISqlSugarRepository<AnnouncementAggregateRoot> announcementRepository,
IConfiguration configuration,
IDistributedCache<AnnouncementCacheDto> announcementCache)
{
_announcementRepository = announcementRepository;
_configuration = configuration;
_announcementCache = announcementCache;
}
/// <summary>
/// 获取公告信息
/// </summary>
public async Task<List<AnnouncementLogDto>> GetAsync()
{
// 使用 GetOrAddAsync 从缓存获取或添加数据缓存1小时
var cacheData = await _announcementCache.GetOrAddAsync(
AnnouncementCacheKey,
async () => await LoadAnnouncementDataAsync(),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
}
);
return cacheData?.Logs ?? new List<AnnouncementLogDto>();
}
/// <summary>
/// 从数据库加载公告数据
/// </summary>
private async Task<AnnouncementCacheDto> LoadAnnouncementDataAsync()
{
// 查询所有公告日志,按日期降序排列
var logs = await _announcementRepository._DbQueryable
.OrderByDescending(x => x.StartTime)
.ToListAsync();
// 转换为 DTO
var logDtos = logs.Adapt<List<AnnouncementLogDto>>();
return new AnnouncementCacheDto
{
Logs = logDtos
};
}
}