patient.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:vital_local_database/core/index.dart';
  2. import 'package:vitalapp/database/entities/defines.dart';
  3. import 'package:vitalapp/database/entities/patient.dart';
  4. import 'interfaces/patient.dart';
  5. enum _FunctionCategory {
  6. exam,
  7. followUp,
  8. diagnosis,
  9. }
  10. class PatientRepository
  11. extends BaseDbRepository<PatientEntity, PatientColumnsDefine>
  12. implements IPatientRepository {
  13. PatientRepository(super.database);
  14. @override
  15. PatientEntity createEntityInstance() => PatientEntity();
  16. @override
  17. Future<PatientEntity?> singleByCode(String code) async {
  18. final entity = await queryable
  19. .where(
  20. (x) => [
  21. x.isValid.equals(true),
  22. x.code.equals(code),
  23. ],
  24. )
  25. .first();
  26. return entity;
  27. }
  28. @override
  29. Future<PatientEntity?> singleByCodeWithUserCode(
  30. String code,
  31. String userCode,
  32. ) async {
  33. final entity = await queryable
  34. .where((x) => [
  35. x.isValid.equals(true),
  36. x.code.equals(code),
  37. x.userCode.equals(userCode), //添加用户Code
  38. ])
  39. .first();
  40. return entity;
  41. }
  42. @override
  43. Future<PatientEntity?> singleByCodeWithOrgCode(
  44. String code,
  45. String orgCode,
  46. ) async {
  47. final entity = await queryable
  48. .where((x) => [
  49. x.isValid.equals(true),
  50. x.code.equals(code),
  51. x.orgCode.equals(orgCode),
  52. ])
  53. .first();
  54. return entity;
  55. }
  56. @override
  57. Future<bool> isNotUploadedPatient(String code) async {
  58. final entity = await singleByCode(code);
  59. return entity != null &&
  60. (entity.overallSyncState != OfflineDataSyncState.success ||
  61. entity.syncState != OfflineDataSyncState.success);
  62. }
  63. @override
  64. Future<bool> increaseFollowUpCount(String code) =>
  65. _updateCountByCategory(code, _FunctionCategory.followUp, 1);
  66. @override
  67. Future<bool> increaseExamCount(String code) =>
  68. _updateCountByCategory(code, _FunctionCategory.exam, 1);
  69. @override
  70. Future<bool> increaseDiagnoissCount(String code) =>
  71. _updateCountByCategory(code, _FunctionCategory.diagnosis, 1);
  72. @override
  73. Future<bool> decreaseFollowUpCount(String code) =>
  74. _updateCountByCategory(code, _FunctionCategory.followUp, -1);
  75. @override
  76. Future<bool> decreaseExamCount(String code) =>
  77. _updateCountByCategory(code, _FunctionCategory.exam, -1);
  78. @override
  79. Future<bool> decreaseDiagnoissCount(String code) =>
  80. _updateCountByCategory(code, _FunctionCategory.diagnosis, -1);
  81. /// 根据分类更新离线数量
  82. ///
  83. /// [num] 1 则加1,-1 则减1
  84. Future<bool> _updateCountByCategory(
  85. String code,
  86. _FunctionCategory category,
  87. int num,
  88. ) async {
  89. final entity = await singleByCode(code);
  90. if (entity == null) {
  91. return false;
  92. }
  93. switch (category) {
  94. case _FunctionCategory.exam:
  95. // TODO OFFLINE MELON : 待体检业务重新整理完成后实现
  96. return true;
  97. case _FunctionCategory.followUp:
  98. entity.followUpCount += num;
  99. break;
  100. case _FunctionCategory.diagnosis:
  101. entity.diagnosisCount += num;
  102. break;
  103. }
  104. if (num > 0) {
  105. entity.overallSyncState = OfflineDataSyncState.wait;
  106. }
  107. final result = await update(entity);
  108. return result > 0;
  109. }
  110. }