report_template_document.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import 'package:fis_lib_report/report/element_tag.dart';
  2. import 'package:fis_lib_report/report/element_type.dart';
  3. import 'package:fis_lib_report/report/inputImageList.dart';
  4. import 'package:fis_lib_report/report/interfaces/block_element.dart';
  5. import 'package:fis_lib_report/report/interfaces/report_template_document.dart';
  6. import 'package:fis_lib_report/report/paragraph.dart';
  7. import 'package:fis_lib_report/report/rt_page_size.dart';
  8. import 'package:fis_lib_report/report/rt_table.dart';
  9. import 'package:fis_lib_report/report/rt_thickness.dart';
  10. import 'package:uuid/uuid.dart';
  11. class ReportTemplateDocument implements IReportTemplateDocument {
  12. Map<String, Map<String, String>> _customizeTranslation = {};
  13. Set<String> _customizeComments = {};
  14. Set<String> _customizeReportComments = {};
  15. Set<String> _customizeCalculations = {};
  16. List<IBlockElement> _blocks = [];
  17. List<IBlockElement> _headers = [];
  18. List<IBlockElement> _footers = [];
  19. String? _fontName;
  20. Map<String, Map<String, String>> get customizeTranslation =>
  21. _customizeTranslation;
  22. @override
  23. set customizeTranslation(Map<String, Map<String, String>>? v) {
  24. _customizeTranslation = v ?? {};
  25. }
  26. Set<String> get customizeComments => _customizeComments;
  27. @override
  28. set customizeReportComments(Set<String>? v) {
  29. _customizeReportComments = v ?? {};
  30. }
  31. Set<String> get customizeReportComments => _customizeReportComments;
  32. @override
  33. set customizeComments(Set<String>? v) {
  34. _customizeComments = v ?? {};
  35. }
  36. Set<String> get customizeCalculations => _customizeCalculations;
  37. @override
  38. set customizeCalculations(Set<String>? v) {
  39. _customizeCalculations = v ?? {};
  40. }
  41. /// Gets the author of the report template.
  42. @override
  43. String? author;
  44. @override
  45. double? baseFontSize;
  46. /// Gets all the report template blocks.
  47. @override
  48. List<IBlockElement>? blocks = [];
  49. /// get or set the distance between the footer and the bottom of the page
  50. @override
  51. double? footerDistance;
  52. /// Gets or sets the height of footer.
  53. @override
  54. double? footerHeight;
  55. /// get or set the distance between the header and the top of the page
  56. @override
  57. double? headerDistance;
  58. /// Gets or sets the height of header.
  59. @override
  60. double? headerHeight;
  61. @override
  62. bool? invertColor;
  63. /// Gets or sets the value to indicate whether the report template is created by user.
  64. @override
  65. bool? isCustom;
  66. /// Gets the report template name.
  67. @override
  68. String? name;
  69. /// Gets or sets the page padding. Default is 20mm.
  70. @override
  71. RTThickness? pagePadding;
  72. /// Gets or sets the page size.
  73. @override
  74. RTPageSize? pageSize;
  75. /// Gets the custom input element tags of the report template.
  76. @override
  77. Map<String, ElementTag>? tags;
  78. /// Gets or sets the update time.
  79. @override
  80. DateTime? updateTime;
  81. ///version
  82. @override
  83. String? version;
  84. /// Gets the id.
  85. @override
  86. late String id;
  87. /// Gets the report template footer.
  88. @override
  89. List<IBlockElement>? footer = [];
  90. /// Gets the report template header.
  91. @override
  92. List<IBlockElement>? header = [];
  93. @override
  94. ReportTemplateDocument() {
  95. id = Uuid().v1();
  96. pageSize = RTPageSize.a4;
  97. pagePadding = new RTThickness.uniform(56.83);
  98. headerHeight = null;
  99. footerHeight = null;
  100. baseFontSize = 9;
  101. headerDistance = 34;
  102. footerDistance = 34;
  103. }
  104. //TODO(Loki):目前仅能支持常规模板解析,其他模板需添加缺少的类
  105. ReportTemplateDocument.fromJson(Map<String, dynamic> json) {
  106. try {
  107. isCustom = json['IsCustom'];
  108. id = json['Id'];
  109. final jsonData = json['UpdateTime'];
  110. updateTime = DateTime.parse(jsonData!).toLocal();
  111. author = json['Author'];
  112. name = json['Name'];
  113. headerHeight = json['HeaderHeight'];
  114. footerHeight = json['FooterHeight'];
  115. headerDistance = json['HeaderDistance'];
  116. footerDistance = json['FooterDistance'];
  117. baseFontSize = json['BaseFontSize'];
  118. invertColor = json['InvertColor'];
  119. version = json['Version'];
  120. pagePadding = RTThickness.fromJson(json['PagePadding'] ?? {});
  121. pageSize = RTPageSize.fromJson(json['PageSize'] ?? {});
  122. Map jsonTags = json['Tags'];
  123. if (jsonTags.length > 0) {
  124. //TODO(Loki):tags convert
  125. } else {
  126. tags = {};
  127. }
  128. Map<dynamic, dynamic> jsonLanguages = json['CustomizeTranslation'] ?? {};
  129. jsonLanguages.forEach((key, value) {
  130. if (value is Map<dynamic, dynamic>) {
  131. if (value.isNotEmpty) {
  132. customizeTranslation[key] = value as Map<String, String>;
  133. } else {
  134. customizeTranslation[key] = {};
  135. }
  136. } else {
  137. customizeTranslation[key] = {};
  138. }
  139. });
  140. _initComments(json);
  141. _initBlocks(json);
  142. } catch (e) {
  143. print(e);
  144. }
  145. }
  146. void _initBlocks(Map<String, dynamic> json) {
  147. List<dynamic> jsonBlocks = json['Blocks'];
  148. if (jsonBlocks.isNotEmpty) {
  149. for (var block in jsonBlocks) {
  150. final type = block['ElementType'];
  151. final jsonType = ElementType.fromJson(type);
  152. final name = jsonType.name;
  153. if (name == ElementType.rtTable!.name) {
  154. final jsonBlock = RTTable.fromJson(block);
  155. blocks!.add(jsonBlock);
  156. } else if (name == ElementType.paragraph!.name) {
  157. final jsonBlock = Paragraph.fromJson(block);
  158. blocks!.add(jsonBlock);
  159. } else if (name == ElementType.imageList!.name) {
  160. final inputImageList = InputImageList.fromJson(block);
  161. blocks!.add(inputImageList);
  162. }
  163. }
  164. }
  165. List<dynamic> jsonHeads = json['Header'];
  166. if (jsonHeads.isNotEmpty) {
  167. //TODO(Loki): init headers
  168. }
  169. List<dynamic> jsonFooter = json['Footer'];
  170. if (jsonFooter.isNotEmpty) {
  171. for (var block in jsonFooter) {
  172. final type = block['ElementType'];
  173. final jsonType = ElementType.fromJson(type);
  174. final name = jsonType.name;
  175. if (name == ElementType.rtTable!.name) {
  176. final jsonBlock = RTTable.fromJson(block);
  177. footer!.add(jsonBlock);
  178. } else if (name == ElementType.paragraph!.name) {
  179. final paragraph = Paragraph.fromJson(block);
  180. footer!.add(paragraph);
  181. }
  182. }
  183. }
  184. }
  185. void _initComments(Map<String, dynamic> json) {
  186. List<String> tempComments = [];
  187. List<dynamic> jsonCustomizeComments = json['CustomizeComments'] ?? [];
  188. for (var element in jsonCustomizeComments) {
  189. tempComments.add(element.toString());
  190. }
  191. customizeComments = tempComments.toSet();
  192. tempComments.clear();
  193. List<dynamic> jsonReportComments = json['CustomizeReportComments'] ?? [];
  194. for (var element in jsonReportComments) {
  195. tempComments.add(element.toString());
  196. }
  197. customizeReportComments = tempComments.toSet();
  198. tempComments.clear();
  199. List<dynamic> jsonCustomizeCalculations =
  200. json['CustomizeCalculations'] ?? [];
  201. for (var element in jsonCustomizeCalculations) {
  202. tempComments.add(element.toString());
  203. }
  204. customizeCalculations = tempComments.toSet();
  205. }
  206. }