YD_Event.Admin/YD_Event.Application/Certificate/CertificateTemplateService.cs
2026-03-03 16:46:02 +08:00

108 lines
3.7 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 YD_Event.Application.Certificate.Dtos;
using YD_Event.Core.Entities;
using SqlSugar;
using Mapster;
namespace YD_Event.Application.Certificate;
/// <summary>
/// 证书模板管理服务
/// </summary>
public class CertificateTemplateService : IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<CertificateTemplate> _templateRepository;
private readonly ISqlSugarRepository<CertificateElement> _elementRepository;
private readonly IIdGenerator _idGenerator;
public CertificateTemplateService(
ISqlSugarRepository<CertificateTemplate> templateRepository,
ISqlSugarRepository<CertificateElement> elementRepository,
IIdGenerator idGenerator)
{
_templateRepository = templateRepository;
_elementRepository = elementRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 上传并创建模板
/// </summary>
[DisplayName("上传模板")]
public async Task<long> Upload(UploadTemplateInput input)
{
var entity = input.Adapt<CertificateTemplate>();
entity.Id = _idGenerator.NextId();
// 独立模板不再有 CertificateId,Version 概念可能需要调整,或者所有模板都是 V1
// 如果要保留版本控制,应该是基于某种“模板组”的概念,但这里简化为每次上传都是新模板
entity.Version = 1;
entity.IsActive = true; // 默认为激活,因为现在是单次引用
await _templateRepository.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 获取模板详情
/// </summary>
[DisplayName("获取模板详情")]
public async Task<CertificateTemplateOutput> Get(IdInput input)
{
var entity = await _templateRepository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("模板不存在");
return entity.Adapt<CertificateTemplateOutput>();
}
/// <summary>
/// 获取模板元素列表
/// </summary>
[DisplayName("获取元素列表")]
public async Task<List<CertificateElementOutput>> GetElementList(IdInput input)
{
var list = await _elementRepository.AsQueryable()
.Where(x => x.TemplateId == input.Id)
.OrderBy(x => x.Sort)
.ToListAsync();
return list.Adapt<List<CertificateElementOutput>>();
}
/// <summary>
/// 批量保存元素
/// </summary>
[DisplayName("批量保存元素")]
public async Task SaveElementList(SaveElementListInput input)
{
// 校验模板是否存在
var exists = await _templateRepository.IsAnyAsync(x => x.Id == input.TemplateId);
if (!exists) throw Oops.Bah("模板不存在");
await _elementRepository.AsSugarClient().Ado.BeginTranAsync();
try
{
// 删除旧元素
await _elementRepository.DeleteAsync(x => x.TemplateId == input.TemplateId);
// 插入新元素
if (input.Elements != null && input.Elements.Any())
{
var entities = input.Elements.Select(x =>
{
var entity = x.Adapt<CertificateElement>();
entity.Id = _idGenerator.NextId();
entity.TemplateId = input.TemplateId;
return entity;
}).ToList();
await _elementRepository.InsertRangeAsync(entities);
}
await _elementRepository.AsSugarClient().Ado.CommitTranAsync();
}
catch (Exception)
{
await _elementRepository.AsSugarClient().Ado.RollbackTranAsync();
throw;
}
}
}