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 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 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 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> getPatientPageAsync( PatientPageRequest request) async { final keyword = request.keyword; final hasKeyword = keyword != null && keyword.isNotEmpty; var query = db.repositories.patient.queryable.where((x) { final List 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( pageIndex: request.pageIndex, pageSize: request.pageSize, dataCount: count, pageData: dtos, ); return pagedList; } Future _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; } }