123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- 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<dynamic> jsonItems = [];
- ///选择词条按钮点击时触发
- FEventHandler<String> onDiagnosticTap = FEventHandler();
- ///ReportInfo全局单例
- static ReportInfo get instance {
- _reportInfo ??= ReportInfo._internal();
- return _reportInfo!;
- }
- ///从Json中加载数据
- void fromJson(List<dynamic> json) {
- for (var item in json) {
- final itemJson = item as Map<String, dynamic>;
- 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<String> list = [];
- final items = value as List<dynamic>;
- 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<dynamic>;
- List<String> 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<String, dynamic> toJson() {
- jsonItems.clear();
- final map = <String, dynamic>{};
- 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<String> 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<String, dynamic> block) {
- final type = block['ElementType'];
- final jsonType = ElementType.fromJson(type);
- final name = jsonType.name;
- if (name == ElementType.paragraph.name) {
- final paragraphInfo = block as Map<String, dynamic>;
- _paragraphInfoFromJson(paragraphInfo);
- } else if (name == ElementType.rtTable.name) {
- final tableInfo = block as Map<String, dynamic>;
- List<dynamic> 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<String, dynamic>;
- final jsonBlocks = cellMap['Blocks'] as Map<String, dynamic>;
- _paragraphInfoFromJson(jsonBlocks);
- }
- }
- index++;
- }
- } else if (name == ElementType.imageList.name) {
- final imageList = block as Map<String, dynamic>;
- final id = imageList['Id'];
- final imagesInfo = getElementInfoById(id) as InputImageListInfo;
- final images = imageList['SelectedImages'];
- List<String> targetImages = [];
- for (var i in images) {
- targetImages.add(i.toString());
- }
- imagesInfo.selectedImages = targetImages;
- }
- }
- void _paragraphInfoFromJson(Map<String, dynamic> paragraphInfo) {
- List<dynamic> 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<String> 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<String, dynamic> _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();
- }
- }
- }
|