2026-03-03 16:46:02 +08:00

123 lines
4.2 KiB
C#

using YD_Event.Application.Certificate.Dtos;
using YD_Event.Core.Entities;
using SqlSugar;
using Mapster;
namespace YD_Event.Application.Certificate;
/// <summary>
/// 证书管理服务
/// </summary>
public class CertificateService : BaseService<YD_Event.Core.Entities.Certificate>, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Certificate> _repository;
private readonly ISqlSugarRepository<CertificateTemplate> _templateRepository;
private readonly IIdGenerator _idGenerator;
public CertificateService(
ISqlSugarRepository<YD_Event.Core.Entities.Certificate> repository,
ISqlSugarRepository<CertificateTemplate> templateRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_templateRepository = templateRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 证书分页查询
/// </summary>
[DisplayName("证书分页查询")]
public async Task<SqlSugar.PageResult<CertificateListOutput>> Page(CertificateQueryInput input)
{
var query = _repository.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name))
.WhereIF(!string.IsNullOrWhiteSpace(input.Code), x => x.Code.Contains(input.Code))
.WhereIF(input.Status.HasValue, x => x.Status == input.Status.Value);
return await query.OrderBy(x => x.Sort)
.OrderByDescending(x => x.CreatedTime)
.Select(x => new CertificateListOutput
{
Id = x.Id,
Name = x.Name,
Code = x.Code,
TemplateId = x.TemplateId,
Remark = x.Remark,
Status = x.Status,
Sort = x.Sort,
CreatedTime = x.CreatedTime
})
.ToPagedListAsync(input);
}
/// <summary>
/// 添加证书
/// </summary>
[DisplayName("添加证书")]
public async Task Add(AddCertificateInput input)
{
var isExist = await _repository.IsAnyAsync(x => x.Code == input.Code);
if (isExist) throw Oops.Oh("证书编码已存在");
// 校验模板是否存在
var isTemplateExist = await _templateRepository.IsAnyAsync(x => x.Id == input.TemplateId);
if (!isTemplateExist) throw Oops.Bah("无效的模板ID");
var entity = input.Adapt<YD_Event.Core.Entities.Certificate>();
entity.Id = _idGenerator.NextId();
await _repository.InsertAsync(entity);
}
/// <summary>
/// 更新证书
/// </summary>
[DisplayName("更新证书")]
public async Task Update(UpdateCertificateInput input)
{
var isExist = await _repository.IsAnyAsync(x => x.Code == input.Code && x.Id != input.Id);
if (isExist) throw Oops.Oh("证书编码已存在");
// 校验模板是否存在
var isTemplateExist = await _templateRepository.IsAnyAsync(x => x.Id == input.TemplateId);
if (!isTemplateExist) throw Oops.Bah("无效的模板ID");
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("证书不存在");
input.Adapt(entity);
await _repository.UpdateAsync(entity);
}
/// <summary>
/// 删除证书
/// </summary>
[DisplayName("删除证书")]
public async Task Delete(DeleteCertificateInput input)
{
if (input.Ids == null || !input.Ids.Any()) return;
await _repository.DeleteAsync(x => input.Ids.Contains(x.Id));
}
/// <summary>
/// 获取证书详情
/// </summary>
[DisplayName("获取证书详情")]
public async Task<CertificateDetailOutput> Detail(IdInput input)
{
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("证书不存在");
var result = entity.Adapt<CertificateDetailOutput>();
// 填充模板信息
var template = await _templateRepository.GetByIdAsync(entity.TemplateId);
if (template != null)
{
result.Template = template.Adapt<CertificateTemplateOutput>();
}
return result;
}
}