import 'dart:convert'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; import 'package:vital_local_database/core/index.dart'; import 'package:vitalapp/consts/diagnosis.dart'; import 'package:vitalapp/database/db.dart'; import 'package:vitalapp/database/entities/defines.dart'; import 'package:vitalapp/database/entities/diagnosis.dart'; import 'package:vitalapp/database/entities/patient.dart'; import 'package:vitalapp/global.dart'; import 'package:vitalapp/managers/interfaces/diagnosis.dart'; import 'package:vitalapp/managers/interfaces/dictionary.dart'; import 'package:vitalapp/managers/interfaces/models/diagnosis_aggregation_record_model.dart'; import 'package:vitalapp/rpc.dart'; import 'package:vitalapp/store/store.dart'; import 'package:fis_common/logger/logger.dart'; class DiagnosisManager implements IDiagnosisManager { DiagnosisManager(); @override Future?> getDiagnosisAggregationPageAsync( String patientCode, int pageIndex, int pageSize) async { PageCollection diagnosisAggregationRecord = PageCollection(); try { var request = DiagnosisPageRequest( token: Store.user.token, pageIndex: pageIndex, pageSize: pageSize, patientCode: patientCode, ); diagnosisAggregationRecord.pageData = []; var result = await rpc.vitalDiagnosis.getDiagnosisAggregationPageAsync(request); diagnosisAggregationRecord.pageIndex = result.pageIndex; diagnosisAggregationRecord.pageSize = result.pageSize; diagnosisAggregationRecord.dataCount = result.dataCount; for (var element in result.pageData!) { var record = DiagnosisAggregationRecordModel.fromJson(element.toJson()); record.isExistLocalData = false; diagnosisAggregationRecord.pageData!.add(record); } } catch (e) { // } return diagnosisAggregationRecord; } @override Future?> getListByPatientCode( String patientCode) async { try { List records = []; List localRecords = []; if (kIsOnline) { localRecords = await db.repositories.diagnosis .getNotUploadedListByPatientCode(patientCode, Store.user.userCode!); } else { localRecords = await db.repositories.diagnosis .getListByPatientCode(patientCode, userCode: Store.user.userCode!); } var currentPatient = Store.user.currentSelectPatientInfo!; for (var element in localRecords) { Map data = jsonDecode(element.dataJson); List list = []; for (var key in data.keys) { list.add(DiagnosisAggregationData( key: key, diagnosisData: jsonEncode(data[key]))); } records.add(DiagnosisAggregationRecordModel( appDataId: element.code, patientCode: element.patientCode, patientName: currentPatient.patientName, doctorName: Store.user.displayName, diagnosisTime: element.createTime, diagnosisAggregationData: list, isExistLocalData: element.syncState != OfflineDataSyncState.success, )); } return records; } catch (e) { return null; } } /// 创建健康检测 @override Future submitDiagnosisAsync(SubmitDiagnosisRequest request) async { request.token = Store.user.token; var result = await rpc.vitalDiagnosis.submitDiagnosisAsync(request); return result; } /// 创建健康检测(在线) @override Future syncPatientAndDiagnosisData( SyncPatientAndDiagnosisDataRequest request) async { try { var result = await rpc.vitalDiagnosis.syncPatientAndDiagnosisDataAsync( request, ); if (result) { final code = request.cardNo!; var existEntity = await _checkPatientExist(code); if (existEntity != null) { existEntity.syncState = OfflineDataSyncState.success; existEntity.diagnosisCount -= 1; // 待上传检测条数减一 if (existEntity.diagnosisCount == 0 && existEntity.examCount == 0 && existEntity.gxyFollowUpCount == 0 && existEntity.tnbFollowUpCount == 0) { // 要其他数据也上传完成,才能更新overallSyncState为成功 existEntity.overallSyncState = OfflineDataSyncState.success; } await db.repositories.patient.update(existEntity); } } return result; } catch (e) { logger.e( "syncPatientAndDiagnosisData - Check patient exist error. Code: ${request.cardNo}.", e); return false; } } Future _checkPatientExist(String code) async { try { final entity = await db.repositories.patient .singleByCodeWithUserCode(code, Store.user.userCode!); return entity; } catch (e) { logger.e( "PatientServiceMock - Check patient exist error. Code: $code.", e); } return null; } ///获取所有userCode为空的数据,并赋值 @override Future resettingUsercodeIsEmptyData() async { if (kIsWeb) return; var version = await db.database.getVersion(); if (version > 0) { final list = await db.repositories.diagnosis.queryable.where((x) { final List arr = []; arr.add(x.isValid.equals(true)); arr.add(x.userCode.equals('')); return arr; }).toList(); logger.w( "DiagnosisManager resettingUsercodeIsEmptyData list.count:${list.length}."); for (var element in list) { if (element.syncState == OfflineDataSyncState.success) { var result = await getDiagnosisAggregationPageAsync( element.patientCode, 1, 1000); if (result != null && result.pageData != null) { var diagnosisAggregationRecord = result.pageData! .firstWhereOrNull((x) => x.appDataId == element.code); if (diagnosisAggregationRecord != null) { element.userCode = diagnosisAggregationRecord.doctorCode ?? ''; } } } else { element.userCode = Store.user.userCode!; } await db.repositories.diagnosis.update(element); } } } @override Future>> getTableData(DiagnosisAggregationRecordModel dto, {bool isLastRecord = false}) async { var currentDiagnosis = >[]; var index = 1; for (var element in dto.diagnosisAggregationData!) { if (element.diagnosisData != "null") { var jsonData = json.decode(element.diagnosisData!); List keys = jsonData.keys.toList(); List? dtos = []; if (!kIsOnline) { for (var key in keys) { dtos.add(DictionaryWithUnitDTO( key: key, name: DiagnosisTranslator.reportTr(key), unit: DiagnosisTranslator.getExamUnit(key))); } } else { dtos = await Get.find() .getDictionaryNameAndUnitByKeysAsync(keys) ?? []; } for (var key in keys) { if (key == "ECG_POINT" || key == "ECG_POINT12" || key == "ECG12") { continue; } var dto = dtos.firstWhereOrNull((item) => item.key == key); final value = jsonData[key].toString(); if (value.isEmpty) { // 不展示空值 continue; } currentDiagnosis.add([ if (!isLastRecord) (index++).toString(), dto?.name ?? '', value, dto?.unit ?? '', ]); } } } return currentDiagnosis; } @override Future removeDiagnosis(String appDataId) async { try { bool result = false; final entity = await db.repositories.diagnosis.singleByCode(appDataId); if (entity != null) { result = await db.repositories.diagnosis.delete(entity.id); if (!result) { return false; } } if (kIsOnline) { result = await rpc.vitalDiagnosis.removeDiagnosisByAppDataIdAsync( RemoveDiagnosisByAppDataIdRequest( token: Store.user.token, appDataId: appDataId, ), ); } return result; } catch (e) { logger.e("DiagnosisManager remove diagnosis-$appDataId error.", e); return false; } } @override Future getLastRecordInfo() async { try { if (Store.user.currentSelectPatientInfo?.code != null) { List getRecords = []; logger.i('onReadLastRecordInfo: 开始获取 最近检测记录.'); /// 这是在线的 if (kIsOnline) { var getLastRecords = await getDiagnosisAggregationPageAsync( Store.user.currentSelectPatientInfo!.code!, 1, 10); if (getLastRecords != null) { getRecords.addAll(getLastRecords.pageData ?? []); logger.i( 'PatientDetailController onReadLastRecordInfo getLastRecords.length:${getLastRecords.dataCount}.'); } else { logger.i( 'PatientDetailController onReadLastRecordInfo no online data available.'); } } /// 这是离线的 var listRecords = await getListByPatientCode( Store.user.currentSelectPatientInfo!.code!); if (listRecords != null) { getRecords.addAll(listRecords); logger.i( 'PatientDetailController onReadLastRecordInfo listRecords.length:${listRecords.length}.'); } else { logger.i( 'PatientDetailController onReadLastRecordInfo no local data available.'); } if (getRecords.isNotEmpty) { var lastRecordInfo = getRecords.reduce((curr, next) => (curr.diagnosisTime!).isAfter(next.diagnosisTime!) ? curr : next); logger.i('onReadLastRecordInfo: ${lastRecordInfo.toJson()}'); return lastRecordInfo; } else { logger.i('onReadLastRecordInfo: empty'); } } } catch (e) { logger.e('PatientDetailController onReadLastRecordInfo.', e); } return null; } }