report_info.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import 'package:fis_lib_report/converts/event_type.dart';
  2. import 'package:fis_lib_report/report/element_type.dart';
  3. import 'package:fis_lib_report/report/interfaces/block_element.dart';
  4. import 'package:fis_lib_report/report/interfaces/element.dart';
  5. import 'package:fis_lib_report/report_info/block_element_info_interface.dart';
  6. import 'package:fis_lib_report/report_info/date_time_info.dart';
  7. import 'package:fis_lib_report/report_info/element_info.dart';
  8. import 'package:fis_lib_report/report_info/element_tag_names.dart';
  9. import 'package:fis_lib_report/report_info/input_image_info.dart';
  10. import 'package:fis_lib_report/report_info/input_image_list_info.dart';
  11. import 'package:fis_lib_report/report_info/input_text_info.dart';
  12. import 'package:fis_lib_report/report_info/multi_selected_info.dart';
  13. import 'package:fis_lib_report/report_info/paragraph_info.dart';
  14. import 'package:fis_lib_report/report_info/report_base_info.dart';
  15. import 'package:fis_lib_report/report_info/report_event_args.dart';
  16. import 'package:fis_lib_report/report_info/rt_table_info.dart';
  17. import 'package:fis_lib_report/report_info/single_selected_info.dart';
  18. class ReportInfo extends ReportBaseInfo {
  19. static ReportInfo? _reportInfo;
  20. ReportInfo._internal();
  21. ///ReportInfo的key-value的数据集合
  22. List<dynamic> jsonItems = [];
  23. ///选择词条按钮点击时触发
  24. FEventHandler<String> onDiagnosticTap = FEventHandler();
  25. ///ReportInfo全局单例
  26. static ReportInfo get instance {
  27. _reportInfo ??= ReportInfo._internal();
  28. return _reportInfo!;
  29. }
  30. ///从Json中加载数据
  31. void fromJson(List<dynamic> json) {
  32. for (var item in json) {
  33. final itemJson = item as Map<String, dynamic>;
  34. final key = itemJson['Key'];
  35. final value = itemJson['Value'];
  36. final elementInfo = getElementInfoById(key);
  37. if (elementInfo != null) {
  38. final elementType = elementInfo.elementType;
  39. if (elementType!.name == ElementType.inputText.name) {
  40. final inputInfo = elementInfo as InputTextInfo;
  41. inputInfo.text = value;
  42. } else if (elementType.name == ElementType.singleSelected.name) {
  43. final info = elementInfo as SingleSelectedInfo;
  44. info.selectedItem = value;
  45. } else if (elementType.name == ElementType.multiSelected.name) {
  46. final info = elementInfo as MulitiSelectedInfo;
  47. List<String> list = [];
  48. final items = value as List<dynamic>;
  49. for (var i in items) {
  50. list.add(i.toString());
  51. }
  52. info.selectedItems = list;
  53. } else if (elementType.name == ElementType.imageList.name) {
  54. final imagesInfo = getElementInfoById(key) as InputImageListInfo;
  55. final images = value as List<dynamic>;
  56. List<String> targetImages = [];
  57. for (var i in images) {
  58. targetImages.add(i.toString());
  59. }
  60. imagesInfo.selectedImages = targetImages;
  61. } else if (elementType.name == ElementType.dateTime.name) {
  62. final info = elementInfo as DateTimeInfo;
  63. info.text = value;
  64. }
  65. }
  66. }
  67. }
  68. ///插入报告描述&超声提示
  69. void insertDiagnostic(String desc, String tips) {
  70. try {
  71. if (desc.isNotEmpty) {
  72. final descInfo =
  73. getElementInfoByTagName(TagNames.REPORTDES) as InputTextInfo;
  74. if (descInfo.text.isEmpty) {
  75. descInfo.text = desc;
  76. } else {
  77. descInfo.text = descInfo.text + '\r\n' + desc;
  78. }
  79. }
  80. if (tips.isNotEmpty) {
  81. final tipsInfo =
  82. getElementInfoByTagName(TagNames.REPORTSUMARY) as InputTextInfo;
  83. if (tipsInfo.text.isEmpty) {
  84. tipsInfo.text = tips;
  85. } else {
  86. tipsInfo.text = tipsInfo.text + '\r\n' + tips;
  87. }
  88. }
  89. } catch (e) {
  90. print(e);
  91. }
  92. }
  93. ///ReportInfo转为Json,用于报告存储至Server
  94. Map<String, dynamic> toJson() {
  95. jsonItems.clear();
  96. final map = <String, dynamic>{};
  97. try {
  98. final headInfos = [];
  99. for (var element in headers) {
  100. headInfos.add(_getElementInfo(element));
  101. }
  102. map['HeadInfos'] = headInfos;
  103. final blockInfos = [];
  104. for (var block in blocks) {
  105. blockInfos.add(_getElementInfo(block));
  106. }
  107. map['BlockInfos'] = blockInfos;
  108. final footerInfos = [];
  109. for (var block in footers) {
  110. footerInfos.add(_getElementInfo(block));
  111. }
  112. map['FooterInfos'] = footerInfos;
  113. } catch (e) {
  114. print(e);
  115. }
  116. return map;
  117. }
  118. ///重新渲染UI&重构reportInfo
  119. void reload(String reporter, DateTime reportDate, String jsonStr,
  120. FEventHandler<String> onSelect) {
  121. _reportInfo = null;
  122. onReload.emit(
  123. this, ReportEventArgs(reportDate, reporter, jsonStr, onSelect));
  124. }
  125. ///获取二级ElementInfo(布局组件,非最基础的组件)
  126. IBlockElementInfo? getBlockElement(IBlockElement block) {
  127. final id = block.id;
  128. for (var element in headers) {
  129. if (element.id == id) {
  130. return element;
  131. }
  132. }
  133. for (var element in blocks) {
  134. if (element.id == id) {
  135. return element;
  136. }
  137. }
  138. for (var element in footers) {
  139. if (element.id == id) {
  140. return element;
  141. }
  142. }
  143. }
  144. ///获取报告模板中当前处于选中状态的图像输入框
  145. void selectedInputImage(String imageUrl) {
  146. for (var element in headers) {
  147. if (_checkImageisSelected(element, imageUrl)) {
  148. return;
  149. }
  150. }
  151. for (var element in blocks) {
  152. if (_checkImageisSelected(element, imageUrl)) {
  153. return;
  154. }
  155. }
  156. for (var element in footers) {
  157. if (_checkImageisSelected(element, imageUrl)) {
  158. return;
  159. }
  160. }
  161. }
  162. ///跟据UI组件获取ReportInfo中的组件
  163. ElementInfo? getElementInfo(IElement element) {
  164. try {
  165. for (var e in headers) {
  166. final elementInfo = _getBaseElementInfo(e, element);
  167. if (elementInfo != null) {
  168. return elementInfo;
  169. }
  170. }
  171. for (var e in blocks) {
  172. final elementInfo = _getBaseElementInfo(e, element);
  173. if (elementInfo != null) {
  174. return elementInfo;
  175. }
  176. }
  177. for (var e in footers) {
  178. final elementInfo = _getBaseElementInfo(e, element);
  179. if (elementInfo != null) {
  180. return elementInfo;
  181. }
  182. }
  183. } catch (e) {
  184. print(e);
  185. }
  186. return null;
  187. }
  188. void _blockElementInfoFromJson(Map<String, dynamic> block) {
  189. final type = block['ElementType'];
  190. final jsonType = ElementType.fromJson(type);
  191. final name = jsonType.name;
  192. if (name == ElementType.paragraph.name) {
  193. final paragraphInfo = block as Map<String, dynamic>;
  194. _paragraphInfoFromJson(paragraphInfo);
  195. } else if (name == ElementType.rtTable.name) {
  196. final tableInfo = block as Map<String, dynamic>;
  197. List<dynamic> jsonElements = tableInfo['Cells'];
  198. int index = 0;
  199. for (var map in jsonElements) {
  200. //TODO(Loki):此处可优化Json的结构,无需区分奇偶
  201. if (index.isOdd) {
  202. final jsonType = map['ElementType'];
  203. final type = ElementType.fromJson(jsonType);
  204. if (type.name == ElementType.rtCell.name) {
  205. final cellMap = map as Map<String, dynamic>;
  206. final jsonBlocks = cellMap['Blocks'] as Map<String, dynamic>;
  207. _paragraphInfoFromJson(jsonBlocks);
  208. }
  209. }
  210. index++;
  211. }
  212. } else if (name == ElementType.imageList.name) {
  213. final imageList = block as Map<String, dynamic>;
  214. final id = imageList['Id'];
  215. final imagesInfo = getElementInfoById(id) as InputImageListInfo;
  216. final images = imageList['SelectedImages'];
  217. List<String> targetImages = [];
  218. for (var i in images) {
  219. targetImages.add(i.toString());
  220. }
  221. imagesInfo.selectedImages = targetImages;
  222. }
  223. }
  224. void _paragraphInfoFromJson(Map<String, dynamic> paragraphInfo) {
  225. List<dynamic> jsonElements = paragraphInfo['ElementInfos'];
  226. if (jsonElements.isEmpty) {
  227. return;
  228. }
  229. for (var map in jsonElements) {
  230. final jsonType = ElementType.fromJson(map['ElementType']);
  231. final id = map['Id'];
  232. if (jsonType.name == ElementType.inputText.name) {
  233. final info = getElementInfoById(id);
  234. if (info != null) {
  235. final inputInfo = info as InputTextInfo;
  236. inputInfo.text = map['Text'];
  237. }
  238. } else if (jsonType.name == ElementType.singleSelected.name) {
  239. final info = getElementInfoById(id);
  240. if (info != null) {
  241. final inputInfo = info as SingleSelectedInfo;
  242. inputInfo.selectedItem = map['SelectedItem'];
  243. }
  244. } else if (jsonType.name == ElementType.multiSelected.name) {
  245. final info = getElementInfoById(id);
  246. if (info != null) {
  247. final inputInfo = info as MulitiSelectedInfo;
  248. List<String> list = [];
  249. final items = map['SelectedItems'];
  250. for (var i in items) {
  251. list.add(i.toString());
  252. }
  253. inputInfo.selectedItems = list;
  254. }
  255. }
  256. }
  257. }
  258. bool _checkImageisSelected(IBlockElementInfo block, String url) {
  259. if (block is InputImageListInfo) {
  260. final inputImage = block;
  261. if (inputImage.isSelected ?? false) {
  262. inputImage.addImage(url);
  263. return true;
  264. }
  265. } else if (block is ParagraphInfo) {
  266. for (var el in block.elementInfos!) {
  267. if (el is InputImageInfo) {
  268. final image = el;
  269. if (image.isSelected!) {
  270. image.addImage(url);
  271. return true;
  272. }
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. ///获取基础组件信息
  279. ElementInfo? _getBaseElementInfo(IBlockElementInfo block, IElement element) {
  280. ElementInfo? result;
  281. final type = block.elementType;
  282. if (type!.name == ElementType.paragraph.name) {
  283. final paragraph = block as ParagraphInfo;
  284. for (var el in paragraph.elementInfos!) {
  285. if (el.id == element.id) {
  286. result = el;
  287. break;
  288. }
  289. }
  290. }
  291. if (result == null) {
  292. if (type.name == ElementType.rtTable.name) {
  293. final table = block as RTTableInfo;
  294. final cells = table.cells!;
  295. for (var cell in cells.values) {
  296. for (var b in cell.blocks!) {
  297. final info = _getBaseElementInfo(b, element);
  298. if (info != null) {
  299. result = info;
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. return result;
  307. }
  308. Map<String, dynamic> _getElementInfo(IBlockElementInfo element) {
  309. final _type = element.elementType!;
  310. if (_type.name == ElementType.rtTable.name) {
  311. final table = element as RTTableInfo;
  312. return table.toJson();
  313. } else if (_type.name == ElementType.paragraph.name) {
  314. final paragraph = element as ParagraphInfo;
  315. return paragraph.toJson();
  316. } else {
  317. final inputImageList = element as InputImageListInfo;
  318. return inputImageList.toJson();
  319. }
  320. }
  321. }