|
|
|
@ -1,4 +1,5 @@
@@ -1,4 +1,5 @@
|
|
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
|
import 'package:win_text_editor/framework/controllers/logger.dart'; |
|
|
|
|
import 'package:win_text_editor/shared/base/base_content_controller.dart'; |
|
|
|
|
import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
|
|
|
|
|
|
|
|
@ -13,53 +14,56 @@ class DataCompareController extends BaseContentController {
@@ -13,53 +14,56 @@ class DataCompareController extends BaseContentController {
|
|
|
|
|
dataSource = DataCompareDataSource(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void importLeftTable(String csvContent) { |
|
|
|
|
final lines = csvContent.split('\n'); |
|
|
|
|
if (lines.isEmpty) return; |
|
|
|
|
|
|
|
|
|
// Parse headers (skip first column which is for serial number) |
|
|
|
|
leftColumns = lines[0].split(',').skip(1).toList(); |
|
|
|
|
|
|
|
|
|
// Parse data |
|
|
|
|
leftData = []; |
|
|
|
|
for (int i = 1; i < lines.length; i++) { |
|
|
|
|
if (lines[i].trim().isEmpty) continue; |
|
|
|
|
final values = lines[i].split(','); |
|
|
|
|
final row = {'serial': i.toString(), 'key': values[0]}; |
|
|
|
|
for (int j = 0; j < leftColumns.length; j++) { |
|
|
|
|
row[leftColumns[j]] = j + 1 < values.length ? values[j + 1] : ''; |
|
|
|
|
} |
|
|
|
|
leftData.add(row); |
|
|
|
|
} |
|
|
|
|
Future<void> importCsv(bool isLeftTable, String csvContent) async { |
|
|
|
|
try { |
|
|
|
|
// 1. 确定分隔符 |
|
|
|
|
final lines = csvContent.split('\n'); |
|
|
|
|
if (lines.isEmpty) return; |
|
|
|
|
|
|
|
|
|
_compareData(); |
|
|
|
|
notifyListeners(); |
|
|
|
|
} |
|
|
|
|
String delimiter = lines[0].contains(',') ? ',' : '\t'; |
|
|
|
|
|
|
|
|
|
// 2. 解析列标题(从第一行第二列开始) |
|
|
|
|
final headers = lines[0].split(delimiter).skip(1).toList(); |
|
|
|
|
|
|
|
|
|
// 3. 解析数据 |
|
|
|
|
List<Map<String, dynamic>> data = []; |
|
|
|
|
for (int i = 1; i < lines.length; i++) { |
|
|
|
|
if (lines[i].trim().isEmpty) continue; |
|
|
|
|
|
|
|
|
|
final values = lines[i].split(delimiter); |
|
|
|
|
if (values.length < 1) continue; |
|
|
|
|
|
|
|
|
|
void importRightTable(String csvContent) { |
|
|
|
|
final lines = csvContent.split('\n'); |
|
|
|
|
if (lines.isEmpty) return; |
|
|
|
|
|
|
|
|
|
// Parse headers (skip first column which is for serial number) |
|
|
|
|
rightColumns = lines[0].split(',').skip(1).toList(); |
|
|
|
|
|
|
|
|
|
// Parse data |
|
|
|
|
rightData = []; |
|
|
|
|
for (int i = 1; i < lines.length; i++) { |
|
|
|
|
if (lines[i].trim().isEmpty) continue; |
|
|
|
|
final values = lines[i].split(','); |
|
|
|
|
final row = {'serial': i.toString(), 'key': values[0]}; |
|
|
|
|
for (int j = 0; j < rightColumns.length; j++) { |
|
|
|
|
row[rightColumns[j]] = j + 1 < values.length ? values[j + 1] : ''; |
|
|
|
|
// 创建行数据:序号、主键和各列值 |
|
|
|
|
final row = { |
|
|
|
|
'serial': i.toString(), // 序号 |
|
|
|
|
'key': values[0], // 主键(第一列) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// 填充各列数据 |
|
|
|
|
for (int j = 0; j < headers.length; j++) { |
|
|
|
|
row[headers[j]] = (j + 1 < values.length) ? values[j + 1] : ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data.add(row); |
|
|
|
|
} |
|
|
|
|
rightData.add(row); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_compareData(); |
|
|
|
|
notifyListeners(); |
|
|
|
|
if (isLeftTable) { |
|
|
|
|
leftColumns = headers; |
|
|
|
|
leftData = data; |
|
|
|
|
} else { |
|
|
|
|
rightColumns = headers; |
|
|
|
|
rightData = data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
compareData(); |
|
|
|
|
notifyListeners(); |
|
|
|
|
} catch (e) { |
|
|
|
|
Logger().error('${isLeftTable ? '左表' : '右表'}数据导入失败: $e'); |
|
|
|
|
rethrow; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void _compareData() { |
|
|
|
|
void compareData() { |
|
|
|
|
// Implement comparison logic here |
|
|
|
|
// This would update the comparison status for each row |
|
|
|
|
dataSource.updateData(leftData, rightData); |
|
|
|
|