167 lines
5.7 KiB
C#
167 lines
5.7 KiB
C#
using Furion.FriendlyException;
|
||
using Microsoft.Extensions.Logging;
|
||
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 ISqlSugarRepository<EventCertificate> _eventCertificateRepository;
|
||
private readonly IIdGenerator _idGenerator;
|
||
private readonly Microsoft.Extensions.Logging.ILogger<CertificateService> _logger;
|
||
|
||
public CertificateService(
|
||
ISqlSugarRepository<YD_Event.Core.Entities.Certificate> repository,
|
||
ISqlSugarRepository<CertificateTemplate> templateRepository,
|
||
ISqlSugarRepository<EventCertificate> eventCertificateRepository,
|
||
IIdGenerator idGenerator,
|
||
Microsoft.Extensions.Logging.ILogger<CertificateService> logger) : base(repository)
|
||
{
|
||
_repository = repository;
|
||
_templateRepository = templateRepository;
|
||
_eventCertificateRepository = eventCertificateRepository;
|
||
_idGenerator = idGenerator;
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <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("删除证书")]
|
||
[UnitOfWork]
|
||
public async Task Delete(DeleteCertificateInput input)
|
||
{
|
||
_logger.LogInformation("开始删除证书:证书ID列表={Ids}", string.Join(",", input.Ids ?? new List<long>()));
|
||
|
||
if (input.Ids == null || !input.Ids.Any())
|
||
{
|
||
_logger.LogWarning("删除证书失败:未选择证书");
|
||
return;
|
||
}
|
||
|
||
|
||
|
||
// 执行删除 - 软删
|
||
await _repository.SoftDeleteByIdsAsync(input.Ids);
|
||
|
||
_logger.LogInformation("删除证书成功:证书ID列表={Ids}", string.Join(",", input.Ids));
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
}
|