data_sync.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/services/patient.m.dart';
  3. import 'package:vital_local_database/core/interface/queryable.dart';
  4. import 'package:vitalapp/database/db.dart';
  5. import 'package:vitalapp/database/entities/defines.dart';
  6. import 'package:vitalapp/database/entities/patient.dart';
  7. import 'package:vitalapp/managers/adapters/offline/patient.dart';
  8. import 'package:vitalapp/managers/interfaces/models/common.dart';
  9. import 'package:vitalapp/rpc.dart';
  10. import 'package:vitalapp/store/store.dart';
  11. import 'interfaces/data_sync.dart';
  12. import 'package:fis_common/logger/logger.dart';
  13. class DataSyncManager implements IDataSyncManager {
  14. @override
  15. Future<PagedDataCollection<PatientEntity>> getPatientPagedList(
  16. int pageIndex, {
  17. int pageSize = 50,
  18. OfflineDataSyncState? syncState,
  19. }) async {
  20. try {
  21. final query = db.repositories.patient.queryable.where((x) {
  22. final list = <IDbColumnCondition>[];
  23. list.add(x.isValid.equals(true));
  24. list.add(x.userCode.equals(Store.user.userCode)); //添加用户code
  25. if (syncState != null) {
  26. list.add(x.syncState.equals(syncState));
  27. }
  28. return list;
  29. });
  30. final count = await query.count();
  31. final offset = pageSize * (pageIndex - 1);
  32. final entities = await query.offset(offset).limit(pageSize).toList();
  33. return PagedDataCollection<PatientEntity>(
  34. totalCount: count,
  35. pageIndex: pageIndex,
  36. pageSize: pageSize,
  37. data: entities,
  38. );
  39. } catch (e) {
  40. logger.e("DataSyncManager getPatientPagedList error.", e);
  41. return PagedDataCollection<PatientEntity>(
  42. totalCount: 0,
  43. pageIndex: pageIndex,
  44. pageSize: pageSize,
  45. data: [],
  46. );
  47. }
  48. }
  49. @override
  50. Future<bool> syncPatient(PatientEntity entity) async {
  51. try {
  52. final jsonMap = jsonDecode(entity.dataJson);
  53. final dto = PatientDTO.fromJson(jsonMap);
  54. bool syncResult = false;
  55. if (entity.syncType == OfflineDataSyncType.create) {
  56. final request = PatientDtoConverter.dto2Create(dto);
  57. request.token = Store.user.token;
  58. final code = await rpc.patient.createPatientAsync(request);
  59. syncResult = code.isNotEmpty;
  60. } else {
  61. final request = PatientDtoConverter.dto2Update(dto);
  62. request.token = Store.user.token;
  63. syncResult = await rpc.patient.updatePatientAsync(request);
  64. }
  65. entity.syncState =
  66. syncResult ? OfflineDataSyncState.success : OfflineDataSyncState.fail;
  67. await db.repositories.patient.update(entity);
  68. return syncResult;
  69. } catch (e) {
  70. logger.e("DataSyncManager syncPatient error.", e);
  71. return false;
  72. }
  73. }
  74. }