patient.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. gxyFollowUp,
  8. tnbFollowUp,
  9. diagnosis,
  10. tcmConsitution,
  11. }
  12. class PatientRepository
  13. extends BaseDbRepository<PatientEntity, PatientColumnsDefine>
  14. implements IPatientRepository {
  15. PatientRepository(super.database);
  16. @override
  17. PatientEntity createEntityInstance() => PatientEntity();
  18. @override
  19. Future<PatientEntity?> singleByCode(String code) async {
  20. final entity = await queryable
  21. .where(
  22. (x) => [
  23. x.isValid.equals(true),
  24. x.code.equals(code),
  25. ],
  26. )
  27. .first();
  28. return entity;
  29. }
  30. @override
  31. Future<PatientEntity?> singleByCodeWithUserCode(
  32. String code,
  33. String userCode,
  34. ) async {
  35. final entity = await queryable
  36. .where((x) => [
  37. x.isValid.equals(true),
  38. x.code.equals(code),
  39. x.userCode.equals(userCode), //添加用户Code
  40. ])
  41. .first();
  42. return entity;
  43. }
  44. @override
  45. Future<PatientEntity?> singleByCodeWithOrgCode(
  46. String code,
  47. String orgCode,
  48. ) async {
  49. final entity = await queryable
  50. .where((x) => [
  51. x.isValid.equals(true),
  52. x.code.equals(code),
  53. x.orgCode.equals(orgCode),
  54. ])
  55. .first();
  56. return entity;
  57. }
  58. @override
  59. Future<bool> isNotUploadedPatient(String code, String userCode) async {
  60. final entity = await singleByCodeWithUserCode(code, userCode);
  61. return entity != null &&
  62. (entity.overallSyncState != OfflineDataSyncState.success ||
  63. entity.syncState != OfflineDataSyncState.success);
  64. }
  65. @override
  66. Future<bool> increaseGxyFollowUpCount(String code, String userCode) =>
  67. _updateCountByCategory(code, userCode, _FunctionCategory.gxyFollowUp, 1);
  68. @override
  69. Future<bool> increaseTnbFollowUpCount(String code, String userCode) =>
  70. _updateCountByCategory(code, userCode, _FunctionCategory.tnbFollowUp, 1);
  71. @override
  72. Future<bool> increaseExamCount(String code, String userCode) =>
  73. _updateCountByCategory(code, userCode, _FunctionCategory.exam, 1);
  74. @override
  75. Future<bool> increaseDiagnoissCount(String code, String userCode) =>
  76. _updateCountByCategory(code, userCode, _FunctionCategory.diagnosis, 1);
  77. @override
  78. Future<bool> increaseTCMConsitutionCount(String code, String userCode) =>
  79. _updateCountByCategory(
  80. code, userCode, _FunctionCategory.tcmConsitution, 1);
  81. @override
  82. Future<bool> decreaseGxyFollowUpCount(String code, String userCode) =>
  83. _updateCountByCategory(code, userCode, _FunctionCategory.gxyFollowUp, -1);
  84. @override
  85. Future<bool> decreaseTnbFollowUpCount(String code, String userCode) =>
  86. _updateCountByCategory(code, userCode, _FunctionCategory.tnbFollowUp, -1);
  87. @override
  88. Future<bool> decreaseExamCount(String code, String userCode) =>
  89. _updateCountByCategory(code, userCode, _FunctionCategory.exam, -1);
  90. @override
  91. Future<bool> decreaseDiagnoissCount(String code, String userCode) =>
  92. _updateCountByCategory(code, userCode, _FunctionCategory.diagnosis, -1);
  93. @override
  94. Future<bool> decreaseTCMConsitutionCount(String code, String userCode) =>
  95. _updateCountByCategory(
  96. code, userCode, _FunctionCategory.tcmConsitution, -1);
  97. /// 根据分类更新离线数量
  98. ///
  99. /// [num] 1 则加1,-1 则减1
  100. Future<bool> _updateCountByCategory(
  101. String code,
  102. String userCode,
  103. _FunctionCategory category,
  104. int num,
  105. ) async {
  106. final entity = await singleByCodeWithUserCode(code, userCode);
  107. if (entity == null) {
  108. return false;
  109. }
  110. switch (category) {
  111. case _FunctionCategory.exam:
  112. entity.examCount += num;
  113. break;
  114. case _FunctionCategory.tcmConsitution:
  115. entity.tcmConsitutionCount += num;
  116. break;
  117. case _FunctionCategory.gxyFollowUp:
  118. entity.gxyFollowUpCount += num;
  119. break;
  120. case _FunctionCategory.tnbFollowUp:
  121. entity.tnbFollowUpCount += num;
  122. break;
  123. case _FunctionCategory.diagnosis:
  124. entity.diagnosisCount += num;
  125. break;
  126. }
  127. if (num > 0) {
  128. entity.overallSyncState = OfflineDataSyncState.wait;
  129. }
  130. final result = await update(entity);
  131. return result > 0;
  132. }
  133. }