Browse Source

fix followup offline sync state bug

Melon 1 year ago
parent
commit
9680e7e5b6
4 changed files with 74 additions and 12 deletions
  1. 3 2
      lib/database/db.dart
  2. 2 0
      lib/managers/account.dart
  3. 64 8
      lib/managers/data_sync.dart
  4. 5 2
      lib/mock/services/followup.dart

+ 3 - 2
lib/database/db.dart

@@ -59,8 +59,9 @@ class VitalDatabaseAccessor
     // database.execute("DROP TABLE ${FollowUpEntity().tableName};");
     // database.execute("DROP TABLE ${ExamBatchEntity().tableName};");
     // database.execute("DROP TABLE ${ExamEntity().tableName};");
-    // database.execute(
-    //     "ALTER TABLE patients ADD COLUMN orgCode VARCHAR(100) NOT NULL DEFAULT '';");
+    database.execute("DELETE FROM diagnosis WHERE 1=1;");
+    database.execute("DELETE FROM followup WHERE 1=1;");
+    database.execute("DELETE FROM patients WHERE 1=1;");
   }
 
   ///检查本地数据库版本,以判断是否需要升级

+ 2 - 0
lib/managers/account.dart

@@ -35,6 +35,8 @@ class AccountManager implements IAccountManager {
           if (e.message?.toLowerCase().contains("timeout") == true) {
             code = -1002; // 超时
           }
+        } else if (e.data?.code != null) {
+          code = e.data.code!;
         } else if (e.code != null) {
           code = e.code!;
         }

+ 64 - 8
lib/managers/data_sync.dart

@@ -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;
+  }
 }

+ 5 - 2
lib/mock/services/followup.dart

@@ -60,12 +60,15 @@ class FollowUpServiceMock extends VitalFollowUpService {
     entity.typeKey = request.key!;
     entity.mode = request.followUpMode!;
     entity.followUpTime = request.followUpTime!;
-    entity.nextFollowUpTime = request.nextFollowUpTime!;
+    if (request.nextFollowUpTime != null) {
+      // TODO: 理论不能为空,但是这个默认值目前没有确定
+      entity.nextFollowUpTime = request.nextFollowUpTime!;
+    }
     entity.followUpPhtots = request.followUpPhotos!;
     entity.dataJson = request.followUpData!;
 
     int result;
-    if (entity.id == 0) {
+    if (entity.id == -1) {
       result = await db.repositories.followUp.insert(entity);
       if (result > 0) {
         // 统计数量+1