report_info.dart 7.6 KB

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