template.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:vitalapp/architecture/storage/text_storage.dart';
  4. import 'package:vitalapp/managers/interfaces/template.dart';
  5. import 'package:vitalapp/rpc.dart';
  6. import 'package:vitalapp/store/store.dart';
  7. import 'package:fis_common/logger/logger.dart';
  8. class TemplateManager implements ITemplateManager {
  9. /// 获取系统模板设置
  10. @override
  11. Future<SyncSettingsDTO?> syncSetting(SyncSettingRequest request) async {
  12. try {
  13. request.token = Store.user.token;
  14. final result = await rpc.systemSetting.syncSettingAsync(request);
  15. return result;
  16. } catch (e) {
  17. logger.e("TemplateManager query patient paged list error.", e);
  18. return null;
  19. }
  20. }
  21. @override
  22. Future<TemplateDTO?> getTemplateDetail(GetTemplateRequest request) async {
  23. try {
  24. request.token = Store.user.token;
  25. final result = await rpc.template.getTemplateDetailAsync(request);
  26. return result;
  27. } catch (e) {
  28. logger.e("TemplateManager query patient paged list error.", e);
  29. return null;
  30. }
  31. }
  32. @override
  33. Future<bool?> saveTemplate() async {
  34. try {
  35. Map<String, String> currentTemplateIndex = {};
  36. SyncSettingRequest syncSettingRequest = SyncSettingRequest();
  37. var syncSettingsDTO = await syncSetting(syncSettingRequest);
  38. for (TemplateDTO i in syncSettingsDTO?.templates ?? []) {
  39. TemplateDTO? templateDTO = await getTemplateDetail(
  40. GetTemplateRequest(
  41. code: i.code!,
  42. ),
  43. );
  44. TextStorage template = TextStorage(
  45. fileName: i.code!,
  46. directory: "template",
  47. );
  48. currentTemplateIndex[templateDTO!.key!] = templateDTO.code!;
  49. await template.save(jsonEncode(templateDTO.toJson()));
  50. }
  51. /// 模板索引
  52. TextStorage templateRelation = TextStorage(
  53. fileName: "templateRelation",
  54. directory: "template",
  55. );
  56. await templateRelation.save(jsonEncode(currentTemplateIndex));
  57. return true;
  58. } catch (e) {
  59. logger.e("TemplateManager saveTemplate error.", e);
  60. return null;
  61. }
  62. }
  63. @override
  64. Future<String?> readTemplate(String fileName) async {
  65. try {
  66. TextStorage t = TextStorage(fileName: fileName, directory: "template");
  67. final str = await t.read();
  68. return str;
  69. } catch (e) {
  70. logger.e("TemplateManager readTemplate error.", e);
  71. return null;
  72. }
  73. }
  74. }