增加导入文件重复判定
This commit is contained in:
parent
f82332fd1f
commit
90245dbb80
|
|
@ -12,10 +12,12 @@
|
|||
"output": "release"
|
||||
},
|
||||
"win": {
|
||||
"target": "dir"
|
||||
"target": "portable",
|
||||
"sign": null,
|
||||
"forceCodeSigning": false
|
||||
},
|
||||
"npmRebuild": false,
|
||||
"asar": false,
|
||||
"asar": true,
|
||||
"extraMetadata": {
|
||||
"main": "main.js"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@
|
|||
"react-start": "react-scripts start",
|
||||
"react-build": "react-scripts build",
|
||||
"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-portable": "npm run react-build && electron-packager . DataTools --platform=win32 --arch=x64 --out=release --overwrite --asar",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
},
|
||||
|
|
|
|||
155
src/App.js
155
src/App.js
|
|
@ -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 zhCN from 'antd/locale/zh_CN';
|
||||
import dayjs from 'dayjs';
|
||||
|
|
@ -26,6 +26,7 @@ function App() {
|
|||
const [currentProject, setCurrentProject] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [isDarkMode] = useState(true); // 固定使用深色主题
|
||||
const [lastImportedFilePath, setLastImportedFilePath] = useState(null); // 记录最后导入的文件路径
|
||||
|
||||
// 初始化数据
|
||||
useEffect(() => {
|
||||
|
|
@ -203,68 +204,22 @@ function App() {
|
|||
const filePath = await window.electronAPI.selectExcelFile();
|
||||
|
||||
if (filePath) {
|
||||
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);
|
||||
// 检查是否重复导入同一个文件
|
||||
if (lastImportedFilePath === filePath) {
|
||||
// 显示确认对话框
|
||||
Modal.confirm({
|
||||
title: '文件已导入',
|
||||
content: '该文件已经导入过,重新导入会覆盖之前的记录。确定要继续吗?',
|
||||
okText: '确定',
|
||||
cancelText: '取消',
|
||||
onOk: () => {
|
||||
// 用户确认后继续导入
|
||||
importFile(filePath);
|
||||
}
|
||||
|
||||
// 更新文本
|
||||
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 {
|
||||
// 直接导入文件
|
||||
importFile(filePath);
|
||||
}
|
||||
}
|
||||
} 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 () => {
|
||||
// 显示确认对话框
|
||||
|
|
|
|||
|
|
@ -148,16 +148,8 @@ class ExcelService {
|
|||
// 将数据保存到数据库
|
||||
saveDataToDatabase(data) {
|
||||
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 (
|
||||
unit,
|
||||
project_number,
|
||||
|
|
@ -303,7 +295,6 @@ class ExcelService {
|
|||
// 完成后关闭准备好的语句
|
||||
stmt.finalize();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 构建树状结构
|
||||
|
|
|
|||
Loading…
Reference in New Issue