record_data_cache_manager.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.wait;
  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. final id = await db.repositories.diagnosis.insert(entity);
  48. if (id > 0) {
  49. // 统计数量+1
  50. await db.repositories.patient
  51. .increaseDiagnoissCount(patientCode, Store.user.userCode!);
  52. }
  53. }
  54. return true;
  55. } catch (e) {
  56. logger.e('RecordDataCacheManager saveRecordData ex:', e);
  57. }
  58. return false;
  59. }
  60. ///更新记录状态
  61. @override
  62. Future<bool> recordSyncStateChange(
  63. String appDataId, {
  64. OfflineDataSyncState state = OfflineDataSyncState.success,
  65. }) async {
  66. try {
  67. var existData = await db.repositories.diagnosis.queryable
  68. .where((x) => [
  69. x.code.equals(appDataId),
  70. x.userCode.equals(Store.user.userCode), //添加用户code
  71. ])
  72. .first();
  73. if (existData != null) {
  74. existData.syncState = state;
  75. //更新数据库
  76. await db.repositories.diagnosis.update(existData);
  77. }
  78. } catch (e) {
  79. logger.e('RecordDataCacheManager recordSyncStateChange ex:', e);
  80. }
  81. return false;
  82. }
  83. ///根据病人Code与用户Code获取最近一条检查记录
  84. @override
  85. Future<DiagnosisEntity?> getLastRecordByPatientCode(
  86. String patientCode) async {
  87. var existDatas = db.repositories.diagnosis.queryable.where((x) => [
  88. x.patientCode.equals(patientCode),
  89. x.userCode.equals(Store.user.userCode), //添加用户code
  90. ]);
  91. var updateTimeData = await existDatas
  92. .orderBy((x) => x.updateTime, DbOrderByType.desc)
  93. .first();
  94. var createTimeData = await existDatas
  95. .orderBy((x) => x.createTime, DbOrderByType.desc)
  96. .first();
  97. if (updateTimeData != null && createTimeData != null) {
  98. final now = DateTime.now();
  99. ///如果存在更新时间的数据,则需要比较和创建时间哪个更新
  100. final diff1 = (updateTimeData.updateTime ?? DateTime(1970, 1, 1, 0, 0, 0))
  101. .difference(now);
  102. final diff2 = createTimeData.createTime.difference(now);
  103. if (diff1.abs() < diff2.abs()) {
  104. return updateTimeData;
  105. } else {
  106. return createTimeData;
  107. }
  108. } else if (createTimeData != null) {
  109. //如果不存在更新时间的数据,则直接返回创建时间的
  110. return createTimeData;
  111. }
  112. return null;
  113. }
  114. @override
  115. Future<List<DiagnosisEntity>> getNoSubmitRecords(String patientCode) async {
  116. List<DiagnosisEntity> result = [];
  117. try {
  118. result = await db.repositories.diagnosis.queryable
  119. .where((x) => [
  120. x.syncState.equals(OfflineDataSyncState.wait),
  121. x.patientCode.equals(patientCode),
  122. x.userCode.equals(Store.user.userCode), //添加用户code
  123. ])
  124. .toList();
  125. } catch (e) {
  126. logger.e('RecordDataCacheManager getRecordsByPatientCode ex:', e);
  127. }
  128. return result;
  129. }
  130. @override
  131. Future<List<DiagnosisItem>> convertDiagnosisDataToList(
  132. Map<String, dynamic> diagnosisDataValue) async {
  133. List<DiagnosisItem> diagnosisItems = [];
  134. for (var entry in diagnosisDataValue.entries) {
  135. var key = entry.key;
  136. var value = entry.value;
  137. if (value != null) {
  138. Store.app.setBusy("提交中");
  139. if (['Heart', 'TwelveHeart'].contains(key) && value is Map) {
  140. value = await uploadData(value);
  141. }
  142. Store.app.busy = false;
  143. diagnosisItems.add(
  144. DiagnosisItem(
  145. key: key,
  146. diagnosisData: jsonEncode(value),
  147. ),
  148. );
  149. }
  150. print('$key: $value');
  151. }
  152. Store.app.busy = false;
  153. return diagnosisItems;
  154. }
  155. @override
  156. Future<List<DiagnosisItem>> verifyDiagnosisDataList(
  157. Map<String, dynamic> diagnosisDataValue) async {
  158. List<DiagnosisItem> diagnosisItems = [];
  159. for (var entry in diagnosisDataValue.entries) {
  160. var key = entry.key;
  161. var value = entry.value;
  162. if (value != null) {
  163. if (['Heart', 'TwelveHeart'].contains(key) && value is Map) {
  164. if (value.isEmpty) {
  165. continue;
  166. }
  167. }
  168. diagnosisItems.add(
  169. DiagnosisItem(
  170. key: key,
  171. diagnosisData: jsonEncode(value),
  172. ),
  173. );
  174. }
  175. print('$key: $value');
  176. }
  177. return diagnosisItems;
  178. }
  179. bool isUploaded(String url) {
  180. return url.startsWith('https://') || url.startsWith('http://');
  181. }
  182. Future<Map> uploadData(Map data) async {
  183. if (data['ECG_POINT'] != null && !isUploaded(data['ECG_POINT'])) {
  184. File ecgPointFile =
  185. await rpc.storage.writeStringToFile(data['ECG_POINT']);
  186. String? ecgPointUrl = await rpc.storage.uploadFile(ecgPointFile);
  187. data['ECG_POINT'] = ecgPointUrl ?? '';
  188. // ... 上传点集
  189. }
  190. if (data['ECG'] != null && !isUploaded(data['ECG'])) {
  191. // ... 上传图片
  192. /// 图片地址
  193. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG']);
  194. String? imageUrl = await rpc.storage.upload(imageFile!);
  195. data['ECG'] = imageUrl ?? '';
  196. }
  197. if (data['ECG_POINT12'] != null && !isUploaded(data['ECG_POINT12'])) {
  198. File ecgPointFile =
  199. await rpc.storage.writeStringToFile(data['ECG_POINT12']);
  200. String? ecgPointUrl = await rpc.storage.uploadFile(ecgPointFile);
  201. data['ECG_POINT12'] = ecgPointUrl ?? '';
  202. // ... 上传点集
  203. }
  204. if (data['ECG12'] != null && !isUploaded(data['ECG12'])) {
  205. // ... 上传图片
  206. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG12']);
  207. String? imageUrl = await rpc.storage.upload(imageFile!);
  208. data['ECG12'] = imageUrl ?? '';
  209. }
  210. if (data['ECG12_30s'] != null && !isUploaded(data['ECG12_30s'])) {
  211. // ... 上传图片
  212. final imageFile = UploadUtils.convertBase64ToXFile(data['ECG12_30s']);
  213. String? imageUrl = await rpc.storage.upload(imageFile!, fileType: "jpg");
  214. data['ECG12_30s'] = imageUrl ?? '';
  215. }
  216. return data;
  217. }
  218. }