123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import 'dart:convert';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/foundation.dart';
- import 'package:vitalapp/database/db.dart';
- import 'package:vitalapp/database/entities/defines.dart';
- import 'package:vitalapp/global.dart';
- import 'package:vitalapp/managers/interfaces/exam.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'package:fis_common/logger/logger.dart';
- class ExamManager implements IExamManager {
- String? get userCode => Store.user.userCode;
- @override
- Future<bool?> createExam(CreateExamRequest examRequest) async {
- try {
- examRequest.token = Store.user.token;
- final examCode = await rpc.vitalExam.createExamAsync(
- examRequest,
- );
- print(jsonEncode(examRequest.toJson()));
- if (examCode.isNotEmpty) {
- return true;
- }
- return false;
- } catch (e) {
- print(jsonEncode(examRequest.toJson()));
- logger.e("ExamManager sync user info error.", e);
- return null;
- }
- }
- @override
- Future<bool?> editExam(UpdateExamRequest examRequest) async {
- try {
- examRequest.token = Store.user.token;
- final result = await rpc.vitalExam.updateExamAsync(
- examRequest,
- );
- return result;
- } catch (e) {
- logger.e("ExamManager sync user info error.", e);
- return null;
- }
- }
- @override
- Future<bool?> updateExamByBatchNumberAsync(
- UpdateExamByBatchNumberRequest updateExamByBatchNumberRequest) async {
- try {
- updateExamByBatchNumberRequest.token = Store.user.token;
- final result = await rpc.vitalExam.updateExamByBatchNumberAsync(
- updateExamByBatchNumberRequest,
- );
- print(jsonEncode(updateExamByBatchNumberRequest.toJson()));
- return result;
- } catch (e) {
- logger.e("updateExamByBatchNumberAsync sync user info error.", e);
- return null;
- }
- }
- @override
- Future<List<ExamDTO>?> getExamByBatchNumberAsync(String batchNumber) async {
- GetExamByBatchNumberRequest getExamByBatchNumberRequest =
- GetExamByBatchNumberRequest();
- try {
- getExamByBatchNumberRequest.token = Store.user.token;
- getExamByBatchNumberRequest.batchNumber = batchNumber;
- final result = await rpc.vitalExam.getExamByBatchNumberAsync(
- getExamByBatchNumberRequest,
- );
- print(jsonEncode(getExamByBatchNumberRequest.toJson()));
- return result;
- } catch (e) {
- logger.e("updateExamByBatchNumberAsync sync user info error.", e);
- return null;
- }
- }
- @override
- Future<List<ExamConclusionDTO>?> getPatientExamByPageAsync(
- String patientCode, String examKey) async {
- try {
- final dtos = <ExamConclusionDTO>[];
- if (kIsOnline && !kIsWeb) {
- final offlineDtos =
- await _getPatientTCMCAllNotUploadedExamList(patientCode);
- dtos.addAll(offlineDtos);
- }
- GetPatientExamByPageRequest request = GetPatientExamByPageRequest(
- patientCode: patientCode,
- token: Store.user.token,
- examKey: examKey,
- pageIndex: 1,
- pageSize: 9999,
- );
- final result = await rpc.vitalExam.getPatientExamByPageAsync(request);
- if (result.dataCount > 0) {
- dtos.addAll(result.pageData!);
- }
- return dtos;
- } catch (e) {
- logger.e("getPatientExamByPageAsync sync user info error.", e);
- return null;
- }
- }
- @override
- Future<bool> checkRecordOfflineCached(String code) async {
- try {
- final count = await db.repositories.exam.queryable
- .where((x) => [
- x.isValid.equals(true),
- x.userCode.equals(userCode),
- x.code.equals(code),
- ])
- .count();
- return count > 0;
- } catch (e) {
- logger.e("ExamManager checkRecordOfflineCached-$code error", e);
- return false;
- }
- }
- Future<List<ExamConclusionDTO>> _getPatientTCMCAllNotUploadedExamList(
- String patientCode) async {
- final entities = await db.repositories.exam.queryable
- .where((x) => [
- x.isValid.equals(true),
- x.userCode.equals(userCode),
- x.patientCode.equals(patientCode),
- x.templateKey.equals("HEITCMC"),
- x.syncState.notEquals(OfflineDataSyncState.success),
- ])
- .toList();
- if (entities.isEmpty) {
- return [];
- }
- final patient = await db.repositories.patient
- .singleByCodeWithUserCode(patientCode, userCode!);
- final dtos = entities.map((entity) {
- final jsonMap = jsonDecode(entity.dataJson);
- return ExamConclusionDTO(
- contractedDoctor: "NOT_UPLOADED", // TODO: 先借用一下此字段
- code: entity.code,
- key: entity.templateKey,
- patientCode: entity.patientCode,
- patientName: patient?.name ?? "",
- examDoctor: Store.user.displayName,
- physicalExamNumber: entity.physicalEaxmNumber,
- batchNumber: entity.batchNumber,
- templateCode: entity.templateCode,
- createTime: entity.createTime,
- updateTime: entity.updateTime ?? entity.createTime,
- examData: entity.dataJson,
- tcmConclusion: jsonMap["PhysicalConclusion"],
- );
- }).toList();
- return dtos;
- }
- }
|