123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import 'dart:convert';
- import 'package:fis_common/logger/logger.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'interfaces/csvData.dart';
- import 'interfaces/models/csv_data.dart';
- import 'interfaces/template.dart';
- class CsvDataManager implements ICsvDataManager {
- ///将csv中的数据解析成结构化数据
- @override
- Future<List<CsvRevord>> CsvDataConvert(List<List<dynamic>> datas) async {
- var json = await Get.find<ITemplateManager>()
- .getTemplateByKey("BiochemicalValues");
- List<CsvRevord> csvRevords = [];
- List<dynamic> items = jsonDecode(json);
- List<List<List<dynamic>>> result = _splitIntoChunks(datas);
- for (List<List<dynamic>> chunkDatas in result) {
- List<CsvData> 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;
- }
- csvDatas.add(CsvData(
- key: key,
- value: value,
- des: des,
- unit: unit,
- identifier: identifier,
- referenceRange: referenceRange,
- ));
- }
- } catch (e) {
- logger.e("CsvDataManager csvDataConvert ex:", e);
- }
- }
- csvRevords.add(CsvRevord(csvDatas));
- }
- return csvRevords;
- }
- 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;
- }
- }
|