db_util.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:eitc_erm_dental_flutter/entity/db/local_patient_info.dart';
  2. import 'package:eitc_erm_dental_flutter/sp_util.dart';
  3. import 'package:isar/isar.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. ///数据库工具
  6. class DbUtil {
  7. static DbUtil? _instance;
  8. static DbUtil get instance => _instance ??= DbUtil._();
  9. late Isar _isar;
  10. DbUtil._();
  11. ///初始化
  12. Future<dynamic> init() async {
  13. _isar = await Isar.open([LocalPatientInfoSchema],
  14. name: "dental",
  15. directory: (await getApplicationSupportDirectory()).path);
  16. return true;
  17. }
  18. ///获取咨询人列表
  19. Future<List<LocalPatientInfo>> getLocalPatientList() async {
  20. String userId = await SpUtil.getUserId();
  21. return _isar.localPatientInfos.filter().userIdEqualTo(userId).findAll();
  22. }
  23. ///添加或更新单个咨询人
  24. Future<Id> putLocalPatient(LocalPatientInfo info) {
  25. return _isar.writeTxn(() => _isar.localPatientInfos.put(info));
  26. }
  27. ///添加或更新多个咨询人
  28. Future<List<int>> putLocalPatients(List<LocalPatientInfo> list) {
  29. return _isar.writeTxn(() => _isar.localPatientInfos.putAll(list));
  30. }
  31. ///删除所有咨询人
  32. Future<int> deleteAllLocalPatients() async {
  33. String userId = await SpUtil.getUserId();
  34. return _isar.writeTxn(() =>
  35. _isar.localPatientInfos.filter().userIdEqualTo(userId).deleteAll());
  36. }
  37. ///删除咨询人
  38. Future<bool> deleteLocalPatinetById(Id id) {
  39. return _isar.writeTxn(() => _isar.localPatientInfos.delete(id));
  40. }
  41. ///根据服务器ID查找咨询人
  42. Future<LocalPatientInfo?> getLocalPatinetByServerId(String serverId) {
  43. return _isar.localPatientInfos
  44. .filter()
  45. .serverIdEqualTo(serverId)
  46. .findFirst();
  47. }
  48. ///查找咨询人
  49. Future<LocalPatientInfo?> getLocalPatinet(
  50. String name, String idCard, String relation) async {
  51. String userId = await SpUtil.getUserId();
  52. return _isar.localPatientInfos
  53. .filter()
  54. .userIdEqualTo(userId)
  55. .and()
  56. .nameEqualTo(name)
  57. .and()
  58. .idCardEqualTo(idCard)
  59. .and()
  60. .relationEqualTo(relation)
  61. .findFirst();
  62. }
  63. ///根据ID获取咨询人
  64. Future<LocalPatientInfo?> getLocalPatientById(Id id) {
  65. return _isar.localPatientInfos.get(id);
  66. }
  67. }