123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import 'dart:convert';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/foundation.dart';
- import 'package:vital_local_database/core/interface/queryable.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/database/entities/defines.dart';
- import 'package:vitalapp/database/entities/patient.dart';
- import 'package:vitalapp/global.dart';
- import 'package:vitalapp/managers/interfaces/models/patient_model_dto.dart';
- import 'package:vitalapp/managers/interfaces/patient.dart';
- import 'package:vitalapp/mock/services/patient.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'package:vitalapp/store/store.dart';
- class PatientManager implements IPatientManager {
- String? get userCode => Store.user.userCode;
- @override
- Future<String?> create(CreatePatientRequest2 request) async {
- request.token = Store.user.token;
- request.code = request.cardNo;
- final result = await rpc.vitalPatient.createPatientAsync(request);
- return result;
- }
- @override
- Future<List<PatientModelDTO>?> getPagedOfflineList(
- String? keyword,
- DateTime? startTime,
- DateTime? endTime,
- ) async {
- final isKeywordQuery = keyword != null && keyword.isNotEmpty;
- final query = db.repositories.patient.queryable.where((x) {
- final List<IDbColumnCondition> arr = [];
- arr.add(x.isValid.equals(true));
- arr.add(x.orgCode.equals(Store.user.organizationCode));
- arr.add(x.syncState.notEquals(OfflineDataSyncState.success));
- if (!isKeywordQuery) {
- if (startTime != null) {
- arr.add(x.createTime.gte(startTime));
- }
- if (endTime != null) {
- arr.add(x.createTime.lt(endTime));
- }
- }
- return arr;
- });
- if (isKeywordQuery) {
- // 姓名查询
- query.and((x) => x.name.like("%$keyword%"));
- // 身份照查询
- query.or((x) => x.code.like("%$keyword%"));
- }
- final entities =
- await query.orderBy((x) => x.createTime, DbOrderByType.desc).toList();
- final dtos = entities.map((e) {
- var patientModel = PatientModelDTO.fromJson(jsonDecode(e.dataJson));
- patientModel.isExistLocalData = true;
- return patientModel;
- }).toList();
- return dtos;
- }
- @override
- Future<PatientDTO?> getDetail(String code) async {
- try {
- final request = GetPatientRequest(code: code, token: Store.user.token);
- if (kIsOnline && !kIsWeb) {
- //在线状态,先查询是否存在本体未提交数据
- final entity = await db.repositories.patient.singleByCode(code);
- if (entity != null &&
- entity.dataJson.isNotEmpty &&
- entity.syncState != OfflineDataSyncState.success) {
- logger.i("PatientManager query patient detail location exist.");
- final jsonMap = jsonDecode(entity.dataJson);
- final dto = PatientDTO.fromJson(jsonMap);
- return dto;
- } else {
- logger.i("PatientManager query patient detail location not exist.");
- }
- }
- final result = await rpc.vitalPatient.getPatientDetailAsync(request);
- return result;
- } catch (e) {
- logger.e("PatientManager query patient detail error.", e);
- return null;
- }
- }
- @override
- Future<PageCollection<PatientModelDTO>?> getPagedList(
- PatientPageRequest request) async {
- PageCollection<PatientModelDTO>? listPatientDto =
- PageCollection<PatientModelDTO>();
- try {
- request.token = Store.user.token;
- final result = await rpc.vitalPatient.getPatientPageAsync(request);
- listPatientDto.dataCount = result.dataCount;
- listPatientDto.pageData = [];
- for (var element in result.pageData!) {
- var patientModel = PatientModelDTO.fromJson(element.toJson());
- if (!kIsWeb) {
- var isNotUploadedPatient =
- await db.repositories.patient.isNotUploadedPatient(element.code!);
- if (isNotUploadedPatient) {
- if (kIsOnline) {
- continue;
- } else {
- patientModel.isExistLocalData = true;
- }
- } else {
- int localCount = 0;
- if (!kIsWeb) {
- localCount = await db.repositories.diagnosis
- .getNotUploadedCountByPatientCode(
- element.code!, Store.user.userCode!);
- }
- if (localCount > 0) {
- patientModel.isExistLocalData = true;
- }
- }
- }
- listPatientDto.pageData?.add(patientModel);
- }
- return listPatientDto;
- } catch (e) {
- logger.e("PatientManager query patient paged list error.", e);
- return null;
- }
- }
- @override
- Future<bool> updatePatientAsync(UpdatePatientRequest2 request) async {
- try {
- if (kIsOnline) {
- await _updateLocationPatientAsync(request);
- }
- final result = await rpc.vitalPatient.updatePatientAsync(request);
- return result;
- } catch (e) {
- return false;
- }
- }
- ///在线编辑居民信息时,同步更改本地缓存
- Future<bool> _updateLocationPatientAsync(
- 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.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;
- }
- }
- Future<PatientEntity?> _checkPatientExist(String code) async {
- try {
- final entity = await db.repositories.patient.singleByCode(code);
- return entity;
- } catch (e) {
- logger.e(
- "PatientServiceMock - Check patient exist error. Code: $code.", e);
- }
- return null;
- }
- @override
- Future<bool> setCrowdLabelsAsync(
- String patientCode, List<String> crowLabels) async {
- try {
- final request = SetCrowdLabelsRequest(
- code: patientCode, crowdLabels: crowLabels, token: Store.user.token);
- final result = rpc.vitalPatient.setCrowdLabelsAsync(request);
- return result;
- } catch (e) {
- return false;
- }
- }
- @override
- Future<String> createPatientExtensionAsync(
- CreatePatientExtensionRequest request) async {
- try {
- final result =
- await rpc.vitalPatientExtension.createPatientExtensionAsync(request);
- return result;
- } catch (e) {
- return '';
- }
- }
- @override
- Future<PatientExtensionDTO?> getPatientExtensionDetailByCodeAndKeyAsync(
- GetPatientExtensionByCodeAndKeyRequest request) async {
- try {
- final result = await rpc.vitalPatientExtension
- .getPatientExtensionDetailByCodeAndKeyAsync(request);
- return result;
- } catch (e) {
- return null;
- }
- }
- @override
- ///更新居民健康信息
- Future<bool> updatePatientExtensionAsync(
- UpdatePatientExtensionRequest request) async {
- try {
- final result =
- await rpc.vitalPatientExtension.updatePatientExtensionAsync(request);
- return result;
- } catch (e) {}
- return false;
- }
- ///获取所有userCode为空的数据,并赋值
- @override
- Future<void> resettingUsercodeIsEmptyData() async {
- if (kIsWeb) return;
- var version = await db.database.getVersion();
- if (version > 0) {
- final list = await db.repositories.patient.queryable.where((x) {
- final List<IDbColumnCondition> arr = [];
- arr.add(x.isValid.equals(true));
- arr.add(x.userCode.equals(''));
- return arr;
- }).toList();
- logger.w(
- "PatientManager resettingUsercodeIsEmptyData list.count:${list.length}.");
- for (var element in list) {
- if (element.syncState == OfflineDataSyncState.success) {
- var result = await getDetail(element.code);
- if (result != null) {
- element.userCode = result.createdDoctor!;
- }
- } else {
- element.userCode = Store.user.userCode!;
- }
- await db.repositories.patient.update(element);
- }
- // 更新归属于该医生下的居民的组织Code
- final orgCode = Store.user.organizationCode;
- final userCode = Store.user.userCode;
- await db.database.execute(
- "UPDATE patients SET orgCode = '$orgCode' WHERE 1=1 AND isValid=1 AND orgCode='' AND userCode='$userCode';");
- }
- }
- @override
- Future<void> switchCurrentPatient(PatientDTO dto) async {
- try {
- if (!kIsWeb) {
- PatientEntity? entity =
- await db.repositories.patient.singleByCode(dto.code!);
- if (entity == null) {
- entity = PatientEntity();
- entity.isValid = true;
- entity.code = dto.code!;
- entity.userCode = userCode!;
- entity.orgCode = Store.user.organizationCode;
- // 从在线数据中刚获取的,不需要同步
- entity.syncState = OfflineDataSyncState.success;
- entity.overallSyncState = OfflineDataSyncState.success;
- // 先创建,后面统一代码更新
- final id = await db.repositories.patient.insert(entity);
- entity.id = id;
- } else {
- entity.updateTime = DateTime.now();
- }
- entity.name = dto.patientName!;
- final dtoJsonMap = dto.toJson();
- entity.dataJson = jsonEncode(dtoJsonMap);
- await db.repositories.patient.update(entity);
- }
- Store.user.currentSelectPatientInfo = dto;
- } catch (e) {
- logger.e("PatientManager switchCurrentPatient-${dto.code} error.", e);
- }
- }
- @override
- Future<RegisterPersonInfoDTO?>
- getRegisterPersonInfoByPhysicalExamNumberAsync({
- required String physicalExamNumber,
- }) async {
- try {
- var request = GetRegisterPersonRequest();
- request.physicalExamNumber = physicalExamNumber;
- final registerPersonInfo = await rpc.vitalHealthExamBooking
- .getRegisterPersonInfoByPhysicalExamNumberAsync(
- request,
- );
- return registerPersonInfo;
- } catch (e) {
- logger.e(
- "PatientManager sync getRegisterPersonInfoByPhysicalExamNumberAsync error.",
- e);
- return null;
- }
- }
- }
|