123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import 'dart:convert';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:uuid/uuid.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/exam.dart';
- import 'package:vitalapp/database/entities/exam_batch.dart';
- import 'package:vitalapp/store/store.dart';
- class ExamServiceMock extends VitalExamService {
- ExamServiceMock(super.host);
- static const tcmKey = "HEITCMC";
- String? get userCode => Store.user.userCode;
- @override
- Future<String> createExamAsync(CreateExamRequest request) async {
- final checkBatch =
- await _checkBatch(request.batchNumber!, request.patientCode!);
- if (!checkBatch) {
- return "";
- }
- if (request.code != null && request.code!.isNotEmpty) {
- ExamEntity? entity = await db.repositories.exam
- .singleByCodeWithUserCode(request.code!, userCode!);
- if (entity == null) {
- // 纯在线记录直接不让离线修改
- return "";
- }
- entity.dataJson = request.examData!;
- final result = await db.repositories.exam.update(entity);
- return result > 0 ? entity.code : "";
- }
- final entity = ExamEntity();
- entity.isValid = true;
- entity.syncType = OfflineDataSyncType.create;
- // 本地先生成一个Code,上传后更新
- final uuid = const Uuid().v4().replaceAll('-', '');
- entity.code = "mock_$uuid";
- entity.userCode = userCode!;
- entity.patientCode = request.patientCode!;
- entity.batchNumber = request.batchNumber!;
- entity.physicalEaxmNumber = request.physicalExamNumber!;
- entity.templateKey = request.key!;
- entity.templateCode = request.templateCode ?? "";
- entity.dataJson = request.examData!;
- final id = await db.repositories.exam.insert(entity);
- final success = id > 0;
- if (success) {
- // 统计数量+1
- if (request.key == tcmKey) {
- await db.repositories.patient
- .increaseTCMConsitutionCount(request.patientCode!, userCode!);
- } else {
- await db.repositories.patient
- .increaseExamCount(request.patientCode!, userCode!);
- }
- }
- return success ? entity.code : "";
- }
- /// 目前仅中医体质用
- @override
- Future<bool> updateExamAsync(UpdateExamRequest request) async {
- ExamEntity? entity = await db.repositories.exam
- .singleByCodeWithUserCode(request.code!, userCode!);
- if (entity == null) {
- // TODO: 此时拿不到批次号、体检号等信息,无法创建完整的离线数据,先直接返回失败
- return false;
- }
- entity.dataJson = request.examData!;
- final rows = await db.repositories.exam.update(entity);
- return rows > 0;
- }
- @override
- Future<bool> updateExamByBatchNumberAsync(
- UpdateExamByBatchNumberRequest request) async {
- final entity = await db.repositories.exam.queryable
- .where((x) => [
- x.isValid.equals(true),
- x.batchNumber.equals(request.batchNumber),
- x.patientCode.equals(request.patientCode),
- x.templateKey.equals(request.key),
- ])
- .first();
- if (entity == null) {
- // TODO: 此时拿不到批次号、体检号等信息,无法创建完整的离线数据,先直接返回失败
- return false;
- }
- entity.dataJson = request.examData!;
- final rows = await db.repositories.exam.update(entity);
- return rows > 0;
- }
- @override
- Future<List<ExamDTO>> getExamByBatchNumberAsync(
- GetExamByBatchNumberRequest request) async {
- final entities = await db.repositories.exam.queryable
- .where((x) => [
- x.isValid.equals(true),
- x.userCode.equals(true),
- x.isValid.equals(request.batchNumber),
- ])
- .toList();
- if (entities.isEmpty) {
- return [];
- }
- final patientCode = entities.first.patientCode;
- final patient = await db.repositories.patient
- .singleByCodeWithUserCode(patientCode, userCode!);
- List<ExamDTO> dtos = [];
- for (var entity in entities) {
- final dto = ExamDTO(
- 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,
- examData: entity.dataJson,
- );
- dtos.add(dto);
- }
- return dtos;
- }
- @override
- Future<PageCollection<ExamConclusionDTO>> getPatientExamByPageAsync(
- GetPatientExamByPageRequest request) async {
- var query = db.repositories.exam.queryable.where((x) => [
- x.isValid.equals(true),
- x.userCode.equals(userCode),
- x.patientCode.equals(request.patientCode),
- x.templateKey.equals(request.examKey),
- ]);
- final count = await query.count();
- final offset = request.pageSize * (request.pageIndex - 1);
- final entities = await query
- .orderBy((x) => x.updateTime, DbOrderByType.desc)
- .orderBy((x) => x.createTime, DbOrderByType.desc)
- .offset(offset)
- .limit(request.pageSize)
- .toList();
- List<ExamConclusionDTO> dtos = [];
- if (entities.isNotEmpty) {
- final isTCM = request.examKey == "HEITCMC";
- final patient = await db.repositories.patient
- .singleByCodeWithUserCode(request.patientCode!, userCode!);
- for (var entity in entities) {
- final dto = ExamConclusionDTO(
- 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,
- );
- if (entity.syncState != OfflineDataSyncState.success) {
- dto.contractedDoctor = "NOT_UPLOADED"; // TODO: 先借用一下此字段
- }
- if (isTCM) {
- final jsonMap = jsonDecode(entity.dataJson);
- dto.tcmConclusion = jsonMap["PhysicalConclusion"];
- }
- dtos.add(dto);
- }
- }
- final result = PageCollection<ExamConclusionDTO>(
- currentPage: request.pageIndex,
- pageIndex: request.pageIndex,
- pageSize: request.pageSize,
- totalCount: count,
- dataCount: dtos.length,
- pageData: dtos,
- );
- return result;
- }
- @override
- Future<List<ExamRecordDTO>> getExamRecordListAsync(
- GetExamRecordListRequest request) async {
- final entities = await db.repositories.exam
- .queryPatientAllList(request.patientCode!, userCode!);
- if (entities.isEmpty) {
- return [];
- }
- final patient = await db.repositories.patient
- .singleByCodeWithUserCode(request.patientCode!, userCode!);
- final batchMap = <String, ExamRecordDTO>{};
- for (var entity in entities) {
- if (!batchMap.containsKey(entity.batchNumber)) {
- batchMap[entity.batchNumber] = ExamRecordDTO(
- batchNumber: entity.batchNumber,
- patientName: patient?.name,
- examDoctor: Store.user.displayName,
- examRecordDatas: [],
- );
- }
- final dto = batchMap[entity.batchNumber]!;
- if (dto.examTime == null || (dto.examTime!.isAfter(entity.createTime))) {
- dto.examTime = entity.createTime;
- }
- dto.examRecordDatas!.add(
- ExamRecordDataDTO(
- code: entity.code,
- key: entity.templateKey,
- templateCode: entity.templateCode,
- examData: entity.dataJson,
- ),
- );
- }
- return batchMap.values.toList();
- }
- Future<bool> _checkBatch(String batchNumber, String patientCode) async {
- final hisCount = await db.repositories.examBatch.queryable
- .where((x) => [
- x.isValid.equals(true),
- x.batchNumber.equals(batchNumber),
- x.patientCode.equals(patientCode),
- ])
- .count();
- if (hisCount > 0) {
- // 已存在。不重复创建
- return true;
- }
- final entity = ExamBatchEntity();
- entity.isValid = true;
- entity.batchNumber = batchNumber;
- entity.patientCode = patientCode;
- entity.userCode = Store.user.userCode!;
- final batchId = await db.repositories.examBatch.insert(entity);
- return batchId > 0;
- }
- }
|