diagnosis.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vital_local_database/core/index.dart';
  6. import 'package:vitalapp/consts/diagnosis.dart';
  7. import 'package:vitalapp/database/db.dart';
  8. import 'package:vitalapp/database/entities/defines.dart';
  9. import 'package:vitalapp/database/entities/diagnosis.dart';
  10. import 'package:vitalapp/database/entities/patient.dart';
  11. import 'package:vitalapp/global.dart';
  12. import 'package:vitalapp/managers/interfaces/diagnosis.dart';
  13. import 'package:vitalapp/managers/interfaces/dictionary.dart';
  14. import 'package:vitalapp/managers/interfaces/models/diagnosis_aggregation_record_model.dart';
  15. import 'package:vitalapp/rpc.dart';
  16. import 'package:vitalapp/store/store.dart';
  17. import 'package:fis_common/logger/logger.dart';
  18. class DiagnosisManager implements IDiagnosisManager {
  19. DiagnosisManager();
  20. @override
  21. Future<PageCollection<DiagnosisAggregationRecordModel>?>
  22. getDiagnosisAggregationPageAsync(
  23. String patientCode, int pageIndex, int pageSize) async {
  24. PageCollection<DiagnosisAggregationRecordModel> diagnosisAggregationRecord =
  25. PageCollection<DiagnosisAggregationRecordModel>();
  26. try {
  27. var request = DiagnosisPageRequest(
  28. token: Store.user.token,
  29. pageIndex: pageIndex,
  30. pageSize: pageSize,
  31. patientCode: patientCode,
  32. );
  33. diagnosisAggregationRecord.pageData = [];
  34. var result =
  35. await rpc.vitalDiagnosis.getDiagnosisAggregationPageAsync(request);
  36. diagnosisAggregationRecord.pageIndex = result.pageIndex;
  37. diagnosisAggregationRecord.pageSize = result.pageSize;
  38. diagnosisAggregationRecord.dataCount = result.dataCount;
  39. for (var element in result.pageData!) {
  40. var record = DiagnosisAggregationRecordModel.fromJson(element.toJson());
  41. record.isExistLocalData = false;
  42. diagnosisAggregationRecord.pageData!.add(record);
  43. }
  44. } catch (e) {}
  45. return diagnosisAggregationRecord;
  46. }
  47. @override
  48. Future<List<DiagnosisAggregationRecordModel>?> getListByPatientCode(
  49. String patientCode) async {
  50. try {
  51. List<DiagnosisAggregationRecordModel> records =
  52. <DiagnosisAggregationRecordModel>[];
  53. List<DiagnosisEntity> localRecords = [];
  54. if (kIsOnline) {
  55. localRecords = await db.repositories.diagnosis
  56. .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!);
  57. } else {
  58. localRecords = await db.repositories.diagnosis
  59. .getListByPatientCode(patientCode, userCode: Store.user.userCode!);
  60. }
  61. var currentPatient = Store.user.currentSelectPatientInfo!;
  62. for (var element in localRecords) {
  63. Map<String, dynamic> data = jsonDecode(element.dataJson);
  64. List<DiagnosisAggregationData> list = [];
  65. for (var key in data.keys) {
  66. list.add(DiagnosisAggregationData(
  67. key: key, diagnosisData: jsonEncode(data[key])));
  68. }
  69. records.add(DiagnosisAggregationRecordModel(
  70. appDataId: element.code,
  71. patientCode: element.patientCode,
  72. patientName: currentPatient.patientName,
  73. doctorName: Store.user.displayName,
  74. diagnosisTime: element.createTime,
  75. diagnosisAggregationData: list,
  76. isExistLocalData: element.syncState != OfflineDataSyncState.success,
  77. ));
  78. }
  79. return records;
  80. } catch (e) {}
  81. return null;
  82. }
  83. /// 创建健康检测
  84. @override
  85. Future<bool> submitDiagnosisAsync(SubmitDiagnosisRequest request) async {
  86. request.token = Store.user.token;
  87. var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request);
  88. return result;
  89. }
  90. /// 创建健康检测(在线)
  91. @override
  92. Future<bool> syncPatientAndDiagnosisData(
  93. SyncPatientAndDiagnosisDataRequest request) async {
  94. try {
  95. var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync(
  96. request,
  97. );
  98. if (result) {
  99. final code = request.cardNo!;
  100. var existEntity = await _checkPatientExist(code);
  101. if (existEntity != null) {
  102. existEntity.syncState = OfflineDataSyncState.success;
  103. existEntity.diagnosisCount -= 1; // 待上传检测条数减一
  104. if (existEntity.diagnosisCount == 0 &&
  105. existEntity.examCount == 0 &&
  106. existEntity.followUpCount == 0) {
  107. // 要其他数据也上传完成,才能更新overallSyncState为成功
  108. existEntity.overallSyncState = OfflineDataSyncState.success;
  109. }
  110. await db.repositories.patient.update(existEntity);
  111. }
  112. }
  113. return result;
  114. } catch (e) {
  115. logger.e(
  116. "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.",
  117. e);
  118. return false;
  119. }
  120. }
  121. Future<PatientEntity?> _checkPatientExist(String code) async {
  122. try {
  123. final entity = await db.repositories.patient.singleByCode(code);
  124. return entity;
  125. } catch (e) {
  126. logger.e(
  127. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  128. }
  129. return null;
  130. }
  131. ///获取所有userCode为空的数据,并赋值
  132. @override
  133. Future<void> resettingUsercodeIsEmptyData() async {
  134. if (kIsWeb) return;
  135. var version = await db.database.getVersion();
  136. if (version > 0) {
  137. final list = await db.repositories.diagnosis.queryable.where((x) {
  138. final List<IDbColumnCondition> arr = [];
  139. arr.add(x.isValid.equals(true));
  140. arr.add(x.userCode.equals(''));
  141. return arr;
  142. }).toList();
  143. logger.w(
  144. "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  145. for (var element in list) {
  146. if (element.syncState == OfflineDataSyncState.success) {
  147. var result = await getDiagnosisAggregationPageAsync(
  148. element.patientCode, 1, 1000);
  149. if (result != null && result.pageData != null) {
  150. var diagnosisAggregationRecord = result.pageData!
  151. .firstWhereOrNull((x) => x.appDataId == element.code);
  152. if (diagnosisAggregationRecord != null) {
  153. element.userCode = diagnosisAggregationRecord.doctorCode ?? '';
  154. }
  155. }
  156. } else {
  157. element.userCode = Store.user.userCode!;
  158. }
  159. await db.repositories.diagnosis.update(element);
  160. }
  161. }
  162. }
  163. @override
  164. Future<List<List<String>>> getTableData(DiagnosisAggregationRecordModel dto,
  165. {bool isLastRecord = false}) async {
  166. var currentDiagnosis = <List<String>>[];
  167. var index = 1;
  168. for (var element in dto.diagnosisAggregationData!) {
  169. if (element.diagnosisData != "null") {
  170. var jsonData = json.decode(element.diagnosisData!);
  171. List<String> keys = jsonData.keys.toList();
  172. List<DictionaryWithUnitDTO>? dtos = [];
  173. if (!kIsOnline) {
  174. for (var key in keys) {
  175. dtos.add(DictionaryWithUnitDTO(
  176. key: key,
  177. name: DiagnosisTranslator.reportTr(key),
  178. unit: DiagnosisTranslator.getExamUnit(key)));
  179. }
  180. } else {
  181. dtos = await Get.find<IDictionaryManager>()
  182. .getDictionaryNameAndUnitByKeysAsync(keys) ??
  183. [];
  184. }
  185. for (var key in keys) {
  186. if (key == "ECG_POINT" || key == "ECG_POINT12") {
  187. continue;
  188. }
  189. var dto = dtos.firstWhereOrNull((item) => item.key == key);
  190. final value = jsonData[key].toString();
  191. if (value.isEmpty) {
  192. // 不展示空值
  193. continue;
  194. }
  195. currentDiagnosis.add([
  196. if (!isLastRecord) (index++).toString(),
  197. dto?.name ?? '',
  198. value,
  199. dto?.unit ?? '',
  200. ]);
  201. }
  202. }
  203. }
  204. return currentDiagnosis;
  205. }
  206. @override
  207. Future<bool> removeDiagnosis(String appDataId) async {
  208. try {
  209. bool result = false;
  210. final entity = await db.repositories.diagnosis.singleByCode(appDataId);
  211. if (entity != null) {
  212. result = await db.repositories.diagnosis.delete(entity.id);
  213. }
  214. if (result && kIsOnline) {
  215. result = await rpc.vitalDiagnosis.removeDiagnosisByAppDataIdAsync(
  216. RemoveDiagnosisByAppDataIdRequest(
  217. token: Store.user.token,
  218. appDataId: appDataId,
  219. ),
  220. );
  221. }
  222. return result;
  223. } catch (e) {
  224. logger.e("DiagnosisManager remove diagnosis-$appDataId error.", e);
  225. return false;
  226. }
  227. }
  228. }