导出证书
This commit is contained in:
parent
c29382ce0b
commit
fee32191cb
@ -1555,42 +1555,68 @@ public class EventScoreService : BaseService<EventResult>, ITransient
|
|||||||
/// 导出证书
|
/// 导出证书
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DisplayName("导出证书")]
|
[DisplayName("导出证书")]
|
||||||
[HttpGet]
|
public async Task<IActionResult> ExportCertificate(ScoreQueryInput input)
|
||||||
public async Task<IActionResult> ExportCertificate([FromQuery] ScoreQueryInput input)
|
|
||||||
{
|
{
|
||||||
var list = await _certificateRepository.AsQueryable()
|
var certificates = await _certificateRepository.AsQueryable()
|
||||||
.LeftJoin<EventResult>((c, r) => c.ResultId == r.Id)
|
.Where(c => c.EventId == input.EventId && !c.DeleteMark)
|
||||||
.LeftJoin<EventRegistration>((c, r, reg) => c.RegistrationId == reg.Id)
|
.WhereIF(input.ProjectId.HasValue, c => c.ProjectId == input.ProjectId.Value)
|
||||||
.LeftJoin<EventProject>((c, r, reg, ep) => c.ProjectId == ep.Id)
|
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), c => c.GroupName == input.GroupName)
|
||||||
.LeftJoin<YD_Event.Core.Entities.Project>((c, r, reg, ep, p) => ep.ProjectId == p.Id)
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), c => c.ParticipantName.Contains(input.Name))
|
||||||
.Where((c, r, reg, ep, p) => c.EventId == input.EventId)
|
.Where(c => c.CertificateStatus >= 1 && !string.IsNullOrEmpty(c.CertificateUrl))
|
||||||
.WhereIF(input.ProjectId.HasValue, (c, r, reg, ep, p) => c.ProjectId == input.ProjectId)
|
.Select(c => new
|
||||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), (c, r, reg, ep, p) => c.GroupName == input.GroupName)
|
{
|
||||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), (c, r, reg, ep, p) => c.ParticipantName.Contains(input.Name))
|
c.Id,
|
||||||
.Select((c, r, reg, ep, p) => new CertificateExportDto
|
c.CertificateNo,
|
||||||
{
|
c.ParticipantName,
|
||||||
CertificateNo = c.CertificateNo,
|
c.GroupName,
|
||||||
ParticipantName = c.ParticipantName,
|
c.CertificateUrl,
|
||||||
ProjectName = p.Name,
|
c.CertificateType
|
||||||
GroupName = c.GroupName,
|
|
||||||
UnitName = reg.TeamName,
|
|
||||||
Score = r.Score,
|
|
||||||
Rank = r.Rank,
|
|
||||||
CertificateType = c.CertificateType == 1 ? "冠军证书" :
|
|
||||||
c.CertificateType == 2 ? "亚军证书" :
|
|
||||||
c.CertificateType == 3 ? "季军证书" :
|
|
||||||
c.CertificateType == 4 ? "优胜证书" : "参与证书",
|
|
||||||
IssuedTime = c.IssuedTime
|
|
||||||
})
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
using var stream = new MemoryStream();
|
if (!certificates.Any())
|
||||||
await stream.SaveAsAsync(list);
|
|
||||||
var bytes = stream.ToArray();
|
|
||||||
|
|
||||||
return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
||||||
{
|
{
|
||||||
FileDownloadName = $"证书列表_{DateTime.Now:yyyyMMddHHmmss}.xlsx"
|
throw Oops.Bah("暂无已生成的证书可导出");
|
||||||
|
}
|
||||||
|
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
using (var archive = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true))
|
||||||
|
{
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
int successCount = 0;
|
||||||
|
int failCount = 0;
|
||||||
|
|
||||||
|
foreach (var cert in certificates)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pdfBytes = await httpClient.GetByteArrayAsync(cert.CertificateUrl);
|
||||||
|
var fileName = $"{cert.ParticipantName}_{cert.GroupName}_{cert.CertificateNo}.pdf";
|
||||||
|
fileName = string.Join("_", fileName.Split(Path.GetInvalidFileNameChars()));
|
||||||
|
|
||||||
|
var entry = archive.CreateEntry(fileName, System.IO.Compression.CompressionLevel.Optimal);
|
||||||
|
using var entryStream = entry.Open();
|
||||||
|
await entryStream.WriteAsync(pdfBytes);
|
||||||
|
successCount++;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "导出证书失败: CertificateId={Id}, CertificateNo={CertificateNo}",
|
||||||
|
cert.Id, cert.CertificateNo);
|
||||||
|
failCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (successCount == 0)
|
||||||
|
{
|
||||||
|
throw Oops.Bah("所有证书导出失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
return new FileContentResult(memoryStream.ToArray(), "application/zip")
|
||||||
|
{
|
||||||
|
FileDownloadName = $"证书导出_{DateTime.Now:yyyyMMddHHmmss}.zip"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user