patient.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:vital_local_database/index.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:fis_common/logger/logger.dart';
  8. import 'package:vitalapp/store/store.dart';
  9. class PatientServiceMock extends VitalPatientService {
  10. PatientServiceMock(String host) : super(host);
  11. @override
  12. Future<String> createPatientAsync(CreatePatientRequest2 request) async {
  13. final code = request.code!;
  14. final existEntity = await _checkPatientExist(code);
  15. PatientEntity entity;
  16. PatientDTO dto;
  17. if (existEntity == null) {
  18. entity = PatientEntity();
  19. entity.code = request.code!;
  20. entity.isValid = true;
  21. entity.name = request.patientName!;
  22. entity.userCode = Store.user.userCode!; //添加用户code
  23. entity.orgCode = Store.user.organizationCode; //添加用户机构code
  24. dto = PatientDTO();
  25. ModelConverter.syncCreate2Dto(dto, request);
  26. dto.createdDoctorName = Store.user.displayName;
  27. dto.createdOrgName = Store.user.organizationName;
  28. final dtoJson = dto.toJson();
  29. entity.dataJson = jsonEncode(dtoJson);
  30. final id = await db.repositories.patient.insert(entity);
  31. return id > 0 ? code : "";
  32. } else {
  33. if (existEntity.orgCode != Store.user.organizationCode) {
  34. existEntity.orgCode = Store.user.organizationCode;
  35. }
  36. entity = existEntity;
  37. final jsonMap = jsonDecode(entity.dataJson);
  38. dto = PatientDTO.fromJson(jsonMap);
  39. ModelConverter.syncCreate2Dto(dto, request);
  40. final dtoJson = dto.toJson();
  41. entity.dataJson = jsonEncode(dtoJson);
  42. final rows = await db.repositories.patient.update(entity);
  43. return rows > 0 ? code : "";
  44. }
  45. }
  46. @override
  47. Future<bool> updatePatientAsync(UpdatePatientRequest2 request) async {
  48. final code = request.code!;
  49. final existEntity = await _checkPatientExist(code);
  50. PatientEntity entity;
  51. PatientDTO dto;
  52. if (existEntity == null) {
  53. entity = PatientEntity();
  54. entity.code = request.code!;
  55. entity.isValid = true;
  56. entity.name = request.patientName!;
  57. entity.userCode = Store.user.userCode!; //添加用户code
  58. entity.orgCode = Store.user.organizationCode;
  59. entity.syncState = OfflineDataSyncState.success;
  60. dto = PatientDTO();
  61. ModelConverter.syncUpdate2Dto(dto, request);
  62. final dtoJson = dto.toJson();
  63. entity.dataJson = jsonEncode(dtoJson);
  64. final id = await db.repositories.patient.insert(entity);
  65. return id > 0;
  66. } else {
  67. entity = existEntity;
  68. final jsonMap = jsonDecode(entity.dataJson);
  69. dto = PatientDTO.fromJson(jsonMap);
  70. ModelConverter.syncUpdate2Dto(dto, request);
  71. final dtoJson = dto.toJson();
  72. entity.dataJson = jsonEncode(dtoJson);
  73. final rows = await db.repositories.patient.update(entity);
  74. return rows > 0;
  75. }
  76. }
  77. @override
  78. Future<PatientDTO> getPatientDetailAsync(GetPatientRequest request) async {
  79. final code = request.code!;
  80. final entity = await db.repositories.patient
  81. .singleByCodeWithUserCode(code, Store.user.userCode!);
  82. if (entity == null || entity.dataJson.isEmpty) {
  83. throw JsonRpcException(code: -1, message: "Offline patient not found");
  84. }
  85. final jsonMap = jsonDecode(entity.dataJson);
  86. final dto = PatientDTO.fromJson(jsonMap);
  87. return dto;
  88. }
  89. @override
  90. Future<PageCollection<PatientDTO>> getPatientPageAsync(
  91. PatientPageRequest request) async {
  92. final keyword = request.keyword;
  93. final hasKeyword = keyword != null && keyword.isNotEmpty;
  94. var query = db.repositories.patient.queryable.where((x) {
  95. final List<IDbColumnCondition> arr = [];
  96. arr.add(x.isValid.equals(true));
  97. arr.add(x.userCode.equals(Store.user.userCode));
  98. if (!hasKeyword) {
  99. if (request.startTime != null) {
  100. arr.add(x.createTime.gte(request.startTime));
  101. }
  102. if (request.endTime != null) {
  103. arr.add(x.createTime.lt(request.endTime));
  104. }
  105. }
  106. return arr;
  107. });
  108. if (hasKeyword) {
  109. query = query.whereCustom(
  110. " AND (name LIKE '%$keyword%' OR code LIKE '%$keyword%') ");
  111. }
  112. final count = await query.count();
  113. final offset = request.pageSize * (request.pageIndex - 1);
  114. final entities = await query
  115. .offset(offset)
  116. .limit(request.pageSize)
  117. .orderBy((x) => x.updateTime, DbOrderByType.desc)
  118. .toList();
  119. final dtos = entities.map((e) {
  120. final jsonMap = jsonDecode(e.dataJson);
  121. final dto = PatientDTO.fromJson(jsonMap);
  122. return dto;
  123. }).toList();
  124. final pagedList = PageCollection<PatientDTO>(
  125. pageIndex: request.pageIndex,
  126. pageSize: request.pageSize,
  127. dataCount: count,
  128. pageData: dtos,
  129. );
  130. return pagedList;
  131. }
  132. Future<PatientEntity?> _checkPatientExist(String code) async {
  133. try {
  134. final entity = await db.repositories.patient
  135. .singleByCodeWithUserCode(code, Store.user.userCode!);
  136. return entity;
  137. } catch (e) {
  138. logger.e(
  139. "PatientServiceMock - Check patient exist error. Code: $code.", e);
  140. }
  141. return null;
  142. }
  143. }
  144. abstract class ModelConverter {
  145. /// 根据新建请求创建DTO
  146. static void syncCreate2Dto(PatientDTO dto, CreatePatientRequest2 request) {
  147. dto.code = request.code;
  148. dto.patientName = request.patientName;
  149. dto.phone = request.phone;
  150. dto.emergencyName = request.emergencyName;
  151. dto.emergencyPhone = request.emergencyPhone;
  152. dto.cardNo = request.cardNo;
  153. dto.nationality = request.nationality;
  154. dto.birthday = request.birthday;
  155. dto.crowdLabels = request.crowdLabels;
  156. dto.cardType = request.cardType;
  157. dto.patientGender = request.patientGender;
  158. dto.patientAddress = request.patientAddress;
  159. dto.permanentResidenceAddress = request.permanentResidenceAddress;
  160. dto.teamRegionCode = request.teamRegionCode;
  161. dto.contractedDoctor = request.contractedDoctor;
  162. }
  163. /// 根据DTO创建新建请求
  164. static CreatePatientRequest dto2Create(PatientDTO dto) {
  165. final dtoJson = dto.toJson();
  166. final request = CreatePatientRequest.fromJson(dtoJson);
  167. return request;
  168. }
  169. /// 同步更新到DTO
  170. static void syncUpdate2Dto(PatientDTO dto, UpdatePatientRequest2 request) {
  171. dto.code = request.code;
  172. dto.patientName = request.patientName;
  173. dto.phone = request.phone;
  174. dto.cardNo = request.cardNo;
  175. dto.emergencyName = request.emergencyName;
  176. dto.emergencyPhone = request.emergencyPhone;
  177. dto.nationality = request.nationality;
  178. dto.birthday = request.birthday;
  179. dto.crowdLabels = request.crowdLabels;
  180. if (request.cardType != null) {
  181. dto.cardType = request.cardType!;
  182. }
  183. if (request.patientGender != null) {
  184. dto.patientGender = request.patientGender!;
  185. }
  186. dto.patientAddress = request.patientAddress;
  187. dto.permanentResidenceAddress = request.permanentResidenceAddress;
  188. dto.teamRegionCode = request.teamRegionCode;
  189. dto.contractedDoctor = request.contractedDoctor;
  190. }
  191. /// 根据DTO创建更新请求
  192. static UpdatePatientRequest dto2Update(PatientDTO dto) {
  193. final dtoJson = dto.toJson();
  194. final request = UpdatePatientRequest.fromJson(dtoJson);
  195. return request;
  196. }
  197. }