feat: implement IPC handlers, database services and Extractor UI

- Add SqlServerService and MySqlService for database persistence
- Implement IPC handlers for file, extractor, cleaner, and database operations
- Define IPC API types and update preload script
- Create ExtractorPage UI with OrderNumberInput component
- Add unit and integration tests for MySQL and SQL Server
- Update vitest config with path aliases
This commit is contained in:
Misaka
2026-03-01 15:46:01 +08:00
parent c39e1504aa
commit 5760b56f70
23 changed files with 2156 additions and 33 deletions

View File

@@ -1,34 +1,88 @@
import { useState } from 'react'
import Versions from './components/Versions'
import { ExtractorPage } from './pages/ExtractorPage'
import electronLogo from './assets/electron.svg'
type Page = 'home' | 'extractor' | 'cleaner'
function App(): React.JSX.Element {
const ipcHandle = (): void => window.electron.ipcRenderer.send('ping')
const [currentPage, setCurrentPage] = useState<Page>('home')
const renderPage = () => {
switch (currentPage) {
case 'extractor':
return <ExtractorPage />
default:
return (
<>
<img alt="logo" className="logo" src={electronLogo} />
<div className="creator">Powered by electron-vite</div>
<div className="text">
Build an Electron app with <span className="react">React</span>
&nbsp;and <span className="ts">TypeScript</span>
</div>
<p className="tip">
Please try pressing <code>F12</code> to open the devTool
</p>
<div className="actions">
<div className="action">
<a
href="#"
onClick={(e) => {
e.preventDefault()
setCurrentPage('extractor')
}}
>
</a>
</div>
<div className="action">
<a href="https://electron-vite.org/" target="_blank" rel="noreferrer">
Documentation
</a>
</div>
</div>
<Versions />
</>
)
}
}
return (
<>
<img alt="logo" className="logo" src={electronLogo} />
<div className="creator">Powered by electron-vite</div>
<div className="text">
Build an Electron app with <span className="react">React</span>
&nbsp;and <span className="ts">TypeScript</span>
</div>
<p className="tip">
Please try pressing <code>F12</code> to open the devTool
</p>
<div className="actions">
<div className="action">
<a href="https://electron-vite.org/" target="_blank" rel="noreferrer">
Documentation
</a>
</div>
<div className="action">
<a target="_blank" rel="noreferrer" onClick={ipcHandle}>
Send IPC
</a>
</div>
</div>
<Versions></Versions>
</>
<div className="app">
{currentPage !== 'home' && (
<nav className="nav">
<button className="nav-btn" onClick={() => setCurrentPage('home')}>
</button>
</nav>
)}
{renderPage()}
<style>{`
.app {
min-height: 100vh;
background: #f5f7fa;
}
.nav {
background: #fff;
padding: 12px 24px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.nav-btn {
background: #1890ff;
color: #fff;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
transition: background 0.3s;
}
.nav-btn:hover {
background: #40a9ff;
}
`}</style>
</div>
)
}

View File

@@ -0,0 +1,92 @@
import React, { useState, useEffect } from 'react'
interface OrderNumberInputProps {
value: string
onChange: (value: string) => void
placeholder?: string
label?: string
}
/**
* OrderNumberInput - A textarea component for entering line-separated order numbers
* Displays a count of valid order numbers entered
*/
export const OrderNumberInput: React.FC<OrderNumberInputProps> = ({
value,
onChange,
placeholder = '请输入订单号,每行一个',
label = '订单号列表'
}) => {
const [count, setCount] = useState(0)
useEffect(() => {
// Count non-empty lines
const lines = value.split('\n').filter((line) => line.trim().length > 0)
setCount(lines.length)
}, [value])
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
onChange(e.target.value)
}
return (
<div className="order-number-input">
<div className="input-header">
<label>{label}</label>
<span className="count-badge">{count} </span>
</div>
<textarea
value={value}
onChange={handleChange}
placeholder={placeholder}
rows={10}
className="order-textarea"
/>
<style>{`
.order-number-input {
margin-bottom: 16px;
}
.input-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.input-header label {
font-weight: 600;
font-size: 14px;
color: #333;
}
.count-badge {
background: #e6f7ff;
color: #1890ff;
padding: 2px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.order-textarea {
width: 100%;
padding: 12px;
border: 1px solid #d9d9d9;
border-radius: 6px;
font-size: 14px;
font-family: 'Consolas', 'Monaco', monospace;
resize: vertical;
transition: border-color 0.3s;
box-sizing: border-box;
}
.order-textarea:focus {
outline: none;
border-color: #1890ff;
box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}
.order-textarea::placeholder {
color: #bfbfbf;
}
`}</style>
</div>
)
}
export default OrderNumberInput

