增加导入文件重复判定

This commit is contained in:
吕继龙 2025-04-15 18:18:59 +08:00
parent f82332fd1f
commit 90245dbb80
4 changed files with 101 additions and 76 deletions

View File

@ -12,10 +12,12 @@
"output": "release" "output": "release"
}, },
"win": { "win": {
"target": "dir" "target": "portable",
"sign": null,
"forceCodeSigning": false
}, },
"npmRebuild": false, "npmRebuild": false,
"asar": false, "asar": true,
"extraMetadata": { "extraMetadata": {
"main": "main.js" "main": "main.js"
} }

View File

@ -9,8 +9,9 @@
"react-start": "react-scripts start", "react-start": "react-scripts start",
"react-build": "react-scripts build", "react-build": "react-scripts build",
"rebuild": "electron-rebuild", "rebuild": "electron-rebuild",
"build": "electron-builder --publish never", "build": "electron-builder --config electron-builder-config.json --publish never",
"package": "npm run react-build && npm run rebuild && npm run build", "package": "npm run react-build && npm run rebuild && npm run build",
"package-portable": "npm run react-build && electron-packager . DataTools --platform=win32 --arch=x64 --out=release --overwrite --asar",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject"
}, },

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { ConfigProvider, theme, Switch, Space, Modal, Progress, message } from 'antd'; import { ConfigProvider, theme, Switch, Space, Modal, Progress, message } from 'antd';
import zhCN from 'antd/locale/zh_CN'; import zhCN from 'antd/locale/zh_CN';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
@ -26,6 +26,7 @@ function App() {
const [currentProject, setCurrentProject] = useState(null); const [currentProject, setCurrentProject] = useState(null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [isDarkMode] = useState(true); // 固定使用深色主题 const [isDarkMode] = useState(true); // 固定使用深色主题
const [lastImportedFilePath, setLastImportedFilePath] = useState(null); // 记录最后导入的文件路径
// 初始化数据 // 初始化数据
useEffect(() => { useEffect(() => {
@ -203,68 +204,22 @@ function App() {
const filePath = await window.electronAPI.selectExcelFile(); const filePath = await window.electronAPI.selectExcelFile();
if (filePath) { if (filePath) {
setLoading(true); // 检查是否重复导入同一个文件
if (lastImportedFilePath === filePath) {
// 创建进度对话框 // 显示确认对话框
const progressModal = Modal.info({ Modal.confirm({
title: '导入Excel', title: '文件已导入',
content: ( content: '该文件已经导入过,重新导入会覆盖之前的记录。确定要继续吗?',
<div> okText: '确定',
<p id="import-progress-message">正在准备导入...</p> cancelText: '取消',
<Progress id="import-progress-bar" percent={0} status="active" /> onOk: () => {
</div> // 用户确认后继续导入
), importFile(filePath);
icon: null,
okButtonProps: { style: { display: 'none' } },
maskClosable: false,
closable: false,
});
// 监听导入进度
window.electronAPI.onImportProgress((data) => {
// 更新进度条和消息
const progressBar = document.getElementById('import-progress-bar');
const progressMessage = document.getElementById('import-progress-message');
if (progressBar && progressMessage) {
// 更新进度条
const antProgress = progressBar.querySelector('.ant-progress-bg');
if (antProgress) {
antProgress.style.width = `${data.progress}%`;
antProgress.setAttribute('aria-valuenow', data.progress);
} }
});
// 更新文本 } else {
progressMessage.textContent = data.message; // 直接导入文件
importFile(filePath);
// 如果导入完成,关闭对话框并重新加载数据
if (data.status === 'complete') {
setTimeout(() => {
progressModal.destroy();
message.success('Excel导入成功');
// 重新加载数据
loadData();
setLoading(false);
}, 1000);
} else if (data.status === 'error') {
setTimeout(() => {
progressModal.destroy();
message.error(`导入失败: ${data.message}`);
setLoading(false);
}, 1000);
}
}
});
// 导入Excel文件
const result = await window.electronAPI.importExcel(filePath);
// 如果导入失败且没有进度事件处理,则显示错误消息
if (!result.success) {
progressModal.destroy();
message.error(`导入失败: ${result.error}`);
console.error('导入Excel文件失败:', result.error);
setLoading(false);
} }
} }
} catch (error) { } catch (error) {
@ -274,6 +229,82 @@ function App() {
} }
}; };
// 导入文件的实际逻辑
const importFile = async (filePath) => {
try {
setLoading(true);
// 创建进度对话框
const progressModal = Modal.info({
title: '导入Excel',
content: (
<div>
<p id="import-progress-message">正在准备导入...</p>
<Progress id="import-progress-bar" percent={0} status="active" />
</div>
),
icon: null,
okButtonProps: { style: { display: 'none' } },
maskClosable: false,
closable: false,
});
// 监听导入进度
window.electronAPI.onImportProgress((data) => {
// 更新进度条和消息
const progressBar = document.getElementById('import-progress-bar');
const progressMessage = document.getElementById('import-progress-message');
if (progressBar && progressMessage) {
// 更新进度条
const antProgress = progressBar.querySelector('.ant-progress-bg');
if (antProgress) {
antProgress.style.width = `${data.progress}%`;
antProgress.setAttribute('aria-valuenow', data.progress);
}
// 更新文本
progressMessage.textContent = data.message;
// 如果导入完成,关闭对话框并重新加载数据
if (data.status === 'complete') {
setTimeout(() => {
progressModal.destroy();
message.success('Excel导入成功');
// 重新加载数据
loadData();
setLoading(false);
}, 1000);
} else if (data.status === 'error') {
setTimeout(() => {
progressModal.destroy();
message.error(`导入失败: ${data.message}`);
setLoading(false);
}, 1000);
}
}
});
// 导入Excel文件
const result = await window.electronAPI.importExcel(filePath);
// 如果导入失败且没有进度事件处理,则显示错误消息
if (!result.success) {
progressModal.destroy();
message.error(`导入失败: ${result.error}`);
console.error('导入Excel文件失败:', result.error);
setLoading(false);
} else {
// 导入成功,更新最后导入的文件路径
setLastImportedFilePath(filePath);
}
} catch (error) {
console.error('导入Excel文件失败:', error);
message.error(`导入失败: ${error.message}`);
setLoading(false);
}
};
// 处理清除数据 // 处理清除数据
const handleClearData = async () => { const handleClearData = async () => {
// 显示确认对话框 // 显示确认对话框

View File

@ -148,16 +148,8 @@ class ExcelService {
// 将数据保存到数据库 // 将数据保存到数据库
saveDataToDatabase(data) { saveDataToDatabase(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 清空现有数据 // 不再清空现有数据,直接插入新数据
this.db.run('DELETE FROM projects', (err) => { const stmt = this.db.prepare(`
if (err) {
console.error('清空项目数据失败:', err);
reject(err);
return;
}
// 插入新数据
const stmt = this.db.prepare(`
INSERT INTO projects ( INSERT INTO projects (
unit, unit,
project_number, project_number,
@ -303,7 +295,6 @@ class ExcelService {
// 完成后关闭准备好的语句 // 完成后关闭准备好的语句
stmt.finalize(); stmt.finalize();
}); });
});
} }
// 构建树状结构 // 构建树状结构