2026-06-16 08:37:15 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.IService;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Entity;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Repository;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
namespace QYZH.InteractiveMagazine.Service;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// AIPrompt配置服务实现
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AiBasePromptService(
|
|
|
|
|
|
BaseRepository<AiBasePrompt> promptRepository,
|
|
|
|
|
|
ILogger<AiBasePromptService> logger) : BaseRepository<AiBasePrompt>, IAiBasePromptService
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建Prompt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AiBasePromptOutput> CreateAsync(AiBasePromptInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在创建Prompt配置,PromptKey: {PromptKey}", input.PromptKey);
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptKey))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("配置标识不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptName))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("配置名称不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptTemplate))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt模板内容不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查PromptKey是否已存在
|
|
|
|
|
|
var exists = await promptRepository.IsAnyAsync(p => p.PromptKey == input.PromptKey.Trim());
|
|
|
|
|
|
if (exists)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已存在", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var entity = new AiBasePrompt
|
|
|
|
|
|
{
|
|
|
|
|
|
PromptKey = input.PromptKey.Trim(),
|
|
|
|
|
|
PromptName = input.PromptName.Trim(),
|
|
|
|
|
|
PromptTemplate = input.PromptTemplate,
|
|
|
|
|
|
Description = input.Description,
|
|
|
|
|
|
Priority = input.Priority,
|
|
|
|
|
|
IsDefault = input.IsDefault,
|
|
|
|
|
|
CreatedBy = "System",
|
|
|
|
|
|
UpdatedBy = "System",
|
|
|
|
|
|
CreatedAt = DateTime.Now,
|
|
|
|
|
|
UpdatedAt = DateTime.Now,
|
|
|
|
|
|
IsDeleted = false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var result = await promptRepository.InsertAsync(entity);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("Prompt配置创建失败,PromptKey: {PromptKey}", input.PromptKey);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("创建Prompt配置失败", ResultCode.GLOBAL_ERROR);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Prompt配置创建成功,PromptKey: {PromptKey}, ID: {Id}", input.PromptKey, entity.Id);
|
|
|
|
|
|
|
|
|
|
|
|
return new AiBasePromptOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = entity.Id,
|
|
|
|
|
|
PromptKey = entity.PromptKey,
|
|
|
|
|
|
PromptName = entity.PromptName,
|
|
|
|
|
|
PromptTemplate = entity.PromptTemplate,
|
|
|
|
|
|
Description = entity.Description,
|
|
|
|
|
|
Priority = entity.Priority,
|
|
|
|
|
|
IsDefault = entity.IsDefault,
|
2026-06-16 16:54:02 +08:00
|
|
|
|
Status = entity.Status,
|
2026-06-16 08:37:15 +08:00
|
|
|
|
CreatedBy = entity.CreatedBy,
|
|
|
|
|
|
CreatedAt = entity.CreatedAt,
|
|
|
|
|
|
UpdatedBy = entity.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = entity.UpdatedAt
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新Prompt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AiBasePromptOutput> UpdateAsync(long id, AiBasePromptInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在更新Prompt配置,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
var entity = await promptRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("未找到要更新的Prompt配置,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptKey))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("配置标识不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptName))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("配置名称不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.PromptTemplate))
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt模板内容不能为空", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查PromptKey是否被其他记录占用
|
|
|
|
|
|
var exists = await promptRepository.IsAnyAsync(p => p.PromptKey == input.PromptKey.Trim() && p.Id != id);
|
|
|
|
|
|
if (exists)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已被其他配置使用", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entity.PromptKey = input.PromptKey.Trim();
|
|
|
|
|
|
entity.PromptName = input.PromptName.Trim();
|
|
|
|
|
|
entity.PromptTemplate = input.PromptTemplate;
|
|
|
|
|
|
entity.Description = input.Description;
|
|
|
|
|
|
entity.Priority = input.Priority;
|
|
|
|
|
|
entity.IsDefault = input.IsDefault;
|
|
|
|
|
|
entity.UpdatedBy = "System";
|
|
|
|
|
|
entity.UpdatedAt = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
|
|
var result = await promptRepository.UpdateAsync(entity);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("Prompt配置更新失败,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("更新Prompt配置失败", ResultCode.GLOBAL_ERROR);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Prompt配置更新成功,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
return new AiBasePromptOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = entity.Id,
|
|
|
|
|
|
PromptKey = entity.PromptKey,
|
|
|
|
|
|
PromptName = entity.PromptName,
|
|
|
|
|
|
PromptTemplate = entity.PromptTemplate,
|
|
|
|
|
|
Description = entity.Description,
|
|
|
|
|
|
Priority = entity.Priority,
|
|
|
|
|
|
IsDefault = entity.IsDefault,
|
2026-06-16 16:54:02 +08:00
|
|
|
|
Status = entity.Status,
|
2026-06-16 08:37:15 +08:00
|
|
|
|
CreatedBy = entity.CreatedBy,
|
|
|
|
|
|
CreatedAt = entity.CreatedAt,
|
|
|
|
|
|
UpdatedBy = entity.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = entity.UpdatedAt
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除Prompt配置(软删除)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task DeleteAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在删除Prompt配置,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
var entity = await promptRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("未找到要删除的Prompt配置,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await promptRepository.DeleteByIdAsync(id);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("Prompt配置删除失败,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("删除Prompt配置失败", ResultCode.GLOBAL_ERROR);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Prompt配置删除成功,ID: {Id}", id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取Prompt配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<AiBasePromptOutput> GetByIdAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在获取Prompt配置信息,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
var entity = await promptRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("未找到Prompt配置,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new AiBasePromptOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = entity.Id,
|
|
|
|
|
|
PromptKey = entity.PromptKey,
|
|
|
|
|
|
PromptName = entity.PromptName,
|
|
|
|
|
|
PromptTemplate = entity.PromptTemplate,
|
|
|
|
|
|
Description = entity.Description,
|
|
|
|
|
|
Priority = entity.Priority,
|
|
|
|
|
|
IsDefault = entity.IsDefault,
|
2026-06-16 16:54:02 +08:00
|
|
|
|
Status = entity.Status,
|
2026-06-16 08:37:15 +08:00
|
|
|
|
CreatedBy = entity.CreatedBy,
|
|
|
|
|
|
CreatedAt = entity.CreatedAt,
|
|
|
|
|
|
UpdatedBy = entity.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = entity.UpdatedAt
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分页查询Prompt配置列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<PageListModel<AiBasePromptOutput>> GetListAsync(AiBasePromptQueryInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在查询Prompt配置列表,页码: {PageIndex}, 每页条数: {PageSize}", input.PageIndex, input.PageSize);
|
|
|
|
|
|
|
|
|
|
|
|
if (input.PageIndex <= 0)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("页码必须大于0", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (input.PageSize <= 0 || input.PageSize > 100)
|
|
|
|
|
|
{
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("每页条数必须在1-100之间", ResultCode.BAD_REQUEST);
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RefAsync<int> totalNumber = 0;
|
|
|
|
|
|
var pageResult = await promptRepository.Queryable()
|
|
|
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.PromptKey), p => p.PromptKey == input.PromptKey)
|
|
|
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.PromptName), p => p.PromptName.Contains(input.PromptName))
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
|
|
|
|
|
.OrderByDescending(p => p.CreatedAt)
|
|
|
|
|
|
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
|
|
|
|
|
|
|
|
|
|
|
var result = pageResult.Select(p => new AiBasePromptOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
|
PromptKey = p.PromptKey,
|
|
|
|
|
|
PromptName = p.PromptName,
|
|
|
|
|
|
PromptTemplate = p.PromptTemplate,
|
|
|
|
|
|
Description = p.Description,
|
|
|
|
|
|
Priority = p.Priority,
|
|
|
|
|
|
IsDefault = p.IsDefault,
|
2026-06-16 16:54:02 +08:00
|
|
|
|
Status = p.Status,
|
2026-06-16 08:37:15 +08:00
|
|
|
|
CreatedBy = p.CreatedBy,
|
|
|
|
|
|
CreatedAt = p.CreatedAt,
|
|
|
|
|
|
UpdatedBy = p.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = p.UpdatedAt
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return new PageListModel<AiBasePromptOutput>(result, input.PageIndex, input.PageSize, totalNumber);
|
|
|
|
|
|
}
|
2026-06-16 16:54:02 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取全部Prompt配置(无分页)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<List<AiBasePromptOutput>> GetAllAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在查询全部Prompt配置");
|
|
|
|
|
|
|
|
|
|
|
|
var list = await promptRepository.Queryable()
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
|
|
|
|
|
.OrderByDescending(p => p.CreatedAt)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return list.Select(p => new AiBasePromptOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
|
PromptKey = p.PromptKey,
|
|
|
|
|
|
PromptName = p.PromptName,
|
|
|
|
|
|
PromptTemplate = p.PromptTemplate,
|
|
|
|
|
|
Description = p.Description,
|
|
|
|
|
|
Priority = p.Priority,
|
|
|
|
|
|
IsDefault = p.IsDefault,
|
|
|
|
|
|
Status = p.Status,
|
|
|
|
|
|
CreatedBy = p.CreatedBy,
|
|
|
|
|
|
CreatedAt = p.CreatedAt,
|
|
|
|
|
|
UpdatedBy = p.UpdatedBy,
|
|
|
|
|
|
UpdatedAt = p.UpdatedAt
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 切换Prompt启用/禁用状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task ToggleStatusAsync(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("正在切换Prompt配置状态,ID: {Id}", id);
|
|
|
|
|
|
|
|
|
|
|
|
var entity = await promptRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("未找到要切换状态的Prompt配置,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
|
2026-06-16 16:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entity.Status = entity.Status == 1 ? 0 : 1;
|
|
|
|
|
|
entity.UpdatedBy = "System";
|
|
|
|
|
|
entity.UpdatedAt = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
|
|
var result = await promptRepository.UpdateAsync(entity);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("Prompt配置状态切换失败,ID: {Id}", id);
|
2026-06-29 16:34:26 +08:00
|
|
|
|
throw new BusinessException("切换Prompt状态失败", ResultCode.GLOBAL_ERROR);
|
2026-06-16 16:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Prompt配置状态切换成功,ID: {Id}, Status: {Status}", id, entity.Status);
|
|
|
|
|
|
}
|
2026-06-16 08:37:15 +08:00
|
|
|
|
}
|