import 'package:fis_lib_report/converts/event_type.dart'; import 'package:fis_lib_report/report/element_type.dart'; import 'package:fis_lib_report/report/interfaces/block_element.dart'; import 'package:fis_lib_report/report/interfaces/element.dart'; import 'package:fis_lib_report/report_info/block_element_info_interface.dart'; import 'package:fis_lib_report/report_info/date_time_info.dart'; import 'package:fis_lib_report/report_info/element_info.dart'; import 'package:fis_lib_report/report_info/element_tag_names.dart'; import 'package:fis_lib_report/report_info/input_image_info.dart'; import 'package:fis_lib_report/report_info/input_image_list_info.dart'; import 'package:fis_lib_report/report_info/input_text_info.dart'; import 'package:fis_lib_report/report_info/multi_selected_info.dart'; import 'package:fis_lib_report/report_info/paragraph_info.dart'; import 'package:fis_lib_report/report_info/report_base_info.dart'; import 'package:fis_lib_report/report_info/report_event_args.dart'; import 'package:fis_lib_report/report_info/rt_table_info.dart'; import 'package:fis_lib_report/report_info/single_selected_info.dart'; class ReportInfo extends ReportBaseInfo { static ReportInfo? _reportInfo; ReportInfo._internal(); ///ReportInfo的key-value的数据集合 List jsonItems = []; ///选择词条按钮点击时触发 FEventHandler onDiagnosticTap = FEventHandler(); ///ReportInfo全局单例 static ReportInfo get instance { _reportInfo ??= ReportInfo._internal(); return _reportInfo!; } ///从Json中加载数据 void fromJson(List json) { for (var item in json) { final itemJson = item as Map; final key = itemJson['Key']; final value = itemJson['Value']; final elementInfo = getElementInfoById(key); if (elementInfo != null) { final elementType = elementInfo.elementType; if (elementType!.name == ElementType.inputText.name) { final inputInfo = elementInfo as InputTextInfo; inputInfo.text = value; } else if (elementType.name == ElementType.singleSelected.name) { final info = elementInfo as SingleSelectedInfo; info.selectedItem = value; } else if (elementType.name == ElementType.multiSelected.name) { final info = elementInfo as MulitiSelectedInfo; List list = []; final items = value as List; for (var i in items) { list.add(i.toString()); } info.selectedItems = list; } else if (elementType.name == ElementType.imageList.name) { final imagesInfo = getElementInfoById(key) as InputImageListInfo; final images = value as List; List targetImages = []; for (var i in images) { targetImages.add(i.toString()); } imagesInfo.selectedImages = targetImages; } else if (elementType.name == ElementType.dateTime.name) { final info = elementInfo as DateTimeInfo; info.text = value; } } } } ///插入报告描述&超声提示 void insertDiagnostic(String desc, String tips) { try { if (desc.isNotEmpty) { final descInfo = getElementInfoByTagName(TagNames.REPORTDES) as InputTextInfo; if (descInfo.text.isEmpty) { descInfo.text = desc; } else { descInfo.text = descInfo.text + '\r\n' + desc; } } if (tips.isNotEmpty) { final tipsInfo = getElementInfoByTagName(TagNames.REPORTSUMARY) as InputTextInfo; if (tipsInfo.text.isEmpty) { tipsInfo.text = tips; } else { tipsInfo.text = tipsInfo.text + '\r\n' + tips; } } } catch (e) { print(e); } } ///ReportInfo转为Json,用于报告存储至Server Map toJson() { jsonItems.clear(); final map = {}; try { final headInfos = []; for (var element in headers) { headInfos.add(_getElementInfo(element)); } map['HeadInfos'] = headInfos; final blockInfos = []; for (var block in blocks) { blockInfos.add(_getElementInfo(block)); } map['BlockInfos'] = blockInfos; final footerInfos = []; for (var block in footers) { footerInfos.add(_getElementInfo(block)); } map['FooterInfos'] = footerInfos; } catch (e) { print(e); } return map; } ///重新渲染UI&重构reportInfo void reload(String reporter, DateTime reportDate, String jsonStr, FEventHandler onSelect) { _reportInfo = null; onReload.emit( this, ReportEventArgs(reportDate, reporter, jsonStr, onSelect)); } ///获取二级ElementInfo(布局组件,非最基础的组件) IBlockElementInfo? getBlockElement(IBlockElement block) { final id = block.id; for (var element in headers) { if (element.id == id) { return element; } } for (var element in blocks) { if (element.id == id) { return element; } } for (var element in footers) { if (element.id == id) { return element; } } } ///获取报告模板中当前处于选中状态的图像输入框 void selectedInputImage(String imageUrl) { for (var element in headers) { if (_checkImageisSelected(element, imageUrl)) { return; } } for (var element in blocks) { if (_checkImageisSelected(element, imageUrl)) { return; } } for (var element in footers) { if (_checkImageisSelected(element, imageUrl)) { return; } } } ///跟据UI组件获取ReportInfo中的组件 ElementInfo? getElementInfo(IElement element) { try { for (var e in headers) { final elementInfo = _getBaseElementInfo(e, element); if (elementInfo != null) { return elementInfo; } } for (var e in blocks) { final elementInfo = _getBaseElementInfo(e, element); if (elementInfo != null) { return elementInfo; } } for (var e in footers) { final elementInfo = _getBaseElementInfo(e, element); if (elementInfo != null) { return elementInfo; } } } catch (e) { print(e); } return null; } void _blockElementInfoFromJson(Map block) { final type = block['ElementType']; final jsonType = ElementType.fromJson(type); final name = jsonType.name; if (name == ElementType.paragraph.name) { final paragraphInfo = block as Map; _paragraphInfoFromJson(paragraphInfo); } else if (name == ElementType.rtTable.name) { final tableInfo = block as Map; List jsonElements = tableInfo['Cells']; int index = 0; for (var map in jsonElements) { //TODO(Loki):此处可优化Json的结构,无需区分奇偶 if (index.isOdd) { final jsonType = map['ElementType']; final type = ElementType.fromJson(jsonType); if (type.name == ElementType.rtCell.name) { final cellMap = map as Map; final jsonBlocks = cellMap['Blocks'] as Map; _paragraphInfoFromJson(jsonBlocks); } } index++; } } else if (name == ElementType.imageList.name) { final imageList = block as Map; final id = imageList['Id']; final imagesInfo = getElementInfoById(id) as InputImageListInfo; final images = imageList['SelectedImages']; List targetImages = []; for (var i in images) { targetImages.add(i.toString()); } imagesInfo.selectedImages = targetImages; } } void _paragraphInfoFromJson(Map paragraphInfo) { List jsonElements = paragraphInfo['ElementInfos']; if (jsonElements.isEmpty) { return; } for (var map in jsonElements) { final jsonType = ElementType.fromJson(map['ElementType']); final id = map['Id']; if (jsonType.name == ElementType.inputText.name) { final info = getElementInfoById(id); if (info != null) { final inputInfo = info as InputTextInfo; inputInfo.text = map['Text']; } } else if (jsonType.name == ElementType.singleSelected.name) { final info = getElementInfoById(id); if (info != null) { final inputInfo = info as SingleSelectedInfo; inputInfo.selectedItem = map['SelectedItem']; } } else if (jsonType.name == ElementType.multiSelected.name) { final info = getElementInfoById(id); if (info != null) { final inputInfo = info as MulitiSelectedInfo; List list = []; final items = map['SelectedItems']; for (var i in items) { list.add(i.toString()); } inputInfo.selectedItems = list; } } } } bool _checkImageisSelected(IBlockElementInfo block, String url) { if (block is InputImageListInfo) { final inputImage = block; if (inputImage.isSelected ?? false) { inputImage.addImage(url); return true; } } else if (block is ParagraphInfo) { for (var el in block.elementInfos!) { if (el is InputImageInfo) { final image = el; if (image.isSelected!) { image.addImage(url); return true; } } } } return false; } ///获取基础组件信息 ElementInfo? _getBaseElementInfo(IBlockElementInfo block, IElement element) { ElementInfo? result; final type = block.elementType; if (type!.name == ElementType.paragraph.name) { final paragraph = block as ParagraphInfo; for (var el in paragraph.elementInfos!) { if (el.id == element.id) { result = el; break; } } } if (result == null) { if (type.name == ElementType.rtTable.name) { final table = block as RTTableInfo; final cells = table.cells!; for (var cell in cells.values) { for (var b in cell.blocks!) { final info = _getBaseElementInfo(b, element); if (info != null) { result = info; break; } } } } } return result; } Map _getElementInfo(IBlockElementInfo element) { final _type = element.elementType!; if (_type.name == ElementType.rtTable.name) { final table = element as RTTableInfo; return table.toJson(); } else if (_type.name == ElementType.paragraph.name) { final paragraph = element as ParagraphInfo; return paragraph.toJson(); } else { final inputImageList = element as InputImageListInfo; return inputImageList.toJson(); } } }