template.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'dart:convert';
  2. import 'package:fis_common/index.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:vitalapp/architecture/storage/file_storage_native_and_web.dart';
  5. import 'package:vitalapp/architecture/storage/text_storage.dart';
  6. import 'package:vitalapp/managers/interfaces/template.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'package:vitalapp/store/store.dart';
  9. import 'package:fis_common/logger/logger.dart';
  10. class TemplateManager implements ITemplateManager {
  11. /// 获取系统模板设置
  12. @override
  13. Future<SyncSettingsDTO?> syncSetting(SyncSettingRequest request) async {
  14. try {
  15. request.token = Store.user.token;
  16. final result = await rpc.vitalSystemSetting.syncSettingAsync(request);
  17. return result;
  18. } catch (e) {
  19. logger.e("TemplateManager query patient paged list error.", e);
  20. return null;
  21. }
  22. }
  23. @override
  24. Future<TemplateDTO?> getTemplateDetail(GetTemplateRequest request) async {
  25. try {
  26. request.token = Store.user.token;
  27. final result = await rpc.vitalTemplate.getTemplateDetailAsync(request);
  28. return result;
  29. } catch (e) {
  30. logger.e("TemplateManager query patient paged list error.", e);
  31. return null;
  32. }
  33. }
  34. @override
  35. Future<bool?> saveTemplate() async {
  36. try {
  37. Map<String, String> currentTemplateIndex = {};
  38. SyncSettingRequest syncSettingRequest = SyncSettingRequest();
  39. var syncSettingsDTO = await syncSetting(syncSettingRequest);
  40. logger.i(
  41. "TemplateManager saveTemplate template count:${syncSettingsDTO?.templates?.length}.");
  42. ///Excel生化模板
  43. if (syncSettingsDTO?.biochemicalTemplate?.code.isNotNullOrEmpty ??
  44. false) {
  45. var code = syncSettingsDTO!.biochemicalTemplate!.code!;
  46. TextStorage template = TextStorage(
  47. fileName: code,
  48. directory: "template/$code",
  49. );
  50. var key = syncSettingsDTO.biochemicalTemplate!.key!;
  51. currentTemplateIndex[key] = code;
  52. Store.user.biochemicalTemplateKey = key;
  53. await template.save(jsonEncode(syncSettingsDTO.biochemicalTemplate));
  54. logger
  55. .i("TemplateManager saveTemplate template Code:$code , key:$key.");
  56. }
  57. ///Excel血常规模板
  58. if (syncSettingsDTO?.bloodRoutineTemplate?.code.isNotNullOrEmpty ??
  59. false) {
  60. var code = syncSettingsDTO!.bloodRoutineTemplate!.code!;
  61. TextStorage template = TextStorage(
  62. fileName: code,
  63. directory: "template/$code",
  64. );
  65. var key = syncSettingsDTO.bloodRoutineTemplate!.key!;
  66. currentTemplateIndex[key] = code;
  67. Store.user.bloodRoutineTemplateKey = key;
  68. await template.save(jsonEncode(syncSettingsDTO.bloodRoutineTemplate));
  69. logger
  70. .i("TemplateManager saveTemplate template Code:$code , key:$key.");
  71. }
  72. /// 模板索引
  73. TextStorage templateRelation = TextStorage(
  74. fileName: "templateRelation",
  75. directory: "templateRelation",
  76. );
  77. await templateRelation.deleteDirectory();
  78. await templateRelation.save(jsonEncode(currentTemplateIndex));
  79. return true;
  80. } catch (e) {
  81. logger.e("TemplateManager saveTemplate error.", e);
  82. return null;
  83. }
  84. }
  85. @override
  86. Future<String?> readTemplateRelation(String fileName) async {
  87. try {
  88. TextStorage t =
  89. TextStorage(fileName: fileName, directory: "templateRelation");
  90. final str = await t.read();
  91. return str;
  92. } catch (e) {
  93. logger.e("TemplateManager readTemplate error.", e);
  94. return null;
  95. }
  96. }
  97. @override
  98. Future<String> getTemplateByKey(String key) async {
  99. var templates = await readTemplateRelation('templateRelation');
  100. var templateRelation = jsonDecode(templates!);
  101. var templateCode = templateRelation[key] ?? '';
  102. var template = await readTemplate(templateCode) ?? '';
  103. String templateContent =
  104. TemplateDTO.fromJson(jsonDecode(template)).templateContent!;
  105. return templateContent;
  106. }
  107. @override
  108. Future<String?> readTemplate(String fileName) async {
  109. try {
  110. TextStorage t =
  111. TextStorage(fileName: fileName, directory: "template/$fileName");
  112. final str = await t.read();
  113. return str;
  114. } catch (e) {
  115. logger.e("TemplateManager readTemplate error.", e);
  116. return null;
  117. }
  118. }
  119. }