import 'dart:convert'; import 'package:fis_common/index.dart'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:vitalapp/architecture/storage/file_storage_native_and_web.dart'; import 'package:vitalapp/architecture/storage/text_storage.dart'; import 'package:vitalapp/managers/interfaces/template.dart'; import 'package:vitalapp/rpc.dart'; import 'package:vitalapp/store/store.dart'; import 'package:fis_common/logger/logger.dart'; class TemplateManager implements ITemplateManager { /// 获取系统模板设置 @override Future syncSetting(SyncSettingRequest request) async { try { request.token = Store.user.token; final result = await rpc.vitalSystemSetting.syncSettingAsync(request); return result; } catch (e) { logger.e("TemplateManager query patient paged list error.", e); return null; } } @override Future getTemplateDetail(GetTemplateRequest request) async { try { request.token = Store.user.token; final result = await rpc.vitalTemplate.getTemplateDetailAsync(request); return result; } catch (e) { logger.e("TemplateManager query patient paged list error.", e); return null; } } @override Future saveTemplate() async { try { if (FPlatform.current == FPlatformEnum.android) { ///下载模版之前删除旧模版,保证每次下载的都是新模版 FileStorageNativeAndWeb templateDel = FileStorageNativeAndWeb.createInstance("template"); var isDeleteSuccess = await templateDel.deleteDirectory("template"); logger.i( "TemplateManager saveTemplate is delete template success:${isDeleteSuccess}."); } Map currentTemplateIndex = {}; SyncSettingRequest syncSettingRequest = SyncSettingRequest(); var syncSettingsDTO = await syncSetting(syncSettingRequest); logger.i( "TemplateManager saveTemplate template count:${syncSettingsDTO?.templates?.length}."); for (TemplateDTO i in syncSettingsDTO?.templates ?? []) { TemplateDTO? templateDTO = await getTemplateDetail( GetTemplateRequest( code: i.code!, ), ); TextStorage template = TextStorage( fileName: i.code!, directory: "template/${i.code!}", ); currentTemplateIndex[templateDTO!.key!] = templateDTO.code!; await template.save(jsonEncode(templateDTO.toJson())); logger.i( "TemplateManager saveTemplate template Code:${i.code} , key:${i.key}."); } ///Excel生化模板 if (syncSettingsDTO?.biochemicalTemplate?.code.isNotNullOrEmpty ?? false) { var code = syncSettingsDTO!.biochemicalTemplate!.code!; TextStorage template = TextStorage( fileName: code, directory: "template/$code", ); var key = syncSettingsDTO.biochemicalTemplate!.key!; currentTemplateIndex[key] = code; Store.user.biochemicalTemplateKey = key; await template.save(jsonEncode(syncSettingsDTO.biochemicalTemplate)); logger .i("TemplateManager saveTemplate template Code:$code , key:$key."); } ///Excel血常规模板 if (syncSettingsDTO?.bloodRoutineTemplate?.code.isNotNullOrEmpty ?? false) { var code = syncSettingsDTO!.bloodRoutineTemplate!.code!; TextStorage template = TextStorage( fileName: code, directory: "template/$code", ); var key = syncSettingsDTO.bloodRoutineTemplate!.key!; currentTemplateIndex[key] = code; Store.user.bloodRoutineTemplateKey = key; await template.save(jsonEncode(syncSettingsDTO.bloodRoutineTemplate)); logger .i("TemplateManager saveTemplate template Code:$code , key:$key."); } /// 模板索引 TextStorage templateRelation = TextStorage( fileName: "templateRelation", directory: "templateRelation", ); await templateRelation.deleteDirectory(); await templateRelation.save(jsonEncode(currentTemplateIndex)); return true; } catch (e) { logger.e("TemplateManager saveTemplate error.", e); return null; } } @override Future readTemplateRelation(String fileName) async { try { TextStorage t = TextStorage(fileName: fileName, directory: "templateRelation"); final str = await t.read(); return str; } catch (e) { logger.e("TemplateManager readTemplate error.", e); return null; } } @override Future getTemplateByKey(String key) async { var templates = await readTemplateRelation('templateRelation'); var templateRelation = jsonDecode(templates!); var templateCode = templateRelation[key] ?? ''; var template = await readTemplate(templateCode) ?? ''; String templateContent = TemplateDTO.fromJson(jsonDecode(template)).templateContent!; return templateContent; } @override Future readTemplate(String fileName) async { try { TextStorage t = TextStorage(fileName: fileName, directory: "template/$fileName"); final str = await t.read(); return str; } catch (e) { logger.e("TemplateManager readTemplate error.", e); return null; } } }