record_data_cache_manager.dart 7.1 KB

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