patient_view_model.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:eitc_erm_dental_flutter/db_util.dart';
  2. import 'package:eitc_erm_dental_flutter/entity/db/local_patient_info.dart';
  3. import 'package:eitc_erm_dental_flutter/entity/dict_info.dart';
  4. import 'package:eitc_erm_dental_flutter/entity/patient_info.dart';
  5. import 'package:eitc_erm_dental_flutter/exts.dart';
  6. import 'package:eitc_erm_dental_flutter/funcs.dart';
  7. import 'package:eitc_erm_dental_flutter/global.dart';
  8. import 'package:eitc_erm_dental_flutter/http/api_exception.dart';
  9. import 'package:eitc_erm_dental_flutter/http/api_service.dart';
  10. import 'package:eitc_erm_dental_flutter/http/http.dart';
  11. import 'package:eitc_erm_dental_flutter/sp_util.dart';
  12. import 'package:flutter/foundation.dart';
  13. import 'package:riverpod_annotation/riverpod_annotation.dart';
  14. part 'patient_view_model.g.dart';
  15. ///本地咨询人列表
  16. @riverpod
  17. class LocalPatientList extends _$LocalPatientList {
  18. @override
  19. FutureOr<List<LocalPatientInfo>> build() async {
  20. //未登录
  21. if (!hasToken) {
  22. return SynchronousFuture([]);
  23. }
  24. return DbUtil.instance.getLocalPatientList();
  25. }
  26. ///同步咨询人列表
  27. ///
  28. /// [list] 用来同步的服务器咨询人列表,如果为空就会先从服务器读取列表
  29. void syncPatientList([List<PatientInfo>? list]) async {
  30. //未登录
  31. if (!hasToken) {
  32. return;
  33. }
  34. state = AsyncLoading();
  35. try {
  36. //服务器列表
  37. List<PatientInfo> serverList;
  38. //如果list为空就从服务器读取
  39. list ??= await _getPaitienInfoList();
  40. //如果list仍然为空,表示从服务器获取列表出了异常,就返回
  41. if (list == null) {
  42. ref.invalidateSelf();
  43. return;
  44. }
  45. serverList = list;
  46. List<LocalPatientInfo> localList =
  47. await DbUtil.instance.getLocalPatientList();
  48. //服务器列表为空
  49. if (serverList.isEmpty) {
  50. logd("同步咨询人列表,服务器列表为空");
  51. await DbUtil.instance.deleteAllLocalPatients();
  52. setSelectedPatientId(-1);
  53. ref.invalidateSelf();
  54. return;
  55. }
  56. //转换服务器列表到本地列表方法
  57. Future<List<LocalPatientInfo>> convertList(
  58. List<PatientInfo> toConvert) async {
  59. String userId = await SpUtil.getUserId();
  60. return toConvert
  61. .map((info) => LocalPatientInfo(
  62. name: info.patientName,
  63. //加密身份证
  64. idCard: aesEncrypt(info.identificationCard ?? ""),
  65. relation: info.relationship,
  66. userId: userId,
  67. namePic: info.patientNamePic,
  68. serverId: info.appUserPatientsId))
  69. .toList();
  70. }
  71. //本地列表为空
  72. if (localList.isEmpty) {
  73. logd("同步咨询人列表,本地列表为空");
  74. List<int> ids = await DbUtil.instance
  75. .putLocalPatients(await convertList(serverList));
  76. setSelectedPatientId(ids.first);
  77. ref.invalidateSelf();
  78. return;
  79. }
  80. //同步服务器列表和本地列表
  81. //本地已选择的咨询人信息
  82. LocalPatientInfo? selectedInfo =
  83. localList.where((info) => info.id == selectedPatientId).firstOrNull;
  84. //服务器列表转换为本地列表
  85. List<LocalPatientInfo> newList = await convertList(serverList);
  86. int index = -1;
  87. //如果已选择的信息不为空,则找到新本地列表里相同数据的信息的索引
  88. if (selectedInfo != null) {
  89. logd("同步咨询人列表,存在已选择的咨询人信息");
  90. index = newList
  91. .indexWhere((info) => info.serverId == selectedInfo.serverId);
  92. }
  93. //删除所有的本地数据
  94. await DbUtil.instance.deleteAllLocalPatients();
  95. //把服务器数据保存到本地
  96. List<int> ids = await DbUtil.instance.putLocalPatients(newList);
  97. //检查已选择的信息的ID并更新
  98. int id = (index >= 0 && index < ids.length) ? ids[index] : -1;
  99. setSelectedPatientId(id);
  100. ref.invalidateSelf();
  101. } catch (e) {
  102. //记录日志,不设置为error状态
  103. loge("同步咨询人数据异常", error: e, stackTrace: StackTrace.current);
  104. ref.invalidateSelf();
  105. }
  106. }
  107. }
  108. ///咨询人列表
  109. @riverpod
  110. class PatientList extends _$PatientList {
  111. @override
  112. FutureOr<List<PatientInfo>> build() async {
  113. try {
  114. return await _getPaitienInfoList() ?? [];
  115. } catch (e) {
  116. throw ApiException.from(e);
  117. }
  118. }
  119. ///添加咨询人
  120. Future<bool> addPatient(String name, String idCard, String relation) async {
  121. try {
  122. await Http.instance.request(
  123. ApiService(Http.instance.dio).addPatient(name, idCard, relation));
  124. } catch (e) {
  125. loge("增加咨询人异常", error: e);
  126. return false;
  127. }
  128. return true;
  129. }
  130. }
  131. ///获取咨询人列表
  132. Future<List<PatientInfo>?> _getPaitienInfoList() async {
  133. try {
  134. List<PatientInfo>? list = await Http.instance
  135. .request(ApiService(Http.instance.dio).getPatientList());
  136. return list;
  137. } catch (e) {
  138. loge("读取咨询人列表异常", error: e);
  139. }
  140. return null;
  141. }
  142. ///咨询人关系列表
  143. @riverpod
  144. Future<List<String>> patientRelationList(PatientRelationListRef ref) async {
  145. try {
  146. List<DictInfo>? list = await Http.instance.request(
  147. ApiService(Http.instance.dio).dictList("erm_personal_relationship"));
  148. if (list.isNullOrEmpty) {
  149. return [];
  150. }
  151. return list!
  152. .where((info) => !info.dictValue.isNullOrEmpty)
  153. .map((info) => info.dictValue!)
  154. .toList();
  155. } catch (e) {
  156. loge("获取咨询人关系列表异常", error: e);
  157. }
  158. return [];
  159. }