123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import 'dart:convert';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:vital_local_database/index.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/database/entities/defines.dart';
- import 'package:vitalapp/database/entities/patient.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'package:vitalapp/store/store.dart';
- class PatientServiceMock extends VitalPatientService {
- PatientServiceMock(String host) : super(host);
- @override
- Future<String> createPatientAsync(CreatePatientRequest2 request) async {
- final code = request.code!;
- final existEntity = await _checkPatientExist(code);
- PatientEntity entity;
- PatientDTO dto;
- if (existEntity == null) {
- entity = PatientEntity();
- entity.code = request.code!;
- entity.isValid = true;
- entity.name = request.patientName!;
- entity.userCode = Store.user.userCode!; //添加用户code
- entity.orgCode = Store.user.organizationCode; //添加用户机构code
- dto = PatientDTO();
- ModelConverter.syncCreate2Dto(dto, request);
- dto.createdDoctorName = Store.user.displayName;
- dto.createdOrgName = Store.user.organizationName;
- final dtoJson = dto.toJson();
- entity.dataJson = jsonEncode(dtoJson);
- final id = await db.repositories.patient.insert(entity);
- return id > 0 ? code : "";
- } else {
- if (existEntity.orgCode != Store.user.organizationCode) {
- existEntity.orgCode = Store.user.organizationCode;
- }
- entity = existEntity;
- final jsonMap = jsonDecode(entity.dataJson);
- dto = PatientDTO.fromJson(jsonMap);
- ModelConverter.syncCreate2Dto(dto, request);
- final dtoJson = dto.toJson();
- entity.dataJson = jsonEncode(dtoJson);
- final rows = await db.repositories.patient.update(entity);
- return rows > 0 ? code : "";
- }
- }
- @override
- Future<bool> updatePatientAsync(UpdatePatientRequest2 request) async {
- final code = request.code!;
- final existEntity = await _checkPatientExist(code);
- PatientEntity entity;
- PatientDTO dto;
- if (existEntity == null) {
- entity = PatientEntity();
- entity.code = request.code!;
- entity.isValid = true;
- entity.name = request.patientName!;
- entity.userCode = Store.user.userCode!; //添加用户code
- entity.orgCode = Store.user.organizationCode;
- entity.syncState = OfflineDataSyncState.success;
- dto = PatientDTO();
- ModelConverter.syncUpdate2Dto(dto, request);
- final dtoJson = dto.toJson();
- entity.dataJson = jsonEncode(dtoJson);
- final id = await db.repositories.patient.insert(entity);
- return id > 0;
- } else {
- entity = existEntity;
- final jsonMap = jsonDecode(entity.dataJson);
- dto = PatientDTO.fromJson(jsonMap);
- ModelConverter.syncUpdate2Dto(dto, request);
- final dtoJson = dto.toJson();
- entity.dataJson = jsonEncode(dtoJson);
- final rows = await db.repositories.patient.update(entity);
- return rows > 0;
- }
- }
- @override
- Future<PatientDTO> getPatientDetailAsync(GetPatientRequest request) async {
- final code = request.code!;
- final entity = await db.repositories.patient
- .singleByCodeWithUserCode(code, Store.user.userCode!);
- if (entity == null || entity.dataJson.isEmpty) {
- throw JsonRpcException(code: -1, message: "Offline patient not found");
- }
- final jsonMap = jsonDecode(entity.dataJson);
- final dto = PatientDTO.fromJson(jsonMap);
- return dto;
- }
- @override
- Future<PageCollection<PatientDTO>> getPatientPageAsync(
- PatientPageRequest request) async {
- final keyword = request.keyword;
- final hasKeyword = keyword != null && keyword.isNotEmpty;
- var query = db.repositories.patient.queryable.where((x) {
- final List<IDbColumnCondition> arr = [];
- arr.add(x.isValid.equals(true));
- arr.add(x.userCode.equals(Store.user.userCode));
- if (!hasKeyword) {
- if (request.startTime != null) {
- arr.add(x.createTime.gte(request.startTime));
- }
- if (request.endTime != null) {
- arr.add(x.createTime.lt(request.endTime));
- }
- }
- return arr;
- });
- if (hasKeyword) {
- query = query.whereCustom(
- " AND (name LIKE '%$keyword%' OR code LIKE '%$keyword%') ");
- }
- final count = await query.count();
- final offset = request.pageSize * (request.pageIndex - 1);
- final entities = await query
- .offset(offset)
- .limit(request.pageSize)
- .orderBy((x) => x.updateTime, DbOrderByType.desc)
- .toList();
- final dtos = entities.map((e) {
- final jsonMap = jsonDecode(e.dataJson);
- final dto = PatientDTO.fromJson(jsonMap);
- return dto;
- }).toList();
- final pagedList = PageCollection<PatientDTO>(
- pageIndex: request.pageIndex,
- pageSize: request.pageSize,
- dataCount: count,
- pageData: dtos,
- );
- return pagedList;
- }
- Future<PatientEntity?> _checkPatientExist(String code) async {
- try {
- final entity = await db.repositories.patient
- .singleByCodeWithUserCode(code, Store.user.userCode!);
- return entity;
- } catch (e) {
- logger.e(
- "PatientServiceMock - Check patient exist error. Code: $code.", e);
- }
- return null;
- }
- }
- abstract class ModelConverter {
- /// 根据新建请求创建DTO
- static void syncCreate2Dto(PatientDTO dto, CreatePatientRequest2 request) {
- dto.code = request.code;
- dto.patientName = request.patientName;
- dto.phone = request.phone;
- dto.emergencyName = request.emergencyName;
- dto.emergencyPhone = request.emergencyPhone;
- dto.cardNo = request.cardNo;
- dto.nationality = request.nationality;
- dto.birthday = request.birthday;
- dto.crowdLabels = request.crowdLabels;
- dto.cardType = request.cardType;
- dto.patientGender = request.patientGender;
- dto.patientAddress = request.patientAddress;
- dto.permanentResidenceAddress = request.permanentResidenceAddress;
- dto.teamRegionCode = request.teamRegionCode;
- dto.contractedDoctor = request.contractedDoctor;
- }
- /// 根据DTO创建新建请求
- static CreatePatientRequest dto2Create(PatientDTO dto) {
- final dtoJson = dto.toJson();
- final request = CreatePatientRequest.fromJson(dtoJson);
- return request;
- }
- /// 同步更新到DTO
- static void syncUpdate2Dto(PatientDTO dto, UpdatePatientRequest2 request) {
- dto.code = request.code;
- dto.patientName = request.patientName;
- dto.phone = request.phone;
- dto.cardNo = request.cardNo;
- dto.emergencyName = request.emergencyName;
- dto.emergencyPhone = request.emergencyPhone;
- dto.nationality = request.nationality;
- dto.birthday = request.birthday;
- dto.crowdLabels = request.crowdLabels;
- if (request.cardType != null) {
- dto.cardType = request.cardType!;
- }
- if (request.patientGender != null) {
- dto.patientGender = request.patientGender!;
- }
- dto.patientAddress = request.patientAddress;
- dto.permanentResidenceAddress = request.permanentResidenceAddress;
- dto.teamRegionCode = request.teamRegionCode;
- dto.contractedDoctor = request.contractedDoctor;
- }
- /// 根据DTO创建更新请求
- static UpdatePatientRequest dto2Update(PatientDTO dto) {
- final dtoJson = dto.toJson();
- final request = UpdatePatientRequest.fromJson(dtoJson);
- return request;
- }
- }
|