|
@@ -2,7 +2,10 @@ import 'dart:convert';
|
|
|
|
|
|
import 'package:fis_jsonrpc/rpc.dart';
|
|
|
import 'package:fis_jsonrpc/services/patient.m.dart';
|
|
|
+import 'package:get/get.dart';
|
|
|
+import 'package:uuid/uuid.dart';
|
|
|
import 'package:vital_local_database/core/interface/queryable.dart';
|
|
|
+import 'package:vitalapp/architecture/storage/text_storage.dart';
|
|
|
import 'package:vitalapp/database/db.dart';
|
|
|
import 'package:vitalapp/database/entities/defines.dart';
|
|
|
import 'package:vitalapp/database/entities/diagnosis.dart';
|
|
@@ -16,6 +19,8 @@ import 'package:vitalapp/store/store.dart';
|
|
|
import 'interfaces/data_sync.dart';
|
|
|
import 'package:fis_common/logger/logger.dart';
|
|
|
|
|
|
+import 'interfaces/record_data_cache.dart';
|
|
|
+
|
|
|
class DataSyncManager implements IDataSyncManager {
|
|
|
String get userCode => Store.user.userCode!;
|
|
|
|
|
@@ -173,8 +178,9 @@ class DataSyncManager implements IDataSyncManager {
|
|
|
final list = await db.repositories.diagnosis
|
|
|
.getNotUploadedListByPatientCode(patientCode, userCode);
|
|
|
int count = 0;
|
|
|
+ final appDataId = await _getDiagnosisAppDataId(patientCode);
|
|
|
for (var entity in list) {
|
|
|
- final success = await _syncSingleDiagnosis(entity);
|
|
|
+ final success = await _syncSingleDiagnosis(appDataId, entity);
|
|
|
if (success) {
|
|
|
count++;
|
|
|
}
|
|
@@ -299,8 +305,9 @@ class DataSyncManager implements IDataSyncManager {
|
|
|
try {
|
|
|
if (entity.syncType == OfflineDataSyncType.create) {
|
|
|
final request = CreateFollowUpRequest(
|
|
|
+ token: Store.user.token,
|
|
|
key: entity.typeKey,
|
|
|
- patientCode: entity.code,
|
|
|
+ patientCode: entity.patientCode,
|
|
|
templateCode: entity.templateCode,
|
|
|
followUpData: entity.dataJson,
|
|
|
followUpTime: entity.followUpTime,
|
|
@@ -311,11 +318,11 @@ class DataSyncManager implements IDataSyncManager {
|
|
|
final code = await rpc.followUp.createFollowUpAsync(request);
|
|
|
result = code.isNotEmpty;
|
|
|
if (result) {
|
|
|
- entity.code = code;
|
|
|
- await db.repositories.followUp.update(entity); // 更新真实Code
|
|
|
+ entity.code = code; // 更新真实Code
|
|
|
}
|
|
|
} else {
|
|
|
final request = UpdateFollowUpRequest(
|
|
|
+ token: Store.user.token,
|
|
|
key: entity.typeKey,
|
|
|
followUpData: entity.dataJson,
|
|
|
followUpTime: entity.followUpTime,
|
|
@@ -326,6 +333,13 @@ class DataSyncManager implements IDataSyncManager {
|
|
|
);
|
|
|
result = await rpc.followUp.updateFollowUpAsync(request);
|
|
|
}
|
|
|
+ if (result) {
|
|
|
+ entity.syncState = OfflineDataSyncState.success;
|
|
|
+ } else {
|
|
|
+ entity.syncState = OfflineDataSyncState.fail;
|
|
|
+ }
|
|
|
+ final updateRows = await db.repositories.followUp.update(entity);
|
|
|
+ result = updateRows > 0;
|
|
|
} catch (e) {
|
|
|
logger.e(
|
|
|
"DataSyncManager_syncSingleFollowUp error. id: ${entity.id}.", e);
|
|
@@ -333,18 +347,60 @@ class DataSyncManager implements IDataSyncManager {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- Future<bool> _syncSingleDiagnosis(DiagnosisEntity entity) async {
|
|
|
+ Future<bool> _syncSingleDiagnosis(
|
|
|
+ String appDataId, DiagnosisEntity entity) async {
|
|
|
+ final currPatient = Store.user.currentSelectPatientInfo;
|
|
|
+ if (currPatient == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
bool result = false;
|
|
|
try {
|
|
|
- if (entity.syncType == OfflineDataSyncType.create) {
|
|
|
-// TODO OFFLINE MELON
|
|
|
+ final valuesMap = jsonDecode(entity.dataJson);
|
|
|
+ List<DiagnosisItem> diagnosisItems =
|
|
|
+ await Get.find<IRecordDataCacheManager>()
|
|
|
+ .verifyDiagnosisDataList(valuesMap);
|
|
|
+ // 目前不存在更新
|
|
|
+ final request = SyncPatientAndDiagnosisDataRequest(
|
|
|
+ token: Store.user.token,
|
|
|
+ patientCode: currPatient.code,
|
|
|
+ patientName: currPatient.patientName ?? '',
|
|
|
+ patientAddress: currPatient.patientAddress ?? '',
|
|
|
+ patientGender: currPatient.patientGender,
|
|
|
+ permanentResidenceAddress: currPatient.permanentResidenceAddress,
|
|
|
+ phone: currPatient.phone,
|
|
|
+ cardNo: currPatient.cardNo,
|
|
|
+ nationality: currPatient.nationality,
|
|
|
+ birthday: currPatient.birthday,
|
|
|
+ crowdLabels: currPatient.crowdLabels,
|
|
|
+ cardType: currPatient.cardType,
|
|
|
+ contractedDoctor: currPatient.contractedDoctor,
|
|
|
+ appDataId: appDataId,
|
|
|
+ diagnosisTime: DateTime.now(),
|
|
|
+ diagnosisItems: diagnosisItems,
|
|
|
+ );
|
|
|
+ result = await rpc.diagnosis.syncPatientAndDiagnosisDataAsync(request);
|
|
|
+ if (result) {
|
|
|
+ entity.syncState = OfflineDataSyncState.success;
|
|
|
} else {
|
|
|
-// TODO OFFLINE MELON
|
|
|
+ entity.syncState = OfflineDataSyncState.fail;
|
|
|
}
|
|
|
+ final updateRows = await db.repositories.diagnosis.update(entity);
|
|
|
+ result = updateRows > 0;
|
|
|
} catch (e) {
|
|
|
logger.e(
|
|
|
"DataSyncManager_syncSingleDiagnosis error. id: ${entity.id}.", e);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ Future<String> _getDiagnosisAppDataId(String patientCode) async {
|
|
|
+ // TODO 待封装
|
|
|
+ TextStorage cachedRecord = TextStorage(
|
|
|
+ fileName: 'appDataId',
|
|
|
+ directory: "patient/$patientCode",
|
|
|
+ );
|
|
|
+ String? appDataId = await cachedRecord.read();
|
|
|
+ appDataId ??= const Uuid().v4().replaceAll('-', '');
|
|
|
+ return appDataId;
|
|
|
+ }
|
|
|
}
|