InspectionReportUtil.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using UniformMaterialManagementSystem.Entities;
  10. using UniformMaterialManagementSystem.Models;
  11. namespace UniformMaterialManagementSystem.Utils
  12. {
  13. public static class InspectionReportUtil
  14. {
  15. public static void ExportInspectionReportFile(InspectionReportModel inspectionReport)
  16. {
  17. const string templateFilePath = "Template\\军需物资质量监督检验报告模板.docx";
  18. SaveFileDialog saveFileDialog = new SaveFileDialog()
  19. {
  20. Title = "请选择检验报告保存位置",
  21. FileName = "被装材料检验报告-" + inspectionReport.ReportNo + "-" + WordUtil.GenerateFileSerialNumber(),
  22. Filter = "PDF文件格式 (*.pdf)|*.pdf",
  23. };
  24. if (saveFileDialog.ShowDialog() == false) return;
  25. var destinationFile = saveFileDialog.FileName;
  26. var dictionary = CreateDictionary(inspectionReport);
  27. var tempFile = $"Template\\temp_{WordUtil.GenerateFileSerialNumber()}.docx";
  28. WordUtil.GenerateWordByTemplate(templateFilePath, tempFile, dictionary);
  29. WordUtil.SaveWordToPdf(tempFile, destinationFile);
  30. //删除临时文件
  31. File.Delete(tempFile);
  32. MessageBox.Show("文档已生成!");
  33. }
  34. private static Dictionary<string, string> CreateDictionary(InspectionReportModel inspectionReport)
  35. {
  36. Dictionary<string, string> dictionary = new()
  37. {
  38. { "ReportNo", inspectionReport.ReportNo },
  39. { "Department", inspectionReport.Department },
  40. { "ReportTime", inspectionReport.ReportTime.ToString("yyyy-MM-dd") }
  41. };
  42. //检验类别
  43. switch (inspectionReport.InspectApply.InspCategory)
  44. {
  45. case "报样检验":
  46. dictionary.Add("FirstSelected", "\u25a0");//选中框
  47. break;
  48. case "首批检验":
  49. dictionary.Add("SecondSelected", "\u25a0");//选中框
  50. break;
  51. case "过程检验":
  52. dictionary.Add("ThirdSelected", "\u25a0");//选中框
  53. break;
  54. case "出厂检验":
  55. dictionary.Add("ForthSelected", "\u25a0");//选中框
  56. break;
  57. }
  58. dictionary.Add("ProductName", inspectionReport.InspectApply.ProductName);
  59. dictionary.Add("Company", inspectionReport.InspectApply.Company);
  60. dictionary.Add("InspQuantity", inspectionReport.InspectApply.InspQuantity + inspectionReport.InspectApply.Material.MeasureUnit);
  61. dictionary.Add("ProductDate", inspectionReport.InspectApply.StartProductDate.ToString("yyyy-MM-dd") + "至" + inspectionReport.InspectApply.EndProductDate.ToString("yyyy-MM-dd"));
  62. //采购机构、合同号
  63. StringBuilder purchaseCompanies = new StringBuilder();
  64. StringBuilder contractNos = new StringBuilder();
  65. foreach (var detail in inspectionReport.InspectApply.InspectApplyContractDetails)
  66. {
  67. purchaseCompanies.Append(detail.PurchaseCompanyShortName).Append(" ");
  68. contractNos.Append(detail.ContractNo).Append(";");
  69. }
  70. dictionary.Add("PurchaseCompany", purchaseCompanies.ToString().Substring(0, purchaseCompanies.Length - 1));
  71. dictionary.Add("ContractNumber", contractNos.ToString().Substring(0, contractNos.Length - 1));
  72. dictionary.Add("ReportBasis", inspectionReport.ReportBasis);
  73. dictionary.Add("ReportDesc", inspectionReport.ReportDesc);
  74. dictionary.Add("ConclusionDesc", inspectionReport.ConclusionDesc);
  75. //验收组
  76. for (int i = 0; i < 5; i++)
  77. {
  78. if (i < inspectionReport.InspectionReportDetails.Count)
  79. {
  80. var detail = inspectionReport.InspectionReportDetails[i];
  81. dictionary.Add("JobCategory" + i, detail.JobCategory);
  82. dictionary.Add("SupervisionUnit" + i, detail.SupervisionUnit);
  83. dictionary.Add("Inspector" + i, detail.Inspector);
  84. }
  85. else
  86. {
  87. dictionary.Add("JobCategory" + i, string.Empty);
  88. dictionary.Add("SupervisionUnit" + i, string.Empty);
  89. dictionary.Add("Inspector" + i, string.Empty);
  90. }
  91. }
  92. dictionary.Add("EditUser", inspectionReport.EditUser);
  93. return dictionary;
  94. }
  95. }
  96. }