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; /// /// 证书管理 /// [AllowAnonymous] public class CertificateService : IDynamicApiController { private readonly CustomConfigService _customConfigService; private readonly AuthManager _authManager; private readonly IHttpContextAccessor _httpContextAccessor; private readonly ISqlSugarRepository _certificateRepository; public CertificateService(CustomConfigService customConfigService, AuthManager authManager, IHttpContextAccessor httpContextAccessor, ISqlSugarRepository certificateRepository) { _customConfigService = customConfigService; _authManager = authManager; _httpContextAccessor = httpContextAccessor; _certificateRepository = certificateRepository; } /// /// 获取证书列表 /// /// 查询条件 /// 证书列表 [HttpGet] public async Task> 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); } /// /// 获取证书详情 /// /// 证书ID /// 证书详情 [HttpGet("{id}")] public async Task GetDetail(long id) { var certificate = await _certificateRepository.GetFirstAsync(x => x.Id == id && !x.DeleteMark); if (certificate == null) { throw Oops.Oh("证书不存在"); } return certificate.Adapt(); } /// /// 新增证书 /// /// 证书信息 /// 操作结果 [HttpPost] public async Task Add([FromBody] CreateCertificateInput input) { var certificate = input.Adapt(); certificate.CreatedUserId = _authManager.UserId; certificate.CreatedTime = DateTime.Now; certificate.UpdatedTime = DateTime.Now; certificate.DeleteMark = false; await _certificateRepository.InsertAsync(certificate); } /// /// 更新证书 /// /// 证书信息 /// 操作结果 [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); } /// /// 删除证书 /// /// 证书ID /// 操作结果 [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); } /// /// 扫码查看证书 /// /// 证书编号 /// PDF证书文件 [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); } }