View File

@@ -0,0 +1,357 @@
import React, { useState } from 'react'
import { OrderNumberInput } from '../components/OrderNumberInput'
// Extractor result type (matches the type from main process)
interface ExtractorResult {
downloadedFiles: string[]
mergedFile: string | null
recordCount: number
errors: string[]
}
interface ExtractorProgress {
message: string
progress: number
}
/**
* ExtractorPage - Main page for ERP data extraction
*/
export const ExtractorPage: React.FC = () => {
const [orderNumbers, setOrderNumbers] = useState('')
const [batchSize, setBatchSize] = useState(100)
const [isRunning, setIsRunning] = useState(false)
const [progress, setProgress] = useState<ExtractorProgress | null>(null)
const [result, setResult] = useState<ExtractorResult | null>(null)
const [error, setError] = useState<string | null>(null)
const handleExtract = async () => {
if (!orderNumbers.trim()) {
setError('请输入至少一个订单号')
return
}
setIsRunning(true)
setProgress(null)
setResult(null)
setError(null)
try {
const orderNumberList = orderNumbers
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0)
// Call extractor API through electron
const response = await window.electron.extractor.runExtractor({
orderNumbers: orderNumberList,
batchSize
})
if (response.success && response.data) {
setResult(response.data)
} else {
setError(response.error || '提取失败')
}
} catch (err) {
setError(err instanceof Error ? err.message : '发生未知错误')
} finally {
setIsRunning(false)
setProgress(null)
}
}
const handleReset = () => {
setOrderNumbers('')
setBatchSize(100)
setResult(null)
setError(null)
setProgress(null)
}
return (
<div className="extractor-page">
<h1 className="page-title">ERP </h1>
<div className="extractor-content">
{/* Input Section */}
<div className="input-section">
<OrderNumberInput
value={orderNumbers}
onChange={setOrderNumbers}
label="订单号列表"
placeholder="请输入订单号,每行一个&#10;例如:&#10;SC70202602120085&#10;SC70202602120120&#10;SC70202602120137"
/>
<div className="batch-size-input">
<label></label>
<input
type="number"
value={batchSize}
onChange={(e) => setBatchSize(parseInt(e.target.value) || 100)}
min={1}
max={1000}
disabled={isRunning}
/>
<span className="hint"></span>
</div>
<div className="button-group">
<button
className="btn btn-primary"
onClick={handleExtract}
disabled={isRunning || !orderNumbers.trim()}
>
{isRunning ? '提取中...' : '开始提取'}
</button>
<button className="btn btn-secondary" onClick={handleReset} disabled={isRunning}>
</button>
</div>
</div>
{/* Progress Section */}
{progress && (
<div className="progress-section">
<div className="progress-bar">
<div className="progress-fill" style={{ width: `${progress.progress}%` }} />
</div>
<p className="progress-message">{progress.message}</p>
</div>
)}
{/* Error Section */}
{error && (
<div className="error-section">
<h3></h3>
<p>{error}</p>
</div>
)}
{/* Result Section */}
{result && (
<div className="result-section">
<h3></h3>
<div className="result-stats">
<div className="stat-item">
<span className="stat-label"></span>
<span className="stat-value">{result.downloadedFiles.length}</span>
</div>
<div className="stat-item">
<span className="stat-label"></span>
<span className="stat-value">{result.recordCount}</span>
</div>
<div className="stat-item">
<span className="stat-label"></span>
<span className="stat-value error">{result.errors.length}</span>
</div>
</div>
{result.downloadedFiles.length > 0 && (
<div className="file-list">
<h4></h4>
<ul>
{result.downloadedFiles.map((file, index) => (
<li key={index}>{file}</li>
))}
</ul>
</div>
)}
{result.errors.length > 0 && (
<div className="error-list">
<h4></h4>
<ul>
{result.errors.map((err, index) => (
<li key={index} className="error-item">
{err}
</li>
))}
</ul>
</div>
)}
</div>
)}
</div>
<style>{`
.extractor-page {
padding: 24px;
max-width: 800px;
margin: 0 auto;
}
.page-title {
font-size: 24px;
font-weight: 600;
color: #333;
margin-bottom: 24px;
}
.extractor-content {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
padding: 24px;
}
.input-section {
margin-bottom: 24px;
}
.batch-size-input {
margin-bottom: 16px;
display: flex;
align-items: center;
gap: 12px;
}
.batch-size-input label {
font-weight: 500;
font-size: 14px;
}
.batch-size-input input {
padding: 6px 12px;
border: 1px solid #d9d9d9;
border-radius: 4px;
font-size: 14px;
width: 100px;
}
.batch-size-input input:disabled {
background: #f5f5f5;
cursor: not-allowed;
}
.batch-size-input .hint {
font-size: 12px;
color: #999;
}
.button-group {
display: flex;
gap: 12px;
}
.btn {
padding: 10px 24px;
border: none;
border-radius: 6px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s;
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.btn-primary {
background: #1890ff;
color: #fff;
}
.btn-primary:hover:not(:disabled) {
background: #40a9ff;
}
.btn-secondary {
background: #f5f5f5;
color: #666;
}
.btn-secondary:hover:not(:disabled) {
background: #e8e8e8;
}
.progress-section {
margin-top: 24px;
padding: 16px;
background: #f5f5f5;
border-radius: 6px;
}
.progress-bar {
height: 8px;
background: #e8e8e8;
border-radius: 4px;
overflow: hidden;
margin-bottom: 8px;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #1890ff, #40a9ff);
transition: width 0.3s;
}
.progress-message {
font-size: 14px;
color: #666;
margin: 0;
}
.error-section {
margin-top: 24px;
padding: 16px;
background: #fff1f0;
border: 1px solid #ffa39e;
border-radius: 6px;
}
.error-section h3 {
color: #ff4d4f;
margin: 0 0 8px 0;
font-size: 16px;
}
.error-section p {
color: #666;
margin: 0;
}
.result-section {
margin-top: 24px;
padding: 16px;
background: #f6ffed;
border: 1px solid #b7eb8f;
border-radius: 6px;
}
.result-section h3 {
color: #52c41a;
margin: 0 0 16px 0;
font-size: 16px;
}
.result-stats {
display: flex;
gap: 24px;
margin-bottom: 16px;
}
.stat-item {
display: flex;
flex-direction: column;
gap: 4px;
}
.stat-label {
font-size: 12px;
color: #999;
}
.stat-value {
font-size: 24px;
font-weight: 600;
color: #333;
}
.stat-value.error {
color: #ff4d4f;
}
.file-list, .error-list {
margin-top: 16px;
}
.file-list h4, .error-list h4 {
font-size: 14px;
color: #666;
margin-bottom: 8px;
}
.file-list ul, .error-list ul {
list-style: none;
padding: 0;
margin: 0;
}
.file-list li, .error-list li {
padding: 6px 12px;
background: #fff;
border-radius: 4px;
margin-bottom: 4px;
font-size: 13px;
font-family: 'Consolas', 'Monaco', monospace;
}
.error-list li {
background: #fff1f0;
color: #ff4d4f;
}
`}</style>
</div>
)
}
export default ExtractorPage