增加导入文件重复判定

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,6 +204,34 @@ function App() {
const filePath = await window.electronAPI.selectExcelFile(); const filePath = await window.electronAPI.selectExcelFile();
if (filePath) { if (filePath) {
// 检查是否重复导入同一个文件
if (lastImportedFilePath === filePath) {
// 显示确认对话框
Modal.confirm({
title: '文件已导入',
content: '该文件已经导入过,重新导入会覆盖之前的记录。确定要继续吗?',
okText: '确定',
cancelText: '取消',
onOk: () => {
// 用户确认后继续导入
importFile(filePath);
}
});
} else {
// 直接导入文件
importFile(filePath);
}
}
} catch (error) {
console.error('导入Excel文件失败:', error);
message.error(`导入失败: ${error.message}`);
setLoading(false);
}
};
// 导入文件的实际逻辑
const importFile = async (filePath) => {
try {
setLoading(true); setLoading(true);
// 创建进度对话框 // 创建进度对话框
@ -265,7 +294,9 @@ function App() {
message.error(`导入失败: ${result.error}`); message.error(`导入失败: ${result.error}`);
console.error('导入Excel文件失败:', result.error); console.error('导入Excel文件失败:', result.error);
setLoading(false); setLoading(false);
} } else {
// 导入成功,更新最后导入的文件路径
setLastImportedFilePath(filePath);
} }
} catch (error) { } catch (error) {
console.error('导入Excel文件失败:', error); console.error('导入Excel文件失败:', error);

View File

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