patient.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:vital_local_database/core/interface/queryable.dart';
  5. import 'package:vitalapp/database/db.dart';
  6. import 'package:vitalapp/database/entities/defines.dart';
  7. import 'package:vitalapp/database/entities/patient.dart';
  8. import 'package:vitalapp/global.dart';
  9. import 'package:vitalapp/managers/interfaces/models/patient_model_dto.dart';
  10. import 'package:vitalapp/managers/interfaces/patient.dart';
  11. import 'package:vitalapp/mock/services/patient.dart';
  12. import 'package:vitalapp/rpc.dart';
  13. import 'package:fis_common/logger/logger.dart';
  14. import 'package:vitalapp/store/store.dart';
  15. class PatientManager implements IPatientManager {
  16. String? get userCode => Store.user.userCode;
  17. @override
  18. Future<String?> create(CreatePatientRequest2 request) async {
  19. request.token = Store.user.token;
  20. request.code = request.cardNo;
  21. final result = await rpc.vitalPatient.createPatientAsync(request);
  22. return result;
  23. }
  24. @override
  25. Future<List<PatientModelDTO>?> getPagedOfflineList(
  26. String? keyword,
  27. DateTime? startTime,
  28. DateTime? endTime,
  29. ) async {
  30. final isKeywordQuery = keyword != null && keyword.isNotEmpty;
  31. final query = db.repositories.patient.queryable.where((x) {
  32. final List<IDbColumnCondition> arr = [];
  33. arr.add(x.isValid.equals(true));
  34. arr.add(x.orgCode.equals(Store.user.organizationCode));
  35. arr.add(x.syncState.notEquals(OfflineDataSyncState.success));
  36. if (!isKeywordQuery) {
  37. if (startTime != null) {
  38. arr.add(x.createTime.gte(startTime));
  39. }
  40. if (endTime != null) {
  41. arr.add(x.createTime.lt(endTime));
  42. }
  43. }
  44. return arr;
  45. });
  46. if (isKeywordQuery) {
  47. // 姓名查询
  48. query.and((x) => x.name.like("%$keyword%"));
  49. // 身份照查询
  50. query.or((x) => x.code.like("%$keyword%"));
  51. }
  52. final entities =
  53. await query.orderBy((x) => x.createTime, DbOrderByType.desc).toList();
  54. final dtos = entities.map((e) {
  55. var patientModel = PatientModelDTO.fromJson(jsonDecode(e.dataJson));
  56. patientModel.isExistLocalData = true;
  57. return patientModel;
  58. }).toList();
  59. return dtos;
  60. }
  61. @override
  62. Future<PatientDTO?> getDetail(String code) async {
  63. try {
  64. final request = GetPatientRequest(code: code, token: Store.user.token);
  65. if (kIsOnline && !kIsWeb) {
  66. //在线状态,先查询是否存在本体未提交数据
  67. final entity = await db.repositories.patient.singleByCode(code);
  68. if (entity != null &&
  69. entity.dataJson.isNotEmpty &&
  70. entity.syncState != OfflineDataSyncState.success) {
  71. logger.i("PatientManager query patient detail location exist.");
  72. final jsonMap = jsonDecode(entity.dataJson);
  73. final dto = PatientDTO.fromJson(jsonMap);
  74. return dto;
  75. } else {
  76. logger.i("PatientManager query patient detail location not exist.");
  77. }
  78. }
  79. final result = await rpc.vitalPatient.getPatientDetailAsync(request);
  80. return result;
  81. } catch (e) {
  82. logger.e("PatientManager query patient detail error.", e);
  83. return null;
  84. }
  85. }
  86. @override
  87. Future<PageCollection<PatientModelDTO>?> getPagedList(
  88. PatientPageRequest request) async {
  89. PageCollection<PatientModelDTO>? listPatientDto =
  90. PageCollection<PatientModelDTO>();
  91. try {
  92. request.token = Store.user.token;
  93. final result = await rpc.vitalPatient.getPatientPageAsync(request);
  94. listPatientDto.dataCount = result.dataCount;
  95. listPatientDto.pageData = [];
  96. for (var element in result.pageData!) {
  97. var patientModel = PatientModelDTO.fromJson(element.toJson());
  98. if (!kIsWeb) {
  99. var isNotUploadedPatient =
  100. await db.repositories.patient.isNotUploadedPatient(element.code!);
  101. if (isNotUploadedPatient) {
  102. if (kIsOnline) {
  103. continue;
  104. } else {
  105. patientModel.isExistLocalData = true;
  106. }
  107. } else {
  108. int localCount = 0;
  109. if (!kIsWeb) {
  110. localCount = await db.repositories.diagnosis
  111. .getNotUploadedCountByPatientCode(
  112. element.code!, Store.user.userCode!);
  113. }
  114. if (localCount > 0) {
  115. patientModel.isExistLocalData = true;
  116. }
  117. }
  118. }
  119. listPatientDto.pageData?.add(patientModel);
  120. }
  121. return listPatientDto;
  122. } catch (e) {
  123. logger.e("PatientManager query patient paged list error.", e);
  124. return null;
  125. }
  126. }
  127. @override
  128. Future<bool> updatePatientAsync(UpdatePatientRequest2 request) async {
  129. try {
  130. if (kIsOnline) {
  131. await _updateLocationPatientAsync(request);
  132. }
  133. final result = await rpc.vitalPatient.updatePatientAsync(request);
  134. return result;
  135. } catch (e) {
  136. return false;
  137. }
  138. }
  139. ///在线编辑居民信息时,同步更改本地缓存
  140. Future<bool> _updateLocationPatientAsync(
  141. UpdatePatientRequest2 request) async {
  142. final code = request.code!;
  143. final existEntity = await _checkPatientExist(code);
  144. PatientEntity entity;
  145. PatientDTO dto;
  146. if (existEntity == null) {
  147. entity = PatientEntity();
  148. entity.code = request.code!;
  149. entity.isValid = true;
  150. entity.name = request.patientName!;
  151. entity.userCode = Store.user.userCode!; //添加用户code
  152. entity.syncState = OfflineDataSyncState.success;
  153. dto = PatientDTO();
  154. ModelConverter.syncUpdate2Dto(dto, request);
  155. final dtoJson = dto.toJson();
  156. entity.dataJson = jsonEncode(dtoJson);
  157. final id = await db.repositories.patient.insert(entity);
  158. return id > 0;
  159. } else {
  160. entity = existEntity;
  161. final jsonMap = jsonDecode(entity.dataJson);
  162. dto = PatientDTO.fromJson(jsonMap);
  163. ModelConverter.syncUpdate2Dto(dto, request);
  164. final dtoJson = dto.toJson();
  165. entity.dataJson = jsonEncode(dtoJson);
  166. final rows = await db.repositories.patient.update(entity);
  167. return rows > 0;
  168. }
  169. }
  170. Future<PatientEntity?> _checkPatientExist(String code) async {
  171. try {
  172. final entity = await db.repositories.patient.singleByCode(code);
  173. return entity;
  174. } catch (e) {
  175. logger.e(
  176. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  177. }
  178. return null;
  179. }
  180. @override
  181. Future<bool> setCrowdLabelsAsync(
  182. String patientCode, List<String> crowLabels) async {
  183. try {
  184. final request = SetCrowdLabelsRequest(
  185. code: patientCode, crowdLabels: crowLabels, token: Store.user.token);
  186. final result = rpc.vitalPatient.setCrowdLabelsAsync(request);
  187. return result;
  188. } catch (e) {
  189. return false;
  190. }
  191. }
  192. @override
  193. Future<String> createPatientExtensionAsync(
  194. CreatePatientExtensionRequest request) async {
  195. try {
  196. final result =
  197. await rpc.vitalPatientExtension.createPatientExtensionAsync(request);
  198. return result;
  199. } catch (e) {
  200. return '';
  201. }
  202. }
  203. @override
  204. Future<PatientExtensionDTO?> getPatientExtensionDetailByCodeAndKeyAsync(
  205. GetPatientExtensionByCodeAndKeyRequest request) async {
  206. try {
  207. final result = await rpc.vitalPatientExtension
  208. .getPatientExtensionDetailByCodeAndKeyAsync(request);
  209. return result;
  210. } catch (e) {
  211. return null;
  212. }
  213. }
  214. @override
  215. ///更新居民健康信息
  216. Future<bool> updatePatientExtensionAsync(
  217. UpdatePatientExtensionRequest request) async {
  218. try {
  219. final result =
  220. await rpc.vitalPatientExtension.updatePatientExtensionAsync(request);
  221. return result;
  222. } catch (e) {}
  223. return false;
  224. }
  225. ///获取所有userCode为空的数据,并赋值
  226. @override
  227. Future<void> resettingUsercodeIsEmptyData() async {
  228. if (kIsWeb) return;
  229. var version = await db.database.getVersion();
  230. if (version > 0) {
  231. final list = await db.repositories.patient.queryable.where((x) {
  232. final List<IDbColumnCondition> arr = [];
  233. arr.add(x.isValid.equals(true));
  234. arr.add(x.userCode.equals(''));
  235. return arr;
  236. }).toList();
  237. logger.w(
  238. "PatientManager resettingUsercodeIsEmptyData list.count:${list.length}.");
  239. for (var element in list) {
  240. if (element.syncState == OfflineDataSyncState.success) {
  241. var result = await getDetail(element.code);
  242. if (result != null) {
  243. element.userCode = result.createdDoctor!;
  244. }
  245. } else {
  246. element.userCode = Store.user.userCode!;
  247. }
  248. await db.repositories.patient.update(element);
  249. }
  250. // 更新归属于该医生下的居民的组织Code
  251. final orgCode = Store.user.organizationCode;
  252. final userCode = Store.user.userCode;
  253. await db.database.execute(
  254. "UPDATE patients SET orgCode = '$orgCode' WHERE 1=1 AND isValid=1 AND orgCode='' AND userCode='$userCode';");
  255. }
  256. }
  257. @override
  258. Future<void> switchCurrentPatient(PatientDTO dto) async {
  259. try {
  260. if (!kIsWeb) {
  261. PatientEntity? entity =
  262. await db.repositories.patient.singleByCode(dto.code!);
  263. if (entity == null) {
  264. entity = PatientEntity();
  265. entity.isValid = true;
  266. entity.code = dto.code!;
  267. entity.userCode = userCode!;
  268. entity.orgCode = Store.user.organizationCode;
  269. // 从在线数据中刚获取的,不需要同步
  270. entity.syncState = OfflineDataSyncState.success;
  271. entity.overallSyncState = OfflineDataSyncState.success;
  272. // 先创建,后面统一代码更新
  273. final id = await db.repositories.patient.insert(entity);
  274. entity.id = id;
  275. } else {
  276. entity.updateTime = DateTime.now();
  277. }
  278. entity.name = dto.patientName!;
  279. final dtoJsonMap = dto.toJson();
  280. entity.dataJson = jsonEncode(dtoJsonMap);
  281. await db.repositories.patient.update(entity);
  282. }
  283. Store.user.currentSelectPatientInfo = dto;
  284. } catch (e) {
  285. logger.e("PatientManager switchCurrentPatient-${dto.code} error.", e);
  286. }
  287. }
  288. @override
  289. Future<RegisterPersonInfoDTO?>
  290. getRegisterPersonInfoByPhysicalExamNumberAsync({
  291. required String physicalExamNumber,
  292. }) async {
  293. try {
  294. var request = GetRegisterPersonRequest();
  295. request.physicalExamNumber = physicalExamNumber;
  296. final registerPersonInfo = await rpc.vitalHealthExamBooking
  297. .getRegisterPersonInfoByPhysicalExamNumberAsync(
  298. request,
  299. );
  300. return registerPersonInfo;
  301. } catch (e) {
  302. logger.e(
  303. "PatientManager sync getRegisterPersonInfoByPhysicalExamNumberAsync error.",
  304. e);
  305. return null;
  306. }
  307. }
  308. }