123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 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<SyncSettingsDTO?> 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<TemplateDTO?> 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<bool?> saveTemplate() async {
- try {
- Map<String, String> currentTemplateIndex = {};
- SyncSettingRequest syncSettingRequest = SyncSettingRequest();
- var syncSettingsDTO = await syncSetting(syncSettingRequest);
- logger.i(
- "TemplateManager saveTemplate template count:${syncSettingsDTO?.templates?.length}.");
- ///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<String?> 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<String> 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<String?> 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;
- }
- }
- }
|