template.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. if (FPlatform.current == FPlatformEnum.android) {
  38. ///下载模版之前删除旧模版,保证每次下载的都是新模版
  39. FileStorageNativeAndWeb templateDel =
  40. FileStorageNativeAndWeb.createInstance("template");
  41. var isDeleteSuccess = await templateDel.deleteDirectory("template");
  42. logger.i(
  43. "TemplateManager saveTemplate is delete template success:${isDeleteSuccess}.");
  44. }
  45. Map<String, String> currentTemplateIndex = {};
  46. SyncSettingRequest syncSettingRequest = SyncSettingRequest();
  47. var syncSettingsDTO = await syncSetting(syncSettingRequest);
  48. logger.i(
  49. "TemplateManager saveTemplate template count:${syncSettingsDTO?.templates?.length}.");
  50. for (TemplateDTO i in syncSettingsDTO?.templates ?? []) {
  51. TemplateDTO? templateDTO = await getTemplateDetail(
  52. GetTemplateRequest(
  53. code: i.code!,
  54. ),
  55. );
  56. TextStorage template = TextStorage(
  57. fileName: i.code!,
  58. directory: "template/${i.code!}",
  59. );
  60. currentTemplateIndex[templateDTO!.key!] = templateDTO.code!;
  61. await template.save(jsonEncode(templateDTO.toJson()));
  62. logger.i(
  63. "TemplateManager saveTemplate template Code:${i.code} , key:${i.key}.");
  64. }
  65. ///Excel生化模板
  66. if (syncSettingsDTO?.biochemicalTemplate?.code.isNotNullOrEmpty ??
  67. false) {
  68. var code = syncSettingsDTO!.biochemicalTemplate!.code!;
  69. TextStorage template = TextStorage(
  70. fileName: code,
  71. directory: "template/$code",
  72. );
  73. var key = syncSettingsDTO.biochemicalTemplate!.key!;
  74. currentTemplateIndex[key] = code;
  75. Store.user.biochemicalTemplateKey = key;
  76. await template.save(jsonEncode(syncSettingsDTO.biochemicalTemplate));
  77. logger
  78. .i("TemplateManager saveTemplate template Code:$code , key:$key.");
  79. }
  80. ///Excel血常规模板
  81. if (syncSettingsDTO?.bloodRoutineTemplate?.code.isNotNullOrEmpty ??
  82. false) {
  83. var code = syncSettingsDTO!.bloodRoutineTemplate!.code!;
  84. TextStorage template = TextStorage(
  85. fileName: code,
  86. directory: "template/$code",
  87. );
  88. var key = syncSettingsDTO.bloodRoutineTemplate!.key!;
  89. currentTemplateIndex[key] = code;
  90. Store.user.bloodRoutineTemplateKey = key;
  91. await template.save(jsonEncode(syncSettingsDTO.bloodRoutineTemplate));
  92. logger
  93. .i("TemplateManager saveTemplate template Code:$code , key:$key.");
  94. }
  95. /// 模板索引
  96. TextStorage templateRelation = TextStorage(
  97. fileName: "templateRelation",
  98. directory: "templateRelation",
  99. );
  100. await templateRelation.deleteDirectory();
  101. await templateRelation.save(jsonEncode(currentTemplateIndex));
  102. return true;
  103. } catch (e) {
  104. logger.e("TemplateManager saveTemplate error.", e);
  105. return null;
  106. }
  107. }
  108. @override
  109. Future<String?> readTemplateRelation(String fileName) async {
  110. try {
  111. TextStorage t =
  112. TextStorage(fileName: fileName, directory: "templateRelation");
  113. final str = await t.read();
  114. return str;
  115. } catch (e) {
  116. logger.e("TemplateManager readTemplate error.", e);
  117. return null;
  118. }
  119. }
  120. @override
  121. Future<String> getTemplateByKey(String key) async {
  122. var templates = await readTemplateRelation('templateRelation');
  123. var templateRelation = jsonDecode(templates!);
  124. var templateCode = templateRelation[key] ?? '';
  125. var template = await readTemplate(templateCode) ?? '';
  126. String templateContent =
  127. TemplateDTO.fromJson(jsonDecode(template)).templateContent!;
  128. return templateContent;
  129. }
  130. @override
  131. Future<String?> readTemplate(String fileName) async {
  132. try {
  133. TextStorage t =
  134. TextStorage(fileName: fileName, directory: "template/$fileName");
  135. final str = await t.read();
  136. return str;
  137. } catch (e) {
  138. logger.e("TemplateManager readTemplate error.", e);
  139. return null;
  140. }
  141. }
  142. }