123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- import 'dart:convert';
- import 'package:fis_common/index.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'package:excel/excel.dart';
- import 'package:intl/intl.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'interfaces/excelData.dart';
- import 'interfaces/models/exce_keys.dart';
- import 'interfaces/models/excel_data.dart';
- import 'interfaces/template.dart';
- class ExcelDataManager implements IExcelDataManager {
- final List<String> _bloodRoutineKeys = [
- "Blood_Hgb",
- "Blood_Wbc",
- "ALC",
- "AMC",
- "ANC",
- "AEC",
- "Blood_NeutrophilRatio",
- "Blood_LymphocyteRatio",
- "Blood_MonocyteRatio",
- "EOS",
- "RBC",
- "Blood_Mchc",
- "Urea",
- "Blood_Hematocrit",
- "Blood_McV",
- "Blood_Mch",
- "Blood_Rlt",
- "Blood_MeanPlateletVolume",
- "Blood_PlateletDW",
- "Blood_PlateletPct"
- ];
- final List<String> _biochemicalKeys = [
- "Liver_Alt",
- "Liver_Ast",
- "Total_Protein",
- "Liver_Alb",
- "Liver_Tbil",
- "directBili",
- "GGT",
- "Renal_Cr",
- "UricAcid",
- "Urea",
- "BCH_Glu",
- "Lipid_Cho",
- "Lipid_Tg",
- "Lipid_Ldl",
- "Lipid_Hdl",
- "AGratio",
- "ALP",
- "UB",
- "Globulin",
- ];
- final List<String> biochemicalValues_CQZYKeys = [
- "Liver_Alt",
- "Liver_Ast",
- "Liver_Tbil",
- "Liver_Dbil",
- "Liver_Alb",
- "Urea",
- "Lipid_Cho",
- "Lipid_Tg",
- "Lipid_Ldl",
- "Lipid_Hdl",
- "BCH_Glu",
- "Renal_Cr",
- ];
- ///将csv中的数据解析成结构化数据
- @override
- Future<List<ExcelDataRecord>> CsvDataConvert(
- List<List<dynamic>> datas, String key) async {
- List<String> keys = [];
- if (key.contains("BiochemicalValues_CQZY")) {
- keys = biochemicalValues_CQZYKeys;
- } else if (key.contains("Biochemical")) {}
- String json = '';
- json = await Get.find<ITemplateManager>().getTemplateByKey(key);
- if (kDebugMode) {
- // String path = "assets/BiochemicalValues_CQZY.json";
- // json = await rootBundle.loadString(path);
- }
- List<ExcelDataRecord> csvRevords = [];
- List<dynamic> items = [];
- items = jsonDecode(json);
- if (kDebugMode) {
- // items = jsonDecode(json)["Content"];
- }
- List<List<List<dynamic>>> result = _splitIntoChunks(datas);
- for (List<List<dynamic>> chunkDatas in result) {
- List<ExcelDataItem> csvDatas = [];
- for (var item in items) {
- try {
- if (item is Map) {
- int row = item["row"];
- int column = item["column"];
- String key = item["key"];
- String des = item["des"];
- String identifier = item["identifier"];
- String referenceRange = item["referenceRange"];
- String unit = item["unit"];
- String value = "";
- if (chunkDatas.length >= row + 1) {
- var rowValue = chunkDatas[row];
- if (rowValue.length >= column + 1) {
- value = chunkDatas[row][column].toString().trim();
- } else {
- continue;
- }
- } else {
- continue;
- }
- ///过滤不需要展示的数据
- if (keys.contains(key) ||
- key == "sampleBarcode" ||
- key == "patientName" ||
- key == "samplingTime") {
- csvDatas.add(ExcelDataItem(
- key: key,
- value: value,
- des: des,
- unit: unit,
- identifier: identifier,
- referenceRange: referenceRange,
- ));
- }
- }
- } catch (e) {
- logger.e("CsvDataManager csvDataConvert ex:", e);
- }
- }
- csvRevords.add(ExcelDataRecord(csvDatas));
- }
- return csvRevords;
- }
- @override
- Future<List<ExcelDataRecord>> ExcelDataConvert(
- List<List<Data?>> rows, String templateKey) async {
- ///首行是标题
- rows.removeAt(0);
- List<ExcelDataRecord> records = [];
- var json = await Get.find<ITemplateManager>().getTemplateByKey(templateKey);
- List<dynamic> items = jsonDecode(json);
- List<String> keys = [];
- if (templateKey.contains("BloodRoutine")) {
- keys = _bloodRoutineKeys;
- } else if (templateKey.contains("Biochemical")) {
- keys = _biochemicalKeys;
- }
- for (List<Data?> rowData in rows) {
- List<ExcelDataItem> csvDatas = [];
- for (var item in items) {
- try {
- if (item is Map) {
- String key = item["key"];
- ///过滤不需要展示的数据
- if (keys.contains(key) ||
- key == "sampleBarcode" ||
- key == "patientName" ||
- key == "samplingTime") {
- int column = item["column"];
- String des = item["des"];
- String unit = item["unit"];
- String identifier = item["identifier"];
- String referenceRange = item["referenceRange"];
- String value = "";
- if (key == "Blood_Wbc") {
- print("sampleBarcode");
- }
- if (rowData.length >= column + 1) {
- Data? data = rowData[column];
- if (data == null) {
- continue;
- } else {
- value = data.value.toString();
- }
- csvDatas.add(ExcelDataItem(
- key: key,
- value: value,
- des: des,
- unit: unit,
- identifier: identifier,
- referenceRange: referenceRange,
- ));
- }
- }
- }
- } catch (e) {
- logger.e("CsvDataManager ExcelDataConvert ex:", e);
- }
- }
- records.add(ExcelDataRecord(csvDatas));
- }
- return records;
- }
- @override
- Future<bool> uploadDatas(
- List<ExcelDataRecord> datas, String templateKey) async {
- try {
- List<String> keys = [];
- if (templateKey.contains("BiochemicalValues_CQZY")) {
- keys = biochemicalValues_CQZYKeys;
- } else if (templateKey.contains("BloodRoutine")) {
- keys = _bloodRoutineKeys;
- } else if (templateKey.contains("Biochemical")) {
- keys = _biochemicalKeys;
- }
- if (ExcelTemplateKeys.AllBiochemicalKeys.contains(templateKey)) {
- templateKey = "HEIBiochemical";
- } else if (ExcelTemplateKeys.AllBloodRoutineKeys.contains(templateKey)) {
- templateKey = "HEIBloodRoutine";
- }
- List<AddExamDTO> examInfos = [];
- for (ExcelDataRecord data in datas) {
- List<ExcelDataItem> items = data.excelDatas;
- var physicalExamNumber =
- items.firstWhere((element) => element.key == "sampleBarcode").value;
- String examTimeStr =
- items.firstWhere((element) => element.key == "samplingTime").value;
- // 使用DateFormat格式化日期,去除时间部分
- DateFormat dateOnlyFormat = DateFormat('yyyy-MM-dd');
- DateTime dateOnlyDateTime = dateOnlyFormat.parse(examTimeStr);
- var examDTO = AddExamDTO(
- key: templateKey,
- examNumber: physicalExamNumber,
- examTime: dateOnlyDateTime,
- examData: data.toJson(keys),
- );
- examInfos.add(examDTO);
- }
- if (examInfos.length <= 200) {
- await rpc.vitalExam.batchAddExamAsync(BatchAddExamRequest(
- examInfos: examInfos,
- token: Store.user.token,
- ));
- } else {
- // 计算需要拆分成多少个小组
- int groupCount = (examInfos.length / 200).ceil();
- // 遍历并拆分列表
- for (int i = 0; i < groupCount; i++) {
- // 计算当前小组的起始索引和结束索引
- int startIndex = i * 200;
- int endIndex = (i + 1) * 200;
- // 如果已经到达列表的末尾,设置结束索引为列表的长度
- if (endIndex > examInfos.length) {
- endIndex = examInfos.length;
- }
- // 使用 getRange 方法获取当前小组的数据
- List<AddExamDTO> group =
- examInfos.getRange(startIndex, endIndex).toList();
- await rpc.vitalExam.batchAddExamAsync(BatchAddExamRequest(
- examInfos: group,
- token: Store.user.token,
- ));
- }
- }
- } catch (e) {
- logger.e("ExcelDataManager uploadDatas ex:", e);
- return false;
- }
- return true;
- }
- List<List<List<dynamic>>> _splitIntoChunks(List<List<dynamic>> datas) {
- List<List<List<dynamic>>> records = [];
- List<List<dynamic>> currentRecord = [];
- for (int i = 0; i < datas.length; i++) {
- // Check if the current element and the next one are empty lists
- if ((datas[i].length == 1 && datas[i][0].isEmpty) &&
- i + 1 < datas.length &&
- (datas[i + 1].length == 1 && datas[i + 1][0].isEmpty)) {
- // Skip the next empty list as it is part of the separator
- i++;
- // Add the current record to records if it is not empty
- if (currentRecord.isNotEmpty) {
- records.add(List.from(currentRecord));
- currentRecord.clear();
- }
- } else {
- // Add the current data to the current record
- currentRecord.add(datas[i]);
- }
- }
- // Add the last record if it's not empty (case when no trailing empty lists)
- if (currentRecord.isNotEmpty) {
- records.add(currentRecord);
- }
- return records;
- }
- }
|