PrintTemplateAppService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using System.Threading.Tasks;
  7. using Volo.Abp.Application.Dtos;
  8. using Volo.Abp.Application.Services;
  9. using Volo.Abp.Domain.Repositories;
  10. using Business.PrintTemplateManagement.Dto;
  11. using Business.Models;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Business.Permissions;
  14. using Volo.Abp;
  15. using Magicodes.ExporterAndImporter.Pdf;
  16. using WkHtmlToPdfDotNet;
  17. using Microsoft.AspNetCore.Mvc;
  18. using System.IO;
  19. namespace Business.PrintTemplateManagement
  20. {
  21. [Authorize(BusinessPermissions.PrintTemplate.Default)]
  22. public class PrintTemplateAppService : ApplicationService, IPrintTemplateAppService
  23. {
  24. private const string FormName = "PrintTemplate";
  25. private IRepository<PrintTemplate, Guid> _repository;
  26. public PrintTemplateAppService(
  27. IRepository<PrintTemplate, Guid> repository
  28. )
  29. {
  30. _repository = repository;
  31. }
  32. #region 增删改查基础方法
  33. public async Task<PrintTemplateDto> Get(Guid id)
  34. {
  35. var data = await _repository.GetAsync(id);
  36. var dto = ObjectMapper.Map<PrintTemplate, PrintTemplateDto>(data);
  37. return dto;
  38. }
  39. public async Task<PagedResultDto<PrintTemplateDto>> GetAll(GetPrintTemplateInputDto input)
  40. {
  41. var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.Name.Contains(input.Filter));
  42. var totalCount = await query.CountAsync();
  43. var items = await query.OrderBy(input.Sorting ?? "Id")
  44. .Skip(input.SkipCount)
  45. .Take(input.MaxResultCount)
  46. .ToListAsync();
  47. var dto = ObjectMapper.Map<List<PrintTemplate>, List<PrintTemplateDto>>(items);
  48. return new PagedResultDto<PrintTemplateDto>(totalCount, dto);
  49. }
  50. public async Task<PrintTemplateDto> DataPost(CreateOrUpdatePrintTemplateDto input)
  51. {
  52. PrintTemplate result = null;
  53. if (!input.Id.HasValue)
  54. {
  55. input.Id = GuidGenerator.Create();
  56. result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdatePrintTemplateDto, PrintTemplate>(input));
  57. }
  58. else
  59. {
  60. var data = await _repository.GetAsync(input.Id.Value);
  61. result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
  62. }
  63. return ObjectMapper.Map<PrintTemplate, PrintTemplateDto>(result);
  64. }
  65. public async Task Delete(List<Guid> ids)
  66. {
  67. foreach (var item in ids)
  68. {
  69. await _repository.DeleteAsync(item);
  70. }
  71. }
  72. public async Task<dynamic> CreatePdf(Guid id)
  73. {
  74. var temp = await _repository.GetAsync(id);
  75. var exporter = new PdfExporter();
  76. var pdfAtt = new PdfExporterAttribute();
  77. pdfAtt.Orientation = Orientation.Landscape;
  78. pdfAtt.PaperKind = PaperKind.A4;
  79. var result = await exporter.ExportBytesByTemplate(temp, pdfAtt, temp.Content);
  80. return new FileStreamResult(new MemoryStream(result), "application/octet-stream") { FileDownloadName = $"{temp.Name}.PDF" };
  81. }
  82. #endregion
  83. }
  84. }