diagnosis.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. //
  46. }
  47. return diagnosisAggregationRecord;
  48. }
  49. @override
  50. Future<List<DiagnosisAggregationRecordModel>?> getListByPatientCode(
  51. String patientCode) async {
  52. try {
  53. List<DiagnosisAggregationRecordModel> records =
  54. <DiagnosisAggregationRecordModel>[];
  55. List<DiagnosisEntity> localRecords = [];
  56. if (kIsOnline) {
  57. localRecords = await db.repositories.diagnosis
  58. .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!);
  59. } else {
  60. localRecords = await db.repositories.diagnosis
  61. .getListByPatientCode(patientCode, userCode: Store.user.userCode!);
  62. }
  63. var currentPatient = Store.user.currentSelectPatientInfo!;
  64. for (var element in localRecords) {
  65. Map<String, dynamic> data = jsonDecode(element.dataJson);
  66. List<DiagnosisAggregationData> list = [];
  67. for (var key in data.keys) {
  68. list.add(DiagnosisAggregationData(
  69. key: key, diagnosisData: jsonEncode(data[key])));
  70. }
  71. records.add(DiagnosisAggregationRecordModel(
  72. appDataId: element.code,
  73. patientCode: element.patientCode,
  74. patientName: currentPatient.patientName,
  75. doctorName: Store.user.displayName,
  76. diagnosisTime: element.createTime,
  77. diagnosisAggregationData: list,
  78. isExistLocalData: element.syncState != OfflineDataSyncState.success,
  79. ));
  80. }
  81. return records;
  82. } catch (e) {
  83. return null;
  84. }
  85. }
  86. /// 创建健康检测
  87. @override
  88. Future<bool> submitDiagnosisAsync(SubmitDiagnosisRequest request) async {
  89. request.token = Store.user.token;
  90. var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request);
  91. return result;
  92. }
  93. /// 创建健康检测(在线)
  94. @override
  95. Future<bool> syncPatientAndDiagnosisData(
  96. SyncPatientAndDiagnosisDataRequest request) async {
  97. try {
  98. var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync(
  99. request,
  100. );
  101. if (result) {
  102. final code = request.cardNo!;
  103. var existEntity = await _checkPatientExist(code);
  104. if (existEntity != null) {
  105. existEntity.syncState = OfflineDataSyncState.success;
  106. existEntity.diagnosisCount -= 1; // 待上传检测条数减一
  107. if (existEntity.diagnosisCount == 0 &&
  108. existEntity.examCount == 0 &&
  109. existEntity.gxyFollowUpCount == 0 &&
  110. existEntity.tnbFollowUpCount == 0) {
  111. // 要其他数据也上传完成,才能更新overallSyncState为成功
  112. existEntity.overallSyncState = OfflineDataSyncState.success;
  113. }
  114. await db.repositories.patient.update(existEntity);
  115. }
  116. }
  117. return result;
  118. } catch (e) {
  119. logger.e(
  120. "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.",
  121. e);
  122. return false;
  123. }
  124. }
  125. Future<PatientEntity?> _checkPatientExist(String code) async {
  126. try {
  127. final entity = await db.repositories.patient
  128. .singleByCodeWithUserCode(code, Store.user.userCode!);
  129. return entity;
  130. } catch (e) {
  131. logger.e(
  132. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  133. }
  134. return null;
  135. }
  136. ///获取所有userCode为空的数据,并赋值
  137. @override
  138. Future<void> resettingUsercodeIsEmptyData() async {
  139. if (kIsWeb) return;
  140. var version = await db.database.getVersion();
  141. if (version > 0) {
  142. final list = await db.repositories.diagnosis.queryable.where((x) {
  143. final List<IDbColumnCondition> arr = [];
  144. arr.add(x.isValid.equals(true));
  145. arr.add(x.userCode.equals(''));
  146. return arr;
  147. }).toList();
  148. logger.w(
  149. "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  150. for (var element in list) {
  151. if (element.syncState == OfflineDataSyncState.success) {
  152. var result = await getDiagnosisAggregationPageAsync(
  153. element.patientCode, 1, 1000);
  154. if (result != null && result.pageData != null) {
  155. var diagnosisAggregationRecord = result.pageData!
  156. .firstWhereOrNull((x) => x.appDataId == element.code);
  157. if (diagnosisAggregationRecord != null) {
  158. element.userCode = diagnosisAggregationRecord.doctorCode ?? '';
  159. }
  160. }
  161. } else {
  162. element.userCode = Store.user.userCode!;
  163. }
  164. await db.repositories.diagnosis.update(element);
  165. }
  166. }
  167. }
  168. @override
  169. Future<List<List<String>>> getTableData(DiagnosisAggregationRecordModel dto,
  170. {bool isLastRecord = false}) async {
  171. var currentDiagnosis = <List<String>>[];
  172. var index = 1;
  173. for (var element in dto.diagnosisAggregationData!) {
  174. if (element.diagnosisData != "null") {
  175. var jsonData = json.decode(element.diagnosisData!);
  176. List<String> keys = jsonData.keys.toList();
  177. List<DictionaryWithUnitDTO>? dtos = [];
  178. if (!kIsOnline) {
  179. for (var key in keys) {
  180. dtos.add(DictionaryWithUnitDTO(
  181. key: key,
  182. name: DiagnosisTranslator.reportTr(key),
  183. unit: DiagnosisTranslator.getExamUnit(key)));
  184. }
  185. } else {
  186. dtos = await Get.find<IDictionaryManager>()
  187. .getDictionaryNameAndUnitByKeysAsync(keys) ??
  188. [];
  189. }
  190. for (var key in keys) {
  191. if (key == "ECG_POINT" || key == "ECG_POINT12" || key == "ECG12") {
  192. continue;
  193. }
  194. var dto = dtos.firstWhereOrNull((item) => item.key == key);
  195. final value = jsonData[key].toString();
  196. if (value.isEmpty) {
  197. // 不展示空值
  198. continue;
  199. }
  200. currentDiagnosis.add([
  201. if (!isLastRecord) (index++).toString(),
  202. dto?.name ?? '',
  203. value,
  204. dto?.unit ?? '',
  205. ]);
  206. }
  207. }
  208. }
  209. return currentDiagnosis;
  210. }
  211. @override
  212. Future<bool> removeDiagnosis(String appDataId) async {
  213. try {
  214. bool result = false;
  215. final entity = await db.repositories.diagnosis.singleByCode(appDataId);
  216. if (entity != null) {
  217. result = await db.repositories.diagnosis.delete(entity.id);
  218. if (!result) {
  219. return false;
  220. }
  221. }
  222. if (kIsOnline) {
  223. result = await rpc.vitalDiagnosis.removeDiagnosisByAppDataIdAsync(
  224. RemoveDiagnosisByAppDataIdRequest(
  225. token: Store.user.token,
  226. appDataId: appDataId,
  227. ),
  228. );
  229. }
  230. return result;
  231. } catch (e) {
  232. logger.e("DiagnosisManager remove diagnosis-$appDataId error.", e);
  233. return false;
  234. }
  235. }
  236. @override
  237. Future<DiagnosisAggregationRecordModel?> getLastRecordInfo() async {
  238. try {
  239. if (Store.user.currentSelectPatientInfo?.code != null) {
  240. List<DiagnosisAggregationRecordModel> getRecords = [];
  241. logger.i('onReadLastRecordInfo: 开始获取 最近检测记录.');
  242. /// 这是在线的
  243. if (kIsOnline) {
  244. var getLastRecords = await getDiagnosisAggregationPageAsync(
  245. Store.user.currentSelectPatientInfo!.code!, 1, 10);
  246. if (getLastRecords != null) {
  247. getRecords.addAll(getLastRecords.pageData ?? []);
  248. logger.i(
  249. 'PatientDetailController onReadLastRecordInfo getLastRecords.length:${getLastRecords.dataCount}.');
  250. } else {
  251. logger.i(
  252. 'PatientDetailController onReadLastRecordInfo no online data available.');
  253. }
  254. }
  255. /// 这是离线的
  256. var listRecords = await getListByPatientCode(
  257. Store.user.currentSelectPatientInfo!.code!);
  258. if (listRecords != null) {
  259. getRecords.addAll(listRecords);
  260. logger.i(
  261. 'PatientDetailController onReadLastRecordInfo listRecords.length:${listRecords.length}.');
  262. } else {
  263. logger.i(
  264. 'PatientDetailController onReadLastRecordInfo no local data available.');
  265. }
  266. if (getRecords.isNotEmpty) {
  267. var lastRecordInfo = getRecords.reduce((curr, next) =>
  268. (curr.diagnosisTime!).isAfter(next.diagnosisTime!) ? curr : next);
  269. logger.i('onReadLastRecordInfo: ${lastRecordInfo.toJson()}');
  270. return lastRecordInfo;
  271. } else {
  272. logger.i('onReadLastRecordInfo: empty');
  273. }
  274. }
  275. } catch (e) {
  276. logger.e('PatientDetailController onReadLastRecordInfo.', e);
  277. }
  278. return null;
  279. }
  280. }