176 lines
5.8 KiB
C#
176 lines
5.8 KiB
C#
|
|
using QuestPDF.Fluent;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using YD_Event.Application.Auth;
|
|||
|
|
using YD_Event.Application.Config;
|
|||
|
|
using YD_Event.Core.Certificate;
|
|||
|
|
using YD_Event.Application.Certificate.Dtos;
|
|||
|
|
|
|||
|
|
namespace YD_Event.Application.Certificate;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 证书管理
|
|||
|
|
/// </summary>
|
|||
|
|
[AllowAnonymous]
|
|||
|
|
public class CertificateService : IDynamicApiController
|
|||
|
|
{
|
|||
|
|
private readonly CustomConfigService _customConfigService;
|
|||
|
|
private readonly AuthManager _authManager;
|
|||
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|||
|
|
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Certificate> _certificateRepository;
|
|||
|
|
|
|||
|
|
public CertificateService(CustomConfigService customConfigService,
|
|||
|
|
AuthManager authManager,
|
|||
|
|
IHttpContextAccessor httpContextAccessor,
|
|||
|
|
ISqlSugarRepository<YD_Event.Core.Entities.Certificate> certificateRepository)
|
|||
|
|
{
|
|||
|
|
_customConfigService = customConfigService;
|
|||
|
|
_authManager = authManager;
|
|||
|
|
_httpContextAccessor = httpContextAccessor;
|
|||
|
|
_certificateRepository = certificateRepository;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取证书列表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input">查询条件</param>
|
|||
|
|
/// <returns>证书列表</returns>
|
|||
|
|
[HttpGet]
|
|||
|
|
public async Task<PageResult<CertificateListOutput>> GetList([FromQuery] CertificateQueryInput input)
|
|||
|
|
{
|
|||
|
|
var query = _certificateRepository.AsQueryable()
|
|||
|
|
.Where(x => !x.DeleteMark)
|
|||
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name))
|
|||
|
|
.WhereIF(input.Status.HasValue, x => x.Status == input.Status.Value)
|
|||
|
|
.OrderBy(x => x.Sort)
|
|||
|
|
.OrderBy(x => x.Id)
|
|||
|
|
.Select(x => new CertificateListOutput
|
|||
|
|
{
|
|||
|
|
Id = x.Id,
|
|||
|
|
Name = x.Name,
|
|||
|
|
Title = x.Title,
|
|||
|
|
IssueRule = x.IssueRule,
|
|||
|
|
QrCodeUrl = x.QrCodeUrl,
|
|||
|
|
Status = x.Status,
|
|||
|
|
Sort = x.Sort,
|
|||
|
|
CreatedTime = x.CreatedTime
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
return await query.ToPagedListAsync(input);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取证书详情
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">证书ID</param>
|
|||
|
|
/// <returns>证书详情</returns>
|
|||
|
|
[HttpGet("{id}")]
|
|||
|
|
public async Task<CertificateDetailOutput> GetDetail(long id)
|
|||
|
|
{
|
|||
|
|
var certificate = await _certificateRepository.GetFirstAsync(x => x.Id == id && !x.DeleteMark);
|
|||
|
|
if (certificate == null)
|
|||
|
|
{
|
|||
|
|
throw Oops.Oh("证书不存在");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return certificate.Adapt<CertificateDetailOutput>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新增证书
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input">证书信息</param>
|
|||
|
|
/// <returns>操作结果</returns>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task Add([FromBody] CreateCertificateInput input)
|
|||
|
|
{
|
|||
|
|
var certificate = input.Adapt<YD_Event.Core.Entities.Certificate>();
|
|||
|
|
certificate.CreatedUserId = _authManager.UserId;
|
|||
|
|
certificate.CreatedTime = DateTime.Now;
|
|||
|
|
certificate.UpdatedTime = DateTime.Now;
|
|||
|
|
certificate.DeleteMark = false;
|
|||
|
|
|
|||
|
|
await _certificateRepository.InsertAsync(certificate);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 更新证书
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input">证书信息</param>
|
|||
|
|
/// <returns>操作结果</returns>
|
|||
|
|
[HttpPut]
|
|||
|
|
public async Task Update([FromBody] UpdateCertificateInput input)
|
|||
|
|
{
|
|||
|
|
var certificate = await _certificateRepository.GetFirstAsync(x => x.Id == input.Id && !x.DeleteMark);
|
|||
|
|
if (certificate == null)
|
|||
|
|
{
|
|||
|
|
throw Oops.Oh("证书不存在");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
input.Adapt(certificate);
|
|||
|
|
certificate.UpdatedTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
await _certificateRepository.UpdateAsync(certificate);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除证书
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">证书ID</param>
|
|||
|
|
/// <returns>操作结果</returns>
|
|||
|
|
[HttpDelete("{id}")]
|
|||
|
|
public async Task Delete(long id)
|
|||
|
|
{
|
|||
|
|
var certificate = await _certificateRepository.GetFirstAsync(x => x.Id == id && !x.DeleteMark);
|
|||
|
|
if (certificate == null)
|
|||
|
|
{
|
|||
|
|
throw Oops.Oh("证书不存在");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 软删除
|
|||
|
|
await _certificateRepository.UpdateAsync(x => new YD_Event.Core.Entities.Certificate { DeleteMark = true }, x => x.Id == id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 扫码查看证书
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="certificateNo">证书编号</param>
|
|||
|
|
/// <returns>PDF证书文件</returns>
|
|||
|
|
[HttpGet("scan/{certificateNo}")]
|
|||
|
|
[NonUnify]
|
|||
|
|
[AllowAnonymous]
|
|||
|
|
public async Task Scan(string certificateNo)
|
|||
|
|
{
|
|||
|
|
var certificate = await _certificateRepository.GetFirstAsync(x => x.Name.Contains(certificateNo) && !x.DeleteMark);
|
|||
|
|
if (certificate == null)
|
|||
|
|
{
|
|||
|
|
throw Oops.Oh("证书不存在或已失效");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 构建证书数据模型
|
|||
|
|
var model = new CertificateModel
|
|||
|
|
{
|
|||
|
|
UserName = certificate.Name,
|
|||
|
|
CourseName = certificate.Title,
|
|||
|
|
CertificateNo = certificate.Name, // 使用证书名称作为证书编号
|
|||
|
|
IssueDate = certificate.CreatedTime
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 生成PDF文档
|
|||
|
|
var doc = new CertificateDocument(model);
|
|||
|
|
byte[] pdfBytes = doc.GeneratePdf();
|
|||
|
|
|
|||
|
|
// 设置HTTP响应头,触发文件下载
|
|||
|
|
var context = _httpContextAccessor.HttpContext;
|
|||
|
|
context!.Response.ContentType = "application/pdf";
|
|||
|
|
context.Response.Headers["Content-Disposition"] = $"attachment; filename={certificate.Name}_certificate.pdf";
|
|||
|
|
context.Response.Headers["Content-Length"] = pdfBytes.Length.ToString();
|
|||
|
|
|
|||
|
|
// 写入PDF字节流到响应
|
|||
|
|
await context.Response.Body.WriteAsync(pdfBytes);
|
|||
|
|
}
|
|||
|
|
}
|