controller.dart 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  7. import 'package:get/get.dart';
  8. import 'package:intl/intl.dart';
  9. import 'package:vitalapp/architecture/defines.dart';
  10. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  11. import 'package:vitalapp/database/db.dart';
  12. import 'package:vitalapp/managers/interfaces/follow_up.dart';
  13. import 'package:vitalapp/pages/check/follow_up/controller.dart';
  14. import 'package:vitalapp/pages/check/follow_up/widgets/follow_up_from.dart';
  15. import 'package:vitalapp/pages/check/follow_up_record/state.dart';
  16. import 'package:vitalapp/pages/check/widgets/configurable_card.dart';
  17. import 'package:vitalapp/store/store.dart';
  18. class FollowUpRecordController extends FControllerBase {
  19. FollowUpRecordController();
  20. final state = FollowUpRecordState();
  21. final _followUpManager = Get.find<IFollowUpManager>();
  22. late String patientCode;
  23. late String patientName;
  24. late FollowUpController _followUpController;
  25. List<List> offlineSyncTemp = [];
  26. @override
  27. void onReady() {
  28. super.onReady();
  29. _initData();
  30. }
  31. _initData() {
  32. update(["contract_records"]);
  33. patientCode = Store.user.currentSelectPatientInfo?.code ?? '';
  34. patientName = Store.user.currentSelectPatientInfo?.patientName ?? '';
  35. _getFollowUpRecordList();
  36. _followUpController = Get.put(FollowUpController());
  37. }
  38. Future<void> _getFollowUpRecordList() async {
  39. try {
  40. var result = await _followUpManager.getFollowUpRecordList(
  41. followUpKeyValue.keys.toList(), patientCode);
  42. if (result != null) {
  43. await _loadUnsyncIndexs(result); // TODO temp
  44. state.followUpDTOList = result;
  45. }
  46. } catch (e) {
  47. return;
  48. }
  49. }
  50. Future<void> _loadUnsyncIndexs(List<FollowUpRecordDTO> dtos) async {
  51. offlineSyncTemp = [];
  52. /// web 端没有本地数据库
  53. if (!kIsWeb) {
  54. // TODO 临时打个补丁,后续再优化
  55. final entities = await db.repositories.followUp
  56. .queryAllListByPatient(patientCode, Store.user.userCode!);
  57. for (var i = 0; i < dtos.length; i++) {
  58. offlineSyncTemp.add([]);
  59. final records = dtos[i].followUpRecordDatas;
  60. if (records == null) {
  61. continue;
  62. }
  63. for (var j = 0; j < records.length; j++) {
  64. final data = records[j];
  65. final entity = entities.firstWhereOrNull((e) => e.code == data.code);
  66. if (entity != null) {
  67. offlineSyncTemp[i].add(entity.syncState);
  68. } else {
  69. offlineSyncTemp[i].add(null);
  70. }
  71. }
  72. }
  73. } else {
  74. for (var i = 0; i < dtos.length; i++) {
  75. offlineSyncTemp.add([]);
  76. final records = dtos[i].followUpRecordDatas;
  77. if (records == null) {
  78. continue;
  79. }
  80. for (var j = 0; j < records.length; j++) {
  81. final data = records[j];
  82. offlineSyncTemp[i].add(data);
  83. }
  84. }
  85. }
  86. }
  87. Future<void> updateFollowUp(key, code, data) async {
  88. List<String> followUpPhotos = [];
  89. if (_followUpController.state.followUpPhoto?.isNotEmpty ?? false) {
  90. followUpPhotos = [_followUpController.state.followUpPhoto!];
  91. }
  92. final result = await _followUpManager.updateFollowUp(
  93. UpdateFollowUpRequest(
  94. key: key,
  95. followUpData: data,
  96. followUpTime: _followUpController.state.followUpTime,
  97. nextFollowUpTime: _followUpController.state.nextFollowUpTime,
  98. followUpMode: _followUpController.state.followUpMode ??
  99. FollowUpModeEnum.Outpatient,
  100. code: code,
  101. followUpPhotos: followUpPhotos,
  102. ),
  103. );
  104. if (result ?? false) {
  105. PromptBox.toast('保存成功');
  106. } else {
  107. PromptBox.toast('保存失败');
  108. }
  109. }
  110. ///读取静态Html
  111. Future<String> loadingLocalAsset(String key) async {
  112. String tableName = "";
  113. if (key == 'GXY') {
  114. tableName = 'assets/docs/highBloodPressureTable.html';
  115. } else if (key == 'TNB') {
  116. tableName = 'assets/docs/diabetesTable.html';
  117. }
  118. ///加载
  119. String htmlData = await rootBundle.loadString(tableName);
  120. print("加载数据完成 $htmlData");
  121. return htmlData;
  122. }
  123. Future<List> getFollowUpTableData(FollowUpRecordDTO dto, int index) async {
  124. var currentFollowUp = dto.followUpRecordDatas?[index];
  125. var followUpTableData = await _followUpManager
  126. .getFollowUpRecordListByYearAsync(GetFollowUpRecordListByYearRequest(
  127. year: (currentFollowUp?.followUpTime?.year ?? DateTime.now().year)
  128. .toString(),
  129. keys: [currentFollowUp?.key ?? ''],
  130. patientCode: patientCode,
  131. ));
  132. List tableData = [];
  133. followUpTableData?.first.followUpRecordDatas?.forEach((element) {
  134. Map followUp = {
  135. "Follow_Time": element.followUpTime != null
  136. ? DateFormat("yyyy-MM-dd").format(element.followUpTime!.toLocal())
  137. : "",
  138. "Follow_Type": '${(element.followUpMode?.index ?? 0) + 1} ',
  139. "Next_Follow_Up_Time": element.nextFollowUpTime != null
  140. ? DateFormat("yyyy-MM-dd")
  141. .format(element.nextFollowUpTime!.toLocal())
  142. : "",
  143. };
  144. followUp.addAll(jsonDecode(element.followUpData ?? ''));
  145. tableData.add(followUp);
  146. });
  147. return tableData;
  148. }
  149. Future<String> loadLocalAsset(String key) async {
  150. String tableName = "";
  151. if (key == 'GXY') {
  152. tableName = 'assets/docs/highBloodPressureTable.html';
  153. } else if (key == 'TNB') {
  154. tableName = 'assets/docs/diabetesTable.html';
  155. }
  156. ///加载
  157. String htmlData = await rootBundle.loadString(tableName);
  158. print("加载数据完成 $htmlData");
  159. return htmlData;
  160. }
  161. Future<void> showDialogBox(String initialData, String jsonData) async {
  162. Get.dialog(Column(
  163. children: [
  164. Container(
  165. width: 900,
  166. alignment: Alignment.centerRight,
  167. padding: const EdgeInsets.only(right: 20, top: 10),
  168. color: Colors.white,
  169. child: IconButton(
  170. onPressed: () {
  171. Get.back();
  172. },
  173. icon: const Icon(
  174. Icons.close,
  175. ),
  176. ),
  177. ),
  178. Expanded(
  179. child: SizedBox(
  180. width: 900,
  181. child: InAppWebView(
  182. initialData: InAppWebViewInitialData(
  183. data: '<body style="padding:30px 100px;">$initialData </body>',
  184. mimeType: 'text/html',
  185. encoding: 'utf-8',
  186. ),
  187. onWebViewCreated: (controller) {
  188. controller.evaluateJavascript(
  189. source: "window.tableData = $jsonData;");
  190. },
  191. onConsoleMessage: (controller, consoleMessage) {
  192. // print(consoleMessage);
  193. },
  194. ),
  195. ),
  196. ),
  197. ],
  198. ));
  199. }
  200. Future<void> toFollowUpDetailPage(int index, FollowUpRecordDTO dto) async {
  201. var tableData = await getFollowUpTableData(dto, index);
  202. var initialData =
  203. await loadLocalAsset(dto.followUpRecordDatas?[index].key ?? '');
  204. var jsonData = jsonEncode(tableData);
  205. showDialogBox(initialData, jsonData);
  206. }
  207. toCheckPage(FollowUpRecordDataDTO dataDTO) async {
  208. _followUpController.state.followUpTime = dataDTO.followUpTime;
  209. _followUpController.state.nextFollowUpTime = dataDTO.nextFollowUpTime;
  210. _followUpController.state.followUpMode = dataDTO.followUpMode;
  211. Get.to(
  212. ConfigurableCard(
  213. cardKey: dataDTO.key!,
  214. examData: dataDTO.followUpData,
  215. followUpWidget: const FollowUpFrom(),
  216. patientCode: patientCode,
  217. callBack: (key, code, data) async {
  218. await updateFollowUp(key, dataDTO.code, data);
  219. await _getFollowUpRecordList();
  220. return true;
  221. },
  222. ),
  223. transition: Transition.rightToLeft,
  224. );
  225. }
  226. final Map<String, String> followUpKeyValue = {
  227. 'FJHSFFW': "肺结核随访服务",
  228. 'FJHRHSF': "肺结核入户随访",
  229. 'GXB': "冠心病",
  230. 'NCZ': "脑卒中",
  231. 'YZJSZASFFW': "严重精神障碍随访服务",
  232. 'YZJSZAGRXXBC': "严重精神障碍个人信息补充",
  233. 'TNB': "2型糖尿病",
  234. 'GXY': "高血压",
  235. // 'LNRZYYJKGLFWJL': "老年人中医药健康管理服务记录表",
  236. 'LNRSHZLNLPGB': "老年人生活自理能力评估表",
  237. 'YCF_CH42TJKJCLB': "产后42天健康检查列表",
  238. 'YCF_CHFSLB': "产后访视列表",
  239. 'YCF_2_5CCQSFLB': "2~5次产前随访列表",
  240. 'YCF_DYCCQJCLB': "第一次产前检查列表",
  241. 'YCF_JBXX': "基本信息",
  242. 'ET_ZYYJKGLLB': "儿童中医药健康管理列表",
  243. 'ET_3_6SETJKJCLB': "3~6岁儿童健康检查列表",
  244. 'ET_1_2SETJKJCLB': "1~2岁儿童健康检查列表",
  245. 'ET_1SNETJKJCLB': "1岁内儿童健康检查列表",
  246. 'ET_XSEFSLB': "新生儿访视列表",
  247. };
  248. String getFollowUpValueByKey(String key) {
  249. if (followUpKeyValue[key] != null) {
  250. return followUpKeyValue[key]!;
  251. } else {
  252. return "";
  253. }
  254. }
  255. String followUpStateTransition(FollowUpStateEnum state) {
  256. switch (state) {
  257. case FollowUpStateEnum.NoFollowUp:
  258. return "未随访";
  259. case FollowUpStateEnum.FollowUpVisit:
  260. return "已随访";
  261. case FollowUpStateEnum.Cancelled:
  262. return "已作废";
  263. default:
  264. return "";
  265. }
  266. }
  267. MaterialColor followUpStateColors(FollowUpStateEnum state) {
  268. switch (state) {
  269. case FollowUpStateEnum.NoFollowUp:
  270. return Colors.grey;
  271. case FollowUpStateEnum.Cancelled:
  272. return Colors.red;
  273. case FollowUpStateEnum.FollowUpVisit:
  274. return Colors.green;
  275. default:
  276. return Colors.blue;
  277. }
  278. }
  279. String getFollowUpMode(FollowUpModeEnum? modeEnum) {
  280. switch (modeEnum) {
  281. case FollowUpModeEnum.Outpatient:
  282. return "门诊";
  283. case FollowUpModeEnum.Phone:
  284. return "电话";
  285. case FollowUpModeEnum.Visit:
  286. return "家庭";
  287. default:
  288. return "";
  289. }
  290. }
  291. }