TemplateService.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. namespace SHJX.Service.Control.Service
  2. {
  3. public class TemplateService : ITemplateService
  4. {
  5. private readonly IDataManager _dataManager;
  6. private readonly ReadConfigUtil _readConfig;
  7. public TemplateService(IDataManager dataManager, ReadConfigUtil readConfig)
  8. {
  9. _dataManager = dataManager;
  10. _readConfig = readConfig;
  11. }
  12. public List<T> GetData<T>() where T : class
  13. {
  14. return _dataManager.Query<T>().ToList();
  15. }
  16. public List<SampleDetail> GetSampleDetailsByState()
  17. {
  18. return _dataManager.Query<SampleDetail>().Where(it => it.TaskStatus.Equals(DetailState.New)).ToList();
  19. }
  20. public List<SampleDetail> GetSampleDetailsByTemplate()
  21. {
  22. if (string.IsNullOrWhiteSpace(_readConfig.TemplateFilePath))
  23. {
  24. UMessageBox.Error("模板路径为空,无法进行保存与导入!");
  25. return null;
  26. }
  27. if (!File.Exists(_readConfig.TemplateFilePath))
  28. {
  29. UMessageBox.Error("本地模板不存在!");
  30. return null;
  31. }
  32. return _readConfig.TemplateFilePath.ReadExcel().ConvertToList<SampleDetail>();
  33. }
  34. public void SaveTemplate(List<SampleDetail> details)
  35. {
  36. if (string.IsNullOrWhiteSpace(_readConfig.TemplateFilePath))
  37. {
  38. UMessageBox.Error($"导出模板失败,模板路径为空!");
  39. return;
  40. }
  41. DataTable dt = new("Template");
  42. dt.Columns.Add("节点名称", Type.GetType("System.String")!);
  43. dt.Columns.Add("详细描述", Type.GetType("System.String")!);
  44. dt.Columns.Add("取样体积", Type.GetType("System.Double")!);
  45. dt.Columns.Add("取样倍数", Type.GetType("System.Double")!);
  46. dt.Columns.Add("样品类型", Type.GetType("System.String")!);
  47. dt.Columns.Add("酸碱性", Type.GetType("System.String")!);
  48. dt.Columns.Add("是否标定", Type.GetType("System.Boolean")!);
  49. details.ForEach(item =>
  50. {
  51. dt.Rows.Add(item.NodeName, item.DetailInfo, item.SampleVolume, item.SampleMultiple, item.SampleType, item.AcidBaseProp, item.IsCalibration);
  52. });
  53. dt.ConvertToExcel(_readConfig.TemplateFilePath);
  54. }
  55. public bool UpdateSampleDetails(List<SampleDetail> details)
  56. {
  57. return _dataManager.Update(details) > 0;
  58. }
  59. }
  60. }