record_data_cache_manager.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:vital_local_database/core/index.dart';
  6. import 'package:vitalapp/architecture/storage/storage.dart';
  7. import 'package:vitalapp/architecture/utils/upload.dart';
  8. import 'package:vitalapp/database/entities/defines.dart';
  9. import 'package:vitalapp/database/entities/diagnosis.dart';
  10. import 'package:vitalapp/rpc.dart';
  11. import 'package:vitalapp/store/store.dart';
  12. import 'package:vitalapp/database/db.dart';
  13. import 'interfaces/record_data_cache.dart';
  14. import 'package:fis_common/logger/logger.dart';
  15. class RecordDataCacheManager implements IRecordDataCacheManager {
  16. @override
  17. Future<bool> saveRecordData(
  18. String appDataId,
  19. String patientCode,
  20. Map<String, dynamic> diagnosisDataValue,
  21. ) async {
  22. try {
  23. String data = jsonEncode(diagnosisDataValue);
  24. ///更新内存缓存
  25. Store.resident.handleSaveMedicalData(data);
  26. var existData = await db.repositories.diagnosis.queryable
  27. .where((x) => [
  28. x.code.equals(appDataId),
  29. x.userCode.equals(Store.user.userCode), //添加用户Code
  30. ])
  31. .first();
  32. if (existData != null) {
  33. existData.isValid = true;
  34. existData.dataJson = data;
  35. existData.patientCode = patientCode;
  36. existData.syncState = OfflineDataSyncState.wait;
  37. //更新数据库
  38. await db.repositories.diagnosis.update(existData);
  39. } else {
  40. final entity = DiagnosisEntity();
  41. entity.code = appDataId;
  42. entity.isValid = true;
  43. entity.dataJson = data;
  44. entity.patientCode = patientCode;
  45. entity.userCode = Store.user.userCode!; //添加用户Code
  46. entity.syncState = OfflineDataSyncState.wait;
  47. //新增数据
  48. final id = await db.repositories.diagnosis.insert(entity);
  49. if (id > 0) {
  50. // 统计数量+1
  51. await db.repositories.patient
  52. .increaseDiagnoissCount(patientCode, Store.user.userCode!);
  53. }
  54. }
  55. return true;
  56. } catch (e) {
  57. logger.e('RecordDataCacheManager saveRecordData ex:', e);
  58. }
  59. return false;
  60. }
  61. ///更新记录状态
  62. @override
  63. Future<bool> recordSyncStateChange(
  64. String appDataId, {
  65. OfflineDataSyncState state = OfflineDataSyncState.success,
  66. }) async {
  67. try {
  68. var existData = await db.repositories.diagnosis.queryable
  69. .where((x) => [
  70. x.code.equals(appDataId),
  71. x.userCode.equals(Store.user.userCode), //添加用户code
  72. ])
  73. .first();
  74. if (existData != null) {
  75. existData.syncState = state;
  76. //更新数据库
  77. await db.repositories.diagnosis.update(existData);
  78. }
  79. } catch (e) {
  80. logger.e('RecordDataCacheManager recordSyncStateChange ex:', e);
  81. }
  82. return false;
  83. }
  84. ///根据病人Code与用户Code获取最近一条检查记录
  85. @override
  86. Future<DiagnosisEntity?> getLastRecordByPatientCode(
  87. String patientCode) async {
  88. var existDatas = db.repositories.diagnosis.queryable.where((x) => [
  89. x.patientCode.equals(patientCode),
  90. x.userCode.equals(Store.user.userCode), //添加用户code
  91. ]);
  92. var updateTimeData = await existDatas
  93. .orderBy((x) => x.updateTime, DbOrderByType.desc)
  94. .first();
  95. var createTimeData = await existDatas
  96. .orderBy((x) => x.createTime, DbOrderByType.desc)
  97. .first();
  98. if (updateTimeData != null && createTimeData != null) {
  99. final now = DateTime.now();
  100. ///如果存在更新时间的数据,则需要比较和创建时间哪个更新
  101. final diff1 = (updateTimeData.updateTime ?? DateTime(1970, 1, 1, 0, 0, 0))
  102. .difference(now);
  103. final diff2 = createTimeData.createTime.difference(now);
  104. if (diff1.abs() < diff2.abs()) {
  105. return updateTimeData;
  106. } else {
  107. return createTimeData;
  108. }
  109. } else if (createTimeData != null) {
  110. //如果不存在更新时间的数据,则直接返回创建时间的
  111. return createTimeData;
  112. }
  113. return null;
  114. }
  115. @override
  116. Future<List<DiagnosisEntity>> getNoSubmitRecords(String patientCode) async {
  117. List<DiagnosisEntity> result = [];
  118. try {
  119. result = await db.repositories.diagnosis.queryable
  120. .where((x) => [
  121. x.syncState.equals(OfflineDataSyncState.wait),
  122. x.patientCode.equals(patientCode),
  123. x.userCode.equals(Store.user.userCode), //添加用户code
  124. ])
  125. .toList();
  126. } catch (e) {
  127. logger.e('RecordDataCacheManager getRecordsByPatientCode ex:', e);
  128. }
  129. return result;
  130. }
  131. @override
  132. Future<List<DiagnosisItem>> convertDiagnosisDataToList(
  133. Map<String, dynamic> diagnosisDataValue) async {
  134. List<DiagnosisItem> diagnosisItems = [];
  135. for (var entry in diagnosisDataValue.entries) {
  136. var key = entry.key;
  137. var value = entry.value;
  138. if (value != null) {
  139. Store.app.setBusy("提交中");
  140. if (['Heart', 'TwelveHeart'].contains(key) && value is Map) {
  141. value = await uploadData(value);
  142. }
  143. Store.app.busy = false;
  144. diagnosisItems.add(
  145. DiagnosisItem(
  146. key: key,
  147. diagnosisData: jsonEncode(value),
  148. ),
  149. );
  150. }
  151. print('$key: $value');
  152. }
  153. Store.app.busy = false;
  154. return diagnosisItems;
  155. }
  156. @override
  157. Future<List<DiagnosisItem>> verifyDiagnosisDataList(
  158. Map<String, dynamic> diagnosisDataValue) async {
  159. List<DiagnosisItem> diagnosisItems = [];
  160. for (var entry in diagnosisDataValue.entries) {
  161. var key = entry.key;
  162. var value = entry.value;
  163. if (value != null) {
  164. if (['Heart', 'TwelveHeart'].contains(key) && value is Map) {
  165. if (value.isEmpty) {
  166. continue;
  167. }
  168. }
  169. diagnosisItems.add(
  170. DiagnosisItem(
  171. key: key,
  172. diagnosisData: jsonEncode(value),
  173. ),
  174. );
  175. }
  176. print('$key: $value');
  177. }
  178. return diagnosisItems;
  179. }
  180. bool isUploaded(String url) {
  181. return url.startsWith('https://') || url.startsWith('http://');
  182. }
  183. Future<Map> uploadData(Map data) async {
  184. if (data['ECG_POINT'] != null && !isUploaded(data['ECG_POINT'])) {
  185. if (kIsWeb) {
  186. Uint8List ecgPointbyte =
  187. await rpc.storage.writeStringToByte(data['ECG_POINT']);
  188. String? ecgPointUrl = await rpc.storage.uploadByte(ecgPointbyte);
  189. data['ECG_POINT'] = ecgPointUrl ?? '';
  190. } else {
  191. File ecgPointFile =
  192. await rpc.storage.writeStringToFile(data['ECG_POINT']);
  193. String? ecgPointUrl = await rpc.storage.uploadFile(ecgPointFile);
  194. data['ECG_POINT'] = ecgPointUrl ?? '';
  195. }
  196. // ... 上传点集
  197. }
  198. if (data['ECG'] != null && !isUploaded(data['ECG'])) {
  199. // ... 上传图片
  200. /// 图片地址
  201. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG']);
  202. String? imageUrl = await rpc.storage.upload(imageFile!);
  203. data['ECG'] = imageUrl ?? '';
  204. }
  205. if (data['ECG_POINT12'] != null && !isUploaded(data['ECG_POINT12'])) {
  206. if (kIsWeb) {
  207. Uint8List ecgPointBytes =
  208. await rpc.storage.writeStringToByte(data['ECG_POINT12']);
  209. String? ecgPointUrl = await rpc.storage.uploadByte(ecgPointBytes);
  210. data['ECG_POINT12'] = ecgPointUrl ?? '';
  211. } else {
  212. File ecgPointFile =
  213. await rpc.storage.writeStringToFile(data['ECG_POINT12']);
  214. String? ecgPointUrl = await rpc.storage.uploadFile(ecgPointFile);
  215. data['ECG_POINT12'] = ecgPointUrl ?? '';
  216. }
  217. // ... 上传点集
  218. }
  219. if (data['ECG12'] != null && !isUploaded(data['ECG12'])) {
  220. // ... 上传图片
  221. if (kIsWeb) {
  222. var file = UploadUtils.convertBase64ToFile(data['ECG12']);
  223. String? url = await rpc.storage.webUpload(file!);
  224. data['ECG12'] = url ?? '';
  225. } else {
  226. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG12']);
  227. String? imageUrl = await rpc.storage.upload(imageFile!);
  228. data['ECG12'] = imageUrl ?? '';
  229. }
  230. }
  231. if (data['ECG12_30s'] != null && !isUploaded(data['ECG12_30s'])) {
  232. // ... 上传图片
  233. if (kIsWeb) {
  234. var file = UploadUtils.convertBase64ToFile(data['ECG12_30s']);
  235. String? url = await rpc.storage.webUpload(file!);
  236. data['ECG12_30s'] = url ?? '';
  237. } else {
  238. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG12_30s']);
  239. String? imageUrl =
  240. await rpc.storage.upload(imageFile!, fileType: "jpg");
  241. data['ECG12_30s'] = imageUrl ?? '';
  242. }
  243. }
  244. return data;
  245. }
  246. }