patient.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.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/global.dart';
  8. import 'package:vitalapp/managers/interfaces/models/patient_model_dto.dart';
  9. import 'package:vitalapp/managers/interfaces/patient.dart';
  10. import 'package:vitalapp/mock/services/patient.dart';
  11. import 'package:vitalapp/rpc.dart';
  12. import 'package:fis_common/logger/logger.dart';
  13. import 'package:vitalapp/store/store.dart';
  14. class PatientManager implements IPatientManager {
  15. @override
  16. Future<String?> create(CreatePatientRequest request) async {
  17. request.token = Store.user.token;
  18. request.code = request.cardNo;
  19. final result = await rpc.patient.createPatientAsync(request);
  20. return result;
  21. }
  22. @override
  23. Future<List<PatientModelDTO>?> getPagedOfflineList(
  24. PatientPageRequest request) async {
  25. final entities = await db.repositories.patient.queryable
  26. .where((x) {
  27. final List<IDbColumnCondition> arr = [];
  28. arr.add(x.isValid.equals(true));
  29. arr.add(x.userCode.equals(Store.user.userCode)); //添加用户code
  30. arr.add(x.syncState.notEquals(OfflineDataSyncState.success));
  31. final keyword = request.keyword;
  32. if (keyword != null && keyword.isNotEmpty) {
  33. // TODO: 暂时只支持姓名模糊
  34. arr.add(x.name.like("%$keyword%' OR code like '%$keyword%"));
  35. } else {
  36. if (request.startTime != null) {
  37. arr.add(x.createTime.gte(request.startTime));
  38. }
  39. if (request.endTime != null) {
  40. arr.add(x.createTime.lt(request.endTime));
  41. }
  42. }
  43. return arr;
  44. })
  45. .orderBy((x) => x.createTime, DbOrderByType.desc)
  46. .toList();
  47. final dtos = entities.map((e) {
  48. var patientModel = PatientModelDTO.fromJson(jsonDecode(e.dataJson));
  49. patientModel.isExistLocalData = true;
  50. return patientModel;
  51. }).toList();
  52. return dtos;
  53. }
  54. @override
  55. Future<PatientDTO?> getDetail(String code) async {
  56. try {
  57. final request = GetPatientRequest(code: code, token: Store.user.token);
  58. if (kIsOnline) {
  59. //在线状态,先查询是否存在本体未提交数据
  60. final entity = await db.repositories.patient
  61. .singleByCode(code, Store.user.userCode!);
  62. if (entity != null &&
  63. entity.dataJson.isNotEmpty &&
  64. entity.syncState != OfflineDataSyncState.success) {
  65. logger.i("PatientManager query patient detail location exist.");
  66. final jsonMap = jsonDecode(entity.dataJson);
  67. final dto = PatientDTO.fromJson(jsonMap);
  68. return dto;
  69. } else {
  70. logger.i("PatientManager query patient detail location not exist.");
  71. }
  72. }
  73. final result = await rpc.patient.getPatientDetailAsync(request);
  74. return result;
  75. } catch (e) {
  76. logger.e("PatientManager query patient detail error.", e);
  77. return null;
  78. }
  79. }
  80. @override
  81. Future<PageCollection<PatientModelDTO>?> getPagedList(
  82. PatientPageRequest request) async {
  83. PageCollection<PatientModelDTO>? listPatientDto =
  84. PageCollection<PatientModelDTO>();
  85. try {
  86. request.token = Store.user.token;
  87. final result = await rpc.patient.getPatientPageAsync(request);
  88. listPatientDto.dataCount = result.dataCount;
  89. listPatientDto.pageData = [];
  90. for (var element in result.pageData!) {
  91. var patientModel = PatientModelDTO.fromJson(element.toJson());
  92. var isNotUploadedPatient = await db.repositories.patient
  93. .isNotUploadedPatient(element.code!, Store.user.userCode!);
  94. if (isNotUploadedPatient) {
  95. if (kIsOnline) {
  96. continue;
  97. } else {
  98. patientModel.isExistLocalData = true;
  99. }
  100. } else {
  101. final localCount = await db.repositories.diagnosis
  102. .getNotUploadedCountByPatientCode(
  103. element.code!, Store.user.userCode!);
  104. if (localCount > 0) {
  105. patientModel.isExistLocalData = true;
  106. }
  107. }
  108. listPatientDto.pageData?.add(patientModel);
  109. }
  110. return listPatientDto;
  111. } catch (e) {
  112. logger.e("PatientManager query patient paged list error.", e);
  113. return null;
  114. }
  115. }
  116. @override
  117. Future<bool> updatePatientAsync(UpdatePatientRequest request) async {
  118. try {
  119. if (kIsOnline) {
  120. await _updateLocationPatientAsync(request);
  121. }
  122. final result = await rpc.patient.updatePatientAsync(request);
  123. return result;
  124. } catch (e) {
  125. return false;
  126. }
  127. }
  128. ///在线编辑居民信息时,同步更改本地缓存
  129. Future<bool> _updateLocationPatientAsync(UpdatePatientRequest request) async {
  130. final code = request.code!;
  131. final existEntity = await _checkPatientExist(code);
  132. PatientEntity entity;
  133. PatientDTO dto;
  134. if (existEntity == null) {
  135. entity = PatientEntity();
  136. entity.code = request.code!;
  137. entity.isValid = true;
  138. entity.name = request.patientName!;
  139. entity.userCode = Store.user.userCode!; //添加用户code
  140. entity.syncState = OfflineDataSyncState.success;
  141. dto = PatientDTO();
  142. ModelConverter.syncUpdate2Dto(dto, request);
  143. final dtoJson = dto.toJson();
  144. entity.dataJson = jsonEncode(dtoJson);
  145. final id = await db.repositories.patient.insert(entity);
  146. return id > 0;
  147. } else {
  148. entity = existEntity;
  149. final jsonMap = jsonDecode(entity.dataJson);
  150. dto = PatientDTO.fromJson(jsonMap);
  151. ModelConverter.syncUpdate2Dto(dto, request);
  152. final dtoJson = dto.toJson();
  153. entity.dataJson = jsonEncode(dtoJson);
  154. final rows = await db.repositories.patient.update(entity);
  155. return rows > 0;
  156. }
  157. }
  158. Future<PatientEntity?> _checkPatientExist(String code) async {
  159. try {
  160. final entity = await db.repositories.patient
  161. .singleByCode(code, Store.user.userCode!);
  162. return entity;
  163. } catch (e) {
  164. logger.e(
  165. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  166. }
  167. return null;
  168. }
  169. @override
  170. Future<bool> setCrowdLabelsAsync(
  171. String patientCode, List<String> crowLabels) async {
  172. try {
  173. final request = SetCrowdLabelsRequest(
  174. code: patientCode, crowdLabels: crowLabels, token: Store.user.token);
  175. final result = rpc.patient.setCrowdLabelsAsync(request);
  176. return result;
  177. } catch (e) {
  178. return false;
  179. }
  180. }
  181. @override
  182. Future<String> createPatientExtensionAsync(
  183. CreatePatientExtensionRequest request) async {
  184. try {
  185. final result =
  186. await rpc.patientExtension.createPatientExtensionAsync(request);
  187. return result;
  188. } catch (e) {
  189. return '';
  190. }
  191. }
  192. @override
  193. Future<PatientExtensionDTO?> getPatientExtensionDetailByCodeAndKeyAsync(
  194. GetPatientExtensionByCodeAndKeyRequest request) async {
  195. try {
  196. final result = await rpc.patientExtension
  197. .getPatientExtensionDetailByCodeAndKeyAsync(request);
  198. return result;
  199. } catch (e) {
  200. return null;
  201. }
  202. }
  203. @override
  204. ///更新居民健康信息
  205. Future<bool> updatePatientExtensionAsync(
  206. UpdatePatientExtensionRequest request) async {
  207. try {
  208. final result =
  209. await rpc.patientExtension.updatePatientExtensionAsync(request);
  210. return result;
  211. } catch (e) {}
  212. return false;
  213. }
  214. ///获取所有userCode为空的数据,并赋值
  215. @override
  216. Future<void> resettingUsercodeIsEmptyData() async {
  217. var version = await db.database.getVersion();
  218. if (version > 0) {
  219. final list = await db.repositories.patient.queryable.where((x) {
  220. final List<IDbColumnCondition> arr = [];
  221. arr.add(x.isValid.equals(true));
  222. arr.add(x.userCode.equals(''));
  223. return arr;
  224. }).toList();
  225. logger.w(
  226. "PatientManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  227. for (var element in list) {
  228. if (element.syncState == OfflineDataSyncState.success) {
  229. var result = await getDetail(element.code);
  230. if (result != null) {
  231. element.userCode = result.createdDoctor!;
  232. }
  233. } else {
  234. element.userCode = Store.user.userCode!;
  235. }
  236. await db.repositories.patient.update(element);
  237. }
  238. }
  239. }
  240. }