167 lines
5.7 KiB
C#
Raw Normal View History

2026-04-10 22:39:32 +08:00
using Furion.FriendlyException;
using Microsoft.Extensions.Logging;
2026-03-02 21:09:03 +08:00
using YD_Event.Application.Certificate.Dtos;
using YD_Event.Core.Entities;
2026-03-03 16:46:02 +08:00
using SqlSugar;
using Mapster;
2025-12-27 12:53:54 +08:00
namespace YD_Event.Application.Certificate;
/// <summary>
/// 证书管理服务
2025-12-27 12:53:54 +08:00
/// </summary>
2026-03-03 16:46:02 +08:00
public class CertificateService : BaseService<YD_Event.Core.Entities.Certificate>, IDynamicApiController, ITransient
2025-12-27 12:53:54 +08:00
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Certificate> _repository;
2026-03-03 16:46:02 +08:00
private readonly ISqlSugarRepository<CertificateTemplate> _templateRepository;
2026-04-10 22:39:32 +08:00
private readonly ISqlSugarRepository<EventCertificate> _eventCertificateRepository;
private readonly IIdGenerator _idGenerator;
2026-04-10 22:39:32 +08:00
private readonly Microsoft.Extensions.Logging.ILogger<CertificateService> _logger;
2025-12-27 12:53:54 +08:00
2026-03-03 16:46:02 +08:00
public CertificateService(
ISqlSugarRepository<YD_Event.Core.Entities.Certificate> repository,
ISqlSugarRepository<CertificateTemplate> templateRepository,
2026-04-10 22:39:32 +08:00
ISqlSugarRepository<EventCertificate> eventCertificateRepository,
IIdGenerator idGenerator,
Microsoft.Extensions.Logging.ILogger<CertificateService> logger) : base(repository)
2025-12-27 12:53:54 +08:00
{
_repository = repository;
2026-03-03 16:46:02 +08:00
_templateRepository = templateRepository;
2026-04-10 22:39:32 +08:00
_eventCertificateRepository = eventCertificateRepository;
_idGenerator = idGenerator;
2026-04-10 22:39:32 +08:00
_logger = logger;
2025-12-27 12:53:54 +08:00
}
/// <summary>
/// 证书分页查询
2025-12-27 12:53:54 +08:00
/// </summary>
[DisplayName("证书分页查询")]
2026-03-03 16:46:02 +08:00
public async Task<SqlSugar.PageResult<CertificateListOutput>> Page(CertificateQueryInput input)
2025-12-27 12:53:54 +08:00
{
2026-03-03 16:46:02 +08:00
var query = _repository.AsQueryable()
2025-12-27 12:53:54 +08:00
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name))
2026-03-03 16:46:02 +08:00
.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)
2025-12-27 12:53:54 +08:00
.Select(x => new CertificateListOutput
{
Id = x.Id,
Name = x.Name,
2026-03-03 16:46:02 +08:00
Code = x.Code,
TemplateId = x.TemplateId,
Remark = x.Remark,
Status = x.Status,
2025-12-27 12:53:54 +08:00
Sort = x.Sort,
CreatedTime = x.CreatedTime
})
.ToPagedListAsync(input);
2025-12-27 12:53:54 +08:00
}
/// <summary>
/// 添加证书
2025-12-27 12:53:54 +08:00
/// </summary>
[DisplayName("添加证书")]
public async Task Add(AddCertificateInput input)
2025-12-27 12:53:54 +08:00
{
2026-03-03 16:46:02 +08:00
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);
2025-12-27 12:53:54 +08:00
}
/// <summary>
/// 更新证书
/// </summary>
[DisplayName("更新证书")]
public async Task Update(UpdateCertificateInput input)
2025-12-27 12:53:54 +08:00
{
2026-03-03 16:46:02 +08:00
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("证书不存在");
2025-12-27 12:53:54 +08:00
input.Adapt(entity);
await _repository.UpdateAsync(entity);
2025-12-27 12:53:54 +08:00
}
2026-02-07 17:28:46 +08:00
/// <summary>
/// 删除证书
/// </summary>
[DisplayName("删除证书")]
2026-04-10 22:39:32 +08:00
[UnitOfWork]
2026-03-03 16:46:02 +08:00
public async Task Delete(DeleteCertificateInput input)
2026-02-07 17:28:46 +08:00
{
2026-04-10 22:39:32 +08:00
_logger.LogInformation("开始删除证书:证书ID列表={Ids}", string.Join(",", input.Ids ?? new List<long>()));
if (input.Ids == null || !input.Ids.Any())
{
_logger.LogWarning("删除证书失败:未选择证书");
return;
}
// 执行删除
2026-03-03 16:46:02 +08:00
await _repository.DeleteAsync(x => input.Ids.Contains(x.Id));
2026-04-10 22:39:32 +08:00
_logger.LogInformation("删除证书成功:证书ID列表={Ids}", string.Join(",", input.Ids));
2026-02-07 17:28:46 +08:00
}
2026-04-15 15:14:52 +08:00
/// <summary>
/// 获取证书列表(用于选择)
/// </summary>
[HttpGet("getlist")]
[DisplayName("获取证书列表")]
public async Task<List<CertificateSimpleOutput>> GetList()
{
var certificates = await _repository.AsQueryable()
.Where(x => x.Status == AvailabilityStatus.Enable && !x.DeleteMark)
.OrderBy(x => x.Sort)
.OrderByDescending(x => x.CreatedTime)
.Select(x => new CertificateSimpleOutput
{
Id = x.Id,
Name = x.Name,
Code = x.Code,
TemplateId = x.TemplateId
})
.ToListAsync();
return certificates;
}
2025-12-27 12:53:54 +08:00
/// <summary>
/// 获取证书详情
2025-12-27 12:53:54 +08:00
/// </summary>
[DisplayName("获取证书详情")]
2026-03-03 16:46:02 +08:00
public async Task<CertificateDetailOutput> Detail(IdInput input)
2025-12-27 12:53:54 +08:00
{
2026-03-03 16:46:02 +08:00
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("证书不存在");
2026-03-02 21:09:03 +08:00
2026-03-03 16:46:02 +08:00
var result = entity.Adapt<CertificateDetailOutput>();
2026-03-02 21:09:03 +08:00
2026-03-03 16:46:02 +08:00
// 填充模板信息
var template = await _templateRepository.GetByIdAsync(entity.TemplateId);
if (template != null)
2026-03-02 21:09:03 +08:00
{
2026-03-03 16:46:02 +08:00
result.Template = template.Adapt<CertificateTemplateOutput>();
2026-03-02 21:09:03 +08:00
}
2026-03-03 16:46:02 +08:00
return result;
2026-03-02 21:09:03 +08:00
}
2025-12-27 12:53:54 +08:00
}