report_info.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/element_info.dart';
  7. import 'package:fis_lib_report/report_info/input_image_info.dart';
  8. import 'package:fis_lib_report/report_info/input_image_list_info.dart';
  9. import 'package:fis_lib_report/report_info/input_text_info.dart';
  10. import 'package:fis_lib_report/report_info/multi_selected_info.dart';
  11. import 'package:fis_lib_report/report_info/paragraph_info.dart';
  12. import 'package:fis_lib_report/report_info/report_base_info.dart';
  13. import 'package:fis_lib_report/report_info/report_event_args.dart';
  14. import 'package:fis_lib_report/report_info/rt_table_info.dart';
  15. import 'package:fis_lib_report/report_info/single_selected_info.dart';
  16. class ReportInfo extends ReportBaseInfo {
  17. static ReportInfo? _reportInfo;
  18. ReportInfo._internal();
  19. ///ReportInfo全局单例
  20. static ReportInfo get instance {
  21. _reportInfo ??= ReportInfo._internal();
  22. return _reportInfo!;
  23. }
  24. ///从Json中加载数据
  25. void fromJson(Map<String, dynamic> json) {
  26. List<dynamic> jsonBlocks = json['BlockInfos'];
  27. if (jsonBlocks.isNotEmpty) {
  28. for (var block in jsonBlocks) {
  29. final type = block['ElementType'];
  30. final jsonType = ElementType.fromJson(type);
  31. final name = jsonType.name;
  32. if (name == ElementType.paragraph.name) {
  33. final paragraphInfo = block as Map<String, dynamic>;
  34. _paragraphInfoFromJson(paragraphInfo);
  35. } else if (name == ElementType.rtTable.name) {
  36. final tableInfo = block as Map<String, dynamic>;
  37. List<dynamic> jsonElements = tableInfo['Cells'];
  38. int index = 0;
  39. for (var map in jsonElements) {
  40. //TODO(Loki):此处可优化Json的结构,无需区分奇偶
  41. if (index.isOdd) {
  42. final jsonType = map['ElementType'];
  43. final type = ElementType.fromJson(jsonType);
  44. if (type.name == ElementType.rtCell.name) {
  45. final cellMap = map as Map<String, dynamic>;
  46. final jsonBlocks = cellMap['Blocks'] as Map<String, dynamic>;
  47. _paragraphInfoFromJson(jsonBlocks);
  48. }
  49. }
  50. index++;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. ///ReportInfo转为Json,用于报告存储至Server
  57. Map<String, dynamic> toJson() {
  58. final map = <String, dynamic>{};
  59. try {
  60. final headInfos = [];
  61. for (var element in headers) {
  62. headInfos.add(_getElementInfo(element));
  63. }
  64. map['HeadInfos'] = headInfos;
  65. final blockInfos = [];
  66. for (var block in blocks) {
  67. blockInfos.add(_getElementInfo(block));
  68. }
  69. map['BlockInfos'] = blockInfos;
  70. final footerInfos = [];
  71. for (var block in footers) {
  72. footerInfos.add(_getElementInfo(block));
  73. }
  74. map['FooterInfos'] = footerInfos;
  75. } catch (e) {
  76. print(e);
  77. }
  78. return map;
  79. }
  80. ///重新渲染UI&重构reportInfo
  81. void reload(String reporter, DateTime reportDate, String jsonStr,
  82. FEventHandler<String> onSelect) {
  83. _reportInfo = null;
  84. onReload.emit(
  85. this, ReportEventArgs(reportDate, reporter, jsonStr, onSelect));
  86. }
  87. ///获取二级ElementInfo(布局组件,非最基础的组件)
  88. IBlockElementInfo? getBlockElement(IBlockElement block) {
  89. final id = block.id;
  90. for (var element in headers) {
  91. if (element.id == id) {
  92. return element;
  93. }
  94. }
  95. for (var element in blocks) {
  96. if (element.id == id) {
  97. return element;
  98. }
  99. }
  100. for (var element in footers) {
  101. if (element.id == id) {
  102. return element;
  103. }
  104. }
  105. }
  106. ///获取报告模板中当前处于选中状态的图像输入框
  107. void selectedInputImage(String imageUrl) {
  108. for (var element in headers) {
  109. if (_checkImageisSelected(element, imageUrl)) {
  110. return;
  111. }
  112. }
  113. for (var element in blocks) {
  114. if (_checkImageisSelected(element, imageUrl)) {
  115. return;
  116. }
  117. }
  118. for (var element in footers) {
  119. if (_checkImageisSelected(element, imageUrl)) {
  120. return;
  121. }
  122. }
  123. }
  124. ///跟据UI组件获取ReportInfo中的组件
  125. ElementInfo? getElementInfo(IElement element) {
  126. try {
  127. for (var e in headers) {
  128. final elementInfo = _getBaseElementInfo(e, element);
  129. if (elementInfo != null) {
  130. return elementInfo;
  131. }
  132. }
  133. for (var e in blocks) {
  134. final elementInfo = _getBaseElementInfo(e, element);
  135. if (elementInfo != null) {
  136. return elementInfo;
  137. }
  138. }
  139. for (var e in footers) {
  140. final elementInfo = _getBaseElementInfo(e, element);
  141. if (elementInfo != null) {
  142. return elementInfo;
  143. }
  144. }
  145. } catch (e) {
  146. print(e);
  147. }
  148. return null;
  149. }
  150. void _paragraphInfoFromJson(Map<String, dynamic> paragraphInfo) {
  151. List<dynamic> jsonElements = paragraphInfo['ElementInfos'];
  152. if (jsonElements.isEmpty) {
  153. return;
  154. }
  155. for (var map in jsonElements) {
  156. final jsonType = ElementType.fromJson(map['ElementType']);
  157. final id = map['Id'];
  158. if (jsonType.name == ElementType.inputText.name) {
  159. final info = getElementInfoById(id);
  160. if (info != null) {
  161. final inputInfo = info as InputTextInfo;
  162. inputInfo.text = map['Text'];
  163. }
  164. } else if (jsonType.name == ElementType.singleSelected.name) {
  165. final info = getElementInfoById(id);
  166. if (info != null) {
  167. final inputInfo = info as SingleSelectedInfo;
  168. inputInfo.selectedItem = map['SelectedItem'];
  169. }
  170. } else if (jsonType.name == ElementType.multiSelected.name) {
  171. final info = getElementInfoById(id);
  172. if (info != null) {
  173. final inputInfo = info as MulitiSelectedInfo;
  174. List<String> list = [];
  175. final items = map['SelectedItems'];
  176. for (var i in items) {
  177. list.add(i.toString());
  178. }
  179. inputInfo.selectedItems = list;
  180. }
  181. }
  182. }
  183. }
  184. bool _checkImageisSelected(IBlockElementInfo block, String url) {
  185. if (block is InputImageListInfo) {
  186. final inputImage = block;
  187. if (inputImage.isSelected ?? false) {
  188. inputImage.addImage(url);
  189. return true;
  190. }
  191. } else if (block is ParagraphInfo) {
  192. for (var el in block.elementInfos!) {
  193. if (el is InputImageInfo) {
  194. final image = el;
  195. if (image.isSelected!) {
  196. image.addImage(url);
  197. return true;
  198. }
  199. }
  200. }
  201. }
  202. return false;
  203. }
  204. ///获取基础组件信息
  205. ElementInfo? _getBaseElementInfo(IBlockElementInfo block, IElement element) {
  206. ElementInfo? result;
  207. final type = block.elementType;
  208. if (type!.name == ElementType.paragraph.name) {
  209. final paragraph = block as ParagraphInfo;
  210. for (var el in paragraph.elementInfos!) {
  211. if (el.id == element.id) {
  212. result = el;
  213. break;
  214. }
  215. }
  216. }
  217. if (result == null) {
  218. if (type.name == ElementType.rtTable.name) {
  219. final table = block as RTTableInfo;
  220. final cells = table.cells!;
  221. for (var cell in cells.values) {
  222. for (var b in cell.blocks!) {
  223. final info = _getBaseElementInfo(b, element);
  224. if (info != null) {
  225. result = info;
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return result;
  233. }
  234. Map<String, dynamic> _getElementInfo(IBlockElementInfo element) {
  235. final _type = element.elementType!;
  236. if (_type.name == ElementType.rtTable.name) {
  237. final table = element as RTTableInfo;
  238. return table.toJson();
  239. } else if (_type.name == ElementType.paragraph.name) {
  240. final paragraph = element as ParagraphInfo;
  241. return paragraph.toJson();
  242. } else {
  243. final inputImageList = element as InputImageListInfo;
  244. return inputImageList.toJson();
  245. }
  246. }
  247. }