Compare commits
67 Commits
5d8563a4c9
...
v1.12.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c6bb85e67 | ||
|
|
1a48dca57c | ||
|
|
e91b7308a7 | ||
|
|
35cad8baa9 | ||
|
|
6f49596467 | ||
|
|
95eb44979a | ||
|
|
420f811488 | ||
|
|
6aa1fc29e5 | ||
|
|
151485caed | ||
|
|
116539ff42 | ||
|
|
0286df94dd | ||
|
|
32931cecad | ||
|
|
b95f12fca1 | ||
|
|
4c40457c71 | ||
|
|
bd68444a74 | ||
|
|
7dfa88c2a3 | ||
|
|
998edb4b84 | ||
|
|
74096fbfa0 | ||
|
|
73656dada8 | ||
|
|
9a91658121 | ||
|
|
a924e8a4e8 | ||
|
|
b363a53d8a | ||
|
|
e1d55b8b39 | ||
|
|
8b173890fa | ||
|
|
b065e23306 | ||
|
|
1c0a000a67 | ||
|
|
6b3c62268a | ||
|
|
bb86208d32 | ||
|
|
3be7959067 | ||
|
|
91f29a1167 | ||
|
|
abad61758c | ||
|
|
d0c745e243 | ||
|
|
fe6cdbf076 | ||
|
|
188117e5ce | ||
|
|
0560b3c84a | ||
|
|
fb3bdbc493 | ||
|
|
c6f67e49a4 | ||
|
|
d16f2d1af0 | ||
|
|
4150a13175 | ||
|
|
d0f8ad0fef | ||
|
|
4a7c220baa | ||
|
|
f51cae0f6f | ||
|
|
7601b5f176 | ||
|
|
e2669af870 | ||
|
|
e54d94fce2 | ||
|
|
9791a84047 | ||
|
|
b5ba18b595 | ||
|
|
13fb7bcf46 | ||
|
|
0ca17a1807 | ||
|
|
54a3ac680a | ||
|
|
16b2882729 | ||
|
|
e97ec63433 | ||
|
|
9556891dea | ||
|
|
fa57f9e564 | ||
|
|
9300f3455f | ||
|
|
7e521da3f1 | ||
|
|
130e0602d1 | ||
|
|
0956bf907f | ||
|
|
6c730616b8 | ||
|
|
4f4e5fd91a | ||
|
|
ae29f38d24 | ||
|
|
406a8dfd2f | ||
|
|
9086aa753f | ||
|
|
fc71b2a585 | ||
|
|
75f0105167 | ||
|
|
8386309fff | ||
|
|
d7ebb10f38 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -45,3 +45,7 @@ nul
|
|||||||
|
|
||||||
# TypeScript incremental compilation cache
|
# TypeScript incremental compilation cache
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# temporary files
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
# 部署说明:
|
# 部署说明:
|
||||||
# 1. 复制此文件为 config.yaml
|
# 1. 复制此文件为 config.yaml
|
||||||
# 2. 根据实际环境修改配置值
|
# 2. 根据实际环境修改配置值
|
||||||
# 3. 设置 database.activeType 为 mysql 或 sqlserver
|
# 3. 设置 database.activeType 为 mysql、sqlserver 或 postgresql
|
||||||
# ================================
|
# ================================
|
||||||
# 注意:ERP 认证信息存储在数据库 (dbo_BIPUsers) 中,按用户管理
|
# 注意:ERP 认证信息存储在数据库 (dbo_BIPUsers) 中,按用户管理
|
||||||
# ================================
|
# ================================
|
||||||
@@ -29,6 +29,14 @@ database:
|
|||||||
driver: 'ODBC Driver 18 for SQL Server'
|
driver: 'ODBC Driver 18 for SQL Server'
|
||||||
trustServerCertificate: true
|
trustServerCertificate: true
|
||||||
|
|
||||||
|
postgresql:
|
||||||
|
host: <PG_HOST>
|
||||||
|
port: 5432
|
||||||
|
database: <DATABASE_NAME>
|
||||||
|
username: <USERNAME>
|
||||||
|
password: <PASSWORD>
|
||||||
|
maxPoolSize: 10
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
dataDir: './data/'
|
dataDir: './data/'
|
||||||
defaultOutput: 'output.xlsx'
|
defaultOutput: 'output.xlsx'
|
||||||
|
|||||||
@@ -11,20 +11,20 @@
|
|||||||
|
|
||||||
### 失败测试分布
|
### 失败测试分布
|
||||||
|
|
||||||
| 测试文件 | 失败数量 | 根因分类 | 预计工时 |
|
| 测试文件 | 失败数量 | 根因分类 | 预计工时 |
|
||||||
|---------|---------|---------|---------|
|
| -------------------------- | --------------- | ------------------------------ | --------- |
|
||||||
| `logger.test.ts` | 11 failures | Winston format mock + 循环依赖 | 2-3h |
|
| `logger.test.ts` | 11 failures | Winston format mock + 循环依赖 | 2-3h |
|
||||||
| `update-service.test.ts` | 1 failure | Mock 参数不匹配 | 30min |
|
| `update-service.test.ts` | 1 failure | Mock 参数不匹配 | 30min |
|
||||||
| `update-installer.test.ts` | 1 failure | 路径断言错误 | 15min |
|
| `update-installer.test.ts` | 1 failure | 路径断言错误 | 15min |
|
||||||
| **总计** | **13 failures** | - | **~3-4h** |
|
| **总计** | **13 failures** | - | **~3-4h** |
|
||||||
|
|
||||||
### 测试通过率
|
### 测试通过率
|
||||||
|
|
||||||
| 指标 | 当前 | 修复后 |
|
| 指标 | 当前 | 修复后 |
|
||||||
|------|------|--------|
|
| -------- | ------------- | -------------- |
|
||||||
| 失败套件 | 3 suites | 0 suites |
|
| 失败套件 | 3 suites | 0 suites |
|
||||||
| 失败测试 | 13 tests | 0 tests |
|
| 失败测试 | 13 tests | 0 tests |
|
||||||
| 通过率 | 95% (311/327) | 100% (327/327) |
|
| 通过率 | 95% (311/327) | 100% (327/327) |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -42,6 +42,7 @@
|
|||||||
#### 问题诊断
|
#### 问题诊断
|
||||||
|
|
||||||
**失败模式**:
|
**失败模式**:
|
||||||
|
|
||||||
```
|
```
|
||||||
TypeError: __vite_ssr_import_0__.default.format(...) is not a function
|
TypeError: __vite_ssr_import_0__.default.format(...) is not a function
|
||||||
at src/main/services/logger/index.ts:114:4
|
at src/main/services/logger/index.ts:114:4
|
||||||
@@ -54,6 +55,7 @@ TypeError: __vite_ssr_import_0__.default.format(...) is not a function
|
|||||||
3. **具体表现**: 第 114 行的 `winston.format()` 链式调用在 mock 环境中返回 undefined
|
3. **具体表现**: 第 114 行的 `winston.format()` 链式调用在 mock 环境中返回 undefined
|
||||||
|
|
||||||
**调用栈**:
|
**调用栈**:
|
||||||
|
|
||||||
```
|
```
|
||||||
logger.test.ts
|
logger.test.ts
|
||||||
→ imports logger.ts
|
→ imports logger.ts
|
||||||
@@ -63,6 +65,7 @@ logger.test.ts
|
|||||||
```
|
```
|
||||||
|
|
||||||
**文件位置**:
|
**文件位置**:
|
||||||
|
|
||||||
- 测试文件:`tests/unit/logger.test.ts`
|
- 测试文件:`tests/unit/logger.test.ts`
|
||||||
- 被 mock 文件:`src/main/services/logger/index.ts:100-116`
|
- 被 mock 文件:`src/main/services/logger/index.ts:100-116`
|
||||||
- Setup mock: `tests/setup.ts` (无 winston mock 冲突)
|
- Setup mock: `tests/setup.ts` (无 winston mock 冲突)
|
||||||
@@ -135,11 +138,13 @@ vi.mock('winston', () => ({
|
|||||||
**方案 B: 将 logger.test.ts 转为集成测试 (2 小时)**
|
**方案 B: 将 logger.test.ts 转为集成测试 (2 小时)**
|
||||||
|
|
||||||
如果 mock 过于复杂,可以考虑:
|
如果 mock 过于复杂,可以考虑:
|
||||||
|
|
||||||
- 使用 vi.resetModules() 确保每次测试都重新加载
|
- 使用 vi.resetModules() 确保每次测试都重新加载
|
||||||
- 使用 vi.mock(importOriginal) 混合真实模块
|
- 使用 vi.mock(importOriginal) 混合真实模块
|
||||||
- 或完全重写测试,只测试 logger 的公共 API
|
- 或完全重写测试,只测试 logger 的公共 API
|
||||||
|
|
||||||
**预期结果**:
|
**预期结果**:
|
||||||
|
|
||||||
- ✅ 18/18 tests passing
|
- ✅ 18/18 tests passing
|
||||||
- ✅ format().combine().timestamp().printf() 链式调用正常工作
|
- ✅ format().combine().timestamp().printf() 链式调用正常工作
|
||||||
- ✅ logger 创建、子 logger、日志输出测试全部通过
|
- ✅ logger 创建、子 logger、日志输出测试全部通过
|
||||||
@@ -165,6 +170,7 @@ vi.mock('winston', () => ({
|
|||||||
**失败测试**: `checks updates for user and auto-downloads available recommendation`
|
**失败测试**: `checks updates for user and auto-downloads available recommendation`
|
||||||
|
|
||||||
**错误信息**:
|
**错误信息**:
|
||||||
|
|
||||||
```
|
```
|
||||||
AssertionError: expected "vi.fn()" to be called with arguments:
|
AssertionError: expected "vi.fn()" to be called with arguments:
|
||||||
['stable/1.1.0.exe', 'preview/1.1.0.exe']
|
['stable/1.1.0.exe', 'preview/1.1.0.exe']
|
||||||
@@ -175,6 +181,7 @@ Number of calls: 0
|
|||||||
**根因**: Mock 调用参数与实际调用不匹配
|
**根因**: Mock 调用参数与实际调用不匹配
|
||||||
|
|
||||||
**代码位置**:
|
**代码位置**:
|
||||||
|
|
||||||
- 测试文件:`tests/unit/update-service.test.ts:165-175`
|
- 测试文件:`tests/unit/update-service.test.ts:165-175`
|
||||||
- 被测文件:`src/main/services/update/update-service.ts`
|
- 被测文件:`src/main/services/update/update-service.ts`
|
||||||
|
|
||||||
@@ -200,12 +207,14 @@ it('checks updates for user and auto-downloads available recommendation', async
|
|||||||
**步骤 2.2.3**: 更新测试断言
|
**步骤 2.2.3**: 更新测试断言
|
||||||
|
|
||||||
**选项 A: 匹配实际调用**
|
**选项 A: 匹配实际调用**
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 如果实际只调用了一个参数
|
// 如果实际只调用了一个参数
|
||||||
expect(mockDownload).toHaveBeenCalledWith('stable/1.1.0.exe')
|
expect(mockDownload).toHaveBeenCalledWith('stable/1.1.0.exe')
|
||||||
```
|
```
|
||||||
|
|
||||||
**选项 B: 使用更松散的断言**
|
**选项 B: 使用更松散的断言**
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 如果参数顺序或数量有变化
|
// 如果参数顺序或数量有变化
|
||||||
expect(mockDownload).toHaveBeenCalled()
|
expect(mockDownload).toHaveBeenCalled()
|
||||||
@@ -213,14 +222,12 @@ expect(mockDownload.mock.calls[0]).toContain('stable/1.1.0.exe')
|
|||||||
```
|
```
|
||||||
|
|
||||||
**选项 C: 调整 mock 设置**
|
**选项 C: 调整 mock 设置**
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 确保 mock 正确设置
|
// 确保 mock 正确设置
|
||||||
mockDownload.mockClear()
|
mockDownload.mockClear()
|
||||||
// ... 触发动作 ...
|
// ... 触发动作 ...
|
||||||
expect(mockDownload).toHaveBeenCalledWith(
|
expect(mockDownload).toHaveBeenCalledWith(expect.stringContaining('stable'), expect.any(String))
|
||||||
expect.stringContaining('stable'),
|
|
||||||
expect.any(String)
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 成功标准
|
#### 成功标准
|
||||||
@@ -243,6 +250,7 @@ expect(mockDownload).toHaveBeenCalledWith(
|
|||||||
**失败测试**: `builds downloaded package path under userData pending-update`
|
**失败测试**: `builds downloaded package path under userData pending-update`
|
||||||
|
|
||||||
**错误信息**:
|
**错误信息**:
|
||||||
|
|
||||||
```
|
```
|
||||||
AssertionError: expected 'D:\...\test-user-data\pending-update\stable-1.2.3.exe'
|
AssertionError: expected 'D:\...\test-user-data\pending-update\stable-1.2.3.exe'
|
||||||
to contain 'logs\pending-update'
|
to contain 'logs\pending-update'
|
||||||
@@ -254,6 +262,7 @@ Received: "D:\...\test-user-data\pending-update\stable-1.2.3.exe"
|
|||||||
**根因**: Electron mock 的 `app.getPath('userData')` 返回 `test-user-data`,但测试期望路径包含 `logs`
|
**根因**: Electron mock 的 `app.getPath('userData')` 返回 `test-user-data`,但测试期望路径包含 `logs`
|
||||||
|
|
||||||
**代码位置**:
|
**代码位置**:
|
||||||
|
|
||||||
- 测试文件:`tests/unit/update-installer.test.ts:13-16`
|
- 测试文件:`tests/unit/update-installer.test.ts:13-16`
|
||||||
- Setup mock: `tests/setup.ts:17-24`
|
- Setup mock: `tests/setup.ts:17-24`
|
||||||
|
|
||||||
@@ -286,6 +295,7 @@ userData: path.join(process.cwd(), 'logs')
|
|||||||
```
|
```
|
||||||
|
|
||||||
**推荐**: 方案 2.3.1 (测试适应 mock)
|
**推荐**: 方案 2.3.1 (测试适应 mock)
|
||||||
|
|
||||||
- 理由:mock 是为了测试隔离,测试应该适应 mock 环境
|
- 理由:mock 是为了测试隔离,测试应该适应 mock 环境
|
||||||
|
|
||||||
#### 成功标准
|
#### 成功标准
|
||||||
@@ -310,6 +320,7 @@ npm run test:run tests/unit/logger.test.ts
|
|||||||
```
|
```
|
||||||
|
|
||||||
**失败时排查**:
|
**失败时排查**:
|
||||||
|
|
||||||
1. 检查 vi.mock 是否在文件顶部 (hoisted)
|
1. 检查 vi.mock 是否在文件顶部 (hoisted)
|
||||||
2. 清除 vitest 缓存:`npx vitest --clearCache`
|
2. 清除 vitest 缓存:`npx vitest --clearCache`
|
||||||
3. 检查是否有多个 winston mock 冲突
|
3. 检查是否有多个 winston mock 冲突
|
||||||
@@ -354,11 +365,11 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
|
|
||||||
### 技术指标
|
### 技术指标
|
||||||
|
|
||||||
| 指标 | 修复前 | 修复后 | 验证命令 |
|
| 指标 | 修复前 | 修复后 | 验证命令 |
|
||||||
|------|-------|-------|---------|
|
| -------- | ------------- | ------------------ | ------------------ |
|
||||||
| 失败套件 | 3 suites | 0 suites | `npm run test:run` |
|
| 失败套件 | 3 suites | 0 suites | `npm run test:run` |
|
||||||
| 失败测试 | 13 tests | 0 tests | `npm run test:run` |
|
| 失败测试 | 13 tests | 0 tests | `npm run test:run` |
|
||||||
| 通过率 | 95% (311/327) | **100%** (327/327) | 测试报告 |
|
| 通过率 | 95% (311/327) | **100%** (327/327) | 测试报告 |
|
||||||
|
|
||||||
### 验收条件
|
### 验收条件
|
||||||
|
|
||||||
@@ -373,11 +384,11 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
|
|
||||||
### 技术风险
|
### 技术风险
|
||||||
|
|
||||||
| 风险 | 可能性 | 影响 | 缓解措施 |
|
| 风险 | 可能性 | 影响 | 缓解措施 |
|
||||||
|------|--------|------|---------|
|
| -------------------- | ------ | ---- | ------------------------------- |
|
||||||
| logger mock 实现复杂 | 中 | 高 | 采用延迟 mock,先跑通一部分测试 |
|
| logger mock 实现复杂 | 中 | 高 | 采用延迟 mock,先跑通一部分测试 |
|
||||||
| 链式调用 mock 不完整 | 高 | 中 | 使用 createFormatFn 工厂函数 |
|
| 链式调用 mock 不完整 | 高 | 中 | 使用 createFormatFn 工厂函数 |
|
||||||
| 循环依赖难解耦 | 低 | 高 | 只修复 mock,不重构依赖关系 |
|
| 循环依赖难解耦 | 低 | 高 | 只修复 mock,不重构依赖关系 |
|
||||||
|
|
||||||
### 时间风险
|
### 时间风险
|
||||||
|
|
||||||
@@ -386,6 +397,7 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
- **保守估计**: 6 小时 (遇到意外问题)
|
- **保守估计**: 6 小时 (遇到意外问题)
|
||||||
|
|
||||||
**风险缓解**: 如果 logger mock 问题超过 3 小时无法解决,考虑:
|
**风险缓解**: 如果 logger mock 问题超过 3 小时无法解决,考虑:
|
||||||
|
|
||||||
1. 暂时跳过 logger.test.ts (保持 95% 通过率)
|
1. 暂时跳过 logger.test.ts (保持 95% 通过率)
|
||||||
2. 先修复简单的 update 测试 (13 failures → 2 failures)
|
2. 先修复简单的 update 测试 (13 failures → 2 failures)
|
||||||
3. 记录问题,后续专门花精力解决
|
3. 记录问题,后续专门花精力解决
|
||||||
@@ -401,6 +413,7 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
**实际工时**: X 小时
|
**实际工时**: X 小时
|
||||||
|
|
||||||
**修复步骤**:
|
**修复步骤**:
|
||||||
|
|
||||||
1. [ ] 诊断 mock 问题
|
1. [ ] 诊断 mock 问题
|
||||||
2. [ ] 实现 formatFn 工厂
|
2. [ ] 实现 formatFn 工厂
|
||||||
3. [ ] 添加所有链式方法
|
3. [ ] 添加所有链式方法
|
||||||
@@ -408,10 +421,12 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
5. [ ] 验证测试通过
|
5. [ ] 验证测试通过
|
||||||
|
|
||||||
**遇到的问题**:
|
**遇到的问题**:
|
||||||
|
|
||||||
- 问题 1: [描述] → 解决方案: [方案]
|
- 问题 1: [描述] → 解决方案: [方案]
|
||||||
- 问题 2: [描述] → 解决方案: [方案]
|
- 问题 2: [描述] → 解决方案: [方案]
|
||||||
|
|
||||||
**关键代码**:
|
**关键代码**:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 最终有效的 mock 实现
|
// 最终有效的 mock 实现
|
||||||
```
|
```
|
||||||
@@ -425,6 +440,7 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
**实际工时**: X 分钟
|
**实际工时**: X 分钟
|
||||||
|
|
||||||
**修复方式**:
|
**修复方式**:
|
||||||
|
|
||||||
- [ ] 修改断言
|
- [ ] 修改断言
|
||||||
- [ ] 修改 mock 参数
|
- [ ] 修改 mock 参数
|
||||||
- [ ] 其他: [描述]
|
- [ ] 其他: [描述]
|
||||||
@@ -440,6 +456,7 @@ npm run test:run 2>&1 | Select-String "Test Files.*failed"
|
|||||||
**实际工时**: X 分钟
|
**实际工时**: X 分钟
|
||||||
|
|
||||||
**修复方式**:
|
**修复方式**:
|
||||||
|
|
||||||
- [ ] 修改断言
|
- [ ] 修改断言
|
||||||
- [ ] 修改 mock
|
- [ ] 修改 mock
|
||||||
- [ ] 其他: [描述]
|
- [ ] 其他: [描述]
|
||||||
|
|||||||
781
docs/TEST_COVERAGE_IMPROVEMENT_PLAN.md
Normal file
781
docs/TEST_COVERAGE_IMPROVEMENT_PLAN.md
Normal file
@@ -0,0 +1,781 @@
|
|||||||
|
# ERPAuto 测试覆盖率提升计划
|
||||||
|
|
||||||
|
## 1. 执行摘要
|
||||||
|
|
||||||
|
### 1.1 当前状态评估
|
||||||
|
|
||||||
|
| 指标 | 当前值 | 目标值 | 差距 |
|
||||||
|
| ------------------ | ------ | ------ | ------- |
|
||||||
|
| **总体行覆盖率** | 11.36% | 70% | -58.64% |
|
||||||
|
| **总体函数覆盖率** | 21.29% | 70% | -48.71% |
|
||||||
|
| **总体分支覆盖率** | 10.08% | 60% | -49.92% |
|
||||||
|
| **测试文件总数** | 54 | 100+ | -46+ |
|
||||||
|
|
||||||
|
**关键模块覆盖率差距:**
|
||||||
|
|
||||||
|
| 模块 | 当前覆盖率 | 要求阈值 | 优先级 |
|
||||||
|
| ---------------------------------------- | ---------- | -------- | ------------- |
|
||||||
|
| ERP 服务 (`src/main/services/erp/**`) | 11.68% | 80% | P0 |
|
||||||
|
| 更新服务 (`src/main/services/update/**`) | 42.45% | 80% | P0 |
|
||||||
|
| 数据库服务 | 17.24% | 70% | P1 |
|
||||||
|
| 配置管理 | 20.56% | 70% | P1 |
|
||||||
|
| 日志服务 | 70.67% | 70% | P2 (已达标的) |
|
||||||
|
|
||||||
|
### 1.2 提升目标
|
||||||
|
|
||||||
|
**阶段性目标:**
|
||||||
|
|
||||||
|
- **Phase 1 (4 周)**:ERP 服务达到 60%,更新服务达到 70%
|
||||||
|
- **Phase 2 (4 周)**:数据库服务达到 60%,配置管理达到 60%
|
||||||
|
- **Phase 3 (4 周)**:所有关键模块达到目标阈值,总体覆盖率达到 70%
|
||||||
|
|
||||||
|
**最终目标:**
|
||||||
|
|
||||||
|
- 全局覆盖率:70% 行 / 70% 函数 / 60% 分支
|
||||||
|
- ERP 服务:80% 行 / 80% 函数 / 70% 分支
|
||||||
|
- 更新服务:80% 行 / 80% 函数 / 70% 分支
|
||||||
|
|
||||||
|
### 1.3 时间线估算
|
||||||
|
|
||||||
|
| 阶段 | 持续时间 | 里程碑 |
|
||||||
|
| -------- | --------- | ---------------------- |
|
||||||
|
| Phase 1 | 4 周 | ERP 核心服务测试完成 |
|
||||||
|
| Phase 2 | 4 周 | 数据层与配置层测试完成 |
|
||||||
|
| Phase 3 | 4 周 | 集成测试与 E2E 补全 |
|
||||||
|
| 缓冲期 | 2 周 | 修复与优化 |
|
||||||
|
| **总计** | **14 周** | **达到目标覆盖率** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 分阶段提升计划
|
||||||
|
|
||||||
|
### Phase 1: ERP 核心服务测试攻坚(第 1-4 周)
|
||||||
|
|
||||||
|
**目标:** ERP 服务覆盖率从 11.68% 提升至 60%
|
||||||
|
|
||||||
|
**工作内容:**
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| ---------------------- | ------ | ---------- | ------ |
|
||||||
|
| `erp-auth.ts` | 1 | 15 | P0 |
|
||||||
|
| `extractor.ts` | 1 | 20 | P0 |
|
||||||
|
| `extractor-core.ts` | 1 | 15 | P0 |
|
||||||
|
| `cleaner.ts` | 1 | 12 | P0 |
|
||||||
|
| `ErpBrowserManager.ts` | 1 | 10 | P1 |
|
||||||
|
| `order-resolver.ts` | 1 | 8 | P1 |
|
||||||
|
| `page-diagnostics.ts` | 1 | 6 | P2 |
|
||||||
|
| `erp-error-context.ts` | 1 | 5 | P2 |
|
||||||
|
| `locators.ts` | 1 | 8 | P1 |
|
||||||
|
|
||||||
|
**预计投入:** 80-100 小时
|
||||||
|
|
||||||
|
**成功标准:**
|
||||||
|
|
||||||
|
- [ ] ERP 服务行覆盖率 ≥ 60%
|
||||||
|
- [ ] ERP 服务函数覆盖率 ≥ 70%
|
||||||
|
- [ ] 新增测试文件:9 个
|
||||||
|
- [ ] 所有 P0 模块有完整测试覆盖
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase 2: 数据层与配置层测试(第 5-8 周)
|
||||||
|
|
||||||
|
**目标:** 数据库服务与配置管理覆盖率达标
|
||||||
|
|
||||||
|
**工作内容:**
|
||||||
|
|
||||||
|
#### 2.1 数据库服务(17.24% → 60%)
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| ---------------------------------------------- | ------ | ---------- | ------ |
|
||||||
|
| `mysql.ts` / `sql-server.ts` / `postgresql.ts` | 3 | 18 | P0 |
|
||||||
|
| `data-source.ts` | 1 | 8 | P0 |
|
||||||
|
| `data-importer.ts` | 1 | 10 | P0 |
|
||||||
|
| DAO 层文件 | 4 | 16 | P1 |
|
||||||
|
| Repository 层 | 2 | 8 | P1 |
|
||||||
|
| 数据库实体 | 2 | 6 | P2 |
|
||||||
|
|
||||||
|
#### 2.2 配置管理(20.56% → 60%)
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| ------------------- | ------ | ---------- | ------ |
|
||||||
|
| `config-manager.ts` | 1 | 20 | P0 |
|
||||||
|
| 配置 Schema 验证 | 1 | 10 | P1 |
|
||||||
|
|
||||||
|
#### 2.3 用户服务(新增)
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| ---------------------------- | ------ | ---------- | ------ |
|
||||||
|
| `session-manager.ts` | 1 | 8 | P1 |
|
||||||
|
| `user-erp-config-service.ts` | 1 | 10 | P1 |
|
||||||
|
| `bip-users-dao.ts` | 1 | 6 | P2 |
|
||||||
|
|
||||||
|
**预计投入:** 100-120 小时
|
||||||
|
|
||||||
|
**成功标准:**
|
||||||
|
|
||||||
|
- [ ] 数据库服务行覆盖率 ≥ 60%
|
||||||
|
- [ ] 配置管理行覆盖率 ≥ 60%
|
||||||
|
- [ ] 新增测试文件:15 个
|
||||||
|
- [ ] 所有数据库方言有完整测试
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase 3: 更新服务与其他模块补全(第 9-12 周)
|
||||||
|
|
||||||
|
**目标:** 更新服务达到 80%,其他服务达到 70%
|
||||||
|
|
||||||
|
**工作内容:**
|
||||||
|
|
||||||
|
#### 3.1 更新服务(42.45% → 80%)
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| ---------------------------- | ------ | ---------- | ------ |
|
||||||
|
| `update-service.ts` | 1 | 15 | P0 |
|
||||||
|
| `update-catalog-service.ts` | 1 | 12 | P0 |
|
||||||
|
| `update-installer.ts` | 1 | 10 | P0 |
|
||||||
|
| `update-storage-client.ts` | 1 | 10 | P0 |
|
||||||
|
| `update-status-publisher.ts` | 1 | 6 | P1 |
|
||||||
|
| `update-support.ts` | 1 | 5 | P1 |
|
||||||
|
| `update-utils.ts` | 1 | 5 | P2 |
|
||||||
|
|
||||||
|
#### 3.2 其他关键服务
|
||||||
|
|
||||||
|
| 模块 | 文件数 | 新增测试数 | 优先级 |
|
||||||
|
| -------------------------- | ------ | ---------- | ------ |
|
||||||
|
| 验证服务 (`validation/**`) | 3 | 15 | P1 |
|
||||||
|
| 清理服务 (`cleaner/**`) | 2 | 10 | P1 |
|
||||||
|
| Excel 服务 | 2 | 8 | P2 |
|
||||||
|
| 报告生成 | 1 | 6 | P2 |
|
||||||
|
| Playwright 浏览器服务 | 2 | 10 | P1 |
|
||||||
|
| RustFS 服务 | 2 | 8 | P2 |
|
||||||
|
|
||||||
|
**预计投入:** 100-120 小时
|
||||||
|
|
||||||
|
**成功标准:**
|
||||||
|
|
||||||
|
- [ ] 更新服务行覆盖率 ≥ 80%
|
||||||
|
- [ ] 更新服务函数覆盖率 ≥ 80%
|
||||||
|
- [ ] 新增测试文件:17 个
|
||||||
|
- [ ] 所有 P0/P1 模块覆盖率达标
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase 4: 集成测试与 E2E 强化(第 13-14 周)
|
||||||
|
|
||||||
|
**目标:** 强化集成测试与端到端测试
|
||||||
|
|
||||||
|
**工作内容:**
|
||||||
|
|
||||||
|
#### 4.1 集成测试扩展(7 → 20 个)
|
||||||
|
|
||||||
|
| 测试场景 | 优先级 | 描述 |
|
||||||
|
| -------------------------- | ------ | ---------------------- |
|
||||||
|
| ERP 登录 + 提取完整流程 | P0 | 验证认证与数据提取集成 |
|
||||||
|
| 数据库事务完整流程 | P0 | 验证 TypeORM 事务边界 |
|
||||||
|
| 配置热加载与验证 | P1 | 验证配置更新传播 |
|
||||||
|
| 更新检查 + 下载 + 安装流程 | P0 | 验证更新完整链路 |
|
||||||
|
| 日志异步写入与轮转 | P1 | 验证日志系统 |
|
||||||
|
| 用户会话切换流程 | P1 | 验证多用户场景 |
|
||||||
|
| Excel 导入导出完整流程 | P2 | 验证文件处理链 |
|
||||||
|
|
||||||
|
#### 4.2 E2E 测试扩展(3 → 15 个)
|
||||||
|
|
||||||
|
| 用户旅程 | 优先级 | 描述 |
|
||||||
|
| -------------------- | ------ | -------------------------------- |
|
||||||
|
| 管理员完整工作流程 | P0 | 登录 → 提取 → 清理 → 验证 → 登出 |
|
||||||
|
| 普通用户数据提取流程 | P0 | 登录 → 提取 → 查看结果 |
|
||||||
|
| Guest 只读访问流程 | P1 | 登录 → 查看历史记录 |
|
||||||
|
| 配置管理流程 | P1 | 修改配置 → 保存 → 验证生效 |
|
||||||
|
| 自动更新流程 | P0 | 检查更新 → 下载 → 安装 → 重启 |
|
||||||
|
| 错误恢复流程 | P1 | 断网重连、会话过期恢复 |
|
||||||
|
| 批量处理流程 | P1 | 大批量订单处理性能验证 |
|
||||||
|
|
||||||
|
**预计投入:** 60-80 小时
|
||||||
|
|
||||||
|
**成功标准:**
|
||||||
|
|
||||||
|
- [ ] 集成测试文件:20 个
|
||||||
|
- [ ] E2E 测试文件:15 个
|
||||||
|
- [ ] 关键用户旅程 100% 覆盖
|
||||||
|
- [ ] 整体覆盖率达到 70%
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 逐模块测试计划
|
||||||
|
|
||||||
|
### 3.1 ERP 服务模块
|
||||||
|
|
||||||
|
#### 3.1.1 `erp-auth.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** < 20%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ------------------- | -------- | ------------------------------- | ------------------- |
|
||||||
|
| 成功登录流程 | 单元 | Playwright Browser/Context/Page | 返回有效 ErpSession |
|
||||||
|
| 登录失败 - 网络错误 | 单元 | Playwright + 模拟网络错误 | 抛出连接错误 |
|
||||||
|
| 登录失败 - 凭证错误 | 单元 | Page + 模拟错误消息 | 抛出认证错误 |
|
||||||
|
| 会话复用 - 已登录 | 单元 | Session Mock | 直接返回现有会话 |
|
||||||
|
| 登出流程 | 单元 | Browser/Context Mock | 资源正确释放 |
|
||||||
|
| 会话超时检测 | 单元 | Page + 超时 Mock | 返回未登录状态 |
|
||||||
|
| 页面元素定位失败 | 单元 | Page + Selector 失败 | 抛出元素未找到错误 |
|
||||||
|
| SSL 证书错误处理 | 集成 | 真实 Browser + 自签名证书 | 成功建立连接 |
|
||||||
|
|
||||||
|
**预计测试数:** 15
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.1.2 `extractor.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~30%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| -------------- | -------- | -------------------------- | -------------------- |
|
||||||
|
| 单订单提取成功 | 单元 | ErpAuthService + Page | 返回 ExtractorResult |
|
||||||
|
| 批量订单提取 | 单元 | ErpAuthService + 循环 Mock | 正确分批处理 |
|
||||||
|
| 订单号无效处理 | 单元 | Page + 错误响应 | 记录错误,继续处理 |
|
||||||
|
| 下载文件合并 | 单元 | ExcelJS + fs Mock | 生成合并文件 |
|
||||||
|
| 数据库持久化 | 集成 | DatabaseService Mock | 记录成功导入 |
|
||||||
|
| 并发限制控制 | 单元 | 信号量 Mock | 不超过并发上限 |
|
||||||
|
| 提取中断恢复 | 集成 | 模拟中断 + 恢复 | 从断点继续 |
|
||||||
|
| 结果统计准确性 | 单元 | 完整 Mock 链 | 统计数字准确 |
|
||||||
|
|
||||||
|
**预计测试数:** 20
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.1.3 `extractor-core.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** < 10%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ---------------- | -------- | ---------------- | ------------ |
|
||||||
|
| 页面导航到列表页 | 单元 | Page + Frame | 成功导航 |
|
||||||
|
| 订单号输入 | 单元 | Locator Mock | 正确填充 |
|
||||||
|
| 查询按钮点击 | 单元 | Locator Mock | 触发查询 |
|
||||||
|
| 表格数据解析 | 单元 | Table Locator | 返回物料列表 |
|
||||||
|
| 分页处理 | 单元 | Page + 多页 Mock | 遍历所有页 |
|
||||||
|
| 下载按钮点击 | 单元 | Locator + Dialog | 触发下载 |
|
||||||
|
| 下载完成等待 | 单元 | fs + 文件事件 | 文件落地 |
|
||||||
|
| 错误弹窗检测 | 单元 | Page + 错误元素 | 捕获错误消息 |
|
||||||
|
|
||||||
|
**预计测试数:** 15
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.1.4 `cleaner.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~25%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ---------------- | -------- | ------------------ | ------------ |
|
||||||
|
| 单物料删除成功 | 单元 | Page + Locator | 删除成功 |
|
||||||
|
| 批量物料删除 | 单元 | 循环删除 Mock | 全部删除 |
|
||||||
|
| 物料不存在处理 | 单元 | Page + 空结果 | 跳过并记录 |
|
||||||
|
| 删除按钮失效处理 | 单元 | Locator + disabled | 跳过该物料 |
|
||||||
|
| 干运行模式 | 单元 | 不执行实际删除 | 返回预览结果 |
|
||||||
|
| 并发控制 | 单元 | 信号量 Mock | 限制并发数 |
|
||||||
|
| 错误重试机制 | 集成 | 失败→成功 Mock | 重试成功 |
|
||||||
|
| 删除结果统计 | 单元 | 完整 Mock 链 | 统计准确 |
|
||||||
|
|
||||||
|
**预计测试数:** 12
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.2 数据库服务模块
|
||||||
|
|
||||||
|
#### 3.2.1 数据库连接服务 (P0)
|
||||||
|
|
||||||
|
**文件:** `mysql.ts`, `sql-server.ts`, `postgresql.ts`
|
||||||
|
|
||||||
|
**当前覆盖率:** ~20%
|
||||||
|
**目标覆盖率:** 70%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ------------------- | -------- | ----------------------- | ------------ |
|
||||||
|
| MySQL 连接成功 | 单元 | mysql2 Pool Mock | 返回连接实例 |
|
||||||
|
| SQL Server 连接成功 | 单元 | mssql Connection Mock | 返回连接实例 |
|
||||||
|
| PostgreSQL 连接成功 | 单元 | pg Pool Mock | 返回连接实例 |
|
||||||
|
| 连接失败处理 | 单元 | 模拟连接拒绝 | 抛出错误 |
|
||||||
|
| 查询执行成功 | 集成 | 数据库 Mock + 返回结果 | 正确返回数据 |
|
||||||
|
| 事务提交 | 集成 | Transaction Mock | 成功提交 |
|
||||||
|
| 事务回滚 | 集成 | Transaction Mock + 错误 | 正确回滚 |
|
||||||
|
| 连接池释放 | 单元 | Pool Mock | 正确关闭 |
|
||||||
|
|
||||||
|
**预计测试数:** 18 (3 个数据库 × 6 场景)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.2.2 数据源管理 (P0)
|
||||||
|
|
||||||
|
**文件:** `data-source.ts`
|
||||||
|
|
||||||
|
**当前覆盖率:** < 10%
|
||||||
|
**目标覆盖率:** 70%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| --------------- | -------- | --------------- | -------------- |
|
||||||
|
| TypeORM 初始化 | 单元 | DataSource Mock | 成功初始化 |
|
||||||
|
| 数据源销毁 | 单元 | DataSource Mock | 正确释放 |
|
||||||
|
| Repository 获取 | 单元 | Repository Mock | 返回对应仓库 |
|
||||||
|
| 实体注册验证 | 单元 | Entity Mock | 所有实体已注册 |
|
||||||
|
| 多次初始化防护 | 单元 | 状态检查 Mock | 不重复初始化 |
|
||||||
|
|
||||||
|
**预计测试数:** 8
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.2.3 数据导入器 (P0)
|
||||||
|
|
||||||
|
**文件:** `data-importer.ts`
|
||||||
|
|
||||||
|
**当前覆盖率:** < 15%
|
||||||
|
**目标覆盖率:** 70%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| -------------- | -------- | ------------------------ | ------------ |
|
||||||
|
| Excel 读取成功 | 集成 | ExcelJS + 测试文件 | 解析数据结构 |
|
||||||
|
| 数据验证通过 | 单元 | Schema 验证 Mock | 数据合法 |
|
||||||
|
| 数据验证失败 | 单元 | Schema 验证 Mock | 抛出验证错误 |
|
||||||
|
| 批量插入 | 集成 | Repository Mock | 正确分批插入 |
|
||||||
|
| 重复数据处理 | 单元 | Repository + exists 检查 | 跳过或更新 |
|
||||||
|
| 插入失败回滚 | 集成 | Transaction Mock + 错误 | 全部回滚 |
|
||||||
|
| 导入进度追踪 | 单元 | EventEmitter Mock | 发送进度事件 |
|
||||||
|
| 导入结果统计 | 单元 | 完整 Mock 链 | 统计准确 |
|
||||||
|
|
||||||
|
**预计测试数:** 10
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.3 配置管理模块
|
||||||
|
|
||||||
|
#### 3.3.1 `config-manager.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~25%
|
||||||
|
**目标覆盖率:** 70%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ---------------- | -------- | -------------------- | ------------ |
|
||||||
|
| 配置文件加载成功 | 单元 | fs + yaml Mock | 返回有效配置 |
|
||||||
|
| 配置文件不存在 | 单元 | fs Mock + 不存在 | 使用默认配置 |
|
||||||
|
| 配置文件格式错误 | 单元 | yaml Mock + 解析失败 | 抛出解析错误 |
|
||||||
|
| Zod 验证失败 | 单元 | 无效配置数据 | 抛出验证错误 |
|
||||||
|
| 配置更新 | 单元 | fs + yaml Mock | 文件正确写入 |
|
||||||
|
| 重置为默认值 | 单元 | 完整 Mock 链 | 恢复默认 |
|
||||||
|
| 导出为 YAML | 单元 | yaml.stringify Mock | 格式正确 |
|
||||||
|
| 数据库类型切换 | 单元 | 状态 Mock | 返回正确配置 |
|
||||||
|
| 日志配置应用 | 集成 | Winston Mock | 日志级别生效 |
|
||||||
|
| 审计配置应用 | 集成 | AuditLogger Mock | 审计配置生效 |
|
||||||
|
| 单例模式验证 | 单元 | 多次 getInstance | 返回同一实例 |
|
||||||
|
| 并发读取安全 | 集成 | 并发 Mock + 竞争 | 数据一致 |
|
||||||
|
|
||||||
|
**预计测试数:** 20
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.4 更新服务模块
|
||||||
|
|
||||||
|
#### 3.4.1 `update-service.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~50%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ------------------- | -------- | ------------------------- | ------------ |
|
||||||
|
| 服务初始化 | 单元 | ConfigManager + 依赖 Mock | 服务就绪 |
|
||||||
|
| 获取更新状态 | 单元 | 状态 Mock | 返回当前状态 |
|
||||||
|
| 获取更新目录 | 单元 | CatalogService Mock | 返回目录结构 |
|
||||||
|
| 检查更新 - 有新版本 | 集成 | S3Client Mock + 新版本 | 返回更新列表 |
|
||||||
|
| 检查更新 - 无新版本 | 集成 | S3Client Mock + 最新版 | 返回空列表 |
|
||||||
|
| 下载更新 - 成功 | 集成 | S3Client + fs Mock | 文件下载成功 |
|
||||||
|
| 下载更新 - 失败 | 集成 | S3Client + 网络错误 | 抛出错误 |
|
||||||
|
| 校验 SHA256 - 通过 | 单元 | crypto Mock | 校验通过 |
|
||||||
|
| 校验 SHA256 - 失败 | 单元 | crypto Mock + 不匹配 | 抛出校验错误 |
|
||||||
|
| 安装更新 | 集成 | child_process Mock | 启动安装器 |
|
||||||
|
| 用户权限检查 | 单元 | UserType Mock | 正确过滤 |
|
||||||
|
| 定期自动检查 | 集成 | setInterval Mock | 按时检查 |
|
||||||
|
|
||||||
|
**预计测试数:** 15
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.4.2 `update-catalog-service.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~40%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| ------------ | -------- | ------------------ | ------------ |
|
||||||
|
| 构建更新目录 | 单元 | StorageClient Mock | 返回分类目录 |
|
||||||
|
| 稳定版过滤 | 单元 | UserType + 目录 | 只看 stable |
|
||||||
|
| 管理员全访问 | 单元 | AdminType + 目录 | 看全部通道 |
|
||||||
|
| 更新历史记录 | 单元 | Repository Mock | 返回历史记录 |
|
||||||
|
| 限制记录数量 | 单元 | 数据截断 | 不超过上限 |
|
||||||
|
|
||||||
|
**预计测试数:** 12
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 3.4.3 `update-storage-client.ts` (P0)
|
||||||
|
|
||||||
|
**当前覆盖率:** ~35%
|
||||||
|
**目标覆盖率:** 80%
|
||||||
|
|
||||||
|
| 测试场景 | 测试类型 | Mock 对象 | 预期结果 |
|
||||||
|
| --------------- | -------- | ------------------- | -------------- |
|
||||||
|
| S3 客户端初始化 | 单元 | AWS SDK Mock | 客户端创建成功 |
|
||||||
|
| 列出更新包 | 单元 | S3 listObjects Mock | 返回对象列表 |
|
||||||
|
| 下载文件 | 单元 | S3 getObject Mock | 返回文件流 |
|
||||||
|
| 下载失败处理 | 单元 | S3 + 网络错误 | 抛出错误 |
|
||||||
|
| 计算 SHA256 | 单元 | crypto Mock | 哈希值正确 |
|
||||||
|
| 重试机制 | 集成 | 失败→成功 Mock | 重试成功 |
|
||||||
|
|
||||||
|
**预计测试数:** 10
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 测试类别实施指南
|
||||||
|
|
||||||
|
### 4.1 单元测试
|
||||||
|
|
||||||
|
**适用范围:**
|
||||||
|
|
||||||
|
- 服务类(Service)的业务逻辑
|
||||||
|
- 工具函数(Utility Functions)
|
||||||
|
- 数据处理函数
|
||||||
|
- 类型转换函数
|
||||||
|
|
||||||
|
**Mock 策略:**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 使用现有 Mock 库
|
||||||
|
import {
|
||||||
|
createMockLogger,
|
||||||
|
createMockConfigManager,
|
||||||
|
createMockErpAuthService,
|
||||||
|
createMockDatabaseService,
|
||||||
|
createMockDataSource,
|
||||||
|
createMockRepository
|
||||||
|
} from '@/tests/mocks'
|
||||||
|
|
||||||
|
// 示例:ERP Auth 测试
|
||||||
|
describe('ErpAuthService', () => {
|
||||||
|
const mockConfig = { url: 'https://test.com', username: 'test', password: 'test' }
|
||||||
|
const mockPage = createMockPage() // 来自 mocks/index.ts
|
||||||
|
|
||||||
|
it('should login successfully', async () => {
|
||||||
|
mockPage.goto.mockResolvedValue(undefined)
|
||||||
|
mockPage.waitForSelector.mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
const authService = new ErpAuthService(mockConfig)
|
||||||
|
// 注入 mock (需要构造函数支持或使用 vi.mock)
|
||||||
|
const session = await authService.login()
|
||||||
|
|
||||||
|
expect(session.isLoggedIn).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**测试覆盖重点:**
|
||||||
|
|
||||||
|
1. **正常路径:** 主要业务流程成功执行
|
||||||
|
2. **异常路径:** 错误处理、回滚、重试
|
||||||
|
3. **边界条件:** 空输入、极大值、极小值
|
||||||
|
4. **分支覆盖:** if/else、switch/case 所有分支
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.2 集成测试
|
||||||
|
|
||||||
|
**适用范围:**
|
||||||
|
|
||||||
|
- 多服务协作场景
|
||||||
|
- 数据库事务边界
|
||||||
|
- 文件系统交互
|
||||||
|
- 外部服务调用(需 Stub)
|
||||||
|
|
||||||
|
**测试模式:**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||||
|
import { DatabaseService } from '@/main/services/database'
|
||||||
|
import { ConfigManager } from '@/main/services/config'
|
||||||
|
|
||||||
|
describe('Database + Config Integration', () => {
|
||||||
|
let db: DatabaseService
|
||||||
|
let configManager: ConfigManager
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
// 使用内存数据库或测试配置
|
||||||
|
configManager = ConfigManager.getInstance()
|
||||||
|
db = new DatabaseService(configManager)
|
||||||
|
await db.connect()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await db.disconnect()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should persist and retrieve data', async () => {
|
||||||
|
// 实际数据库操作
|
||||||
|
await db.query('INSERT INTO ...')
|
||||||
|
const result = await db.query('SELECT ...')
|
||||||
|
|
||||||
|
expect(result.rows).toHaveLength(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**集成测试清单:**
|
||||||
|
|
||||||
|
| 集成场景 | 涉及模块 | 预期时间 |
|
||||||
|
| --------------- | --------------------------- | -------- |
|
||||||
|
| ERP 登录 + 提取 | ErpAuth + Extractor | < 5s |
|
||||||
|
| 数据库事务 | DataSource + Repository | < 2s |
|
||||||
|
| 配置更新传播 | ConfigManager + Logger | < 1s |
|
||||||
|
| 文件导入导出 | ExcelParser + fs | < 3s |
|
||||||
|
| 更新下载校验 | UpdateService + S3 + crypto | < 10s |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.3 E2E 测试
|
||||||
|
|
||||||
|
**适用范围:**
|
||||||
|
|
||||||
|
- 完整用户旅程
|
||||||
|
- UI 交互验证
|
||||||
|
- 真实浏览器行为
|
||||||
|
- 跨进程通信
|
||||||
|
|
||||||
|
**Playwright 测试模式:**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { test, expect } from '@playwright/test'
|
||||||
|
|
||||||
|
test('complete extraction workflow', async ({ page }) => {
|
||||||
|
// 1. 导航到登录页
|
||||||
|
await page.goto('http://localhost:5173/login')
|
||||||
|
|
||||||
|
// 2. 登录
|
||||||
|
await page.getByPlaceholder('用户名').fill('admin')
|
||||||
|
await page.getByPlaceholder('密码').fill('admin123')
|
||||||
|
await page.getByRole('button', { name: '登录' }).click()
|
||||||
|
|
||||||
|
// 3. 等待跳转
|
||||||
|
await expect(page).toHaveURL(/dashboard/)
|
||||||
|
|
||||||
|
// 4. 进入提取页面
|
||||||
|
await page.getByText('数据提取').click()
|
||||||
|
|
||||||
|
// 5. 输入订单号
|
||||||
|
await page.getByPlaceholder('请输入订单号').fill('SC202601001')
|
||||||
|
|
||||||
|
// 6. 开始提取
|
||||||
|
await page.getByRole('button', { name: '开始提取' }).click()
|
||||||
|
|
||||||
|
// 7. 等待完成
|
||||||
|
await expect(page.getByText('提取完成')).toBeVisible({ timeout: 30000 })
|
||||||
|
|
||||||
|
// 8. 验证结果
|
||||||
|
await expect(page.getByText('记录数:')).toBeVisible()
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**E2E 测试关键场景:**
|
||||||
|
|
||||||
|
| 用户旅程 | 步骤数 | 预期时间 | 优先级 |
|
||||||
|
| ---------------- | ------ | -------- | ------ |
|
||||||
|
| 管理员完整工作流 | 15 | < 60s | P0 |
|
||||||
|
| 普通用户提取 | 8 | < 45s | P0 |
|
||||||
|
| 配置管理 | 10 | < 30s | P1 |
|
||||||
|
| 自动更新 | 8 | < 90s | P0 |
|
||||||
|
| 错误恢复 | 6 | < 40s | P1 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 资源与工作量估算
|
||||||
|
|
||||||
|
### 5.1 人员配置建议
|
||||||
|
|
||||||
|
| 角色 | 人数 | 职责 |
|
||||||
|
| -------------- | -------- | ----------------------- |
|
||||||
|
| 测试开发工程师 | 2 人 | 单元测试、集成测试编写 |
|
||||||
|
| 全栈工程师 | 1 人 | E2E 测试、Mock 基础设施 |
|
||||||
|
| 代码审查员 | 1 人 | 测试代码质量审查 |
|
||||||
|
| **总计** | **4 人** | **14 周完成** |
|
||||||
|
|
||||||
|
**单人模式调整:**
|
||||||
|
|
||||||
|
若只有 1 人负责,时间调整为:
|
||||||
|
|
||||||
|
- 周投入:20-25 小时
|
||||||
|
- 总周期:20-24 周
|
||||||
|
- 优先级:P0 → P1 → P2
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.2 工作量分解
|
||||||
|
|
||||||
|
| 阶段 | 任务 | 估算小时 |
|
||||||
|
| -------- | ----------------- | ---------------- |
|
||||||
|
| Phase 1 | ERP 服务单元测试 | 80-100 |
|
||||||
|
| | Mock 基础设施优化 | 10-15 |
|
||||||
|
| Phase 2 | 数据库单元测试 | 60-80 |
|
||||||
|
| | 配置单元测试 | 20-30 |
|
||||||
|
| | 集成测试 | 20-30 |
|
||||||
|
| Phase 3 | 更新服务测试 | 60-80 |
|
||||||
|
| | 其他服务测试 | 40-50 |
|
||||||
|
| Phase 4 | E2E 测试 | 40-60 |
|
||||||
|
| | 覆盖率优化 | 20-30 |
|
||||||
|
| **总计** | | **350-475 小时** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.3 风险因素
|
||||||
|
|
||||||
|
| 风险 | 可能性 | 影响 | 缓解措施 |
|
||||||
|
| --------------------------- | ------ | ---- | ------------------------ |
|
||||||
|
| Playwright 浏览器兼容性问题 | 中 | 高 | 提前验证浏览器版本 |
|
||||||
|
| 数据库连接不稳定 | 低 | 中 | 使用内存数据库或容器 |
|
||||||
|
| Mock 与实现不同步 | 高 | 中 | 定期同步,添加类型检查 |
|
||||||
|
| 测试维护成本过高 | 中 | 中 | 使用工厂模式,避免硬编码 |
|
||||||
|
| 覆盖率工具性能影响 | 低 | 低 | CI 中仅对变更文件检查 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 成功度量标准
|
||||||
|
|
||||||
|
### 6.1 覆盖率指标
|
||||||
|
|
||||||
|
| 里程碑 | 总体行覆盖率 | ERP 服务 | 更新服务 | 数据库 |
|
||||||
|
| ------------ | ------------ | -------- | -------- | ------- |
|
||||||
|
| Phase 1 完成 | 25% | 60% | 50% | 25% |
|
||||||
|
| Phase 2 完成 | 45% | 65% | 60% | 60% |
|
||||||
|
| Phase 3 完成 | 65% | 75% | 80% | 65% |
|
||||||
|
| Phase 4 完成 | **70%** | **80%** | **80%** | **70%** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6.2 测试数量目标
|
||||||
|
|
||||||
|
| 类型 | 当前 | Phase 1 | Phase 2 | Phase 3 | Phase 4 |
|
||||||
|
| -------------- | ------ | ------- | ------- | ------- | ------- |
|
||||||
|
| 单元测试文件 | 40 | 50 | 60 | 75 | 85 |
|
||||||
|
| 集成测试文件 | 7 | 8 | 12 | 15 | 20 |
|
||||||
|
| E2E 测试文件 | 3 | 3 | 3 | 5 | 15 |
|
||||||
|
| **总测试文件** | **50** | **61** | **75** | **95** | **120** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6.3 质量门禁
|
||||||
|
|
||||||
|
**每个 PR 必须满足:**
|
||||||
|
|
||||||
|
1. **新增代码覆盖率 ≥ 80%** (使用 `vitest --coverage --changed`)
|
||||||
|
2. **无测试失败**
|
||||||
|
3. **测试执行时间 < 30s** (单元测试) / < 120s (集成) / < 5min (E2E)
|
||||||
|
4. **无 Mock 滥用** (真实逻辑必须有真实测试)
|
||||||
|
|
||||||
|
**CI/CD 检查:**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# GitHub Actions 示例
|
||||||
|
- name: Test & Coverage
|
||||||
|
run: |
|
||||||
|
npm run test:coverage
|
||||||
|
# 检查覆盖率阈值
|
||||||
|
npx vitest --coverage --thresholds
|
||||||
|
# 生成报告
|
||||||
|
npx vitest --coverage --reporter=html
|
||||||
|
# 上传覆盖率
|
||||||
|
uses: codecov/codecov-action@v4
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 立即行动项(本周)
|
||||||
|
|
||||||
|
### 7.1 优先级 P0 - 必须完成
|
||||||
|
|
||||||
|
| 任务 | 负责人 | 截止日期 | 状态 |
|
||||||
|
| -------------------------------- | ------ | -------- | ---- |
|
||||||
|
| 创建 ERP Auth 测试文件框架 | - | Day 2 | ☐ |
|
||||||
|
| 创建 Extractor Core 测试文件框架 | - | Day 3 | ☐ |
|
||||||
|
| 扩展现有 Mock 库支持新增场景 | - | Day 4 | ☐ |
|
||||||
|
| 运行首次覆盖率基准测试 | - | Day 1 | ☐ |
|
||||||
|
|
||||||
|
### 7.2 优先级 P1 - 建议完成
|
||||||
|
|
||||||
|
| 任务 | 负责人 | 截止日期 | 状态 |
|
||||||
|
| -------------------------- | ------ | -------- | ---- |
|
||||||
|
| 整理现有测试文件结构 | - | Day 3 | ☐ |
|
||||||
|
| 创建测试模板和最佳实践文档 | - | Day 5 | ☐ |
|
||||||
|
| 设置覆盖率 CI 报告 | - | Day 5 | ☐ |
|
||||||
|
|
||||||
|
### 7.3 技术准备清单
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 安装覆盖率报告工具
|
||||||
|
npm install --save-dev @vitest/coverage-v8
|
||||||
|
|
||||||
|
# 2. 运行基准测试
|
||||||
|
npm run test:coverage
|
||||||
|
|
||||||
|
# 3. 查看 HTML 报告
|
||||||
|
npm run test:coverage
|
||||||
|
# 打开 coverage/index.html
|
||||||
|
|
||||||
|
# 4. 按文件查看详细覆盖率
|
||||||
|
npx vitest --coverage --reporter=verbose
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.4 第一个 Sprint 目标(Week 1-2)
|
||||||
|
|
||||||
|
**目标:ERP Auth 测试完成 50%**
|
||||||
|
|
||||||
|
- [ ] `tests/unit/services/erp/erp-auth.test.ts` 创建
|
||||||
|
- [ ] 成功登录场景测试(3 个)
|
||||||
|
- [ ] 失败场景测试(5 个)
|
||||||
|
- [ ] 会话管理测试(3 个)
|
||||||
|
- [ ] Mock 优化支持 Page 生命周期事件
|
||||||
|
- [ ] 运行测试,覆盖率 ≥ 40%
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 附录
|
||||||
|
|
||||||
|
### A. 现有测试资源
|
||||||
|
|
||||||
|
| 资源 | 路径 | 状态 |
|
||||||
|
| --------- | ---------------------------- | ------------------ |
|
||||||
|
| 测试设置 | `tests/setup.ts` | 完整 Electron Mock |
|
||||||
|
| 测试工厂 | `tests/fixtures/factory.ts` | 8 个工厂类 |
|
||||||
|
| Mock 库 | `tests/mocks/index.ts` | 15+ Mock 函数 |
|
||||||
|
| 测试文档 | `docs/TEST_FACTORY_USAGE.md` | 工厂使用指南 |
|
||||||
|
| Mock 文档 | `docs/MOCK_LIBRARY_USAGE.md` | Mock 使用指南 |
|
||||||
|
|
||||||
|
### B. 推荐测试工具
|
||||||
|
|
||||||
|
| 工具 | 用途 |
|
||||||
|
| ---------------------- | ------------- |
|
||||||
|
| `vitest` | 单元测试框架 |
|
||||||
|
| `@playwright/test` | E2E 测试框架 |
|
||||||
|
| `@vitest/coverage-v8` | V8 覆盖率引擎 |
|
||||||
|
| `vitest-html-reporter` | HTML 报告生成 |
|
||||||
|
|
||||||
|
### C. 相关文件
|
||||||
|
|
||||||
|
- `vitest.config.ts` - Vitest 配置与覆盖率阈值
|
||||||
|
- `package.json` - 测试脚本定义
|
||||||
|
- `.github/workflows/test.yml` - CI 测试工作流
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档版本:** 1.0
|
||||||
|
**创建日期:** 2026-04-05
|
||||||
|
**最后更新:** 2026-04-05
|
||||||
|
**维护者:** ERPAuto 开发团队
|
||||||
931
docs/TEST_QUALITY_REVIEW_REPORT.md
Normal file
931
docs/TEST_QUALITY_REVIEW_REPORT.md
Normal file
@@ -0,0 +1,931 @@
|
|||||||
|
# ERPAuto 测试质量审查报告
|
||||||
|
|
||||||
|
**审查日期**: 2026-04-05
|
||||||
|
**审查范围**: 新增的 ERP 服务单元测试文件
|
||||||
|
**审查者**: AI Code Review Agent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 执行摘要
|
||||||
|
|
||||||
|
本次审查覆盖了 6 个新增的 ERP 服务单元测试文件,共计 **117 个测试用例**(114 个通过,3 个待实现)。测试整体质量**优秀**,符合企业级测试标准。
|
||||||
|
|
||||||
|
### 总体评分:**A (90/100)**
|
||||||
|
|
||||||
|
| 评估维度 | 得分 | 权重 | 加权分 |
|
||||||
|
| ------------ | ------ | -------- | -------- |
|
||||||
|
| 测试覆盖率 | 85/100 | 30% | 25.5 |
|
||||||
|
| 测试设计质量 | 92/100 | 25% | 23.0 |
|
||||||
|
| Mock 策略 | 90/100 | 20% | 18.0 |
|
||||||
|
| 可维护性 | 88/100 | 15% | 13.2 |
|
||||||
|
| 错误处理测试 | 95/100 | 10% | 9.5 |
|
||||||
|
| **总计** | | **100%** | **89.2** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 测试文件概览
|
||||||
|
|
||||||
|
### 1.1 文件统计
|
||||||
|
|
||||||
|
| 测试文件 | 测试用例数 | 通过 | 失败 | 跳过/Todo | 行数 |
|
||||||
|
| --------------------------- | ---------- | ------- | ----- | --------- | -------- |
|
||||||
|
| `erp-auth.test.ts` | 11 | 11 | 0 | 0 | 216 |
|
||||||
|
| `cleaner.test.ts` | 20 | 20 | 0 | 0 | 272 |
|
||||||
|
| `ErpBrowserManager.test.ts` | 20 | 20 | 0 | 0 | 252 |
|
||||||
|
| `extractor-core.test.ts` | 11 | 8 | 0 | 3 | 265 |
|
||||||
|
| `extractor.test.ts` | 17 | 17 | 0 | 0 | 350 |
|
||||||
|
| `order-resolver.test.ts` | 26 | 26 | 0 | 0 | 363 |
|
||||||
|
| `page-diagnostics.test.ts` | 6 | 6 | 0 | 0 | - |
|
||||||
|
| `erp-error-context.test.ts` | 7 | 7 | 0 | 0 | - |
|
||||||
|
| **总计** | **118** | **115** | **0** | **3** | **1718** |
|
||||||
|
|
||||||
|
### 1.2 测试执行结果
|
||||||
|
|
||||||
|
```
|
||||||
|
✓ 8 个测试文件全部通过
|
||||||
|
✓ 114 个测试用例通过
|
||||||
|
✓ 0 个测试失败
|
||||||
|
⚠ 3 个测试标记为 todo(需要集成测试环境)
|
||||||
|
✓ 执行时间:< 1.5 秒(优秀)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 详细质量评估
|
||||||
|
|
||||||
|
### 2.1 `erp-auth.test.ts` - **A+ (95/100)**
|
||||||
|
|
||||||
|
**测试对象**: `ErpAuthService` - ERP 认证服务
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **完整的生命周期测试**
|
||||||
|
- 构造函数初始化验证
|
||||||
|
- 登录流程(成功/失败)
|
||||||
|
- 会话复用机制
|
||||||
|
- 登出/关闭处理
|
||||||
|
|
||||||
|
2. **优秀的 Mock 策略**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
vi.mock('playwright', () => ({
|
||||||
|
chromium: { launch: vi.fn() }
|
||||||
|
}))
|
||||||
|
```
|
||||||
|
|
||||||
|
- 外部依赖完全隔离
|
||||||
|
- 模拟对象结构清晰
|
||||||
|
|
||||||
|
3. **边界条件覆盖**
|
||||||
|
- `contentFrame` 返回 `null` 的异常处理
|
||||||
|
- 重复登录的会话复用
|
||||||
|
- 未登录时调用 `getSession()` 的错误处理
|
||||||
|
|
||||||
|
4. **测试命名规范**
|
||||||
|
- 使用 `should/could` 语义
|
||||||
|
- 清晰表达测试意图
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
1. **缺少真实场景集成测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// TODO: 添加集成测试
|
||||||
|
it('should login with real browser (integration)', async () => {
|
||||||
|
// 使用真实 Playwright 浏览器测试
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **错误消息验证不够精确**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 当前
|
||||||
|
expect(() => service.getSession()).toThrow('Not logged in')
|
||||||
|
|
||||||
|
// 建议
|
||||||
|
expect(() => service.getSession()).toThrow('Not logged in. Call login() first.')
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **缺少性能测试**
|
||||||
|
```typescript
|
||||||
|
it('should complete login within 5 seconds', async () => {
|
||||||
|
const start = Date.now()
|
||||||
|
await service.login()
|
||||||
|
expect(Date.now() - start).toBeLessThan(5000)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 覆盖率评估
|
||||||
|
|
||||||
|
| 方法 | 测试覆盖 | 评价 |
|
||||||
|
| --------------- | ----------------- | ---- |
|
||||||
|
| `constructor()` | ✓ 完全覆盖 | 优秀 |
|
||||||
|
| `login()` | ✓ 主要路径 + 异常 | 优秀 |
|
||||||
|
| `getSession()` | ✓ 覆盖 | 良好 |
|
||||||
|
| `isActive()` | ✓ 覆盖 | 良好 |
|
||||||
|
| `close()` | ✓ 覆盖 | 良好 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 `cleaner.test.ts` - **A (90/100)**
|
||||||
|
|
||||||
|
**测试对象**: `CleanerService` - 物料清理服务
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **纯函数测试设计优秀**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
describe('shouldDeleteMaterial()', () => {
|
||||||
|
it('should return true when material matches all deletion criteria', () => {
|
||||||
|
const result = cleaner.shouldDeleteMaterial({...})
|
||||||
|
expect(result).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
- 无副作用,易于测试
|
||||||
|
- 输入输出明确
|
||||||
|
|
||||||
|
2. **边界值测试完备**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should respect boundary row numbers', () => {
|
||||||
|
// Row 1999: can delete
|
||||||
|
expect(...).toBe(true)
|
||||||
|
// Row 2000: protected
|
||||||
|
expect(...).toBe(false)
|
||||||
|
// Row 7999: protected
|
||||||
|
expect(...).toBe(false)
|
||||||
|
// Row 8000: can delete
|
||||||
|
expect(...).toBe(true)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **辅助函数测试充分**
|
||||||
|
- `createBatches()`: 数组分批逻辑
|
||||||
|
- `runWithConcurrency()`: 并发控制验证
|
||||||
|
- `getMissingOrders()`: 集合差集计算
|
||||||
|
|
||||||
|
4. **并发测试验证**
|
||||||
|
```typescript
|
||||||
|
it('should limit parallelism to specified concurrency', async () => {
|
||||||
|
let running = 0
|
||||||
|
let peak = 0
|
||||||
|
await runWithConcurrency(items, 2, async () => {
|
||||||
|
running += 1
|
||||||
|
peak = Math.max(peak, running)
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
|
running -= 1
|
||||||
|
})
|
||||||
|
expect(peak).toBeLessThanOrEqual(2)
|
||||||
|
expect(peak).toBe(2)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
1. **缺少 `clean()` 主方法测试**
|
||||||
|
- 文件顶部有 TODO 注释说明需要集成测试
|
||||||
|
- 建议补充:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
describe('clean() - Integration', () => {
|
||||||
|
it('should complete full cleanup workflow', async () => {
|
||||||
|
// 完整流程集成测试
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **错误场景测试不足**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 建议添加
|
||||||
|
it('should handle page navigation failure', async () => {
|
||||||
|
// Mock 导航失败场景
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **干运行模式测试可以更详细**
|
||||||
|
```typescript
|
||||||
|
it('should not delete materials in dry-run mode', async () => {
|
||||||
|
// 验证 dryRun=true 时不执行实际删除
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 `ErpBrowserManager.test.ts` - **A+ (95/100)**
|
||||||
|
|
||||||
|
**测试对象**: `ErpBrowserManager` - 浏览器管理器
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **状态管理测试完备**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should return existing browser if running', async () => {
|
||||||
|
const firstBrowser = await manager.launch()
|
||||||
|
const secondBrowser = await manager.launch()
|
||||||
|
expect(firstBrowser).toBe(secondBrowser)
|
||||||
|
expect(chromium.launch).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **参数化测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it.each([true, false])('should launch with headless=%s', async (headless) => {
|
||||||
|
const manager = new ErpBrowserManager({ headless })
|
||||||
|
await manager.launch()
|
||||||
|
expect(chromium.launch).toHaveBeenCalledWith(expect.objectContaining({ headless }))
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **错误恢复测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should close browser even if context.close fails', async () => {
|
||||||
|
mockContext.close.mockRejectedValue(new Error('Context close error'))
|
||||||
|
await manager.close()
|
||||||
|
expect(mockBrowser.close).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **生命周期覆盖全面**
|
||||||
|
- 启动 → 初始化 → 导航 → 创建上下文 → 关闭
|
||||||
|
- 所有公开方法都有测试
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
1. **缺少超时测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should timeout on slow page navigation', async () => {
|
||||||
|
mockPage.goto.mockImplementation(() => new Promise((resolve) => setTimeout(resolve, 60000)))
|
||||||
|
await expect(manager.navigate('http://slow.com')).rejects.toThrow('timeout')
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **可以添加内存泄漏检测**
|
||||||
|
```typescript
|
||||||
|
it('should release all resources after close', async () => {
|
||||||
|
await manager.launch()
|
||||||
|
await manager.close()
|
||||||
|
// 验证没有悬空引用
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.4 `extractor-core.test.ts` - **B+ (85/100)**
|
||||||
|
|
||||||
|
**测试对象**: `ExtractorCore` - 提取核心逻辑
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **私有方法测试策略合理**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// @ts-ignore - accessing private method for testing
|
||||||
|
await extractorCore.waitForLoading(mockWorkFrame)
|
||||||
|
```
|
||||||
|
|
||||||
|
- 使用 `@ts-ignore` 测试私有方法是可接受的
|
||||||
|
- 避免了为了测试而暴露内部实现
|
||||||
|
|
||||||
|
2. **进度回调测试精确**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should calculate progress correctly', async () => {
|
||||||
|
await extractorCore.downloadAllBatches(input)
|
||||||
|
expect(progressCallback).toHaveBeenNthCalledWith(1, '处理批次 1/2', 40, {...})
|
||||||
|
expect(progressCallback).toHaveBeenNthCalledWith(2, '处理批次 2/2', 60, {...})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **错误处理验证**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should handle errors in batch download gracefully', async () => {
|
||||||
|
vi.spyOn(extractorCore as any, 'downloadBatch')
|
||||||
|
.mockResolvedValueOnce('/path/file1.xlsx')
|
||||||
|
.mockRejectedValueOnce(new Error('Network error'))
|
||||||
|
|
||||||
|
const result = await extractorCore.downloadAllBatches(input)
|
||||||
|
expect(result.errors).toHaveLength(1)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 不足 ⚠️
|
||||||
|
|
||||||
|
1. **3 个测试标记为 TODO**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it.todo('TODO: needs integration test setup - should handle complete navigation flow')
|
||||||
|
it.todo('TODO: needs integration test setup - should handle download events correctly')
|
||||||
|
it.todo('TODO: needs integration test setup - should verify locator interactions')
|
||||||
|
```
|
||||||
|
|
||||||
|
- **影响**: 核心功能缺少完整流程测试
|
||||||
|
- **建议**: 优先级 P0,尽快补充集成测试
|
||||||
|
|
||||||
|
2. **Mock 过于复杂**
|
||||||
|
- `navigateToExtractorPage` 和 `downloadBatch` 都被 Mock
|
||||||
|
- 实际只测试了流程编排,未测试真实逻辑
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
**高优先级**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 集成测试示例
|
||||||
|
describe('ExtractorCore - Integration', () => {
|
||||||
|
it('should handle real iframe navigation', async () => {
|
||||||
|
// 使用真实 Playwright 浏览器
|
||||||
|
// 测试完整的 iframe 查找和内容帧获取
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.5 `extractor.test.ts` - **A (90/100)**
|
||||||
|
|
||||||
|
**测试对象**: `ExtractorService` - 提取服务
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **依赖注入测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
beforeEach(() => {
|
||||||
|
mockExcelParserInstance = { parse: vi.fn().mockResolvedValue(undefined) }
|
||||||
|
mockDataImportInstance = { importFromExcel: vi.fn().mockResolvedValue({...}) }
|
||||||
|
mockExtractorCoreInstance = { downloadAllBatches: vi.fn().mockResolvedValue({...}) }
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **私有方法测试合理**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// @ts-ignore - accessing private method for testing
|
||||||
|
const result = await service.mergeFiles(['./file1.xlsx'], ['ORD001'])
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **错误传播测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should handle extraction errors gracefully', async () => {
|
||||||
|
mockExtractorCoreInstance.downloadAllBatches.mockRejectedValue(new Error('Network error'))
|
||||||
|
const result = await service.extract({ orderNumbers: ['ORD001'] })
|
||||||
|
expect(Array.isArray(result.errors)).toBe(true)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **性能监控集成测试**
|
||||||
|
```typescript
|
||||||
|
it('should wrap import in trackDuration', async () => {
|
||||||
|
await service.importToDatabaseWithLogging('./merged.xlsx', onLog)
|
||||||
|
expect(trackDuration).toHaveBeenCalledWith(
|
||||||
|
expect.any(Function),
|
||||||
|
expect.objectContaining({ operationName: 'Database Import' })
|
||||||
|
)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
1. **缺少 `extract()` 主方法完整流程测试**
|
||||||
|
- 只有基础行为测试
|
||||||
|
- 建议添加完整 E2E 流程
|
||||||
|
|
||||||
|
2. **Mock 重置策略可以更清晰**
|
||||||
|
```typescript
|
||||||
|
// 建议在每个测试前明确重置所有 Mock
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
mockExcelParserInstance.lastOrders = [] // 显式清空
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.6 `order-resolver.test.ts` - **A+ (95/100)**
|
||||||
|
|
||||||
|
**测试对象**: `OrderNumberResolver` - 订单号解析器
|
||||||
|
|
||||||
|
#### 优点 ✅
|
||||||
|
|
||||||
|
1. **测试覆盖率最高**
|
||||||
|
- 26 个测试用例,覆盖所有公开方法
|
||||||
|
- 包含性能测试
|
||||||
|
|
||||||
|
2. **类型识别测试完备**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
describe('isProductionId()', () => {
|
||||||
|
it('should recognize valid production IDs', () => {
|
||||||
|
expect(resolver.isProductionId('22A1')).toBe(true)
|
||||||
|
expect(resolver.isProductionId('26B10617')).toBe(true)
|
||||||
|
})
|
||||||
|
it('should reject invalid formats', () => {
|
||||||
|
expect(resolver.isProductionId('SC70202602120085')).toBe(false)
|
||||||
|
expect(resolver.isProductionId('abc')).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **去重逻辑测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('deduplicates identical inputs', async () => {
|
||||||
|
const results = await resolver.resolve(['22A1', '22A1', '22A1'])
|
||||||
|
expect(results).toHaveLength(1) // deduplicated
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **性能测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('performance with large order sets', async () => {
|
||||||
|
const largeInput = Array.from({ length: 100 }, (_, i) => `22A${i}`)
|
||||||
|
const startTime = Date.now()
|
||||||
|
const results = await resolver.resolve(largeInput)
|
||||||
|
const elapsed = Date.now() - startTime
|
||||||
|
expect(elapsed).toBeLessThan(5000)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
5. **统计和报告测试**
|
||||||
|
- `getStats()`: 统计数据准确性
|
||||||
|
- `getWarnings()`: 警告消息格式化
|
||||||
|
- `getDeduplicationReport()`: 去重报告生成
|
||||||
|
|
||||||
|
#### 改进建议 🔧
|
||||||
|
|
||||||
|
1. **可以添加数据库连接失败的重试测试**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should retry on transient database errors', async () => {
|
||||||
|
// Mock 第一次失败,第二次成功
|
||||||
|
// 验证重试逻辑
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **缓存策略测试可以更详细**
|
||||||
|
```typescript
|
||||||
|
it('should cache resolved mappings', async () => {
|
||||||
|
// 验证相同输入不会重复查询数据库
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 共性问题与建议
|
||||||
|
|
||||||
|
### 3.1 Mock 策略优化
|
||||||
|
|
||||||
|
**当前做法**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
vi.mock('playwright', () => ({
|
||||||
|
chromium: { launch: vi.fn() }
|
||||||
|
}))
|
||||||
|
```
|
||||||
|
|
||||||
|
**建议改进**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 使用工厂函数创建可重置的 Mock
|
||||||
|
const createMockPlaywright = () => ({
|
||||||
|
chromium: {
|
||||||
|
launch: vi.fn().mockResolvedValue(createMockBrowser()),
|
||||||
|
connect: vi.fn()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.mocked(chromium.launch).mockResolvedValue(createMockBrowser())
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**好处**:
|
||||||
|
|
||||||
|
- 每个测试独立的 Mock 状态
|
||||||
|
- 避免测试间的相互影响
|
||||||
|
- 更易维护
|
||||||
|
|
||||||
|
### 3.2 测试数据工厂
|
||||||
|
|
||||||
|
**当前**: 手动创建测试数据
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const config = {
|
||||||
|
url: 'https://test-erp.com',
|
||||||
|
username: 'testuser',
|
||||||
|
password: 'testpass',
|
||||||
|
headless: true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**建议**: 使用工厂函数
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// tests/fixtures/factory.ts
|
||||||
|
const ErpConfigFactory = {
|
||||||
|
create: (overrides?: Partial<ErpConfig>) => ({
|
||||||
|
url: 'https://test-erp.com',
|
||||||
|
username: 'testuser',
|
||||||
|
password: 'testpass',
|
||||||
|
headless: true,
|
||||||
|
...overrides
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试中
|
||||||
|
const config = ErpConfigFactory.create({ headless: false })
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 错误消息断言
|
||||||
|
|
||||||
|
**当前**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await expect(service.login()).rejects.toThrow('Failed to access')
|
||||||
|
```
|
||||||
|
|
||||||
|
**建议**: 使用更精确的匹配
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await expect(service.login()).rejects.toThrow(
|
||||||
|
expect.objectContaining({
|
||||||
|
message: expect.stringContaining('Failed to access forwardFrame')
|
||||||
|
})
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.4 集成测试缺失
|
||||||
|
|
||||||
|
**问题**: 多个文件有 TODO 注释说明需要集成测试
|
||||||
|
|
||||||
|
**建议优先级**:
|
||||||
|
|
||||||
|
1. **P0**: `extractor-core.test.ts` - 3 个 TODO
|
||||||
|
2. **P1**: `extractor.test.ts` - `extract()` 完整流程
|
||||||
|
3. **P1**: `cleaner.test.ts` - `clean()` 完整流程
|
||||||
|
|
||||||
|
**集成测试框架建议**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// tests/integration/erp/extractor.integration.test.ts
|
||||||
|
import { test, expect } from '@playwright/test'
|
||||||
|
|
||||||
|
test('complete extraction workflow', async () => {
|
||||||
|
// 使用真实浏览器
|
||||||
|
// 测试完整提取流程
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 测试设计模式评估
|
||||||
|
|
||||||
|
### 4.1 AAA 模式 (Arrange-Act-Assert)
|
||||||
|
|
||||||
|
**评分**: **优秀** ✅
|
||||||
|
|
||||||
|
所有测试都遵循 AAA 模式:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should create session on successful login', async () => {
|
||||||
|
// Arrange
|
||||||
|
service = new ErpAuthService(config)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const session = await service.login()
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(chromium.launch).toHaveBeenCalledWith(...)
|
||||||
|
expect(session.isLoggedIn).toBe(true)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 测试独立性
|
||||||
|
|
||||||
|
**评分**: **良好** ⚠️
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
|
||||||
|
- 每个测试使用 `beforeEach` 重置状态
|
||||||
|
- `vi.clearAllMocks()` 调用普遍
|
||||||
|
|
||||||
|
**改进点**:
|
||||||
|
|
||||||
|
- 部分测试依赖前一个测试的 Mock 状态
|
||||||
|
- 建议在每个测试中完全独立设置 Mock
|
||||||
|
|
||||||
|
### 4.3 测试可读性
|
||||||
|
|
||||||
|
**评分**: **优秀** ✅
|
||||||
|
|
||||||
|
- 测试命名清晰:`should/could` 语义
|
||||||
|
- 分组合理:`describe` 层次分明
|
||||||
|
- 注释充分:关键步骤有说明
|
||||||
|
|
||||||
|
### 4.4 测试可维护性
|
||||||
|
|
||||||
|
**评分**: **良好** ⚠️
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
|
||||||
|
- 代码结构清晰
|
||||||
|
- 重复代码较少
|
||||||
|
|
||||||
|
**改进点**:
|
||||||
|
|
||||||
|
- 缺少测试数据工厂
|
||||||
|
- Mock 设置代码重复
|
||||||
|
- 魔法数字(如 `40`, `60` 进度值)缺少常量定义
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 覆盖率分析
|
||||||
|
|
||||||
|
### 5.1 方法覆盖率
|
||||||
|
|
||||||
|
| 服务 | 公开方法 | 已测试 | 覆盖率 |
|
||||||
|
| --------------------- | -------- | ------ | ------ |
|
||||||
|
| `ErpAuthService` | 5 | 5 | 100% |
|
||||||
|
| `CleanerService` | 7 | 4 | 57% ⚠️ |
|
||||||
|
| `ErpBrowserManager` | 9 | 9 | 100% |
|
||||||
|
| `ExtractorCore` | 3 | 2 | 67% ⚠️ |
|
||||||
|
| `ExtractorService` | 5 | 4 | 80% |
|
||||||
|
| `OrderNumberResolver` | 10 | 10 | 100% |
|
||||||
|
|
||||||
|
### 5.2 分支覆盖率估算
|
||||||
|
|
||||||
|
| 服务 | 条件分支 | 已覆盖 | 估算覆盖率 |
|
||||||
|
| --------------------- | -------- | ------ | ---------- |
|
||||||
|
| `ErpAuthService` | 8 | 7 | 87% |
|
||||||
|
| `CleanerService` | 15 | 12 | 80% |
|
||||||
|
| `ErpBrowserManager` | 10 | 9 | 90% |
|
||||||
|
| `ExtractorCore` | 12 | 8 | 67% |
|
||||||
|
| `ExtractorService` | 14 | 11 | 78% |
|
||||||
|
| `OrderNumberResolver` | 20 | 18 | 90% |
|
||||||
|
|
||||||
|
### 5.3 未覆盖的关键路径
|
||||||
|
|
||||||
|
1. **CleanerService**
|
||||||
|
- `clean()` 主方法的完整流程
|
||||||
|
- 重试机制 (`retryFailedOrders`)
|
||||||
|
- 进度发布 (`publishProgress`)
|
||||||
|
|
||||||
|
2. **ExtractorCore**
|
||||||
|
- `navigateToExtractorPage()` 完整导航逻辑
|
||||||
|
- `downloadBatch()` 实际下载流程
|
||||||
|
- iframe 交互的真实场景
|
||||||
|
|
||||||
|
3. **ExtractorService**
|
||||||
|
- `extract()` 方法的完整编排流程
|
||||||
|
- 并发控制在实际场景中的表现
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 性能测试评估
|
||||||
|
|
||||||
|
### 6.1 现有性能测试
|
||||||
|
|
||||||
|
**优秀示例**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('performance with large order sets', async () => {
|
||||||
|
const largeInput = Array.from({ length: 100 }, (_, i) => `22A${i}`)
|
||||||
|
const startTime = Date.now()
|
||||||
|
const results = await resolver.resolve(largeInput)
|
||||||
|
const elapsed = Date.now() - startTime
|
||||||
|
expect(elapsed).toBeLessThan(5000)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.2 缺失的性能测试
|
||||||
|
|
||||||
|
1. **并发性能**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should handle 1000 concurrent orders', async () => {
|
||||||
|
const orders = Array.from({ length: 1000 }, (_, i) => `ORD${i}`)
|
||||||
|
const start = Date.now()
|
||||||
|
await resolver.resolve(orders)
|
||||||
|
expect(Date.now() - start).toBeLessThan(10000)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **内存使用**
|
||||||
|
```typescript
|
||||||
|
it('should not leak memory on repeated calls', async () => {
|
||||||
|
const initialMemory = process.memoryUsage().heapUsed
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
await service.extract({ orderNumbers: ['ORD001'] })
|
||||||
|
}
|
||||||
|
const finalMemory = process.memoryUsage().heapUsed
|
||||||
|
expect(finalMemory - initialMemory).toBeLessThan(10 * 1024 * 1024) // < 10MB
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 错误处理测试评估
|
||||||
|
|
||||||
|
### 7.1 优秀实践 ✅
|
||||||
|
|
||||||
|
1. **网络错误处理**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
mockExtractorCoreInstance.downloadAllBatches.mockRejectedValue(new Error('Network error'))
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **数据库连接失败**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
vi.mocked(mockDbService.query).mockRejectedValue(new Error('Database connection failed'))
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **元素未找到**
|
||||||
|
```typescript
|
||||||
|
mockPage.locator = vi.fn().mockReturnValue({
|
||||||
|
contentFrame: vi.fn().mockResolvedValue(null)
|
||||||
|
})
|
||||||
|
await expect(service.login()).rejects.toThrow('Failed to access')
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.2 改进建议 🔧
|
||||||
|
|
||||||
|
1. **添加错误类型验证**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
it('should throw specific error types', async () => {
|
||||||
|
await expect(service.login()).rejects.toThrow(ErpAuthenticationError)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **错误上下文验证**
|
||||||
|
```typescript
|
||||||
|
it('should include context in error messages', async () => {
|
||||||
|
try {
|
||||||
|
await service.login()
|
||||||
|
} catch (error) {
|
||||||
|
expect(error.context).toEqual({
|
||||||
|
url: 'https://test-erp.com',
|
||||||
|
step: 'login'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 与测试覆盖率提升计划对标
|
||||||
|
|
||||||
|
### 8.1 计划目标回顾
|
||||||
|
|
||||||
|
根据 `TEST_COVERAGE_IMPROVEMENT_PLAN.md`:
|
||||||
|
|
||||||
|
| 模块 | 当前覆盖率 | 目标覆盖率 | 优先级 |
|
||||||
|
| ---------------------- | ---------- | ---------- | ------ |
|
||||||
|
| `erp-auth.ts` | < 20% | 80% | P0 |
|
||||||
|
| `extractor.ts` | ~30% | 80% | P0 |
|
||||||
|
| `extractor-core.ts` | < 10% | 80% | P0 |
|
||||||
|
| `cleaner.ts` | ~25% | 80% | P0 |
|
||||||
|
| `ErpBrowserManager.ts` | N/A | 80% | P1 |
|
||||||
|
| `order-resolver.ts` | N/A | 80% | P1 |
|
||||||
|
|
||||||
|
### 8.2 当前进展
|
||||||
|
|
||||||
|
**估算覆盖率提升**:
|
||||||
|
|
||||||
|
| 模块 | 测试前 | 测试后(估算) | 提升 | 达标状态 |
|
||||||
|
| ---------------------- | ------ | -------------- | ---- | ------------------- |
|
||||||
|
| `erp-auth.ts` | < 20% | ~75% | +55% | ⚠️ 接近达标 |
|
||||||
|
| `extractor.ts` | ~30% | ~70% | +40% | ⚠️ 接近达标 |
|
||||||
|
| `extractor-core.ts` | < 10% | ~55% | +45% | ❌ 需补充集成测试 |
|
||||||
|
| `cleaner.ts` | ~25% | ~65% | +40% | ⚠️ 需补充主方法测试 |
|
||||||
|
| `ErpBrowserManager.ts` | N/A | ~85% | N/A | ✅ 已达标 |
|
||||||
|
| `order-resolver.ts` | N/A | ~90% | N/A | ✅ 已达标 |
|
||||||
|
|
||||||
|
### 8.3 下一步行动
|
||||||
|
|
||||||
|
**P0 - 立即执行**:
|
||||||
|
|
||||||
|
1. 补充 `extractor-core.test.ts` 的 3 个 TODO 测试
|
||||||
|
2. 添加 `cleaner.ts` 的 `clean()` 方法集成测试
|
||||||
|
3. 补充 `extractor.ts` 的 `extract()` 完整流程测试
|
||||||
|
|
||||||
|
**P1 - 本周执行**:
|
||||||
|
|
||||||
|
1. 为所有错误路径添加断言
|
||||||
|
2. 添加性能测试覆盖关键路径
|
||||||
|
3. 创建测试数据工厂减少重复代码
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 总体评价与建议
|
||||||
|
|
||||||
|
### 9.1 优点总结
|
||||||
|
|
||||||
|
1. **测试设计优秀**
|
||||||
|
- AAA 模式遵循良好
|
||||||
|
- 测试命名清晰
|
||||||
|
- 分组合理
|
||||||
|
|
||||||
|
2. **Mock 策略成熟**
|
||||||
|
- 外部依赖完全隔离
|
||||||
|
- Mock 对象结构清晰
|
||||||
|
- 参数化测试使用得当
|
||||||
|
|
||||||
|
3. **错误处理充分**
|
||||||
|
- 主要错误场景都有覆盖
|
||||||
|
- 异常传播验证到位
|
||||||
|
|
||||||
|
4. **边界条件重视**
|
||||||
|
- 边界值测试普遍
|
||||||
|
- 特殊情况考虑周全
|
||||||
|
|
||||||
|
### 9.2 改进优先级
|
||||||
|
|
||||||
|
**P0 - 必须完成(本周)**:
|
||||||
|
|
||||||
|
1. ✅ 补充 `extractor-core.test.ts` 的集成测试
|
||||||
|
2. ✅ 添加 `cleaner()` 主方法测试
|
||||||
|
3. ✅ 完成 `extractor.extract()` 完整流程测试
|
||||||
|
|
||||||
|
**P1 - 强烈建议(下周)**:
|
||||||
|
|
||||||
|
1. 创建测试数据工厂
|
||||||
|
2. 统一 Mock 设置模式
|
||||||
|
3. 添加性能基准测试
|
||||||
|
|
||||||
|
**P2 - 建议(本月)**:
|
||||||
|
|
||||||
|
1. 添加内存泄漏检测测试
|
||||||
|
2. 补充错误类型验证
|
||||||
|
3. 完善并发场景测试
|
||||||
|
|
||||||
|
### 9.3 测试文化建议
|
||||||
|
|
||||||
|
1. **测试审查流程**
|
||||||
|
- 将测试审查纳入 PR 必选项
|
||||||
|
- 使用本报告的评分标准
|
||||||
|
|
||||||
|
2. **测试文档**
|
||||||
|
- 编写《测试最佳实践》文档
|
||||||
|
- 建立测试模式库
|
||||||
|
|
||||||
|
3. **覆盖率门禁**
|
||||||
|
- CI/CD 中设置覆盖率阈值
|
||||||
|
- 新增代码覆盖率要求 ≥ 80%
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 结论
|
||||||
|
|
||||||
|
本次审查的测试文件整体质量**优秀**,展现了团队对测试工作的重视和高超的测试设计能力。主要优势在于:
|
||||||
|
|
||||||
|
- ✅ 测试设计模式成熟(AAA 模式)
|
||||||
|
- ✅ Mock 策略合理,依赖隔离充分
|
||||||
|
- ✅ 错误处理和边界条件覆盖全面
|
||||||
|
- ✅ 测试可读性和可维护性良好
|
||||||
|
|
||||||
|
需要改进的方面:
|
||||||
|
|
||||||
|
- ⚠️ 集成测试缺失(3 个 TODO 待实现)
|
||||||
|
- ⚠️ 部分主方法测试不完整
|
||||||
|
- ⚠️ 缺少性能基准测试
|
||||||
|
- ⚠️ 测试数据工厂可进一步优化
|
||||||
|
|
||||||
|
**总体评分:A (90/100)**
|
||||||
|
|
||||||
|
按照本报告的改进建议执行后,预计可将 ERP 服务模块的测试覆盖率提升至 **75-85%**,达到项目设定的阶段性目标。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**附录 A: 测试运行统计**
|
||||||
|
|
||||||
|
```
|
||||||
|
Test Files: 8 passed (8)
|
||||||
|
Tests: 114 passed | 3 todo (117)
|
||||||
|
Duration: ~1.0s
|
||||||
|
Setup: ~259ms
|
||||||
|
Transform: ~708ms
|
||||||
|
```
|
||||||
|
|
||||||
|
**附录 B: 审查工具**
|
||||||
|
|
||||||
|
- Vitest 测试运行器
|
||||||
|
- Playwright Mock 库
|
||||||
|
- TypeScript 类型检查
|
||||||
|
- ESLint 代码规范检查
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**报告结束**
|
||||||
309
docs/cleaner-role-based-flow.md
Normal file
309
docs/cleaner-role-based-flow.md
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
# 清理器角色差异流程 — Admin vs User
|
||||||
|
|
||||||
|
**文档版本**: 1.1
|
||||||
|
**创建日期**: 2026-04-06
|
||||||
|
**面向对象**: 开发人员
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
清理器(Cleaner)在决定"哪些物料需要被清除"时,Admin 和 User 两个角色存在系统性的差异。这些差异贯穿三个阶段:**初始化 → 校验确认 → 执行清理**。
|
||||||
|
|
||||||
|
本文档使用 Mermaid 图表说明每个阶段的角色分支逻辑。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 全局流程概览
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
subgraph init["阶段一:页面初始化"]
|
||||||
|
I1([页面加载]) --> I2{角色判断}
|
||||||
|
I2 -->|Admin| I3["管理员列表 ← 全部负责人<br/>默认选中全部"]
|
||||||
|
I2 -->|User| I4["管理员列表 ← 空<br/>默认选中仅自己"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph validate["阶段二:校验 → 勾选 → 同步数据库"]
|
||||||
|
V1([点击校验]) --> V2["后端查询物料<br/>(不区分角色)"]
|
||||||
|
V2 --> V3["物料匹配算法<br/>(User 有覆盖匹配)"]
|
||||||
|
V3 --> V4{角色判断}
|
||||||
|
V4 -->|Admin| V5["显示全部物料<br/>侧边栏可按负责人筛选"]
|
||||||
|
V4 -->|User| V6["仅显示自己的物料<br/>+ 无负责人的物料"]
|
||||||
|
V5 --> V7["用户勾选/取消勾选"]
|
||||||
|
V6 --> V7
|
||||||
|
V7 --> V8{点击同步数据库}
|
||||||
|
V8 --> V9{角色判断}
|
||||||
|
V9 -->|Admin| V10["处理范围:全部校验结果"]
|
||||||
|
V9 -->|User| V11["处理范围:仅筛选后结果"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph execute["阶段三:执行清理(ERP 删除)"]
|
||||||
|
E1([点击执行清理]) --> E2["getCleanerData(selectedManagers)<br/>获取物料代码"]
|
||||||
|
E2 --> E3{角色判断}
|
||||||
|
E3 -->|Admin| E3a{selectedManagers<br/>非空?}
|
||||||
|
E3a -->|"是"| E4["SQL WHERE ManagerName IN (选中)<br/>从 MaterialsToBeDeleted 获取"]
|
||||||
|
E3a -->|"否"| E4b["从 DiscreteMaterialPlanData<br/>按 orderNumbers 获取"]
|
||||||
|
E3 -->|User| E5["SQL WHERE ManagerName = 用户<br/>仅获取自己的物料代码"]
|
||||||
|
E4 --> E6["传递给 runCleaner 执行"]
|
||||||
|
E4b --> E6
|
||||||
|
E5 --> E6
|
||||||
|
E6 --> E7([在 ERP 中删除物料])
|
||||||
|
end
|
||||||
|
|
||||||
|
init --> validate --> execute
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 阶段一:页面初始化
|
||||||
|
|
||||||
|
**源码位置**: `src/renderer/src/hooks/cleaner/api.ts:25-52` 与 `src/renderer/src/hooks/useCleaner.ts:98-112`
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
Start([页面加载]) --> GetAdmin["调用 auth:isAdmin<br/>判断是否管理员"]
|
||||||
|
GetAdmin --> GetUser["调用 auth:getCurrentUser<br/>获取当前用户名"]
|
||||||
|
GetUser --> RoleCheck{isAdmin?}
|
||||||
|
|
||||||
|
RoleCheck -->|Admin| GetManagers["调用 materials:getManagers<br/>获取全部负责人列表"]
|
||||||
|
GetManagers --> SelectAll["selectedManagers ← 全部负责人<br/>(默认全选)"]
|
||||||
|
SelectAll --> RenderSidebar["渲染 CleanerSidebar<br/>显示负责人复选框"]
|
||||||
|
|
||||||
|
RoleCheck -->|User| SetSelf["selectedManagers ← {currentUsername}<br/>(仅选中自己)"]
|
||||||
|
SetSelf --> NoSidebar["不渲染 CleanerSidebar<br/>无侧边栏"]
|
||||||
|
|
||||||
|
RenderSidebar --> Ready([就绪])
|
||||||
|
NoSidebar --> Ready
|
||||||
|
```
|
||||||
|
|
||||||
|
**差异总结**:
|
||||||
|
|
||||||
|
| 维度 | Admin | User |
|
||||||
|
| ---------- | ----------------- | ------ |
|
||||||
|
| 侧边栏 | 有 CleanerSidebar | 无 |
|
||||||
|
| 管理员列表 | 查询全部负责人 | 不查询 |
|
||||||
|
| 默认选中 | 所有负责人 | 仅自己 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 阶段二:校验 → 勾选 → 同步数据库
|
||||||
|
|
||||||
|
### 2.1 物料校验(后端,不区分角色)
|
||||||
|
|
||||||
|
**源码位置**: `src/main/services/validation/validation-application-service.ts`
|
||||||
|
|
||||||
|
校验阶段后端查询不区分角色,Admin 和 User 拿到相同的物料数据。区别在于**匹配算法**:
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
Start([遍历每条物料记录]) --> P1{"优先级1<br/>MaterialsToBeDeleted<br/>精确匹配 MaterialCode?"}
|
||||||
|
|
||||||
|
P1 -->|"匹配"| SetManager["managerName ← 表中记录<br/>isMarkedForDeletion = true"]
|
||||||
|
P1 -->|"未匹配"| P2{"优先级2<br/>MaterialsTypeToBeDeleted<br/>MaterialName 包含匹配?"}
|
||||||
|
|
||||||
|
P2 -->|"匹配"| SetType["managerName ← 类型关键词负责人<br/>matchedTypeKeyword ← 匹配项"]
|
||||||
|
P2 -->|"未匹配"| SetNull["managerName = null"]
|
||||||
|
|
||||||
|
SetManager --> RoleCheck{角色?}
|
||||||
|
SetType --> RoleCheck
|
||||||
|
SetNull --> RoleCheck
|
||||||
|
|
||||||
|
RoleCheck -->|"Admin"| Skip["跳过覆盖<br/>使用当前结果"]
|
||||||
|
RoleCheck -->|"User"| P3{"优先级3(User 覆盖)<br/>自己的类型关键词匹配?"}
|
||||||
|
|
||||||
|
P3 -->|"匹配"| Override["强制覆盖<br/>managerName ← 当前用户"]
|
||||||
|
P3 -->|"未匹配"| Keep["保持当前结果"]
|
||||||
|
Skip --> Next(["下一条物料"])
|
||||||
|
Override --> Next
|
||||||
|
Keep --> Next
|
||||||
|
```
|
||||||
|
|
||||||
|
**匹配优先级说明**:
|
||||||
|
|
||||||
|
| 优先级 | 数据源 | 匹配方式 | 适用角色 |
|
||||||
|
| -------------- | -------------------------- | --------------------- | -------- |
|
||||||
|
| 1(最高) | `MaterialsToBeDeleted` | MaterialCode 精确匹配 | 全部 |
|
||||||
|
| 2 | `MaterialsTypeToBeDeleted` | MaterialName 包含匹配 | 全部 |
|
||||||
|
| 3(User 覆盖) | 当前用户的类型关键词 | MaterialName 包含匹配 | 仅 User |
|
||||||
|
|
||||||
|
> **优先级 3 的作用**:当某个物料按优先级 2 被分配给其他负责人,但当前 User 有匹配的类型关键词时,会强制覆盖为自己的。这确保 User 不会为他人操作物料。
|
||||||
|
|
||||||
|
### 2.2 前端显示过滤
|
||||||
|
|
||||||
|
**源码位置**: `src/renderer/src/hooks/cleaner/helpers.ts:34-57`
|
||||||
|
|
||||||
|
校验结果返回前端后,会根据角色进行显示过滤:
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
Input([校验结果 validationResults]) --> RoleCheck{角色判断}
|
||||||
|
|
||||||
|
RoleCheck -->|Admin| FilterManagers["按侧边栏选中的负责人过滤<br/>selectedManagers.has(managerName)<br/>|| !managerName"]
|
||||||
|
RoleCheck -->|User| FilterSelf["仅显示自己的 + 无负责人的<br/>managerName === currentUsername<br/>|| !managerName"]
|
||||||
|
|
||||||
|
FilterManagers --> FilterHidden["排除已隐藏的物料<br/>!hiddenItems.has(materialCode)"]
|
||||||
|
FilterSelf --> FilterHidden
|
||||||
|
|
||||||
|
FilterHidden --> Output([filteredResults<br/>用于表格显示])
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.3 确认删除(同步数据库)
|
||||||
|
|
||||||
|
**源码位置**: `src/renderer/src/hooks/useCleaner.ts:289-344`
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
Start([点击确认删除]) --> RoleScope{角色判断}
|
||||||
|
|
||||||
|
RoleScope -->|Admin| UseAll["resultsToProcess = validationResults<br/>处理全部校验结果"]
|
||||||
|
RoleScope -->|User| UseFiltered["resultsToProcess = filteredResults<br/>仅处理筛选后结果"]
|
||||||
|
|
||||||
|
UseAll --> BuildPlan["buildDeletionPlan(resultsToProcess, selectedItems)"]
|
||||||
|
UseFiltered --> BuildPlan
|
||||||
|
|
||||||
|
BuildPlan --> Loop["遍历 resultsToProcess"]
|
||||||
|
Loop --> Check{物料是否勾选?}
|
||||||
|
|
||||||
|
Check -->|"已勾选"| HasManager{有负责人?}
|
||||||
|
Check -->|"未勾选"| ToDelete["加入 materialsToDelete<br/>从数据库移除标记"]
|
||||||
|
|
||||||
|
HasManager -->|"有"| ToUpsert["加入 materialsToUpsert<br/>写入/更新到数据库"]
|
||||||
|
HasManager -->|"无"| Missing["加入 missingManager<br/>阻止操作"]
|
||||||
|
|
||||||
|
ToUpsert --> Save["调用 materials:upsertBatch"]
|
||||||
|
ToDelete --> Del["调用 materials:delete"]
|
||||||
|
Missing --> Warn(["弹窗警告:缺少负责人"])
|
||||||
|
Save --> Done([完成])
|
||||||
|
Del --> Done
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键代码**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Admin 处理全部结果,User 只处理筛选后的结果
|
||||||
|
const resultsToProcess = isAdmin ? validationResults : filteredResults
|
||||||
|
```
|
||||||
|
|
||||||
|
**差异总结**:
|
||||||
|
|
||||||
|
| 维度 | Admin | User |
|
||||||
|
| ---------------- | --------------------------- | -------------------------------------- |
|
||||||
|
| 处理范围 | `validationResults`(全部) | `filteredResults`(自己的+无负责人的) |
|
||||||
|
| 可操作物料 | 所有负责人的物料 | 仅自己的 + 无负责人的 |
|
||||||
|
| 能否修改他人数据 | 是 | 否 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 阶段三:执行清理(ERP 删除)
|
||||||
|
|
||||||
|
**源码位置**:
|
||||||
|
|
||||||
|
- 前端调用: `src/renderer/src/hooks/cleaner/api.ts:116-166`
|
||||||
|
- 获取数据: `src/main/services/validation/validation-application-service.ts:497-655`
|
||||||
|
- 执行删除: `src/main/services/cleaner/cleaner-application-service.ts`
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant UI as 前端 useCleaner
|
||||||
|
participant API as api.ts
|
||||||
|
participant Main as 主进程
|
||||||
|
participant DB as 数据库
|
||||||
|
participant ERP as ERP 系统
|
||||||
|
|
||||||
|
UI->>API: runCleanerExecution({ dryRun, selectedManagers, ... })
|
||||||
|
API->>Main: getCleanerData({ selectedManagers })
|
||||||
|
|
||||||
|
alt Admin + selectedManagers 非空
|
||||||
|
Main->>DB: SELECT MaterialCode FROM MaterialsToBeDeleted<br/>WHERE ManagerName IN (@manager0, @manager1, ...)
|
||||||
|
Note over Main,DB: 按选中的负责人过滤<br/>从 MaterialsToBeDeleted 获取
|
||||||
|
else Admin + selectedManagers 为空
|
||||||
|
Main->>DB: SELECT DISTINCT MaterialCode FROM DiscreteMaterialPlanData<br/>WHERE SourceNumber IN (orderNumbers)
|
||||||
|
Note over Main,DB: 按订单号查询<br/>从 DiscreteMaterialPlanData 获取
|
||||||
|
else User
|
||||||
|
Main->>DB: SELECT MaterialCode FROM MaterialsToBeDeleted<br/>WHERE ManagerName = @username
|
||||||
|
Note over Main,DB: 按 ManagerName 过滤<br/>仅获取自己的物料代码
|
||||||
|
end
|
||||||
|
|
||||||
|
DB-->>Main: materialCodes[]
|
||||||
|
Main-->>API: { orderNumbers, materialCodes }
|
||||||
|
|
||||||
|
Note over API: 传入角色过滤后的 materialCodes
|
||||||
|
API->>Main: cleaner.runCleaner({ orderNumbers, materialCodes, ... })
|
||||||
|
|
||||||
|
Main->>ERP: 按订单遍历,删除指定物料
|
||||||
|
ERP-->>Main: 删除结果
|
||||||
|
Main-->>API: CleanerResult
|
||||||
|
API-->>UI: 显示执行报告
|
||||||
|
```
|
||||||
|
|
||||||
|
**SQL 差异**:
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
subgraph AdminWithMgr["Admin + selectedManagers 非空"]
|
||||||
|
A1["SELECT MaterialCode<br/>FROM MaterialsToBeDeleted<br/>WHERE ManagerName IN (@manager0, ...)<br/>AND MaterialCode IS NOT NULL"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph AdminNoMgr["Admin + selectedManagers 为空"]
|
||||||
|
A2["SELECT DISTINCT MaterialCode<br/>FROM DiscreteMaterialPlanData<br/>WHERE SourceNumber IN (orderNumbers)"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph User["User 查询"]
|
||||||
|
U1["SELECT MaterialCode<br/>FROM MaterialsToBeDeleted<br/>WHERE ManagerName = @username<br/>AND MaterialCode IS NOT NULL"]
|
||||||
|
end
|
||||||
|
|
||||||
|
AdminWithMgr --> |"按选中负责人过滤"| Result([传入 runCleaner])
|
||||||
|
AdminNoMgr --> |"按订单号查 DiscreteMaterialPlanData"| Result
|
||||||
|
User --> |"仅返回自己的物料代码"| Result
|
||||||
|
```
|
||||||
|
|
||||||
|
**差异总结**:
|
||||||
|
|
||||||
|
| 维度 | Admin(有 selectedManagers) | Admin(无 selectedManagers) | User |
|
||||||
|
| ---------- | ---------------------------- | -------------------------------------- | ------------------------------- |
|
||||||
|
| 数据源 | `MaterialsToBeDeleted` | `DiscreteMaterialPlanData` | `MaterialsToBeDeleted` |
|
||||||
|
| 查询条件 | `WHERE ManagerName IN (...)` | `WHERE SourceNumber IN (orderNumbers)` | `WHERE ManagerName = @username` |
|
||||||
|
| 可删除物料 | 选中负责人的物料 | 订单关联的全部物料 | 仅自己标记的物料 |
|
||||||
|
| 无订单号时 | — | 返回空数组 | — |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 数据安全边界
|
||||||
|
|
||||||
|
角色隔离在**三个层面**同时生效,形成纵深防御:
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart TB
|
||||||
|
subgraph layer1["第一层:前端过滤"]
|
||||||
|
L1["filterValidationResults()<br/>User 仅看到自己的物料"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph layer2["第二层:同步范围"]
|
||||||
|
L2["handleConfirmDeletion()<br/>User 仅同步 filteredResults"]
|
||||||
|
end
|
||||||
|
|
||||||
|
subgraph layer3["第三层:后端查询"]
|
||||||
|
L3["loadMaterialCodesForCleaner()<br/>Admin: WHERE ManagerName IN (selectedManagers)<br/>User: SQL WHERE ManagerName = user"]
|
||||||
|
end
|
||||||
|
|
||||||
|
L1 -->|"防止误操作"| L2
|
||||||
|
L2 -->|"缩小同步范围"| L3
|
||||||
|
L3 -->|"最终保证"| Safe([User 无法删除他人物料])
|
||||||
|
```
|
||||||
|
|
||||||
|
> **注意**:`runCleaner()` 本身不做角色过滤,它信任上游传入的 `materialCodes` 已经过角色过滤。安全性由 `getCleanerData()` 的 SQL 查询保证。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 涉及文件索引
|
||||||
|
|
||||||
|
| 文件 | 关键函数/逻辑 | 行号 |
|
||||||
|
| ---------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ------------------------- |
|
||||||
|
| `src/renderer/src/hooks/cleaner/api.ts` | `initializeCleanerPage()`, `runCleanerExecution()` | 25-52, 116-166 |
|
||||||
|
| `src/renderer/src/hooks/useCleaner.ts` | `handleConfirmDeletion()`, 初始化逻辑 | 98-120, 289-345 |
|
||||||
|
| `src/renderer/src/hooks/cleaner/helpers.ts` | `filterValidationResults()`, `buildDeletionPlan()` | 34-57, 59-92 |
|
||||||
|
| `src/main/services/validation/validation-application-service.ts` | `getCleanerData()`, `loadMaterialCodesForCleaner()`, `queryMaterialCodesByManagers()` | 232-305, 497-604, 606-655 |
|
||||||
|
| `src/main/services/cleaner/cleaner-application-service.ts` | `runCleaner()` | 31-168 |
|
||||||
|
| `src/main/ipc/cleaner-handler.ts` | `CLEANER_RUN` handler | 16-22 |
|
||||||
|
| `src/main/ipc/validation-handler.ts` | `getCleanerData` handler | 194-223 |
|
||||||
|
| `src/preload/api/validation.ts` | `getCleanerData()` IPC 桥接 | 11-12 |
|
||||||
|
| `src/renderer/src/pages/CleanerPage.tsx` | 页面组件,条件渲染侧边栏 | 74-82 |
|
||||||
143
docs/plans/2026-04-05-postgresql-integration-design.md
Normal file
143
docs/plans/2026-04-05-postgresql-integration-design.md
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
# PostgreSQL 集成设计文档
|
||||||
|
|
||||||
|
**日期:** 2026-04-05
|
||||||
|
**状态:** 已批准
|
||||||
|
**分支:** dev-logging
|
||||||
|
|
||||||
|
## 目标
|
||||||
|
|
||||||
|
将 PostgreSQL 作为第三种可选数据库类型集成到 ERPAuto 中,与现有 MySQL、SQL Server 并列。通过引入 SqlDialect 抽象层,统一管理三种数据库的 SQL 方言差异,同时重构现有 DAO 层消除散落的 `isSqlServer` 判断。
|
||||||
|
|
||||||
|
## 背景
|
||||||
|
|
||||||
|
- PostgreSQL 数据库已通过 SSMA 从 SQL Server 迁移完成,表结构、schema 组织、列名完全一致
|
||||||
|
- 连接信息:`postgresql://admin:***@192.168.31.83:5432/postgres`,数据库 `CompanyDB`
|
||||||
|
- 共 15 个 schema、151 张表,`dbo` schema 包含 ERPAuto 直接使用的表
|
||||||
|
|
||||||
|
## 方案:抽象数据库方言层
|
||||||
|
|
||||||
|
### 1. SqlDialect 接口
|
||||||
|
|
||||||
|
新建 `src/main/types/sql-dialect.types.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface SqlDialect {
|
||||||
|
readonly dbType: DatabaseType
|
||||||
|
|
||||||
|
// 表名引用
|
||||||
|
quoteTableName(schema: string, table: string): string
|
||||||
|
|
||||||
|
// 参数占位符
|
||||||
|
param(index: number): string
|
||||||
|
params(count: number): string
|
||||||
|
|
||||||
|
// SQL 函数
|
||||||
|
currentTimestamp(): string
|
||||||
|
|
||||||
|
// UPSERT
|
||||||
|
upsert(p: {
|
||||||
|
table: string
|
||||||
|
keyColumns: string[]
|
||||||
|
valueColumns: string[]
|
||||||
|
placeholderCount: number
|
||||||
|
startParamIndex: number
|
||||||
|
}): string
|
||||||
|
|
||||||
|
// 分页
|
||||||
|
paginate(p: { sql: string; limit: number; offset?: number; paramIndex: number }): {
|
||||||
|
sql: string
|
||||||
|
paramIndex: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量限制
|
||||||
|
maxBatchRows(columnsPerRow: number): number
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 三种方言实现
|
||||||
|
|
||||||
|
新建 `src/main/services/database/dialects/` 目录:
|
||||||
|
|
||||||
|
| 文件 | 数据库 | param(n) | quoteTableName | currentTimestamp | upsert | paginate |
|
||||||
|
| ----------------------- | ---------- | -------- | --------------- | ------------------- | ------------------ | ------------------ |
|
||||||
|
| `mysql-dialect.ts` | MySQL | `?` | `dbo_Table` | `NOW()` | `ON DUPLICATE KEY` | `LIMIT x OFFSET y` |
|
||||||
|
| `sqlserver-dialect.ts` | SQL Server | `@p{n}` | `[dbo].[Table]` | `GETDATE()` | `MERGE` | `OFFSET/FETCH` |
|
||||||
|
| `postgresql-dialect.ts` | PostgreSQL | `${n+1}` | `"dbo"."Table"` | `CURRENT_TIMESTAMP` | `ON CONFLICT` | `LIMIT x OFFSET y` |
|
||||||
|
|
||||||
|
方言工厂 `dialects/index.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function createDialect(type: DatabaseType): SqlDialect
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. DAO 层重构
|
||||||
|
|
||||||
|
每个 DAO 新增 `dialect` 成员,替代原有的 `getTableName()`、`buildPlaceholders()` 和所有 `isSqlServer` 分支:
|
||||||
|
|
||||||
|
**删除:**
|
||||||
|
|
||||||
|
- `getTableName()` 私有方法
|
||||||
|
- `buildPlaceholders()` 私有方法
|
||||||
|
- 所有 `isSqlServer` 局部变量和条件分支
|
||||||
|
- `*_CONFIG` 中的 `TABLE_NAME_SQLSERVER` / `TABLE_NAME_MYSQL` → 合并为 `TABLE_SCHEMA` + `TABLE_NAME`
|
||||||
|
|
||||||
|
**新增:**
|
||||||
|
|
||||||
|
- `private dialect: SqlDialect | null = null`
|
||||||
|
- `private getDialect(): SqlDialect`
|
||||||
|
|
||||||
|
**涉及 DAO:**
|
||||||
|
|
||||||
|
- `DiscreteMaterialPlanDAO` — 占位符、表名、批量大小
|
||||||
|
- `MaterialsToBeDeletedDAO` — 占位符、表名、MERGE/ON DUPLICATE KEY → `upsert()`
|
||||||
|
- `MaterialsTypeToBeDeletedDAO` — 同上
|
||||||
|
- `ExtractorOperationHistoryDAO` — 占位符、表名、GETDATE()/NOW() → `currentTimestamp()`、分页 → `paginate()`
|
||||||
|
|
||||||
|
### 4. PostgreSQL 服务层
|
||||||
|
|
||||||
|
新建 `src/main/services/database/postgresql.ts`:
|
||||||
|
|
||||||
|
- 使用 `pg` 驱动,`Pool` 连接池
|
||||||
|
- 实现 `IDatabaseService` 接口
|
||||||
|
- `query()` 直接传递参数数组给 `pg`
|
||||||
|
- `transaction()` 使用 `client.query('BEGIN/COMMIT/ROLLBACK')`
|
||||||
|
|
||||||
|
### 5. 工厂、配置、TypeORM
|
||||||
|
|
||||||
|
**database/index.ts:** `create()` 新增 `'postgresql'` 分支,新增 `createPostgreSqlConfig()`
|
||||||
|
|
||||||
|
**database.types.ts:** `DatabaseType` 扩展为 `'mysql' | 'sqlserver' | 'postgresql'`,新增 `PostgreSqlConfig`
|
||||||
|
|
||||||
|
**data-source.ts:** TypeORM `type` 映射新增 `'postgres'`
|
||||||
|
|
||||||
|
**config.template.yaml:** 新增 `postgresql` 配置段
|
||||||
|
|
||||||
|
**package.json:** 新增 `pg` 依赖
|
||||||
|
|
||||||
|
## 改动范围
|
||||||
|
|
||||||
|
| 层 | 文件 | 动作 |
|
||||||
|
| ------- | ---------------------------------------------- | ---- |
|
||||||
|
| 类型 | `types/database.types.ts` | 修改 |
|
||||||
|
| 方言 | `database/dialects/index.ts` | 新建 |
|
||||||
|
| 方言 | `database/dialects/mysql-dialect.ts` | 新建 |
|
||||||
|
| 方言 | `database/dialects/sqlserver-dialect.ts` | 新建 |
|
||||||
|
| 方言 | `database/dialects/postgresql-dialect.ts` | 新建 |
|
||||||
|
| 服务 | `database/postgresql.ts` | 新建 |
|
||||||
|
| 工厂 | `database/index.ts` | 修改 |
|
||||||
|
| TypeORM | `database/data-source.ts` | 修改 |
|
||||||
|
| DAO | `database/discrete-material-plan-dao.ts` | 重构 |
|
||||||
|
| DAO | `database/materials-to-be-deleted-dao.ts` | 重构 |
|
||||||
|
| DAO | `database/materials-type-to-be-deleted-dao.ts` | 重构 |
|
||||||
|
| DAO | `database/extractor-operation-history-dao.ts` | 重构 |
|
||||||
|
| 配置 | `config.template.yaml` | 修改 |
|
||||||
|
| 依赖 | `package.json` | 修改 |
|
||||||
|
|
||||||
|
共 **4 个新文件 + 10 个修改文件**。
|
||||||
|
|
||||||
|
## 不在范围内
|
||||||
|
|
||||||
|
- IPC 处理器新增(前端暂不需要直接切换 PostgreSQL)
|
||||||
|
- Entity/Repository 的 TypeScript 类型适配(TypeORM 内部处理方言差异)
|
||||||
|
- 数据迁移工具
|
||||||
|
- 前端 UI 变更
|
||||||
1341
docs/plans/2026-04-05-postgresql-integration-plan.md
Normal file
1341
docs/plans/2026-04-05-postgresql-integration-plan.md
Normal file
File diff suppressed because it is too large
Load Diff
235
docs/plans/2026-04-13-cleaner-db-persistence-design.md
Normal file
235
docs/plans/2026-04-13-cleaner-db-persistence-design.md
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
# Cleaner 数据库持久化设计
|
||||||
|
|
||||||
|
## 背景
|
||||||
|
|
||||||
|
Cleaner 当前使用 Markdown 文件做执行记录持久化,通过 RustFS 上传存储。存在以下问题:
|
||||||
|
|
||||||
|
- 报告是非结构化文本,无法程序化查询和统计
|
||||||
|
- 历史记录无法按用户、时间、状态筛选
|
||||||
|
- 重试时依赖文件名去重,覆盖了首次执行的崩溃信息
|
||||||
|
- 前端需要通过 RustFS 下载报告再解析展示,链路长且脆弱
|
||||||
|
|
||||||
|
Extractor 已有成熟的数据库持久化模式(`ExtractorOperationHistory` 表 + DAO + 前端弹窗),Cleaner 应复用相同模式。
|
||||||
|
|
||||||
|
## 设计决策
|
||||||
|
|
||||||
|
| 决策项 | 选择 | 理由 |
|
||||||
|
|--------|------|------|
|
||||||
|
| 表结构 | 独立建表,不与 Extractor 共用 | Cleaner 数据结构差异大(双层、物料级详情),独立更清晰 |
|
||||||
|
| 记录粒度 | 执行 + 订单 + 物料三层 | 执行表存全局信息,订单表存订单汇总,物料表存操作明细 |
|
||||||
|
| 批次标识 | `BatchId`(UUID),与 Extractor 一致 | 标准、简洁,不需要嵌入时间戳 |
|
||||||
|
| 重试记录 | 不覆盖,每次尝试独立写入,用 `AttemptNumber` 区分 | 保留完整审计链,为后续智能跳过提供数据基础 |
|
||||||
|
| 报告文件 | 移除 Markdown 报告和 RustFS 上传 | 数据库完全替代,报告相关代码(CleanerReportGenerator、generateAndUploadReport)删除 |
|
||||||
|
| 前端历史 | 独立 CleanerOperationHistoryModal,复用 Extractor 的 UI 模式 | 放在 CleanerPage 上,与 Extractor 的"操作历史"按钮对齐 |
|
||||||
|
|
||||||
|
## 数据库表结构
|
||||||
|
|
||||||
|
所有表的 schema 为 `ERPAuto`。
|
||||||
|
|
||||||
|
### 1. `CleanerExecution`(执行级)
|
||||||
|
|
||||||
|
全限定名:`ERPAuto.CleanerExecution`
|
||||||
|
|
||||||
|
一次清理操作(含重试)的全局信息。每次尝试一行记录。
|
||||||
|
|
||||||
|
| 列名 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| ID | INT IDENTITY | 自增主键 |
|
||||||
|
| BatchId | UNIQUEIDENTIFIER | 批次 ID,一次清理操作(含重试)共享 |
|
||||||
|
| AttemptNumber | INT | 第几次尝试(1=首次,2=外层重试) |
|
||||||
|
| UserId | INT | 操作用户 ID |
|
||||||
|
| Username | NVARCHAR(255) | 操作用户名 |
|
||||||
|
| OperationTime | DATETIME | 操作时间 |
|
||||||
|
| EndTime | DATETIME | 结束时间 |
|
||||||
|
| Status | NVARCHAR(50) | pending / success / failed / partial / crashed |
|
||||||
|
| IsDryRun | BIT | 是否模拟运行 |
|
||||||
|
| TotalOrders | INT | 订单总数 |
|
||||||
|
| OrdersProcessed | INT | 已处理订单数 |
|
||||||
|
| TotalMaterialsDeleted | INT | 总删除物料数 |
|
||||||
|
| TotalMaterialsSkipped | INT | 总跳过物料数 |
|
||||||
|
| TotalMaterialsFailed | INT | 总失败物料数 |
|
||||||
|
| TotalUncertainDeletions | INT | 总不确定删除数 |
|
||||||
|
| ErrorMessage | NVARCHAR(MAX) | 全局错误信息(如外层崩溃原因) |
|
||||||
|
| AppVersion | NVARCHAR(20) | 应用版本号 |
|
||||||
|
|
||||||
|
### 2. `CleanerOrderHistory`(订单级)
|
||||||
|
|
||||||
|
全限定名:`ERPAuto.CleanerOrderHistory`
|
||||||
|
|
||||||
|
每个订单在每次尝试中的执行结果。每个订单每次尝试一行记录。
|
||||||
|
|
||||||
|
| 列名 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| ID | INT IDENTITY | 自增主键 |
|
||||||
|
| BatchId | UNIQUEIDENTIFIER | 关联执行表 BatchId |
|
||||||
|
| AttemptNumber | INT | 关联执行表 AttemptNumber |
|
||||||
|
| OrderNumber | NVARCHAR(255) | 订单号 |
|
||||||
|
| Status | NVARCHAR(50) | pending / success / failed |
|
||||||
|
| MaterialsDeleted | INT | 删除物料数 |
|
||||||
|
| MaterialsSkipped | INT | 跳过物料数 |
|
||||||
|
| MaterialsFailed | INT | 删除失败物料数 |
|
||||||
|
| UncertainDeletions | INT | 不确定删除数 |
|
||||||
|
| RetryCount | INT | 内层重试次数 |
|
||||||
|
| RetrySuccess | BIT | 内层重试是否成功 |
|
||||||
|
| ErrorMessage | NVARCHAR(MAX) | 错误信息 |
|
||||||
|
|
||||||
|
关联方式:`BatchId + AttemptNumber` 关联执行表。
|
||||||
|
|
||||||
|
### 3. `CleanerMaterialDetail`(物料级)
|
||||||
|
|
||||||
|
全限定名:`ERPAuto.CleanerMaterialDetail`
|
||||||
|
|
||||||
|
每个物料在每次尝试中的操作明细。
|
||||||
|
|
||||||
|
| 列名 | 类型 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| ID | INT IDENTITY | 自增主键 |
|
||||||
|
| BatchId | UNIQUEIDENTIFIER | 关联执行表 BatchId |
|
||||||
|
| AttemptNumber | INT | 关联执行表 AttemptNumber |
|
||||||
|
| OrderNumber | NVARCHAR(255) | 所属订单号 |
|
||||||
|
| MaterialCode | NVARCHAR(255) | 物料代码 |
|
||||||
|
| MaterialName | NVARCHAR(255) | 物料名称 |
|
||||||
|
| RowNumber | INT | 行号 |
|
||||||
|
| Result | NVARCHAR(50) | deleted / skipped / failed / uncertain |
|
||||||
|
| Reason | NVARCHAR(MAX) | 跳过/失败原因 |
|
||||||
|
| AttemptCount | INT | 删除尝试次数 |
|
||||||
|
| FinalErrorCategory | NVARCHAR(50) | 最终错误分类 |
|
||||||
|
|
||||||
|
关联方式:`BatchId + AttemptNumber + OrderNumber` 关联订单表。
|
||||||
|
|
||||||
|
### 数据示例
|
||||||
|
|
||||||
|
首次执行到第 80 个订单时崩溃,外层重试成功完成全部 211 个订单:
|
||||||
|
|
||||||
|
**CleanerExecution**
|
||||||
|
```
|
||||||
|
BatchId=uuid-1, Attempt=1, Status=crashed, TotalOrders=211, Processed=80, ...
|
||||||
|
BatchId=uuid-1, Attempt=2, Status=success, TotalOrders=211, Processed=211, ...
|
||||||
|
```
|
||||||
|
|
||||||
|
**CleanerOrderHistory**(Attempt=1 中部分记录)
|
||||||
|
```
|
||||||
|
BatchId=uuid-1, Attempt=1, Order=SC001, Status=success, Deleted=5, Skipped=1
|
||||||
|
BatchId=uuid-1, Attempt=1, Order=SC080, Status=crashed, Error=查询超时
|
||||||
|
```
|
||||||
|
|
||||||
|
**CleanerOrderHistory**(Attempt=2 中部分记录)
|
||||||
|
```
|
||||||
|
BatchId=uuid-1, Attempt=2, Order=SC001, Status=success, Deleted=5, Skipped=1
|
||||||
|
BatchId=uuid-1, Attempt=2, Order=SC080, Status=success, Deleted=3, Skipped=0
|
||||||
|
BatchId=uuid-1, Attempt=2, Order=SC211, Status=success, Deleted=2, Skipped=0
|
||||||
|
```
|
||||||
|
|
||||||
|
**CleanerMaterialDetail**(SC080 在 Attempt=2 中的物料)
|
||||||
|
```
|
||||||
|
BatchId=uuid-1, Attempt=2, Order=SC080, Material=MAT-001, Result=deleted
|
||||||
|
BatchId=uuid-1, Attempt=2, Order=SC080, Material=MAT-002, Result=skipped, Reason=不可删除
|
||||||
|
```
|
||||||
|
|
||||||
|
## 写入时机
|
||||||
|
|
||||||
|
```
|
||||||
|
用户点击"执行清理"
|
||||||
|
→ IPC: cleaner:run
|
||||||
|
→ cleaner-handler.ts
|
||||||
|
→ ① BatchId = randomUUID()
|
||||||
|
→ ② 插入 CleanerExecution(Status=pending)
|
||||||
|
→ ③ 插入 CleanerOrderHistory(所有订单,Status=pending)
|
||||||
|
→ ④ 执行清理(CleanerApplicationService.runCleaner)
|
||||||
|
→ ⑤ 更新 CleanerExecution(Status=success/failed/partial/crashed)
|
||||||
|
→ ⑥ 更新 CleanerOrderHistory(每个订单的结果)
|
||||||
|
→ ⑦ 插入 CleanerMaterialDetail(每个物料的操作明细)
|
||||||
|
→ ⑧ 如果 crashed → 外层重试
|
||||||
|
→ 插入新的 CleanerExecution(AttemptNumber=2, Status=pending)
|
||||||
|
→ 插入新的 CleanerOrderHistory(AttemptNumber=2, Status=pending)
|
||||||
|
→ 重新执行
|
||||||
|
→ 更新执行表和订单表状态
|
||||||
|
→ 插入物料明细
|
||||||
|
```
|
||||||
|
|
||||||
|
- 步骤 ②③:在 `cleaner-handler.ts` 中,执行前写入,记录操作人、全局配置、待处理订单
|
||||||
|
- 步骤 ⑤⑥⑦:在 `CleanerApplicationService` 中,执行完成后回调 DAO 写入结果
|
||||||
|
- 步骤 ⑧:外层重试时,三张表都新增 AttemptNumber=2 的记录,首次尝试的数据完整保留
|
||||||
|
|
||||||
|
## 变更清单
|
||||||
|
|
||||||
|
### 新增文件
|
||||||
|
|
||||||
|
1. **`src/main/services/database/cleaner-operation-history-dao.ts`**
|
||||||
|
- `CleanerOperationHistoryDAO` 类
|
||||||
|
- 执行表操作:insertExecution、updateExecutionStatus
|
||||||
|
- 订单表操作:insertOrderRecords、updateOrderStatus
|
||||||
|
- 物料表操作:insertMaterialDetails
|
||||||
|
- 查询操作:getBatches、getBatchDetails(含订单+物料)、deleteBatch
|
||||||
|
- 参考 `ExtractorOperationHistoryDAO` 的模式,表名使用 `ERPAuto.CleanerExecution`、`ERPAuto.CleanerOrderHistory`、`ERPAuto.CleanerMaterialDetail`
|
||||||
|
|
||||||
|
2. **`src/main/types/cleaner-history.types.ts`**
|
||||||
|
- `CleanerExecutionRecord`、`CleanerOrderRecord`、`CleanerMaterialRecord`
|
||||||
|
- `CleanerBatchStats`、`InsertCleanerExecutionInput`、`InsertOrderInput`、`InsertMaterialDetailInput`
|
||||||
|
|
||||||
|
3. **`src/renderer/src/components/CleanerOperationHistoryModal.tsx`**
|
||||||
|
- 操作历史弹窗,复用 ExtractorOperationHistoryModal 的 UI 模式
|
||||||
|
- 批次列表(按 BatchId 聚合,显示操作时间、用户、状态、成功/失败数,区分多次尝试)
|
||||||
|
- 展开明细(订单列表,每订单的删除/跳过/失败数)
|
||||||
|
- 物料级详情(第二层展开,显示每个物料的操作结果)
|
||||||
|
- 管理员可按用户筛选、可删除批次
|
||||||
|
|
||||||
|
### 修改文件
|
||||||
|
|
||||||
|
4. **`src/main/ipc/cleaner-handler.ts`**
|
||||||
|
- `CLEANER_RUN` handler 中:执行前插入 execution + order 的 pending 记录,执行后更新结果
|
||||||
|
- 新增 IPC handlers:`CLEANER_HISTORY_BATCHES`、`CLEANER_HISTORY_DETAILS`、`CLEANER_HISTORY_DELETE`
|
||||||
|
|
||||||
|
5. **`src/main/services/cleaner/cleaner-application-service.ts`**
|
||||||
|
- `runCleaner` 接收 `batchId` 参数
|
||||||
|
- 移除 `generateExecutionId()` 函数
|
||||||
|
- 移除 `generateAndUploadReport()` 方法
|
||||||
|
- 移除 `executionId` 相关逻辑
|
||||||
|
- 外层重试时,通过 DAO 写入 AttemptNumber=2 的执行记录和订单记录,不覆盖首次尝试
|
||||||
|
- 执行完成后回调 DAO 写入订单结果和物料明细
|
||||||
|
|
||||||
|
6. **`src/main/ipc/index.ts`**
|
||||||
|
- 注册新的 cleaner history IPC handlers
|
||||||
|
|
||||||
|
7. **`src/preload/api/cleaner.ts`**
|
||||||
|
- 新增 IPC 调用方法:getBatches、getBatchDetails、deleteBatch
|
||||||
|
|
||||||
|
8. **`src/preload/index.d.ts`**
|
||||||
|
- `CleanerAPI` 接口新增 getBatches、getBatchDetails、deleteBatch 类型声明
|
||||||
|
|
||||||
|
9. **`src/renderer/src/pages/CleanerPage.tsx`**
|
||||||
|
- 新增"操作历史"按钮
|
||||||
|
- 引入 CleanerOperationHistoryModal
|
||||||
|
|
||||||
|
### 删除文件
|
||||||
|
|
||||||
|
10. **`src/main/services/report/cleaner-report-generator.ts`**
|
||||||
|
- 整个文件删除,报告生成逻辑不再需要
|
||||||
|
|
||||||
|
### 可选清理
|
||||||
|
|
||||||
|
11. **`src/renderer/src/components/ReportViewerDialog.tsx`**
|
||||||
|
- 基于 RustFS 文件的报告查看器,Cleaner 不再使用
|
||||||
|
- 如果 Extractor 不共用此组件,可删除
|
||||||
|
|
||||||
|
12. **`src/renderer/src/components/ReportAnalysisDialog.tsx`**
|
||||||
|
- 基于报告文件的分析,Cleaner 不再使用
|
||||||
|
- 后续可基于数据库重新实现统计分析
|
||||||
|
|
||||||
|
## 移除的概念
|
||||||
|
|
||||||
|
| 概念 | 原因 |
|
||||||
|
|------|------|
|
||||||
|
| ExecutionId(CLN-时间戳-随机) | 为文件名设计,数据库用 UUID |
|
||||||
|
| generateExecutionId() | 随 ExecutionId 一起移除 |
|
||||||
|
| CleanerReportGenerator | Markdown 报告生成器,被数据库替代 |
|
||||||
|
| generateAndUploadReport() | RustFS 上传链路,被数据库写入替代 |
|
||||||
|
| 报告文件名去重 | 数据库 UUID 天然唯一 |
|
||||||
|
| 重试覆盖旧报告 | 数据库保留所有尝试记录 |
|
||||||
|
|
||||||
|
## 不涉及的部分
|
||||||
|
|
||||||
|
- Extractor 的持久化逻辑不变
|
||||||
|
- 数据库 schema 迁移(需 DBA 创建表,应用层只做 CRUD)
|
||||||
|
- 后续智能跳过功能(基于已有 success 记录跳过已成功的订单)
|
||||||
|
- 内层重试逻辑(订单级/物料级)不变
|
||||||
620
docs/plans/2026-04-13-cleaner-db-persistence-plan.md
Normal file
620
docs/plans/2026-04-13-cleaner-db-persistence-plan.md
Normal file
@@ -0,0 +1,620 @@
|
|||||||
|
# Cleaner 数据库持久化实施计划
|
||||||
|
|
||||||
|
> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
|
||||||
|
|
||||||
|
**Goal:** 将 Cleaner 的执行记录从 Markdown 文件持久化迁移到数据库(三张表:执行级、订单级、物料级),并在前端新增操作历史弹窗。
|
||||||
|
|
||||||
|
**Architecture:** 新建 `CleanerOperationHistoryDAO` 操作三张表(`ERPAuto.CleanerExecution`、`ERPAuto.CleanerOrderHistory`、`ERPAuto.CleanerMaterialDetail`),通过新增 IPC handlers 暴露给前端。执行前写入 pending 记录,执行后更新结果和物料明细。外层重试时新增 AttemptNumber=2 的记录,不覆盖首次尝试。移除 Markdown 报告生成和 RustFS 上传链路。
|
||||||
|
|
||||||
|
**Tech Stack:** TypeScript, Electron IPC, SQL (MySQL/SQL Server/PostgreSQL via existing DAO+dialect pattern), React
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1: 新增类型定义
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/main/types/cleaner-history.types.ts`
|
||||||
|
|
||||||
|
**Step 1: 创建类型文件**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// src/main/types/cleaner-history.types.ts
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleaner 操作历史类型定义
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 执行级记录 */
|
||||||
|
export interface CleanerExecutionRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: Date
|
||||||
|
endTime: Date | null
|
||||||
|
status: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsSkipped: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
totalUncertainDeletions: number
|
||||||
|
errorMessage: string | null
|
||||||
|
appVersion: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 订单级记录 */
|
||||||
|
export interface CleanerOrderRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
status: string
|
||||||
|
materialsDeleted: number
|
||||||
|
materialsSkipped: number
|
||||||
|
materialsFailed: number
|
||||||
|
uncertainDeletions: number
|
||||||
|
retryCount: number
|
||||||
|
retrySuccess: boolean
|
||||||
|
errorMessage: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 物料级记录 */
|
||||||
|
export interface CleanerMaterialRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 批次统计(前端列表展示用) */
|
||||||
|
export interface CleanerBatchStats {
|
||||||
|
batchId: string
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string
|
||||||
|
/** 最终一次尝试的状态 */
|
||||||
|
status: string
|
||||||
|
totalAttempts: number
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
successCount: number
|
||||||
|
failedCount: number
|
||||||
|
isDryRun: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入执行记录的输入 */
|
||||||
|
export interface InsertCleanerExecutionInput {
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
appVersion: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入订单记录的输入 */
|
||||||
|
export interface InsertOrderInput {
|
||||||
|
orderNumber: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入物料明细的输入 */
|
||||||
|
export interface InsertMaterialDetailInput {
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 查询批次的选项 */
|
||||||
|
export interface GetCleanerBatchesOptions {
|
||||||
|
limit?: number
|
||||||
|
offset?: number
|
||||||
|
usernames?: string[]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS(新文件不影响现有代码)
|
||||||
|
|
||||||
|
**Step 3: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add type definitions for cleaner operation history
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2: 新增 DAO 层
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/main/services/database/cleaner-operation-history-dao.ts`
|
||||||
|
|
||||||
|
**Step 1: 创建 DAO 文件**
|
||||||
|
|
||||||
|
参考 `extractor-operation-history-dao.ts` 的模式(`create()` 获取数据库连接、`createDialect()` 处理 SQL 方言、`trackDuration()` 记录耗时)。表名使用 `ERPAuto` schema。
|
||||||
|
|
||||||
|
关键方法:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export class CleanerOperationHistoryDAO {
|
||||||
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
// ===== 执行表 =====
|
||||||
|
private getExecutionTableName(): string {
|
||||||
|
return this.getDialect().quoteTableName('ERPAuto', 'CleanerExecution')
|
||||||
|
}
|
||||||
|
|
||||||
|
async insertExecution(input: InsertCleanerExecutionInput): Promise<boolean>
|
||||||
|
async updateExecutionStatus(batchId: string, attemptNumber: number, status: string, ordersProcessed: number, materialsDeleted: number, materialsSkipped: number, materialsFailed: number, uncertainDeletions: number, endTime: Date, errorMessage?: string): Promise<boolean>
|
||||||
|
|
||||||
|
// ===== 订单表 =====
|
||||||
|
private getOrderTableName(): string {
|
||||||
|
return this.getDialect().quoteTableName('ERPAuto', 'CleanerOrderHistory')
|
||||||
|
}
|
||||||
|
|
||||||
|
async insertOrderRecords(batchId: string, attemptNumber: number, orders: InsertOrderInput[]): Promise<boolean>
|
||||||
|
async updateOrderStatus(batchId: string, attemptNumber: number, orderNumber: string, status: string, materialsDeleted: number, materialsSkipped: number, materialsFailed: number, uncertainDeletions: number, retryCount: number, retrySuccess: boolean, errorMessage?: string): Promise<boolean>
|
||||||
|
|
||||||
|
// ===== 物料表 =====
|
||||||
|
private getMaterialTableName(): string {
|
||||||
|
return this.getDialect().quoteTableName('ERPAuto', 'CleanerMaterialDetail')
|
||||||
|
}
|
||||||
|
|
||||||
|
async insertMaterialDetails(batchId: string, attemptNumber: number, details: InsertMaterialDetailInput[]): Promise<boolean>
|
||||||
|
|
||||||
|
// ===== 查询 =====
|
||||||
|
async getBatches(userId?: number, options?: GetCleanerBatchesOptions): Promise<CleanerBatchStats[]>
|
||||||
|
async getBatchDetails(batchId: string): Promise<{ executions: CleanerExecutionRecord[]; orders: CleanerOrderRecord[] }>
|
||||||
|
async getMaterialDetails(batchId: string, attemptNumber: number, orderNumber: string): Promise<CleanerMaterialRecord[]>
|
||||||
|
|
||||||
|
// ===== 删除 =====
|
||||||
|
async deleteBatch(batchId: string, requestingUserId: number, isAdmin: boolean): Promise<{ success: boolean; error?: string }>
|
||||||
|
|
||||||
|
// ===== 列询执行级记录 =====
|
||||||
|
async getMaterialDetails(batchId: string, attemptNumber: number, orderNumber: string): Promise<CleanerMaterialRecord[]>
|
||||||
|
|
||||||
|
// ===== 删除 =====
|
||||||
|
async deleteBatch(batchId: string, requestingUserId: number, isAdmin: boolean): Promise<{ success: boolean; error?: string }>
|
||||||
|
async disconnect(): Promise<void>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`getBatches` 查询逻辑:
|
||||||
|
- `GROUP BY BatchId`,取 `MAX(AttemptNumber)` 对应的执行记录状态作为最终状态
|
||||||
|
- 汇总订单级的 success/failed 计数
|
||||||
|
- 支持 userId 过滤(普通用户)和 usernames 过滤(管理员)
|
||||||
|
- 支持分页
|
||||||
|
|
||||||
|
`getBatchDetails` 查询逻辑:
|
||||||
|
- 返回某 BatchId 下所有 execution 记录 + order 记录
|
||||||
|
- 前端用 attemptNumber 区分不同尝试
|
||||||
|
|
||||||
|
每个 INSERT/UPDATE 使用 `trackDuration()` 包裹,error handling 与 Extractor DAO 一致。
|
||||||
|
|
||||||
|
**Step 2: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 3: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add CleanerOperationHistoryDAO for three-table persistence
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3: 新增 IPC channels
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/shared/ipc-channels.ts`
|
||||||
|
|
||||||
|
**Step 1: 添加 cleaner history channels**
|
||||||
|
|
||||||
|
在现有的 `CLEANER_PROGRESS` 之后添加:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Cleaner history
|
||||||
|
CLEANER_HISTORY_GET_BATCHES: 'cleanerHistory:getBatches',
|
||||||
|
CLEANER_HISTORY_GET_BATCH_DETAILS: 'cleanerHistory:getBatchDetails',
|
||||||
|
CLEANER_HISTORY_GET_MATERIAL_DETAILS: 'cleanerHistory:getMaterialDetails',
|
||||||
|
CLEANER_HISTORY_DELETE_BATCH: 'cleanerHistory:deleteBatch',
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add IPC channels for cleaner operation history
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4: 新增 IPC handler
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/main/ipc/cleaner-history-handler.ts`
|
||||||
|
- Modify: `src/main/ipc/index.ts` — 注册新 handler
|
||||||
|
|
||||||
|
**Step 1: 创建 cleaner-history-handler.ts**
|
||||||
|
|
||||||
|
参考 `operation-history-handler.ts` 的模式。四个 handler:
|
||||||
|
|
||||||
|
- `CLEANER_HISTORY_GET_BATCHES`:获取批次列表,Admin 看全部,User 看自己的
|
||||||
|
- `CLEANER_HISTORY_GET_BATCH_DETAILS`:获取某个批次的执行记录和订单记录
|
||||||
|
- `CLEANER_HISTORY_GET_MATERIAL_DETAILS`:获取某个订单的物料明细
|
||||||
|
- `CLEANER_HISTORY_DELETE_BATCH`:删除批次,权限校验与 Extractor 一致
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function registerCleanerHistoryHandlers(): void {
|
||||||
|
const dao = new CleanerOperationHistoryDAO()
|
||||||
|
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_BATCHES,
|
||||||
|
async (event, options?: GetCleanerBatchesOptions): Promise<IpcResult<CleanerBatchStats[]>> => {
|
||||||
|
return withErrorHandling(async () => {
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
if (!currentUser) throw new Error('用户未登录')
|
||||||
|
const userId = currentUser.userType === 'Admin' ? undefined : currentUser.id
|
||||||
|
return dao.getBatches(userId, options)
|
||||||
|
}, 'cleanerHistory:getBatches')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_BATCH_DETAILS,
|
||||||
|
async (event, batchId: string): Promise<IpcResult<{ executions: CleanerExecutionRecord[]; orders: CleanerOrderRecord[] }>> => {
|
||||||
|
// ... 与 operation-history-handler 的 getBatchDetails 模式一致
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_MATERIAL_DETAILS,
|
||||||
|
async (event, batchId: string, attemptNumber: number, orderNumber: string): Promise<IpcResult<CleanerMaterialRecord[]>> => {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_DELETE_BATCH,
|
||||||
|
async (event, batchId: string): Promise<IpcResult<{ deleted: boolean }>> => {
|
||||||
|
// ... 权限校验后删除三张表的记录
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: 在 index.ts 中注册**
|
||||||
|
|
||||||
|
在 `registerIpcHandlers()` 中添加 `registerCleanerHistoryHandlers()` 调用,并在顶部添加 import。
|
||||||
|
|
||||||
|
**Step 3: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add IPC handlers for cleaner operation history
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5: 新增 Preload API
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/preload/api/cleaner.ts` — 新增 history 方法
|
||||||
|
- Modify: `src/preload/index.d.ts` — 新增类型声明
|
||||||
|
|
||||||
|
**Step 1: 在 cleaner.ts 中新增 history 方法**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import type {
|
||||||
|
CleanerBatchStats,
|
||||||
|
CleanerExecutionRecord,
|
||||||
|
CleanerOrderRecord,
|
||||||
|
CleanerMaterialRecord,
|
||||||
|
GetCleanerBatchesOptions
|
||||||
|
} from '../../main/types/cleaner-history.types'
|
||||||
|
|
||||||
|
// 在 cleanerApi 对象中追加:
|
||||||
|
getHistoryBatches: (options?: GetCleanerBatchesOptions): Promise<IpcResult<CleanerBatchStats[]>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_GET_BATCHES, options),
|
||||||
|
|
||||||
|
getHistoryBatchDetails: (batchId: string): Promise<IpcResult<{
|
||||||
|
executions: CleanerExecutionRecord[]
|
||||||
|
orders: CleanerOrderRecord[]
|
||||||
|
}>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_GET_BATCH_DETAILS, batchId),
|
||||||
|
|
||||||
|
getHistoryMaterialDetails: (batchId: string, attemptNumber: number, orderNumber: string): Promise<IpcResult<CleanerMaterialRecord[]>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_GET_MATERIAL_DETAILS, batchId, attemptNumber, orderNumber),
|
||||||
|
|
||||||
|
deleteHistoryBatch: (batchId: string): Promise<IpcResult<{ deleted: boolean }>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_DELETE_BATCH, batchId),
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: 在 index.d.ts 中更新 CleanerAPI 接口**
|
||||||
|
|
||||||
|
在 `CleanerAPI` 接口中添加对应的类型声明,与实际 API 对齐。
|
||||||
|
|
||||||
|
**Step 3: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add preload API for cleaner operation history
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 6: 改造 CleanerApplicationService — 写入数据库记录
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/main/services/cleaner/cleaner-application-service.ts`
|
||||||
|
|
||||||
|
这是核心变更。`runCleaner` 方法需要:
|
||||||
|
|
||||||
|
**Step 1: 修改 runCleaner 签名,接收 batchId 和 DAO**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async runCleaner(
|
||||||
|
eventSender: WebContents,
|
||||||
|
input: CleanerInput,
|
||||||
|
batchId: string,
|
||||||
|
historyDao: CleanerOperationHistoryDAO
|
||||||
|
): Promise<CleanerResult>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: 移除报告相关代码**
|
||||||
|
|
||||||
|
- 删除 `import { app } from 'electron'`(仅用于 `app.getVersion()`)
|
||||||
|
- 删除 `generateExecutionId()` 函数
|
||||||
|
- 删除 `generateAndUploadReport()` 方法
|
||||||
|
- 删除所有 `executionId` 相关变量和日志
|
||||||
|
|
||||||
|
**Step 3: 插入 pending 订单记录**
|
||||||
|
|
||||||
|
在登录成功后、执行清理前,调用 `historyDao.insertOrderRecords(batchId, 1, orders)` 写入 pending 状态的订单记录。
|
||||||
|
|
||||||
|
**Step 4: 执行后更新订单记录和写入物料明细**
|
||||||
|
|
||||||
|
清理完成后遍历 `result.details`(`OrderCleanDetail[]`),对每个订单:
|
||||||
|
- 调用 `historyDao.updateOrderStatus(...)` 更新订单结果
|
||||||
|
- 调用 `historyDao.insertMaterialDetails(...)` 写入物料明细(skipped + failed 材料全部写入)
|
||||||
|
|
||||||
|
**Step 5: 更新执行记录状态**
|
||||||
|
|
||||||
|
调用 `historyDao.updateExecutionStatus(batchId, 1, ...)` 更新为最终状态。
|
||||||
|
|
||||||
|
**Step 6: 外层重试改造**
|
||||||
|
|
||||||
|
当 `result.crashed` 时:
|
||||||
|
1. 调用 `historyDao.updateExecutionStatus(batchId, 1, 'crashed', ...)` 标记首次尝试为 crashed
|
||||||
|
2. 调用 `historyDao.insertExecution({ batchId, attemptNumber: 2, ... })` 创建第二次尝试
|
||||||
|
3. 调用 `historyDao.insertOrderRecords(batchId, 2, orders)` 写入第二次尝试的 pending 订单
|
||||||
|
4. 重新登录并执行
|
||||||
|
5. 执行后更新 AttemptNumber=2 的订单和物料记录
|
||||||
|
|
||||||
|
**Step 7: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 8: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
refactor(cleaner): replace report generation with database persistence
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 7: 改造 cleaner-handler.ts — 执行前后写入
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/main/ipc/cleaner-handler.ts`
|
||||||
|
|
||||||
|
**Step 1: 修改 CLEANER_RUN handler**
|
||||||
|
|
||||||
|
在调用 `cleanerService.runCleaner()` 之前:
|
||||||
|
1. 获取当前用户信息
|
||||||
|
2. `batchId = randomUUID()`
|
||||||
|
3. 创建 `CleanerOperationHistoryDAO` 实例
|
||||||
|
4. 调用 `dao.insertExecution({ batchId, attemptNumber: 1, userId, username, isDryRun, totalOrders, appVersion })`
|
||||||
|
|
||||||
|
将 `batchId` 和 `dao` 传入 `runCleaner()`。
|
||||||
|
|
||||||
|
执行完成后(无论成功失败),更新执行记录的最终状态。
|
||||||
|
|
||||||
|
**Step 2: 移除 app.getVersion() 调用**
|
||||||
|
|
||||||
|
`appVersion` 改为在 handler 层获取(因为 handler 已有 electron 访问权限),传给 DAO。
|
||||||
|
|
||||||
|
**Step 3: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
refactor(cleaner): write execution records to database in IPC handler
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 8: 删除 Markdown 报告生成器
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Delete: `src/main/services/report/cleaner-report-generator.ts`
|
||||||
|
|
||||||
|
**Step 1: 删除文件**
|
||||||
|
|
||||||
|
删除 `cleaner-report-generator.ts`。
|
||||||
|
|
||||||
|
**Step 2: 检查是否有其他文件引用它**
|
||||||
|
|
||||||
|
搜索 `cleaner-report-generator` 或 `CleanerReportGenerator`,如有引用则一并移除(主要是 `cleaner-application-service.ts` 中已删除的 import)。
|
||||||
|
|
||||||
|
**Step 3: 验证编译通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
refactor(cleaner): remove Markdown report generator
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 9: 前端 — 新增操作历史弹窗
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/renderer/src/components/CleanerOperationHistoryModal.tsx`
|
||||||
|
- Modify: `src/renderer/src/pages/CleanerPage.tsx`
|
||||||
|
|
||||||
|
**Step 1: 创建 CleanerOperationHistoryModal**
|
||||||
|
|
||||||
|
参考 `ExtractorOperationHistoryModal.tsx` 的 UI 模式和代码结构。关键差异:
|
||||||
|
|
||||||
|
- 数据源使用 `window.electron.cleaner.getHistoryBatches()` 等新 API
|
||||||
|
- 批次列表增加"尝试次数"列和"模拟运行"标识
|
||||||
|
- 展开明细时,顶部显示执行级信息(尝试次数、crashed 状态等)
|
||||||
|
- 订单表格增加 deleted/skipped/failed/uncertain 列
|
||||||
|
- 订单行可再次展开查看物料明细(调用 `getHistoryMaterialDetails`)
|
||||||
|
- 管理员按用户筛选、删除功能与 Extractor 一致
|
||||||
|
|
||||||
|
**Step 2: 在 CleanerPage 中添加"操作历史"按钮和弹窗**
|
||||||
|
|
||||||
|
- 在 `CleanerToolbar` 中添加"操作历史"按钮(或直接在 CleanerPage 添加)
|
||||||
|
- 引入 `CleanerOperationHistoryModal` 组件
|
||||||
|
- 传入 `user` 和 `isOpen/onClose` 控制
|
||||||
|
|
||||||
|
**Step 3: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add operation history modal with database-backed records
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 10: 更新 renderer 类型定义
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/renderer/src/hooks/cleaner/types.ts`
|
||||||
|
|
||||||
|
**Step 1: 添加 history 相关类型**
|
||||||
|
|
||||||
|
在 types.ts 中添加前端需要的类型(或直接从 `cleaner-history.types.ts` import,根据项目的前端类型引用模式决定)。
|
||||||
|
|
||||||
|
**Step 2: 验证类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 3: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
feat(cleaner): add renderer types for cleaner operation history
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 11: 清理旧代码
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/renderer/src/hooks/cleaner/types.ts` — 移除 `CleanerReportData.crashed`(如果不再需要)
|
||||||
|
- 检查 `ReportViewerDialog.tsx`、`ReportAnalysisDialog.tsx` 是否仍被 Cleaner 使用
|
||||||
|
|
||||||
|
**Step 1: 清理 renderer 中不再需要的类型**
|
||||||
|
|
||||||
|
- `CleanerReportData` 中如果 `crashed` 字段已无用,移除
|
||||||
|
- 确认 `CleanerPhase` 的 `'retry'` 值是否仍需要(前端进度通知仍在使用,保留)
|
||||||
|
|
||||||
|
**Step 2: 评估 ReportViewerDialog 和 ReportAnalysisDialog**
|
||||||
|
|
||||||
|
这两个组件目前用于查看 Markdown 报告文件。如果 Cleaner 不再使用它们:
|
||||||
|
- 在 CleanerPage 中移除相关按钮和引用
|
||||||
|
- 不删除组件本身(Extractor 可能仍在使用,后续统一清理)
|
||||||
|
|
||||||
|
**Step 3: 验证编译和类型检查通过**
|
||||||
|
|
||||||
|
Run: `npm run typecheck && npm run lint`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: Commit**
|
||||||
|
|
||||||
|
```
|
||||||
|
chore(cleaner): clean up legacy report-related code
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 12: 集成测试
|
||||||
|
|
||||||
|
**Step 1: 运行完整类型检查**
|
||||||
|
|
||||||
|
Run: `npm run typecheck`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 2: 运行 lint**
|
||||||
|
|
||||||
|
Run: `npm run lint`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 3: 运行单元测试**
|
||||||
|
|
||||||
|
Run: `npm run test`
|
||||||
|
Expected: PASS
|
||||||
|
|
||||||
|
**Step 4: 手动验证**
|
||||||
|
|
||||||
|
1. 启动 `npm run dev`
|
||||||
|
2. 在 Cleaner 页面执行一次清理(模拟运行)
|
||||||
|
3. 检查数据库三张表是否正确写入
|
||||||
|
4. 点击"操作历史"按钮,验证批次列表和详情展示
|
||||||
|
5. 模拟崩溃场景(如果可以),验证外层重试写入 AttemptNumber=2 的记录
|
||||||
|
6. 用管理员账号验证用户筛选和删除功能
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 执行顺序
|
||||||
|
|
||||||
|
```
|
||||||
|
Task 1 (types) → Task 2 (DAO) → Task 3 (IPC channels) → Task 4 (IPC handler)
|
||||||
|
→ Task 5 (preload) → Task 6 (CleanerApplicationService) → Task 7 (cleaner-handler)
|
||||||
|
→ Task 8 (删除报告生成器) → Task 10 (renderer types) → Task 9 (前端弹窗)
|
||||||
|
→ Task 11 (清理) → Task 12 (集成测试)
|
||||||
|
```
|
||||||
|
|
||||||
|
Task 9 和 Task 10 可以并行。Task 8 必须在 Task 6、7 之后。
|
||||||
151
docs/plans/2026-04-13-cleaner-outer-retry-design.md
Normal file
151
docs/plans/2026-04-13-cleaner-outer-retry-design.md
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
# Cleaner 外层重试机制设计
|
||||||
|
|
||||||
|
## 背景
|
||||||
|
|
||||||
|
当 CleanerService.performCleanup 的主循环抛出未捕获异常时(如查询超时、浏览器崩溃),代码进入 outer catch 块,直接返回 partial result。位于 try 块后半段的订单级重试逻辑(retryFailedOrders)永远没有机会执行。
|
||||||
|
|
||||||
|
典型场景:211 个订单中处理到第 80 个时,查询列表页等待表格行超时 → Cleaner failed → 浏览器被关闭 → 剩余 131 个订单未处理 → 无重试。
|
||||||
|
|
||||||
|
## 设计决策
|
||||||
|
|
||||||
|
| 决策项 | 选择 | 理由 |
|
||||||
|
|---|---|---|
|
||||||
|
| 重试层级 | CleanerApplicationService | 崩溃后浏览器不可用,必须重新登录 |
|
||||||
|
| 重试范围 | 全部订单重新跑 | 简单可靠,物料删除是幂等操作 |
|
||||||
|
| 最大重试次数 | 1 次 | 覆盖瞬态故障,不过度消耗时间 |
|
||||||
|
| 触发条件 | result.crashed === true | 仅 outer catch 触发时才重试 |
|
||||||
|
| 报告去重 | 执行 ID | 用户点击执行时生成,重试不变 |
|
||||||
|
|
||||||
|
## 变更清单
|
||||||
|
|
||||||
|
### 1. CleanerResult 新增字段
|
||||||
|
|
||||||
|
**文件**: `src/main/types/cleaner.types.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface CleanerResult {
|
||||||
|
// ... 现有字段
|
||||||
|
crashed?: boolean // true = outer catch triggered, 流程级崩溃
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
同步更新 `src/shared/types/cleaner.types.ts`(如有独立定义)和 preload 暴露的类型声明。
|
||||||
|
|
||||||
|
### 2. CleanerService 标记崩溃
|
||||||
|
|
||||||
|
**文件**: `src/main/services/erp/cleaner.ts`,line 375 的 catch 块
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : 'Unknown error'
|
||||||
|
log.error('Cleaner failed', { ... })
|
||||||
|
result.errors.push(`Clean failed: ${message}`)
|
||||||
|
result.crashed = true // ← 新增
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. CleanerApplicationService 重试逻辑
|
||||||
|
|
||||||
|
**文件**: `src/main/services/cleaner/cleaner-application-service.ts`
|
||||||
|
|
||||||
|
在 `runCleaner()` 中,`cleaner.clean()` 返回后增加重试判断:
|
||||||
|
|
||||||
|
```
|
||||||
|
runCleaner(eventSender, input) {
|
||||||
|
const executionId = generateExecutionId() // 用户点击时生成
|
||||||
|
const startTime = Date.now()
|
||||||
|
|
||||||
|
// 1. 获取 ERP 配置、数据库连接、订单解析(不变)
|
||||||
|
// 2. 登录 ERP(不变)
|
||||||
|
|
||||||
|
let result = await cleaner.clean(modifiedInput)
|
||||||
|
|
||||||
|
// === 外层重试 ===
|
||||||
|
if (result.crashed) {
|
||||||
|
log.warn('检测到流程级崩溃,准备外层重试', { executionId })
|
||||||
|
|
||||||
|
await authService.close() // 关闭不可用的浏览器
|
||||||
|
authService = new ErpAuthService({...})
|
||||||
|
await authService.login() // 重新登录
|
||||||
|
|
||||||
|
cleaner = new CleanerService(authService)
|
||||||
|
result = await cleaner.clean(modifiedInput) // 全部订单重新跑
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 生成报告(使用 executionId 作为文件名一部分,避免重复)
|
||||||
|
await this.generateAndUploadReport(input, result, startTime, executionId)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 执行 ID 生成规则
|
||||||
|
|
||||||
|
格式: `CLN-{yyyyMMddHHmmss}-{4位随机字母}`
|
||||||
|
|
||||||
|
示例: `CLN-20260410112930-A7FK`
|
||||||
|
|
||||||
|
生成时机: `runCleaner()` 入口处,在 ERP 登录之前。重试时同一个 executionId 不变。
|
||||||
|
|
||||||
|
用途:
|
||||||
|
- 报告文件名: `cleaner-report-CLN-20260410112930-A7FK.md`
|
||||||
|
- RustFS 存储路径中包含该 ID,重试时覆盖同一文件
|
||||||
|
- 报告内容中显示该 ID
|
||||||
|
|
||||||
|
### 5. 报告增强
|
||||||
|
|
||||||
|
**文件**: `src/main/services/report/cleaner-report-generator.ts`
|
||||||
|
|
||||||
|
在执行摘要表格中新增字段:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
| 项目 | 值 |
|
||||||
|
| ---------------- | --------------------------------- |
|
||||||
|
| **执行 ID** | `CLN-20260410112930-A7FK` | ← 新增
|
||||||
|
| **应用版本** | `1.11.1` | ← 新增
|
||||||
|
| **执行时间** | `2026-04-10 11:29:30` |
|
||||||
|
| **执行模式** | `正式执行` |
|
||||||
|
| ... | ... |
|
||||||
|
```
|
||||||
|
|
||||||
|
- **执行 ID**: 从 ReportOptions 传入
|
||||||
|
- **应用版本**: `app.getVersion()`,沿用 logger 中已有的获取方式
|
||||||
|
|
||||||
|
**ReportOptions 变更**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface ReportOptions {
|
||||||
|
dryRun: boolean
|
||||||
|
username: string
|
||||||
|
startTime: number
|
||||||
|
endTime: number
|
||||||
|
executionId: string // ← 新增
|
||||||
|
appVersion: string // ← 新增
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**报告文件名变更**:
|
||||||
|
|
||||||
|
```
|
||||||
|
旧: cleaner-report-2026-04-10-03-30-12.md
|
||||||
|
新: cleaner-report-CLN-20260410112930-A7FK.md
|
||||||
|
```
|
||||||
|
|
||||||
|
重试时同一个 executionId 生成相同的文件名,本地文件和 RustFS 上传都会覆盖旧报告,无需额外去重逻辑。
|
||||||
|
|
||||||
|
### 6. 进度通知增强
|
||||||
|
|
||||||
|
重试时向前端发送进度通知,让用户知道正在重试:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
this.sendProgress(eventSender, '流程崩溃,正在重新登录并重试...', 0, {
|
||||||
|
phase: 'retry',
|
||||||
|
...
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## 不涉及的部分
|
||||||
|
|
||||||
|
- 前端 UI 变更(后续可单独做,展示重试状态)
|
||||||
|
- IPC channel 变更
|
||||||
|
- 内层重试逻辑(订单级/物料级)不变
|
||||||
|
- 数据库 schema 变更
|
||||||
16
docs/releases/1.10.0.md
Normal file
16
docs/releases/1.10.0.md
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# 1.10.0
|
||||||
|
|
||||||
|
## 数据库
|
||||||
|
|
||||||
|
- **新增 PostgreSQL 支持**:应用现可连接 PostgreSQL 数据库,与 MySQL、SQL Server 并列可选。
|
||||||
|
- 数据库方言自动适配,SQL 语句根据数据库类型生成正确的标识符引用格式。
|
||||||
|
|
||||||
|
## 稳定性
|
||||||
|
|
||||||
|
- 修复 PostgreSQL 环境下表名双引号导致的 SQL 语法错误。
|
||||||
|
- 修复 PostgreSQL 关键字冲突和大小写敏感问题,自动处理标识符转义。
|
||||||
|
|
||||||
|
## 质量改进
|
||||||
|
|
||||||
|
- 扩展核心业务模块(认证、清理、校验)的单元测试覆盖,提升回归检测能力。
|
||||||
|
- 改进测试隔离性,减少跨用例状态泄漏和测试日志噪音。
|
||||||
16
docs/releases/1.11.0.md
Normal file
16
docs/releases/1.11.0.md
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# 1.11.0
|
||||||
|
|
||||||
|
## 物料清理
|
||||||
|
|
||||||
|
- 管理员执行清理时可按负责人筛选物料,仅处理指定负责人的数据,避免误删其他人的标记。
|
||||||
|
- 未选择负责人时自动按订单号关联查询物料,保证清理范围准确。
|
||||||
|
|
||||||
|
## 审计日志
|
||||||
|
|
||||||
|
- 统一审计记录中的计算机名称来源,消除多来源不一致的情况。
|
||||||
|
- 增强审计日志的类型安全性和覆盖范围,异常情况下不再丢失日志。
|
||||||
|
|
||||||
|
## 质量改进
|
||||||
|
|
||||||
|
- 端到端测试迁移至 Playwright 框架,提升测试稳定性和执行效率。
|
||||||
|
- 改进单元测试的隔离性和模拟驱动覆盖,减少跨用例状态干扰。
|
||||||
5
docs/releases/1.11.1.md
Normal file
5
docs/releases/1.11.1.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# 1.11.1
|
||||||
|
|
||||||
|
## 问题修复
|
||||||
|
|
||||||
|
- 修复管理员按负责人筛选清理时,因类型声明缺失导致构建失败的问题。
|
||||||
19
docs/releases/1.12.0.md
Normal file
19
docs/releases/1.12.0.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# 1.12.0
|
||||||
|
|
||||||
|
## 清理操作历史
|
||||||
|
|
||||||
|
- 新增操作历史面板,每次清理的执行记录、订单结果、物料明细均可回溯查看。
|
||||||
|
- 历史记录按批次归档,支持管理员查看所有用户记录、普通用户查看自己的记录。
|
||||||
|
- 批次支持展开查看多层详情:执行概况、订单状态、物料操作明细。
|
||||||
|
|
||||||
|
## 订单追踪
|
||||||
|
|
||||||
|
- 所有输入的订单(含总排号)均会记录在历史中,不再遗漏未找到或未匹配的订单。
|
||||||
|
- 总排号与订单号并列显示,未匹配的总排号标注为"未找到",ERP 中不存在的订单标注为"ERP 不存在"。
|
||||||
|
- 内层重试和外层崩溃重试信息在订单详情中完整展示。
|
||||||
|
|
||||||
|
## 改进
|
||||||
|
|
||||||
|
- 数据库时间统一使用 UTC 存储,界面显示本地时间。
|
||||||
|
- 试运行模式下跳过物料级别的数据库写入,避免产生无效记录。
|
||||||
|
- 操作历史面板加宽至 140%,改善订单表格的阅读体验。
|
||||||
165
package-lock.json
generated
165
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "erpauto",
|
"name": "erpauto",
|
||||||
"version": "1.9.0",
|
"version": "1.12.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "erpauto",
|
"name": "erpauto",
|
||||||
"version": "1.9.0",
|
"version": "1.12.0",
|
||||||
"hasInstallScript": true,
|
"hasInstallScript": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.929.0",
|
"@aws-sdk/client-s3": "^3.929.0",
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
"lucide-react": "^0.575.0",
|
"lucide-react": "^0.575.0",
|
||||||
"mssql": "^12.2.0",
|
"mssql": "^12.2.0",
|
||||||
"mysql2": "^3.18.2",
|
"mysql2": "^3.18.2",
|
||||||
|
"pg": "^8.20.0",
|
||||||
"playwright": "^1.58.2",
|
"playwright": "^1.58.2",
|
||||||
"playwright-core": "^1.58.2",
|
"playwright-core": "^1.58.2",
|
||||||
"react-focus-lock": "^2.13.7",
|
"react-focus-lock": "^2.13.7",
|
||||||
@@ -48,6 +49,7 @@
|
|||||||
"@playwright/test": "^1.58.2",
|
"@playwright/test": "^1.58.2",
|
||||||
"@types/mssql": "^9.1.9",
|
"@types/mssql": "^9.1.9",
|
||||||
"@types/node": "^22.19.13",
|
"@types/node": "^22.19.13",
|
||||||
|
"@types/pg": "^8.20.0",
|
||||||
"@types/react": "^19.2.7",
|
"@types/react": "^19.2.7",
|
||||||
"@types/react-dom": "^19.2.3",
|
"@types/react-dom": "^19.2.3",
|
||||||
"@types/uuid": "^10.0.0",
|
"@types/uuid": "^10.0.0",
|
||||||
@@ -5020,6 +5022,18 @@
|
|||||||
"undici-types": "~6.21.0"
|
"undici-types": "~6.21.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/pg": {
|
||||||
|
"version": "8.20.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.20.0.tgz",
|
||||||
|
"integrity": "sha512-bEPFOaMAHTEP1EzpvHTbmwR8UsFyHSKsRisLIHVMXnpNefSbGA1bD6CVy+qKjGSqmZqNqBDV2azOBo8TgkcVow==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "*",
|
||||||
|
"pg-protocol": "*",
|
||||||
|
"pg-types": "^2.2.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/plist": {
|
"node_modules/@types/plist": {
|
||||||
"version": "3.0.5",
|
"version": "3.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/@types/plist/-/plist-3.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/@types/plist/-/plist-3.0.5.tgz",
|
||||||
@@ -13532,6 +13546,96 @@
|
|||||||
"integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==",
|
"integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/pg": {
|
||||||
|
"version": "8.20.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg/-/pg-8.20.0.tgz",
|
||||||
|
"integrity": "sha512-ldhMxz2r8fl/6QkXnBD3CR9/xg694oT6DZQ2s6c/RI28OjtSOpxnPrUCGOBJ46RCUxcWdx3p6kw/xnDHjKvaRA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
|
"dependencies": {
|
||||||
|
"pg-connection-string": "^2.12.0",
|
||||||
|
"pg-pool": "^3.13.0",
|
||||||
|
"pg-protocol": "^1.13.0",
|
||||||
|
"pg-types": "2.2.0",
|
||||||
|
"pgpass": "1.0.5"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 16.0.0"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"pg-cloudflare": "^1.3.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"pg-native": ">=3.0.1"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"pg-native": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-cloudflare": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.3.0.tgz",
|
||||||
|
"integrity": "sha512-6lswVVSztmHiRtD6I8hw4qP/nDm1EJbKMRhf3HCYaqud7frGysPv7FYJ5noZQdhQtN2xJnimfMtvQq21pdbzyQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"node_modules/pg-connection-string": {
|
||||||
|
"version": "2.12.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.12.0.tgz",
|
||||||
|
"integrity": "sha512-U7qg+bpswf3Cs5xLzRqbXbQl85ng0mfSV/J0nnA31MCLgvEaAo7CIhmeyrmJpOr7o+zm0rXK+hNnT5l9RHkCkQ==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/pg-int8": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-pool": {
|
||||||
|
"version": "3.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.13.0.tgz",
|
||||||
|
"integrity": "sha512-gB+R+Xud1gLFuRD/QgOIgGOBE2KCQPaPwkzBBGC9oG69pHTkhQeIuejVIk3/cnDyX39av2AxomQiyPT13WKHQA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"pg": ">=8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-protocol": {
|
||||||
|
"version": "1.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.13.0.tgz",
|
||||||
|
"integrity": "sha512-zzdvXfS6v89r6v7OcFCHfHlyG/wvry1ALxZo4LqgUoy7W9xhBDMaqOuMiF3qEV45VqsN6rdlcehHrfDtlCPc8w==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/pg-types": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"pg-int8": "1.0.1",
|
||||||
|
"postgres-array": "~2.0.0",
|
||||||
|
"postgres-bytea": "~1.0.0",
|
||||||
|
"postgres-date": "~1.0.4",
|
||||||
|
"postgres-interval": "^1.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pgpass": {
|
||||||
|
"version": "1.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
|
||||||
|
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"split2": "^4.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/picocolors": {
|
"node_modules/picocolors": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
||||||
@@ -13641,6 +13745,45 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/postgres-array": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-bytea": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-date": {
|
||||||
|
"version": "1.0.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
|
||||||
|
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-interval": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"xtend": "^4.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/postject": {
|
"node_modules/postject": {
|
||||||
"version": "1.0.0-alpha.6",
|
"version": "1.0.0-alpha.6",
|
||||||
"resolved": "https://registry.npmjs.org/postject/-/postject-1.0.0-alpha.6.tgz",
|
"resolved": "https://registry.npmjs.org/postject/-/postject-1.0.0-alpha.6.tgz",
|
||||||
@@ -14912,6 +15055,15 @@
|
|||||||
"url": "https://github.com/sponsors/wooorm"
|
"url": "https://github.com/sponsors/wooorm"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/split2": {
|
||||||
|
"version": "4.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
|
||||||
|
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 10.x"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/sprintf-js": {
|
"node_modules/sprintf-js": {
|
||||||
"version": "1.1.3",
|
"version": "1.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz",
|
||||||
@@ -17836,6 +17988,15 @@
|
|||||||
"integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
|
"integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/xtend": {
|
||||||
|
"version": "4.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||||
|
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/y18n": {
|
"node_modules/y18n": {
|
||||||
"version": "5.0.8",
|
"version": "5.0.8",
|
||||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
|
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "erpauto",
|
"name": "erpauto",
|
||||||
"version": "1.9.0",
|
"version": "1.12.0",
|
||||||
"description": "An Electron application with React and TypeScript",
|
"description": "An Electron application with React and TypeScript",
|
||||||
"main": "./out/main/index.js",
|
"main": "./out/main/index.js",
|
||||||
"author": "example.com",
|
"author": "example.com",
|
||||||
@@ -48,6 +48,7 @@
|
|||||||
"lucide-react": "^0.575.0",
|
"lucide-react": "^0.575.0",
|
||||||
"mssql": "^12.2.0",
|
"mssql": "^12.2.0",
|
||||||
"mysql2": "^3.18.2",
|
"mysql2": "^3.18.2",
|
||||||
|
"pg": "^8.20.0",
|
||||||
"playwright": "^1.58.2",
|
"playwright": "^1.58.2",
|
||||||
"playwright-core": "^1.58.2",
|
"playwright-core": "^1.58.2",
|
||||||
"react-focus-lock": "^2.13.7",
|
"react-focus-lock": "^2.13.7",
|
||||||
@@ -72,6 +73,7 @@
|
|||||||
"@playwright/test": "^1.58.2",
|
"@playwright/test": "^1.58.2",
|
||||||
"@types/mssql": "^9.1.9",
|
"@types/mssql": "^9.1.9",
|
||||||
"@types/node": "^22.19.13",
|
"@types/node": "^22.19.13",
|
||||||
|
"@types/pg": "^8.20.0",
|
||||||
"@types/react": "^19.2.7",
|
"@types/react": "^19.2.7",
|
||||||
"@types/react-dom": "^19.2.3",
|
"@types/react-dom": "^19.2.3",
|
||||||
"@types/uuid": "^10.0.0",
|
"@types/uuid": "^10.0.0",
|
||||||
|
|||||||
@@ -1,34 +1,42 @@
|
|||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import logger from '../services/logger/index'
|
import logger from '../services/logger/index'
|
||||||
import { logAudit, closeAuditLogger } from '../services/logger/audit-logger'
|
import { logAudit, closeAuditLogger, cachedHostname } from '../services/logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../types/audit.types'
|
||||||
import { serializeError } from '../services/logger/error-utils'
|
import { serializeError } from '../services/logger/error-utils'
|
||||||
|
|
||||||
export function setupProcessGuards(): void {
|
export function setupProcessGuards(): void {
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
logger.error('Uncaught exception', { error: err })
|
logger.error('Uncaught exception', { error: err })
|
||||||
logAudit('SYSTEM_CRASH', 'system', {
|
try {
|
||||||
username: 'system',
|
logAudit(AuditAction.SYSTEM_CRASH, 'system', {
|
||||||
computerName: process.env.COMPUTERNAME || 'unknown',
|
username: 'system',
|
||||||
resource: 'main-process',
|
computerName: cachedHostname,
|
||||||
status: 'failure',
|
resource: 'main-process',
|
||||||
metadata: { error: err.message, stack: err.stack }
|
status: AuditStatus.FAILURE,
|
||||||
})
|
metadata: { error: err.message, stack: err.stack }
|
||||||
setTimeout(() => process.exit(1), 1000)
|
})
|
||||||
|
} catch (auditError) {
|
||||||
|
logger.error('Failed to write crash audit log', { error: auditError })
|
||||||
|
} finally {
|
||||||
|
setTimeout(() => process.exit(1), 1000)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason) => {
|
process.on('unhandledRejection', (reason) => {
|
||||||
const errorMeta =
|
const errorMeta =
|
||||||
reason instanceof Error
|
reason instanceof Error ? { error: serializeError(reason) } : { reason: String(reason) }
|
||||||
? { error: serializeError(reason) }
|
|
||||||
: { reason: String(reason) }
|
|
||||||
logger.error('Unhandled Rejection', errorMeta)
|
logger.error('Unhandled Rejection', errorMeta)
|
||||||
logAudit('SYSTEM_ERROR', 'system', {
|
try {
|
||||||
username: 'system',
|
logAudit(AuditAction.SYSTEM_ERROR, 'system', {
|
||||||
computerName: process.env.COMPUTERNAME || 'unknown',
|
username: 'system',
|
||||||
resource: 'main-process',
|
computerName: cachedHostname,
|
||||||
status: 'failure',
|
resource: 'main-process',
|
||||||
metadata: errorMeta
|
status: AuditStatus.FAILURE,
|
||||||
})
|
metadata: errorMeta
|
||||||
|
})
|
||||||
|
} catch (auditError) {
|
||||||
|
logger.error('Failed to write unhandled rejection audit log', { error: auditError })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('render-process-gone', (_, webContents, details) => {
|
app.on('render-process-gone', (_, webContents, details) => {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { randomUUID } from 'crypto'
|
||||||
|
import { app } from 'electron'
|
||||||
import { ipcMain } from 'electron'
|
import { ipcMain } from 'electron'
|
||||||
import { withErrorHandling, type IpcResult } from './index'
|
import { withErrorHandling, type IpcResult } from './index'
|
||||||
import type {
|
import type {
|
||||||
@@ -8,6 +10,7 @@ import type {
|
|||||||
} from '../types/cleaner.types'
|
} from '../types/cleaner.types'
|
||||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
import { CleanerApplicationService } from '../services/cleaner/cleaner-application-service'
|
import { CleanerApplicationService } from '../services/cleaner/cleaner-application-service'
|
||||||
|
import { CleanerOperationHistoryDAO } from '../services/database/cleaner-operation-history-dao'
|
||||||
|
|
||||||
export function registerCleanerHandlers(): void {
|
export function registerCleanerHandlers(): void {
|
||||||
const cleanerService = new CleanerApplicationService()
|
const cleanerService = new CleanerApplicationService()
|
||||||
@@ -15,10 +18,21 @@ export function registerCleanerHandlers(): void {
|
|||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
IPC_CHANNELS.CLEANER_RUN,
|
IPC_CHANNELS.CLEANER_RUN,
|
||||||
async (event, input: CleanerInput): Promise<IpcResult<CleanerResult>> => {
|
async (event, input: CleanerInput): Promise<IpcResult<CleanerResult>> => {
|
||||||
return withErrorHandling(
|
return withErrorHandling(async () => {
|
||||||
async () => cleanerService.runCleaner(event.sender, input),
|
const batchId = randomUUID()
|
||||||
'cleaner:run'
|
const historyDao = new CleanerOperationHistoryDAO()
|
||||||
)
|
const appVersion = app.getVersion()
|
||||||
|
|
||||||
|
const result = await cleanerService.runCleaner(
|
||||||
|
event.sender,
|
||||||
|
input,
|
||||||
|
batchId,
|
||||||
|
historyDao,
|
||||||
|
appVersion
|
||||||
|
)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}, 'cleaner:run')
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
156
src/main/ipc/cleaner-history-handler.ts
Normal file
156
src/main/ipc/cleaner-history-handler.ts
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
/**
|
||||||
|
* IPC Handler for Cleaner Operation History
|
||||||
|
*
|
||||||
|
* Handles IPC requests for cleaner operation history management:
|
||||||
|
* - Get batch list (filtered by user for non-admin users)
|
||||||
|
* - Get batch details (executions + orders)
|
||||||
|
* - Get material details for a specific order
|
||||||
|
* - Delete batches
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ipcMain } from 'electron'
|
||||||
|
import { CleanerOperationHistoryDAO } from '../services/database/cleaner-operation-history-dao'
|
||||||
|
import { SessionManager } from '../services/user/session-manager'
|
||||||
|
import { withErrorHandling, type IpcResult } from './index'
|
||||||
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
|
import { createLogger } from '../services/logger'
|
||||||
|
import type {
|
||||||
|
CleanerBatchStats,
|
||||||
|
CleanerExecutionRecord,
|
||||||
|
CleanerOrderRecord,
|
||||||
|
CleanerMaterialRecord,
|
||||||
|
GetCleanerBatchesOptions
|
||||||
|
} from '../types/cleaner-history.types'
|
||||||
|
|
||||||
|
const log = createLogger('CleanerHistoryHandler')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register IPC handlers for cleaner operation history
|
||||||
|
*/
|
||||||
|
export function registerCleanerHistoryHandlers(): void {
|
||||||
|
const dao = new CleanerOperationHistoryDAO()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get batches list
|
||||||
|
* Admin users get all batches, regular users get only their own
|
||||||
|
*/
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_BATCHES,
|
||||||
|
async (_event, options?: GetCleanerBatchesOptions): Promise<IpcResult<CleanerBatchStats[]>> => {
|
||||||
|
return withErrorHandling(async () => {
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
|
||||||
|
if (!currentUser) {
|
||||||
|
throw new Error('用户未登录')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Admin gets all batches, User gets only their own
|
||||||
|
const userId = currentUser.userType === 'Admin' ? undefined : currentUser.id
|
||||||
|
|
||||||
|
log.info('Getting cleaner history batches', {
|
||||||
|
userId: currentUser.id,
|
||||||
|
userType: currentUser.userType,
|
||||||
|
filtered: userId !== undefined
|
||||||
|
})
|
||||||
|
|
||||||
|
return await dao.getBatches(userId, options)
|
||||||
|
}, 'cleanerHistory:getBatches')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get batch details (executions + orders)
|
||||||
|
* Users can only view their own batch details, admins can view all
|
||||||
|
*/
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_BATCH_DETAILS,
|
||||||
|
async (
|
||||||
|
_event,
|
||||||
|
batchId: string
|
||||||
|
): Promise<
|
||||||
|
IpcResult<{ executions: CleanerExecutionRecord[]; orders: CleanerOrderRecord[] }>
|
||||||
|
> => {
|
||||||
|
return withErrorHandling(async () => {
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
|
||||||
|
if (!currentUser) {
|
||||||
|
throw new Error('用户未登录')
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info('Getting cleaner batch details', { batchId, userId: currentUser.id })
|
||||||
|
|
||||||
|
const details = await dao.getBatchDetails(batchId)
|
||||||
|
|
||||||
|
// For non-admin users, verify they own this batch
|
||||||
|
if (currentUser.userType !== 'Admin' && details.executions.length > 0) {
|
||||||
|
const batchOwnerId = details.executions[0].userId
|
||||||
|
if (batchOwnerId !== currentUser.id) {
|
||||||
|
throw new Error('没有权限查看此批次详情')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return details
|
||||||
|
}, 'cleanerHistory:getBatchDetails')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get material details for a specific order
|
||||||
|
*/
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_MATERIAL_DETAILS,
|
||||||
|
async (
|
||||||
|
_event,
|
||||||
|
batchId: string,
|
||||||
|
attemptNumber: number,
|
||||||
|
orderNumber: string
|
||||||
|
): Promise<IpcResult<CleanerMaterialRecord[]>> => {
|
||||||
|
return withErrorHandling(async () => {
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
|
||||||
|
if (!currentUser) {
|
||||||
|
throw new Error('用户未登录')
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info('Getting cleaner material details', { batchId, attemptNumber, orderNumber })
|
||||||
|
|
||||||
|
return await dao.getMaterialDetails(batchId, attemptNumber, orderNumber)
|
||||||
|
}, 'cleanerHistory:getMaterialDetails')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a batch
|
||||||
|
* Users can only delete their own batches, admins can delete any
|
||||||
|
*/
|
||||||
|
ipcMain.handle(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_DELETE_BATCH,
|
||||||
|
async (_event, batchId: string): Promise<IpcResult<{ deleted: boolean }>> => {
|
||||||
|
return withErrorHandling(async () => {
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
|
||||||
|
if (!currentUser) {
|
||||||
|
throw new Error('用户未登录')
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAdmin = currentUser.userType === 'Admin'
|
||||||
|
|
||||||
|
log.info('Deleting cleaner batch', {
|
||||||
|
batchId,
|
||||||
|
userId: currentUser.id,
|
||||||
|
isAdmin
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await dao.deleteBatch(batchId, currentUser.id, isAdmin)
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error(result.error || '删除批次失败')
|
||||||
|
}
|
||||||
|
|
||||||
|
return { deleted: true }
|
||||||
|
}, 'cleanerHistory:deleteBatch')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
log.info('Cleaner history IPC handlers registered')
|
||||||
|
}
|
||||||
@@ -5,7 +5,8 @@ import { OrderNumberResolver } from '../services/erp/order-resolver'
|
|||||||
import { create, type IDatabaseService } from '../services/database'
|
import { create, type IDatabaseService } from '../services/database'
|
||||||
import { ExtractorOperationHistoryDAO } from '../services/database/extractor-operation-history-dao'
|
import { ExtractorOperationHistoryDAO } from '../services/database/extractor-operation-history-dao'
|
||||||
import { createLogger } from '../services/logger'
|
import { createLogger } from '../services/logger'
|
||||||
import { logAudit } from '../services/logger/audit-logger'
|
import { logAuditWithCurrentUser } from '../services/logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../types/audit.types'
|
||||||
import { SessionManager } from '../services/user/session-manager'
|
import { SessionManager } from '../services/user/session-manager'
|
||||||
import { withErrorHandling, type IpcResult } from './index'
|
import { withErrorHandling, type IpcResult } from './index'
|
||||||
import { ErpConnectionError, ValidationError, DatabaseQueryError } from '../types/errors'
|
import { ErpConnectionError, ValidationError, DatabaseQueryError } from '../types/errors'
|
||||||
@@ -278,24 +279,17 @@ export function registerExtractorHandlers(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Audit log: EXTRACT (non-blocking)
|
// Audit log: EXTRACT (non-blocking)
|
||||||
const os = await import('os')
|
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
const auditStatus: 'success' | 'failure' | 'partial' =
|
const auditStatus: AuditStatus =
|
||||||
result.errors.length > 0 && result.recordCount > 0
|
result.errors.length > 0 && result.recordCount > 0
|
||||||
? 'partial'
|
? AuditStatus.PARTIAL
|
||||||
: result.errors.length > 0
|
: result.errors.length > 0
|
||||||
? 'failure'
|
? AuditStatus.FAILURE
|
||||||
: 'success'
|
: AuditStatus.SUCCESS
|
||||||
logAudit('EXTRACT', String(currentUser.id), {
|
logAuditWithCurrentUser(AuditAction.EXTRACT, 'MATERIAL_PLAN', auditStatus, {
|
||||||
username: currentUser.username,
|
orderCount: validOrderNumbers.length,
|
||||||
computerName: os.hostname(),
|
recordCount: result.recordCount,
|
||||||
resource: 'MATERIAL_PLAN',
|
errorCount: result.errors.length
|
||||||
status: auditStatus,
|
|
||||||
metadata: {
|
|
||||||
orderCount: validOrderNumbers.length,
|
|
||||||
recordCount: result.recordCount,
|
|
||||||
errorCount: result.errors.length
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { registerReportHandlers } from './report-handler'
|
|||||||
import { registerUpdateHandlers } from './update-handler'
|
import { registerUpdateHandlers } from './update-handler'
|
||||||
import { registerPlaywrightBrowserHandlers } from './playwright-browser'
|
import { registerPlaywrightBrowserHandlers } from './playwright-browser'
|
||||||
import { registerOperationHistoryHandlers } from './operation-history-handler'
|
import { registerOperationHistoryHandlers } from './operation-history-handler'
|
||||||
|
import { registerCleanerHistoryHandlers } from './cleaner-history-handler'
|
||||||
import { createLogger, logError } from '../services/logger'
|
import { createLogger, logError } from '../services/logger'
|
||||||
import { serializeError, sanitizeError } from '../services/logger/error-utils'
|
import { serializeError, sanitizeError } from '../services/logger/error-utils'
|
||||||
import { getErrorMessage, getErrorCode, isBaseError } from '../types/errors'
|
import { getErrorMessage, getErrorCode, isBaseError } from '../types/errors'
|
||||||
@@ -115,5 +116,6 @@ export function registerIpcHandlers(): void {
|
|||||||
registerUpdateHandlers()
|
registerUpdateHandlers()
|
||||||
registerPlaywrightBrowserHandlers()
|
registerPlaywrightBrowserHandlers()
|
||||||
registerOperationHistoryHandlers()
|
registerOperationHistoryHandlers()
|
||||||
|
registerCleanerHistoryHandlers()
|
||||||
log.info('All IPC handlers registered')
|
log.info('All IPC handlers registered')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import { UserErpConfigService } from '../services/user/user-erp-config-service'
|
|||||||
import { MySqlService } from '../services/database/mysql'
|
import { MySqlService } from '../services/database/mysql'
|
||||||
import { SqlServerService } from '../services/database/sql-server'
|
import { SqlServerService } from '../services/database/sql-server'
|
||||||
import { createLogger } from '../services/logger'
|
import { createLogger } from '../services/logger'
|
||||||
import { logAudit } from '../services/logger/audit-logger'
|
import { logAuditWithCurrentUser } from '../services/logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../types/audit.types'
|
||||||
import type { UserType, ConnectionTestResult, SaveSettingsResult } from '../types/settings.types'
|
import type { UserType, ConnectionTestResult, SaveSettingsResult } from '../types/settings.types'
|
||||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
import { ValidationError } from '../types/errors'
|
import { ValidationError } from '../types/errors'
|
||||||
@@ -67,13 +68,9 @@ export function registerSettingsHandlers(): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Audit log: SETTINGS_CHANGE (non-blocking)
|
// Audit log: SETTINGS_CHANGE (non-blocking)
|
||||||
const os = await import('os')
|
logAuditWithCurrentUser(AuditAction.SETTINGS_CHANGE, 'ERP_CONFIG', AuditStatus.SUCCESS, {
|
||||||
logAudit('SETTINGS_CHANGE', String(currentUser.id), {
|
changeType: 'erp_credentials',
|
||||||
username: currentUser.username,
|
usernameChanged: !!settings.erp.username
|
||||||
computerName: os.hostname(),
|
|
||||||
resource: 'ERP_CONFIG',
|
|
||||||
status: 'success',
|
|
||||||
metadata: { changeType: 'erp_credentials', usernameChanged: !!settings.erp.username }
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,8 @@ export function registerValidationHandlers(): void {
|
|||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA,
|
IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA,
|
||||||
async (
|
async (
|
||||||
event
|
event,
|
||||||
|
params?: { selectedManagers?: string[] }
|
||||||
): Promise<{
|
): Promise<{
|
||||||
success: boolean
|
success: boolean
|
||||||
orderNumbers?: string[]
|
orderNumbers?: string[]
|
||||||
@@ -213,7 +214,11 @@ export function registerValidationHandlers(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return validationApplicationService.getCleanerData(userInfo, event.sender.id)
|
return validationApplicationService.getCleanerData(
|
||||||
|
userInfo,
|
||||||
|
event.sender.id,
|
||||||
|
params?.selectedManagers ?? []
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { SessionManager } from '../user/session-manager'
|
|||||||
import { UpdateService } from '../update/update-service'
|
import { UpdateService } from '../update/update-service'
|
||||||
import { createLogger, run, getRequestId, getContext } from '../logger'
|
import { createLogger, run, getRequestId, getContext } from '../logger'
|
||||||
import { logAudit } from '../logger/audit-logger'
|
import { logAudit } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
import { ValidationError } from '../../types/errors'
|
import { ValidationError } from '../../types/errors'
|
||||||
import type { UserInfo } from '../../types/user.types'
|
import type { UserInfo } from '../../types/user.types'
|
||||||
import type {
|
import type {
|
||||||
@@ -73,11 +74,11 @@ export class AuthApplicationService {
|
|||||||
userId: userInfo.id
|
userId: userInfo.id
|
||||||
})
|
})
|
||||||
|
|
||||||
this.writeAuditLog('LOGIN', String(userInfo.id), {
|
this.writeAuditLog(AuditAction.LOGIN, String(userInfo.id), {
|
||||||
username: userInfo.username,
|
username: userInfo.username,
|
||||||
computerName: hostname(),
|
computerName: hostname(),
|
||||||
resource: 'ERP_SYSTEM',
|
resource: 'ERP_SYSTEM',
|
||||||
status: 'success',
|
status: AuditStatus.SUCCESS,
|
||||||
metadata: { loginType: 'silent', userType: userInfo.userType }
|
metadata: { loginType: 'silent', userType: userInfo.userType }
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -127,11 +128,11 @@ export class AuthApplicationService {
|
|||||||
const userInfo = this.sessionManager.getUserInfo()
|
const userInfo = this.sessionManager.getUserInfo()
|
||||||
|
|
||||||
if (!success || !userInfo) {
|
if (!success || !userInfo) {
|
||||||
this.writeAuditLog('LOGIN', '0', {
|
this.writeAuditLog(AuditAction.LOGIN, '0', {
|
||||||
username,
|
username,
|
||||||
computerName: hostname(),
|
computerName: hostname(),
|
||||||
resource: 'ERP_SYSTEM',
|
resource: 'ERP_SYSTEM',
|
||||||
status: 'failure',
|
status: AuditStatus.FAILURE,
|
||||||
metadata: { loginType: 'credentials', reason: 'invalid_credentials' }
|
metadata: { loginType: 'credentials', reason: 'invalid_credentials' }
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -155,11 +156,11 @@ export class AuthApplicationService {
|
|||||||
})
|
})
|
||||||
await this.updateService.setUserContext(userInfo.userType)
|
await this.updateService.setUserContext(userInfo.userType)
|
||||||
|
|
||||||
this.writeAuditLog('LOGIN', String(userInfo.id), {
|
this.writeAuditLog(AuditAction.LOGIN, String(userInfo.id), {
|
||||||
username: userInfo.username,
|
username: userInfo.username,
|
||||||
computerName: hostname(),
|
computerName: hostname(),
|
||||||
resource: 'ERP_SYSTEM',
|
resource: 'ERP_SYSTEM',
|
||||||
status: 'success',
|
status: AuditStatus.SUCCESS,
|
||||||
metadata: { loginType: 'credentials', userType: userInfo.userType }
|
metadata: { loginType: 'credentials', userType: userInfo.userType }
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -204,11 +205,11 @@ export class AuthApplicationService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (userInfo) {
|
if (userInfo) {
|
||||||
this.writeAuditLog('LOGOUT', String(userInfo.id), {
|
this.writeAuditLog(AuditAction.LOGOUT, String(userInfo.id), {
|
||||||
username: userInfo.username,
|
username: userInfo.username,
|
||||||
computerName: hostname(),
|
computerName: hostname(),
|
||||||
resource: 'ERP_SYSTEM',
|
resource: 'ERP_SYSTEM',
|
||||||
status: 'success',
|
status: AuditStatus.SUCCESS,
|
||||||
metadata: { userType: userInfo.userType }
|
metadata: { userType: userInfo.userType }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -310,7 +311,7 @@ export class AuthApplicationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private writeAuditLog(
|
private writeAuditLog(
|
||||||
action: 'LOGIN' | 'LOGOUT',
|
action: AuditAction.LOGIN | AuditAction.LOGOUT,
|
||||||
actorId: string,
|
actorId: string,
|
||||||
payload: Parameters<typeof logAudit>[2]
|
payload: Parameters<typeof logAudit>[2]
|
||||||
): void {
|
): void {
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
import type { WebContents } from 'electron'
|
import type { WebContents } from 'electron'
|
||||||
import type { MySqlService } from '../database/mysql'
|
import type { IDatabaseService } from '../../types/database.types'
|
||||||
import type { SqlServerService } from '../database/sql-server'
|
|
||||||
import { ErpAuthService } from '../erp/erp-auth'
|
import { ErpAuthService } from '../erp/erp-auth'
|
||||||
import { CleanerService } from '../erp/cleaner'
|
import { CleanerService } from '../erp/cleaner'
|
||||||
import { OrderNumberResolver } from '../erp/order-resolver'
|
import { OrderNumberResolver } from '../erp/order-resolver'
|
||||||
import { MySqlService as MySqlServiceImpl } from '../database/mysql'
|
import { MySqlService as MySqlServiceImpl } from '../database/mysql'
|
||||||
import { SqlServerService as SqlServerServiceImpl } from '../database/sql-server'
|
import { SqlServerService as SqlServerServiceImpl } from '../database/sql-server'
|
||||||
|
import { PostgreSqlService as PostgreSqlServiceImpl } from '../database/postgresql'
|
||||||
import { ConfigManager } from '../config/config-manager'
|
import { ConfigManager } from '../config/config-manager'
|
||||||
import { ResultExporter } from '../excel/result-exporter'
|
import { ResultExporter } from '../excel/result-exporter'
|
||||||
import { CleanerReportGenerator } from '../report/cleaner-report-generator'
|
|
||||||
import { RustfsService } from '../rustfs'
|
|
||||||
import { SessionManager } from '../user/session-manager'
|
import { SessionManager } from '../user/session-manager'
|
||||||
import { UserErpConfigService } from '../user/user-erp-config-service'
|
import { UserErpConfigService } from '../user/user-erp-config-service'
|
||||||
import { createLogger } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
import { logAudit } from '../logger/audit-logger'
|
import { logAuditWithCurrentUser } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
import { IPC_CHANNELS } from '../../../shared/ipc-channels'
|
import { IPC_CHANNELS } from '../../../shared/ipc-channels'
|
||||||
import { DatabaseQueryError, ErpConnectionError, ValidationError } from '../../types/errors'
|
import { DatabaseQueryError, ErpConnectionError, ValidationError } from '../../types/errors'
|
||||||
|
import { CleanerOperationHistoryDAO } from '../database/cleaner-operation-history-dao'
|
||||||
import type {
|
import type {
|
||||||
CleanerInput,
|
CleanerInput,
|
||||||
CleanerProgress,
|
CleanerProgress,
|
||||||
@@ -23,16 +23,21 @@ import type {
|
|||||||
ExportResultItem,
|
ExportResultItem,
|
||||||
ExportResultResponse
|
ExportResultResponse
|
||||||
} from '../../types/cleaner.types'
|
} from '../../types/cleaner.types'
|
||||||
|
import type { InsertMaterialDetailInput, InsertOrderInput } from '../../types/cleaner-history.types'
|
||||||
|
import type { OrderMapping } from '../../types/order-resolver.types'
|
||||||
|
|
||||||
const log = createLogger('CleanerApplicationService')
|
const log = createLogger('CleanerApplicationService')
|
||||||
|
|
||||||
type DatabaseService = MySqlService | SqlServerService
|
|
||||||
|
|
||||||
export class CleanerApplicationService {
|
export class CleanerApplicationService {
|
||||||
async runCleaner(eventSender: WebContents, input: CleanerInput): Promise<CleanerResult> {
|
async runCleaner(
|
||||||
const startTime = Date.now()
|
eventSender: WebContents,
|
||||||
|
input: CleanerInput,
|
||||||
|
batchId: string,
|
||||||
|
historyDao: CleanerOperationHistoryDAO,
|
||||||
|
appVersion: string
|
||||||
|
): Promise<CleanerResult> {
|
||||||
let authService: ErpAuthService | null = null
|
let authService: ErpAuthService | null = null
|
||||||
let dbService: DatabaseService | null = null
|
let dbService: IDatabaseService | null = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.info('Fetching ERP configuration from database...')
|
log.info('Fetching ERP configuration from database...')
|
||||||
@@ -46,7 +51,7 @@ export class CleanerApplicationService {
|
|||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const dbType = configManager.getDatabaseType()
|
const dbType = configManager.getDatabaseType()
|
||||||
log.info(
|
log.info(
|
||||||
`Connecting to ${dbType === 'sqlserver' ? 'SQL Server' : 'MySQL'} for order resolution...`
|
`Connecting to ${dbType === 'sqlserver' ? 'SQL Server' : dbType === 'postgresql' ? 'PostgreSQL' : 'MySQL'} for order resolution...`
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -68,14 +73,60 @@ export class CleanerApplicationService {
|
|||||||
log.warn('Resolution warnings', { warnings })
|
log.warn('Resolution warnings', { warnings })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (validOrderNumbers.length === 0) {
|
// Build order inputs from ALL mappings (including resolution failures)
|
||||||
throw new ValidationError(
|
const orderInputs = this.buildOrderInputs(mappings)
|
||||||
'没有有效的生产订单号可处理。请检查输入的格式或数据库连接。',
|
|
||||||
'VAL_INVALID_INPUT'
|
log.info('Resolved order numbers', {
|
||||||
)
|
total: mappings.length,
|
||||||
|
resolved: validOrderNumbers.length,
|
||||||
|
failed: mappings.length - validOrderNumbers.length
|
||||||
|
})
|
||||||
|
|
||||||
|
// Insert execution record and ALL order records into database
|
||||||
|
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||||
|
if (currentUser) {
|
||||||
|
await historyDao.insertExecution({
|
||||||
|
batchId,
|
||||||
|
attemptNumber: 1,
|
||||||
|
userId: currentUser.id,
|
||||||
|
username: currentUser.username,
|
||||||
|
isDryRun: input.dryRun ?? false,
|
||||||
|
totalOrders: orderInputs.length,
|
||||||
|
appVersion
|
||||||
|
})
|
||||||
|
await historyDao.insertOrderRecords(batchId, 1, orderInputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info('Resolved order numbers', { count: validOrderNumbers.length })
|
// If no valid order numbers, update execution to failed and return early
|
||||||
|
if (validOrderNumbers.length === 0) {
|
||||||
|
if (currentUser) {
|
||||||
|
await historyDao.updateExecutionStatus(
|
||||||
|
batchId,
|
||||||
|
1,
|
||||||
|
'failed',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
new Date(),
|
||||||
|
warnings.join('\n') || '没有有效的生产订单号可处理'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
const emptyResult: CleanerResult = {
|
||||||
|
ordersProcessed: 0,
|
||||||
|
materialsDeleted: 0,
|
||||||
|
materialsSkipped: 0,
|
||||||
|
errors: [...warnings],
|
||||||
|
details: [],
|
||||||
|
retriedOrders: 0,
|
||||||
|
successfulRetries: 0,
|
||||||
|
materialsFailed: 0,
|
||||||
|
uncertainDeletions: 0
|
||||||
|
}
|
||||||
|
await this.recordCleanupAudit(0, input, emptyResult)
|
||||||
|
return emptyResult
|
||||||
|
}
|
||||||
|
|
||||||
authService = new ErpAuthService({
|
authService = new ErpAuthService({
|
||||||
url: erpConfig.url,
|
url: erpConfig.url,
|
||||||
@@ -105,7 +156,6 @@ export class CleanerApplicationService {
|
|||||||
totalMaterialsInOrder: 0
|
totalMaterialsInOrder: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService)
|
|
||||||
const modifiedInput: CleanerInput = {
|
const modifiedInput: CleanerInput = {
|
||||||
...input,
|
...input,
|
||||||
orderNumbers: validOrderNumbers,
|
orderNumbers: validOrderNumbers,
|
||||||
@@ -115,12 +165,94 @@ export class CleanerApplicationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.info('Starting cleaning', {
|
log.info('Starting cleaning', {
|
||||||
|
batchId,
|
||||||
orderCount: validOrderNumbers.length,
|
orderCount: validOrderNumbers.length,
|
||||||
queryBatchSize: input.queryBatchSize ?? 100,
|
queryBatchSize: input.queryBatchSize ?? 100,
|
||||||
processConcurrency: input.processConcurrency ?? 1
|
processConcurrency: input.processConcurrency ?? 1
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = await cleaner.clean(modifiedInput)
|
let cleaner = new CleanerService(authService)
|
||||||
|
let result = await cleaner.clean(modifiedInput)
|
||||||
|
|
||||||
|
// Outer retry: re-login and re-run all orders on fatal crash
|
||||||
|
if (result.crashed) {
|
||||||
|
log.warn('检测到流程级崩溃,准备外层重试', { batchId })
|
||||||
|
|
||||||
|
// Save attempt 1 result as crashed
|
||||||
|
await this.saveAttemptToDatabase(historyDao, batchId, 1, result)
|
||||||
|
|
||||||
|
this.sendProgress(eventSender, '流程崩溃,正在重新登录并重试...', 0, {
|
||||||
|
phase: 'retry',
|
||||||
|
currentOrderIndex: 0,
|
||||||
|
totalOrders,
|
||||||
|
currentMaterialIndex: 0,
|
||||||
|
totalMaterialsInOrder: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await authService.close()
|
||||||
|
} catch {
|
||||||
|
// Browser may already be dead, ignore close errors
|
||||||
|
}
|
||||||
|
authService = null
|
||||||
|
|
||||||
|
authService = new ErpAuthService({
|
||||||
|
url: erpConfig.url,
|
||||||
|
username: erpConfig.username,
|
||||||
|
password: erpConfig.password,
|
||||||
|
headless: input.headless ?? true
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await authService.login()
|
||||||
|
log.info('Outer retry: re-login successful', { batchId })
|
||||||
|
} catch (loginError) {
|
||||||
|
log.error('Outer retry: re-login failed', {
|
||||||
|
batchId,
|
||||||
|
error: loginError instanceof Error ? loginError.message : String(loginError)
|
||||||
|
})
|
||||||
|
// Return the original crash result if re-login fails
|
||||||
|
result.errors.push(
|
||||||
|
`外层重试登录失败: ${loginError instanceof Error ? loginError.message : String(loginError)}`
|
||||||
|
)
|
||||||
|
if (warnings.length > 0) {
|
||||||
|
result.errors = [...warnings, ...result.errors]
|
||||||
|
}
|
||||||
|
await this.recordCleanupAudit(validOrderNumbers.length, input, result)
|
||||||
|
// Attempt 1 already saved as crashed above
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert execution and order records for attempt 2
|
||||||
|
if (currentUser) {
|
||||||
|
await historyDao.insertExecution({
|
||||||
|
batchId,
|
||||||
|
attemptNumber: 2,
|
||||||
|
userId: currentUser.id,
|
||||||
|
username: currentUser.username,
|
||||||
|
isDryRun: input.dryRun ?? false,
|
||||||
|
totalOrders: orderInputs.length,
|
||||||
|
appVersion
|
||||||
|
})
|
||||||
|
await historyDao.insertOrderRecords(batchId, 2, orderInputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleaner = new CleanerService(authService)
|
||||||
|
result = await cleaner.clean(modifiedInput)
|
||||||
|
|
||||||
|
// Save attempt 2 result
|
||||||
|
await this.saveAttemptToDatabase(historyDao, batchId, 2, result)
|
||||||
|
|
||||||
|
log.info('Outer retry completed', {
|
||||||
|
batchId,
|
||||||
|
processedCount: result.ordersProcessed,
|
||||||
|
errorCount: result.errors.length,
|
||||||
|
crashed: result.crashed
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// No crash — save attempt 1 result
|
||||||
|
await this.saveAttemptToDatabase(historyDao, batchId, 1, result)
|
||||||
|
}
|
||||||
|
|
||||||
if (warnings.length > 0) {
|
if (warnings.length > 0) {
|
||||||
result.errors = [...warnings, ...result.errors]
|
result.errors = [...warnings, ...result.errors]
|
||||||
@@ -135,12 +267,12 @@ export class CleanerApplicationService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
log.info('Cleaning completed', {
|
log.info('Cleaning completed', {
|
||||||
|
batchId,
|
||||||
processedCount: result.ordersProcessed,
|
processedCount: result.ordersProcessed,
|
||||||
errorCount: result.errors.length
|
errorCount: result.errors.length
|
||||||
})
|
})
|
||||||
|
|
||||||
await this.recordCleanupAudit(validOrderNumbers.length, input, result)
|
await this.recordCleanupAudit(validOrderNumbers.length, input, result)
|
||||||
await this.generateAndUploadReport(input, result, startTime)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
} finally {
|
} finally {
|
||||||
@@ -201,7 +333,7 @@ export class CleanerApplicationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getDatabaseService(): Promise<DatabaseService> {
|
private async getDatabaseService(): Promise<IDatabaseService> {
|
||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const config = configManager.getConfig()
|
const config = configManager.getConfig()
|
||||||
const dbType = configManager.getDatabaseType()
|
const dbType = configManager.getDatabaseType()
|
||||||
@@ -223,6 +355,19 @@ export class CleanerApplicationService {
|
|||||||
return sqlServerService
|
return sqlServerService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dbType === 'postgresql') {
|
||||||
|
const dbConfig = config.database.postgresql
|
||||||
|
const pgService = new PostgreSqlServiceImpl({
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database
|
||||||
|
})
|
||||||
|
await pgService.connect()
|
||||||
|
return pgService
|
||||||
|
}
|
||||||
|
|
||||||
const dbConfig = config.database.mysql
|
const dbConfig = config.database.mysql
|
||||||
const mysqlService = new MySqlServiceImpl({
|
const mysqlService = new MySqlServiceImpl({
|
||||||
host: dbConfig.host,
|
host: dbConfig.host,
|
||||||
@@ -257,6 +402,38 @@ export class CleanerApplicationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build order inputs from ALL mappings, including resolution failures.
|
||||||
|
* Deduplicates by orderNumber for resolved mappings, includes all failed mappings.
|
||||||
|
*/
|
||||||
|
private buildOrderInputs(mappings: OrderMapping[]): InsertOrderInput[] {
|
||||||
|
const inputs: InsertOrderInput[] = []
|
||||||
|
const seenOrderNumbers = new Set<string>()
|
||||||
|
|
||||||
|
for (const mapping of mappings) {
|
||||||
|
if (mapping.resolved && mapping.orderNumber) {
|
||||||
|
// Deduplicate resolved mappings by order number
|
||||||
|
if (!seenOrderNumbers.has(mapping.orderNumber)) {
|
||||||
|
seenOrderNumbers.add(mapping.orderNumber)
|
||||||
|
inputs.push({
|
||||||
|
orderNumber: mapping.orderNumber,
|
||||||
|
productionId: mapping.productionId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Resolution failure: use original input as orderNumber identifier
|
||||||
|
inputs.push({
|
||||||
|
orderNumber: mapping.input,
|
||||||
|
productionId: mapping.productionId,
|
||||||
|
initialStatus: 'not_found',
|
||||||
|
errorMessage: mapping.error || '未在数据库中找到对应的订单号'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return inputs
|
||||||
|
}
|
||||||
|
|
||||||
private async recordCleanupAudit(
|
private async recordCleanupAudit(
|
||||||
orderCount: number,
|
orderCount: number,
|
||||||
input: CleanerInput,
|
input: CleanerInput,
|
||||||
@@ -267,93 +444,150 @@ export class CleanerApplicationService {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const status: 'success' | 'failure' | 'partial' =
|
const status: AuditStatus =
|
||||||
result.errors.length > 0 && result.materialsDeleted > 0
|
result.errors.length > 0 && result.materialsDeleted > 0
|
||||||
? 'partial'
|
? AuditStatus.PARTIAL
|
||||||
: result.errors.length > 0
|
: result.errors.length > 0
|
||||||
? 'failure'
|
? AuditStatus.FAILURE
|
||||||
: 'success'
|
: AuditStatus.SUCCESS
|
||||||
|
|
||||||
logAudit('CLEAN', String(currentUser.id), {
|
logAuditWithCurrentUser(AuditAction.CLEAN, 'MATERIAL_PLAN', status, {
|
||||||
username: currentUser.username,
|
orderCount,
|
||||||
computerName: (await import('os')).hostname(),
|
dryRun: input.dryRun ?? false,
|
||||||
resource: 'MATERIAL_PLAN',
|
queryBatchSize: input.queryBatchSize ?? 100,
|
||||||
status,
|
processConcurrency: input.processConcurrency ?? 1,
|
||||||
metadata: {
|
materialsDeleted: result.materialsDeleted,
|
||||||
orderCount,
|
materialsSkipped: result.materialsSkipped,
|
||||||
dryRun: input.dryRun ?? false,
|
errorCount: result.errors.length
|
||||||
queryBatchSize: input.queryBatchSize ?? 100,
|
|
||||||
processConcurrency: input.processConcurrency ?? 1,
|
|
||||||
materialsDeleted: result.materialsDeleted,
|
|
||||||
materialsSkipped: result.materialsSkipped,
|
|
||||||
errorCount: result.errors.length
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async generateAndUploadReport(
|
/**
|
||||||
input: CleanerInput,
|
* Save attempt results to database: update order statuses, insert material details,
|
||||||
result: CleanerResult,
|
* and update execution status.
|
||||||
startTime: number
|
*/
|
||||||
|
private async saveAttemptToDatabase(
|
||||||
|
historyDao: CleanerOperationHistoryDAO,
|
||||||
|
batchId: string,
|
||||||
|
attemptNumber: number,
|
||||||
|
result: CleanerResult
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const endTime = Date.now()
|
// Query execution record to determine if this is a dry run
|
||||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
const batchDetails = await historyDao.getBatchDetails(batchId)
|
||||||
const username = currentUser?.username ?? 'unknown'
|
const execution = batchDetails.executions.find((e) => e.attemptNumber === attemptNumber)
|
||||||
|
const isDryRun = execution?.isDryRun ?? false
|
||||||
|
|
||||||
const reportGenerator = new CleanerReportGenerator()
|
// Update order statuses and insert material details
|
||||||
const reportPath = await reportGenerator.generateReport(result, {
|
for (const detail of result.details) {
|
||||||
dryRun: input.dryRun ?? false,
|
await historyDao.updateOrderStatus(
|
||||||
username,
|
batchId,
|
||||||
startTime,
|
attemptNumber,
|
||||||
endTime
|
detail.orderNumber,
|
||||||
})
|
detail.notFound
|
||||||
log.info('Report generated', { path: reportPath })
|
? 'erp_not_found'
|
||||||
|
: detail.errors.length > 0
|
||||||
const configManager = ConfigManager.getInstance()
|
? 'failed'
|
||||||
const config = configManager.getConfig()
|
: 'success',
|
||||||
|
detail.materialsDeleted,
|
||||||
if (!config.rustfs?.enabled || !config.rustfs.endpoint) {
|
detail.materialsSkipped,
|
||||||
log.debug('RustFS is not enabled, skipping upload')
|
detail.materialsFailed,
|
||||||
return
|
detail.uncertainDeletions,
|
||||||
}
|
detail.retryCount,
|
||||||
|
detail.retrySuccess ?? false,
|
||||||
try {
|
detail.errors.length > 0 ? detail.errors.join('\n') : undefined
|
||||||
const rustfs = new RustfsService({ config: config.rustfs })
|
|
||||||
const reportFileName = reportPath.split(/[\\/]/).pop() || 'report.md'
|
|
||||||
const storageKey = rustfs.generateReportKey(reportFileName, username)
|
|
||||||
|
|
||||||
log.info('Uploading report to RustFS', {
|
|
||||||
localPath: reportPath,
|
|
||||||
storageKey
|
|
||||||
})
|
|
||||||
|
|
||||||
const uploadResult = await rustfs.uploadFile(
|
|
||||||
reportPath,
|
|
||||||
storageKey,
|
|
||||||
'text/markdown; charset=utf-8'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (uploadResult.success) {
|
// Insert material details for all materials
|
||||||
log.info('Report uploaded to RustFS successfully', {
|
const materialDetails: InsertMaterialDetailInput[] = []
|
||||||
key: storageKey,
|
|
||||||
etag: uploadResult.etag
|
for (const deleted of detail.deletedMaterials) {
|
||||||
})
|
materialDetails.push({
|
||||||
} else {
|
orderNumber: detail.orderNumber,
|
||||||
log.warn('Failed to upload report to RustFS', {
|
materialCode: deleted.materialCode,
|
||||||
error: uploadResult.error,
|
materialName: deleted.materialName,
|
||||||
key: storageKey
|
rowNumber: deleted.rowNumber,
|
||||||
|
result: deleted.outcome,
|
||||||
|
reason: null,
|
||||||
|
attemptCount: 1,
|
||||||
|
finalErrorCategory: null
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (rustfsError) {
|
|
||||||
log.error('RustFS upload failed', {
|
for (const skipped of detail.skippedMaterials) {
|
||||||
error: rustfsError instanceof Error ? rustfsError.message : String(rustfsError)
|
materialDetails.push({
|
||||||
})
|
orderNumber: detail.orderNumber,
|
||||||
|
materialCode: skipped.materialCode,
|
||||||
|
materialName: skipped.materialName,
|
||||||
|
rowNumber: skipped.rowNumber,
|
||||||
|
result: 'skipped',
|
||||||
|
reason: skipped.reason,
|
||||||
|
attemptCount: 0,
|
||||||
|
finalErrorCategory: null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const failed of detail.failedMaterials) {
|
||||||
|
materialDetails.push({
|
||||||
|
orderNumber: detail.orderNumber,
|
||||||
|
materialCode: failed.materialCode,
|
||||||
|
materialName: failed.materialName,
|
||||||
|
rowNumber: failed.rowNumber,
|
||||||
|
result: failed.finalOutcome,
|
||||||
|
reason:
|
||||||
|
failed.attempts
|
||||||
|
.map((a) => a.errorMessage)
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('; ') || null,
|
||||||
|
attemptCount: failed.attempts.length,
|
||||||
|
finalErrorCategory: failed.finalErrorCategory ?? null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (materialDetails.length > 0 && !isDryRun) {
|
||||||
|
await historyDao.insertMaterialDetails(batchId, attemptNumber, materialDetails)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (reportError) {
|
|
||||||
log.warn('Failed to generate report', {
|
// Determine execution status
|
||||||
error: reportError instanceof Error ? reportError.message : String(reportError)
|
const execStatus = result.crashed
|
||||||
|
? 'crashed'
|
||||||
|
: result.errors.length > 0 && result.materialsDeleted > 0
|
||||||
|
? 'partial'
|
||||||
|
: result.errors.length > 0
|
||||||
|
? 'failed'
|
||||||
|
: 'success'
|
||||||
|
|
||||||
|
// Build error message from execution-level errors (excluding per-order errors)
|
||||||
|
const execErrorMessage = result.errors.length > 0 ? result.errors.join('\n') : undefined
|
||||||
|
|
||||||
|
// Update execution status
|
||||||
|
await historyDao.updateExecutionStatus(
|
||||||
|
batchId,
|
||||||
|
attemptNumber,
|
||||||
|
execStatus,
|
||||||
|
result.ordersProcessed,
|
||||||
|
result.materialsDeleted,
|
||||||
|
result.materialsSkipped,
|
||||||
|
result.materialsFailed,
|
||||||
|
result.uncertainDeletions,
|
||||||
|
new Date(),
|
||||||
|
execErrorMessage
|
||||||
|
)
|
||||||
|
|
||||||
|
log.info('Attempt results saved to database', {
|
||||||
|
batchId,
|
||||||
|
attemptNumber,
|
||||||
|
execStatus,
|
||||||
|
ordersProcessed: result.ordersProcessed
|
||||||
})
|
})
|
||||||
|
} catch (dbError) {
|
||||||
|
log.error('Failed to save attempt results to database', {
|
||||||
|
batchId,
|
||||||
|
attemptNumber,
|
||||||
|
error: dbError instanceof Error ? dbError.message : String(dbError)
|
||||||
|
})
|
||||||
|
// Don't throw — database save failure should not affect the main result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { dirname } from 'path'
|
|||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import yaml from 'js-yaml'
|
import yaml from 'js-yaml'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { createLogger, applyLoggingConfig, trackDuration } from '../logger'
|
import { createLogger, applyLoggingConfig } from '../logger'
|
||||||
import { applyAuditConfig } from '../logger/audit-logger'
|
import { applyAuditConfig } from '../logger/audit-logger'
|
||||||
import {
|
import {
|
||||||
fullConfigSchema,
|
fullConfigSchema,
|
||||||
@@ -28,6 +28,7 @@ import {
|
|||||||
type DatabaseType,
|
type DatabaseType,
|
||||||
type MySqlConfig,
|
type MySqlConfig,
|
||||||
type SqlServerConfig,
|
type SqlServerConfig,
|
||||||
|
type PostgreSqlConfig,
|
||||||
type LoggingConfig
|
type LoggingConfig
|
||||||
} from '../../types/config.schema'
|
} from '../../types/config.schema'
|
||||||
|
|
||||||
@@ -66,6 +67,14 @@ const DEFAULT_CONFIG: FullConfig = {
|
|||||||
password: '',
|
password: '',
|
||||||
driver: 'ODBC Driver 18 for SQL Server',
|
driver: 'ODBC Driver 18 for SQL Server',
|
||||||
trustServerCertificate: true
|
trustServerCertificate: true
|
||||||
|
},
|
||||||
|
postgresql: {
|
||||||
|
host: 'localhost',
|
||||||
|
port: 5432,
|
||||||
|
database: 'erp_db',
|
||||||
|
username: 'postgres',
|
||||||
|
password: '',
|
||||||
|
maxPoolSize: 10
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
paths: {
|
paths: {
|
||||||
@@ -299,13 +308,20 @@ export class ConfigManager {
|
|||||||
/**
|
/**
|
||||||
* 获取当前激活的数据库配置
|
* 获取当前激活的数据库配置
|
||||||
*/
|
*/
|
||||||
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig {
|
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig | PostgreSqlConfig {
|
||||||
if (!this.config) {
|
if (!this.config) {
|
||||||
throw new Error('Configuration not initialized')
|
throw new Error('Configuration not initialized')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { activeType, mysql, sqlserver } = this.config.database
|
const { activeType, mysql, sqlserver, postgresql } = this.config.database
|
||||||
return activeType === 'mysql' ? mysql : sqlserver
|
switch (activeType) {
|
||||||
|
case 'postgresql':
|
||||||
|
return postgresql
|
||||||
|
case 'sqlserver':
|
||||||
|
return sqlserver
|
||||||
|
default:
|
||||||
|
return mysql
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
1000
src/main/services/database/cleaner-operation-history-dao.ts
Normal file
1000
src/main/services/database/cleaner-operation-history-dao.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { createLogger } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
|
import { logAuditWithCurrentUser } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
import { DiscreteMaterialPlanDAO, type MaterialPlanRecord } from './discrete-material-plan-dao'
|
import { DiscreteMaterialPlanDAO, type MaterialPlanRecord } from './discrete-material-plan-dao'
|
||||||
|
|
||||||
const log = createLogger('DataImportService')
|
const log = createLogger('DataImportService')
|
||||||
@@ -148,6 +150,20 @@ export class DataImportService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Audit log: DATA_IMPORT
|
||||||
|
logAuditWithCurrentUser(
|
||||||
|
AuditAction.DATA_IMPORT,
|
||||||
|
'MATERIAL_PLAN',
|
||||||
|
result.success ? AuditStatus.SUCCESS : AuditStatus.FAILURE,
|
||||||
|
{
|
||||||
|
recordsRead: result.recordsRead,
|
||||||
|
recordsDeleted: result.recordsDeleted,
|
||||||
|
recordsImported: result.recordsImported,
|
||||||
|
uniqueSourceNumbers: result.uniqueSourceNumbers,
|
||||||
|
errorCount: result.errors.length
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* TypeORM Data Source Configuration
|
* TypeORM Data Source Configuration
|
||||||
*
|
*
|
||||||
* Provides a centralized database connection for TypeORM entities.
|
* Provides a centralized database connection for TypeORM entities.
|
||||||
* Supports both MySQL and SQL Server based on configuration.
|
* Supports MySQL, SQL Server, and PostgreSQL based on configuration.
|
||||||
*
|
*
|
||||||
* Note: Configuration is now loaded from config.yaml via ConfigManager,
|
* Note: Configuration is now loaded from config.yaml via ConfigManager,
|
||||||
* not from environment variables.
|
* not from environment variables.
|
||||||
@@ -18,10 +18,17 @@ const log = createLogger('DataSource')
|
|||||||
/**
|
/**
|
||||||
* Get database type from config manager
|
* Get database type from config manager
|
||||||
*/
|
*/
|
||||||
function getDatabaseType(): 'mysql' | 'mssql' {
|
function getDatabaseType(): 'mysql' | 'mssql' | 'postgres' {
|
||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const dbType = configManager.getDatabaseType()
|
const dbType = configManager.getDatabaseType()
|
||||||
return dbType === 'sqlserver' ? 'mssql' : 'mysql'
|
switch (dbType) {
|
||||||
|
case 'sqlserver':
|
||||||
|
return 'mssql'
|
||||||
|
case 'postgresql':
|
||||||
|
return 'postgres'
|
||||||
|
default:
|
||||||
|
return 'mysql'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,6 +61,17 @@ function buildDataSourceOptions(): DataSourceOptions {
|
|||||||
},
|
},
|
||||||
...commonOptions
|
...commonOptions
|
||||||
} as DataSourceOptions
|
} as DataSourceOptions
|
||||||
|
} else if (type === 'postgres') {
|
||||||
|
const dbConfig = config.database.postgresql
|
||||||
|
return {
|
||||||
|
type: 'postgres',
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
username: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database,
|
||||||
|
...commonOptions
|
||||||
|
} as DataSourceOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
const dbConfig = config.database.mysql
|
const dbConfig = config.database.mysql
|
||||||
|
|||||||
28
src/main/services/database/dialects/index.ts
Normal file
28
src/main/services/database/dialects/index.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* SQL Dialect Factory
|
||||||
|
*
|
||||||
|
* Creates the appropriate SqlDialect implementation based on database type.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { DatabaseType } from '../../../types/database.types'
|
||||||
|
import type { SqlDialect } from '../../../types/sql-dialect.types'
|
||||||
|
|
||||||
|
import { MySqlDialect } from './mysql-dialect'
|
||||||
|
import { PostgreSqlDialect } from './postgresql-dialect'
|
||||||
|
import { SqlServerDialect } from './sqlserver-dialect'
|
||||||
|
|
||||||
|
export { MySqlDialect } from './mysql-dialect'
|
||||||
|
export { PostgreSqlDialect } from './postgresql-dialect'
|
||||||
|
export { SqlServerDialect } from './sqlserver-dialect'
|
||||||
|
export type { SqlDialect } from '../../../types/sql-dialect.types'
|
||||||
|
|
||||||
|
export function createDialect(type: DatabaseType): SqlDialect {
|
||||||
|
switch (type) {
|
||||||
|
case 'sqlserver':
|
||||||
|
return new SqlServerDialect()
|
||||||
|
case 'postgresql':
|
||||||
|
return new PostgreSqlDialect()
|
||||||
|
default:
|
||||||
|
return new MySqlDialect()
|
||||||
|
}
|
||||||
|
}
|
||||||
71
src/main/services/database/dialects/mysql-dialect.ts
Normal file
71
src/main/services/database/dialects/mysql-dialect.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* MySQL SQL Dialect Implementation
|
||||||
|
*
|
||||||
|
* Encapsulates MySQL-specific SQL syntax for:
|
||||||
|
* - Table name quoting (underscore-separated)
|
||||||
|
* - Positional parameter placeholders (?)
|
||||||
|
* - INSERT ... ON DUPLICATE KEY UPDATE upsert
|
||||||
|
* - LIMIT/OFFSET pagination
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { SqlDialect } from '../../../types/sql-dialect.types'
|
||||||
|
|
||||||
|
export class MySqlDialect implements SqlDialect {
|
||||||
|
readonly dbType = 'mysql' as const
|
||||||
|
|
||||||
|
quoteTableName(schema: string, table: string): string {
|
||||||
|
return `${schema}_${table}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
param(_index: number): string {
|
||||||
|
return '?'
|
||||||
|
}
|
||||||
|
|
||||||
|
params(count: number): string {
|
||||||
|
return Array.from({ length: count }, () => '?').join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTimestamp(): string {
|
||||||
|
return 'UTC_TIMESTAMP()'
|
||||||
|
}
|
||||||
|
|
||||||
|
upsert(params: {
|
||||||
|
table: string
|
||||||
|
keyColumns: string[]
|
||||||
|
allColumns: string[]
|
||||||
|
startParamIndex: number
|
||||||
|
}): { sql: string; nextParamIndex: number } {
|
||||||
|
const { table, keyColumns, allColumns, startParamIndex } = params
|
||||||
|
|
||||||
|
const columns = allColumns.join(', ')
|
||||||
|
const placeholders = allColumns.map(() => '?').join(', ')
|
||||||
|
|
||||||
|
const nonKeyColumns = allColumns.filter((col) => !keyColumns.includes(col))
|
||||||
|
const updateClause = nonKeyColumns.map((col) => `${col} = VALUES(${col})`).join(', ')
|
||||||
|
|
||||||
|
const sql = `INSERT INTO ${table} (${columns}) VALUES (${placeholders}) ON DUPLICATE KEY UPDATE ${updateClause}`
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql,
|
||||||
|
nextParamIndex: startParamIndex + allColumns.length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paginate(params: { sql: string; limit: number; offset?: number; paramIndex: number }): {
|
||||||
|
sql: string
|
||||||
|
nextParamIndex: number
|
||||||
|
} {
|
||||||
|
const { sql, limit, offset, paramIndex } = params
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql: `${sql} LIMIT ${limit} OFFSET ${offset ?? 0}`,
|
||||||
|
nextParamIndex: paramIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
maxBatchRows(_columnsPerRow: number): number {
|
||||||
|
return 1000
|
||||||
|
}
|
||||||
|
}
|
||||||
76
src/main/services/database/dialects/postgresql-dialect.ts
Normal file
76
src/main/services/database/dialects/postgresql-dialect.ts
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
/**
|
||||||
|
* PostgreSQL Dialect Implementation
|
||||||
|
*
|
||||||
|
* Encapsulates PostgreSQL-specific SQL syntax for:
|
||||||
|
* - Table name quoting (double-quoted "schema"."table")
|
||||||
|
* - Positional parameter placeholders ($1, $2, ...) — 1-based
|
||||||
|
* - INSERT ... ON CONFLICT ... DO UPDATE SET upsert
|
||||||
|
* - LIMIT/OFFSET pagination
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { SqlDialect } from '../../../types/sql-dialect.types'
|
||||||
|
|
||||||
|
export class PostgreSqlDialect implements SqlDialect {
|
||||||
|
readonly dbType = 'postgresql' as const
|
||||||
|
|
||||||
|
quoteTableName(schema: string, table: string): string {
|
||||||
|
return `"${schema}"."${table}"`
|
||||||
|
}
|
||||||
|
|
||||||
|
param(index: number): string {
|
||||||
|
return `$${index + 1}`
|
||||||
|
}
|
||||||
|
|
||||||
|
params(count: number): string {
|
||||||
|
return Array.from({ length: count }, (_, i) => `$${i + 1}`).join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTimestamp(): string {
|
||||||
|
return 'CURRENT_TIMESTAMP'
|
||||||
|
}
|
||||||
|
|
||||||
|
upsert(params: {
|
||||||
|
table: string
|
||||||
|
keyColumns: string[]
|
||||||
|
allColumns: string[]
|
||||||
|
startParamIndex: number
|
||||||
|
}): { sql: string; nextParamIndex: number } {
|
||||||
|
const { table, keyColumns, allColumns, startParamIndex } = params
|
||||||
|
|
||||||
|
const columns = allColumns.join(', ')
|
||||||
|
const placeholders = allColumns.map((_, i) => `$${startParamIndex + i + 1}`).join(', ')
|
||||||
|
|
||||||
|
const conflictKeys = keyColumns.map((col) => `"${col}"`).join(', ')
|
||||||
|
|
||||||
|
const nonKeyColumns = allColumns.filter((col) => !keyColumns.includes(col))
|
||||||
|
const updateSet = nonKeyColumns.map((col) => `"${col}" = EXCLUDED."${col}"`).join(', ')
|
||||||
|
|
||||||
|
const sql = [
|
||||||
|
`INSERT INTO ${table} (${columns}) VALUES (${placeholders})`,
|
||||||
|
`ON CONFLICT (${conflictKeys})`,
|
||||||
|
`DO UPDATE SET ${updateSet}`
|
||||||
|
].join(' ')
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql,
|
||||||
|
nextParamIndex: startParamIndex + allColumns.length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paginate(params: { sql: string; limit: number; offset?: number; paramIndex: number }): {
|
||||||
|
sql: string
|
||||||
|
nextParamIndex: number
|
||||||
|
} {
|
||||||
|
const { sql, limit, offset, paramIndex } = params
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql: `${sql} LIMIT ${limit} OFFSET ${offset ?? 0}`,
|
||||||
|
nextParamIndex: paramIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
maxBatchRows(_columnsPerRow: number): number {
|
||||||
|
return 1000
|
||||||
|
}
|
||||||
|
}
|
||||||
88
src/main/services/database/dialects/sqlserver-dialect.ts
Normal file
88
src/main/services/database/dialects/sqlserver-dialect.ts
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
/**
|
||||||
|
* SQL Server Dialect Implementation
|
||||||
|
*
|
||||||
|
* Encapsulates SQL Server-specific SQL syntax for:
|
||||||
|
* - Table name quoting (bracket notation [schema].[table])
|
||||||
|
* - Named parameter placeholders (@p0, @p1, ...)
|
||||||
|
* - MERGE ... USING upsert
|
||||||
|
* - OFFSET/FETCH pagination
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { SqlDialect } from '../../../types/sql-dialect.types'
|
||||||
|
|
||||||
|
export class SqlServerDialect implements SqlDialect {
|
||||||
|
readonly dbType = 'sqlserver' as const
|
||||||
|
|
||||||
|
quoteTableName(schema: string, table: string): string {
|
||||||
|
return `[${schema}].[${table}]`
|
||||||
|
}
|
||||||
|
|
||||||
|
param(index: number): string {
|
||||||
|
return `@p${index}`
|
||||||
|
}
|
||||||
|
|
||||||
|
params(count: number): string {
|
||||||
|
return Array.from({ length: count }, (_, i) => `@p${i}`).join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTimestamp(): string {
|
||||||
|
return 'SYSUTCDATETIME()'
|
||||||
|
}
|
||||||
|
|
||||||
|
upsert(params: {
|
||||||
|
table: string
|
||||||
|
keyColumns: string[]
|
||||||
|
allColumns: string[]
|
||||||
|
startParamIndex: number
|
||||||
|
}): { sql: string; nextParamIndex: number } {
|
||||||
|
const { table, keyColumns, allColumns, startParamIndex } = params
|
||||||
|
|
||||||
|
const valueParams = allColumns.map((_, i) => `@p${startParamIndex + i}`).join(', ')
|
||||||
|
const sourceColumns = allColumns.join(', ')
|
||||||
|
|
||||||
|
const joinCondition = keyColumns.map((col) => `target.${col} = source.${col}`).join(' AND ')
|
||||||
|
|
||||||
|
const nonKeyColumns = allColumns.filter((col) => !keyColumns.includes(col))
|
||||||
|
const updateSet = nonKeyColumns.map((col) => `target.${col} = source.${col}`).join(', ')
|
||||||
|
|
||||||
|
const insertColumns = allColumns.join(', ')
|
||||||
|
const insertValues = allColumns.map((col) => `source.${col}`).join(', ')
|
||||||
|
|
||||||
|
const sql = [
|
||||||
|
`MERGE ${table} AS target`,
|
||||||
|
`USING (VALUES (${valueParams})) AS source (${sourceColumns})`,
|
||||||
|
`ON ${joinCondition}`,
|
||||||
|
`WHEN MATCHED THEN UPDATE SET ${updateSet}`,
|
||||||
|
`WHEN NOT MATCHED THEN INSERT (${insertColumns}) VALUES (${insertValues});`
|
||||||
|
].join(' ')
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql,
|
||||||
|
nextParamIndex: startParamIndex + allColumns.length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paginate(params: { sql: string; limit: number; offset?: number; paramIndex: number }): {
|
||||||
|
sql: string
|
||||||
|
nextParamIndex: number
|
||||||
|
} {
|
||||||
|
const { sql, offset, paramIndex } = params
|
||||||
|
void params.limit // used by caller to push param values
|
||||||
|
|
||||||
|
if (offset !== undefined) {
|
||||||
|
return {
|
||||||
|
sql: `${sql} OFFSET @p${paramIndex} ROWS FETCH NEXT @p${paramIndex + 1} ROWS ONLY`,
|
||||||
|
nextParamIndex: paramIndex + 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sql: `${sql} OFFSET 0 ROWS FETCH NEXT @p${paramIndex} ROWS ONLY`,
|
||||||
|
nextParamIndex: paramIndex + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
maxBatchRows(columnsPerRow: number): number {
|
||||||
|
return Math.floor(2000 / columnsPerRow)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { create, type IDatabaseService } from './index'
|
import { create, type IDatabaseService } from './index'
|
||||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
import { createDialect, type SqlDialect } from './dialects'
|
||||||
|
import { createLogger, getRequestId, trackDuration } from '../logger'
|
||||||
|
|
||||||
const log = createLogger('DiscreteMaterialPlanDAO')
|
const log = createLogger('DiscreteMaterialPlanDAO')
|
||||||
|
|
||||||
@@ -53,8 +54,6 @@ export interface MaterialPlanRecord {
|
|||||||
* Configuration for DiscreteMaterialPlanData table
|
* Configuration for DiscreteMaterialPlanData table
|
||||||
*/
|
*/
|
||||||
export const DISCRETE_MATERIAL_PLAN_CONFIG = {
|
export const DISCRETE_MATERIAL_PLAN_CONFIG = {
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[DiscreteMaterialPlanData]',
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_DiscreteMaterialPlanData',
|
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
FACTORY: 'Factory',
|
FACTORY: 'Factory',
|
||||||
@@ -94,15 +93,20 @@ export const DISCRETE_MATERIAL_PLAN_CONFIG = {
|
|||||||
*/
|
*/
|
||||||
export class DiscreteMaterialPlanDAO {
|
export class DiscreteMaterialPlanDAO {
|
||||||
private dbService: IDatabaseService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
private getDialect(): SqlDialect {
|
||||||
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'DiscreteMaterialPlanData')
|
||||||
return isSqlServer
|
|
||||||
? DISCRETE_MATERIAL_PLAN_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: DISCRETE_MATERIAL_PLAN_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,15 +121,6 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
return this.dbService
|
return this.dbService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build placeholders for IN clause based on database type
|
|
||||||
*/
|
|
||||||
private buildPlaceholders(count: number, isSqlServer: boolean): string {
|
|
||||||
return isSqlServer
|
|
||||||
? Array.from({ length: count }, (_, idx) => `@p${idx}`).join(',')
|
|
||||||
: Array.from({ length: count }, () => '?').join(',')
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== QUERY ALL ====================
|
// ==================== QUERY ALL ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -219,13 +214,13 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
const batchSize = 1500
|
const batchSize = 1500
|
||||||
const allResults: any[] = []
|
const allResults: any[] = []
|
||||||
|
|
||||||
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
||||||
const batch = sourceNumbers.slice(i, i + batchSize)
|
const batch = sourceNumbers.slice(i, i + batchSize)
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT *
|
SELECT *
|
||||||
@@ -273,13 +268,13 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
const batchSize = 1500
|
const batchSize = 1500
|
||||||
const allResults: any[] = []
|
const allResults: any[] = []
|
||||||
|
|
||||||
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
||||||
const batch = sourceNumbers.slice(i, i + batchSize)
|
const batch = sourceNumbers.slice(i, i + batchSize)
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
WITH RankedRecords AS (
|
WITH RankedRecords AS (
|
||||||
@@ -338,9 +333,9 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -377,9 +372,9 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -418,13 +413,13 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
const batchSize = 1500
|
const batchSize = 1500
|
||||||
const allResults: any[] = []
|
const allResults: any[] = []
|
||||||
|
|
||||||
for (let i = 0; i < planNumbers.length; i += batchSize) {
|
for (let i = 0; i < planNumbers.length; i += batchSize) {
|
||||||
const batch = planNumbers.slice(i, i + batchSize)
|
const batch = planNumbers.slice(i, i + batchSize)
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT *
|
SELECT *
|
||||||
@@ -476,7 +471,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
const batchSize = 2000
|
const batchSize = 2000
|
||||||
|
|
||||||
// Get unique source numbers
|
// Get unique source numbers
|
||||||
@@ -495,7 +490,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
for (let i = 0; i < uniqueSourceNumbers.length; i += batchSize) {
|
for (let i = 0; i < uniqueSourceNumbers.length; i += batchSize) {
|
||||||
const batch = uniqueSourceNumbers.slice(i, i + batchSize)
|
const batch = uniqueSourceNumbers.slice(i, i + batchSize)
|
||||||
const batchNumber = Math.floor(i / batchSize) + 1
|
const batchNumber = Math.floor(i / batchSize) + 1
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
@@ -565,23 +560,19 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
// SQL Server has a limit of 2100 parameters per query
|
// SQL Server has a limit of 2100 parameters per query
|
||||||
// Each record has 28 columns, so max rows per batch = 2100 / 28 = 75
|
// Each record has 28 columns, so max rows per batch = 2100 / 28 = 75
|
||||||
// Leave some margin for query overhead
|
// Leave some margin for query overhead
|
||||||
const columnsPerRow = 28
|
const columnsPerRow = 28
|
||||||
const sqlServerMaxParams = 2000
|
const effectiveBatchSize = Math.min(batchSize, dialect.maxBatchRows(columnsPerRow))
|
||||||
const effectiveBatchSize = isSqlServer
|
|
||||||
? Math.min(batchSize, Math.floor(sqlServerMaxParams / columnsPerRow))
|
|
||||||
: batchSize
|
|
||||||
const totalBatches = Math.ceil(records.length / effectiveBatchSize)
|
const totalBatches = Math.ceil(records.length / effectiveBatchSize)
|
||||||
|
|
||||||
log.info('Batch insert started', {
|
log.info('Batch insert started', {
|
||||||
tableName,
|
tableName,
|
||||||
operationType: 'INSERT',
|
operationType: 'INSERT',
|
||||||
requestId: batchId,
|
requestId: batchId,
|
||||||
isSqlServer,
|
|
||||||
dbType: dbService.type,
|
dbType: dbService.type,
|
||||||
columnsPerRow,
|
columnsPerRow,
|
||||||
effectiveBatchSize,
|
effectiveBatchSize,
|
||||||
@@ -598,7 +589,6 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
dbService,
|
dbService,
|
||||||
tableName,
|
tableName,
|
||||||
batch,
|
batch,
|
||||||
isSqlServer,
|
|
||||||
batchId,
|
batchId,
|
||||||
batchNumber,
|
batchNumber,
|
||||||
totalBatches
|
totalBatches
|
||||||
@@ -643,7 +633,6 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
dbService: IDatabaseService,
|
dbService: IDatabaseService,
|
||||||
tableName: string,
|
tableName: string,
|
||||||
records: MaterialPlanRecord[],
|
records: MaterialPlanRecord[],
|
||||||
isSqlServer: boolean,
|
|
||||||
batchId: string,
|
batchId: string,
|
||||||
batchNumber: number,
|
batchNumber: number,
|
||||||
totalBatches: number
|
totalBatches: number
|
||||||
@@ -689,7 +678,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
const rowPlaceholders: string[] = []
|
const rowPlaceholders: string[] = []
|
||||||
|
|
||||||
records.forEach((record, rowIndex) => {
|
records.forEach((record, rowIndex) => {
|
||||||
const rowValues = this.buildRowValues(record, columns, rowIndex, isSqlServer, values)
|
const rowValues = this.buildRowValues(record, columns, rowIndex, values)
|
||||||
rowPlaceholders.push(`(${rowValues.join(',')})`)
|
rowPlaceholders.push(`(${rowValues.join(',')})`)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -718,10 +707,9 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
private async insertBatch(
|
private async insertBatch(
|
||||||
dbService: IDatabaseService,
|
dbService: IDatabaseService,
|
||||||
tableName: string,
|
tableName: string,
|
||||||
records: MaterialPlanRecord[],
|
records: MaterialPlanRecord[]
|
||||||
isSqlServer: boolean
|
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
return this.insertBatchWithTracking(dbService, tableName, records, isSqlServer, 'unknown', 1, 1)
|
return this.insertBatchWithTracking(dbService, tableName, records, 'unknown', 1, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -731,18 +719,14 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
record: MaterialPlanRecord,
|
record: MaterialPlanRecord,
|
||||||
columns: string[],
|
columns: string[],
|
||||||
_rowIndex: number,
|
_rowIndex: number,
|
||||||
isSqlServer: boolean,
|
|
||||||
values: any[]
|
values: any[]
|
||||||
): string[] {
|
): string[] {
|
||||||
|
const dialect = this.getDialect()
|
||||||
return columns.map((col) => {
|
return columns.map((col) => {
|
||||||
const value = this.getColumnValue(record, col)
|
const value = this.getColumnValue(record, col)
|
||||||
values.push(value)
|
values.push(value)
|
||||||
|
|
||||||
if (isSqlServer) {
|
return dialect.param(values.length - 1)
|
||||||
return `@p${values.length - 1}`
|
|
||||||
} else {
|
|
||||||
return '?'
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -839,9 +823,9 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT COUNT(*) as count
|
SELECT COUNT(*) as count
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -876,7 +860,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (sourceNumbers && sourceNumbers.length > 0) {
|
if (sourceNumbers && sourceNumbers.length > 0) {
|
||||||
const batchSize = 1500
|
const batchSize = 1500
|
||||||
@@ -884,7 +868,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
|
|
||||||
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
for (let i = 0; i < sourceNumbers.length; i += batchSize) {
|
||||||
const batch = sourceNumbers.slice(i, i + batchSize)
|
const batch = sourceNumbers.slice(i, i + batchSize)
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT DISTINCT MaterialName
|
SELECT DISTINCT MaterialName
|
||||||
@@ -975,6 +959,7 @@ export class DiscreteMaterialPlanDAO {
|
|||||||
if (this.dbService) {
|
if (this.dbService) {
|
||||||
await this.dbService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.dbService = null
|
this.dbService = null
|
||||||
|
this.dialect = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { create, type IDatabaseService } from './index'
|
import { create, type IDatabaseService } from './index'
|
||||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
import { createDialect, type SqlDialect } from './dialects'
|
||||||
|
import { createLogger, getRequestId, trackDuration } from '../logger'
|
||||||
import type {
|
import type {
|
||||||
OperationHistoryRecord,
|
OperationHistoryRecord,
|
||||||
BatchStats,
|
BatchStats,
|
||||||
@@ -36,8 +37,6 @@ function formatDateTime(value: unknown): string {
|
|||||||
* Configuration for ExtractorOperationHistory table
|
* Configuration for ExtractorOperationHistory table
|
||||||
*/
|
*/
|
||||||
export const EXTRACTOR_OPERATION_HISTORY_CONFIG = {
|
export const EXTRACTOR_OPERATION_HISTORY_CONFIG = {
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[ExtractorOperationHistory]',
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_ExtractorOperationHistory',
|
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
BATCH_ID: 'BatchId',
|
BATCH_ID: 'BatchId',
|
||||||
@@ -57,15 +56,20 @@ export const EXTRACTOR_OPERATION_HISTORY_CONFIG = {
|
|||||||
*/
|
*/
|
||||||
export class ExtractorOperationHistoryDAO {
|
export class ExtractorOperationHistoryDAO {
|
||||||
private dbService: IDatabaseService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
private getDialect(): SqlDialect {
|
||||||
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'ExtractorOperationHistory')
|
||||||
return isSqlServer
|
|
||||||
? EXTRACTOR_OPERATION_HISTORY_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: EXTRACTOR_OPERATION_HISTORY_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,15 +84,6 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
return this.dbService
|
return this.dbService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build placeholders for IN clause based on database type
|
|
||||||
*/
|
|
||||||
private buildPlaceholders(count: number, isSqlServer: boolean): string {
|
|
||||||
return isSqlServer
|
|
||||||
? Array.from({ length: count }, (_, idx) => `@p${idx}`).join(',')
|
|
||||||
: Array.from({ length: count }, () => '?').join(',')
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== INSERT ====================
|
// ==================== INSERT ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,7 +114,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
log.info('Batch records insertion started', {
|
log.info('Batch records insertion started', {
|
||||||
tableName,
|
tableName,
|
||||||
@@ -133,49 +128,26 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
|
|
||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
try {
|
try {
|
||||||
if (isSqlServer) {
|
const sqlString = `
|
||||||
const sqlString = `
|
INSERT INTO ${tableName}
|
||||||
INSERT INTO ${tableName}
|
(BatchId, UserId, Username, ProductionId, OrderNumber, OperationTime, Status)
|
||||||
(BatchId, UserId, Username, ProductionId, OrderNumber, OperationTime, Status)
|
VALUES
|
||||||
VALUES
|
(${dialect.param(0)}, ${dialect.param(1)}, ${dialect.param(2)}, ${dialect.param(3)}, ${dialect.param(4)}, ${dialect.currentTimestamp()}, 'pending')
|
||||||
(@p0, @p1, @p2, @p3, @p4, GETDATE(), 'pending')
|
`
|
||||||
`
|
await trackDuration(
|
||||||
await trackDuration(
|
async () =>
|
||||||
async () =>
|
await dbService.query(sqlString, [
|
||||||
await dbService.query(sqlString, [
|
batchId,
|
||||||
batchId,
|
userId,
|
||||||
userId,
|
username,
|
||||||
username,
|
record.productionId || null,
|
||||||
record.productionId || null,
|
record.orderNumber
|
||||||
record.orderNumber
|
]),
|
||||||
]),
|
{
|
||||||
{
|
operationName: 'ExtractorOperationHistoryDAO.insertBatchRecords',
|
||||||
operationName: 'ExtractorOperationHistoryDAO.insertBatchRecords',
|
context: { tableName, operationType: 'INSERT', batchId }
|
||||||
context: { tableName, operationType: 'INSERT', batchId }
|
}
|
||||||
}
|
)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName}
|
|
||||||
(BatchId, UserId, Username, ProductionId, OrderNumber, OperationTime, Status)
|
|
||||||
VALUES
|
|
||||||
(?, ?, ?, ?, ?, NOW(), 'pending')
|
|
||||||
`
|
|
||||||
await trackDuration(
|
|
||||||
async () =>
|
|
||||||
await dbService.query(sqlString, [
|
|
||||||
batchId,
|
|
||||||
userId,
|
|
||||||
username,
|
|
||||||
record.productionId || null,
|
|
||||||
record.orderNumber
|
|
||||||
]),
|
|
||||||
{
|
|
||||||
operationName: 'ExtractorOperationHistoryDAO.insertBatchRecords',
|
|
||||||
context: { tableName, operationType: 'INSERT', batchId }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Error inserting individual record', {
|
log.error('Error inserting individual record', {
|
||||||
tableName,
|
tableName,
|
||||||
@@ -221,16 +193,16 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
UPDATE ${tableName}
|
UPDATE ${tableName}
|
||||||
SET Status = ${isSqlServer ? '@p0' : '?'}
|
SET Status = ${dialect.param(0)}
|
||||||
WHERE BatchId = ${isSqlServer ? '@p1' : '?'}
|
WHERE BatchId = ${dialect.param(1)}
|
||||||
`
|
`
|
||||||
const params = [status, batchId]
|
const params = [status, batchId]
|
||||||
|
|
||||||
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
|
await trackDuration(async () => await dbService.query(sqlString, params), {
|
||||||
operationName: 'ExtractorOperationHistoryDAO.updateBatchStatus',
|
operationName: 'ExtractorOperationHistoryDAO.updateBatchStatus',
|
||||||
context: { tableName, operationType: 'UPDATE', batchId }
|
context: { tableName, operationType: 'UPDATE', batchId }
|
||||||
})
|
})
|
||||||
@@ -274,7 +246,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
let sqlString: string
|
let sqlString: string
|
||||||
let params: (string | number | null)[]
|
let params: (string | number | null)[]
|
||||||
@@ -282,20 +254,20 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
if (recordCount !== undefined) {
|
if (recordCount !== undefined) {
|
||||||
sqlString = `
|
sqlString = `
|
||||||
UPDATE ${tableName}
|
UPDATE ${tableName}
|
||||||
SET Status = ${isSqlServer ? '@p0' : '?'},
|
SET Status = ${dialect.param(0)},
|
||||||
ErrorMessage = ${isSqlServer ? '@p1' : '?'},
|
ErrorMessage = ${dialect.param(1)},
|
||||||
RecordCount = ${isSqlServer ? '@p2' : '?'}
|
RecordCount = ${dialect.param(2)}
|
||||||
WHERE BatchId = ${isSqlServer ? '@p3' : '?'}
|
WHERE BatchId = ${dialect.param(3)}
|
||||||
AND OrderNumber = ${isSqlServer ? '@p4' : '?'}
|
AND OrderNumber = ${dialect.param(4)}
|
||||||
`
|
`
|
||||||
params = [status, errorMessage || null, recordCount, batchId, orderNumber]
|
params = [status, errorMessage || null, recordCount, batchId, orderNumber]
|
||||||
} else {
|
} else {
|
||||||
sqlString = `
|
sqlString = `
|
||||||
UPDATE ${tableName}
|
UPDATE ${tableName}
|
||||||
SET Status = ${isSqlServer ? '@p0' : '?'},
|
SET Status = ${dialect.param(0)},
|
||||||
ErrorMessage = ${isSqlServer ? '@p1' : '?'}
|
ErrorMessage = ${dialect.param(1)}
|
||||||
WHERE BatchId = ${isSqlServer ? '@p2' : '?'}
|
WHERE BatchId = ${dialect.param(2)}
|
||||||
AND OrderNumber = ${isSqlServer ? '@p3' : '?'}
|
AND OrderNumber = ${dialect.param(3)}
|
||||||
`
|
`
|
||||||
params = [status, errorMessage || null, batchId, orderNumber]
|
params = [status, errorMessage || null, batchId, orderNumber]
|
||||||
}
|
}
|
||||||
@@ -331,7 +303,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
let sqlString = `
|
let sqlString = `
|
||||||
SELECT
|
SELECT
|
||||||
@@ -350,11 +322,10 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
const params: (number | string)[] = []
|
const params: (number | string)[] = []
|
||||||
|
|
||||||
if (userId !== undefined) {
|
if (userId !== undefined) {
|
||||||
sqlString += isSqlServer ? ` WHERE UserId = @p0 ` : ` WHERE UserId = ? `
|
sqlString += ` WHERE UserId = ${dialect.param(params.length)} `
|
||||||
params.push(userId)
|
params.push(userId)
|
||||||
} else if (options?.usernames && options.usernames.length > 0) {
|
} else if (options?.usernames && options.usernames.length > 0) {
|
||||||
const placeholders = this.buildPlaceholders(options.usernames.length, isSqlServer)
|
sqlString += ` WHERE Username IN (${dialect.params(options.usernames.length)}) `
|
||||||
sqlString += ` WHERE Username IN (${placeholders}) `
|
|
||||||
params.push(...options.usernames)
|
params.push(...options.usernames)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -367,24 +338,19 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
const safeLimit = Math.floor(options.limit)
|
const safeLimit = Math.floor(options.limit)
|
||||||
const safeOffset = options.offset !== undefined ? Math.floor(options.offset) : undefined
|
const safeOffset = options.offset !== undefined ? Math.floor(options.offset) : undefined
|
||||||
|
|
||||||
if (isSqlServer) {
|
const result = dialect.paginate({
|
||||||
const offsetIndex = params.length
|
sql: sqlString,
|
||||||
|
limit: safeLimit,
|
||||||
|
offset: safeOffset,
|
||||||
|
paramIndex: params.length
|
||||||
|
})
|
||||||
|
sqlString = result.sql
|
||||||
|
|
||||||
|
if (dialect.dbType === 'sqlserver') {
|
||||||
if (safeOffset !== undefined) {
|
if (safeOffset !== undefined) {
|
||||||
params.push(safeOffset)
|
params.push(safeOffset)
|
||||||
}
|
}
|
||||||
params.push(safeLimit)
|
params.push(safeLimit)
|
||||||
|
|
||||||
if (safeOffset !== undefined) {
|
|
||||||
sqlString += ` OFFSET @p${offsetIndex} ROWS FETCH NEXT @p${offsetIndex + 1} ROWS ONLY`
|
|
||||||
} else {
|
|
||||||
sqlString += ` OFFSET 0 ROWS FETCH NEXT @p${offsetIndex} ROWS ONLY`
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (safeOffset !== undefined) {
|
|
||||||
sqlString += ` LIMIT ${safeLimit} OFFSET ${safeOffset}`
|
|
||||||
} else {
|
|
||||||
sqlString += ` LIMIT ${safeLimit}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -425,9 +391,9 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT
|
SELECT
|
||||||
ID,
|
ID,
|
||||||
@@ -483,9 +449,9 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT
|
SELECT
|
||||||
BatchId,
|
BatchId,
|
||||||
@@ -554,7 +520,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
// First check if the batch exists and if the user has permission
|
// First check if the batch exists and if the user has permission
|
||||||
const batchStats = await this.getBatchStats(batchId)
|
const batchStats = await this.getBatchStats(batchId)
|
||||||
@@ -569,7 +535,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete the batch
|
// Delete the batch
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
WHERE BatchId = ${placeholder}
|
WHERE BatchId = ${placeholder}
|
||||||
@@ -612,9 +578,9 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
WHERE UserId = ${placeholder}
|
WHERE UserId = ${placeholder}
|
||||||
@@ -648,9 +614,9 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT COUNT(*) as count
|
SELECT COUNT(*) as count
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -684,7 +650,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
let sqlString = `
|
let sqlString = `
|
||||||
SELECT COUNT(DISTINCT BatchId) as count
|
SELECT COUNT(DISTINCT BatchId) as count
|
||||||
@@ -694,11 +660,10 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
const params: (number | string)[] = []
|
const params: (number | string)[] = []
|
||||||
|
|
||||||
if (userId !== undefined) {
|
if (userId !== undefined) {
|
||||||
sqlString += isSqlServer ? ` WHERE UserId = @p0 ` : ` WHERE UserId = ? `
|
sqlString += ` WHERE UserId = ${dialect.param(params.length)} `
|
||||||
params.push(userId)
|
params.push(userId)
|
||||||
} else if (usernames && usernames.length > 0) {
|
} else if (usernames && usernames.length > 0) {
|
||||||
const placeholders = this.buildPlaceholders(usernames.length, isSqlServer)
|
sqlString += ` WHERE Username IN (${dialect.params(usernames.length)}) `
|
||||||
sqlString += ` WHERE Username IN (${placeholders}) `
|
|
||||||
params.push(...usernames)
|
params.push(...usernames)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -725,6 +690,7 @@ export class ExtractorOperationHistoryDAO {
|
|||||||
if (this.dbService) {
|
if (this.dbService) {
|
||||||
await this.dbService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.dbService = null
|
this.dbService = null
|
||||||
|
this.dialect = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,19 @@
|
|||||||
* Database Factory
|
* Database Factory
|
||||||
*
|
*
|
||||||
* Creates and manages database service instances based on configuration.
|
* Creates and manages database service instances based on configuration.
|
||||||
* Supports both MySQL and SQL Server databases.
|
* Supports MySQL, SQL Server, and PostgreSQL databases.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ConfigManager } from '../config/config-manager'
|
import { ConfigManager } from '../config/config-manager'
|
||||||
import { MySqlService } from './mysql'
|
import { MySqlService } from './mysql'
|
||||||
import { SqlServerService } from './sql-server'
|
import { SqlServerService } from './sql-server'
|
||||||
|
import { PostgreSqlService } from './postgresql'
|
||||||
import type {
|
import type {
|
||||||
IDatabaseService,
|
IDatabaseService,
|
||||||
DatabaseType,
|
DatabaseType,
|
||||||
MySqlConfig,
|
MySqlConfig,
|
||||||
SqlServerConfig
|
SqlServerConfig,
|
||||||
|
PostgreSqlConfig
|
||||||
} from '../../types/database.types'
|
} from '../../types/database.types'
|
||||||
import { createLogger } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
|
|
||||||
@@ -65,6 +67,22 @@ export function createSqlServerConfig(): SqlServerConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create PostgreSQL configuration from config manager
|
||||||
|
*/
|
||||||
|
export function createPostgreSqlConfig(): PostgreSqlConfig {
|
||||||
|
const configManager = ConfigManager.getInstance()
|
||||||
|
const dbConfig = configManager.getConfig().database.postgresql
|
||||||
|
return {
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database,
|
||||||
|
maxPoolSize: dbConfig.maxPoolSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a database service instance
|
* Create a database service instance
|
||||||
*
|
*
|
||||||
@@ -86,7 +104,10 @@ export async function create(type?: DatabaseType): Promise<IDatabaseService> {
|
|||||||
// Create new instance
|
// Create new instance
|
||||||
let service: IDatabaseService
|
let service: IDatabaseService
|
||||||
|
|
||||||
if (dbType === 'sqlserver') {
|
if (dbType === 'postgresql') {
|
||||||
|
log.info('Creating PostgreSQL database service')
|
||||||
|
service = new PostgreSqlService(createPostgreSqlConfig())
|
||||||
|
} else if (dbType === 'sqlserver') {
|
||||||
log.info('Creating SQL Server database service')
|
log.info('Creating SQL Server database service')
|
||||||
service = new SqlServerService(createSqlServerConfig())
|
service = new SqlServerService(createSqlServerConfig())
|
||||||
} else {
|
} else {
|
||||||
@@ -175,10 +196,12 @@ export function isConnected(type?: DatabaseType): boolean {
|
|||||||
// Re-export types and services
|
// Re-export types and services
|
||||||
export { MySqlService } from './mysql'
|
export { MySqlService } from './mysql'
|
||||||
export { SqlServerService } from './sql-server'
|
export { SqlServerService } from './sql-server'
|
||||||
|
export { PostgreSqlService } from './postgresql'
|
||||||
export type {
|
export type {
|
||||||
IDatabaseService,
|
IDatabaseService,
|
||||||
DatabaseType,
|
DatabaseType,
|
||||||
QueryResult,
|
QueryResult,
|
||||||
MySqlConfig,
|
MySqlConfig,
|
||||||
SqlServerConfig
|
SqlServerConfig,
|
||||||
|
PostgreSqlConfig
|
||||||
} from '../../types/database.types'
|
} from '../../types/database.types'
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { create, type IDatabaseService } from './index'
|
import { create, type IDatabaseService } from './index'
|
||||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
import { createDialect, type SqlDialect } from './dialects'
|
||||||
|
import { createLogger, getRequestId, trackDuration } from '../logger'
|
||||||
|
|
||||||
const log = createLogger('MaterialsToBeDeletedDAO')
|
const log = createLogger('MaterialsToBeDeletedDAO')
|
||||||
|
|
||||||
@@ -44,8 +45,6 @@ export interface MaterialStats {
|
|||||||
* Configuration for MaterialsToBeDeleted table
|
* Configuration for MaterialsToBeDeleted table
|
||||||
*/
|
*/
|
||||||
export const MATERIALS_TO_BE_DELETED_CONFIG = {
|
export const MATERIALS_TO_BE_DELETED_CONFIG = {
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[MaterialsToBeDeleted]',
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_MaterialsToBeDeleted',
|
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
MATERIAL_CODE: 'MaterialCode',
|
MATERIAL_CODE: 'MaterialCode',
|
||||||
@@ -58,15 +57,20 @@ export const MATERIALS_TO_BE_DELETED_CONFIG = {
|
|||||||
*/
|
*/
|
||||||
export class MaterialsToBeDeletedDAO {
|
export class MaterialsToBeDeletedDAO {
|
||||||
private dbService: IDatabaseService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
private getDialect(): SqlDialect {
|
||||||
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'MaterialsToBeDeleted')
|
||||||
return isSqlServer
|
|
||||||
? MATERIALS_TO_BE_DELETED_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: MATERIALS_TO_BE_DELETED_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,15 +85,6 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
return this.dbService
|
return this.dbService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build placeholders for IN clause based on database type
|
|
||||||
*/
|
|
||||||
private buildPlaceholders(count: number, isSqlServer: boolean): string {
|
|
||||||
return isSqlServer
|
|
||||||
? Array.from({ length: count }, (_, idx) => `@p${idx}`).join(',')
|
|
||||||
: Array.from({ length: count }, () => '?').join(',')
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== UPSERT (MERGE) ====================
|
// ==================== UPSERT (MERGE) ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,33 +108,19 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const code = materialCode.trim()
|
const code = materialCode.trim()
|
||||||
const manager = managerName?.trim() || null
|
const manager = managerName?.trim() || null
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const { sql: sqlString } = dialect.upsert({
|
||||||
const sqlString = `
|
table: tableName,
|
||||||
MERGE ${tableName} AS target
|
keyColumns: ['MaterialCode'],
|
||||||
USING (VALUES (@p0, @p1)) AS source (MaterialCode, ManagerName)
|
allColumns: ['MaterialCode', 'ManagerName'],
|
||||||
ON target.MaterialCode = source.MaterialCode
|
startParamIndex: 0
|
||||||
WHEN MATCHED THEN UPDATE SET ManagerName = source.ManagerName
|
})
|
||||||
WHEN NOT MATCHED THEN INSERT (MaterialCode, ManagerName) VALUES (source.MaterialCode, source.ManagerName);
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [code, manager]), {
|
await trackDuration(async () => await dbService.query(sqlString, [code, manager]), {
|
||||||
operationName: 'MaterialsToBeDeletedDAO.upsertMaterial',
|
operationName: 'MaterialsToBeDeletedDAO.upsertMaterial',
|
||||||
context: { tableName, operationType: 'MERGE' }
|
context: { tableName, operationType: 'UPSERT' }
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName} (MaterialCode, ManagerName)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [code, manager]), {
|
|
||||||
operationName: 'MaterialsToBeDeletedDAO.upsertMaterial',
|
|
||||||
context: { tableName, operationType: 'INSERT' }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -176,7 +157,7 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
log.info('Batch upsert started', {
|
log.info('Batch upsert started', {
|
||||||
tableName,
|
tableName,
|
||||||
@@ -196,37 +177,20 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isSqlServer) {
|
const { sql: sqlString } = dialect.upsert({
|
||||||
const sqlString = `
|
table: tableName,
|
||||||
MERGE ${tableName} AS target
|
keyColumns: ['MaterialCode'],
|
||||||
USING (VALUES (@p0, @p1)) AS source (MaterialCode, ManagerName)
|
allColumns: ['MaterialCode', 'ManagerName'],
|
||||||
ON target.MaterialCode = source.MaterialCode
|
startParamIndex: 0
|
||||||
WHEN MATCHED THEN UPDATE SET ManagerName = source.ManagerName
|
})
|
||||||
WHEN NOT MATCHED THEN INSERT (MaterialCode, ManagerName) VALUES (source.MaterialCode, source.ManagerName);
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(
|
await trackDuration(
|
||||||
async () => await dbService.query(sqlString, [materialCode, managerName || null]),
|
async () => await dbService.query(sqlString, [materialCode, managerName || null]),
|
||||||
{
|
{
|
||||||
operationName: 'MaterialsToBeDeletedDAO.upsertBatch',
|
operationName: 'MaterialsToBeDeletedDAO.upsertBatch',
|
||||||
context: { tableName, operationType: 'MERGE', batchId }
|
context: { tableName, operationType: 'UPSERT', batchId }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName} (MaterialCode, ManagerName)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(
|
|
||||||
async () => await dbService.query(sqlString, [materialCode, managerName || null]),
|
|
||||||
{
|
|
||||||
operationName: 'MaterialsToBeDeletedDAO.upsertBatch',
|
|
||||||
context: { tableName, operationType: 'INSERT', batchId }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
stats.success++
|
stats.success++
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -276,25 +240,15 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const { sql: sqlString } = dialect.upsert({
|
||||||
const sqlString = `
|
table: tableName,
|
||||||
MERGE ${tableName} AS target
|
keyColumns: ['MaterialCode'],
|
||||||
USING (VALUES (@p0, @p1)) AS source (MaterialCode, ManagerName)
|
allColumns: ['MaterialCode', 'ManagerName'],
|
||||||
ON target.MaterialCode = source.MaterialCode
|
startParamIndex: 0
|
||||||
WHEN MATCHED THEN UPDATE SET ManagerName = source.ManagerName
|
})
|
||||||
WHEN NOT MATCHED THEN INSERT (MaterialCode, ManagerName) VALUES (source.MaterialCode, source.ManagerName);
|
await dbService.query(sqlString, [materialCode, managerName || null])
|
||||||
`
|
|
||||||
await dbService.query(sqlString, [materialCode, managerName || null])
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName} (MaterialCode, ManagerName)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
|
|
||||||
`
|
|
||||||
await dbService.query(sqlString, [materialCode, managerName || null])
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true }
|
return { success: true }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -387,9 +341,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT ID, MaterialCode, ManagerName
|
SELECT ID, MaterialCode, ManagerName
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -462,9 +416,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const code = materialCode.trim()
|
const code = materialCode.trim()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT ID, MaterialCode, ManagerName
|
SELECT ID, MaterialCode, ManagerName
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -509,9 +463,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const code = materialCode.trim()
|
const code = materialCode.trim()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
WHERE MaterialCode = ${placeholder}
|
WHERE MaterialCode = ${placeholder}
|
||||||
@@ -542,9 +496,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
WHERE ManagerName = ${placeholder}
|
WHERE ManagerName = ${placeholder}
|
||||||
@@ -612,7 +566,7 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
const totalBatches = Math.ceil(materialCodes.length / batchSize)
|
const totalBatches = Math.ceil(materialCodes.length / batchSize)
|
||||||
|
|
||||||
log.info('Batch delete started', {
|
log.info('Batch delete started', {
|
||||||
@@ -627,7 +581,7 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
for (let i = 0; i < materialCodes.length; i += batchSize) {
|
for (let i = 0; i < materialCodes.length; i += batchSize) {
|
||||||
const batch = materialCodes.slice(i, i + batchSize)
|
const batch = materialCodes.slice(i, i + batchSize)
|
||||||
const batchNumber = Math.floor(i / batchSize) + 1
|
const batchNumber = Math.floor(i / batchSize) + 1
|
||||||
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
|
const placeholders = dialect.params(batch.length)
|
||||||
|
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
DELETE FROM ${tableName}
|
DELETE FROM ${tableName}
|
||||||
@@ -688,9 +642,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const code = materialCode.trim()
|
const code = materialCode.trim()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT COUNT(*) as count
|
SELECT COUNT(*) as count
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -749,9 +703,9 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT COUNT(*) as count
|
SELECT COUNT(*) as count
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -845,6 +799,7 @@ export class MaterialsToBeDeletedDAO {
|
|||||||
if (this.dbService) {
|
if (this.dbService) {
|
||||||
await this.dbService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.dbService = null
|
this.dbService = null
|
||||||
|
this.dialect = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { create, type IDatabaseService } from './index'
|
import { create, type IDatabaseService } from './index'
|
||||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
import { createDialect, type SqlDialect } from './dialects'
|
||||||
|
import { createLogger, getRequestId, trackDuration } from '../logger'
|
||||||
|
|
||||||
const log = createLogger('MaterialsTypeToBeDeletedDAO')
|
const log = createLogger('MaterialsTypeToBeDeletedDAO')
|
||||||
|
|
||||||
@@ -32,8 +33,6 @@ export interface MaterialTypeBatchRequest {
|
|||||||
* Configuration for MaterialsTypeToBeDeleted table
|
* Configuration for MaterialsTypeToBeDeleted table
|
||||||
*/
|
*/
|
||||||
export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[MaterialsTypeToBeDeleted]',
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_MaterialsTypeToBeDeleted',
|
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
MATERIAL_NAME: 'MaterialName',
|
MATERIAL_NAME: 'MaterialName',
|
||||||
@@ -46,15 +45,20 @@ export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
|||||||
*/
|
*/
|
||||||
export class MaterialsTypeToBeDeletedDAO {
|
export class MaterialsTypeToBeDeletedDAO {
|
||||||
private dbService: IDatabaseService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
private getDialect(): SqlDialect {
|
||||||
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'MaterialsTypeToBeDeleted')
|
||||||
return isSqlServer
|
|
||||||
? MATERIALS_TYPE_TO_BE_DELETED_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: MATERIALS_TYPE_TO_BE_DELETED_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,9 +120,9 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT ID, MaterialName, ManagerName
|
SELECT ID, MaterialName, ManagerName
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -204,33 +208,19 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const name = materialName.trim()
|
const name = materialName.trim()
|
||||||
const manager = managerName?.trim() || null
|
const manager = managerName?.trim() || null
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const { sql: sqlString } = dialect.upsert({
|
||||||
const sqlString = `
|
table: tableName,
|
||||||
MERGE ${tableName} AS target
|
keyColumns: ['MaterialName'],
|
||||||
USING (VALUES (@p0, @p1)) AS source (MaterialName, ManagerName)
|
allColumns: ['MaterialName', 'ManagerName'],
|
||||||
ON target.MaterialName = source.MaterialName
|
startParamIndex: 0
|
||||||
WHEN MATCHED THEN UPDATE SET ManagerName = source.ManagerName
|
})
|
||||||
WHEN NOT MATCHED THEN INSERT (MaterialName, ManagerName) VALUES (source.MaterialName, source.ManagerName);
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
||||||
context: { tableName, operationType: 'MERGE' }
|
context: { tableName, operationType: 'UPSERT' }
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName} (MaterialName, ManagerName)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
|
||||||
context: { tableName, operationType: 'INSERT' }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -257,25 +247,16 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const name = materialName.trim()
|
const name = materialName.trim()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
let sqlString: string
|
let sqlString: string
|
||||||
let params: (string | null)[]
|
let params: (string | null)[]
|
||||||
|
|
||||||
if (managerName) {
|
if (managerName) {
|
||||||
const placeholder1 = isSqlServer ? '@p0' : '?'
|
sqlString = `DELETE FROM ${tableName} WHERE MaterialName = ${dialect.param(0)} AND ManagerName = ${dialect.param(1)}`
|
||||||
const placeholder2 = isSqlServer ? '@p1' : '?'
|
|
||||||
sqlString = `
|
|
||||||
DELETE FROM ${tableName}
|
|
||||||
WHERE MaterialName = ${placeholder1} AND ManagerName = ${placeholder2}
|
|
||||||
`
|
|
||||||
params = [name, managerName.trim()]
|
params = [name, managerName.trim()]
|
||||||
} else {
|
} else {
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
sqlString = `DELETE FROM ${tableName} WHERE MaterialName = ${dialect.param(0)}`
|
||||||
sqlString = `
|
|
||||||
DELETE FROM ${tableName}
|
|
||||||
WHERE MaterialName = ${placeholder}
|
|
||||||
`
|
|
||||||
params = [name]
|
params = [name]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,49 +295,27 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const sqlString = `
|
||||||
const sqlString = `
|
UPDATE ${tableName}
|
||||||
UPDATE ${tableName}
|
SET MaterialName = ${dialect.param(0)}, ManagerName = ${dialect.param(1)}
|
||||||
SET MaterialName = @p0, ManagerName = @p1
|
WHERE MaterialName = ${dialect.param(2)} AND ManagerName = ${dialect.param(3)}
|
||||||
WHERE MaterialName = @p2 AND ManagerName = @p3
|
`
|
||||||
`
|
const result = await trackDuration(
|
||||||
const result = await trackDuration(
|
async () =>
|
||||||
async () =>
|
await dbService.query(sqlString, [
|
||||||
await dbService.query(sqlString, [
|
newName.trim(),
|
||||||
newName.trim(),
|
newManager.trim(),
|
||||||
newManager.trim(),
|
oldName.trim(),
|
||||||
oldName.trim(),
|
oldManager.trim()
|
||||||
oldManager.trim()
|
]),
|
||||||
]),
|
{
|
||||||
{
|
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
context: { tableName, operationType: 'UPDATE' }
|
||||||
context: { tableName, operationType: 'UPDATE' }
|
}
|
||||||
}
|
)
|
||||||
)
|
return result.result.rowCount > 0
|
||||||
return result.result.rowCount > 0
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
UPDATE ${tableName}
|
|
||||||
SET MaterialName = ?, ManagerName = ?
|
|
||||||
WHERE MaterialName = ? AND ManagerName = ?
|
|
||||||
`
|
|
||||||
const result = await trackDuration(
|
|
||||||
async () =>
|
|
||||||
await dbService.query(sqlString, [
|
|
||||||
newName.trim(),
|
|
||||||
newManager.trim(),
|
|
||||||
oldName.trim(),
|
|
||||||
oldManager.trim()
|
|
||||||
]),
|
|
||||||
{
|
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
|
||||||
context: { tableName, operationType: 'UPDATE' }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return result.result.rowCount > 0
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Update material error', {
|
log.error('Update material error', {
|
||||||
tableName: this.getTableName(),
|
tableName: this.getTableName(),
|
||||||
@@ -456,6 +415,7 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
if (this.dbService) {
|
if (this.dbService) {
|
||||||
await this.dbService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.dbService = null
|
this.dbService = null
|
||||||
|
this.dialect = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
641
src/main/services/database/postgresql.ts
Normal file
641
src/main/services/database/postgresql.ts
Normal file
@@ -0,0 +1,641 @@
|
|||||||
|
import { Pool } from 'pg'
|
||||||
|
import type {
|
||||||
|
IDatabaseService,
|
||||||
|
DatabaseType,
|
||||||
|
QueryResult,
|
||||||
|
PostgreSqlConfig
|
||||||
|
} from '../../types/database.types'
|
||||||
|
import { createLogger, trackDuration } from '../logger'
|
||||||
|
|
||||||
|
const log = createLogger('PostgreSqlService')
|
||||||
|
|
||||||
|
export type { PostgreSqlConfig } from '../../types/database.types'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL keywords that should NOT be double-quoted during identifier preprocessing.
|
||||||
|
* PostgreSQL lowercases unquoted identifiers, but SSMA-migrated databases
|
||||||
|
* have uppercase column names that require double-quoting to preserve case.
|
||||||
|
*
|
||||||
|
* This list covers PostgreSQL reserved words across multiple categories:
|
||||||
|
* - DML (Data Manipulation Language)
|
||||||
|
* - DDL (Data Definition Language)
|
||||||
|
* - Window functions
|
||||||
|
* - CTEs (Common Table Expressions)
|
||||||
|
* - Advanced GROUP BY clauses
|
||||||
|
* - JSON operations
|
||||||
|
* - Type system
|
||||||
|
* - Table sampling
|
||||||
|
* - Transaction control
|
||||||
|
*/
|
||||||
|
const SQL_KEYWORDS = new Set([
|
||||||
|
// ==================== DML (Data Manipulation Language) ====================
|
||||||
|
'SELECT',
|
||||||
|
'FROM',
|
||||||
|
'WHERE',
|
||||||
|
'AND',
|
||||||
|
'OR',
|
||||||
|
'NOT',
|
||||||
|
'IN',
|
||||||
|
'IS',
|
||||||
|
'NULL',
|
||||||
|
'INSERT',
|
||||||
|
'INTO',
|
||||||
|
'VALUES',
|
||||||
|
'UPDATE',
|
||||||
|
'SET',
|
||||||
|
'DELETE',
|
||||||
|
|
||||||
|
// ==================== Ordering & Limiting ====================
|
||||||
|
'ORDER',
|
||||||
|
'BY',
|
||||||
|
'ASC',
|
||||||
|
'DESC',
|
||||||
|
'LIMIT',
|
||||||
|
'OFFSET',
|
||||||
|
'FETCH',
|
||||||
|
'NEXT',
|
||||||
|
'ROWS',
|
||||||
|
'ONLY',
|
||||||
|
|
||||||
|
// ==================== Joins ====================
|
||||||
|
'JOIN',
|
||||||
|
'LEFT',
|
||||||
|
'RIGHT',
|
||||||
|
'INNER',
|
||||||
|
'OUTER',
|
||||||
|
'CROSS',
|
||||||
|
'FULL',
|
||||||
|
'ON',
|
||||||
|
'NATURAL',
|
||||||
|
'LATERAL',
|
||||||
|
|
||||||
|
// ==================== Set Operations ====================
|
||||||
|
'UNION',
|
||||||
|
'ALL',
|
||||||
|
'INTERSECT',
|
||||||
|
'EXCEPT',
|
||||||
|
|
||||||
|
// ==================== Grouping & Aggregation ====================
|
||||||
|
'GROUP',
|
||||||
|
'HAVING',
|
||||||
|
'DISTINCT',
|
||||||
|
'GROUPING',
|
||||||
|
'SETS',
|
||||||
|
'ROLLUP',
|
||||||
|
'CUBE',
|
||||||
|
'FILTER',
|
||||||
|
'WITHIN',
|
||||||
|
|
||||||
|
// ==================== Window Functions ====================
|
||||||
|
'OVER',
|
||||||
|
'PARTITION',
|
||||||
|
'WINDOW',
|
||||||
|
'RANGE',
|
||||||
|
'UNBOUNDED',
|
||||||
|
'PRECEDING',
|
||||||
|
'FOLLOWING',
|
||||||
|
'CURRENT',
|
||||||
|
'ROW',
|
||||||
|
'GROUPS',
|
||||||
|
'EXCLUDE',
|
||||||
|
'TIES',
|
||||||
|
'RANK',
|
||||||
|
'DENSE_RANK',
|
||||||
|
'ROW_NUMBER',
|
||||||
|
'NTILE',
|
||||||
|
'LAG',
|
||||||
|
'LEAD',
|
||||||
|
'FIRST_VALUE',
|
||||||
|
'LAST_VALUE',
|
||||||
|
'NTH_VALUE',
|
||||||
|
|
||||||
|
// ==================== CTE (Common Table Expressions) ====================
|
||||||
|
'WITH',
|
||||||
|
'RECURSIVE',
|
||||||
|
'MATERIALIZED',
|
||||||
|
'SEARCH',
|
||||||
|
'CYCLE',
|
||||||
|
'PATH',
|
||||||
|
'ROOT',
|
||||||
|
'SIBLINGS',
|
||||||
|
|
||||||
|
// ==================== CASE Expressions ====================
|
||||||
|
'CASE',
|
||||||
|
'WHEN',
|
||||||
|
'THEN',
|
||||||
|
'ELSE',
|
||||||
|
'END',
|
||||||
|
|
||||||
|
// ==================== DDL (Data Definition Language) ====================
|
||||||
|
'CREATE',
|
||||||
|
'ALTER',
|
||||||
|
'DROP',
|
||||||
|
'TABLE',
|
||||||
|
'INDEX',
|
||||||
|
'COLUMN',
|
||||||
|
'ADD',
|
||||||
|
'MODIFY',
|
||||||
|
'RENAME',
|
||||||
|
'TO',
|
||||||
|
'GENERATED',
|
||||||
|
'ALWAYS',
|
||||||
|
'IDENTITY',
|
||||||
|
'INCLUDE',
|
||||||
|
'TEMP',
|
||||||
|
'TEMPORARY',
|
||||||
|
'UNLOGGED',
|
||||||
|
|
||||||
|
// ==================== PostgreSQL Specific - UPSERT/MERGE ====================
|
||||||
|
'CONFLICT',
|
||||||
|
'DO',
|
||||||
|
'NOTHING',
|
||||||
|
'EXCLUDED',
|
||||||
|
'RETURNING',
|
||||||
|
'MERGE',
|
||||||
|
'USING',
|
||||||
|
'MATCHED',
|
||||||
|
'TARGET',
|
||||||
|
'SOURCE',
|
||||||
|
|
||||||
|
// ==================== Aggregate Functions ====================
|
||||||
|
'COUNT',
|
||||||
|
'SUM',
|
||||||
|
'AVG',
|
||||||
|
'MIN',
|
||||||
|
'MAX',
|
||||||
|
'EXISTS',
|
||||||
|
'COALESCE',
|
||||||
|
'NULLIF',
|
||||||
|
'CAST',
|
||||||
|
'AS',
|
||||||
|
|
||||||
|
// ==================== JSON Operations ====================
|
||||||
|
'JSON',
|
||||||
|
'JSONB',
|
||||||
|
'JSON_ARRAY',
|
||||||
|
'JSON_OBJECT',
|
||||||
|
'JSON_AGG',
|
||||||
|
'JSONB_AGG',
|
||||||
|
'JSONB_OBJECT_AGG',
|
||||||
|
|
||||||
|
// ==================== Types & Casting ====================
|
||||||
|
'DECIMAL',
|
||||||
|
'NUMERIC',
|
||||||
|
'BOOLEAN',
|
||||||
|
'CHARACTER',
|
||||||
|
'VARYING',
|
||||||
|
'PRECISION',
|
||||||
|
'REAL',
|
||||||
|
'DOUBLE',
|
||||||
|
'FLOAT',
|
||||||
|
'TEXT',
|
||||||
|
'INTEGER',
|
||||||
|
'SERIAL',
|
||||||
|
'BIGINT',
|
||||||
|
'SMALLINT',
|
||||||
|
'DATE',
|
||||||
|
'TIME',
|
||||||
|
'TIMESTAMP',
|
||||||
|
'TIMESTAMPTZ',
|
||||||
|
'TIMEZONE',
|
||||||
|
'INTERVAL',
|
||||||
|
'BIGSERIAL',
|
||||||
|
'SMALLSERIAL',
|
||||||
|
|
||||||
|
// ==================== Table Sampling ====================
|
||||||
|
'TABLESAMPLE',
|
||||||
|
'BERNOULLI',
|
||||||
|
'SYSTEM',
|
||||||
|
'REPEATABLE',
|
||||||
|
'SEED',
|
||||||
|
|
||||||
|
// ==================== Transaction Control ====================
|
||||||
|
'BEGIN',
|
||||||
|
'COMMIT',
|
||||||
|
'ROLLBACK',
|
||||||
|
'SAVEPOINT',
|
||||||
|
'WORK',
|
||||||
|
'ISOLATION',
|
||||||
|
'LEVEL',
|
||||||
|
'READ',
|
||||||
|
'WRITE',
|
||||||
|
'COMMITTED',
|
||||||
|
'REPEATABLE',
|
||||||
|
'SERIALIZABLE',
|
||||||
|
|
||||||
|
// ==================== Types & Values ====================
|
||||||
|
'TRUE',
|
||||||
|
'FALSE',
|
||||||
|
'DEFAULT',
|
||||||
|
'PRIMARY',
|
||||||
|
'KEY',
|
||||||
|
'REFERENCES',
|
||||||
|
'FOREIGN',
|
||||||
|
'CONSTRAINT',
|
||||||
|
'UNIQUE',
|
||||||
|
'CHECK',
|
||||||
|
'NULLS',
|
||||||
|
'FIRST',
|
||||||
|
'LAST',
|
||||||
|
|
||||||
|
// ==================== Scalar & String Functions ====================
|
||||||
|
'UPPER',
|
||||||
|
'LOWER',
|
||||||
|
'TRIM',
|
||||||
|
'LTRIM',
|
||||||
|
'RTRIM',
|
||||||
|
'BTRIM',
|
||||||
|
'SUBSTRING',
|
||||||
|
'CONCAT',
|
||||||
|
'LENGTH',
|
||||||
|
'CHAR_LENGTH',
|
||||||
|
'CHARACTER_LENGTH',
|
||||||
|
'REPLACE',
|
||||||
|
'POSITION',
|
||||||
|
'OVERLAY',
|
||||||
|
'LPAD',
|
||||||
|
'RPAD',
|
||||||
|
'REPEAT',
|
||||||
|
'REVERSE',
|
||||||
|
'SPLIT_PART',
|
||||||
|
'INITCAP',
|
||||||
|
'NORMALIZE',
|
||||||
|
'CHR',
|
||||||
|
'ASCII',
|
||||||
|
'FORMAT',
|
||||||
|
|
||||||
|
// ==================== Numeric Functions ====================
|
||||||
|
'ABS',
|
||||||
|
'CEIL',
|
||||||
|
'CEILING',
|
||||||
|
'FLOOR',
|
||||||
|
'ROUND',
|
||||||
|
'POWER',
|
||||||
|
'SQRT',
|
||||||
|
'MOD',
|
||||||
|
'SIGN',
|
||||||
|
'TRUNC',
|
||||||
|
|
||||||
|
// ==================== Date/Time Functions ====================
|
||||||
|
'EXTRACT',
|
||||||
|
'DATE_TRUNC',
|
||||||
|
'TO_CHAR',
|
||||||
|
'TO_DATE',
|
||||||
|
'TO_TIMESTAMP',
|
||||||
|
'TO_NUMBER',
|
||||||
|
'AGE',
|
||||||
|
|
||||||
|
// ==================== Pattern Matching ====================
|
||||||
|
'BETWEEN',
|
||||||
|
'LIKE',
|
||||||
|
'ILIKE',
|
||||||
|
'SIMILAR',
|
||||||
|
'ESCAPE',
|
||||||
|
'ANY',
|
||||||
|
'SOME',
|
||||||
|
|
||||||
|
// ==================== Functions & Procedures ====================
|
||||||
|
'AFTER',
|
||||||
|
'BEFORE',
|
||||||
|
'EACH',
|
||||||
|
'STATEMENT',
|
||||||
|
'TRIGGER',
|
||||||
|
'FUNCTION',
|
||||||
|
'PROCEDURE',
|
||||||
|
'LANGUAGE',
|
||||||
|
'SQL',
|
||||||
|
'PLPGSQL',
|
||||||
|
'RETURNS',
|
||||||
|
'CALLED',
|
||||||
|
'STRICT',
|
||||||
|
'SECURITY',
|
||||||
|
'INVOKER',
|
||||||
|
'DEFINER',
|
||||||
|
'VOLATILE',
|
||||||
|
'STABLE',
|
||||||
|
'IMMUTABLE',
|
||||||
|
'PARALLEL',
|
||||||
|
'SAFE',
|
||||||
|
'RESTRICTED',
|
||||||
|
'UNSAFE',
|
||||||
|
|
||||||
|
// ==================== Utility Commands ====================
|
||||||
|
'CONCURRENTLY',
|
||||||
|
'REINDEX',
|
||||||
|
'VACUUM',
|
||||||
|
'ANALYZE',
|
||||||
|
'EXPLAIN',
|
||||||
|
'LOCAL',
|
||||||
|
'GLOBAL',
|
||||||
|
'ORDINALITY',
|
||||||
|
'FREEZE',
|
||||||
|
'VERBOSE',
|
||||||
|
'BUFFERS',
|
||||||
|
'FORMAT',
|
||||||
|
'XML',
|
||||||
|
'YAML',
|
||||||
|
|
||||||
|
// ==================== Additional Reserved Words ====================
|
||||||
|
'IF',
|
||||||
|
'CURRENT_TIMESTAMP',
|
||||||
|
'NOW',
|
||||||
|
'GETDATE'
|
||||||
|
])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare SQL for PostgreSQL execution by quoting unquoted identifiers.
|
||||||
|
*
|
||||||
|
* PostgreSQL lowercases unquoted identifiers, but SSMA-migrated tables
|
||||||
|
* have uppercase column names (e.g., "UserName", "ID") that require
|
||||||
|
* double-quoting to preserve case.
|
||||||
|
*
|
||||||
|
* This function:
|
||||||
|
* - Preserves string literals ('...')
|
||||||
|
* - Preserves already-quoted identifiers ("...")
|
||||||
|
* - Preserves parameter placeholders ($1, $2, ...)
|
||||||
|
* - Preserves SQL keywords
|
||||||
|
* - Double-quotes remaining identifiers
|
||||||
|
*/
|
||||||
|
export function prepareSql(sql: string): string {
|
||||||
|
const result: string[] = []
|
||||||
|
let i = 0
|
||||||
|
const len = sql.length
|
||||||
|
|
||||||
|
while (i < len) {
|
||||||
|
const ch = sql[i]
|
||||||
|
|
||||||
|
// Skip whitespace
|
||||||
|
if (/\s/.test(ch)) {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip single-line comments (--)
|
||||||
|
if (ch === '-' && i + 1 < len && sql[i + 1] === '-') {
|
||||||
|
while (i < len && sql[i] !== '\n') {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve string literals ('...')
|
||||||
|
if (ch === "'") {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
while (i < len) {
|
||||||
|
if (sql[i] === "'") {
|
||||||
|
result.push(sql[i++])
|
||||||
|
// Handle escaped quotes ('')
|
||||||
|
if (i < len && sql[i] === "'") {
|
||||||
|
result.push(sql[i++])
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve already-quoted identifiers ("...")
|
||||||
|
if (ch === '"') {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
while (i < len && sql[i] !== '"') {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
if (i < len) {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve parameter placeholders ($N)
|
||||||
|
if (ch === '$') {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
while (i < len && /\d/.test(sql[i])) {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve @param placeholders
|
||||||
|
if (ch === '@') {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
while (i < len && /\w/.test(sql[i])) {
|
||||||
|
result.push(sql[i++])
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve ? placeholders
|
||||||
|
if (ch === '?') {
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect word tokens (identifiers and keywords)
|
||||||
|
if (/[a-zA-Z_]/.test(ch)) {
|
||||||
|
let word = ''
|
||||||
|
while (i < len && /\w/.test(sql[i])) {
|
||||||
|
word += sql[i++]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's a SQL keyword (case-insensitive)
|
||||||
|
if (SQL_KEYWORDS.has(word.toUpperCase())) {
|
||||||
|
result.push(word)
|
||||||
|
} else {
|
||||||
|
// Quote the identifier to preserve case
|
||||||
|
result.push(`"${word}"`)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything else (operators, punctuation, numbers): pass through
|
||||||
|
result.push(ch)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.join('')
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PostgreSqlService implements IDatabaseService {
|
||||||
|
/** Database type identifier */
|
||||||
|
readonly type: DatabaseType = 'postgresql'
|
||||||
|
|
||||||
|
private pool: Pool | null = null
|
||||||
|
private config: PostgreSqlConfig
|
||||||
|
|
||||||
|
constructor(config: PostgreSqlConfig) {
|
||||||
|
this.config = config
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to PostgreSQL database
|
||||||
|
*/
|
||||||
|
async connect(): Promise<void> {
|
||||||
|
if (this.pool) {
|
||||||
|
log.warn('Already connected to PostgreSQL')
|
||||||
|
throw new Error('Already connected to PostgreSQL')
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.pool = new Pool({
|
||||||
|
host: this.config.host,
|
||||||
|
port: this.config.port,
|
||||||
|
user: this.config.user,
|
||||||
|
password: this.config.password,
|
||||||
|
database: this.config.database,
|
||||||
|
max: this.config.maxPoolSize ?? 10,
|
||||||
|
/**
|
||||||
|
* Connection timeout in milliseconds.
|
||||||
|
* Time to wait when connecting to PostgreSQL before failing.
|
||||||
|
* Prevents hanging during network issues or server overload.
|
||||||
|
*/
|
||||||
|
connectionTimeoutMillis: 10000,
|
||||||
|
/**
|
||||||
|
* PostgreSQL statement timeout in milliseconds.
|
||||||
|
* Limits execution time for individual SQL statements.
|
||||||
|
* Prevents long-running queries from blocking the connection pool.
|
||||||
|
*/
|
||||||
|
statement_timeout: 30000,
|
||||||
|
/**
|
||||||
|
* Idle connection timeout in milliseconds.
|
||||||
|
* Closes connections that have been idle for this duration.
|
||||||
|
* Frees up pool resources and prevents stale connections.
|
||||||
|
*/
|
||||||
|
idleTimeoutMillis: 30000,
|
||||||
|
/**
|
||||||
|
* Query timeout in milliseconds (pg driver level).
|
||||||
|
* Fallback protection to abort queries that exceed this duration.
|
||||||
|
* Should be longer than statement_timeout to allow PG to handle first.
|
||||||
|
*/
|
||||||
|
query_timeout: 60000
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test connection
|
||||||
|
const client = await this.pool.connect()
|
||||||
|
client.release()
|
||||||
|
|
||||||
|
log.info('Connected to PostgreSQL', {
|
||||||
|
host: this.config.host,
|
||||||
|
port: this.config.port,
|
||||||
|
database: this.config.database
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
this.pool = null
|
||||||
|
log.error('Failed to connect to PostgreSQL', {
|
||||||
|
host: this.config.host,
|
||||||
|
port: this.config.port,
|
||||||
|
database: this.config.database,
|
||||||
|
error
|
||||||
|
})
|
||||||
|
throw new Error(`Failed to connect to PostgreSQL: ${(error as Error).message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect from PostgreSQL database
|
||||||
|
*/
|
||||||
|
async disconnect(): Promise<void> {
|
||||||
|
if (!this.pool) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.pool.end()
|
||||||
|
this.pool = null
|
||||||
|
log.info('Disconnected from PostgreSQL')
|
||||||
|
} catch (error) {
|
||||||
|
log.error('Failed to disconnect from PostgreSQL', { error })
|
||||||
|
throw new Error(`Failed to disconnect from PostgreSQL: ${(error as Error).message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if connected to PostgreSQL database
|
||||||
|
*/
|
||||||
|
isConnected(): boolean {
|
||||||
|
return this.pool !== null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a query and return results
|
||||||
|
*/
|
||||||
|
async query(sql: string, params?: any[]): Promise<QueryResult> {
|
||||||
|
if (!this.pool) {
|
||||||
|
throw new Error('Not connected to PostgreSQL. Call connect() first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quote unquoted identifiers to preserve case for SSMA-migrated columns
|
||||||
|
const preparedSql = prepareSql(sql)
|
||||||
|
const sqlPreview = preparedSql.substring(0, 100)
|
||||||
|
const paramCount = params?.length ?? 0
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { result: queryResult } = await trackDuration(
|
||||||
|
async () => {
|
||||||
|
const result = await this.pool!.query(preparedSql, params)
|
||||||
|
|
||||||
|
// Extract column names from fields
|
||||||
|
const columns = result.fields ? result.fields.map((field) => field.name) : []
|
||||||
|
|
||||||
|
// Result rows
|
||||||
|
const rows = (result.rows as Record<string, unknown>[]) || []
|
||||||
|
const rowCount = result.rowCount ?? rows.length
|
||||||
|
|
||||||
|
return { rows, columns, rowCount }
|
||||||
|
},
|
||||||
|
{ operationName: 'PostgreSqlService.query' }
|
||||||
|
)
|
||||||
|
|
||||||
|
log.debug('Query executed', { sqlPreview, rowCount: queryResult.rowCount, paramCount })
|
||||||
|
return queryResult
|
||||||
|
} catch (error) {
|
||||||
|
log.error('PostgreSQL query failed', { sqlPreview, paramCount, error })
|
||||||
|
throw new Error(`PostgreSQL query failed: ${(error as Error).message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute multiple queries in a transaction
|
||||||
|
*/
|
||||||
|
async transaction(queries: { sql: string; params?: any[] }[]): Promise<void> {
|
||||||
|
if (!this.pool) {
|
||||||
|
throw new Error('Not connected to PostgreSQL. Call connect() first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryCount = queries.length
|
||||||
|
log.info('Transaction started', { queryCount })
|
||||||
|
|
||||||
|
const client = await this.pool.connect()
|
||||||
|
|
||||||
|
try {
|
||||||
|
await client.query('BEGIN')
|
||||||
|
|
||||||
|
for (let i = 0; i < queries.length; i++) {
|
||||||
|
const { sql, params } = queries[i]
|
||||||
|
const preparedSql = prepareSql(sql)
|
||||||
|
await client.query(preparedSql, params)
|
||||||
|
log.debug('Transaction query executed', {
|
||||||
|
index: i,
|
||||||
|
sqlPreview: preparedSql.substring(0, 100)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.query('COMMIT')
|
||||||
|
log.info('Transaction committed', { queryCount })
|
||||||
|
} catch (error) {
|
||||||
|
await client.query('ROLLBACK')
|
||||||
|
log.warn('Transaction rolled back', { queryCount, error })
|
||||||
|
throw new Error(`PostgreSQL transaction failed: ${(error as Error).message}`)
|
||||||
|
} finally {
|
||||||
|
client.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
import { ERP_LOCATORS } from './locators'
|
import { ERP_LOCATORS } from './locators'
|
||||||
import { ErpAuthService } from './erp-auth'
|
import { ErpAuthService } from './erp-auth'
|
||||||
import type { CleanerInput, CleanerResult, OrderCleanDetail } from '../../types/cleaner.types'
|
import { DeletionErrorCategory, DeletionOutcome } from '../../types/cleaner.types'
|
||||||
|
import type {
|
||||||
|
CleanerInput,
|
||||||
|
CleanerResult,
|
||||||
|
MaterialDeletionAttempt,
|
||||||
|
OrderCleanDetail
|
||||||
|
} from '../../types/cleaner.types'
|
||||||
import type { ErpSession } from '../../types/erp.types'
|
import type { ErpSession } from '../../types/erp.types'
|
||||||
import type { FrameLocator, Locator, Page } from 'playwright'
|
import type { FrameLocator, Locator, Page } from 'playwright'
|
||||||
import { createLogger, run, trackDuration } from '../logger'
|
import { createLogger, run, trackDuration } from '../logger'
|
||||||
@@ -190,7 +196,9 @@ export class CleanerService {
|
|||||||
errors: [],
|
errors: [],
|
||||||
details: [],
|
details: [],
|
||||||
retriedOrders: 0,
|
retriedOrders: 0,
|
||||||
successfulRetries: 0
|
successfulRetries: 0,
|
||||||
|
materialsFailed: 0,
|
||||||
|
uncertainDeletions: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalOrders = input.orderNumbers.length
|
const totalOrders = input.orderNumbers.length
|
||||||
@@ -291,13 +299,15 @@ export class CleanerService {
|
|||||||
result.ordersProcessed += 1
|
result.ordersProcessed += 1
|
||||||
result.materialsDeleted += detail.materialsDeleted
|
result.materialsDeleted += detail.materialsDeleted
|
||||||
result.materialsSkipped += detail.materialsSkipped
|
result.materialsSkipped += detail.materialsSkipped
|
||||||
|
result.materialsFailed += detail.materialsFailed
|
||||||
|
result.uncertainDeletions += detail.uncertainDeletions
|
||||||
})
|
})
|
||||||
|
|
||||||
const missingOrders = getMissingOrders(batchOrders, queriedOrderNumbersInBatch)
|
const missingOrders = getMissingOrders(batchOrders, queriedOrderNumbersInBatch)
|
||||||
for (const missingOrder of missingOrders) {
|
for (const missingOrder of missingOrders) {
|
||||||
const missingMessage = '订单未出现在查询结果中'
|
const missingMessage = '订单未出现在查询结果中'
|
||||||
result.errors.push(`Order ${missingOrder}: ${missingMessage}`)
|
result.errors.push(`Order ${missingOrder}: ${missingMessage}`)
|
||||||
result.details.push(this.createErrorDetail(missingOrder, missingMessage))
|
result.details.push(this.createErrorDetail(missingOrder, missingMessage, true))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -337,6 +347,8 @@ export class CleanerService {
|
|||||||
result.ordersProcessed += 1
|
result.ordersProcessed += 1
|
||||||
result.materialsDeleted += updatedDetail.materialsDeleted
|
result.materialsDeleted += updatedDetail.materialsDeleted
|
||||||
result.materialsSkipped += updatedDetail.materialsSkipped
|
result.materialsSkipped += updatedDetail.materialsSkipped
|
||||||
|
result.materialsFailed += updatedDetail.materialsFailed
|
||||||
|
result.uncertainDeletions += updatedDetail.uncertainDeletions
|
||||||
}
|
}
|
||||||
result.details[index] = updatedDetail
|
result.details[index] = updatedDetail
|
||||||
}
|
}
|
||||||
@@ -353,6 +365,8 @@ export class CleanerService {
|
|||||||
ordersProcessed: result.ordersProcessed,
|
ordersProcessed: result.ordersProcessed,
|
||||||
materialsDeleted: result.materialsDeleted,
|
materialsDeleted: result.materialsDeleted,
|
||||||
materialsSkipped: result.materialsSkipped,
|
materialsSkipped: result.materialsSkipped,
|
||||||
|
materialsFailed: result.materialsFailed,
|
||||||
|
uncertainDeletions: result.uncertainDeletions,
|
||||||
errorCount: result.errors.length,
|
errorCount: result.errors.length,
|
||||||
totalOrders,
|
totalOrders,
|
||||||
totalMaterials,
|
totalMaterials,
|
||||||
@@ -370,6 +384,7 @@ export class CleanerService {
|
|||||||
...(popupPage ? await capturePageContext(popupPage, undefined, 'cleaner.outerCatch') : {})
|
...(popupPage ? await capturePageContext(popupPage, undefined, 'cleaner.outerCatch') : {})
|
||||||
})
|
})
|
||||||
result.errors.push(`Clean failed: ${message}`)
|
result.errors.push(`Clean failed: ${message}`)
|
||||||
|
result.crashed = true
|
||||||
} finally {
|
} finally {
|
||||||
if (popupPage) {
|
if (popupPage) {
|
||||||
try {
|
try {
|
||||||
@@ -743,10 +758,14 @@ export class CleanerService {
|
|||||||
materialsSkipped: 0,
|
materialsSkipped: 0,
|
||||||
errors: [],
|
errors: [],
|
||||||
skippedMaterials: [],
|
skippedMaterials: [],
|
||||||
|
deletedMaterials: [],
|
||||||
retryCount: 0,
|
retryCount: 0,
|
||||||
retryAttempts: [],
|
retryAttempts: [],
|
||||||
retriedAt: undefined,
|
retriedAt: undefined,
|
||||||
retrySuccess: false
|
retrySuccess: false,
|
||||||
|
materialsFailed: 0,
|
||||||
|
failedMaterials: [],
|
||||||
|
uncertainDeletions: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5: Get material counts and status
|
// Step 5: Get material counts and status
|
||||||
@@ -879,33 +898,67 @@ export class CleanerService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (shouldDelete && !dryRun) {
|
if (shouldDelete && !dryRun) {
|
||||||
log.info('[物料操作] 执行删除操作', {
|
log.info('[物料操作] 执行删除操作(多信号验证)', {
|
||||||
orderNumber,
|
orderNumber,
|
||||||
materialIdx,
|
materialIdx,
|
||||||
materialCode,
|
materialCode,
|
||||||
materialName,
|
materialName,
|
||||||
rowNumber: currentRow,
|
rowNumber: currentRow,
|
||||||
|
materialCount: detailCount,
|
||||||
dryRun: false
|
dryRun: false
|
||||||
})
|
})
|
||||||
const oldRowNumber = currentRow
|
|
||||||
await deleteRowBtn.click()
|
|
||||||
|
|
||||||
const deleteSuccess = await this.waitForRowChange(childForm, oldRowNumber, 10000)
|
const currentMaterialCount = await this.readMaterialCount(detailInnerFrame)
|
||||||
|
const deleteResult = await this.deleteWithVerification({
|
||||||
|
childForm,
|
||||||
|
detailInnerFrame,
|
||||||
|
deleteRowBtn,
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
currentRowNumber: currentRow,
|
||||||
|
materialCountBefore: currentMaterialCount ?? detailCount
|
||||||
|
})
|
||||||
|
|
||||||
const deleteElapsed = Date.now() - materialStartTime
|
const deleteElapsed = Date.now() - materialStartTime
|
||||||
|
|
||||||
if (deleteSuccess) {
|
if (
|
||||||
|
deleteResult.outcome === DeletionOutcome.Success ||
|
||||||
|
deleteResult.outcome === DeletionOutcome.Uncertain
|
||||||
|
) {
|
||||||
detail.materialsDeleted += 1
|
detail.materialsDeleted += 1
|
||||||
log.info('[物料操作完成] 物料已成功删除', {
|
detail.deletedMaterials.push({
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
rowNumber: rowNumInt,
|
||||||
|
outcome: deleteResult.outcome
|
||||||
|
})
|
||||||
|
if (deleteResult.outcome === DeletionOutcome.Uncertain) {
|
||||||
|
detail.uncertainDeletions += 1
|
||||||
|
}
|
||||||
|
log.info('[物料操作完成] 物料删除结果', {
|
||||||
orderNumber,
|
orderNumber,
|
||||||
materialCode,
|
materialCode,
|
||||||
rowNumber: currentRow,
|
outcome: deleteResult.outcome,
|
||||||
|
attempts: deleteResult.attempts.length,
|
||||||
elapsedMs: deleteElapsed
|
elapsedMs: deleteElapsed
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
log.warn('[物料操作警告] 删除操作后行号未改变', {
|
detail.materialsFailed += 1
|
||||||
|
detail.failedMaterials.push({
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
rowNumber: rowNumInt,
|
||||||
|
attempts: deleteResult.attempts,
|
||||||
|
finalOutcome: deleteResult.outcome,
|
||||||
|
finalErrorCategory: deleteResult.errorCategory
|
||||||
|
})
|
||||||
|
log.error('[物料操作失败] 物料删除失败', {
|
||||||
orderNumber,
|
orderNumber,
|
||||||
materialCode,
|
materialCode,
|
||||||
rowNumber: currentRow,
|
outcome: deleteResult.outcome,
|
||||||
|
errorCategory: deleteResult.errorCategory,
|
||||||
|
errorMessage: deleteResult.errorMessage,
|
||||||
|
attempts: deleteResult.attempts.length,
|
||||||
elapsedMs: deleteElapsed
|
elapsedMs: deleteElapsed
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -937,11 +990,20 @@ export class CleanerService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.verbose('[物料判断] 物料不在删除清单中,跳过', {
|
// 物料不在删除清单中,记录为 skipped
|
||||||
|
detail.materialsSkipped += 1
|
||||||
|
detail.skippedMaterials.push({
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
rowNumber: rowNumInt,
|
||||||
|
reason: '不在删除清单中'
|
||||||
|
})
|
||||||
|
log.verbose('[物料跳过] 物料不在删除清单中', {
|
||||||
orderNumber,
|
orderNumber,
|
||||||
materialIdx,
|
materialIdx,
|
||||||
materialCode,
|
materialCode,
|
||||||
materialName
|
materialName,
|
||||||
|
rowNumber: rowNumInt
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1053,17 +1115,22 @@ export class CleanerService {
|
|||||||
return /^SC\d{14}$/.test(value)
|
return /^SC\d{14}$/.test(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
private createErrorDetail(orderNumber: string, message: string): OrderCleanDetail {
|
private createErrorDetail(orderNumber: string, message: string, notFound: boolean = false): OrderCleanDetail {
|
||||||
return {
|
return {
|
||||||
orderNumber,
|
orderNumber,
|
||||||
materialsDeleted: 0,
|
materialsDeleted: 0,
|
||||||
materialsSkipped: 0,
|
materialsSkipped: 0,
|
||||||
errors: [message],
|
errors: [message],
|
||||||
skippedMaterials: [],
|
skippedMaterials: [],
|
||||||
|
deletedMaterials: [],
|
||||||
retryCount: 0,
|
retryCount: 0,
|
||||||
retryAttempts: [],
|
retryAttempts: [],
|
||||||
retriedAt: undefined,
|
retriedAt: undefined,
|
||||||
retrySuccess: false
|
retrySuccess: false,
|
||||||
|
materialsFailed: 0,
|
||||||
|
failedMaterials: [],
|
||||||
|
uncertainDeletions: 0,
|
||||||
|
notFound
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1105,6 +1172,286 @@ export class CleanerService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Deletion verification constants ---
|
||||||
|
private static readonly MATERIAL_RETRY_MAX_ATTEMPTS = 3
|
||||||
|
private static readonly MATERIAL_RETRY_DELAY_MS = 1000
|
||||||
|
private static readonly VERIFICATION_TIMEOUT_MS = 8000
|
||||||
|
private static readonly ERROR_CHECK_WINDOW_MS = 2000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read current material count from the "详细信息 (N)" text.
|
||||||
|
*/
|
||||||
|
private async readMaterialCount(
|
||||||
|
detailInnerFrame: FrameLocator | Locator
|
||||||
|
): Promise<number | null> {
|
||||||
|
try {
|
||||||
|
const text = await detailInnerFrame.getByText(/^详细信息 \(\d+\)$/).innerText()
|
||||||
|
const match = text.match(/\((\d+)\)/)
|
||||||
|
return match ? parseInt(match[1], 10) : null
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for ERP error messages or confirm dialogs.
|
||||||
|
*/
|
||||||
|
private async checkErpMessages(
|
||||||
|
container: FrameLocator | Locator | Page
|
||||||
|
): Promise<{ hasError: boolean; errorText?: string; hasConfirmDialog: boolean }> {
|
||||||
|
try {
|
||||||
|
const errorLocator = container.locator(ERP_LOCATORS.common.errorMessage)
|
||||||
|
const hasError = await errorLocator.isVisible().catch(() => false)
|
||||||
|
let errorText: string | undefined
|
||||||
|
if (hasError) {
|
||||||
|
errorText = (await errorLocator.textContent().catch(() => undefined)) ?? undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const confirmLocator = container.locator(ERP_LOCATORS.common.confirmDialog)
|
||||||
|
const hasConfirmDialog = await confirmLocator.isVisible().catch(() => false)
|
||||||
|
|
||||||
|
return { hasError, errorText, hasConfirmDialog }
|
||||||
|
} catch {
|
||||||
|
return { hasError: false, hasConfirmDialog: false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-handle a confirm dialog by clicking the confirm button.
|
||||||
|
*/
|
||||||
|
private async handleConfirmDialog(container: FrameLocator | Locator | Page): Promise<void> {
|
||||||
|
try {
|
||||||
|
const confirmBtn = container.locator(ERP_LOCATORS.common.confirmButton).first()
|
||||||
|
if (await confirmBtn.isVisible().catch(() => false)) {
|
||||||
|
await confirmBtn.click()
|
||||||
|
log.debug('[确认对话框] 已自动点击确定')
|
||||||
|
await this.delay(500)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
log.warn('[确认对话框] 处理确认对话框失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pure logic: evaluate deletion signals and determine outcome.
|
||||||
|
* No Playwright dependencies — easy to unit test.
|
||||||
|
*/
|
||||||
|
evaluateDeletionSignals(params: {
|
||||||
|
rowChanged: boolean
|
||||||
|
countDecreased: boolean | null
|
||||||
|
hasError: boolean
|
||||||
|
errorText?: string
|
||||||
|
}): {
|
||||||
|
outcome: DeletionOutcome
|
||||||
|
errorCategory?: DeletionErrorCategory
|
||||||
|
errorMessage?: string
|
||||||
|
} {
|
||||||
|
const { rowChanged, countDecreased, hasError, errorText } = params
|
||||||
|
|
||||||
|
if (hasError) {
|
||||||
|
return {
|
||||||
|
outcome: DeletionOutcome.FailedErpError,
|
||||||
|
errorCategory: DeletionErrorCategory.ErpRejection,
|
||||||
|
errorMessage: errorText || 'ERP returned an error'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rowChanged && countDecreased !== false) {
|
||||||
|
// countDecreased === true or null (unreadable) — both acceptable
|
||||||
|
return { outcome: DeletionOutcome.Success }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rowChanged && countDecreased === false) {
|
||||||
|
return { outcome: DeletionOutcome.Uncertain }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rowChanged && countDecreased === true) {
|
||||||
|
return { outcome: DeletionOutcome.Success }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Neither row changed nor count decreased
|
||||||
|
return {
|
||||||
|
outcome: DeletionOutcome.FailedNoChange,
|
||||||
|
errorCategory: DeletionErrorCategory.Unknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Core method: delete a single material with multi-signal verification and retry.
|
||||||
|
*/
|
||||||
|
private async deleteWithVerification(params: {
|
||||||
|
childForm: FrameLocator | Locator
|
||||||
|
detailInnerFrame: FrameLocator | Locator
|
||||||
|
deleteRowBtn: Locator
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
currentRowNumber: string
|
||||||
|
materialCountBefore: number
|
||||||
|
maxAttempts?: number
|
||||||
|
attemptDelayMs?: number
|
||||||
|
}): Promise<{
|
||||||
|
outcome: DeletionOutcome
|
||||||
|
errorCategory?: DeletionErrorCategory
|
||||||
|
errorMessage?: string
|
||||||
|
attempts: MaterialDeletionAttempt[]
|
||||||
|
}> {
|
||||||
|
const {
|
||||||
|
childForm,
|
||||||
|
detailInnerFrame,
|
||||||
|
deleteRowBtn,
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
currentRowNumber,
|
||||||
|
materialCountBefore,
|
||||||
|
maxAttempts = CleanerService.MATERIAL_RETRY_MAX_ATTEMPTS,
|
||||||
|
attemptDelayMs = CleanerService.MATERIAL_RETRY_DELAY_MS
|
||||||
|
} = params
|
||||||
|
|
||||||
|
const attempts: MaterialDeletionAttempt[] = []
|
||||||
|
|
||||||
|
for (let attemptNum = 1; attemptNum <= maxAttempts; attemptNum++) {
|
||||||
|
const attemptStart = Date.now()
|
||||||
|
|
||||||
|
// Check button state before clicking
|
||||||
|
const btnEnabled = await this.isButtonEnabled(deleteRowBtn)
|
||||||
|
if (!btnEnabled) {
|
||||||
|
attempts.push({
|
||||||
|
attempt: attemptNum,
|
||||||
|
outcome: DeletionOutcome.FailedButtonDisabled,
|
||||||
|
errorCategory: DeletionErrorCategory.UiUnexpected,
|
||||||
|
errorMessage: '删行按钮不可用',
|
||||||
|
rowNumberBefore: currentRowNumber,
|
||||||
|
rowNumberAfter: currentRowNumber,
|
||||||
|
materialCountBefore,
|
||||||
|
materialCountAfter: materialCountBefore,
|
||||||
|
timestamp: attemptStart,
|
||||||
|
durationMs: Date.now() - attemptStart
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
outcome: DeletionOutcome.FailedButtonDisabled,
|
||||||
|
errorCategory: DeletionErrorCategory.UiUnexpected,
|
||||||
|
errorMessage: '删行按钮不可用',
|
||||||
|
attempts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Click delete button
|
||||||
|
await deleteRowBtn.click()
|
||||||
|
|
||||||
|
// Check for ERP messages (error / confirm dialog)
|
||||||
|
const msgCheck = await this.checkErpMessages(detailInnerFrame)
|
||||||
|
if (msgCheck.hasConfirmDialog) {
|
||||||
|
await this.handleConfirmDialog(detailInnerFrame)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msgCheck.hasError) {
|
||||||
|
const attempt: MaterialDeletionAttempt = {
|
||||||
|
attempt: attemptNum,
|
||||||
|
outcome: DeletionOutcome.FailedErpError,
|
||||||
|
errorCategory: DeletionErrorCategory.ErpRejection,
|
||||||
|
errorMessage: msgCheck.errorText,
|
||||||
|
rowNumberBefore: currentRowNumber,
|
||||||
|
rowNumberAfter: currentRowNumber,
|
||||||
|
materialCountBefore,
|
||||||
|
materialCountAfter: materialCountBefore,
|
||||||
|
timestamp: attemptStart,
|
||||||
|
durationMs: Date.now() - attemptStart
|
||||||
|
}
|
||||||
|
attempts.push(attempt)
|
||||||
|
// No retry for erp_rejection
|
||||||
|
return {
|
||||||
|
outcome: DeletionOutcome.FailedErpError,
|
||||||
|
errorCategory: DeletionErrorCategory.ErpRejection,
|
||||||
|
errorMessage: msgCheck.errorText,
|
||||||
|
attempts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait and poll for verification signals
|
||||||
|
const verificationStart = Date.now()
|
||||||
|
const timeout = CleanerService.VERIFICATION_TIMEOUT_MS
|
||||||
|
let rowNumberAfter = currentRowNumber
|
||||||
|
let materialCountAfter: number | null = materialCountBefore
|
||||||
|
|
||||||
|
while (Date.now() - verificationStart < timeout) {
|
||||||
|
rowNumberAfter = await this.getInputValue(childForm, /^行号$/)
|
||||||
|
materialCountAfter = await this.readMaterialCount(detailInnerFrame)
|
||||||
|
|
||||||
|
const rowChanged = rowNumberAfter !== currentRowNumber
|
||||||
|
const countDecreased =
|
||||||
|
materialCountAfter !== null ? materialCountAfter < materialCountBefore : null
|
||||||
|
|
||||||
|
if (rowChanged || countDecreased === true) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.delay(300)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final read if we didn't update in the loop
|
||||||
|
if (rowNumberAfter === currentRowNumber && materialCountAfter === materialCountBefore) {
|
||||||
|
rowNumberAfter = await this.getInputValue(childForm, /^行号$/)
|
||||||
|
materialCountAfter = await this.readMaterialCount(detailInnerFrame)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rowChanged = rowNumberAfter !== currentRowNumber
|
||||||
|
const countDecreased =
|
||||||
|
materialCountAfter !== null ? materialCountAfter < materialCountBefore : null
|
||||||
|
|
||||||
|
const evaluation = this.evaluateDeletionSignals({
|
||||||
|
rowChanged,
|
||||||
|
countDecreased,
|
||||||
|
hasError: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const attempt: MaterialDeletionAttempt = {
|
||||||
|
attempt: attemptNum,
|
||||||
|
outcome: evaluation.outcome,
|
||||||
|
errorCategory: evaluation.errorCategory,
|
||||||
|
errorMessage: evaluation.errorMessage,
|
||||||
|
rowNumberBefore: currentRowNumber,
|
||||||
|
rowNumberAfter,
|
||||||
|
materialCountBefore,
|
||||||
|
materialCountAfter: materialCountAfter ?? materialCountBefore,
|
||||||
|
timestamp: attemptStart,
|
||||||
|
durationMs: Date.now() - attemptStart
|
||||||
|
}
|
||||||
|
attempts.push(attempt)
|
||||||
|
|
||||||
|
if (
|
||||||
|
evaluation.outcome === DeletionOutcome.Success ||
|
||||||
|
evaluation.outcome === DeletionOutcome.Uncertain
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
outcome: evaluation.outcome,
|
||||||
|
errorCategory: evaluation.errorCategory,
|
||||||
|
errorMessage: evaluation.errorMessage,
|
||||||
|
attempts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retryable failure — wait before next attempt
|
||||||
|
if (attemptNum < maxAttempts) {
|
||||||
|
log.info('[物料删除重试] 等待后重试', {
|
||||||
|
materialCode,
|
||||||
|
materialName,
|
||||||
|
attempt: attemptNum,
|
||||||
|
nextAttempt: attemptNum + 1,
|
||||||
|
delayMs: attemptDelayMs
|
||||||
|
})
|
||||||
|
await this.delay(attemptDelayMs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All attempts exhausted
|
||||||
|
return {
|
||||||
|
outcome: DeletionOutcome.FailedNoChange,
|
||||||
|
errorCategory: DeletionErrorCategory.VerificationTimeout,
|
||||||
|
errorMessage: `${maxAttempts} 次尝试后仍无法确认删除`,
|
||||||
|
attempts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async waitForRowChange(
|
private async waitForRowChange(
|
||||||
childForm: FrameLocator | Locator,
|
childForm: FrameLocator | Locator,
|
||||||
oldRowNumber: string,
|
oldRowNumber: string,
|
||||||
|
|||||||
@@ -58,22 +58,33 @@ export class OrderNumberResolver {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get table name based on database type
|
* Get table name based on database type
|
||||||
* Converts MySQL schema_tablename format to SQL Server [schema].[tablename] format
|
* Converts schema_tablename format to database-specific quoting:
|
||||||
* e.g., productionContractData_26年压力表合同数据 -> [productionContractData].[26年压力表合同数据]
|
* - SQL Server: [schema].[tablename]
|
||||||
* dbo_MaterialsToBeDeleted -> [dbo].[MaterialsToBeDeleted]
|
* - PostgreSQL: "schema"."tablename"
|
||||||
|
* - MySQL: schema_tablename (as-is)
|
||||||
|
* e.g., productionContractData_26年压力表合同数据 ->
|
||||||
|
* SQL Server: [productionContractData].[26年压力表合同数据]
|
||||||
|
* PostgreSQL: "productionContractData"."26年压力表合同数据"
|
||||||
|
* MySQL: productionContractData_26年压力表合同数据
|
||||||
*/
|
*/
|
||||||
private getTableName(tableName: string): string {
|
private getTableName(tableName: string): string {
|
||||||
if (this.dbService.type === 'sqlserver') {
|
if (this.dbService.type === 'sqlserver' || this.dbService.type === 'postgresql') {
|
||||||
// Find the FIRST underscore to split schema and table name
|
// Find the FIRST underscore to split schema and table name
|
||||||
// This handles patterns like: schema_tablename
|
// This handles patterns like: schema_tablename
|
||||||
const firstUnderscoreIndex = tableName.indexOf('_')
|
const firstUnderscoreIndex = tableName.indexOf('_')
|
||||||
if (firstUnderscoreIndex > 0) {
|
if (firstUnderscoreIndex > 0) {
|
||||||
const schema = tableName.substring(0, firstUnderscoreIndex)
|
const schema = tableName.substring(0, firstUnderscoreIndex)
|
||||||
const actualTableName = tableName.substring(firstUnderscoreIndex + 1)
|
const actualTableName = tableName.substring(firstUnderscoreIndex + 1)
|
||||||
return `[${schema}].[${actualTableName}]`
|
if (this.dbService.type === 'sqlserver') {
|
||||||
|
return `[${schema}].[${actualTableName}]`
|
||||||
|
}
|
||||||
|
return `"${schema}"."${actualTableName}"`
|
||||||
}
|
}
|
||||||
// If no underscore found, default to dbo schema
|
// If no underscore found, default schema
|
||||||
return `[dbo].[${tableName}]`
|
if (this.dbService.type === 'sqlserver') {
|
||||||
|
return `[dbo].[${tableName}]`
|
||||||
|
}
|
||||||
|
return `"public"."${tableName}"`
|
||||||
}
|
}
|
||||||
return tableName
|
return tableName
|
||||||
}
|
}
|
||||||
@@ -107,6 +118,12 @@ export class OrderNumberResolver {
|
|||||||
// 使用 COLLATE 指定不区分大小写的排序规则
|
// 使用 COLLATE 指定不区分大小写的排序规则
|
||||||
sql = `SELECT TOP 1 [${dbConfig.FIELD_ORDER_NUMBER}] FROM ${tableName} WHERE [${dbConfig.FIELD_PRODUCTION_ID}] COLLATE SQL_Latin1_General_CP1_CI_AS = @p0`
|
sql = `SELECT TOP 1 [${dbConfig.FIELD_ORDER_NUMBER}] FROM ${tableName} WHERE [${dbConfig.FIELD_PRODUCTION_ID}] COLLATE SQL_Latin1_General_CP1_CI_AS = @p0`
|
||||||
params = [productionId]
|
params = [productionId]
|
||||||
|
} else if (this.dbService.type === 'postgresql') {
|
||||||
|
// PostgreSQL: 使用双引号保护中文标识符,UPPER 实现不区分大小写
|
||||||
|
// prepareSql() 会保留已双引号包裹的标识符
|
||||||
|
// 注意:getTableName() 已返回带双引号的表名,不应再加引号
|
||||||
|
sql = `SELECT DISTINCT "${dbConfig.FIELD_PRODUCTION_ID}", "${dbConfig.FIELD_ORDER_NUMBER}" FROM ${tableName} WHERE UPPER("${dbConfig.FIELD_PRODUCTION_ID}") = UPPER($1) LIMIT 1`
|
||||||
|
params = [productionId]
|
||||||
} else {
|
} else {
|
||||||
// MySQL 默认不区分大小写,但显式使用 UPPER 确保一致性
|
// MySQL 默认不区分大小写,但显式使用 UPPER 确保一致性
|
||||||
sql = `SELECT \`${dbConfig.FIELD_ORDER_NUMBER}\` FROM \`${tableName}\` WHERE UPPER(\`${dbConfig.FIELD_PRODUCTION_ID}\`) = UPPER(?) LIMIT 1`
|
sql = `SELECT \`${dbConfig.FIELD_ORDER_NUMBER}\` FROM \`${tableName}\` WHERE UPPER(\`${dbConfig.FIELD_PRODUCTION_ID}\`) = UPPER(?) LIMIT 1`
|
||||||
@@ -155,6 +172,11 @@ export class OrderNumberResolver {
|
|||||||
// P0: Use DISTINCT to prevent duplicates from one-to-many relationships
|
// P0: Use DISTINCT to prevent duplicates from one-to-many relationships
|
||||||
// 使用 COLLATE 指定不区分大小写的排序规则
|
// 使用 COLLATE 指定不区分大小写的排序规则
|
||||||
sql = `SELECT DISTINCT [${dbConfig.FIELD_PRODUCTION_ID}], [${dbConfig.FIELD_ORDER_NUMBER}] FROM ${tableName} WHERE [${dbConfig.FIELD_PRODUCTION_ID}] COLLATE SQL_Latin1_General_CP1_CI_AS IN (${placeholders})`
|
sql = `SELECT DISTINCT [${dbConfig.FIELD_PRODUCTION_ID}], [${dbConfig.FIELD_ORDER_NUMBER}] FROM ${tableName} WHERE [${dbConfig.FIELD_PRODUCTION_ID}] COLLATE SQL_Latin1_General_CP1_CI_AS IN (${placeholders})`
|
||||||
|
} else if (this.dbService.type === 'postgresql') {
|
||||||
|
// PostgreSQL: 使用双引号保护中文标识符,UPPER 实现不区分大小写
|
||||||
|
// 注意:getTableName() 已返回带双引号的表名,不应再加引号
|
||||||
|
const pgPlaceholders = uniqueProductionIds.map((_, i) => `UPPER($${i + 1})`).join(', ')
|
||||||
|
sql = `SELECT DISTINCT "${dbConfig.FIELD_PRODUCTION_ID}", "${dbConfig.FIELD_ORDER_NUMBER}" FROM ${tableName} WHERE UPPER("${dbConfig.FIELD_PRODUCTION_ID}") IN (${pgPlaceholders})`
|
||||||
} else {
|
} else {
|
||||||
const idPlaceholders = uniqueProductionIds.map(() => 'UPPER(?)').join(', ')
|
const idPlaceholders = uniqueProductionIds.map(() => 'UPPER(?)').join(', ')
|
||||||
// P0: Use DISTINCT to prevent duplicates from one-to-many relationships
|
// P0: Use DISTINCT to prevent duplicates from one-to-many relationships
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import path from 'path'
|
|||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { createLogger } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
|
import { logAuditWithCurrentUser } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
import type { ExportResultItem, ExportResultResponse } from '../../types/cleaner.types'
|
import type { ExportResultItem, ExportResultResponse } from '../../types/cleaner.types'
|
||||||
|
|
||||||
const log = createLogger('ResultExporter')
|
const log = createLogger('ResultExporter')
|
||||||
@@ -37,8 +39,8 @@ export class ResultExporter {
|
|||||||
* @returns Export result with file path or error
|
* @returns Export result with file path or error
|
||||||
*/
|
*/
|
||||||
async exportValidationResults(items: ExportResultItem[]): Promise<ExportResultResponse> {
|
async exportValidationResults(items: ExportResultItem[]): Promise<ExportResultResponse> {
|
||||||
|
const filePath = path.join(this.exportDir, this.fileName)
|
||||||
try {
|
try {
|
||||||
const filePath = path.join(this.exportDir, this.fileName)
|
|
||||||
log.info('Exporting validation results', { count: items.length, path: filePath })
|
log.info('Exporting validation results', { count: items.length, path: filePath })
|
||||||
|
|
||||||
const workbook = new ExcelJS.Workbook()
|
const workbook = new ExcelJS.Workbook()
|
||||||
@@ -97,6 +99,12 @@ export class ResultExporter {
|
|||||||
await workbook.xlsx.writeFile(filePath)
|
await workbook.xlsx.writeFile(filePath)
|
||||||
log.info('Export completed', { path: filePath, rows: items.length })
|
log.info('Export completed', { path: filePath, rows: items.length })
|
||||||
|
|
||||||
|
// Audit log: RESULT_EXPORT success
|
||||||
|
logAuditWithCurrentUser(AuditAction.RESULT_EXPORT, 'VALIDATION_RESULT', AuditStatus.SUCCESS, {
|
||||||
|
itemCount: items.length,
|
||||||
|
filePath
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
filePath
|
filePath
|
||||||
@@ -104,6 +112,14 @@ export class ResultExporter {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : String(error)
|
const errorMessage = error instanceof Error ? error.message : String(error)
|
||||||
log.error('Export failed', { error: errorMessage })
|
log.error('Export failed', { error: errorMessage })
|
||||||
|
|
||||||
|
// Audit log: RESULT_EXPORT failure
|
||||||
|
logAuditWithCurrentUser(AuditAction.RESULT_EXPORT, 'VALIDATION_RESULT', AuditStatus.FAILURE, {
|
||||||
|
itemCount: items.length,
|
||||||
|
filePath,
|
||||||
|
error: errorMessage
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: errorMessage
|
error: errorMessage
|
||||||
|
|||||||
@@ -6,33 +6,12 @@
|
|||||||
import winston from 'winston'
|
import winston from 'winston'
|
||||||
import DailyRotateFile from 'winston-daily-rotate-file'
|
import DailyRotateFile from 'winston-daily-rotate-file'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { hostname } from 'os'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import { getLogDir } from './shared'
|
import { getLogDir } from './shared'
|
||||||
|
import { SessionManager } from '../user/session-manager'
|
||||||
/**
|
import type { AuditEntry } from '../../types/audit.types'
|
||||||
* Audit log entry structure
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
* All 8 required fields for comprehensive audit tracking
|
|
||||||
*/
|
|
||||||
export interface AuditEntry {
|
|
||||||
/** ISO 8601 timestamp of the audit event */
|
|
||||||
timestamp: string
|
|
||||||
/** The action that was performed (e.g., 'LOGIN', 'EXTRACT', 'DELETE') */
|
|
||||||
action: string
|
|
||||||
/** User ID who performed the action */
|
|
||||||
userId: string
|
|
||||||
/** Username of the user who performed the action */
|
|
||||||
username: string
|
|
||||||
/** Computer name from which the action was performed */
|
|
||||||
computerName: string
|
|
||||||
/** Application version when the action was performed */
|
|
||||||
appVersion: string
|
|
||||||
/** The resource that was affected (e.g., table name, file path) */
|
|
||||||
resource: string
|
|
||||||
/** Status of the action: 'success' | 'failure' | 'partial' */
|
|
||||||
status: 'success' | 'failure' | 'partial'
|
|
||||||
/** Additional metadata about the audit event */
|
|
||||||
metadata: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONL formatter - outputs one JSON object per line
|
* JSONL formatter - outputs one JSON object per line
|
||||||
@@ -91,31 +70,58 @@ export function applyAuditConfig(retentionDays: number): void {
|
|||||||
* @param details - Additional details including username, computerName, resource, status, and optional metadata
|
* @param details - Additional details including username, computerName, resource, status, and optional metadata
|
||||||
*/
|
*/
|
||||||
export function logAudit(
|
export function logAudit(
|
||||||
action: string,
|
action: AuditAction,
|
||||||
userId: string,
|
userId: string,
|
||||||
details: {
|
details: {
|
||||||
username: string
|
username: string
|
||||||
computerName: string
|
computerName: string
|
||||||
resource: string
|
resource: string
|
||||||
status: 'success' | 'failure' | 'partial'
|
status: AuditStatus
|
||||||
metadata?: Record<string, unknown>
|
metadata?: Record<string, unknown>
|
||||||
}
|
}
|
||||||
): void {
|
): void {
|
||||||
const entry: AuditEntry = {
|
try {
|
||||||
timestamp: new Date().toISOString(),
|
const entry: AuditEntry = {
|
||||||
action,
|
timestamp: new Date().toISOString(),
|
||||||
userId,
|
action,
|
||||||
username: details.username,
|
userId,
|
||||||
computerName: details.computerName,
|
username: details.username,
|
||||||
appVersion: app.getVersion(),
|
computerName: details.computerName,
|
||||||
resource: details.resource,
|
appVersion: app.getVersion(),
|
||||||
status: details.status,
|
resource: details.resource,
|
||||||
metadata: details.metadata || {}
|
status: details.status,
|
||||||
}
|
metadata: details.metadata ?? {}
|
||||||
|
}
|
||||||
|
|
||||||
// Write as JSONL - one JSON object per line
|
// Write as JSONL - one JSON object per line
|
||||||
// Using info level with the entry stringified as the message
|
// Using info level with the entry stringified as the message
|
||||||
auditLogger.info(JSON.stringify(entry))
|
auditLogger.info(JSON.stringify(entry))
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Audit logging failed:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cached hostname — invariant for the app lifecycle */
|
||||||
|
export const cachedHostname = hostname()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Audit log shortcut that auto-resolves the current user context.
|
||||||
|
* Falls back to 'anonymous' if no user is logged in, so the record is always written.
|
||||||
|
*/
|
||||||
|
export function logAuditWithCurrentUser(
|
||||||
|
action: AuditAction,
|
||||||
|
resource: string,
|
||||||
|
status: AuditStatus,
|
||||||
|
metadata?: Record<string, unknown>
|
||||||
|
): void {
|
||||||
|
const user = SessionManager.getInstance().getUserInfo()
|
||||||
|
logAudit(action, user ? String(user.id) : 'anonymous', {
|
||||||
|
username: user?.username ?? 'anonymous',
|
||||||
|
computerName: cachedHostname,
|
||||||
|
resource,
|
||||||
|
status,
|
||||||
|
metadata: metadata ?? {}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -119,7 +119,6 @@ export async function trackDuration<T>(
|
|||||||
return { result, durationMs, isSlow }
|
return { result, durationMs, isSlow }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const durationMs = performance.now() - startTime
|
const durationMs = performance.now() - startTime
|
||||||
const isSlow = durationMs > slowThresholdMs
|
|
||||||
|
|
||||||
// Log the error with duration
|
// Log the error with duration
|
||||||
logger.error(`${message} failed after ${durationMs.toFixed(2)}ms`, {
|
logger.error(`${message} failed after ${durationMs.toFixed(2)}ms`, {
|
||||||
|
|||||||
@@ -1,299 +0,0 @@
|
|||||||
import path from 'path'
|
|
||||||
import fs from 'fs'
|
|
||||||
import { app } from 'electron'
|
|
||||||
import { createLogger } from '../logger'
|
|
||||||
import type { CleanerResult, OrderCleanDetail, SkippedMaterial } from '../../types/cleaner.types'
|
|
||||||
|
|
||||||
const log = createLogger('CleanerReportGenerator')
|
|
||||||
|
|
||||||
export interface ReportOptions {
|
|
||||||
dryRun: boolean
|
|
||||||
username: string
|
|
||||||
startTime: number
|
|
||||||
endTime: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OrderStats {
|
|
||||||
successCount: number
|
|
||||||
failureCount: number
|
|
||||||
successRate: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export class CleanerReportGenerator {
|
|
||||||
private readonly reportDir: string
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
const logDir = app.isReady() ? app.getPath('logs') : path.join(process.cwd(), 'logs')
|
|
||||||
this.reportDir = path.join(logDir, 'reports')
|
|
||||||
this.ensureReportDir()
|
|
||||||
}
|
|
||||||
|
|
||||||
private ensureReportDir(): void {
|
|
||||||
if (!fs.existsSync(this.reportDir)) {
|
|
||||||
fs.mkdirSync(this.reportDir, { recursive: true })
|
|
||||||
log.info('Created report directory', { path: this.reportDir })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async generateReport(result: CleanerResult, options: ReportOptions): Promise<string> {
|
|
||||||
const filePath = this.getReportFilePath()
|
|
||||||
log.info('Generating cleaner report', { path: filePath })
|
|
||||||
|
|
||||||
const stats = this.calculateOrderStats(result)
|
|
||||||
const content = this.buildReportContent(result, options, stats)
|
|
||||||
|
|
||||||
await fs.promises.writeFile(filePath, content, 'utf-8')
|
|
||||||
log.info('Report generated successfully', { path: filePath })
|
|
||||||
|
|
||||||
return filePath
|
|
||||||
}
|
|
||||||
|
|
||||||
private getReportFilePath(): string {
|
|
||||||
const now = new Date()
|
|
||||||
const timestamp = now.toISOString().replace(/[:.]/g, '-').slice(0, -5).replace('T', '-')
|
|
||||||
const fileName = `cleaner-report-${timestamp}.md`
|
|
||||||
return path.join(this.reportDir, fileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
private calculateOrderStats(result: CleanerResult): OrderStats {
|
|
||||||
const totalOrders = result.details.length
|
|
||||||
const failureCount = result.details.filter((d) => d.errors.length > 0).length
|
|
||||||
const successCount = totalOrders - failureCount
|
|
||||||
const successRate = totalOrders > 0 ? (successCount / totalOrders) * 100 : 0
|
|
||||||
|
|
||||||
return {
|
|
||||||
successCount,
|
|
||||||
failureCount,
|
|
||||||
successRate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private buildReportContent(
|
|
||||||
result: CleanerResult,
|
|
||||||
options: ReportOptions,
|
|
||||||
stats: OrderStats
|
|
||||||
): string {
|
|
||||||
const lines: string[] = []
|
|
||||||
|
|
||||||
lines.push('# ERP 物料清理执行报告')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
lines.push('## 执行摘要')
|
|
||||||
lines.push('')
|
|
||||||
lines.push('| 项目 | 值 |')
|
|
||||||
lines.push('| -------------- | --------------------------------- |')
|
|
||||||
lines.push(`| **执行时间** | \`${this.formatDateTime(options.endTime)}\``)
|
|
||||||
lines.push(`| **执行模式** | \`${options.dryRun ? '模拟运行 (Dry Run)' : '正式执行'}\``)
|
|
||||||
lines.push(`| **操作用户** | \`${options.username}\``)
|
|
||||||
lines.push(`| **处理订单数** | \`${result.ordersProcessed}\``)
|
|
||||||
lines.push(`| **删除物料数** | \`${result.materialsDeleted}\``)
|
|
||||||
lines.push(`| **跳过物料数** | \`${result.materialsSkipped}\``)
|
|
||||||
lines.push(`| **错误数量** | \`${result.errors.length}\``)
|
|
||||||
if (result.retriedOrders > 0) {
|
|
||||||
lines.push(`| **重试订单数** | \`${result.retriedOrders}\``)
|
|
||||||
lines.push(`| **成功重试数** | \`${result.successfulRetries}\``)
|
|
||||||
}
|
|
||||||
lines.push(`| **执行耗时** | \`${this.formatDuration(options.startTime, options.endTime)}\``)
|
|
||||||
lines.push('')
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
lines.push('## 执行状态')
|
|
||||||
lines.push('')
|
|
||||||
lines.push('| 状态 | 数量 | 百分比 |')
|
|
||||||
lines.push('| ----------- | ---- | ------ |')
|
|
||||||
lines.push(`| ✅ 成功订单 | ${stats.successCount} | ${stats.successRate.toFixed(1)}% |`)
|
|
||||||
lines.push(`| ❌ 失败订单 | ${stats.failureCount} | ${(100 - stats.successRate).toFixed(1)}% |`)
|
|
||||||
if (result.retriedOrders > 0) {
|
|
||||||
const retrySuccessRate =
|
|
||||||
result.retriedOrders > 0 ? (result.successfulRetries / result.retriedOrders) * 100 : 0
|
|
||||||
lines.push(`| 🔄 重试订单 | ${result.retriedOrders} | 100% |`)
|
|
||||||
lines.push(`| ✅ 成功重试 | ${result.successfulRetries} | ${retrySuccessRate.toFixed(1)}% |`)
|
|
||||||
}
|
|
||||||
lines.push('')
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
lines.push('## 订单处理详情')
|
|
||||||
lines.push('')
|
|
||||||
lines.push('| # | 订单号 | 删除数 | 跳过数 | 状态 | 错误信息 |')
|
|
||||||
lines.push('| --- | -------- | ------ | ------ | ------- | ------------------------ |')
|
|
||||||
|
|
||||||
result.details.forEach((detail, index) => {
|
|
||||||
const orderNum = index + 1
|
|
||||||
let status = detail.errors.length > 0 ? '❌ 失败' : '✅ 成功'
|
|
||||||
|
|
||||||
// Override status if retry was successful
|
|
||||||
if (detail.retrySuccess) {
|
|
||||||
status = '✅ 重试成功'
|
|
||||||
} else if (detail.retryCount > 0 && !detail.retrySuccess) {
|
|
||||||
status = '❌ 重试失败'
|
|
||||||
}
|
|
||||||
|
|
||||||
const errorMsg = detail.errors.length > 0 ? detail.errors[0] : '-'
|
|
||||||
const retryInfo = detail.retryCount > 0 ? ` [重试${detail.retryCount}次]` : ''
|
|
||||||
lines.push(
|
|
||||||
`| ${orderNum} | \`${detail.orderNumber}\` | ${detail.materialsDeleted} | ${detail.materialsSkipped} | ${status}${retryInfo} | \`${errorMsg}\` |`
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
lines.push('')
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
const allSkippedMaterials = this.collectAllSkippedMaterials(result.details)
|
|
||||||
if (allSkippedMaterials.length > 0) {
|
|
||||||
lines.push('## 跳过的物料原因说明')
|
|
||||||
lines.push('')
|
|
||||||
lines.push('| 订单号 | 物料代码 | 物料名称 | 行号 | 跳过原因 |')
|
|
||||||
lines.push('| -------- | -------- | -------- | ---- | --------------------------------- |')
|
|
||||||
|
|
||||||
allSkippedMaterials.forEach((skipped) => {
|
|
||||||
lines.push(
|
|
||||||
`| \`${skipped.orderNumber}\` | \`${skipped.materialCode}\` | \`${skipped.materialName}\` | ${skipped.rowNumber} | ${skipped.reason} |`
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
lines.push('')
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.errors.length > 0) {
|
|
||||||
lines.push('## 错误详情')
|
|
||||||
lines.push('')
|
|
||||||
lines.push(`**错误总数**: \`${result.errors.length}\``)
|
|
||||||
lines.push('')
|
|
||||||
lines.push('### 错误订单列表')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
const errorOrders = this.extractErrorOrders(result.details)
|
|
||||||
errorOrders.forEach((order) => {
|
|
||||||
lines.push(`- \`${order}\``)
|
|
||||||
})
|
|
||||||
|
|
||||||
lines.push('')
|
|
||||||
lines.push('### 错误详细信息')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
result.details
|
|
||||||
.filter((d) => d.errors.length > 0)
|
|
||||||
.forEach((detail) => {
|
|
||||||
lines.push(`#### \`${detail.orderNumber}\``)
|
|
||||||
lines.push('')
|
|
||||||
lines.push('```')
|
|
||||||
lines.push(`订单号:${detail.orderNumber}`)
|
|
||||||
detail.errors.forEach((error) => {
|
|
||||||
lines.push(`错误:${error}`)
|
|
||||||
})
|
|
||||||
lines.push('```')
|
|
||||||
lines.push('')
|
|
||||||
})
|
|
||||||
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add retry details section
|
|
||||||
if (result.retriedOrders > 0) {
|
|
||||||
lines.push('## 重试执行详情')
|
|
||||||
lines.push('')
|
|
||||||
lines.push(
|
|
||||||
`**重试订单总数**: \`${result.retriedOrders}\` | **成功**: \`${result.successfulRetries}\` | **失败**: \`${result.retriedOrders - result.successfulRetries}\``
|
|
||||||
)
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
const retriedDetails = result.details.filter((d) => d.retryCount > 0)
|
|
||||||
|
|
||||||
if (retriedDetails.length > 0) {
|
|
||||||
lines.push('### 重试订单列表')
|
|
||||||
lines.push('')
|
|
||||||
lines.push('| 订单号 | 重试次数 | 重试结果 | 重试时间 |')
|
|
||||||
lines.push('| -------- | -------- | -------- | ------------ |')
|
|
||||||
|
|
||||||
retriedDetails.forEach((detail) => {
|
|
||||||
const retryStatus = detail.retrySuccess ? '✅ 成功' : '❌ 失败'
|
|
||||||
const retryTime = detail.retriedAt ? this.formatDateTime(detail.retriedAt) : '-'
|
|
||||||
lines.push(
|
|
||||||
`| \`${detail.orderNumber}\` | ${detail.retryCount} | ${retryStatus} | ${retryTime} |`
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
lines.push('')
|
|
||||||
lines.push('### 重试尝试详细记录')
|
|
||||||
lines.push('')
|
|
||||||
|
|
||||||
retriedDetails.forEach((detail) => {
|
|
||||||
lines.push(`#### \`${detail.orderNumber}\``)
|
|
||||||
lines.push('')
|
|
||||||
lines.push(`- **重试次数**: ${detail.retryCount}`)
|
|
||||||
lines.push(`- **最终结果**: ${detail.retrySuccess ? '✅ 成功' : '❌ 失败'}`)
|
|
||||||
|
|
||||||
if (detail.retryAttempts && detail.retryAttempts.length > 0) {
|
|
||||||
lines.push('')
|
|
||||||
lines.push('**重试尝试记录**:')
|
|
||||||
lines.push('')
|
|
||||||
detail.retryAttempts.forEach((attempt, idx) => {
|
|
||||||
lines.push(
|
|
||||||
`${idx + 1}. **第${attempt.attempt}次尝试** - ${this.formatDateTime(attempt.timestamp)}`
|
|
||||||
)
|
|
||||||
lines.push(` - 错误:${attempt.error}`)
|
|
||||||
})
|
|
||||||
lines.push('')
|
|
||||||
}
|
|
||||||
|
|
||||||
lines.push('---')
|
|
||||||
lines.push('')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
lines.push('')
|
|
||||||
}
|
|
||||||
|
|
||||||
lines.push(`**报告生成时间**: \`${this.formatDateTime(options.endTime)}\``)
|
|
||||||
lines.push('**报表版本**: `v1.0`')
|
|
||||||
|
|
||||||
return lines.join('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
private collectAllSkippedMaterials(
|
|
||||||
details: OrderCleanDetail[]
|
|
||||||
): Array<SkippedMaterial & { orderNumber: string }> {
|
|
||||||
const result: Array<SkippedMaterial & { orderNumber: string }> = []
|
|
||||||
|
|
||||||
details.forEach((detail) => {
|
|
||||||
if (detail.skippedMaterials && detail.skippedMaterials.length > 0) {
|
|
||||||
detail.skippedMaterials.forEach((skipped) => {
|
|
||||||
result.push({
|
|
||||||
...skipped,
|
|
||||||
orderNumber: detail.orderNumber
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private extractErrorOrders(details: OrderCleanDetail[]): string[] {
|
|
||||||
return details.filter((d) => d.errors.length > 0).map((d) => d.orderNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
private formatDateTime(timestamp: number): string {
|
|
||||||
const date = new Date(timestamp)
|
|
||||||
const year = date.getFullYear()
|
|
||||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
||||||
const day = String(date.getDate()).padStart(2, '0')
|
|
||||||
const hours = String(date.getHours()).padStart(2, '0')
|
|
||||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
|
||||||
const seconds = String(date.getSeconds()).padStart(2, '0')
|
|
||||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
|
|
||||||
}
|
|
||||||
|
|
||||||
private formatDuration(startTime: number, endTime: number): string {
|
|
||||||
const durationMs = endTime - startTime
|
|
||||||
const minutes = Math.floor(durationMs / 60000)
|
|
||||||
const seconds = Math.floor((durationMs % 60000) / 1000)
|
|
||||||
return `${minutes}分${seconds}秒`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
type GetObjectCommandInput,
|
type GetObjectCommandInput,
|
||||||
type DeleteObjectCommandInput
|
type DeleteObjectCommandInput
|
||||||
} from '@aws-sdk/client-s3'
|
} from '@aws-sdk/client-s3'
|
||||||
import { createLogger, run, trackDuration, PerformanceTracker } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
import type { RustfsConfig } from '../../types/config.schema'
|
import type { RustfsConfig } from '../../types/config.schema'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import { ConfigManager } from '../config/config-manager'
|
import { ConfigManager } from '../config/config-manager'
|
||||||
import { createLogger, run, trackDuration, PerformanceTracker } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
|
import { logAuditWithCurrentUser } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
import type { UpdateConfig } from '../../types/config.schema'
|
import type { UpdateConfig } from '../../types/config.schema'
|
||||||
import type { UserType } from '../../types/user.types'
|
import type { UserType } from '../../types/user.types'
|
||||||
import type {
|
import type {
|
||||||
@@ -281,10 +283,21 @@ export class UpdateService {
|
|||||||
log.error('Update package hash mismatch', {
|
log.error('Update package hash mismatch', {
|
||||||
version: request.version,
|
version: request.version,
|
||||||
channel: request.channel,
|
channel: request.channel,
|
||||||
expectedHash: request.sha256,
|
expectedHash: request.sha256.substring(0, 16),
|
||||||
actualHash: hash
|
actualHash: hash.substring(0, 16)
|
||||||
})
|
})
|
||||||
await fs.promises.rm(downloadPath, { force: true })
|
await fs.promises.rm(downloadPath, { force: true })
|
||||||
|
|
||||||
|
// Audit log: APP_UPDATE download hash mismatch
|
||||||
|
logAuditWithCurrentUser(AuditAction.APP_UPDATE, 'UPDATE_PACKAGE', AuditStatus.FAILURE, {
|
||||||
|
version: request.version,
|
||||||
|
channel: request.channel,
|
||||||
|
phase: 'download',
|
||||||
|
error: 'Hash mismatch',
|
||||||
|
expectedHash: request.sha256.substring(0, 16),
|
||||||
|
actualHash: hash.substring(0, 16)
|
||||||
|
})
|
||||||
|
|
||||||
throw new Error('更新包校验失败,文件哈希不匹配')
|
throw new Error('更新包校验失败,文件哈希不匹配')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +307,13 @@ export class UpdateService {
|
|||||||
downloadPath
|
downloadPath
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Audit log: APP_UPDATE download success
|
||||||
|
logAuditWithCurrentUser(AuditAction.APP_UPDATE, 'UPDATE_PACKAGE', AuditStatus.SUCCESS, {
|
||||||
|
version: request.version,
|
||||||
|
channel: request.channel,
|
||||||
|
phase: 'download'
|
||||||
|
})
|
||||||
|
|
||||||
this.publishStatus({
|
this.publishStatus({
|
||||||
phase: 'downloaded',
|
phase: 'downloaded',
|
||||||
progress: 100,
|
progress: 100,
|
||||||
@@ -336,11 +356,29 @@ export class UpdateService {
|
|||||||
error: undefined
|
error: undefined
|
||||||
})
|
})
|
||||||
|
|
||||||
await this.installer.installDownloadedRelease(downloaded)
|
try {
|
||||||
log.info('Update installation completed', {
|
await this.installer.installDownloadedRelease(downloaded)
|
||||||
version: downloaded.version,
|
log.info('Update installation completed', {
|
||||||
channel: downloaded.channel
|
version: downloaded.version,
|
||||||
})
|
channel: downloaded.channel
|
||||||
|
})
|
||||||
|
|
||||||
|
// Audit log: APP_UPDATE install success
|
||||||
|
logAuditWithCurrentUser(AuditAction.APP_UPDATE, 'UPDATE_PACKAGE', AuditStatus.SUCCESS, {
|
||||||
|
version: downloaded.version,
|
||||||
|
channel: downloaded.channel,
|
||||||
|
phase: 'install'
|
||||||
|
})
|
||||||
|
} catch (installError) {
|
||||||
|
const msg = installError instanceof Error ? installError.message : String(installError)
|
||||||
|
logAuditWithCurrentUser(AuditAction.APP_UPDATE, 'UPDATE_PACKAGE', AuditStatus.FAILURE, {
|
||||||
|
version: downloaded.version,
|
||||||
|
channel: downloaded.channel,
|
||||||
|
phase: 'install',
|
||||||
|
error: msg
|
||||||
|
})
|
||||||
|
throw installError
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ensureInitialized(): void {
|
private ensureInitialized(): void {
|
||||||
|
|||||||
@@ -8,10 +8,8 @@
|
|||||||
* - Create, update, delete users
|
* - Create, update, delete users
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { MySqlService } from '../database/mysql'
|
import { create, type IDatabaseService } from '../database/index'
|
||||||
import { SqlServerService } from '../database/sql-server'
|
import { createDialect, type SqlDialect } from '../database/dialects'
|
||||||
import { ConfigManager } from '../config/config-manager'
|
|
||||||
import sql from 'mssql'
|
|
||||||
import type { UserInfo } from '../../types/user.types'
|
import type { UserInfo } from '../../types/user.types'
|
||||||
import { createLogger, logError } from '../logger'
|
import { createLogger, logError } from '../logger'
|
||||||
|
|
||||||
@@ -21,10 +19,6 @@ const log = createLogger('BipUsersDao')
|
|||||||
* Database configuration for BIPUsers table
|
* Database configuration for BIPUsers table
|
||||||
*/
|
*/
|
||||||
export const BIP_USERS_CONFIG = {
|
export const BIP_USERS_CONFIG = {
|
||||||
/** Table name in SQL Server: [dbo].[BIPUsers] */
|
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[BIPUsers]',
|
|
||||||
/** Table name in MySQL: dbo_BIPUsers */
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_BIPUsers',
|
|
||||||
/** Column names */
|
/** Column names */
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
@@ -44,71 +38,37 @@ export const BIP_USERS_CONFIG = {
|
|||||||
* BIPUsers DAO Class
|
* BIPUsers DAO Class
|
||||||
*/
|
*/
|
||||||
export class BIPUsersDAO {
|
export class BIPUsersDAO {
|
||||||
private mysqlService: MySqlService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
private sqlServerService: SqlServerService | null = null
|
private dialect: SqlDialect | null = null
|
||||||
private dbType: 'mysql' | 'sqlserver' = 'mysql'
|
|
||||||
private configManager: ConfigManager
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor - get database type from ConfigManager
|
|
||||||
*/
|
|
||||||
constructor() {
|
|
||||||
this.configManager = ConfigManager.getInstance()
|
|
||||||
this.dbType = this.configManager.getDatabaseType()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
return this.dbType === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'BIPUsers')
|
||||||
? BIP_USERS_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: BIP_USERS_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get database service instance (MySQL or SQL Server)
|
* Get dialect instance
|
||||||
*/
|
*/
|
||||||
private async getDatabaseService(): Promise<MySqlService | SqlServerService> {
|
private getDialect(): SqlDialect {
|
||||||
const config = this.configManager.getConfig()
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
if (this.dbType === 'sqlserver') {
|
|
||||||
if (this.sqlServerService && this.sqlServerService.isConnected()) {
|
|
||||||
return this.sqlServerService
|
|
||||||
}
|
|
||||||
|
|
||||||
const dbConfig = config.database.sqlserver
|
|
||||||
this.sqlServerService = new SqlServerService({
|
|
||||||
server: dbConfig.server,
|
|
||||||
port: dbConfig.port,
|
|
||||||
user: dbConfig.username,
|
|
||||||
password: dbConfig.password,
|
|
||||||
database: dbConfig.database,
|
|
||||||
options: {
|
|
||||||
encrypt: false,
|
|
||||||
trustServerCertificate: dbConfig.trustServerCertificate
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await this.sqlServerService.connect()
|
|
||||||
return this.sqlServerService
|
|
||||||
} else {
|
|
||||||
if (this.mysqlService && this.mysqlService.isConnected()) {
|
|
||||||
return this.mysqlService
|
|
||||||
}
|
|
||||||
|
|
||||||
const dbConfig = config.database.mysql
|
|
||||||
this.mysqlService = new MySqlService({
|
|
||||||
host: dbConfig.host,
|
|
||||||
port: dbConfig.port,
|
|
||||||
user: dbConfig.username,
|
|
||||||
password: dbConfig.password,
|
|
||||||
database: dbConfig.database
|
|
||||||
})
|
|
||||||
|
|
||||||
await this.mysqlService.connect()
|
|
||||||
return this.mysqlService
|
|
||||||
}
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get database service instance via DatabaseFactory
|
||||||
|
*/
|
||||||
|
private async getDatabaseService(): Promise<IDatabaseService> {
|
||||||
|
if (this.dbService && this.dbService.isConnected()) {
|
||||||
|
return this.dbService
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dbService = await create()
|
||||||
|
this.dialect = null // Reset dialect when service changes
|
||||||
|
return this.dbService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,52 +81,30 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
SELECT ID, UserName, UserType
|
||||||
SELECT ID, UserName, UserType
|
FROM ${tableName}
|
||||||
FROM ${tableName}
|
WHERE UserName = ${dialect.param(0)} AND Password = ${dialect.param(1)}
|
||||||
WHERE UserName = @username AND Password = @password
|
`
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as SqlServerService).queryWithParams(sqlString, {
|
const result = await dbService.query(sqlString, [username, password])
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
|
||||||
password: { value: password, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
if (result.rows.length > 0) {
|
||||||
const row = result.rows[0]
|
const row = result.rows[0]
|
||||||
return {
|
return {
|
||||||
id: row.ID as number,
|
id: row.ID as number,
|
||||||
username: row.UserName as string,
|
username: row.UserName as string,
|
||||||
userType: row.UserType as 'Admin' | 'User'
|
userType: row.UserType as 'Admin' | 'User'
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
SELECT ID, UserName, UserType
|
|
||||||
FROM ${tableName}
|
|
||||||
WHERE UserName = ? AND Password = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as MySqlService).query(sqlString, [username, password])
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
|
||||||
const row = result.rows[0]
|
|
||||||
return {
|
|
||||||
id: row.ID as number,
|
|
||||||
username: row.UserName as string,
|
|
||||||
userType: row.UserType as 'Admin' | 'User'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Authenticate failed',
|
message: 'Authenticate failed',
|
||||||
operation: 'authenticate',
|
operation: 'authenticate',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -181,51 +119,30 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
SELECT ID, UserName, UserType
|
||||||
SELECT ID, UserName, UserType
|
FROM ${tableName}
|
||||||
FROM ${tableName}
|
WHERE ComputerName = ${dialect.param(0)}
|
||||||
WHERE ComputerName = @computerName
|
`
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as SqlServerService).queryWithParams(sqlString, {
|
const result = await dbService.query(sqlString, [computerName])
|
||||||
computerName: { value: computerName, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
if (result.rows.length > 0) {
|
||||||
const row = result.rows[0]
|
const row = result.rows[0]
|
||||||
return {
|
return {
|
||||||
id: row.ID as number,
|
id: row.ID as number,
|
||||||
username: row.UserName as string,
|
username: row.UserName as string,
|
||||||
userType: row.UserType as 'Admin' | 'User'
|
userType: row.UserType as 'Admin' | 'User'
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
SELECT ID, UserName, UserType
|
|
||||||
FROM ${tableName}
|
|
||||||
WHERE ComputerName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as MySqlService).query(sqlString, [computerName])
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
|
||||||
const row = result.rows[0]
|
|
||||||
return {
|
|
||||||
id: row.ID as number,
|
|
||||||
username: row.UserName as string,
|
|
||||||
userType: row.UserType as 'Admin' | 'User'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Silent login failed',
|
message: 'Silent login failed',
|
||||||
operation: 'authenticateByComputerName',
|
operation: 'authenticateByComputerName',
|
||||||
context: { computerName, dbType: this.dbType }
|
context: { computerName, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -246,10 +163,7 @@ export class BIPUsersDAO {
|
|||||||
ORDER BY UserName
|
ORDER BY UserName
|
||||||
`
|
`
|
||||||
|
|
||||||
const result =
|
const result = await dbService.query(sqlString)
|
||||||
this.dbType === 'sqlserver'
|
|
||||||
? await (dbService as SqlServerService).query(sqlString)
|
|
||||||
: await (dbService as MySqlService).query(sqlString)
|
|
||||||
|
|
||||||
return result.rows.map((row) => ({
|
return result.rows.map((row) => ({
|
||||||
id: row.ID as number,
|
id: row.ID as number,
|
||||||
@@ -261,7 +175,7 @@ export class BIPUsersDAO {
|
|||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Get all users failed',
|
message: 'Get all users failed',
|
||||||
operation: 'getAllUsers',
|
operation: 'getAllUsers',
|
||||||
context: { dbType: this.dbType }
|
context: { dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
@@ -284,72 +198,34 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
let sqlString: string
|
||||||
let sqlString: string
|
let params: string[]
|
||||||
let params: Record<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
value: unknown
|
|
||||||
type?: sql.ISqlType | sql.ISqlTypeFactoryWithLength | sql.ISqlTypeWithLength
|
|
||||||
}
|
|
||||||
>
|
|
||||||
|
|
||||||
if (computerName) {
|
if (computerName) {
|
||||||
sqlString = `
|
sqlString = `
|
||||||
INSERT INTO ${tableName}
|
INSERT INTO ${tableName}
|
||||||
(UserName, Password, UserType, ComputerName)
|
(UserName, Password, UserType, ComputerName)
|
||||||
VALUES (@username, @password, @userType, @computerName)
|
VALUES (${dialect.param(0)}, ${dialect.param(1)}, ${dialect.param(2)}, ${dialect.param(3)})
|
||||||
`
|
`
|
||||||
params = {
|
params = [username, password, userType, computerName]
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
|
||||||
password: { value: password, type: sql.NVarChar(255) },
|
|
||||||
userType: { value: userType, type: sql.NVarChar(255) },
|
|
||||||
computerName: { value: computerName, type: sql.NVarChar(255) }
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sqlString = `
|
|
||||||
INSERT INTO ${tableName}
|
|
||||||
(UserName, Password, UserType)
|
|
||||||
VALUES (@username, @password, @userType)
|
|
||||||
`
|
|
||||||
params = {
|
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
|
||||||
password: { value: password, type: sql.NVarChar(255) },
|
|
||||||
userType: { value: userType, type: sql.NVarChar(255) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await (dbService as SqlServerService).queryWithParams(sqlString, params)
|
|
||||||
return true
|
|
||||||
} else {
|
} else {
|
||||||
let sqlString: string
|
sqlString = `
|
||||||
let params: unknown[]
|
INSERT INTO ${tableName}
|
||||||
|
(UserName, Password, UserType)
|
||||||
if (computerName) {
|
VALUES (${dialect.param(0)}, ${dialect.param(1)}, ${dialect.param(2)})
|
||||||
sqlString = `
|
`
|
||||||
INSERT INTO ${tableName}
|
params = [username, password, userType]
|
||||||
(UserName, Password, UserType, ComputerName)
|
|
||||||
VALUES (?, ?, ?, ?)
|
|
||||||
`
|
|
||||||
params = [username, password, userType, computerName]
|
|
||||||
} else {
|
|
||||||
sqlString = `
|
|
||||||
INSERT INTO ${tableName}
|
|
||||||
(UserName, Password, UserType)
|
|
||||||
VALUES (?, ?, ?)
|
|
||||||
`
|
|
||||||
params = [username, password, userType]
|
|
||||||
}
|
|
||||||
|
|
||||||
await (dbService as MySqlService).query(sqlString, params)
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await dbService.query(sqlString, params)
|
||||||
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Create user failed',
|
message: 'Create user failed',
|
||||||
operation: 'createUser',
|
operation: 'createUser',
|
||||||
context: { username, userType, dbType: this.dbType }
|
context: { username, userType, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -365,34 +241,21 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
UPDATE ${tableName}
|
||||||
UPDATE ${tableName}
|
SET UserType = ${dialect.param(0)}
|
||||||
SET UserType = @userType
|
WHERE UserName = ${dialect.param(1)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as SqlServerService).queryWithParams(sqlString, {
|
await dbService.query(sqlString, [userType, username])
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
return true
|
||||||
userType: { value: userType, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
UPDATE ${tableName}
|
|
||||||
SET UserType = ?
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as MySqlService).query(sqlString, [userType, username])
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Update user type failed',
|
message: 'Update user type failed',
|
||||||
operation: 'updateUserType',
|
operation: 'updateUserType',
|
||||||
context: { username, userType, dbType: this.dbType }
|
context: { username, userType, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -408,34 +271,21 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
UPDATE ${tableName}
|
||||||
UPDATE ${tableName}
|
SET Password = ${dialect.param(0)}
|
||||||
SET Password = @newPassword
|
WHERE UserName = ${dialect.param(1)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as SqlServerService).queryWithParams(sqlString, {
|
await dbService.query(sqlString, [newPassword, username])
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
return true
|
||||||
newPassword: { value: newPassword, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
UPDATE ${tableName}
|
|
||||||
SET Password = ?
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as MySqlService).query(sqlString, [newPassword, username])
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Update password failed',
|
message: 'Update password failed',
|
||||||
operation: 'updatePassword',
|
operation: 'updatePassword',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -450,31 +300,20 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
DELETE FROM ${tableName}
|
||||||
DELETE FROM ${tableName}
|
WHERE UserName = ${dialect.param(0)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as SqlServerService).queryWithParams(sqlString, {
|
await dbService.query(sqlString, [username])
|
||||||
username: { value: username, type: sql.NVarChar(255) }
|
return true
|
||||||
})
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
DELETE FROM ${tableName}
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as MySqlService).query(sqlString, [username])
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Delete user failed',
|
message: 'Delete user failed',
|
||||||
operation: 'deleteUser',
|
operation: 'deleteUser',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -489,33 +328,21 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
SELECT COUNT(*) as count
|
||||||
SELECT COUNT(*) as count
|
FROM ${tableName}
|
||||||
FROM ${tableName}
|
WHERE UserName = ${dialect.param(0)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as SqlServerService).queryWithParams(sqlString, {
|
const result = await dbService.query(sqlString, [username])
|
||||||
username: { value: username, type: sql.NVarChar(255) }
|
return result.rows.length > 0 && (result.rows[0].count as number) > 0
|
||||||
})
|
|
||||||
return result.rows.length > 0 && (result.rows[0].count as number) > 0
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
SELECT COUNT(*) as count
|
|
||||||
FROM ${tableName}
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as MySqlService).query(sqlString, [username])
|
|
||||||
return result.rows.length > 0 && (result.rows[0].count as number) > 0
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Check user exists failed',
|
message: 'Check user exists failed',
|
||||||
operation: 'userExists',
|
operation: 'userExists',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -533,50 +360,30 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
const cols = BIP_USERS_CONFIG.COLUMNS
|
const cols = BIP_USERS_CONFIG.COLUMNS
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
SELECT ${cols.ERP_USERNAME}, ${cols.ERP_PASSWORD}
|
||||||
SELECT ${cols.ERP_USERNAME}, ${cols.ERP_PASSWORD}
|
FROM ${tableName}
|
||||||
FROM ${tableName}
|
WHERE UserName = ${dialect.param(0)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as SqlServerService).queryWithParams(sqlString, {
|
const result = await dbService.query(sqlString, [username])
|
||||||
username: { value: username, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
if (result.rows.length > 0) {
|
||||||
const row = result.rows[0]
|
const row = result.rows[0]
|
||||||
return {
|
return {
|
||||||
username: (row[cols.ERP_USERNAME] as string) || '',
|
username: (row[cols.ERP_USERNAME] as string) || '',
|
||||||
password: (row[cols.ERP_PASSWORD] as string) || ''
|
password: (row[cols.ERP_PASSWORD] as string) || ''
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
SELECT ${cols.ERP_USERNAME}, ${cols.ERP_PASSWORD}
|
|
||||||
FROM ${tableName}
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
const result = await (dbService as MySqlService).query(sqlString, [username])
|
|
||||||
|
|
||||||
if (result.rows.length > 0) {
|
|
||||||
const row = result.rows[0]
|
|
||||||
return {
|
|
||||||
username: (row[cols.ERP_USERNAME] as string) || '',
|
|
||||||
password: (row[cols.ERP_PASSWORD] as string) || ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Get user ERP credentials failed',
|
message: 'Get user ERP credentials failed',
|
||||||
operation: 'getUserErpCredentials',
|
operation: 'getUserErpCredentials',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -597,38 +404,23 @@ export class BIPUsersDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
|
const dialect = this.getDialect()
|
||||||
const cols = BIP_USERS_CONFIG.COLUMNS
|
const cols = BIP_USERS_CONFIG.COLUMNS
|
||||||
|
|
||||||
if (this.dbType === 'sqlserver') {
|
const sqlString = `
|
||||||
const sqlString = `
|
UPDATE ${tableName}
|
||||||
UPDATE ${tableName}
|
SET ${cols.ERP_USERNAME} = ${dialect.param(0)},
|
||||||
SET ${cols.ERP_USERNAME} = @erpUsername,
|
${cols.ERP_PASSWORD} = ${dialect.param(1)}
|
||||||
${cols.ERP_PASSWORD} = @erpPassword
|
WHERE UserName = ${dialect.param(2)}
|
||||||
WHERE UserName = @username
|
`
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as SqlServerService).queryWithParams(sqlString, {
|
await dbService.query(sqlString, [erpUsername, erpPassword, username])
|
||||||
username: { value: username, type: sql.NVarChar(255) },
|
return true
|
||||||
erpUsername: { value: erpUsername, type: sql.NVarChar(255) },
|
|
||||||
erpPassword: { value: erpPassword, type: sql.NVarChar(255) }
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
UPDATE ${tableName}
|
|
||||||
SET ${cols.ERP_USERNAME} = ?,
|
|
||||||
${cols.ERP_PASSWORD} = ?
|
|
||||||
WHERE UserName = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
await (dbService as MySqlService).query(sqlString, [erpUsername, erpPassword, username])
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Update user ERP credentials failed',
|
message: 'Update user ERP credentials failed',
|
||||||
operation: 'updateUserErpCredentials',
|
operation: 'updateUserErpCredentials',
|
||||||
context: { username, dbType: this.dbType }
|
context: { username, dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -656,10 +448,7 @@ export class BIPUsersDAO {
|
|||||||
ORDER BY ${cols.USERNAME}
|
ORDER BY ${cols.USERNAME}
|
||||||
`
|
`
|
||||||
|
|
||||||
const result =
|
const result = await dbService.query(sqlString)
|
||||||
this.dbType === 'sqlserver'
|
|
||||||
? await (dbService as SqlServerService).query(sqlString)
|
|
||||||
: await (dbService as MySqlService).query(sqlString)
|
|
||||||
|
|
||||||
return result.rows.map((row) => ({
|
return result.rows.map((row) => ({
|
||||||
username: row[cols.USERNAME] as string,
|
username: row[cols.USERNAME] as string,
|
||||||
@@ -670,7 +459,7 @@ export class BIPUsersDAO {
|
|||||||
logError(log, error, {
|
logError(log, error, {
|
||||||
message: 'Get all users ERP config failed',
|
message: 'Get all users ERP config failed',
|
||||||
operation: 'getAllUsersErpConfig',
|
operation: 'getAllUsersErpConfig',
|
||||||
context: { dbType: this.dbType }
|
context: { dbType: this.dbService?.type }
|
||||||
})
|
})
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
@@ -680,13 +469,10 @@ export class BIPUsersDAO {
|
|||||||
* Disconnect from database
|
* Disconnect from database
|
||||||
*/
|
*/
|
||||||
async disconnect(): Promise<void> {
|
async disconnect(): Promise<void> {
|
||||||
if (this.mysqlService) {
|
if (this.dbService) {
|
||||||
await this.mysqlService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.mysqlService = null
|
this.dbService = null
|
||||||
}
|
this.dialect = null
|
||||||
if (this.sqlServerService) {
|
|
||||||
await this.sqlServerService.disconnect()
|
|
||||||
this.sqlServerService = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
/**
|
||||||
|
* Migration Script: Add ProductionId column to CleanerOrderHistory table
|
||||||
|
*
|
||||||
|
* This script adds the ProductionId column to the ERPAuto.CleanerOrderHistory
|
||||||
|
* table for tracking the original production ID (总排号) input.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* npx tsx src/main/services/user/migration/add-productionid-to-cleaner-order.ts
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ConfigManager } from '../../config/config-manager'
|
||||||
|
import { MySqlService } from '../../database/mysql'
|
||||||
|
import { SqlServerService } from '../../database/sql-server'
|
||||||
|
import { PostgreSqlService } from '../../database/postgresql'
|
||||||
|
import { createLogger } from '../../logger'
|
||||||
|
|
||||||
|
const log = createLogger('Migration')
|
||||||
|
|
||||||
|
async function runMySQLMigration(): Promise<void> {
|
||||||
|
const configManager = ConfigManager.getInstance()
|
||||||
|
await configManager.initialize()
|
||||||
|
const dbConfig = configManager.getConfig().database.mysql
|
||||||
|
|
||||||
|
const service = new MySqlService({
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await service.connect()
|
||||||
|
console.log('Connected to MySQL')
|
||||||
|
|
||||||
|
// Check if column already exists
|
||||||
|
const check = await service.query(
|
||||||
|
`SELECT COUNT(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'CleanerOrderHistory' AND COLUMN_NAME = 'ProductionId'`
|
||||||
|
)
|
||||||
|
if ((check.rows[0]?.cnt as number) > 0) {
|
||||||
|
console.log('Column ProductionId already exists, skipping.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await service.query(
|
||||||
|
`ALTER TABLE CleanerOrderHistory ADD COLUMN ProductionId VARCHAR(50) NULL`
|
||||||
|
)
|
||||||
|
console.log('Added ProductionId column to CleanerOrderHistory.')
|
||||||
|
} finally {
|
||||||
|
if (service.isConnected()) await service.disconnect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runSqlServerMigration(): Promise<void> {
|
||||||
|
const configManager = ConfigManager.getInstance()
|
||||||
|
await configManager.initialize()
|
||||||
|
const dbConfig = configManager.getConfig().database.sqlserver
|
||||||
|
|
||||||
|
const service = new SqlServerService({
|
||||||
|
server: dbConfig.server,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database,
|
||||||
|
options: { trustServerCertificate: dbConfig.trustServerCertificate }
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await service.connect()
|
||||||
|
console.log('Connected to SQL Server')
|
||||||
|
|
||||||
|
// Check if column already exists
|
||||||
|
const check = await service.query(
|
||||||
|
`SELECT COUNT(*) as cnt FROM sys.columns WHERE OBJECT_ID = OBJECT_ID('ERPAuto.CleanerOrderHistory') AND name = 'ProductionId'`
|
||||||
|
)
|
||||||
|
if ((check.rows[0]?.cnt as number) > 0) {
|
||||||
|
console.log('Column ProductionId already exists, skipping.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await service.query(
|
||||||
|
`ALTER TABLE [ERPAuto].[CleanerOrderHistory] ADD ProductionId NVARCHAR(50) NULL`
|
||||||
|
)
|
||||||
|
console.log('Added ProductionId column to CleanerOrderHistory.')
|
||||||
|
} finally {
|
||||||
|
if (service.isConnected()) await service.disconnect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runPostgreSqlMigration(): Promise<void> {
|
||||||
|
const configManager = ConfigManager.getInstance()
|
||||||
|
await configManager.initialize()
|
||||||
|
const dbConfig = configManager.getConfig().database.postgresql
|
||||||
|
|
||||||
|
const service = new PostgreSqlService({
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await service.connect()
|
||||||
|
console.log('Connected to PostgreSQL')
|
||||||
|
|
||||||
|
await service.query(
|
||||||
|
`ALTER TABLE "ERPAuto"."CleanerOrderHistory" ADD COLUMN IF NOT EXISTS "ProductionId" VARCHAR(50) NULL`
|
||||||
|
)
|
||||||
|
console.log('Added ProductionId column to CleanerOrderHistory.')
|
||||||
|
} finally {
|
||||||
|
if (service.isConnected()) await service.disconnect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main(): Promise<void> {
|
||||||
|
console.log('Migration: Add ProductionId to CleanerOrderHistory')
|
||||||
|
|
||||||
|
const configManager = ConfigManager.getInstance()
|
||||||
|
await configManager.initialize()
|
||||||
|
const dbType = configManager.getDatabaseType()
|
||||||
|
|
||||||
|
console.log(`Database type: ${dbType}`)
|
||||||
|
|
||||||
|
if (dbType === 'mysql') {
|
||||||
|
await runMySQLMigration()
|
||||||
|
} else if (dbType === 'sqlserver') {
|
||||||
|
await runSqlServerMigration()
|
||||||
|
} else if (dbType === 'postgresql') {
|
||||||
|
await runPostgreSqlMigration()
|
||||||
|
} else {
|
||||||
|
console.error(`Unsupported database type: ${dbType}`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Done.')
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
console.error('Migration failed:', err)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
import { BIPUsersDAO } from './bip-users-dao'
|
import { BIPUsersDAO } from './bip-users-dao'
|
||||||
import { SessionManager } from './session-manager'
|
import { SessionManager } from './session-manager'
|
||||||
import { createLogger } from '../logger'
|
import { createLogger } from '../logger'
|
||||||
|
import { logAuditWithCurrentUser } from '../logger/audit-logger'
|
||||||
|
import { AuditAction, AuditStatus } from '../../types/audit.types'
|
||||||
|
|
||||||
const log = createLogger('UserErpConfigService')
|
const log = createLogger('UserErpConfigService')
|
||||||
|
|
||||||
@@ -131,9 +133,22 @@ export class UserErpConfigService {
|
|||||||
log.error('Failed to update ERP credentials', { username: currentUser.username })
|
log.error('Failed to update ERP credentials', { username: currentUser.username })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logAuditWithCurrentUser(
|
||||||
|
AuditAction.ERP_CREDENTIALS_UPDATE,
|
||||||
|
'ERP_CREDENTIALS',
|
||||||
|
success ? AuditStatus.SUCCESS : AuditStatus.FAILURE,
|
||||||
|
{ targetUsername: currentUser.username, updateType: 'self' }
|
||||||
|
)
|
||||||
|
|
||||||
return success
|
return success
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Error updating current user ERP credentials', { error })
|
log.error('Error updating current user ERP credentials', { error })
|
||||||
|
logAuditWithCurrentUser(
|
||||||
|
AuditAction.ERP_CREDENTIALS_UPDATE,
|
||||||
|
'ERP_CREDENTIALS',
|
||||||
|
AuditStatus.FAILURE,
|
||||||
|
{ targetUsername: 'unknown', updateType: 'self', error: String(error) }
|
||||||
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -159,9 +174,22 @@ export class UserErpConfigService {
|
|||||||
log.error('Failed to update ERP credentials', { username })
|
log.error('Failed to update ERP credentials', { username })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logAuditWithCurrentUser(
|
||||||
|
AuditAction.ERP_CREDENTIALS_UPDATE,
|
||||||
|
'ERP_CREDENTIALS',
|
||||||
|
success ? AuditStatus.SUCCESS : AuditStatus.FAILURE,
|
||||||
|
{ targetUsername: username, updateType: 'admin' }
|
||||||
|
)
|
||||||
|
|
||||||
return success
|
return success
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Error updating user ERP credentials', { error })
|
log.error('Error updating user ERP credentials', { error })
|
||||||
|
logAuditWithCurrentUser(
|
||||||
|
AuditAction.ERP_CREDENTIALS_UPDATE,
|
||||||
|
'ERP_CREDENTIALS',
|
||||||
|
AuditStatus.FAILURE,
|
||||||
|
{ targetUsername: username, updateType: 'admin', error: String(error) }
|
||||||
|
)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export async function getSourceNumbersFromInputs(
|
|||||||
const productionIds: string[] = []
|
const productionIds: string[] = []
|
||||||
const orderNumbers: string[] = []
|
const orderNumbers: string[] = []
|
||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const isSqlServer = configManager.getDatabaseType() === 'sqlserver'
|
const dbType = configManager.getDatabaseType()
|
||||||
|
|
||||||
for (const item of inputs) {
|
for (const item of inputs) {
|
||||||
const type = identifyInputType(item)
|
const type = identifyInputType(item)
|
||||||
@@ -44,7 +44,7 @@ export async function getSourceNumbersFromInputs(
|
|||||||
const contractTableName = getValidationTableName('productionContractData_26年压力表合同数据')
|
const contractTableName = getValidationTableName('productionContractData_26年压力表合同数据')
|
||||||
const batchSize = 2000
|
const batchSize = 2000
|
||||||
|
|
||||||
if (isSqlServer) {
|
if (dbType === 'sqlserver') {
|
||||||
const sql = await import('mssql')
|
const sql = await import('mssql')
|
||||||
const allOrderNumbers: string[] = []
|
const allOrderNumbers: string[] = []
|
||||||
|
|
||||||
@@ -71,6 +71,22 @@ export async function getSourceNumbersFromInputs(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
orderNumbers.push(...allOrderNumbers)
|
||||||
|
} else if (dbType === 'postgresql') {
|
||||||
|
const allOrderNumbers: string[] = []
|
||||||
|
|
||||||
|
for (let i = 0; i < productionIds.length; i += batchSize) {
|
||||||
|
const batch = productionIds.slice(i, i + batchSize)
|
||||||
|
const placeholders = batch.map((_, idx) => `$${idx + 1}`).join(',')
|
||||||
|
const contractSql = `
|
||||||
|
SELECT DISTINCT "生产订单号"
|
||||||
|
FROM ${contractTableName}
|
||||||
|
WHERE "总排号" IN (${placeholders})
|
||||||
|
`
|
||||||
|
const contractResult = await dbService.query(contractSql, batch)
|
||||||
|
allOrderNumbers.push(...contractResult.rows.map((row) => row.生产订单号 as string))
|
||||||
|
}
|
||||||
|
|
||||||
orderNumbers.push(...allOrderNumbers)
|
orderNumbers.push(...allOrderNumbers)
|
||||||
} else {
|
} else {
|
||||||
const allOrderNumbers: string[] = []
|
const allOrderNumbers: string[] = []
|
||||||
|
|||||||
@@ -231,7 +231,8 @@ export class ValidationApplicationService {
|
|||||||
|
|
||||||
async getCleanerData(
|
async getCleanerData(
|
||||||
userInfo: UserInfo,
|
userInfo: UserInfo,
|
||||||
senderId: number
|
senderId: number,
|
||||||
|
selectedManagers: string[] = []
|
||||||
): Promise<{
|
): Promise<{
|
||||||
success: boolean
|
success: boolean
|
||||||
orderNumbers?: string[]
|
orderNumbers?: string[]
|
||||||
@@ -250,6 +251,7 @@ export class ValidationApplicationService {
|
|||||||
userId: userInfo.id,
|
userId: userInfo.id,
|
||||||
username,
|
username,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
|
selectedManagers,
|
||||||
requestId
|
requestId
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -270,7 +272,13 @@ export class ValidationApplicationService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const materialCodes = await this.loadMaterialCodesForCleaner(dbService, username, isAdmin)
|
const materialCodes = await this.loadMaterialCodesForCleaner(
|
||||||
|
dbService,
|
||||||
|
username,
|
||||||
|
isAdmin,
|
||||||
|
selectedManagers,
|
||||||
|
orderNumbers
|
||||||
|
)
|
||||||
log.info('Cleaner data retrieved', {
|
log.info('Cleaner data retrieved', {
|
||||||
userId: userInfo.id,
|
userId: userInfo.id,
|
||||||
orderCount: orderNumbers.length,
|
orderCount: orderNumbers.length,
|
||||||
@@ -463,6 +471,18 @@ export class ValidationApplicationService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dbService.type === 'postgresql') {
|
||||||
|
return dbService.query(
|
||||||
|
`
|
||||||
|
SELECT "MaterialName", "Specification", "Model"
|
||||||
|
FROM ${detailTableName}
|
||||||
|
WHERE "MaterialCode" = $1
|
||||||
|
LIMIT 1
|
||||||
|
`,
|
||||||
|
[materialCode]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return dbService.query(
|
return dbService.query(
|
||||||
`
|
`
|
||||||
SELECT MaterialName, Specification, Model
|
SELECT MaterialName, Specification, Model
|
||||||
@@ -477,27 +497,54 @@ export class ValidationApplicationService {
|
|||||||
private async loadMaterialCodesForCleaner(
|
private async loadMaterialCodesForCleaner(
|
||||||
dbService: ValidationDatabaseService,
|
dbService: ValidationDatabaseService,
|
||||||
username: string,
|
username: string,
|
||||||
isAdmin: boolean
|
isAdmin: boolean,
|
||||||
|
selectedManagers: string[],
|
||||||
|
orderNumbers: string[]
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
const markedTableName = getValidationTableName('dbo_MaterialsToBeDeleted')
|
const markedTableName = getValidationTableName('dbo_MaterialsToBeDeleted')
|
||||||
|
|
||||||
if (isAdmin) {
|
// Admin with selected managers: filter MaterialsToBeDeleted by ManagerName IN (selectedManagers)
|
||||||
const result = await dbService.query(
|
if (isAdmin && selectedManagers && selectedManagers.length > 0) {
|
||||||
`
|
const materialCodes = await this.queryMaterialCodesByManagers(
|
||||||
SELECT MaterialCode
|
dbService,
|
||||||
FROM ${markedTableName}
|
markedTableName,
|
||||||
WHERE MaterialCode IS NOT NULL
|
selectedManagers
|
||||||
`
|
|
||||||
)
|
)
|
||||||
const materialCodes = result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
|
log.info(`Admin with selected managers: got ${materialCodes.length} materials`, {
|
||||||
log.info(`Admin user: got ${materialCodes.length} materials`, {
|
|
||||||
userId: username,
|
userId: username,
|
||||||
isAdmin: true,
|
isAdmin: true,
|
||||||
|
selectedManagers,
|
||||||
materialCount: materialCodes.length
|
materialCount: materialCodes.length
|
||||||
})
|
})
|
||||||
return materialCodes
|
return materialCodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Admin without selected managers: query all materials from DiscreteMaterialPlanData by orderNumbers
|
||||||
|
if (isAdmin) {
|
||||||
|
if (orderNumbers.length === 0) {
|
||||||
|
log.warn('Admin without selected managers but no orderNumbers available', {
|
||||||
|
userId: username
|
||||||
|
})
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
const materialDao = new DiscreteMaterialPlanDAO()
|
||||||
|
const records = await materialDao.queryBySourceNumbersDistinct(orderNumbers)
|
||||||
|
const materialCodes = [
|
||||||
|
...new Set(records.map((r) => r.MaterialCode as string).filter(Boolean))
|
||||||
|
]
|
||||||
|
log.info(
|
||||||
|
`Admin without selected managers: got ${materialCodes.length} materials from DiscreteMaterialPlanData`,
|
||||||
|
{
|
||||||
|
userId: username,
|
||||||
|
isAdmin: true,
|
||||||
|
orderCount: orderNumbers.length,
|
||||||
|
materialCount: materialCodes.length
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return materialCodes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular user: filter MaterialsToBeDeleted by ManagerName = username
|
||||||
if (dbService.type === 'sqlserver') {
|
if (dbService.type === 'sqlserver') {
|
||||||
const sql = await import('mssql')
|
const sql = await import('mssql')
|
||||||
const result = await (dbService as SqlServerService).queryWithParams(
|
const result = await (dbService as SqlServerService).queryWithParams(
|
||||||
@@ -521,6 +568,24 @@ export class ValidationApplicationService {
|
|||||||
return materialCodes
|
return materialCodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dbService.type === 'postgresql') {
|
||||||
|
const result = await dbService.query(
|
||||||
|
`
|
||||||
|
SELECT "MaterialCode"
|
||||||
|
FROM ${markedTableName}
|
||||||
|
WHERE "ManagerName" = $1 AND "MaterialCode" IS NOT NULL
|
||||||
|
`,
|
||||||
|
[username]
|
||||||
|
)
|
||||||
|
const materialCodes = result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
|
||||||
|
log.info(`Regular user: got ${materialCodes.length} materials`, {
|
||||||
|
userId: username,
|
||||||
|
isAdmin: false,
|
||||||
|
materialCount: materialCodes.length
|
||||||
|
})
|
||||||
|
return materialCodes
|
||||||
|
}
|
||||||
|
|
||||||
const result = await dbService.query(
|
const result = await dbService.query(
|
||||||
`
|
`
|
||||||
SELECT MaterialCode
|
SELECT MaterialCode
|
||||||
@@ -538,6 +603,57 @@ export class ValidationApplicationService {
|
|||||||
return materialCodes
|
return materialCodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async queryMaterialCodesByManagers(
|
||||||
|
dbService: ValidationDatabaseService,
|
||||||
|
tableName: string,
|
||||||
|
managers: string[]
|
||||||
|
): Promise<string[]> {
|
||||||
|
if (dbService.type === 'sqlserver') {
|
||||||
|
const sql = await import('mssql')
|
||||||
|
const params: Record<string, { value: string; type: any }> = {}
|
||||||
|
const paramNames = managers.map((m, i) => {
|
||||||
|
const name = `@manager${i}`
|
||||||
|
params[`manager${i}`] = { value: m, type: sql.default.NVarChar }
|
||||||
|
return name
|
||||||
|
})
|
||||||
|
const result = await (dbService as SqlServerService).queryWithParams(
|
||||||
|
`
|
||||||
|
SELECT MaterialCode
|
||||||
|
FROM ${tableName}
|
||||||
|
WHERE ManagerName IN (${paramNames.join(', ')}) AND MaterialCode IS NOT NULL
|
||||||
|
`,
|
||||||
|
params
|
||||||
|
)
|
||||||
|
return result.rows
|
||||||
|
.map((row: Record<string, unknown>) => row.MaterialCode as string)
|
||||||
|
.filter(Boolean)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dbService.type === 'postgresql') {
|
||||||
|
const placeholders = managers.map((_, i) => `$${i + 1}`).join(', ')
|
||||||
|
const result = await dbService.query(
|
||||||
|
`
|
||||||
|
SELECT "MaterialCode"
|
||||||
|
FROM ${tableName}
|
||||||
|
WHERE "ManagerName" IN (${placeholders}) AND "MaterialCode" IS NOT NULL
|
||||||
|
`,
|
||||||
|
managers
|
||||||
|
)
|
||||||
|
return result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
|
||||||
|
}
|
||||||
|
|
||||||
|
const placeholders = managers.map(() => '?').join(', ')
|
||||||
|
const result = await dbService.query(
|
||||||
|
`
|
||||||
|
SELECT MaterialCode
|
||||||
|
FROM ${tableName}
|
||||||
|
WHERE ManagerName IN (${placeholders}) AND MaterialCode IS NOT NULL
|
||||||
|
`,
|
||||||
|
managers
|
||||||
|
)
|
||||||
|
return result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
|
||||||
|
}
|
||||||
|
|
||||||
private async disconnectQuietly(dbService: ValidationDatabaseService): Promise<void> {
|
private async disconnectQuietly(dbService: ValidationDatabaseService): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await dbService.disconnect()
|
await dbService.disconnect()
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { ConfigManager } from '../config/config-manager'
|
import { ConfigManager } from '../config/config-manager'
|
||||||
import { MySqlService } from '../database/mysql'
|
import { MySqlService } from '../database/mysql'
|
||||||
import { SqlServerService } from '../database/sql-server'
|
import { SqlServerService } from '../database/sql-server'
|
||||||
|
import { PostgreSqlService } from '../database/postgresql'
|
||||||
|
|
||||||
export type ValidationDatabaseService = MySqlService | SqlServerService
|
export type ValidationDatabaseService = MySqlService | SqlServerService | PostgreSqlService
|
||||||
|
|
||||||
export async function createValidationDatabaseService(): Promise<ValidationDatabaseService> {
|
export async function createValidationDatabaseService(): Promise<ValidationDatabaseService> {
|
||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
@@ -26,6 +27,19 @@ export async function createValidationDatabaseService(): Promise<ValidationDatab
|
|||||||
return sqlServerService
|
return sqlServerService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dbType === 'postgresql') {
|
||||||
|
const dbConfig = config.database.postgresql
|
||||||
|
const pgService = new PostgreSqlService({
|
||||||
|
host: dbConfig.host,
|
||||||
|
port: dbConfig.port,
|
||||||
|
user: dbConfig.username,
|
||||||
|
password: dbConfig.password,
|
||||||
|
database: dbConfig.database
|
||||||
|
})
|
||||||
|
await pgService.connect()
|
||||||
|
return pgService
|
||||||
|
}
|
||||||
|
|
||||||
const dbConfig = config.database.mysql
|
const dbConfig = config.database.mysql
|
||||||
const mysqlService = new MySqlService({
|
const mysqlService = new MySqlService({
|
||||||
host: dbConfig.host,
|
host: dbConfig.host,
|
||||||
@@ -42,14 +56,20 @@ export function getValidationTableName(mysqlTableName: string): string {
|
|||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const dbType = configManager.getDatabaseType()
|
const dbType = configManager.getDatabaseType()
|
||||||
|
|
||||||
if (dbType === 'sqlserver') {
|
if (dbType === 'sqlserver' || dbType === 'postgresql') {
|
||||||
const firstUnderscoreIndex = mysqlTableName.indexOf('_')
|
const firstUnderscoreIndex = mysqlTableName.indexOf('_')
|
||||||
if (firstUnderscoreIndex > 0) {
|
if (firstUnderscoreIndex > 0) {
|
||||||
const schema = mysqlTableName.substring(0, firstUnderscoreIndex)
|
const schema = mysqlTableName.substring(0, firstUnderscoreIndex)
|
||||||
const tableName = mysqlTableName.substring(firstUnderscoreIndex + 1)
|
const tableName = mysqlTableName.substring(firstUnderscoreIndex + 1)
|
||||||
return `[${schema}].[${tableName}]`
|
if (dbType === 'sqlserver') {
|
||||||
|
return `[${schema}].[${tableName}]`
|
||||||
|
}
|
||||||
|
return `"${schema}"."${tableName}"`
|
||||||
}
|
}
|
||||||
return `[dbo].[${mysqlTableName}]`
|
if (dbType === 'sqlserver') {
|
||||||
|
return `[dbo].[${mysqlTableName}]`
|
||||||
|
}
|
||||||
|
return `"public"."${mysqlTableName}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
return mysqlTableName
|
return mysqlTableName
|
||||||
|
|||||||
@@ -10,23 +10,31 @@ export enum AuditAction {
|
|||||||
LOGOUT = 'LOGOUT',
|
LOGOUT = 'LOGOUT',
|
||||||
EXTRACT = 'EXTRACT',
|
EXTRACT = 'EXTRACT',
|
||||||
CLEAN = 'CLEAN',
|
CLEAN = 'CLEAN',
|
||||||
SETTINGS_CHANGE = 'SETTINGS_CHANGE'
|
SETTINGS_CHANGE = 'SETTINGS_CHANGE',
|
||||||
|
SYSTEM_CRASH = 'SYSTEM_CRASH',
|
||||||
|
SYSTEM_ERROR = 'SYSTEM_ERROR',
|
||||||
|
DATA_IMPORT = 'DATA_IMPORT',
|
||||||
|
RESULT_EXPORT = 'RESULT_EXPORT',
|
||||||
|
APP_UPDATE = 'APP_UPDATE',
|
||||||
|
ERP_CREDENTIALS_UPDATE = 'ERP_CREDENTIALS_UPDATE'
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Audit status enumeration
|
* Audit status enumeration
|
||||||
|
* Note: Values are lowercase to match JSON logging conventions
|
||||||
*/
|
*/
|
||||||
export enum AuditStatus {
|
export enum AuditStatus {
|
||||||
SUCCESS = 'SUCCESS',
|
SUCCESS = 'success',
|
||||||
FAILURE = 'FAILURE'
|
FAILURE = 'failure',
|
||||||
|
PARTIAL = 'partial'
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Audit entry interface
|
* Audit entry interface
|
||||||
*/
|
*/
|
||||||
export interface AuditEntry {
|
export interface AuditEntry {
|
||||||
/** Timestamp of the action */
|
/** ISO timestamp of the action */
|
||||||
timestamp: Date
|
timestamp: string
|
||||||
/** Action performed */
|
/** Action performed */
|
||||||
action: AuditAction
|
action: AuditAction
|
||||||
/** User ID who performed the action */
|
/** User ID who performed the action */
|
||||||
@@ -38,9 +46,9 @@ export interface AuditEntry {
|
|||||||
/** Application version when action was performed */
|
/** Application version when action was performed */
|
||||||
appVersion: string
|
appVersion: string
|
||||||
/** Resource affected by the action */
|
/** Resource affected by the action */
|
||||||
resource?: string
|
resource: string
|
||||||
/** Status of the action */
|
/** Status of the action */
|
||||||
status: AuditStatus
|
status: AuditStatus
|
||||||
/** Additional metadata in JSON format */
|
/** Additional metadata */
|
||||||
metadata?: string
|
metadata: Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|||||||
113
src/main/types/cleaner-history.types.ts
Normal file
113
src/main/types/cleaner-history.types.ts
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
// src/main/types/cleaner-history.types.ts
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleaner 操作历史类型定义
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 执行级记录 */
|
||||||
|
export interface CleanerExecutionRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: Date
|
||||||
|
endTime: Date | null
|
||||||
|
status: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsSkipped: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
totalUncertainDeletions: number
|
||||||
|
errorMessage: string | null
|
||||||
|
appVersion: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 订单级记录 */
|
||||||
|
export interface CleanerOrderRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
productionId: string | null
|
||||||
|
status: string
|
||||||
|
materialsDeleted: number
|
||||||
|
materialsSkipped: number
|
||||||
|
materialsFailed: number
|
||||||
|
uncertainDeletions: number
|
||||||
|
retryCount: number
|
||||||
|
retrySuccess: boolean
|
||||||
|
errorMessage: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 物料级记录 */
|
||||||
|
export interface CleanerMaterialRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 批次统计(前端列表展示用) */
|
||||||
|
export interface CleanerBatchStats {
|
||||||
|
batchId: string
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string
|
||||||
|
status: string
|
||||||
|
totalAttempts: number
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
successCount: number
|
||||||
|
failedCount: number
|
||||||
|
isDryRun: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入执行记录的输入 */
|
||||||
|
export interface InsertCleanerExecutionInput {
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
appVersion: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入订单记录的输入 */
|
||||||
|
export interface InsertOrderInput {
|
||||||
|
orderNumber: string
|
||||||
|
productionId?: string
|
||||||
|
initialStatus?: string
|
||||||
|
errorMessage?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 插入物料明细的输入 */
|
||||||
|
export interface InsertMaterialDetailInput {
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 查询批次的选项 */
|
||||||
|
export interface GetCleanerBatchesOptions {
|
||||||
|
limit?: number
|
||||||
|
offset?: number
|
||||||
|
usernames?: string[]
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export type CleanerPhase = 'login' | 'processing' | 'complete'
|
export type CleanerPhase = 'login' | 'processing' | 'complete' | 'retry'
|
||||||
|
|
||||||
export interface CleanerProgress {
|
export interface CleanerProgress {
|
||||||
message: string
|
message: string
|
||||||
@@ -30,6 +30,11 @@ export interface CleanerResult {
|
|||||||
// Retry statistics
|
// Retry statistics
|
||||||
retriedOrders: number
|
retriedOrders: number
|
||||||
successfulRetries: number
|
successfulRetries: number
|
||||||
|
// Deletion verification statistics
|
||||||
|
materialsFailed: number
|
||||||
|
uncertainDeletions: number
|
||||||
|
// Outer retry: true when outer catch triggered (browser crash / fatal timeout)
|
||||||
|
crashed?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SkippedMaterial {
|
export interface SkippedMaterial {
|
||||||
@@ -39,6 +44,13 @@ export interface SkippedMaterial {
|
|||||||
reason: string
|
reason: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DeletedMaterial {
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
outcome: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface RetryAttempt {
|
export interface RetryAttempt {
|
||||||
attempt: number
|
attempt: number
|
||||||
error: string
|
error: string
|
||||||
@@ -51,11 +63,58 @@ export interface OrderCleanDetail {
|
|||||||
materialsSkipped: number
|
materialsSkipped: number
|
||||||
errors: string[]
|
errors: string[]
|
||||||
skippedMaterials: SkippedMaterial[]
|
skippedMaterials: SkippedMaterial[]
|
||||||
|
deletedMaterials: DeletedMaterial[]
|
||||||
// Retry-related fields
|
// Retry-related fields
|
||||||
retryCount: number
|
retryCount: number
|
||||||
retryAttempts?: RetryAttempt[]
|
retryAttempts?: RetryAttempt[]
|
||||||
retriedAt?: number
|
retriedAt?: number
|
||||||
retrySuccess?: boolean
|
retrySuccess?: boolean
|
||||||
|
// Deletion verification fields
|
||||||
|
materialsFailed: number
|
||||||
|
failedMaterials: FailedMaterial[]
|
||||||
|
uncertainDeletions: number
|
||||||
|
// Missing order flag: true when order not found in ERP query results
|
||||||
|
notFound?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum DeletionOutcome {
|
||||||
|
Success = 'success',
|
||||||
|
FailedErpError = 'failed_erp_error',
|
||||||
|
FailedNoChange = 'failed_no_change',
|
||||||
|
FailedTimeout = 'failed_timeout',
|
||||||
|
FailedButtonDisabled = 'failed_button_disabled',
|
||||||
|
Uncertain = 'uncertain'
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum DeletionErrorCategory {
|
||||||
|
ErpRejection = 'erp_rejection',
|
||||||
|
ErpBusy = 'erp_busy',
|
||||||
|
NetworkLag = 'network_lag',
|
||||||
|
UiUnexpected = 'ui_unexpected',
|
||||||
|
VerificationTimeout = 'verification_timeout',
|
||||||
|
Unknown = 'unknown'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MaterialDeletionAttempt {
|
||||||
|
attempt: number
|
||||||
|
outcome: DeletionOutcome
|
||||||
|
errorCategory?: DeletionErrorCategory
|
||||||
|
errorMessage?: string
|
||||||
|
rowNumberBefore: string
|
||||||
|
rowNumberAfter: string
|
||||||
|
materialCountBefore: number
|
||||||
|
materialCountAfter: number
|
||||||
|
timestamp: number
|
||||||
|
durationMs: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FailedMaterial {
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
attempts: MaterialDeletionAttempt[]
|
||||||
|
finalOutcome: DeletionOutcome
|
||||||
|
finalErrorCategory?: DeletionErrorCategory
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { z } from 'zod'
|
|||||||
/**
|
/**
|
||||||
* 数据库类型枚举
|
* 数据库类型枚举
|
||||||
*/
|
*/
|
||||||
export const databaseTypeSchema = z.enum(['mysql', 'sqlserver'])
|
export const databaseTypeSchema = z.enum(['mysql', 'sqlserver', 'postgresql'])
|
||||||
export type DatabaseType = z.infer<typeof databaseTypeSchema>
|
export type DatabaseType = z.infer<typeof databaseTypeSchema>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,13 +57,27 @@ export const sqlServerConfigSchema = z.object({
|
|||||||
trustServerCertificate: z.boolean().default(true)
|
trustServerCertificate: z.boolean().default(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL 配置 Schema
|
||||||
|
*/
|
||||||
|
export const postgresqlConfigSchema = z.object({
|
||||||
|
host: z.string().min(1, 'PostgreSQL host is required'),
|
||||||
|
port: z.number().int().min(1).max(65535).default(5432),
|
||||||
|
database: z.string().min(1, 'PostgreSQL database is required'),
|
||||||
|
username: z.string().min(1, 'PostgreSQL username is required'),
|
||||||
|
password: z.string(),
|
||||||
|
maxPoolSize: z.number().int().min(1).max(100).default(10)
|
||||||
|
})
|
||||||
|
export type PostgreSqlConfigSchema = z.infer<typeof postgresqlConfigSchema>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库配置(包含两种数据库的完整配置)
|
* 数据库配置(包含两种数据库的完整配置)
|
||||||
*/
|
*/
|
||||||
export const databaseConfigSchema = z.object({
|
export const databaseConfigSchema = z.object({
|
||||||
activeType: databaseTypeSchema.default('mysql'),
|
activeType: databaseTypeSchema.default('mysql'),
|
||||||
mysql: mysqlConfigSchema,
|
mysql: mysqlConfigSchema,
|
||||||
sqlserver: sqlServerConfigSchema
|
sqlserver: sqlServerConfigSchema,
|
||||||
|
postgresql: postgresqlConfigSchema
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -199,6 +213,7 @@ export type FullConfig = z.infer<typeof fullConfigSchema>
|
|||||||
export type DatabaseConfig = z.infer<typeof databaseConfigSchema>
|
export type DatabaseConfig = z.infer<typeof databaseConfigSchema>
|
||||||
export type MySqlConfig = z.infer<typeof mysqlConfigSchema>
|
export type MySqlConfig = z.infer<typeof mysqlConfigSchema>
|
||||||
export type SqlServerConfig = z.infer<typeof sqlServerConfigSchema>
|
export type SqlServerConfig = z.infer<typeof sqlServerConfigSchema>
|
||||||
|
export type PostgreSqlConfig = z.infer<typeof postgresqlConfigSchema>
|
||||||
export type ErpSystemConfig = z.infer<typeof erpSystemConfigSchema>
|
export type ErpSystemConfig = z.infer<typeof erpSystemConfigSchema>
|
||||||
export type LoggingConfig = z.infer<typeof loggingConfigSchema>
|
export type LoggingConfig = z.infer<typeof loggingConfigSchema>
|
||||||
export type RustfsConfig = z.infer<typeof rustfsConfigSchema>
|
export type RustfsConfig = z.infer<typeof rustfsConfigSchema>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
/**
|
/**
|
||||||
* Supported database types
|
* Supported database types
|
||||||
*/
|
*/
|
||||||
export type DatabaseType = 'mysql' | 'sqlserver'
|
export type DatabaseType = 'mysql' | 'sqlserver' | 'postgresql'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard query result interface
|
* Standard query result interface
|
||||||
@@ -102,3 +102,15 @@ export interface SqlServerConfig extends DatabaseConfig {
|
|||||||
trustServerCertificate?: boolean
|
trustServerCertificate?: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL-specific configuration
|
||||||
|
*/
|
||||||
|
export interface PostgreSqlConfig extends DatabaseConfig {
|
||||||
|
host: string
|
||||||
|
port: number
|
||||||
|
user: string
|
||||||
|
password: string
|
||||||
|
database: string
|
||||||
|
maxPoolSize?: number
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ import type {
|
|||||||
ExportResultItem,
|
ExportResultItem,
|
||||||
ExportResultResponse
|
ExportResultResponse
|
||||||
} from './cleaner.types'
|
} from './cleaner.types'
|
||||||
|
import type {
|
||||||
|
CleanerBatchStats,
|
||||||
|
CleanerExecutionRecord,
|
||||||
|
CleanerOrderRecord,
|
||||||
|
CleanerMaterialRecord
|
||||||
|
} from './cleaner-history.types'
|
||||||
import type { IpcResult } from './ipc.types'
|
import type { IpcResult } from './ipc.types'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,6 +119,45 @@ export interface CleanerAPI {
|
|||||||
* @returns Unsubscribe function
|
* @returns Unsubscribe function
|
||||||
*/
|
*/
|
||||||
onProgress: (callback: (data: CleanerProgress) => void) => () => void
|
onProgress: (callback: (data: CleanerProgress) => void) => () => void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get cleaner operation history batches
|
||||||
|
* @param options - Query options (limit, offset, usernames)
|
||||||
|
*/
|
||||||
|
getHistoryBatches: (options?: {
|
||||||
|
limit?: number
|
||||||
|
offset?: number
|
||||||
|
usernames?: string[]
|
||||||
|
}) => Promise<IpcResult<CleanerBatchStats[]>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get detailed execution and order records for a specific batch
|
||||||
|
* @param batchId - Batch ID
|
||||||
|
*/
|
||||||
|
getHistoryBatchDetails: (batchId: string) => Promise<
|
||||||
|
IpcResult<{
|
||||||
|
executions: CleanerExecutionRecord[]
|
||||||
|
orders: CleanerOrderRecord[]
|
||||||
|
}>
|
||||||
|
>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get material-level details for a specific order in a batch attempt
|
||||||
|
* @param batchId - Batch ID
|
||||||
|
* @param attemptNumber - Attempt number
|
||||||
|
* @param orderNumber - Order number
|
||||||
|
*/
|
||||||
|
getHistoryMaterialDetails: (
|
||||||
|
batchId: string,
|
||||||
|
attemptNumber: number,
|
||||||
|
orderNumber: string
|
||||||
|
) => Promise<IpcResult<CleanerMaterialRecord[]>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a cleaner history batch
|
||||||
|
* @param batchId - Batch ID
|
||||||
|
*/
|
||||||
|
deleteHistoryBatch: (batchId: string) => Promise<IpcResult<{ deleted: boolean }>>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
78
src/main/types/sql-dialect.types.ts
Normal file
78
src/main/types/sql-dialect.types.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
* SQL Dialect Abstraction
|
||||||
|
*
|
||||||
|
* Provides a unified interface for database-specific SQL syntax differences.
|
||||||
|
* Each database type implements this interface to encapsulate:
|
||||||
|
* - Parameter placeholder format
|
||||||
|
* - Table name quoting
|
||||||
|
* - UPSERT syntax
|
||||||
|
* - Pagination syntax
|
||||||
|
* - Current timestamp function
|
||||||
|
* - Batch size limits
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { DatabaseType } from './database.types'
|
||||||
|
|
||||||
|
export interface SqlDialect {
|
||||||
|
/** Database type identifier */
|
||||||
|
readonly dbType: DatabaseType
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quote a table name with schema prefix
|
||||||
|
* MySQL: dbo_TableName
|
||||||
|
* SQL Server: [dbo].[TableName]
|
||||||
|
* PostgreSQL: "dbo"."TableName"
|
||||||
|
*/
|
||||||
|
quoteTableName(schema: string, table: string): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get placeholder for parameter at given index (0-based)
|
||||||
|
* MySQL: ?
|
||||||
|
* SQL Server: @p0
|
||||||
|
* PostgreSQL: $1
|
||||||
|
*/
|
||||||
|
param(index: number): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get comma-separated placeholders for count parameters
|
||||||
|
*/
|
||||||
|
params(count: number): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current timestamp SQL function
|
||||||
|
* MySQL: NOW()
|
||||||
|
* SQL Server: GETDATE()
|
||||||
|
* PostgreSQL: CURRENT_TIMESTAMP
|
||||||
|
*/
|
||||||
|
currentTimestamp(): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate UPSERT SQL for a single row
|
||||||
|
* MySQL: INSERT ... ON DUPLICATE KEY UPDATE
|
||||||
|
* SQL Server: MERGE ... USING ...
|
||||||
|
* PostgreSQL: INSERT ... ON CONFLICT ... DO UPDATE SET
|
||||||
|
*/
|
||||||
|
upsert(params: {
|
||||||
|
table: string
|
||||||
|
keyColumns: string[]
|
||||||
|
allColumns: string[]
|
||||||
|
startParamIndex: number
|
||||||
|
}): { sql: string; nextParamIndex: number }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append pagination clause to SQL
|
||||||
|
* MySQL/PostgreSQL: LIMIT x OFFSET y
|
||||||
|
* SQL Server: OFFSET x ROWS FETCH NEXT y ROWS ONLY
|
||||||
|
*/
|
||||||
|
paginate(params: { sql: string; limit: number; offset?: number; paramIndex: number }): {
|
||||||
|
sql: string
|
||||||
|
nextParamIndex: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum rows per batch given columns per row
|
||||||
|
* SQL Server: ~71 (due to 2100 param limit)
|
||||||
|
* MySQL/PostgreSQL: 1000
|
||||||
|
*/
|
||||||
|
maxBatchRows(columnsPerRow: number): number
|
||||||
|
}
|
||||||
@@ -3,6 +3,14 @@ import type {
|
|||||||
CleanerProgress,
|
CleanerProgress,
|
||||||
ExportResultItem
|
ExportResultItem
|
||||||
} from '../../main/types/cleaner.types'
|
} from '../../main/types/cleaner.types'
|
||||||
|
import type {
|
||||||
|
CleanerBatchStats,
|
||||||
|
CleanerExecutionRecord,
|
||||||
|
CleanerOrderRecord,
|
||||||
|
CleanerMaterialRecord,
|
||||||
|
GetCleanerBatchesOptions
|
||||||
|
} from '../../main/types/cleaner-history.types'
|
||||||
|
import type { IpcResult } from '../../main/types/ipc.types'
|
||||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||||
import { invokeIpc, ipcRenderer } from '../lib/ipc'
|
import { invokeIpc, ipcRenderer } from '../lib/ipc'
|
||||||
|
|
||||||
@@ -15,5 +23,35 @@ export const cleanerApi = {
|
|||||||
callback(data)
|
callback(data)
|
||||||
ipcRenderer.on(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
ipcRenderer.on(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
return () => ipcRenderer.removeListener(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||||
}
|
},
|
||||||
|
|
||||||
|
// Cleaner operation history
|
||||||
|
getHistoryBatches: (
|
||||||
|
options?: GetCleanerBatchesOptions
|
||||||
|
): Promise<IpcResult<CleanerBatchStats[]>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_GET_BATCHES, options),
|
||||||
|
|
||||||
|
getHistoryBatchDetails: (
|
||||||
|
batchId: string
|
||||||
|
): Promise<
|
||||||
|
IpcResult<{
|
||||||
|
executions: CleanerExecutionRecord[]
|
||||||
|
orders: CleanerOrderRecord[]
|
||||||
|
}>
|
||||||
|
> => invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_GET_BATCH_DETAILS, batchId),
|
||||||
|
|
||||||
|
getHistoryMaterialDetails: (
|
||||||
|
batchId: string,
|
||||||
|
attemptNumber: number,
|
||||||
|
orderNumber: string
|
||||||
|
): Promise<IpcResult<CleanerMaterialRecord[]>> =>
|
||||||
|
invokeIpc(
|
||||||
|
IPC_CHANNELS.CLEANER_HISTORY_GET_MATERIAL_DETAILS,
|
||||||
|
batchId,
|
||||||
|
attemptNumber,
|
||||||
|
orderNumber
|
||||||
|
),
|
||||||
|
|
||||||
|
deleteHistoryBatch: (batchId: string): Promise<IpcResult<{ deleted: boolean }>> =>
|
||||||
|
invokeIpc(IPC_CHANNELS.CLEANER_HISTORY_DELETE_BATCH, batchId)
|
||||||
} as const
|
} as const
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ export const validationApi = {
|
|||||||
invokeIpc(IPC_CHANNELS.VALIDATION_SET_SHARED_PRODUCTION_IDS, productionIds),
|
invokeIpc(IPC_CHANNELS.VALIDATION_SET_SHARED_PRODUCTION_IDS, productionIds),
|
||||||
getSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_GET_SHARED_PRODUCTION_IDS),
|
getSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_GET_SHARED_PRODUCTION_IDS),
|
||||||
clearSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_CLEAR_SHARED_PRODUCTION_IDS),
|
clearSharedProductionIds: () => invokeIpc(IPC_CHANNELS.VALIDATION_CLEAR_SHARED_PRODUCTION_IDS),
|
||||||
getCleanerData: () => invokeIpc(IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA)
|
getCleanerData: (params?: { selectedManagers?: string[] }) =>
|
||||||
|
invokeIpc(IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA, params ?? { selectedManagers: [] })
|
||||||
} as const
|
} as const
|
||||||
|
|||||||
87
src/preload/index.d.ts
vendored
87
src/preload/index.d.ts
vendored
@@ -59,7 +59,7 @@ export interface ValidationAPI {
|
|||||||
setSharedProductionIds: (productionIds: string[]) => Promise<IpcResult<void>>
|
setSharedProductionIds: (productionIds: string[]) => Promise<IpcResult<void>>
|
||||||
getSharedProductionIds: () => Promise<IpcResult<{ productionIds: string[] }>>
|
getSharedProductionIds: () => Promise<IpcResult<{ productionIds: string[] }>>
|
||||||
clearSharedProductionIds: () => Promise<IpcResult<void>>
|
clearSharedProductionIds: () => Promise<IpcResult<void>>
|
||||||
getCleanerData: () => Promise<
|
getCleanerData: (params?: { selectedManagers?: string[] }) => Promise<
|
||||||
IpcResult<{
|
IpcResult<{
|
||||||
orderNumbers: string[]
|
orderNumbers: string[]
|
||||||
materialCodes: string[]
|
materialCodes: string[]
|
||||||
@@ -190,6 +190,91 @@ export interface OperationHistoryRecord {
|
|||||||
errorMessage: string | null
|
errorMessage: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryBatchStats {
|
||||||
|
batchId: string
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string
|
||||||
|
status: string
|
||||||
|
totalAttempts: number
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
successCount: number
|
||||||
|
failedCount: number
|
||||||
|
isDryRun: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryExecutionRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: Date
|
||||||
|
endTime: Date | null
|
||||||
|
status: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsSkipped: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
totalUncertainDeletions: number
|
||||||
|
errorMessage: string | null
|
||||||
|
appVersion: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryOrderRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
status: string
|
||||||
|
materialsDeleted: number
|
||||||
|
materialsSkipped: number
|
||||||
|
materialsFailed: number
|
||||||
|
uncertainDeletions: number
|
||||||
|
retryCount: number
|
||||||
|
retrySuccess: boolean
|
||||||
|
errorMessage: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryMaterialRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryAPI {
|
||||||
|
getHistoryBatches: (options?: {
|
||||||
|
limit?: number
|
||||||
|
offset?: number
|
||||||
|
usernames?: string[]
|
||||||
|
}) => Promise<IpcResult<CleanerHistoryBatchStats[]>>
|
||||||
|
getHistoryBatchDetails: (batchId: string) => Promise<
|
||||||
|
IpcResult<{
|
||||||
|
executions: CleanerHistoryExecutionRecord[]
|
||||||
|
orders: CleanerHistoryOrderRecord[]
|
||||||
|
}>
|
||||||
|
>
|
||||||
|
getHistoryMaterialDetails: (
|
||||||
|
batchId: string,
|
||||||
|
attemptNumber: number,
|
||||||
|
orderNumber: string
|
||||||
|
) => Promise<IpcResult<CleanerHistoryMaterialRecord[]>>
|
||||||
|
deleteHistoryBatch: (batchId: string) => Promise<IpcResult<{ deleted: boolean }>>
|
||||||
|
}
|
||||||
|
|
||||||
export interface ProcessAPI {
|
export interface ProcessAPI {
|
||||||
versions: {
|
versions: {
|
||||||
electron: string
|
electron: string
|
||||||
|
|||||||
858
src/renderer/src/components/CleanerOperationHistoryModal.tsx
Normal file
858
src/renderer/src/components/CleanerOperationHistoryModal.tsx
Normal file
@@ -0,0 +1,858 @@
|
|||||||
|
/**
|
||||||
|
* Cleaner Operation History Modal
|
||||||
|
*
|
||||||
|
* Displays cleaner operation history with batch statistics, execution records,
|
||||||
|
* order details, and material-level details.
|
||||||
|
* Admin users see all users' records, regular users see only their own.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React, { useState, useEffect, useCallback, useRef } from 'react'
|
||||||
|
import { Modal } from './ui/Modal'
|
||||||
|
import { useLogger } from '../hooks/useLogger'
|
||||||
|
import {
|
||||||
|
RefreshCw,
|
||||||
|
Trash2,
|
||||||
|
ChevronDown,
|
||||||
|
ChevronRight,
|
||||||
|
CheckCircle,
|
||||||
|
XCircle,
|
||||||
|
Clock,
|
||||||
|
Copy,
|
||||||
|
FlaskConical
|
||||||
|
} from 'lucide-react'
|
||||||
|
import type { UserInfo } from './UserSelectionDialog'
|
||||||
|
import type {
|
||||||
|
CleanerHistoryBatchStats,
|
||||||
|
CleanerHistoryOrderRecord,
|
||||||
|
CleanerHistoryMaterialRecord
|
||||||
|
} from '../hooks/cleaner/types'
|
||||||
|
|
||||||
|
// The preload API returns Date for time fields, but IPC serialization converts them to strings.
|
||||||
|
// Use a local type that accommodates both to satisfy TypeScript.
|
||||||
|
interface ExecutionRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string | Date
|
||||||
|
endTime: string | Date | null
|
||||||
|
status: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsSkipped: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
totalUncertainDeletions: number
|
||||||
|
errorMessage: string | null
|
||||||
|
appVersion: string | null
|
||||||
|
}
|
||||||
|
import { showSuccess, showError, showWarning } from '../stores/useAppStore'
|
||||||
|
|
||||||
|
interface CleanerOperationHistoryModalProps {
|
||||||
|
isOpen: boolean
|
||||||
|
onClose: () => void
|
||||||
|
user?: { username: string; userType: string } | null
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BatchItemProps {
|
||||||
|
batch: CleanerHistoryBatchStats
|
||||||
|
isAdmin: boolean
|
||||||
|
onDelete: (batchId: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusStyles: Record<string, string> = {
|
||||||
|
success: 'bg-green-100 text-green-700',
|
||||||
|
partial: 'bg-amber-100 text-amber-700',
|
||||||
|
failed: 'bg-red-100 text-red-700',
|
||||||
|
crashed: 'bg-red-100 text-red-700',
|
||||||
|
pending: 'bg-gray-100 text-gray-700',
|
||||||
|
not_found: 'bg-orange-100 text-orange-700',
|
||||||
|
erp_not_found: 'bg-orange-100 text-orange-700'
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusLabels: Record<string, string> = {
|
||||||
|
success: '成功',
|
||||||
|
partial: '部分成功',
|
||||||
|
failed: '失败',
|
||||||
|
crashed: '崩溃',
|
||||||
|
pending: '进行中',
|
||||||
|
not_found: '未找到',
|
||||||
|
erp_not_found: 'ERP不存在'
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusIcons: Record<string, React.ReactNode> = {
|
||||||
|
success: <CheckCircle size={16} className="text-green-600" />,
|
||||||
|
partial: <Clock size={16} className="text-amber-600" />,
|
||||||
|
failed: <XCircle size={16} className="text-red-600" />,
|
||||||
|
crashed: <XCircle size={16} className="text-red-600" />,
|
||||||
|
pending: <Clock size={16} className="text-gray-500" />,
|
||||||
|
not_found: <XCircle size={16} className="text-orange-600" />,
|
||||||
|
erp_not_found: <XCircle size={16} className="text-orange-600" />
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatDateTime = (dateStr: string | Date | null | undefined): string => {
|
||||||
|
if (!dateStr) return '-'
|
||||||
|
const date = typeof dateStr === 'string' ? new Date(dateStr) : dateStr
|
||||||
|
|
||||||
|
if (isNaN(date.getTime())) {
|
||||||
|
return String(dateStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use local time instead of UTC for display
|
||||||
|
const year = date.getFullYear()
|
||||||
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
|
const day = String(date.getDate()).padStart(2, '0')
|
||||||
|
const hours = String(date.getHours()).padStart(2, '0')
|
||||||
|
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||||
|
const seconds = String(date.getSeconds()).padStart(2, '0')
|
||||||
|
|
||||||
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatDuration = (startTime: string | Date | null, endTime: string | Date | null) => {
|
||||||
|
if (!startTime || !endTime) return '-'
|
||||||
|
const start = typeof startTime === 'string' ? new Date(startTime) : startTime
|
||||||
|
const end = typeof endTime === 'string' ? new Date(endTime) : endTime
|
||||||
|
if (isNaN(start.getTime()) || isNaN(end.getTime())) return '-'
|
||||||
|
|
||||||
|
const diffMs = end.getTime() - start.getTime()
|
||||||
|
const seconds = Math.floor(diffMs / 1000)
|
||||||
|
if (seconds < 60) return `${seconds}秒`
|
||||||
|
|
||||||
|
const minutes = Math.floor(seconds / 60)
|
||||||
|
const remainSeconds = seconds % 60
|
||||||
|
if (minutes < 60) return `${minutes}分${remainSeconds}秒`
|
||||||
|
|
||||||
|
const hours = Math.floor(minutes / 60)
|
||||||
|
const remainMinutes = minutes % 60
|
||||||
|
return `${hours}时${remainMinutes}分`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====== BatchItem Component ======
|
||||||
|
// Extracted from the modal so that expanding one batch doesn't re-render siblings.
|
||||||
|
// Each BatchItem manages its own details, orders, and material state locally.
|
||||||
|
const BatchItem = React.memo(({ batch, isAdmin, onDelete }: BatchItemProps) => {
|
||||||
|
const [isExpanded, setIsExpanded] = useState(false)
|
||||||
|
const [executions, setExecutions] = useState<ExecutionRecord[]>([])
|
||||||
|
const [orders, setOrders] = useState<CleanerHistoryOrderRecord[]>([])
|
||||||
|
const [currentAttempt, setCurrentAttempt] = useState<number | undefined>()
|
||||||
|
const [expandedOrders, setExpandedOrders] = useState<Set<string>>(() => new Set())
|
||||||
|
const [orderMaterials, setOrderMaterials] = useState<Map<string, CleanerHistoryMaterialRecord[]>>(
|
||||||
|
() => new Map()
|
||||||
|
)
|
||||||
|
const [loadingMaterials, setLoadingMaterials] = useState<Set<string>>(() => new Set())
|
||||||
|
const [isDeleting, setIsDeleting] = useState(false)
|
||||||
|
|
||||||
|
const detailsLoadedRef = useRef(false)
|
||||||
|
const loadedMaterialsRef = useRef<Set<string>>(new Set())
|
||||||
|
const logger = useLogger('BatchItem')
|
||||||
|
|
||||||
|
// Fetch batch details when first expanded
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isExpanded || detailsLoadedRef.current) return
|
||||||
|
detailsLoadedRef.current = true
|
||||||
|
|
||||||
|
const fetchDetails = async () => {
|
||||||
|
try {
|
||||||
|
const result = await window.electron.cleaner.getHistoryBatchDetails(batch.batchId)
|
||||||
|
if (result.success && result.data) {
|
||||||
|
setExecutions(result.data.executions)
|
||||||
|
setOrders(result.data.orders)
|
||||||
|
|
||||||
|
const execs = result.data.executions
|
||||||
|
if (execs.length > 0) {
|
||||||
|
setCurrentAttempt(Math.max(...execs.map((e) => e.attemptNumber)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Failed to fetch batch details', {
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
batchId: batch.batchId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fetchDetails()
|
||||||
|
}, [isExpanded, batch.batchId, logger])
|
||||||
|
|
||||||
|
const fetchMaterials = useCallback(
|
||||||
|
async (attemptNumber: number, orderNumber: string) => {
|
||||||
|
const cacheKey = `${attemptNumber}:${orderNumber}`
|
||||||
|
if (loadedMaterialsRef.current.has(cacheKey)) return
|
||||||
|
loadedMaterialsRef.current.add(cacheKey)
|
||||||
|
|
||||||
|
setLoadingMaterials((prev) => new Set(prev).add(cacheKey))
|
||||||
|
try {
|
||||||
|
const result = await window.electron.cleaner.getHistoryMaterialDetails(
|
||||||
|
batch.batchId,
|
||||||
|
attemptNumber,
|
||||||
|
orderNumber
|
||||||
|
)
|
||||||
|
if (result.success && result.data) {
|
||||||
|
setOrderMaterials((prev) => new Map(prev).set(cacheKey, result.data!))
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Failed to fetch material details', {
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
batchId: batch.batchId,
|
||||||
|
attemptNumber,
|
||||||
|
orderNumber
|
||||||
|
})
|
||||||
|
} finally {
|
||||||
|
setLoadingMaterials((prev) => {
|
||||||
|
const newSet = new Set(prev)
|
||||||
|
newSet.delete(cacheKey)
|
||||||
|
return newSet
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[batch.batchId, logger]
|
||||||
|
)
|
||||||
|
|
||||||
|
const toggleOrderExpansion = (attemptNumber: number, orderNumber: string) => {
|
||||||
|
const cacheKey = `${attemptNumber}:${orderNumber}`
|
||||||
|
const isCurrentlyExpanded = expandedOrders.has(cacheKey)
|
||||||
|
setExpandedOrders((prev) => {
|
||||||
|
const newSet = new Set(prev)
|
||||||
|
if (newSet.has(cacheKey)) {
|
||||||
|
newSet.delete(cacheKey)
|
||||||
|
} else {
|
||||||
|
newSet.add(cacheKey)
|
||||||
|
}
|
||||||
|
return newSet
|
||||||
|
})
|
||||||
|
if (!isCurrentlyExpanded) {
|
||||||
|
void fetchMaterials(attemptNumber, orderNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDelete = async () => {
|
||||||
|
if (isDeleting) return
|
||||||
|
const confirmed = confirm('确定要删除此批次记录吗?此操作不可撤销。')
|
||||||
|
if (!confirmed) return
|
||||||
|
|
||||||
|
setIsDeleting(true)
|
||||||
|
try {
|
||||||
|
const result = await window.electron.cleaner.deleteHistoryBatch(batch.batchId)
|
||||||
|
if (result.success) {
|
||||||
|
onDelete(batch.batchId)
|
||||||
|
} else {
|
||||||
|
alert(result.error || '删除失败')
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
alert(err instanceof Error ? err.message : '删除失败')
|
||||||
|
} finally {
|
||||||
|
setIsDeleting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCopyColumn = (field: keyof CleanerHistoryOrderRecord) => {
|
||||||
|
const filtered =
|
||||||
|
currentAttempt !== undefined
|
||||||
|
? orders.filter((o) => o.attemptNumber === currentAttempt)
|
||||||
|
: orders
|
||||||
|
const values = filtered
|
||||||
|
.map((o) => String(o[field] ?? ''))
|
||||||
|
.filter((v) => v && v !== '-')
|
||||||
|
.join('\n')
|
||||||
|
|
||||||
|
if (!values) {
|
||||||
|
showWarning('没有可复制的数据')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
navigator.clipboard
|
||||||
|
.writeText(values)
|
||||||
|
.then(() => showSuccess(`已复制 ${values.split('\n').length} 条数据`))
|
||||||
|
.catch(() => showError('复制失败,请手动复制'))
|
||||||
|
}
|
||||||
|
|
||||||
|
const filteredOrders =
|
||||||
|
currentAttempt !== undefined
|
||||||
|
? orders.filter((o) => o.attemptNumber === currentAttempt)
|
||||||
|
: orders
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="border border-gray-200 rounded-lg overflow-hidden">
|
||||||
|
{/* Batch summary */}
|
||||||
|
<div
|
||||||
|
className={`flex items-center justify-between p-4 cursor-pointer transition-colors ${
|
||||||
|
isExpanded ? 'bg-gray-50' : 'hover:bg-gray-50'
|
||||||
|
}`}
|
||||||
|
onClick={() => setIsExpanded((prev) => !prev)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-4 flex-1">
|
||||||
|
<button className="p-1 hover:bg-gray-200 rounded">
|
||||||
|
{isExpanded ? <ChevronDown size={18} /> : <ChevronRight size={18} />}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div className="flex-1 grid grid-cols-7 gap-3 text-sm">
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">操作时间</div>
|
||||||
|
<div className="font-medium text-gray-900">
|
||||||
|
{formatDateTime(batch.operationTime)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">操作用户</div>
|
||||||
|
<div className="font-medium text-gray-900">{batch.username}</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">状态</div>
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
{statusIcons[batch.status] || statusIcons.pending}
|
||||||
|
<span
|
||||||
|
className={`px-2 py-0.5 rounded text-xs font-medium ${
|
||||||
|
statusStyles[batch.status] || statusStyles.pending
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{statusLabels[batch.status] || batch.status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">订单数</div>
|
||||||
|
<div className="font-medium text-gray-900">{batch.totalOrders}</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">已删除</div>
|
||||||
|
<div className="font-medium text-green-600">
|
||||||
|
{batch.totalMaterialsDeleted}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div className="text-gray-500 text-xs">失败</div>
|
||||||
|
<div className="font-medium text-gray-900">
|
||||||
|
{batch.totalMaterialsFailed > 0 ? (
|
||||||
|
<span className="text-red-600">{batch.totalMaterialsFailed}</span>
|
||||||
|
) : (
|
||||||
|
'0'
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{batch.isDryRun && (
|
||||||
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-700">
|
||||||
|
<FlaskConical size={12} />
|
||||||
|
试运行
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{batch.totalAttempts > 1 && (
|
||||||
|
<span className="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-700">
|
||||||
|
{batch.totalAttempts}次尝试
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isAdmin && (
|
||||||
|
<button
|
||||||
|
className="p-2 hover:bg-red-50 text-gray-400 hover:text-red-600 rounded transition-colors disabled:opacity-50"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
void handleDelete()
|
||||||
|
}}
|
||||||
|
disabled={isDeleting}
|
||||||
|
title="删除批次"
|
||||||
|
>
|
||||||
|
<Trash2 size={16} className={isDeleting ? 'animate-pulse' : ''} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Batch details */}
|
||||||
|
{isExpanded && (
|
||||||
|
<div className="border-t border-gray-200 bg-white">
|
||||||
|
{/* Execution records */}
|
||||||
|
{executions.length > 0 && (
|
||||||
|
<div className="px-4 py-3 bg-gray-50/50 border-b border-gray-100">
|
||||||
|
<div className="text-xs font-medium text-gray-500 mb-2">执行记录</div>
|
||||||
|
{executions.length > 1 && (
|
||||||
|
<div className="flex gap-1 mb-2">
|
||||||
|
{executions.map((exec) => (
|
||||||
|
<button
|
||||||
|
key={exec.attemptNumber}
|
||||||
|
className={`px-3 py-1 rounded text-xs font-medium transition-colors ${
|
||||||
|
currentAttempt === exec.attemptNumber
|
||||||
|
? 'bg-blue-600 text-white'
|
||||||
|
: 'bg-gray-200 text-gray-700 hover:bg-gray-300'
|
||||||
|
}`}
|
||||||
|
onClick={() => setCurrentAttempt(exec.attemptNumber)}
|
||||||
|
>
|
||||||
|
第{exec.attemptNumber}次
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex flex-wrap gap-4 text-xs text-gray-600">
|
||||||
|
{executions
|
||||||
|
.filter(
|
||||||
|
(e) =>
|
||||||
|
currentAttempt === undefined ||
|
||||||
|
e.attemptNumber === currentAttempt
|
||||||
|
)
|
||||||
|
.map((exec) => (
|
||||||
|
<React.Fragment key={exec.attemptNumber}>
|
||||||
|
<span>
|
||||||
|
耗时:{formatDuration(exec.operationTime, exec.endTime)}
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
订单:{exec.ordersProcessed}/{exec.totalOrders}
|
||||||
|
</span>
|
||||||
|
<span>删除:{exec.totalMaterialsDeleted}</span>
|
||||||
|
{exec.totalMaterialsFailed > 0 && (
|
||||||
|
<span className="text-red-600">
|
||||||
|
失败:{exec.totalMaterialsFailed}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{exec.totalUncertainDeletions > 0 && (
|
||||||
|
<span className="text-amber-600">
|
||||||
|
不确定:{exec.totalUncertainDeletions}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{exec.errorMessage && (
|
||||||
|
<span className="text-red-600" title={exec.errorMessage}>
|
||||||
|
错误:{exec.errorMessage.substring(0, 80)}
|
||||||
|
{exec.errorMessage.length > 80 ? '...' : ''}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{exec.appVersion && (
|
||||||
|
<span className="text-gray-400">v{exec.appVersion}</span>
|
||||||
|
)}
|
||||||
|
</React.Fragment>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Order table */}
|
||||||
|
{filteredOrders.length > 0 ? (
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600 w-8" />
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
总排号
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
订单号
|
||||||
|
<button
|
||||||
|
className="p-1 hover:bg-gray-200 rounded transition-colors"
|
||||||
|
onClick={() => handleCopyColumn('orderNumber')}
|
||||||
|
title="复制所有订单号"
|
||||||
|
>
|
||||||
|
<Copy
|
||||||
|
size={14}
|
||||||
|
className="text-gray-500 hover:text-gray-700"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
状态
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
重试
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
已删除
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
已跳过
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
失败
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
不确定
|
||||||
|
</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||||
|
错误信息
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-gray-100">
|
||||||
|
{filteredOrders.map((order) => {
|
||||||
|
const orderKey = `${currentAttempt ?? order.attemptNumber}:${order.orderNumber}`
|
||||||
|
const isOrderExpanded = expandedOrders.has(orderKey)
|
||||||
|
const materials = orderMaterials.get(orderKey) || []
|
||||||
|
const isLoadingMaterials = loadingMaterials.has(orderKey)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<React.Fragment key={orderKey}>
|
||||||
|
<tr
|
||||||
|
className="hover:bg-gray-50 cursor-pointer"
|
||||||
|
onClick={() =>
|
||||||
|
toggleOrderExpansion(
|
||||||
|
currentAttempt ?? order.attemptNumber,
|
||||||
|
order.orderNumber
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
{isOrderExpanded ? (
|
||||||
|
<ChevronDown size={14} className="text-gray-400" />
|
||||||
|
) : (
|
||||||
|
<ChevronRight size={14} className="text-gray-400" />
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-gray-900 font-mono text-xs">
|
||||||
|
{order.productionId || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-gray-900 font-mono text-xs">
|
||||||
|
{order.status === 'not_found' ? '-' : order.orderNumber}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
<span
|
||||||
|
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium ${
|
||||||
|
statusStyles[order.status] || statusStyles.pending
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{statusIcons[order.status]}
|
||||||
|
{statusLabels[order.status] || order.status}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
{order.retryCount > 0 ? (
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-700">
|
||||||
|
<RefreshCw size={10} />
|
||||||
|
重试{order.retryCount}次
|
||||||
|
</span>
|
||||||
|
{order.retrySuccess && (
|
||||||
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-700">
|
||||||
|
<CheckCircle size={10} />
|
||||||
|
成功
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{!order.retrySuccess && (
|
||||||
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-700">
|
||||||
|
<XCircle size={10} />
|
||||||
|
失败
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<span className="text-gray-400 text-xs">-</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-green-600">
|
||||||
|
{order.materialsDeleted}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-gray-500">
|
||||||
|
{order.materialsSkipped}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-red-600">
|
||||||
|
{order.materialsFailed || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-amber-600">
|
||||||
|
{order.uncertainDeletions || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-red-600 text-xs max-w-xs truncate">
|
||||||
|
{order.errorMessage || '-'}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
{/* Material details */}
|
||||||
|
{isOrderExpanded && (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={10} className="bg-gray-50/50 px-8 py-3">
|
||||||
|
{isLoadingMaterials ? (
|
||||||
|
<div className="text-xs text-gray-500">
|
||||||
|
加载物料详情...
|
||||||
|
</div>
|
||||||
|
) : materials.length > 0 ? (
|
||||||
|
<table className="w-full text-xs">
|
||||||
|
<thead>
|
||||||
|
<tr className="text-gray-500">
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
物料编码
|
||||||
|
</th>
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
物料名称
|
||||||
|
</th>
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
行号
|
||||||
|
</th>
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
结果
|
||||||
|
</th>
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
原因
|
||||||
|
</th>
|
||||||
|
<th className="px-3 py-1.5 text-left font-medium">
|
||||||
|
尝试次数
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-gray-100">
|
||||||
|
{materials.map((mat, idx) => (
|
||||||
|
<tr key={idx} className="hover:bg-gray-50">
|
||||||
|
<td className="px-3 py-1.5 font-mono text-gray-700">
|
||||||
|
{mat.materialCode}
|
||||||
|
</td>
|
||||||
|
<td className="px-3 py-1.5 text-gray-700">
|
||||||
|
{mat.materialName}
|
||||||
|
</td>
|
||||||
|
<td className="px-3 py-1.5 text-gray-600">
|
||||||
|
{mat.rowNumber}
|
||||||
|
</td>
|
||||||
|
<td className="px-3 py-1.5">
|
||||||
|
<span
|
||||||
|
className={`px-1.5 py-0.5 rounded text-xs font-medium ${
|
||||||
|
mat.result === 'deleted'
|
||||||
|
? 'bg-green-100 text-green-700'
|
||||||
|
: mat.result === 'skipped'
|
||||||
|
? 'bg-gray-100 text-gray-700'
|
||||||
|
: mat.result === 'failed'
|
||||||
|
? 'bg-red-100 text-red-700'
|
||||||
|
: mat.result === 'uncertain'
|
||||||
|
? 'bg-amber-100 text-amber-700'
|
||||||
|
: 'bg-gray-100 text-gray-700'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{mat.result === 'deleted'
|
||||||
|
? '已删除'
|
||||||
|
: mat.result === 'skipped'
|
||||||
|
? '已跳过'
|
||||||
|
: mat.result === 'failed'
|
||||||
|
? '失败'
|
||||||
|
: mat.result === 'uncertain'
|
||||||
|
? '不确定'
|
||||||
|
: mat.result}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td className="px-3 py-1.5 text-gray-600 max-w-xs truncate">
|
||||||
|
{mat.reason || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-3 py-1.5 text-gray-600">
|
||||||
|
{mat.attemptCount > 1 ? (
|
||||||
|
<span className="text-amber-600">
|
||||||
|
{mat.attemptCount}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
'1'
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
) : (
|
||||||
|
<div className="text-xs text-gray-500">
|
||||||
|
暂无物料详情
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
)}
|
||||||
|
</React.Fragment>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="px-4 py-6 text-center text-sm text-gray-500">
|
||||||
|
暂无订单记录
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
BatchItem.displayName = 'BatchItem'
|
||||||
|
|
||||||
|
// ====== Main Modal Component ======
|
||||||
|
export const CleanerOperationHistoryModal: React.FC<CleanerOperationHistoryModalProps> = ({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
user
|
||||||
|
}) => {
|
||||||
|
const [batches, setBatches] = useState<CleanerHistoryBatchStats[]>([])
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
const [allUsers, setAllUsers] = useState<string[]>([])
|
||||||
|
const [selectedUsers, setSelectedUsers] = useState<string[]>([])
|
||||||
|
const logger = useLogger('CleanerOperationHistory')
|
||||||
|
|
||||||
|
const isAdmin = user?.userType === 'Admin'
|
||||||
|
|
||||||
|
const fetchBatches = useCallback(async () => {
|
||||||
|
setLoading(true)
|
||||||
|
setError(null)
|
||||||
|
try {
|
||||||
|
const options =
|
||||||
|
isAdmin && selectedUsers.length > 0
|
||||||
|
? { limit: 100, usernames: selectedUsers }
|
||||||
|
: { limit: 100 }
|
||||||
|
|
||||||
|
const result = await window.electron.cleaner.getHistoryBatches(options)
|
||||||
|
if (result.success && result.data) {
|
||||||
|
setBatches(result.data)
|
||||||
|
} else {
|
||||||
|
setError(result.error || '获取历史记录失败')
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : '获取历史记录失败')
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}, [isAdmin, selectedUsers])
|
||||||
|
|
||||||
|
const fetchAllUsers = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const result = await window.electron.auth.getAllUsers()
|
||||||
|
if (result.success && result.data) {
|
||||||
|
const usernames = result.data.map((u: UserInfo) => u.username)
|
||||||
|
setAllUsers(usernames)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Failed to fetch users list', {
|
||||||
|
error: err instanceof Error ? err.message : String(err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [logger])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) {
|
||||||
|
void fetchBatches()
|
||||||
|
if (isAdmin) {
|
||||||
|
void fetchAllUsers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [isOpen, fetchBatches, fetchAllUsers, isAdmin])
|
||||||
|
|
||||||
|
const handleDeleteBatch = useCallback((batchId: string) => {
|
||||||
|
setBatches((prev) => prev.filter((b) => b.batchId !== batchId))
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const toggleUserFilter = (username: string) => {
|
||||||
|
setSelectedUsers((prev) =>
|
||||||
|
prev.includes(username) ? prev.filter((u) => u !== username) : [...prev, username]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const clearUserFilters = () => {
|
||||||
|
setSelectedUsers([])
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isOpen) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal isOpen={isOpen} onClose={onClose} title="清理操作历史" size="3xl" className="!max-w-[68rem]">
|
||||||
|
<div className="flex flex-col h-[70vh]">
|
||||||
|
{/* Toolbar */}
|
||||||
|
<div className="flex items-start justify-between mb-4 pb-4 border-b border-gray-200">
|
||||||
|
<div className="flex-1">
|
||||||
|
{isAdmin && allUsers.length > 0 && (
|
||||||
|
<div className="mb-3">
|
||||||
|
<div className="text-xs text-gray-500 mb-2">筛选用户:</div>
|
||||||
|
<div className="flex flex-wrap gap-2">
|
||||||
|
{allUsers.map((username) => {
|
||||||
|
const isSelected = selectedUsers.includes(username)
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={username}
|
||||||
|
onClick={() => toggleUserFilter(username)}
|
||||||
|
className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-sm font-medium transition-all ${
|
||||||
|
isSelected
|
||||||
|
? 'bg-blue-600 text-white shadow-sm'
|
||||||
|
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{username}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
{selectedUsers.length > 0 && (
|
||||||
|
<button
|
||||||
|
onClick={clearUserFilters}
|
||||||
|
className="inline-flex items-center gap-1 px-3 py-1.5 rounded-full text-sm font-medium bg-red-50 text-red-600 hover:bg-red-100 transition-all"
|
||||||
|
>
|
||||||
|
清空筛选
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<span className="text-sm text-gray-600">
|
||||||
|
{isAdmin ? (
|
||||||
|
<span className="text-amber-600 font-medium">
|
||||||
|
管理员模式:
|
||||||
|
{selectedUsers.length > 0
|
||||||
|
? `已选择 ${selectedUsers.length} 个用户`
|
||||||
|
: '显示所有用户记录'}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span>仅显示您的操作记录</span>
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
{batches.length > 0 && (
|
||||||
|
<span className="text-sm text-gray-500">共 {batches.length} 条批次</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
className="p-2 hover:bg-gray-100 rounded-lg transition-colors disabled:opacity-50 flex-shrink-0"
|
||||||
|
onClick={() => void fetchBatches()}
|
||||||
|
disabled={loading}
|
||||||
|
title="刷新"
|
||||||
|
>
|
||||||
|
<RefreshCw size={18} className={loading ? 'animate-spin' : ''} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Error message */}
|
||||||
|
{error && (
|
||||||
|
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Batch list */}
|
||||||
|
<div className="flex-1 overflow-y-auto">
|
||||||
|
{loading && batches.length === 0 ? (
|
||||||
|
<div className="flex items-center justify-center h-32 text-gray-500">加载中...</div>
|
||||||
|
) : batches.length === 0 ? (
|
||||||
|
<div className="flex items-center justify-center h-32 text-gray-500">暂无操作记录</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex flex-col gap-3">
|
||||||
|
{batches.map((batch) => (
|
||||||
|
<BatchItem
|
||||||
|
key={batch.batchId}
|
||||||
|
batch={batch}
|
||||||
|
isAdmin={isAdmin}
|
||||||
|
onDelete={handleDeleteBatch}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Footer */}
|
||||||
|
{/* Footer */}
|
||||||
|
<div className="pt-4 border-t border-gray-200 flex justify-end">
|
||||||
|
<button
|
||||||
|
className="px-6 py-2 rounded-md bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium transition-colors"
|
||||||
|
onClick={onClose}
|
||||||
|
>
|
||||||
|
关闭
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CleanerOperationHistoryModal
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { CheckCircle, XCircle, SkipForward, Package, Loader2 } from 'lucide-react'
|
import { CheckCircle, XCircle, SkipForward, Package, Loader2, AlertTriangle } from 'lucide-react'
|
||||||
import { Modal } from './ui/Modal'
|
import { Modal } from './ui/Modal'
|
||||||
import type { CleanerProgress } from '../hooks/cleaner/types'
|
import type { CleanerProgress } from '../hooks/cleaner/types'
|
||||||
|
|
||||||
@@ -27,6 +27,9 @@ interface ExecutionReportDialogProps {
|
|||||||
// Retry-related props
|
// Retry-related props
|
||||||
retriedOrders?: number
|
retriedOrders?: number
|
||||||
successfulRetries?: number
|
successfulRetries?: number
|
||||||
|
// Deletion verification props
|
||||||
|
materialsFailed?: number
|
||||||
|
uncertainDeletions?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ExecutionReportDialog: React.FC<ExecutionReportDialogProps> = ({
|
export const ExecutionReportDialog: React.FC<ExecutionReportDialogProps> = ({
|
||||||
@@ -42,12 +45,15 @@ export const ExecutionReportDialog: React.FC<ExecutionReportDialogProps> = ({
|
|||||||
startTime = null,
|
startTime = null,
|
||||||
triggerRef,
|
triggerRef,
|
||||||
retriedOrders = 0,
|
retriedOrders = 0,
|
||||||
successfulRetries = 0
|
successfulRetries = 0,
|
||||||
|
materialsFailed = 0,
|
||||||
|
uncertainDeletions = 0
|
||||||
}) => {
|
}) => {
|
||||||
const [now, setNow] = React.useState(() => Date.now())
|
const [now, setNow] = React.useState(() => Date.now())
|
||||||
|
|
||||||
const hasErrors = errors.length > 0
|
const hasErrors = errors.length > 0
|
||||||
const hasRetries = retriedOrders > 0
|
const hasRetries = retriedOrders > 0
|
||||||
|
const hasFailedMaterials = materialsFailed > 0 || uncertainDeletions > 0
|
||||||
const showProgress = isExecuting && progress
|
const showProgress = isExecuting && progress
|
||||||
const isProgressing = !!showProgress
|
const isProgressing = !!showProgress
|
||||||
|
|
||||||
@@ -327,6 +333,36 @@ export const ExecutionReportDialog: React.FC<ExecutionReportDialogProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{hasFailedMaterials && (
|
||||||
|
<>
|
||||||
|
{materialsFailed > 0 && (
|
||||||
|
<div className="bg-gray-50 rounded-lg p-3 flex items-center gap-3 border border-gray-200">
|
||||||
|
<div className="w-9 h-9 rounded-lg bg-red-50 flex items-center justify-center flex-shrink-0">
|
||||||
|
<XCircle size={20} className="text-red-600" />
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="text-xs text-gray-600">删除失败</div>
|
||||||
|
<div className="text-xl font-semibold text-red-600">{materialsFailed}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{uncertainDeletions > 0 && (
|
||||||
|
<div className="bg-gray-50 rounded-lg p-3 flex items-center gap-3 border border-gray-200">
|
||||||
|
<div className="w-9 h-9 rounded-lg bg-yellow-50 flex items-center justify-center flex-shrink-0">
|
||||||
|
<AlertTriangle size={20} className="text-yellow-600" />
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="text-xs text-gray-600">不确定删除</div>
|
||||||
|
<div className="text-xl font-semibold text-yellow-600">
|
||||||
|
{uncertainDeletions}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{elapsedTime && (
|
{elapsedTime && (
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import {
|
|||||||
Eye,
|
Eye,
|
||||||
EyeOff,
|
EyeOff,
|
||||||
FileSpreadsheet,
|
FileSpreadsheet,
|
||||||
FileText,
|
|
||||||
HardDrive,
|
HardDrive,
|
||||||
|
History,
|
||||||
Search,
|
Search,
|
||||||
Settings2,
|
Settings2,
|
||||||
Square
|
Square
|
||||||
@@ -26,7 +26,7 @@ interface CleanerToolbarProps {
|
|||||||
handleValidation: () => Promise<void>
|
handleValidation: () => Promise<void>
|
||||||
handleConfirmDeletion: () => Promise<void>
|
handleConfirmDeletion: () => Promise<void>
|
||||||
handleExportResults: () => Promise<void>
|
handleExportResults: () => Promise<void>
|
||||||
setIsReportViewerOpen: (open: boolean) => void
|
setShowHistoryModal: (open: boolean) => void
|
||||||
typeManagementButtonRef: React.RefObject<HTMLButtonElement | null>
|
typeManagementButtonRef: React.RefObject<HTMLButtonElement | null>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ export function CleanerToolbar({
|
|||||||
handleValidation,
|
handleValidation,
|
||||||
handleConfirmDeletion,
|
handleConfirmDeletion,
|
||||||
handleExportResults,
|
handleExportResults,
|
||||||
setIsReportViewerOpen,
|
setShowHistoryModal,
|
||||||
typeManagementButtonRef
|
typeManagementButtonRef
|
||||||
}: CleanerToolbarProps): React.JSX.Element {
|
}: CleanerToolbarProps): React.JSX.Element {
|
||||||
return (
|
return (
|
||||||
@@ -123,6 +123,12 @@ export function CleanerToolbar({
|
|||||||
>
|
>
|
||||||
<Settings2 size={14} /> 类型管理
|
<Settings2 size={14} /> 类型管理
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setShowHistoryModal(true)}
|
||||||
|
className="text-xs bg-violet-50 border border-violet-200 text-violet-700 px-3 py-1.5 rounded shadow-sm hover:bg-violet-100 flex items-center gap-1.5 font-medium transition-colors"
|
||||||
|
>
|
||||||
|
<History size={14} /> 操作历史
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => void handleExportResults()}
|
onClick={() => void handleExportResults()}
|
||||||
disabled={isExporting || filteredResults.length === 0}
|
disabled={isExporting || filteredResults.length === 0}
|
||||||
@@ -130,12 +136,6 @@ export function CleanerToolbar({
|
|||||||
>
|
>
|
||||||
<FileSpreadsheet size={14} /> {isExporting ? '导出中...' : '导出结果'}
|
<FileSpreadsheet size={14} /> {isExporting ? '导出中...' : '导出结果'}
|
||||||
</button>
|
</button>
|
||||||
<button
|
|
||||||
onClick={() => setIsReportViewerOpen(true)}
|
|
||||||
className="text-xs bg-emerald-50 border border-emerald-200 text-emerald-700 px-3 py-1.5 rounded shadow-sm hover:bg-emerald-100 flex items-center gap-1.5 font-medium transition-colors"
|
|
||||||
>
|
|
||||||
<FileText size={14} /> 查看报告
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ interface ModalProps {
|
|||||||
title?: string
|
title?: string
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
size?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl'
|
size?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl'
|
||||||
|
/** Additional CSS classes for the modal panel */
|
||||||
|
className?: string
|
||||||
showCloseButton?: boolean
|
showCloseButton?: boolean
|
||||||
/** Ref to the element that triggered opening the modal (for focus restoration) */
|
/** Ref to the element that triggered opening the modal (for focus restoration) */
|
||||||
triggerRef?: React.RefObject<HTMLElement | null>
|
triggerRef?: React.RefObject<HTMLElement | null>
|
||||||
@@ -47,6 +49,7 @@ export function Modal({
|
|||||||
title,
|
title,
|
||||||
children,
|
children,
|
||||||
size = 'md',
|
size = 'md',
|
||||||
|
className,
|
||||||
showCloseButton = true,
|
showCloseButton = true,
|
||||||
triggerRef,
|
triggerRef,
|
||||||
titleId,
|
titleId,
|
||||||
@@ -98,7 +101,7 @@ export function Modal({
|
|||||||
<div className="flex min-h-full items-center justify-center p-4">
|
<div className="flex min-h-full items-center justify-center p-4">
|
||||||
<div
|
<div
|
||||||
ref={dialogRef}
|
ref={dialogRef}
|
||||||
className={`relative w-full ${sizeStyles[size]} bg-white rounded-lg shadow-xl transform transition-all`}
|
className={`relative w-full ${sizeStyles[size]} bg-white rounded-lg shadow-xl transform transition-all ${className || ''}`}
|
||||||
onClick={(e) => e.stopPropagation()}
|
onClick={(e) => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ interface CleanerRunPayload {
|
|||||||
errors: string[]
|
errors: string[]
|
||||||
retriedOrders: number
|
retriedOrders: number
|
||||||
successfulRetries: number
|
successfulRetries: number
|
||||||
|
materialsFailed?: number
|
||||||
|
uncertainDeletions?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initializeCleanerPage(): Promise<CleanerInitializationResult> {
|
export async function initializeCleanerPage(): Promise<CleanerInitializationResult> {
|
||||||
@@ -118,8 +120,11 @@ export async function runCleanerExecution(params: {
|
|||||||
headless: boolean
|
headless: boolean
|
||||||
queryBatchSize: number
|
queryBatchSize: number
|
||||||
processConcurrency: number
|
processConcurrency: number
|
||||||
|
selectedManagers: string[]
|
||||||
}): Promise<CleanerReportData> {
|
}): Promise<CleanerReportData> {
|
||||||
const cleanerDataResult = await window.electron.validation.getCleanerData()
|
const cleanerDataResult = await window.electron.validation.getCleanerData({
|
||||||
|
selectedManagers: params.selectedManagers
|
||||||
|
})
|
||||||
const cleanerData = cleanerDataResult.success
|
const cleanerData = cleanerDataResult.success
|
||||||
? (cleanerDataResult.data as CleanerDataPayload | null)
|
? (cleanerDataResult.data as CleanerDataPayload | null)
|
||||||
: null
|
: null
|
||||||
@@ -158,7 +163,9 @@ export async function runCleanerExecution(params: {
|
|||||||
materialsSkipped: cleanerRunData.materialsSkipped,
|
materialsSkipped: cleanerRunData.materialsSkipped,
|
||||||
errors: cleanerRunData.errors,
|
errors: cleanerRunData.errors,
|
||||||
retriedOrders: cleanerRunData.retriedOrders,
|
retriedOrders: cleanerRunData.retriedOrders,
|
||||||
successfulRetries: cleanerRunData.successfulRetries
|
successfulRetries: cleanerRunData.successfulRetries,
|
||||||
|
materialsFailed: cleanerRunData.materialsFailed ?? 0,
|
||||||
|
uncertainDeletions: cleanerRunData.uncertainDeletions ?? 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export interface CleanerProgress {
|
|||||||
currentMaterialIndex: number
|
currentMaterialIndex: number
|
||||||
totalMaterialsInOrder: number
|
totalMaterialsInOrder: number
|
||||||
currentOrderNumber?: string
|
currentOrderNumber?: string
|
||||||
phase: 'login' | 'processing' | 'complete'
|
phase: 'login' | 'processing' | 'complete' | 'retry'
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CleanerReportData {
|
export interface CleanerReportData {
|
||||||
@@ -45,6 +45,9 @@ export interface CleanerReportData {
|
|||||||
errors: string[]
|
errors: string[]
|
||||||
retriedOrders?: number
|
retriedOrders?: number
|
||||||
successfulRetries?: number
|
successfulRetries?: number
|
||||||
|
materialsFailed?: number
|
||||||
|
uncertainDeletions?: number
|
||||||
|
crashed?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CleanerInitializationResult {
|
export interface CleanerInitializationResult {
|
||||||
@@ -58,3 +61,71 @@ export interface CleanerConfigResult {
|
|||||||
queryBatchSize: number
|
queryBatchSize: number
|
||||||
processConcurrency: number
|
processConcurrency: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleaner operation history types (mirrors preload/index.d.ts)
|
||||||
|
|
||||||
|
export interface CleanerHistoryBatchStats {
|
||||||
|
batchId: string
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string
|
||||||
|
status: string
|
||||||
|
totalAttempts: number
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
successCount: number
|
||||||
|
failedCount: number
|
||||||
|
isDryRun: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryExecutionRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
userId: number
|
||||||
|
username: string
|
||||||
|
operationTime: string
|
||||||
|
endTime: string | null
|
||||||
|
status: string
|
||||||
|
isDryRun: boolean
|
||||||
|
totalOrders: number
|
||||||
|
ordersProcessed: number
|
||||||
|
totalMaterialsDeleted: number
|
||||||
|
totalMaterialsSkipped: number
|
||||||
|
totalMaterialsFailed: number
|
||||||
|
totalUncertainDeletions: number
|
||||||
|
errorMessage: string | null
|
||||||
|
appVersion: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryOrderRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
productionId: string | null
|
||||||
|
status: string
|
||||||
|
materialsDeleted: number
|
||||||
|
materialsSkipped: number
|
||||||
|
materialsFailed: number
|
||||||
|
uncertainDeletions: number
|
||||||
|
retryCount: number
|
||||||
|
retrySuccess: boolean
|
||||||
|
errorMessage: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanerHistoryMaterialRecord {
|
||||||
|
id?: number
|
||||||
|
batchId: string
|
||||||
|
attemptNumber: number
|
||||||
|
orderNumber: string
|
||||||
|
materialCode: string
|
||||||
|
materialName: string
|
||||||
|
rowNumber: number
|
||||||
|
result: string
|
||||||
|
reason: string | null
|
||||||
|
attemptCount: number
|
||||||
|
finalErrorCategory: string | null
|
||||||
|
}
|
||||||
|
|||||||
@@ -376,7 +376,8 @@ export function useCleaner() {
|
|||||||
dryRun,
|
dryRun,
|
||||||
headless,
|
headless,
|
||||||
queryBatchSize,
|
queryBatchSize,
|
||||||
processConcurrency
|
processConcurrency,
|
||||||
|
selectedManagers: Array.from(selectedManagers)
|
||||||
})
|
})
|
||||||
setReportData(result)
|
setReportData(result)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ const MaterialTypeManagementDialog = React.lazy(
|
|||||||
() => import('../components/MaterialTypeManagementDialog')
|
() => import('../components/MaterialTypeManagementDialog')
|
||||||
)
|
)
|
||||||
const ExecutionReportDialog = React.lazy(() => import('../components/ExecutionReportDialog'))
|
const ExecutionReportDialog = React.lazy(() => import('../components/ExecutionReportDialog'))
|
||||||
const ReportViewerDialog = React.lazy(() => import('../components/ReportViewerDialog'))
|
const CleanerOperationHistoryModal = React.lazy(
|
||||||
const ReportAnalysisDialog = React.lazy(() => import('../components/ReportAnalysisDialog'))
|
() => import('../components/CleanerOperationHistoryModal')
|
||||||
|
)
|
||||||
|
|
||||||
const CleanerPage: React.FC = () => {
|
const CleanerPage: React.FC = () => {
|
||||||
const typeManagementButtonRef = React.useRef<HTMLButtonElement>(null)
|
const typeManagementButtonRef = React.useRef<HTMLButtonElement>(null)
|
||||||
@@ -66,8 +67,7 @@ const CleanerPage: React.FC = () => {
|
|||||||
confirmDialog
|
confirmDialog
|
||||||
} = useCleaner()
|
} = useCleaner()
|
||||||
|
|
||||||
const [isReportViewerOpen, setIsReportViewerOpen] = React.useState(false)
|
const [showHistoryModal, setShowHistoryModal] = React.useState(false)
|
||||||
const [isReportAnalysisOpen, setIsReportAnalysisOpen] = React.useState(false)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full flex flex-col xl:flex-row gap-6 animate-in fade-in slide-in-from-bottom-4 duration-500">
|
<div className="h-full flex flex-col xl:flex-row gap-6 animate-in fade-in slide-in-from-bottom-4 duration-500">
|
||||||
@@ -96,7 +96,7 @@ const CleanerPage: React.FC = () => {
|
|||||||
handleValidation={handleValidation}
|
handleValidation={handleValidation}
|
||||||
handleConfirmDeletion={handleConfirmDeletion}
|
handleConfirmDeletion={handleConfirmDeletion}
|
||||||
handleExportResults={handleExportResults}
|
handleExportResults={handleExportResults}
|
||||||
setIsReportViewerOpen={setIsReportViewerOpen}
|
setShowHistoryModal={setShowHistoryModal}
|
||||||
typeManagementButtonRef={typeManagementButtonRef}
|
typeManagementButtonRef={typeManagementButtonRef}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -162,24 +162,20 @@ const CleanerPage: React.FC = () => {
|
|||||||
triggerRef={executeButtonRef}
|
triggerRef={executeButtonRef}
|
||||||
retriedOrders={reportData?.retriedOrders}
|
retriedOrders={reportData?.retriedOrders}
|
||||||
successfulRetries={reportData?.successfulRetries}
|
successfulRetries={reportData?.successfulRetries}
|
||||||
|
materialsFailed={reportData?.materialsFailed}
|
||||||
|
uncertainDeletions={reportData?.uncertainDeletions}
|
||||||
/>
|
/>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
|
|
||||||
<Suspense fallback={null}>
|
<Suspense fallback={null}>
|
||||||
<ReportViewerDialog
|
<CleanerOperationHistoryModal
|
||||||
isOpen={isReportViewerOpen}
|
isOpen={showHistoryModal}
|
||||||
onClose={() => setIsReportViewerOpen(false)}
|
onClose={() => setShowHistoryModal(false)}
|
||||||
isAdmin={isAdmin}
|
user={
|
||||||
currentUsername={currentUsername}
|
currentUsername
|
||||||
onOpenAnalysis={() => setIsReportAnalysisOpen(true)}
|
? { username: currentUsername, userType: isAdmin ? 'Admin' : 'User' }
|
||||||
/>
|
: null
|
||||||
</Suspense>
|
}
|
||||||
|
|
||||||
<Suspense fallback={null}>
|
|
||||||
<ReportAnalysisDialog
|
|
||||||
isOpen={isReportAnalysisOpen}
|
|
||||||
onClose={() => setIsReportAnalysisOpen(false)}
|
|
||||||
isAdmin={isAdmin}
|
|
||||||
/>
|
/>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ export const IPC_CHANNELS = {
|
|||||||
CLEANER_EXPORT_RESULTS: 'cleaner:exportResults',
|
CLEANER_EXPORT_RESULTS: 'cleaner:exportResults',
|
||||||
CLEANER_PROGRESS: 'cleaner:progress',
|
CLEANER_PROGRESS: 'cleaner:progress',
|
||||||
|
|
||||||
|
// Cleaner operation history
|
||||||
|
CLEANER_HISTORY_GET_BATCHES: 'cleanerHistory:getBatches',
|
||||||
|
CLEANER_HISTORY_GET_BATCH_DETAILS: 'cleanerHistory:getBatchDetails',
|
||||||
|
CLEANER_HISTORY_GET_MATERIAL_DETAILS: 'cleanerHistory:getMaterialDetails',
|
||||||
|
CLEANER_HISTORY_DELETE_BATCH: 'cleanerHistory:deleteBatch',
|
||||||
|
|
||||||
// Database service - MySQL
|
// Database service - MySQL
|
||||||
DATABASE_MYSQL_CONNECT: 'database:mysql:connect',
|
DATABASE_MYSQL_CONNECT: 'database:mysql:connect',
|
||||||
DATABASE_MYSQL_DISCONNECT: 'database:mysql:disconnect',
|
DATABASE_MYSQL_DISCONNECT: 'database:mysql:disconnect',
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
|
|
||||||
> erpauto@1.8.0 test:run
|
|
||||||
> vitest run cleaner
|
|
||||||
|
|
||||||
|
|
||||||
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90mD:/FileLib/Projects/CodeMigration/ERPAuto[39m
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/unit/cleaner-handler.test.ts
|
|
||||||
[22m[39mTest suite starting...
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/unit/cleaner-helpers.test.ts
|
|
||||||
[22m[39mTest suite starting...
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/unit/cleaner-helpers.test.ts
|
|
||||||
[22m[39mTest suite completed.
|
|
||||||
|
|
||||||
[32m鉁?[39m tests/unit/cleaner-helpers.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 6[2mms[22m[39m
|
|
||||||
[90mstdout[2m | tests/unit/cleaner-handler.test.ts
|
|
||||||
[22m[39mTest suite completed.
|
|
||||||
|
|
||||||
[32m鉁?[39m tests/unit/cleaner-handler.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 120[2mms[22m[39m
|
|
||||||
[90mstdout[2m | tests/unit/cleaner.test.ts
|
|
||||||
[22m[39mTest suite starting...
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/unit/cleaner.test.ts
|
|
||||||
[22m[39mTest suite completed.
|
|
||||||
|
|
||||||
[32m鉁?[39m tests/unit/cleaner.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 64[2mms[22m[39m
|
|
||||||
[90mstdout[2m | tests/integration/cleaner.test.ts
|
|
||||||
[22m[39mTest suite starting...
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Dry-run mode > should initialize with dry-run mode
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Dry-run mode > should track materials to delete without actually deleting (dry-run)
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should process single order and return details
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should handle order with "瀹℃壒閫氳繃" status
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should handle multiple orders with progress callback
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Error handling > should continue processing after order error
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Navigation > should navigate to discrete production order maintenance page
|
|
||||||
Skipping test: ERP credentials not configured
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/integration/cleaner.test.ts
|
|
||||||
[22m[39mTest suite completed.
|
|
||||||
|
|
||||||
[32m鉁?[39m tests/integration/cleaner.test.ts [2m([22m[2m7 tests[22m[2m)[22m[32m 10[2mms[22m[39m
|
|
||||||
[90mstdout[2m | tests/manual/cleaner-slow-motion.test.ts
|
|
||||||
[22m[39mTest suite starting...
|
|
||||||
|
|
||||||
stderr | tests/manual/cleaner-slow-motion.test.ts > Cleaner Slow Motion Test > should run cleaner in slow motion mode
|
|
||||||
Please set ERP_URL, ERP_USERNAME, ERP_PASSWORD in .env file
|
|
||||||
|
|
||||||
[90mstdout[2m | tests/manual/cleaner-slow-motion.test.ts
|
|
||||||
[22m[39mTest suite completed.
|
|
||||||
|
|
||||||
[32m鉁?[39m tests/manual/cleaner-slow-motion.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 5[2mms[22m[39m
|
|
||||||
|
|
||||||
[2m Test Files [22m [1m[32m5 passed[39m[22m[90m (5)[39m
|
|
||||||
[2m Tests [22m [1m[32m21 passed[39m[22m[90m (21)[39m
|
|
||||||
[2m Start at [22m 10:36:50
|
|
||||||
[2m Duration [22m 1.50s[2m (transform 729ms, setup 228ms, import 2.41s, tests 204ms, environment 1ms)[22m
|
|
||||||
|
|
||||||
@@ -85,8 +85,10 @@ test.describe('Authentication Flow', () => {
|
|||||||
const errorMessage = page.locator('.error, [role="alert"], .text-red')
|
const errorMessage = page.locator('.error, [role="alert"], .text-red')
|
||||||
const hasError = await errorMessage.count()
|
const hasError = await errorMessage.count()
|
||||||
|
|
||||||
// Either error shown or still on login page
|
// Verify login was rejected: either error shown or still on login page
|
||||||
expect(hasError >= 0).toBe(true)
|
const loginDialog = page.locator('[data-testid="login-dialog"]')
|
||||||
|
const isStillOnLoginPage = await loginDialog.isVisible().catch(() => false)
|
||||||
|
expect(hasError > 0 || isStillOnLoginPage).toBe(true)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,45 +5,39 @@
|
|||||||
* Run: npx playwright test tests/e2e/extractor-workflow.test.ts
|
* Run: npx playwright test tests/e2e/extractor-workflow.test.ts
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { _electron as electron } from '@playwright/test'
|
import { test, expect, type ElectronApplication, type Page } from '@playwright/test'
|
||||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
import { _electron as electron } from 'playwright'
|
||||||
import type { ElectronApplication, Page, BrowserWindow } from 'playwright'
|
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
|
|
||||||
describe('Extractor E2E Workflow', () => {
|
let electronApp: ElectronApplication
|
||||||
let electronApp: ElectronApplication
|
let page: Page
|
||||||
let window: BrowserWindow
|
|
||||||
let page: Page
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
// Build the app first (if not already built)
|
|
||||||
// npm run build
|
|
||||||
|
|
||||||
|
test.describe('Extractor E2E Workflow', () => {
|
||||||
|
test.beforeAll(async () => {
|
||||||
// Launch Electron app for testing
|
// Launch Electron app for testing
|
||||||
electronApp = await electron.launch({
|
electronApp = await electron.launch({
|
||||||
args: [join(process.cwd(), 'out/main/index.js')]
|
args: [join(process.cwd(), 'out/main/index.js')]
|
||||||
})
|
})
|
||||||
|
|
||||||
// Get the main window
|
// Get the main window
|
||||||
window = await electronApp.firstWindow()
|
|
||||||
page = await electronApp.firstWindow()
|
page = await electronApp.firstWindow()
|
||||||
|
|
||||||
// Wait for app to load
|
// Wait for app to load
|
||||||
await page.waitForLoadState('domcontentloaded')
|
await page.waitForLoadState('domcontentloaded')
|
||||||
}, 60000)
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
test.afterAll(async () => {
|
||||||
if (electronApp) {
|
if (electronApp) {
|
||||||
await electronApp.close()
|
await electronApp.close()
|
||||||
}
|
}
|
||||||
}, 60000)
|
})
|
||||||
|
|
||||||
it('should launch the application', async () => {
|
test('should launch the application', async () => {
|
||||||
const title = await page.title()
|
const title = await page.title()
|
||||||
expect(title).toBeDefined()
|
expect(title).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should navigate to Extractor page', async () => {
|
test('should navigate to Extractor page', async () => {
|
||||||
// Click on the "数据提取" link
|
// Click on the "数据提取" link
|
||||||
await page.click('a:has-text("数据提取")')
|
await page.click('a:has-text("数据提取")')
|
||||||
|
|
||||||
@@ -55,7 +49,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(pageTitle).toContain('ERP 数据提取')
|
expect(pageTitle).toContain('ERP 数据提取')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should display order number input', async () => {
|
test('should display order number input', async () => {
|
||||||
// Check if order number textarea is visible
|
// Check if order number textarea is visible
|
||||||
const textarea = page.locator('.order-textarea')
|
const textarea = page.locator('.order-textarea')
|
||||||
await expect(textarea).toBeVisible()
|
await expect(textarea).toBeVisible()
|
||||||
@@ -65,7 +59,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(placeholder).toContain('订单号')
|
expect(placeholder).toContain('订单号')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should update order count when typing', async () => {
|
test('should update order count when typing', async () => {
|
||||||
// Fill in order numbers
|
// Fill in order numbers
|
||||||
const textarea = page.locator('.order-textarea')
|
const textarea = page.locator('.order-textarea')
|
||||||
await textarea.fill('SC70202602120085\nSC70202602120120')
|
await textarea.fill('SC70202602120085\nSC70202602120120')
|
||||||
@@ -79,7 +73,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(countText).toContain('2')
|
expect(countText).toContain('2')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should show error when extracting without order numbers', async () => {
|
test('should show error when extracting without order numbers', async () => {
|
||||||
// Clear the textarea
|
// Clear the textarea
|
||||||
const textarea = page.locator('.order-textarea')
|
const textarea = page.locator('.order-textarea')
|
||||||
await textarea.fill('')
|
await textarea.fill('')
|
||||||
@@ -90,7 +84,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(isDisabled).toBe(true)
|
expect(isDisabled).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have batch size input', async () => {
|
test('should have batch size input', async () => {
|
||||||
const batchSizeInput = page.locator('input[type="number"]')
|
const batchSizeInput = page.locator('input[type="number"]')
|
||||||
await expect(batchSizeInput).toBeVisible()
|
await expect(batchSizeInput).toBeVisible()
|
||||||
|
|
||||||
@@ -98,7 +92,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(value).toBe('100')
|
expect(value).toBe('100')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have reset button', async () => {
|
test('should have reset button', async () => {
|
||||||
const resetButton = page.locator('.btn-secondary:has-text("重置")')
|
const resetButton = page.locator('.btn-secondary:has-text("重置")')
|
||||||
await expect(resetButton).toBeVisible()
|
await expect(resetButton).toBeVisible()
|
||||||
|
|
||||||
@@ -111,7 +105,7 @@ describe('Extractor E2E Workflow', () => {
|
|||||||
expect(value).toBe('')
|
expect(value).toBe('')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should navigate back to home', async () => {
|
test('should navigate back to home', async () => {
|
||||||
// Click back button
|
// Click back button
|
||||||
await page.click('.nav-btn:has-text("返回主页")')
|
await page.click('.nav-btn:has-text("返回主页")')
|
||||||
|
|
||||||
|
|||||||
16
tests/fixtures/factory.ts
vendored
16
tests/fixtures/factory.ts
vendored
@@ -73,10 +73,12 @@ export class UserFactory {
|
|||||||
/**
|
/**
|
||||||
* Generate unique user ID
|
* Generate unique user ID
|
||||||
*
|
*
|
||||||
* @returns Unique ID string in format USR-{timestamp}-{random}
|
* @returns Unique numeric ID
|
||||||
*/
|
*/
|
||||||
private static generateId(): string {
|
private static idCounter = 0
|
||||||
return `USR-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`
|
|
||||||
|
private static generateId(): number {
|
||||||
|
return ++UserFactory.idCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -417,11 +419,11 @@ export class AuditLogFactory {
|
|||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // Successful login audit
|
* // Successful login audit
|
||||||
* const entry = AuditLogFactory.createAuditLog('LOGIN', 'SUCCESS')
|
* const entry = AuditLogFactory.createAuditLog(AuditAction.LOGIN, AuditStatus.SUCCESS)
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // Failed extract audit
|
* // Failed extract audit
|
||||||
* const entry = AuditLogFactory.createAuditLog('EXTRACT', 'FAILURE', { resource: 'Order SC123' })
|
* const entry = AuditLogFactory.createAuditLog(AuditAction.EXTRACT, AuditStatus.FAILURE, { resource: 'Order SC123' })
|
||||||
*/
|
*/
|
||||||
static createAuditLog(
|
static createAuditLog(
|
||||||
action: AuditAction = AuditAction.LOGIN,
|
action: AuditAction = AuditAction.LOGIN,
|
||||||
@@ -429,13 +431,15 @@ export class AuditLogFactory {
|
|||||||
overrides?: Partial<AuditEntry>
|
overrides?: Partial<AuditEntry>
|
||||||
): AuditEntry {
|
): AuditEntry {
|
||||||
return {
|
return {
|
||||||
timestamp: new Date(),
|
timestamp: new Date().toISOString(),
|
||||||
action,
|
action,
|
||||||
userId: 'USR-001',
|
userId: 'USR-001',
|
||||||
username: 'test_user',
|
username: 'test_user',
|
||||||
computerName: 'TEST-PC',
|
computerName: 'TEST-PC',
|
||||||
appVersion: '1.0.0',
|
appVersion: '1.0.0',
|
||||||
|
resource: 'test-resource',
|
||||||
status,
|
status,
|
||||||
|
metadata: {},
|
||||||
...overrides
|
...overrides
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
tests/fixtures/other-factories.test.ts
vendored
2
tests/fixtures/other-factories.test.ts
vendored
@@ -91,7 +91,7 @@ describe('AuditLogFactory', () => {
|
|||||||
it('creates audit log with default values', () => {
|
it('creates audit log with default values', () => {
|
||||||
const entry = AuditLogFactory.createAuditLog()
|
const entry = AuditLogFactory.createAuditLog()
|
||||||
|
|
||||||
expect(entry.timestamp).toBeInstanceOf(Date)
|
expect(typeof entry.timestamp).toBe('string')
|
||||||
expect(entry.action).toBe(AuditAction.LOGIN)
|
expect(entry.action).toBe(AuditAction.LOGIN)
|
||||||
expect(entry.status).toBe(AuditStatus.SUCCESS)
|
expect(entry.status).toBe(AuditStatus.SUCCESS)
|
||||||
expect(entry.userId).toBe('USR-001')
|
expect(entry.userId).toBe('USR-001')
|
||||||
|
|||||||
4
tests/fixtures/types.ts
vendored
4
tests/fixtures/types.ts
vendored
@@ -88,8 +88,8 @@ export interface Order {
|
|||||||
* Simplified user data for creating test users.
|
* Simplified user data for creating test users.
|
||||||
*/
|
*/
|
||||||
export interface TestUser {
|
export interface TestUser {
|
||||||
/** User ID */
|
/** User ID (matches UserInfo.id: number) */
|
||||||
id: string
|
id: number
|
||||||
/** Username for login */
|
/** Username for login */
|
||||||
username: string
|
username: string
|
||||||
/** User type/role */
|
/** User type/role */
|
||||||
|
|||||||
11
tests/fixtures/user-factory.test.ts
vendored
11
tests/fixtures/user-factory.test.ts
vendored
@@ -2,17 +2,22 @@
|
|||||||
* UserFactory Unit Tests
|
* UserFactory Unit Tests
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect } from 'vitest'
|
import { describe, it, expect, beforeEach } from 'vitest'
|
||||||
import { UserFactory } from './factory'
|
import { UserFactory } from './factory'
|
||||||
|
|
||||||
describe('UserFactory', () => {
|
describe('UserFactory', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
// Reset ID counter to ensure test isolation
|
||||||
|
;(UserFactory as any).idCounter = 0
|
||||||
|
})
|
||||||
|
|
||||||
it('creates user with default role', () => {
|
it('creates user with default role', () => {
|
||||||
const user = UserFactory.createUser()
|
const user = UserFactory.createUser()
|
||||||
|
|
||||||
expect(user.username).toMatch(/^test_user_\d+$/)
|
expect(user.username).toMatch(/^test_user_\d+$/)
|
||||||
expect(user.userType).toBe('User')
|
expect(user.userType).toBe('User')
|
||||||
expect(user.permissions).toEqual(['read', 'write'])
|
expect(user.permissions).toEqual(['read', 'write'])
|
||||||
expect(user.id).toMatch(/^USR-\d+-[a-z0-9]+$/)
|
expect(user.id).toBeTypeOf('number')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('creates admin with correct permissions', () => {
|
it('creates admin with correct permissions', () => {
|
||||||
@@ -20,7 +25,7 @@ describe('UserFactory', () => {
|
|||||||
|
|
||||||
expect(admin.userType).toBe('Admin')
|
expect(admin.userType).toBe('Admin')
|
||||||
expect(admin.permissions).toEqual(['read', 'write', 'delete', 'admin'])
|
expect(admin.permissions).toEqual(['read', 'write', 'delete', 'admin'])
|
||||||
expect(admin.id).toMatch(/^USR-\d+-[a-z0-9]+$/)
|
expect(admin.id).toBeTypeOf('number')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('generates unique IDs', () => {
|
it('generates unique IDs', () => {
|
||||||
|
|||||||
@@ -21,227 +21,220 @@ describe('Cleaner Service (Integration)', () => {
|
|||||||
const hasCredentials = !!(config.url && config.username && config.password)
|
const hasCredentials = !!(config.url && config.username && config.password)
|
||||||
|
|
||||||
describe('Dry-run mode', () => {
|
describe('Dry-run mode', () => {
|
||||||
it('should initialize with dry-run mode', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should initialize with dry-run mode',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
const authService = new ErpAuthService(config)
|
||||||
}
|
await authService.login()
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
expect(cleaner.isDryRun()).toBe(true)
|
||||||
|
|
||||||
expect(cleaner.isDryRun()).toBe(true)
|
await authService.close()
|
||||||
|
},
|
||||||
|
30000
|
||||||
|
)
|
||||||
|
|
||||||
await authService.close()
|
it.skipIf(!hasCredentials)(
|
||||||
}, 30000)
|
'should track materials to delete without actually deleting (dry-run)',
|
||||||
|
async () => {
|
||||||
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
it('should track materials to delete without actually deleting (dry-run)', async () => {
|
// Read test data
|
||||||
if (!hasCredentials) {
|
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
const orderNumbers = orderContent
|
||||||
return
|
.split('\n')
|
||||||
}
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
.slice(0, 2) // Test first 2 orders
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const materialContent = await fs.readFile(materialCodeFile, 'utf-8')
|
||||||
await authService.login()
|
const materialCodes = materialContent
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
|
||||||
// Read test data
|
console.log(
|
||||||
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
`Testing dry-run with ${orderNumbers.length} orders and ${materialCodes.length} material codes`
|
||||||
const orderNumbers = orderContent
|
)
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
.slice(0, 2) // Test first 2 orders
|
|
||||||
|
|
||||||
const materialContent = await fs.readFile(materialCodeFile, 'utf-8')
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
const materialCodes = materialContent
|
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
|
|
||||||
console.log(
|
const result = await cleaner.clean({
|
||||||
`Testing dry-run with ${orderNumbers.length} orders and ${materialCodes.length} material codes`
|
orderNumbers,
|
||||||
)
|
materialCodes,
|
||||||
|
dryRun: true
|
||||||
|
})
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
// In dry-run mode, materialsDeleted should be tracked but not actually deleted
|
||||||
|
console.log(`Dry-run result:`, {
|
||||||
|
ordersProcessed: result.ordersProcessed,
|
||||||
|
materialsDeleted: result.materialsDeleted,
|
||||||
|
materialsSkipped: result.materialsSkipped,
|
||||||
|
errors: result.errors.length
|
||||||
|
})
|
||||||
|
|
||||||
const result = await cleaner.clean({
|
expect(result.ordersProcessed).toBeGreaterThan(0)
|
||||||
orderNumbers,
|
// In dry-run, no actual deletions should happen
|
||||||
materialCodes,
|
expect(result.errors).toHaveLength(0)
|
||||||
dryRun: true
|
|
||||||
})
|
|
||||||
|
|
||||||
// In dry-run mode, materialsDeleted should be tracked but not actually deleted
|
await authService.close()
|
||||||
console.log(`Dry-run result:`, {
|
},
|
||||||
ordersProcessed: result.ordersProcessed,
|
120000
|
||||||
materialsDeleted: result.materialsDeleted,
|
)
|
||||||
materialsSkipped: result.materialsSkipped,
|
|
||||||
errors: result.errors.length
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result.ordersProcessed).toBeGreaterThan(0)
|
|
||||||
// In dry-run, no actual deletions should happen
|
|
||||||
expect(result.errors).toHaveLength(0)
|
|
||||||
|
|
||||||
await authService.close()
|
|
||||||
}, 120000)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Order processing', () => {
|
describe('Order processing', () => {
|
||||||
it('should process single order and return details', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should process single order and return details',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
const authService = new ErpAuthService(config)
|
||||||
}
|
await authService.login()
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
await authService.login()
|
const orderNumbers = orderContent
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
.slice(0, 1) // Test single order
|
||||||
|
|
||||||
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
const orderNumbers = orderContent
|
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
.slice(0, 1) // Test single order
|
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
const result = await cleaner.clean({
|
||||||
|
orderNumbers,
|
||||||
|
materialCodes: [], // Empty list - nothing to delete
|
||||||
|
dryRun: true
|
||||||
|
})
|
||||||
|
|
||||||
const result = await cleaner.clean({
|
expect(result.ordersProcessed).toBe(1)
|
||||||
orderNumbers,
|
expect(result.details).toHaveLength(1)
|
||||||
materialCodes: [], // Empty list - nothing to delete
|
expect(result.details[0].orderNumber).toBe(orderNumbers[0])
|
||||||
dryRun: true
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result.ordersProcessed).toBe(1)
|
await authService.close()
|
||||||
expect(result.details).toHaveLength(1)
|
},
|
||||||
expect(result.details[0].orderNumber).toBe(orderNumbers[0])
|
60000
|
||||||
|
)
|
||||||
|
|
||||||
await authService.close()
|
it.skipIf(!hasCredentials)(
|
||||||
}, 60000)
|
'should handle order with "审批通过" status',
|
||||||
|
async () => {
|
||||||
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
it('should handle order with "审批通过" status', async () => {
|
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
if (!hasCredentials) {
|
const orderNumbers = orderContent
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
.split('\n')
|
||||||
return
|
.map((line) => line.trim())
|
||||||
}
|
.filter((line) => line.length > 0)
|
||||||
|
.slice(0, 1)
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
const result = await cleaner.clean({
|
||||||
const orderNumbers = orderContent
|
orderNumbers,
|
||||||
.split('\n')
|
materialCodes: [],
|
||||||
.map((line) => line.trim())
|
dryRun: true
|
||||||
.filter((line) => line.length > 0)
|
})
|
||||||
.slice(0, 1)
|
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
// Order details should include status information
|
||||||
|
const detail = result.details[0]
|
||||||
|
console.log(
|
||||||
|
`Order ${detail.orderNumber} - Materials deleted: ${detail.materialsDeleted}, Skipped: ${detail.materialsSkipped}`
|
||||||
|
)
|
||||||
|
|
||||||
const result = await cleaner.clean({
|
expect(detail).toBeDefined()
|
||||||
orderNumbers,
|
|
||||||
materialCodes: [],
|
|
||||||
dryRun: true
|
|
||||||
})
|
|
||||||
|
|
||||||
// Order details should include status information
|
await authService.close()
|
||||||
const detail = result.details[0]
|
},
|
||||||
console.log(
|
60000
|
||||||
`Order ${detail.orderNumber} - Materials deleted: ${detail.materialsDeleted}, Skipped: ${detail.materialsSkipped}`
|
)
|
||||||
)
|
|
||||||
|
|
||||||
expect(detail).toBeDefined()
|
it.skipIf(!hasCredentials)(
|
||||||
|
'should handle multiple orders with progress callback',
|
||||||
|
async () => {
|
||||||
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
await authService.close()
|
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
}, 60000)
|
const orderNumbers = orderContent
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
.slice(0, 3) // Test 3 orders
|
||||||
|
|
||||||
it('should handle multiple orders with progress callback', async () => {
|
const progressMessages: string[] = []
|
||||||
if (!hasCredentials) {
|
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const orderContent = await fs.readFile(productionIdFile, 'utf-8')
|
const result = await cleaner.clean({
|
||||||
const orderNumbers = orderContent
|
orderNumbers,
|
||||||
.split('\n')
|
materialCodes: [],
|
||||||
.map((line) => line.trim())
|
dryRun: true,
|
||||||
.filter((line) => line.length > 0)
|
onProgress: (message, progress) => {
|
||||||
.slice(0, 3) // Test 3 orders
|
progressMessages.push(`${progress?.toFixed(0)}%: ${message}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const progressMessages: string[] = []
|
expect(result.ordersProcessed).toBe(3)
|
||||||
|
expect(progressMessages.length).toBeGreaterThan(0)
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
console.log('Progress messages:', progressMessages.slice(0, 5))
|
||||||
|
|
||||||
const result = await cleaner.clean({
|
await authService.close()
|
||||||
orderNumbers,
|
},
|
||||||
materialCodes: [],
|
180000
|
||||||
dryRun: true,
|
)
|
||||||
onProgress: (message, progress) => {
|
|
||||||
progressMessages.push(`${progress?.toFixed(0)}%: ${message}`)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result.ordersProcessed).toBe(3)
|
|
||||||
expect(progressMessages.length).toBeGreaterThan(0)
|
|
||||||
|
|
||||||
console.log('Progress messages:', progressMessages.slice(0, 5))
|
|
||||||
|
|
||||||
await authService.close()
|
|
||||||
}, 180000)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Error handling', () => {
|
describe('Error handling', () => {
|
||||||
it('should continue processing after order error', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should continue processing after order error',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
const authService = new ErpAuthService(config)
|
||||||
}
|
await authService.login()
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const orderNumbers = ['INVALID_ORDER_12345', 'INVALID_ORDER_67890']
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const orderNumbers = ['INVALID_ORDER_12345', 'INVALID_ORDER_67890']
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
const result = await cleaner.clean({
|
||||||
|
orderNumbers,
|
||||||
|
materialCodes: [],
|
||||||
|
dryRun: true
|
||||||
|
})
|
||||||
|
|
||||||
const result = await cleaner.clean({
|
// Should still process (even if with errors)
|
||||||
orderNumbers,
|
expect(result.details.length).toBeGreaterThan(0)
|
||||||
materialCodes: [],
|
|
||||||
dryRun: true
|
|
||||||
})
|
|
||||||
|
|
||||||
// Should still process (even if with errors)
|
await authService.close()
|
||||||
expect(result.details.length).toBeGreaterThan(0)
|
},
|
||||||
|
120000
|
||||||
await authService.close()
|
)
|
||||||
}, 120000)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Navigation', () => {
|
describe('Navigation', () => {
|
||||||
it('should navigate to discrete production order maintenance page', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should navigate to discrete production order maintenance page',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
const authService = new ErpAuthService(config)
|
||||||
}
|
await authService.login()
|
||||||
|
|
||||||
const authService = new ErpAuthService(config)
|
const cleaner = new CleanerService(authService, { dryRun: true })
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const cleaner = new CleanerService(authService, { dryRun: true })
|
// This tests the internal navigation method
|
||||||
|
const session = authService.getSession()
|
||||||
|
const { popupPage, workFrame } = await cleaner.navigateToCleanerPage(session)
|
||||||
|
|
||||||
// This tests the internal navigation method
|
expect(popupPage).toBeDefined()
|
||||||
const session = authService.getSession()
|
expect(workFrame).toBeDefined()
|
||||||
const { popupPage, workFrame } = await cleaner.navigateToCleanerPage(session)
|
|
||||||
|
|
||||||
expect(popupPage).toBeDefined()
|
// Cleanup
|
||||||
expect(workFrame).toBeDefined()
|
await popupPage.close()
|
||||||
|
await authService.close()
|
||||||
// Cleanup
|
},
|
||||||
await popupPage.close()
|
60000
|
||||||
await authService.close()
|
)
|
||||||
}, 60000)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -15,39 +15,34 @@ describe('ERP Authentication Service (Integration)', () => {
|
|||||||
const hasCredentials = !!(config.url && config.username && config.password)
|
const hasCredentials = !!(config.url && config.username && config.password)
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
if (!hasCredentials) {
|
if (!hasCredentials) return
|
||||||
console.warn('Skipping ERP auth tests: credentials not configured')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
authService = new ErpAuthService(config)
|
authService = new ErpAuthService(config)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should login successfully', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should login successfully',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
const session = await authService.login()
|
||||||
}
|
|
||||||
|
|
||||||
const session = await authService.login()
|
expect(session).toBeDefined()
|
||||||
|
expect(session.browser).toBeDefined()
|
||||||
|
expect(session.context).toBeDefined()
|
||||||
|
expect(session.page).toBeDefined()
|
||||||
|
expect(session.isLoggedIn).toBe(true)
|
||||||
|
},
|
||||||
|
30000
|
||||||
|
)
|
||||||
|
|
||||||
expect(session).toBeDefined()
|
it.skipIf(!hasCredentials)(
|
||||||
expect(session.browser).toBeDefined()
|
'should navigate to main page after login',
|
||||||
expect(session.context).toBeDefined()
|
async () => {
|
||||||
expect(session.page).toBeDefined()
|
const session = await authService.login()
|
||||||
expect(session.isLoggedIn).toBe(true)
|
|
||||||
}, 30000)
|
|
||||||
|
|
||||||
it('should navigate to main page after login', async () => {
|
const url = session.page.url()
|
||||||
if (!hasCredentials) {
|
expect(url).toContain(config.url)
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
},
|
||||||
return
|
30000
|
||||||
}
|
)
|
||||||
|
|
||||||
const session = await authService.login()
|
|
||||||
|
|
||||||
const url = session.page.url()
|
|
||||||
expect(url).toContain(config.url)
|
|
||||||
}, 30000)
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (hasCredentials && authService) {
|
if (hasCredentials && authService) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
import { describe, it, expect } from 'vitest'
|
||||||
import { ExtractorService } from '../../src/main/services/erp/extractor'
|
import { ExtractorService } from '../../src/main/services/erp/extractor'
|
||||||
import { ErpAuthService } from '../../src/main/services/erp/erp-auth'
|
import { ErpAuthService } from '../../src/main/services/erp/erp-auth'
|
||||||
import type { ErpConfig } from '../../src/main/types/erp.types'
|
import type { ErpConfig } from '../../src/main/types/erp.types'
|
||||||
@@ -18,136 +18,129 @@ describe('Extractor Service (Integration)', () => {
|
|||||||
// Check if we have ERP credentials
|
// Check if we have ERP credentials
|
||||||
const hasCredentials = !!(config.url && config.username && config.password)
|
const hasCredentials = !!(config.url && config.username && config.password)
|
||||||
|
|
||||||
it('should extract data for single order number', async () => {
|
it.skipIf(!hasCredentials)(
|
||||||
if (!hasCredentials) {
|
'should extract data for single order number',
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
async () => {
|
||||||
return
|
// Create fresh auth service for this test
|
||||||
}
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
// Create fresh auth service for this test
|
const extractor = new ExtractorService(authService)
|
||||||
const authService = new ErpAuthService(config)
|
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const extractor = new ExtractorService(authService)
|
const result = await extractor.extract({
|
||||||
|
orderNumbers: [testOrderNumber]
|
||||||
|
})
|
||||||
|
|
||||||
const result = await extractor.extract({
|
expect(result.downloadedFiles).toHaveLength(1)
|
||||||
orderNumbers: [testOrderNumber]
|
expect(result.errors).toHaveLength(0)
|
||||||
})
|
|
||||||
|
|
||||||
expect(result.downloadedFiles).toHaveLength(1)
|
// Verify file exists
|
||||||
expect(result.errors).toHaveLength(0)
|
const filePath = result.downloadedFiles[0]
|
||||||
|
|
||||||
// Verify file exists
|
|
||||||
const filePath = result.downloadedFiles[0]
|
|
||||||
const stats = await fs.stat(filePath)
|
|
||||||
expect(stats.size).toBeGreaterThan(0)
|
|
||||||
|
|
||||||
// Clean up
|
|
||||||
await authService.close()
|
|
||||||
}, 60000)
|
|
||||||
|
|
||||||
it('should extract data for multiple order numbers', async () => {
|
|
||||||
if (!hasCredentials) {
|
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create fresh auth service for this test
|
|
||||||
const authService = new ErpAuthService(config)
|
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const extractor = new ExtractorService(authService)
|
|
||||||
|
|
||||||
// Read order numbers from productionID.txt file
|
|
||||||
const fs = await import('fs/promises')
|
|
||||||
const path = await import('path')
|
|
||||||
// productionID.txt is at: D:\FileLib\Projects\CodeMigration\references\demo\productionID.txt
|
|
||||||
// test runs at: D:\FileLib\Projects\CodeMigration\ERPAuto
|
|
||||||
const productionIdFile = path.join(process.cwd(), '../references/demo/productionID.txt')
|
|
||||||
const content = await fs.readFile(productionIdFile, 'utf-8')
|
|
||||||
const orderNumbers = content
|
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
.slice(0, 5) // Test first 5 orders
|
|
||||||
|
|
||||||
console.log(`Testing with ${orderNumbers.length} order numbers:`, orderNumbers)
|
|
||||||
|
|
||||||
const result = await extractor.extract({
|
|
||||||
orderNumbers,
|
|
||||||
batchSize: 100 // Process all in one batch
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log(`Downloaded ${result.downloadedFiles.length} files`)
|
|
||||||
if (result.errors.length > 0) {
|
|
||||||
console.log('Errors:', result.errors)
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(result.downloadedFiles.length).toBeGreaterThanOrEqual(1)
|
|
||||||
|
|
||||||
// Clean up
|
|
||||||
await authService.close()
|
|
||||||
}, 120000) // Increase timeout to 2 minutes
|
|
||||||
|
|
||||||
it('should extract data for 300 orders with batch size 70', async () => {
|
|
||||||
if (!hasCredentials) {
|
|
||||||
console.warn('Skipping test: ERP credentials not configured')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create fresh auth service for this test
|
|
||||||
const authService = new ErpAuthService(config)
|
|
||||||
await authService.login()
|
|
||||||
|
|
||||||
const extractor = new ExtractorService(authService)
|
|
||||||
|
|
||||||
// Read all order numbers from productionID.txt file
|
|
||||||
const fs = await import('fs/promises')
|
|
||||||
const path = await import('path')
|
|
||||||
const productionIdFile = path.join(process.cwd(), '../references/demo/productionID.txt')
|
|
||||||
const content = await fs.readFile(productionIdFile, 'utf-8')
|
|
||||||
const orderNumbers = content
|
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
|
|
||||||
console.log(`Testing with ${orderNumbers.length} order numbers`)
|
|
||||||
console.log(`Batch size: 70, Expected batches: ${Math.ceil(orderNumbers.length / 70)}`)
|
|
||||||
|
|
||||||
const startTime = Date.now()
|
|
||||||
|
|
||||||
const result = await extractor.extract({
|
|
||||||
orderNumbers,
|
|
||||||
batchSize: 70 // Process 70 orders per batch
|
|
||||||
})
|
|
||||||
|
|
||||||
const endTime = Date.now()
|
|
||||||
const duration = ((endTime - startTime) / 1000).toFixed(2)
|
|
||||||
|
|
||||||
console.log(`\n=== Extraction Summary ===`)
|
|
||||||
console.log(`Total orders: ${orderNumbers.length}`)
|
|
||||||
console.log(`Batch size: 70`)
|
|
||||||
console.log(`Expected batches: ${Math.ceil(orderNumbers.length / 70)}`)
|
|
||||||
console.log(`Downloaded files: ${result.downloadedFiles.length}`)
|
|
||||||
console.log(`Total duration: ${duration}s`)
|
|
||||||
console.log(`Average time per batch: ${(duration / result.downloadedFiles.length).toFixed(2)}s`)
|
|
||||||
|
|
||||||
if (result.errors.length > 0) {
|
|
||||||
console.log(`\nErrors encountered: ${result.errors.length}`)
|
|
||||||
result.errors.forEach((err, idx) => console.log(` ${idx + 1}. ${err}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify results
|
|
||||||
expect(result.downloadedFiles.length).toBeGreaterThanOrEqual(1)
|
|
||||||
|
|
||||||
// Verify each downloaded file exists and has content
|
|
||||||
for (const filePath of result.downloadedFiles) {
|
|
||||||
const stats = await fs.stat(filePath)
|
const stats = await fs.stat(filePath)
|
||||||
console.log(` - ${path.basename(filePath)}: ${(stats.size / 1024).toFixed(2)} KB`)
|
|
||||||
expect(stats.size).toBeGreaterThan(0)
|
expect(stats.size).toBeGreaterThan(0)
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
await authService.close()
|
await authService.close()
|
||||||
}, 600000) // 10 minutes timeout for large batch test
|
},
|
||||||
|
60000
|
||||||
|
)
|
||||||
|
|
||||||
|
it.skipIf(!hasCredentials)(
|
||||||
|
'should extract data for multiple order numbers',
|
||||||
|
async () => {
|
||||||
|
// Create fresh auth service for this test
|
||||||
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
|
const extractor = new ExtractorService(authService)
|
||||||
|
|
||||||
|
// Read order numbers from productionID.txt file
|
||||||
|
const productionIdFile = path.join(process.cwd(), '../references/demo/productionID.txt')
|
||||||
|
const content = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
|
const orderNumbers = content
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
.slice(0, 5) // Test first 5 orders
|
||||||
|
|
||||||
|
console.log(`Testing with ${orderNumbers.length} order numbers:`, orderNumbers)
|
||||||
|
|
||||||
|
const result = await extractor.extract({
|
||||||
|
orderNumbers,
|
||||||
|
batchSize: 100 // Process all in one batch
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(`Downloaded ${result.downloadedFiles.length} files`)
|
||||||
|
if (result.errors.length > 0) {
|
||||||
|
console.log('Errors:', result.errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(result.downloadedFiles.length).toBeGreaterThanOrEqual(1)
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
await authService.close()
|
||||||
|
},
|
||||||
|
120000
|
||||||
|
) // Increase timeout to 2 minutes
|
||||||
|
|
||||||
|
it.skipIf(!hasCredentials)(
|
||||||
|
'should extract data for 300 orders with batch size 70',
|
||||||
|
async () => {
|
||||||
|
// Create fresh auth service for this test
|
||||||
|
const authService = new ErpAuthService(config)
|
||||||
|
await authService.login()
|
||||||
|
|
||||||
|
const extractor = new ExtractorService(authService)
|
||||||
|
|
||||||
|
// Read all order numbers from productionID.txt file
|
||||||
|
const productionIdFile = path.join(process.cwd(), '../references/demo/productionID.txt')
|
||||||
|
const content = await fs.readFile(productionIdFile, 'utf-8')
|
||||||
|
const orderNumbers = content
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
|
||||||
|
console.log(`Testing with ${orderNumbers.length} order numbers`)
|
||||||
|
console.log(`Batch size: 70, Expected batches: ${Math.ceil(orderNumbers.length / 70)}`)
|
||||||
|
|
||||||
|
const startTime = Date.now()
|
||||||
|
|
||||||
|
const result = await extractor.extract({
|
||||||
|
orderNumbers,
|
||||||
|
batchSize: 70 // Process 70 orders per batch
|
||||||
|
})
|
||||||
|
|
||||||
|
const endTime = Date.now()
|
||||||
|
const duration = ((endTime - startTime) / 1000).toFixed(2)
|
||||||
|
|
||||||
|
console.log(`\n=== Extraction Summary ===`)
|
||||||
|
console.log(`Total orders: ${orderNumbers.length}`)
|
||||||
|
console.log(`Batch size: 70`)
|
||||||
|
console.log(`Expected batches: ${Math.ceil(orderNumbers.length / 70)}`)
|
||||||
|
console.log(`Downloaded files: ${result.downloadedFiles.length}`)
|
||||||
|
console.log(`Total duration: ${duration}s`)
|
||||||
|
console.log(
|
||||||
|
`Average time per batch: ${(duration / result.downloadedFiles.length).toFixed(2)}s`
|
||||||
|
)
|
||||||
|
|
||||||
|
if (result.errors.length > 0) {
|
||||||
|
console.log(`\nErrors encountered: ${result.errors.length}`)
|
||||||
|
result.errors.forEach((err, idx) => console.log(` ${idx + 1}. ${err}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify results
|
||||||
|
expect(result.downloadedFiles.length).toBeGreaterThanOrEqual(1)
|
||||||
|
|
||||||
|
// Verify each downloaded file exists and has content
|
||||||
|
for (const filePath of result.downloadedFiles) {
|
||||||
|
const stats = await fs.stat(filePath)
|
||||||
|
console.log(` - ${path.basename(filePath)}: ${(stats.size / 1024).toFixed(2)} KB`)
|
||||||
|
expect(stats.size).toBeGreaterThan(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
await authService.close()
|
||||||
|
},
|
||||||
|
600000
|
||||||
|
) // 10 minutes timeout for large batch test
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,17 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest'
|
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest'
|
||||||
import {
|
|
||||||
trackDuration,
|
|
||||||
PerformanceTracker,
|
|
||||||
createPerformanceTracker,
|
|
||||||
DEFAULT_SLOW_THRESHOLD_MS,
|
|
||||||
type TrackDurationOptions
|
|
||||||
} from '../../src/main/services/logger/performance-monitor'
|
|
||||||
import logger from '../../src/main/services/logger/index'
|
|
||||||
|
|
||||||
// Mock the logger to avoid noisy output during tests
|
// Mock the logger to avoid noisy output during tests
|
||||||
vi.mock('../../src/main/services/logger/index', () => ({
|
vi.mock('../../src/main/services/logger', () => ({
|
||||||
default: {
|
default: {
|
||||||
debug: vi.fn(),
|
debug: vi.fn(),
|
||||||
info: vi.fn(),
|
info: vi.fn(),
|
||||||
@@ -26,7 +18,23 @@ vi.mock('../../src/main/services/logger/index', () => ({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
describe('Performance Monitor', () => {
|
describe('Performance Monitor', () => {
|
||||||
beforeEach(() => {
|
let trackDuration: typeof import('../../src/main/services/logger/performance-monitor').trackDuration
|
||||||
|
let PerformanceTracker: typeof import('../../src/main/services/logger/performance-monitor').PerformanceTracker
|
||||||
|
let createPerformanceTracker: typeof import('../../src/main/services/logger/performance-monitor').createPerformanceTracker
|
||||||
|
let DEFAULT_SLOW_THRESHOLD_MS: typeof import('../../src/main/services/logger/performance-monitor').DEFAULT_SLOW_THRESHOLD_MS
|
||||||
|
let logger: typeof import('../../src/main/services/logger').default
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.resetModules()
|
||||||
|
const perfMod = await import('../../src/main/services/logger/performance-monitor')
|
||||||
|
const loggerMod = await import('../../src/main/services/logger/index')
|
||||||
|
|
||||||
|
trackDuration = perfMod.trackDuration
|
||||||
|
PerformanceTracker = perfMod.PerformanceTracker
|
||||||
|
createPerformanceTracker = perfMod.createPerformanceTracker
|
||||||
|
DEFAULT_SLOW_THRESHOLD_MS = perfMod.DEFAULT_SLOW_THRESHOLD_MS
|
||||||
|
logger = loggerMod.default
|
||||||
|
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -230,7 +238,7 @@ describe('Performance Monitor', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should log summary with aggregated metrics', () => {
|
it('should log summary with aggregated metrics', async () => {
|
||||||
const tracker = new PerformanceTracker('TestService', 1000)
|
const tracker = new PerformanceTracker('TestService', 1000)
|
||||||
|
|
||||||
tracker.recordDuration(100)
|
tracker.recordDuration(100)
|
||||||
@@ -248,7 +256,7 @@ describe('Performance Monitor', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should include slow percentage in summary', () => {
|
it('should include slow percentage in summary', async () => {
|
||||||
const tracker = new PerformanceTracker('TestService', 50)
|
const tracker = new PerformanceTracker('TestService', 50)
|
||||||
|
|
||||||
tracker.recordDuration(30) // Normal
|
tracker.recordDuration(30) // Normal
|
||||||
@@ -261,7 +269,7 @@ describe('Performance Monitor', () => {
|
|||||||
expect(summaryCall.slowPercentage).toContain('%')
|
expect(summaryCall.slowPercentage).toContain('%')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should reset metrics when reset() is called', () => {
|
it('should reset metrics when reset() is called', async () => {
|
||||||
const tracker = new PerformanceTracker('TestService', 1000)
|
const tracker = new PerformanceTracker('TestService', 1000)
|
||||||
|
|
||||||
tracker.recordDuration(100)
|
tracker.recordDuration(100)
|
||||||
@@ -275,7 +283,7 @@ describe('Performance Monitor', () => {
|
|||||||
expect(metrics.slowOperationCount).toBe(0)
|
expect(metrics.slowOperationCount).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return zero metrics when no operations tracked', () => {
|
it('should return zero metrics when no operations tracked', async () => {
|
||||||
const tracker = new PerformanceTracker('EmptyService')
|
const tracker = new PerformanceTracker('EmptyService')
|
||||||
|
|
||||||
const metrics = tracker.getMetrics()
|
const metrics = tracker.getMetrics()
|
||||||
@@ -288,7 +296,7 @@ describe('Performance Monitor', () => {
|
|||||||
expect(metrics.slowOperationCount).toBe(0)
|
expect(metrics.slowOperationCount).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should use custom logger if provided', () => {
|
it('should use custom logger if provided', async () => {
|
||||||
const customLogger = {
|
const customLogger = {
|
||||||
debug: vi.fn(),
|
debug: vi.fn(),
|
||||||
info: vi.fn(),
|
info: vi.fn(),
|
||||||
@@ -307,7 +315,7 @@ describe('Performance Monitor', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('createPerformanceTracker', () => {
|
describe('createPerformanceTracker', () => {
|
||||||
it('should create a tracker with default threshold', () => {
|
it('should create a tracker with default threshold', async () => {
|
||||||
const tracker = createPerformanceTracker('MyService')
|
const tracker = createPerformanceTracker('MyService')
|
||||||
|
|
||||||
expect(tracker).toBeInstanceOf(PerformanceTracker)
|
expect(tracker).toBeInstanceOf(PerformanceTracker)
|
||||||
@@ -315,7 +323,7 @@ describe('Performance Monitor', () => {
|
|||||||
expect(metrics.count).toBe(0)
|
expect(metrics.count).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create a tracker with custom threshold', () => {
|
it('should create a tracker with custom threshold', async () => {
|
||||||
const tracker = createPerformanceTracker('FastService', 100)
|
const tracker = createPerformanceTracker('FastService', 100)
|
||||||
|
|
||||||
tracker.recordDuration(150)
|
tracker.recordDuration(150)
|
||||||
|
|||||||
@@ -429,7 +429,7 @@ export function createMockExcelJS() {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function createMockAxios() {
|
export function createMockAxios() {
|
||||||
const mockInstance = {
|
return {
|
||||||
get: vi.fn().mockResolvedValue({ data: {} }),
|
get: vi.fn().mockResolvedValue({ data: {} }),
|
||||||
post: vi.fn().mockResolvedValue({ data: {} }),
|
post: vi.fn().mockResolvedValue({ data: {} }),
|
||||||
put: vi.fn().mockResolvedValue({ data: {} }),
|
put: vi.fn().mockResolvedValue({ data: {} }),
|
||||||
@@ -437,8 +437,6 @@ export function createMockAxios() {
|
|||||||
patch: vi.fn().mockResolvedValue({ data: {} }),
|
patch: vi.fn().mockResolvedValue({ data: {} }),
|
||||||
request: vi.fn().mockResolvedValue({ data: {} })
|
request: vi.fn().mockResolvedValue({ data: {} })
|
||||||
}
|
}
|
||||||
mockInstance.get.mockResolvedValue({ data: {} })
|
|
||||||
return mockInstance as any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -652,7 +650,7 @@ export function createMockElectron(
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function createMockQueryBuilder(options?: {
|
export function createMockQueryBuilder(options?: {
|
||||||
result?: any[]
|
result?: Record<string, unknown>[]
|
||||||
}): import('./types').MockQueryBuilder {
|
}): import('./types').MockQueryBuilder {
|
||||||
const mockResult = options?.result ?? []
|
const mockResult = options?.result ?? []
|
||||||
return {
|
return {
|
||||||
@@ -685,14 +683,14 @@ export function createMockQueryBuilder(options?: {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function createMockRepository(options?: {
|
export function createMockRepository(options?: {
|
||||||
findResult?: any[]
|
findResult?: Record<string, unknown>[]
|
||||||
}): import('./types').MockRepository {
|
}): import('./types').MockRepository {
|
||||||
const mockFindResult = options?.findResult ?? []
|
const mockFindResult = options?.findResult ?? []
|
||||||
return {
|
return {
|
||||||
find: vi.fn().mockResolvedValue(mockFindResult),
|
find: vi.fn().mockResolvedValue(mockFindResult),
|
||||||
findOne: vi.fn().mockResolvedValue(mockFindResult[0] ?? null),
|
findOne: vi.fn().mockResolvedValue(mockFindResult[0] ?? null),
|
||||||
create: vi.fn((plainObject?: any) => plainObject ?? {}),
|
create: vi.fn((plainObject?: Record<string, unknown>) => plainObject ?? {}),
|
||||||
save: vi.fn().mockImplementation((entity) => Promise.resolve(entity)),
|
save: vi.fn().mockImplementation((entity: Record<string, unknown>) => Promise.resolve(entity)),
|
||||||
delete: vi.fn().mockResolvedValue({ affected: 1 }),
|
delete: vi.fn().mockResolvedValue({ affected: 1 }),
|
||||||
count: vi.fn().mockResolvedValue(mockFindResult.length),
|
count: vi.fn().mockResolvedValue(mockFindResult.length),
|
||||||
createQueryBuilder: vi
|
createQueryBuilder: vi
|
||||||
@@ -727,8 +725,13 @@ export function createMockDataSource(
|
|||||||
destroy: vi.fn().mockResolvedValue(undefined),
|
destroy: vi.fn().mockResolvedValue(undefined),
|
||||||
isInitialized,
|
isInitialized,
|
||||||
getRepository: vi.fn().mockReturnValue(mockRepo),
|
getRepository: vi.fn().mockReturnValue(mockRepo),
|
||||||
create: vi.fn().mockImplementation((entityClass: any, plainObject?: any) => plainObject ?? {}),
|
create: vi
|
||||||
save: vi.fn().mockImplementation((entity) => Promise.resolve(entity)),
|
.fn()
|
||||||
|
.mockImplementation(
|
||||||
|
(_entityClass: unknown, plainObject?: Record<string, unknown>) =>
|
||||||
|
plainObject ?? ({} as Record<string, unknown>)
|
||||||
|
),
|
||||||
|
save: vi.fn().mockImplementation((entity: Record<string, unknown>) => Promise.resolve(entity)),
|
||||||
createQueryBuilder: vi
|
createQueryBuilder: vi
|
||||||
.fn()
|
.fn()
|
||||||
.mockImplementation(() => createMockQueryBuilder({ result: queryResult })),
|
.mockImplementation(() => createMockQueryBuilder({ result: queryResult })),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||||
const { _electron: electron } = require('playwright')
|
const { _electron: electron } = require('playwright')
|
||||||
|
|
||||||
;(async () => {
|
;(async () => {
|
||||||
|
|||||||
@@ -1,246 +1,136 @@
|
|||||||
/**
|
/**
|
||||||
* Audit Logger Unit Tests - Real File Write Integration Tests
|
* Audit Logger Unit Tests
|
||||||
*
|
*
|
||||||
* Tests audit logger with real file writes to isolated test directory
|
* Tests audit logger behavior: verifies JSONL entry content,
|
||||||
* Verifies JSONL format, entry structure, and cleanup behavior
|
* status handling, metadata processing, and special characters.
|
||||||
|
* Uses spy on the module's audit logger instance instead of mocking winston,
|
||||||
|
* to avoid cross-contamination with logger.test.ts under isolate:false.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||||
import fs from 'fs'
|
import { AuditAction, AuditStatus } from '../../src/main/types/audit.types'
|
||||||
import path from 'path'
|
|
||||||
import { app } from 'electron'
|
|
||||||
|
|
||||||
// Isolated test log directory
|
describe('Audit Logger', () => {
|
||||||
const TEST_LOG_DIR = path.join(process.cwd(), 'test-logs')
|
let auditLoggerModule: typeof import('../../src/main/services/logger/audit-logger')
|
||||||
|
let infoSpy: ReturnType<typeof vi.fn>
|
||||||
/**
|
|
||||||
* Create a test audit entry with all required fields
|
|
||||||
*/
|
|
||||||
function createTestEntry(overrides?: Partial<Record<string, unknown>>): Record<string, unknown> {
|
|
||||||
return {
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
action: 'LOGIN',
|
|
||||||
userId: 'test-user-123',
|
|
||||||
username: 'test.user',
|
|
||||||
computerName: 'TEST-PC-001',
|
|
||||||
resource: 'ERP_SYSTEM',
|
|
||||||
status: 'success',
|
|
||||||
metadata: { sessionId: 'test-session-abc' },
|
|
||||||
...overrides
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Audit Logger - Real File Integration', () => {
|
|
||||||
// Track original files in test directory
|
|
||||||
const originalFiles = new Set<string>()
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
// Create test log directory
|
|
||||||
if (!fs.existsSync(TEST_LOG_DIR)) {
|
|
||||||
fs.mkdirSync(TEST_LOG_DIR, { recursive: true })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Track existing files for cleanup
|
|
||||||
const files = fs.readdirSync(TEST_LOG_DIR)
|
|
||||||
files.forEach((f) => originalFiles.add(f))
|
|
||||||
|
|
||||||
// Clear mocks
|
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
auditLoggerModule = await import('../../src/main/services/logger/audit-logger')
|
||||||
|
|
||||||
|
// Spy on the audit logger's info method
|
||||||
|
const auditLogger = auditLoggerModule.default
|
||||||
|
infoSpy = vi.fn()
|
||||||
|
auditLogger.info = infoSpy
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(() => {
|
||||||
// Cleanup: Remove all files created during test
|
vi.restoreAllMocks()
|
||||||
if (fs.existsSync(TEST_LOG_DIR)) {
|
|
||||||
const files = fs.readdirSync(TEST_LOG_DIR)
|
|
||||||
files.forEach((file) => {
|
|
||||||
if (!originalFiles.has(file)) {
|
|
||||||
const filePath = path.join(TEST_LOG_DIR, file)
|
|
||||||
try {
|
|
||||||
fs.unlinkSync(filePath)
|
|
||||||
} catch {
|
|
||||||
// Ignore cleanup errors
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Try to remove empty directory
|
|
||||||
try {
|
|
||||||
fs.rmdirSync(TEST_LOG_DIR)
|
|
||||||
} catch {
|
|
||||||
// Directory may not be empty, that's ok
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vi.resetModules()
|
vi.resetModules()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export logAudit function', async () => {
|
it('should produce a valid JSONL entry with all required fields', async () => {
|
||||||
const { logAudit } = await import('../../src/main/services/logger/audit-logger')
|
const { logAudit, applyAuditConfig } = auditLoggerModule
|
||||||
expect(logAudit).toBeDefined()
|
|
||||||
expect(typeof logAudit).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should export closeAuditLogger function', async () => {
|
applyAuditConfig(30)
|
||||||
const { closeAuditLogger } = await import('../../src/main/services/logger/audit-logger')
|
logAudit(AuditAction.LOGIN, 'user-001', {
|
||||||
expect(closeAuditLogger).toBeDefined()
|
username: 'alice',
|
||||||
expect(typeof closeAuditLogger).toBe('function')
|
computerName: 'PC-001',
|
||||||
})
|
resource: 'ERP_SYSTEM',
|
||||||
|
status: AuditStatus.SUCCESS,
|
||||||
it('should log audit entry with all required fields', async () => {
|
metadata: { sessionId: 'abc' }
|
||||||
const { logAudit, closeAuditLogger } =
|
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
const entry = createTestEntry()
|
|
||||||
|
|
||||||
logAudit(entry.action as string, entry.userId as string, {
|
|
||||||
username: entry.username as string,
|
|
||||||
computerName: entry.computerName as string,
|
|
||||||
resource: entry.resource as string,
|
|
||||||
status: entry.status as 'success' | 'failure' | 'partial',
|
|
||||||
metadata: entry.metadata as Record<string, unknown>
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Close logger to flush writes
|
expect(infoSpy).toHaveBeenCalledTimes(1)
|
||||||
closeAuditLogger()
|
const entry = JSON.parse(infoSpy.mock.calls[0][0])
|
||||||
|
|
||||||
// Find the audit log file (should be today's file)
|
|
||||||
const today = new Date().toISOString().split('T')[0]
|
|
||||||
const auditFile = path.join(TEST_LOG_DIR, `audit-${today}.jsonl`)
|
|
||||||
|
|
||||||
// Check if file exists (it may be in a different location due to electron mock)
|
|
||||||
// The actual file location depends on how electron's app.getPath('logs') is mocked
|
|
||||||
expect(entry.action).toBe('LOGIN')
|
expect(entry.action).toBe('LOGIN')
|
||||||
expect(entry.userId).toBe('test-user-123')
|
expect(entry.userId).toBe('user-001')
|
||||||
expect(entry.username).toBe('test.user')
|
expect(entry.username).toBe('alice')
|
||||||
expect(entry.computerName).toBe('TEST-PC-001')
|
expect(entry.computerName).toBe('PC-001')
|
||||||
|
expect(entry.appVersion).toBe('1.9.0-test')
|
||||||
expect(entry.resource).toBe('ERP_SYSTEM')
|
expect(entry.resource).toBe('ERP_SYSTEM')
|
||||||
expect(entry.status).toBe('success')
|
expect(entry.status).toBe('success')
|
||||||
|
expect(entry.metadata).toEqual({ sessionId: 'abc' })
|
||||||
|
// Timestamp should be a valid ISO 8601 string
|
||||||
|
expect(new Date(entry.timestamp).toISOString()).toBe(entry.timestamp)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle all status values (success, failure, partial)', async () => {
|
it('should accept all status values: success, failure, partial', async () => {
|
||||||
const { logAudit, closeAuditLogger } =
|
const { logAudit, applyAuditConfig } = auditLoggerModule
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
// Test success status
|
applyAuditConfig(30)
|
||||||
logAudit('EXTRACT', 'user1', {
|
|
||||||
|
logAudit(AuditAction.EXTRACT, 'user1', {
|
||||||
username: 'extractor',
|
username: 'extractor',
|
||||||
computerName: 'PC-001',
|
computerName: 'PC-001',
|
||||||
resource: 'materials',
|
resource: 'materials',
|
||||||
status: 'success'
|
status: AuditStatus.SUCCESS
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test failure status
|
logAudit(AuditAction.CLEAN, 'user2', {
|
||||||
logAudit('DELETE', 'user2', {
|
|
||||||
username: 'cleaner',
|
username: 'cleaner',
|
||||||
computerName: 'PC-002',
|
computerName: 'PC-002',
|
||||||
resource: 'temp_files',
|
resource: 'temp_files',
|
||||||
status: 'failure',
|
status: AuditStatus.FAILURE,
|
||||||
metadata: { error: 'Permission denied' }
|
metadata: { error: 'Permission denied' }
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test partial status
|
logAudit(AuditAction.APP_UPDATE, 'user3', {
|
||||||
logAudit('UPDATE', 'user3', {
|
|
||||||
username: 'updater',
|
username: 'updater',
|
||||||
computerName: 'PC-003',
|
computerName: 'PC-003',
|
||||||
resource: 'config',
|
resource: 'config',
|
||||||
status: 'partial',
|
status: AuditStatus.PARTIAL,
|
||||||
metadata: { updated: 5, failed: 2 }
|
metadata: { updated: 5, failed: 2 }
|
||||||
})
|
})
|
||||||
|
|
||||||
closeAuditLogger()
|
expect(infoSpy).toHaveBeenCalledTimes(3)
|
||||||
|
const entries = infoSpy.mock.calls.map((call: any[]) => JSON.parse(call[0]))
|
||||||
// Verify all entries were processed
|
expect(entries[0].status).toBe('success')
|
||||||
expect(true).toBe(true) // Logger accepted all status types without error
|
expect(entries[1].status).toBe('failure')
|
||||||
|
expect(entries[2].status).toBe('partial')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle metadata correctly (with and without)', async () => {
|
it('should default to empty metadata when not provided', async () => {
|
||||||
const { logAudit, closeAuditLogger } =
|
const { logAudit, applyAuditConfig } = auditLoggerModule
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
// Without metadata
|
applyAuditConfig(30)
|
||||||
logAudit('LOGIN', 'user-no-meta', {
|
|
||||||
username: 'no.meta',
|
logAudit(AuditAction.SYSTEM_ERROR, 'user-no-meta', {
|
||||||
|
username: 'tester',
|
||||||
computerName: 'PC-001',
|
computerName: 'PC-001',
|
||||||
resource: 'ERP',
|
resource: 'ERP',
|
||||||
status: 'success'
|
status: AuditStatus.SUCCESS
|
||||||
})
|
})
|
||||||
|
|
||||||
// With metadata
|
const entry = JSON.parse(infoSpy.mock.calls[0][0])
|
||||||
logAudit('LOGOUT', 'user-with-meta', {
|
expect(entry.metadata).toEqual({})
|
||||||
username: 'with.meta',
|
|
||||||
computerName: 'PC-002',
|
|
||||||
resource: 'ERP',
|
|
||||||
status: 'success',
|
|
||||||
metadata: { sessionDuration: 3600, actionsPerformed: 15 }
|
|
||||||
})
|
|
||||||
|
|
||||||
closeAuditLogger()
|
|
||||||
|
|
||||||
// Both entries should be processed successfully
|
|
||||||
expect(true).toBe(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should generate ISO 8601 timestamp', async () => {
|
it('should handle special characters in fields without error', async () => {
|
||||||
const { logAudit, closeAuditLogger } =
|
const { logAudit, applyAuditConfig } = auditLoggerModule
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
const beforeLog = Date.now()
|
applyAuditConfig(30)
|
||||||
|
|
||||||
logAudit('TEST', 'timestamp-user', {
|
logAudit(AuditAction.LOGIN, 'user-special', {
|
||||||
username: 'timestamp.test',
|
|
||||||
computerName: 'PC-TS',
|
|
||||||
resource: 'test_resource',
|
|
||||||
status: 'success'
|
|
||||||
})
|
|
||||||
|
|
||||||
closeAuditLogger()
|
|
||||||
|
|
||||||
const afterLog = Date.now()
|
|
||||||
|
|
||||||
// Timestamp should be generated within the test execution window
|
|
||||||
expect(beforeLog).toBeLessThanOrEqual(afterLog)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should close audit logger without errors', async () => {
|
|
||||||
const { closeAuditLogger } = await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
// Should complete without throwing
|
|
||||||
expect(() => closeAuditLogger()).not.toThrow()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle special characters in fields', async () => {
|
|
||||||
const { logAudit, closeAuditLogger } =
|
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
logAudit('LOGIN_ATTEMPT', 'user-special', {
|
|
||||||
username: 'user.name+test@example.com',
|
username: 'user.name+test@example.com',
|
||||||
computerName: 'DESKTOP-特殊字符-001',
|
computerName: 'DESKTOP-特殊字符-001',
|
||||||
resource: 'ERP/子系统',
|
resource: 'ERP/子系统',
|
||||||
status: 'failure',
|
status: AuditStatus.FAILURE,
|
||||||
metadata: { reason: '密码错误', attempt: 3 }
|
metadata: { reason: '密码错误', attempt: 3 }
|
||||||
})
|
})
|
||||||
|
|
||||||
closeAuditLogger()
|
expect(infoSpy).toHaveBeenCalledTimes(1)
|
||||||
|
const entry = JSON.parse(infoSpy.mock.calls[0][0])
|
||||||
// Should handle without errors
|
expect(entry.username).toBe('user.name+test@example.com')
|
||||||
expect(true).toBe(true)
|
expect(entry.computerName).toBe('DESKTOP-特殊字符-001')
|
||||||
|
expect(entry.resource).toBe('ERP/子系统')
|
||||||
|
expect(entry.metadata.reason).toBe('密码错误')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle empty metadata gracefully', async () => {
|
it('should close audit logger without errors', async () => {
|
||||||
const { logAudit, closeAuditLogger } =
|
const { closeAuditLogger } = auditLoggerModule
|
||||||
await import('../../src/main/services/logger/audit-logger')
|
|
||||||
|
|
||||||
logAudit('PING', 'ping-user', {
|
expect(() => closeAuditLogger()).not.toThrow()
|
||||||
username: 'pinger',
|
|
||||||
computerName: 'PC-PING',
|
|
||||||
resource: 'health_check',
|
|
||||||
status: 'success',
|
|
||||||
metadata: {}
|
|
||||||
})
|
|
||||||
|
|
||||||
closeAuditLogger()
|
|
||||||
|
|
||||||
// Should handle empty metadata
|
|
||||||
expect(true).toBe(true)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,11 +8,23 @@ const withErrorHandlingMock = vi.fn(async (handler: () => Promise<unknown>) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const serviceMethods = {
|
const serviceMethods = {
|
||||||
runCleaner: vi.fn(async (_sender: unknown, input: unknown) => ({ success: true, input })),
|
runCleaner: vi.fn(
|
||||||
|
async (
|
||||||
|
_sender: unknown,
|
||||||
|
input: unknown,
|
||||||
|
_batchId: string,
|
||||||
|
_historyDao: unknown,
|
||||||
|
_appVersion: string
|
||||||
|
) => ({ success: true, input })
|
||||||
|
),
|
||||||
exportResults: vi.fn(async (items: unknown[]) => ({ success: true, total: items.length }))
|
exportResults: vi.fn(async (items: unknown[]) => ({ success: true, total: items.length }))
|
||||||
}
|
}
|
||||||
|
|
||||||
vi.mock('electron', () => ({
|
vi.mock('electron', () => ({
|
||||||
|
app: {
|
||||||
|
isPackaged: false,
|
||||||
|
getVersion: () => 'test-version'
|
||||||
|
},
|
||||||
ipcMain: {
|
ipcMain: {
|
||||||
handle: handleMock
|
handle: handleMock
|
||||||
}
|
}
|
||||||
@@ -51,7 +63,14 @@ describe('registerCleanerHandlers', () => {
|
|||||||
|
|
||||||
const result = await runHandler?.(event, input)
|
const result = await runHandler?.(event, input)
|
||||||
|
|
||||||
expect(serviceMethods.runCleaner).toHaveBeenCalledWith(sender, input)
|
expect(serviceMethods.runCleaner).toHaveBeenCalledTimes(1)
|
||||||
|
const actualCall = serviceMethods.runCleaner.mock.calls[0]
|
||||||
|
expect(actualCall[0]).toBe(sender)
|
||||||
|
expect(actualCall[1]).toEqual(input)
|
||||||
|
expect(typeof actualCall[2]).toBe('string') // batchId is UUID
|
||||||
|
expect(actualCall[3]).toBeDefined() // historyDao
|
||||||
|
expect(actualCall[4]).toBe('test-version') // appVersion
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
success: true,
|
success: true,
|
||||||
data: { success: true, input }
|
data: { success: true, input }
|
||||||
|
|||||||
@@ -1,166 +0,0 @@
|
|||||||
import { describe, it, expect } from 'vitest'
|
|
||||||
import {
|
|
||||||
createBatches,
|
|
||||||
getMissingOrders,
|
|
||||||
runWithConcurrency
|
|
||||||
} from '../../src/main/services/erp/cleaner'
|
|
||||||
import type { ShouldDeleteParams } from '../../src/main/services/erp/cleaner'
|
|
||||||
|
|
||||||
describe('Cleaner Service (Unit)', () => {
|
|
||||||
describe('shouldDeleteMaterial', () => {
|
|
||||||
// Create a mock cleaner service (no auth needed for this pure function test)
|
|
||||||
const mockCleaner = {
|
|
||||||
shouldDeleteMaterial: (params: ShouldDeleteParams): boolean => {
|
|
||||||
const { rowNumber, pendingQty, materialCode, deleteSet } = params
|
|
||||||
|
|
||||||
// Check if material is in delete list
|
|
||||||
if (!deleteSet.has(materialCode)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check row number range (2000-7999 are protected)
|
|
||||||
if (rowNumber >= 2000 && rowNumber < 8000) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check pending quantity (must be empty)
|
|
||||||
if (pendingQty && pendingQty.trim() !== '') {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
it('should skip materials with row number 2000-7999', () => {
|
|
||||||
const testCases = [
|
|
||||||
{ rowNumber: 2000, pendingQty: '', materialCode: 'TEST001', expected: false },
|
|
||||||
{ rowNumber: 5000, pendingQty: '', materialCode: 'TEST001', expected: false },
|
|
||||||
{ rowNumber: 7999, pendingQty: '', materialCode: 'TEST001', expected: false },
|
|
||||||
{ rowNumber: 1999, pendingQty: '', materialCode: 'TEST001', expected: true },
|
|
||||||
{ rowNumber: 8000, pendingQty: '', materialCode: 'TEST001', expected: true }
|
|
||||||
]
|
|
||||||
|
|
||||||
for (const tc of testCases) {
|
|
||||||
const shouldDelete = mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: tc.rowNumber,
|
|
||||||
pendingQty: tc.pendingQty,
|
|
||||||
materialCode: tc.materialCode,
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
expect(shouldDelete).toBe(tc.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should skip materials with non-empty pending quantity', () => {
|
|
||||||
const result = mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 100,
|
|
||||||
pendingQty: '5',
|
|
||||||
materialCode: 'TEST001',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should skip materials not in delete list', () => {
|
|
||||||
const result = mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 100,
|
|
||||||
pendingQty: '',
|
|
||||||
materialCode: 'NOT_IN_LIST',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should delete materials with empty pending qty and valid row number', () => {
|
|
||||||
const testCases = [
|
|
||||||
{ rowNumber: 1, pendingQty: '', materialCode: 'TEST001', expected: true },
|
|
||||||
{ rowNumber: 100, pendingQty: '', materialCode: 'TEST001', expected: true },
|
|
||||||
{ rowNumber: 1999, pendingQty: '', materialCode: 'TEST001', expected: true },
|
|
||||||
{ rowNumber: 8000, pendingQty: '', materialCode: 'TEST001', expected: true },
|
|
||||||
{ rowNumber: 10000, pendingQty: '', materialCode: 'TEST001', expected: true }
|
|
||||||
]
|
|
||||||
|
|
||||||
for (const tc of testCases) {
|
|
||||||
const shouldDelete = mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: tc.rowNumber,
|
|
||||||
pendingQty: tc.pendingQty,
|
|
||||||
materialCode: tc.materialCode,
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
expect(shouldDelete).toBe(tc.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle multiple conditions correctly', () => {
|
|
||||||
// Material in list, valid row, no pending qty = should delete
|
|
||||||
expect(
|
|
||||||
mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 100,
|
|
||||||
pendingQty: '',
|
|
||||||
materialCode: 'TEST001',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
).toBe(true)
|
|
||||||
|
|
||||||
// Material in list, protected row, no pending qty = should NOT delete
|
|
||||||
expect(
|
|
||||||
mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 7500,
|
|
||||||
pendingQty: '',
|
|
||||||
materialCode: 'TEST001',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
).toBe(false)
|
|
||||||
|
|
||||||
// Material in list, valid row, has pending qty = should NOT delete
|
|
||||||
expect(
|
|
||||||
mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 100,
|
|
||||||
pendingQty: '10',
|
|
||||||
materialCode: 'TEST001',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
).toBe(false)
|
|
||||||
|
|
||||||
// Material NOT in list = should NOT delete
|
|
||||||
expect(
|
|
||||||
mockCleaner.shouldDeleteMaterial({
|
|
||||||
rowNumber: 100,
|
|
||||||
pendingQty: '',
|
|
||||||
materialCode: 'OTHER',
|
|
||||||
deleteSet: new Set(['TEST001'])
|
|
||||||
})
|
|
||||||
).toBe(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('batch and concurrency helpers', () => {
|
|
||||||
it('should split orders into batches', () => {
|
|
||||||
const batches = createBatches(['A', 'B', 'C', 'D', 'E'], 2)
|
|
||||||
expect(batches).toEqual([['A', 'B'], ['C', 'D'], ['E']])
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should identify missing orders', () => {
|
|
||||||
const missing = getMissingOrders(['SC1', 'SC2', 'SC3'], new Set(['SC1', 'SC3']))
|
|
||||||
expect(missing).toEqual(['SC2'])
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should respect concurrency limit', async () => {
|
|
||||||
const items = [1, 2, 3, 4, 5, 6]
|
|
||||||
let running = 0
|
|
||||||
let peak = 0
|
|
||||||
await runWithConcurrency(items, 2, async () => {
|
|
||||||
running += 1
|
|
||||||
peak = Math.max(peak, running)
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
||||||
running -= 1
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(peak).toBeLessThanOrEqual(2)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* ConfigManager Unit Tests
|
* ConfigManager Unit Tests
|
||||||
*
|
*
|
||||||
* Tests for ConfigManager service configuration and validation functionality.
|
* Tests for ConfigManager default configuration values, schema validation,
|
||||||
|
* and singleton behavior.
|
||||||
* Logger is mocked to isolate ConfigManager testing.
|
* Logger is mocked to isolate ConfigManager testing.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -33,36 +34,7 @@ describe('ConfigManager', () => {
|
|||||||
vi.resetModules()
|
vi.resetModules()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should load ConfigManager class', async () => {
|
it('should return default config with correct logging values', async () => {
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
|
||||||
expect(ConfigManager).toBeDefined()
|
|
||||||
expect(typeof ConfigManager.getInstance).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have logging configuration methods', async () => {
|
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
|
||||||
|
|
||||||
const manager = ConfigManager.getInstance()
|
|
||||||
|
|
||||||
expect(manager.getLoggingConfig).toBeDefined()
|
|
||||||
expect(typeof manager.getLoggingConfig).toBe('function')
|
|
||||||
expect(manager.getDefaultConfig).toBeDefined()
|
|
||||||
expect(typeof manager.getDefaultConfig).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should return default logging config structure', async () => {
|
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
|
||||||
|
|
||||||
const manager = ConfigManager.getInstance()
|
|
||||||
const defaultConfig = manager.getDefaultConfig()
|
|
||||||
|
|
||||||
expect(defaultConfig.logging).toBeDefined()
|
|
||||||
expect(defaultConfig.logging.level).toBe('info')
|
|
||||||
expect(defaultConfig.logging.auditRetention).toBe(30)
|
|
||||||
expect(defaultConfig.logging.appRetention).toBe(14)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should get default logging config values', async () => {
|
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
||||||
|
|
||||||
const manager = ConfigManager.getInstance()
|
const manager = ConfigManager.getInstance()
|
||||||
@@ -73,14 +45,52 @@ describe('ConfigManager', () => {
|
|||||||
expect(defaultConfig.logging.appRetention).toBe(14)
|
expect(defaultConfig.logging.appRetention).toBe(14)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export fullConfigSchema for validation', async () => {
|
it('should return default config with correct database defaults', async () => {
|
||||||
const { fullConfigSchema } = await import('../../src/main/types/config.schema')
|
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
||||||
|
|
||||||
expect(fullConfigSchema).toBeDefined()
|
const manager = ConfigManager.getInstance()
|
||||||
expect(typeof fullConfigSchema.parse).toBe('function')
|
const defaultConfig = manager.getDefaultConfig()
|
||||||
expect(typeof fullConfigSchema.safeParse).toBe('function')
|
|
||||||
|
expect(defaultConfig.database.activeType).toBe('mysql')
|
||||||
|
expect(defaultConfig.database.mysql.host).toBe('localhost')
|
||||||
|
expect(defaultConfig.database.mysql.port).toBe(3306)
|
||||||
|
expect(defaultConfig.database.mysql.database).toBe('erp_db')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return default config with correct extraction defaults', async () => {
|
||||||
|
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
||||||
|
|
||||||
|
const manager = ConfigManager.getInstance()
|
||||||
|
const defaultConfig = manager.getDefaultConfig()
|
||||||
|
|
||||||
|
expect(defaultConfig.extraction.batchSize).toBe(100)
|
||||||
|
expect(defaultConfig.extraction.headless).toBe(true)
|
||||||
|
expect(defaultConfig.extraction.autoConvert).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw when getConfig() is called before initialize()', async () => {
|
||||||
|
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
||||||
|
|
||||||
|
// Reset singleton to get a fresh uninitialized instance
|
||||||
|
const FreshConfigManager = ConfigManager as any
|
||||||
|
FreshConfigManager.instance = null
|
||||||
|
|
||||||
|
const manager = ConfigManager.getInstance()
|
||||||
|
|
||||||
|
expect(() => manager.getConfig()).toThrow('Configuration not initialized')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return the same singleton instance', async () => {
|
||||||
|
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
||||||
|
|
||||||
|
const a = ConfigManager.getInstance()
|
||||||
|
const b = ConfigManager.getInstance()
|
||||||
|
|
||||||
|
expect(a).toBe(b)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Config Schema Validation', () => {
|
||||||
it('should validate complete logging configuration', async () => {
|
it('should validate complete logging configuration', async () => {
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
@@ -99,4 +109,51 @@ describe('ConfigManager', () => {
|
|||||||
expect(result.data.appRetention).toBe(21)
|
expect(result.data.appRetention).toBe(21)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should validate logging level enum values', async () => {
|
||||||
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
|
const validLevels = ['error', 'warn', 'info', 'debug', 'verbose']
|
||||||
|
|
||||||
|
for (const level of validLevels) {
|
||||||
|
const result = loggingConfigSchema.safeParse({ level })
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should reject invalid logging level', async () => {
|
||||||
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
|
const result = loggingConfigSchema.safeParse({ level: 'invalid_level' })
|
||||||
|
expect(result.success).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should validate audit retention range (1-365)', async () => {
|
||||||
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
|
expect(loggingConfigSchema.safeParse({ auditRetention: 1 }).success).toBe(true)
|
||||||
|
expect(loggingConfigSchema.safeParse({ auditRetention: 365 }).success).toBe(true)
|
||||||
|
expect(loggingConfigSchema.safeParse({ auditRetention: 0 }).success).toBe(false)
|
||||||
|
expect(loggingConfigSchema.safeParse({ auditRetention: 366 }).success).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should validate app retention range (1-365)', async () => {
|
||||||
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
|
expect(loggingConfigSchema.safeParse({ appRetention: 1 }).success).toBe(true)
|
||||||
|
expect(loggingConfigSchema.safeParse({ appRetention: 365 }).success).toBe(true)
|
||||||
|
expect(loggingConfigSchema.safeParse({ appRetention: 0 }).success).toBe(false)
|
||||||
|
expect(loggingConfigSchema.safeParse({ appRetention: 366 }).success).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use default values when logging config is partial', async () => {
|
||||||
|
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
||||||
|
|
||||||
|
const result = loggingConfigSchema.safeParse({ level: 'warn' })
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.data.auditRetention).toBe(30)
|
||||||
|
expect(result.data.appRetention).toBe(14)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
141
tests/unit/dialects/mysql-dialect.test.ts
Normal file
141
tests/unit/dialects/mysql-dialect.test.ts
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/**
|
||||||
|
* Unit tests for MySqlDialect
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { MySqlDialect } from '@services/database/dialects/mysql-dialect'
|
||||||
|
|
||||||
|
describe('MySqlDialect', () => {
|
||||||
|
const dialect = new MySqlDialect()
|
||||||
|
|
||||||
|
describe('dbType', () => {
|
||||||
|
it('should return mysql', () => {
|
||||||
|
expect(dialect.dbType).toBe('mysql')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('quoteTableName', () => {
|
||||||
|
it('should join schema and table with underscore', () => {
|
||||||
|
expect(dialect.quoteTableName('dbo', 'Table')).toBe('dbo_Table')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle arbitrary schema and table names', () => {
|
||||||
|
expect(dialect.quoteTableName('my_schema', 'my_table')).toBe('my_schema_my_table')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('param', () => {
|
||||||
|
it('should return ? for any index', () => {
|
||||||
|
expect(dialect.param(0)).toBe('?')
|
||||||
|
expect(dialect.param(1)).toBe('?')
|
||||||
|
expect(dialect.param(5)).toBe('?')
|
||||||
|
expect(dialect.param(100)).toBe('?')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('params', () => {
|
||||||
|
it('should return comma-separated question marks', () => {
|
||||||
|
expect(dialect.params(1)).toBe('?')
|
||||||
|
expect(dialect.params(3)).toBe('?,?,?')
|
||||||
|
expect(dialect.params(5)).toBe('?,?,?,?,?')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return empty string for count 0', () => {
|
||||||
|
expect(dialect.params(0)).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('currentTimestamp', () => {
|
||||||
|
it('should return UTC_TIMESTAMP()', () => {
|
||||||
|
expect(dialect.currentTimestamp()).toBe('UTC_TIMESTAMP()')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('upsert', () => {
|
||||||
|
it('should generate ON DUPLICATE KEY UPDATE SQL', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: 'dbo_Table',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name', 'value'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toBe(
|
||||||
|
'INSERT INTO dbo_Table (id, name, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), value = VALUES(value)'
|
||||||
|
)
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle composite key columns', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: 'dbo_Table',
|
||||||
|
keyColumns: ['id', 'code'],
|
||||||
|
allColumns: ['id', 'code', 'name', 'value'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('ON DUPLICATE KEY UPDATE')
|
||||||
|
expect(result.sql).toContain('name = VALUES(name)')
|
||||||
|
expect(result.sql).toContain('value = VALUES(value)')
|
||||||
|
// key columns should NOT appear in the UPDATE SET clause
|
||||||
|
expect(result.sql).not.toContain('id = VALUES(id)')
|
||||||
|
expect(result.sql).not.toContain('code = VALUES(code)')
|
||||||
|
expect(result.nextParamIndex).toBe(4)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should advance nextParamIndex from non-zero start', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: 'dbo_Table',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name'],
|
||||||
|
startParamIndex: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.nextParamIndex).toBe(7)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('paginate', () => {
|
||||||
|
it('should append LIMIT and OFFSET with literal values', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM dbo_Table',
|
||||||
|
limit: 10,
|
||||||
|
offset: 20,
|
||||||
|
paramIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toBe('SELECT * FROM dbo_Table LIMIT 10 OFFSET 20')
|
||||||
|
expect(result.nextParamIndex).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use 0 as default offset', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM dbo_Table',
|
||||||
|
limit: 50,
|
||||||
|
paramIndex: 3
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toBe('SELECT * FROM dbo_Table LIMIT 50 OFFSET 0')
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not change nextParamIndex (no params added)', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM dbo_Table',
|
||||||
|
limit: 100,
|
||||||
|
offset: 50,
|
||||||
|
paramIndex: 10
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.nextParamIndex).toBe(10)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('maxBatchRows', () => {
|
||||||
|
it('should return 1000 regardless of columns', () => {
|
||||||
|
expect(dialect.maxBatchRows(1)).toBe(1000)
|
||||||
|
expect(dialect.maxBatchRows(10)).toBe(1000)
|
||||||
|
expect(dialect.maxBatchRows(100)).toBe(1000)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
145
tests/unit/dialects/postgresql-dialect.test.ts
Normal file
145
tests/unit/dialects/postgresql-dialect.test.ts
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
/**
|
||||||
|
* Unit tests for PostgreSqlDialect
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { PostgreSqlDialect } from '@services/database/dialects/postgresql-dialect'
|
||||||
|
|
||||||
|
describe('PostgreSqlDialect', () => {
|
||||||
|
const dialect = new PostgreSqlDialect()
|
||||||
|
|
||||||
|
describe('dbType', () => {
|
||||||
|
it('should return postgresql', () => {
|
||||||
|
expect(dialect.dbType).toBe('postgresql')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('quoteTableName', () => {
|
||||||
|
it('should wrap schema and table in double quotes', () => {
|
||||||
|
expect(dialect.quoteTableName('dbo', 'Table')).toBe('"dbo"."Table"')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle arbitrary names', () => {
|
||||||
|
expect(dialect.quoteTableName('my_schema', 'my_table')).toBe('"my_schema"."my_table"')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('param', () => {
|
||||||
|
it('should return $N with 1-based index ($1 for param(0))', () => {
|
||||||
|
expect(dialect.param(0)).toBe('$1')
|
||||||
|
expect(dialect.param(1)).toBe('$2')
|
||||||
|
expect(dialect.param(3)).toBe('$4')
|
||||||
|
expect(dialect.param(10)).toBe('$11')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('params', () => {
|
||||||
|
it('should return comma-separated $N placeholders (1-based)', () => {
|
||||||
|
expect(dialect.params(1)).toBe('$1')
|
||||||
|
expect(dialect.params(3)).toBe('$1,$2,$3')
|
||||||
|
expect(dialect.params(5)).toBe('$1,$2,$3,$4,$5')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return empty string for count 0', () => {
|
||||||
|
expect(dialect.params(0)).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('currentTimestamp', () => {
|
||||||
|
it('should return CURRENT_TIMESTAMP', () => {
|
||||||
|
expect(dialect.currentTimestamp()).toBe('CURRENT_TIMESTAMP')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('upsert', () => {
|
||||||
|
it('should generate ON CONFLICT DO UPDATE SQL', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '"dbo"."Table"',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name', 'value'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('INSERT INTO "dbo"."Table" (id, name, value)')
|
||||||
|
expect(result.sql).toContain('VALUES ($1, $2, $3)')
|
||||||
|
expect(result.sql).toContain('ON CONFLICT ("id")')
|
||||||
|
expect(result.sql).toContain('DO UPDATE SET')
|
||||||
|
expect(result.sql).toContain('"name" = EXCLUDED."name"')
|
||||||
|
expect(result.sql).toContain('"value" = EXCLUDED."value"')
|
||||||
|
// key column should NOT appear in DO UPDATE SET
|
||||||
|
expect(result.sql).not.toContain('"id" = EXCLUDED."id"')
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle composite key columns with all double-quoted', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '"dbo"."Table"',
|
||||||
|
keyColumns: ['id', 'code'],
|
||||||
|
allColumns: ['id', 'code', 'name'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('ON CONFLICT ("id", "code")')
|
||||||
|
expect(result.sql).toContain('"name" = EXCLUDED."name"')
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use startParamIndex for parameter numbering', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '"dbo"."Table"',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name'],
|
||||||
|
startParamIndex: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
// startParamIndex=5 means $6, $7 (1-based: index+1)
|
||||||
|
expect(result.sql).toContain('$6')
|
||||||
|
expect(result.sql).toContain('$7')
|
||||||
|
expect(result.nextParamIndex).toBe(7)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('paginate', () => {
|
||||||
|
it('should append LIMIT and OFFSET with literal values', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM "dbo"."Table"',
|
||||||
|
limit: 10,
|
||||||
|
offset: 20,
|
||||||
|
paramIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toBe('SELECT * FROM "dbo"."Table" LIMIT 10 OFFSET 20')
|
||||||
|
expect(result.nextParamIndex).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use 0 as default offset', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM "dbo"."Table"',
|
||||||
|
limit: 50,
|
||||||
|
paramIndex: 3
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toBe('SELECT * FROM "dbo"."Table" LIMIT 50 OFFSET 0')
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not change nextParamIndex (no params added)', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM "dbo"."Table"',
|
||||||
|
limit: 100,
|
||||||
|
offset: 50,
|
||||||
|
paramIndex: 10
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.nextParamIndex).toBe(10)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('maxBatchRows', () => {
|
||||||
|
it('should return 1000 regardless of columns', () => {
|
||||||
|
expect(dialect.maxBatchRows(1)).toBe(1000)
|
||||||
|
expect(dialect.maxBatchRows(10)).toBe(1000)
|
||||||
|
expect(dialect.maxBatchRows(100)).toBe(1000)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
164
tests/unit/dialects/sqlserver-dialect.test.ts
Normal file
164
tests/unit/dialects/sqlserver-dialect.test.ts
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
/**
|
||||||
|
* Unit tests for SqlServerDialect
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { SqlServerDialect } from '@services/database/dialects/sqlserver-dialect'
|
||||||
|
|
||||||
|
describe('SqlServerDialect', () => {
|
||||||
|
const dialect = new SqlServerDialect()
|
||||||
|
|
||||||
|
describe('dbType', () => {
|
||||||
|
it('should return sqlserver', () => {
|
||||||
|
expect(dialect.dbType).toBe('sqlserver')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('quoteTableName', () => {
|
||||||
|
it('should wrap schema and table in brackets', () => {
|
||||||
|
expect(dialect.quoteTableName('dbo', 'Table')).toBe('[dbo].[Table]')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle arbitrary names', () => {
|
||||||
|
expect(dialect.quoteTableName('my_schema', 'my_table')).toBe('[my_schema].[my_table]')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('param', () => {
|
||||||
|
it('should return @pN with 0-based index', () => {
|
||||||
|
expect(dialect.param(0)).toBe('@p0')
|
||||||
|
expect(dialect.param(1)).toBe('@p1')
|
||||||
|
expect(dialect.param(5)).toBe('@p5')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('params', () => {
|
||||||
|
it('should return comma-separated @pN placeholders', () => {
|
||||||
|
expect(dialect.params(1)).toBe('@p0')
|
||||||
|
expect(dialect.params(3)).toBe('@p0,@p1,@p2')
|
||||||
|
expect(dialect.params(5)).toBe('@p0,@p1,@p2,@p3,@p4')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return empty string for count 0', () => {
|
||||||
|
expect(dialect.params(0)).toBe('')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('currentTimestamp', () => {
|
||||||
|
it('should return SYSUTCDATETIME()', () => {
|
||||||
|
expect(dialect.currentTimestamp()).toBe('SYSUTCDATETIME()')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('upsert', () => {
|
||||||
|
it('should generate MERGE SQL with single key column', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '[dbo].[Table]',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name', 'value'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should contain MERGE ... USING ... ON ... WHEN MATCHED ... WHEN NOT MATCHED ...
|
||||||
|
expect(result.sql).toContain('MERGE [dbo].[Table] AS target')
|
||||||
|
expect(result.sql).toContain('USING (VALUES (@p0, @p1, @p2)) AS source (id, name, value)')
|
||||||
|
expect(result.sql).toContain('ON target.id = source.id')
|
||||||
|
expect(result.sql).toContain('WHEN MATCHED THEN UPDATE SET')
|
||||||
|
expect(result.sql).toContain('target.name = source.name')
|
||||||
|
expect(result.sql).toContain('target.value = source.value')
|
||||||
|
expect(result.sql).toContain('WHEN NOT MATCHED THEN INSERT (id, name, value)')
|
||||||
|
expect(result.sql).toContain('VALUES (source.id, source.name, source.value)')
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle composite key columns', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '[dbo].[Table]',
|
||||||
|
keyColumns: ['id', 'code'],
|
||||||
|
allColumns: ['id', 'code', 'name'],
|
||||||
|
startParamIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('ON target.id = source.id AND target.code = source.code')
|
||||||
|
// non-key columns in UPDATE SET
|
||||||
|
expect(result.sql).toContain('target.name = source.name')
|
||||||
|
// key columns should NOT be in UPDATE SET
|
||||||
|
expect(result.sql).not.toMatch(/UPDATE SET[\s\S]*target\.id = source\.id/)
|
||||||
|
expect(result.nextParamIndex).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use startParamIndex for parameter names', () => {
|
||||||
|
const result = dialect.upsert({
|
||||||
|
table: '[dbo].[Table]',
|
||||||
|
keyColumns: ['id'],
|
||||||
|
allColumns: ['id', 'name'],
|
||||||
|
startParamIndex: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('@p5')
|
||||||
|
expect(result.sql).toContain('@p6')
|
||||||
|
expect(result.nextParamIndex).toBe(7)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('paginate', () => {
|
||||||
|
it('should append OFFSET/FETCH with parameterized offset when offset is provided', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM [dbo].[Table] ORDER BY id',
|
||||||
|
limit: 10,
|
||||||
|
offset: 20,
|
||||||
|
paramIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('OFFSET @p0 ROWS')
|
||||||
|
expect(result.sql).toContain('FETCH NEXT @p1 ROWS ONLY')
|
||||||
|
expect(result.nextParamIndex).toBe(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use literal 0 offset when no offset is provided', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM [dbo].[Table] ORDER BY id',
|
||||||
|
limit: 50,
|
||||||
|
paramIndex: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('OFFSET 0 ROWS')
|
||||||
|
expect(result.sql).toContain('FETCH NEXT @p0 ROWS ONLY')
|
||||||
|
expect(result.nextParamIndex).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should advance paramIndex from non-zero start with offset', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM [dbo].[Table] ORDER BY id',
|
||||||
|
limit: 10,
|
||||||
|
offset: 100,
|
||||||
|
paramIndex: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('OFFSET @p5 ROWS')
|
||||||
|
expect(result.sql).toContain('FETCH NEXT @p6 ROWS ONLY')
|
||||||
|
expect(result.nextParamIndex).toBe(7)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should advance paramIndex from non-zero start without offset', () => {
|
||||||
|
const result = dialect.paginate({
|
||||||
|
sql: 'SELECT * FROM [dbo].[Table] ORDER BY id',
|
||||||
|
limit: 10,
|
||||||
|
paramIndex: 3
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.sql).toContain('OFFSET 0 ROWS')
|
||||||
|
expect(result.sql).toContain('FETCH NEXT @p3 ROWS ONLY')
|
||||||
|
expect(result.nextParamIndex).toBe(4)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('maxBatchRows', () => {
|
||||||
|
it('should return floor(2000 / columnsPerRow)', () => {
|
||||||
|
expect(dialect.maxBatchRows(10)).toBe(200)
|
||||||
|
expect(dialect.maxBatchRows(28)).toBe(71)
|
||||||
|
expect(dialect.maxBatchRows(1)).toBe(2000)
|
||||||
|
expect(dialect.maxBatchRows(100)).toBe(20)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest'
|
|
||||||
import { ErpAuthService } from '../../src/main/services/erp/erp-auth'
|
|
||||||
import type { ErpConfig } from '../../src/main/types/erp.types'
|
|
||||||
|
|
||||||
describe('ERP Authentication Service (Unit)', () => {
|
|
||||||
describe('Session Management', () => {
|
|
||||||
it('should create service instance with config', () => {
|
|
||||||
const config: ErpConfig = {
|
|
||||||
url: 'https://test.example.com',
|
|
||||||
username: 'testuser',
|
|
||||||
password: 'testpass'
|
|
||||||
}
|
|
||||||
|
|
||||||
const service = new ErpAuthService(config)
|
|
||||||
|
|
||||||
expect(service).toBeDefined()
|
|
||||||
expect(service.isActive()).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should throw error when getting session before login', () => {
|
|
||||||
const config: ErpConfig = {
|
|
||||||
url: 'https://test.example.com',
|
|
||||||
username: 'testuser',
|
|
||||||
password: 'testpass'
|
|
||||||
}
|
|
||||||
|
|
||||||
const service = new ErpAuthService(config)
|
|
||||||
|
|
||||||
expect(() => service.getSession()).toThrow('Not logged in. Call login() first.')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should report inactive status before login', () => {
|
|
||||||
const config: ErpConfig = {
|
|
||||||
url: 'https://test.example.com',
|
|
||||||
username: 'testuser',
|
|
||||||
password: 'testpass'
|
|
||||||
}
|
|
||||||
|
|
||||||
const service = new ErpAuthService(config)
|
|
||||||
|
|
||||||
expect(service.isActive()).toBe(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Close Method', () => {
|
|
||||||
it('should handle close when no session exists', async () => {
|
|
||||||
const config: ErpConfig = {
|
|
||||||
url: 'https://test.example.com',
|
|
||||||
username: 'testuser',
|
|
||||||
password: 'testpass'
|
|
||||||
}
|
|
||||||
|
|
||||||
const service = new ErpAuthService(config)
|
|
||||||
|
|
||||||
// Should not throw when closing without session
|
|
||||||
await expect(service.close()).resolves.toBeUndefined()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Class Structure', () => {
|
|
||||||
let service: ErpAuthService
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
const config: ErpConfig = {
|
|
||||||
url: 'https://test.example.com',
|
|
||||||
username: 'testuser',
|
|
||||||
password: 'testpass'
|
|
||||||
}
|
|
||||||
service = new ErpAuthService(config)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have login method that returns a Promise', () => {
|
|
||||||
expect(service.login).toBeDefined()
|
|
||||||
expect(typeof service.login).toBe('function')
|
|
||||||
expect(service.login()).toBeInstanceOf(Promise)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have close method', () => {
|
|
||||||
expect(service.close).toBeDefined()
|
|
||||||
expect(typeof service.close).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have getSession method', () => {
|
|
||||||
expect(service.getSession).toBeDefined()
|
|
||||||
expect(typeof service.getSession).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have isActive method', () => {
|
|
||||||
expect(service.isActive).toBeDefined()
|
|
||||||
expect(typeof service.isActive).toBe('function')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest'
|
|
||||||
import { ExtractorService } from '../../src/main/services/erp/extractor'
|
|
||||||
import { ErpAuthService } from '../../src/main/services/erp/erp-auth'
|
|
||||||
import type { ErpConfig } from '../../src/main/types/erp.types'
|
|
||||||
|
|
||||||
describe('Extractor Service (Unit)', () => {
|
|
||||||
let authService: ErpAuthService
|
|
||||||
let extractor: ExtractorService
|
|
||||||
const mockConfig: ErpConfig = {
|
|
||||||
url: 'https://test.erp.com',
|
|
||||||
username: 'test_user',
|
|
||||||
password: 'test_pass'
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
authService = new ErpAuthService(mockConfig)
|
|
||||||
extractor = new ExtractorService(authService, './test-downloads')
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Batch Creation', () => {
|
|
||||||
it('should create single batch for small order list', () => {
|
|
||||||
// This tests the createBatches method indirectly through extract
|
|
||||||
// We'll need to add a public method or test through the class
|
|
||||||
const orders = ['ORDER1', 'ORDER2', 'ORDER3']
|
|
||||||
const batchSize = 10
|
|
||||||
|
|
||||||
// Expected: 1 batch with 3 orders
|
|
||||||
const expectedBatches = 1
|
|
||||||
expect(Math.ceil(orders.length / batchSize)).toBe(expectedBatches)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should create multiple batches for large order list', () => {
|
|
||||||
const orders = Array.from({ length: 250 }, (_, i) => `ORDER${i}`)
|
|
||||||
const batchSize = 100
|
|
||||||
|
|
||||||
// Expected: 3 batches (100, 100, 50)
|
|
||||||
const expectedBatches = 3
|
|
||||||
expect(Math.ceil(orders.length / batchSize)).toBe(expectedBatches)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle exact batch size', () => {
|
|
||||||
const orders = Array.from({ length: 200 }, (_, i) => `ORDER${i}`)
|
|
||||||
const batchSize = 100
|
|
||||||
|
|
||||||
// Expected: 2 batches exactly
|
|
||||||
const expectedBatches = 2
|
|
||||||
expect(Math.ceil(orders.length / batchSize)).toBe(expectedBatches)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle empty order list', () => {
|
|
||||||
const orders: string[] = []
|
|
||||||
const batchSize = 100
|
|
||||||
|
|
||||||
// Expected: 0 batches
|
|
||||||
const expectedBatches = 0
|
|
||||||
expect(Math.ceil(orders.length / batchSize)).toBe(expectedBatches)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Service Initialization', () => {
|
|
||||||
it('should create service instance', () => {
|
|
||||||
expect(extractor).toBeDefined()
|
|
||||||
expect(extractor).toBeInstanceOf(ExtractorService)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should use default download directory', () => {
|
|
||||||
const defaultExtractor = new ExtractorService(authService)
|
|
||||||
expect(defaultExtractor).toBeDefined()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should use custom download directory', () => {
|
|
||||||
const customExtractor = new ExtractorService(authService, './custom-downloads')
|
|
||||||
expect(customExtractor).toBeDefined()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Error Handling', () => {
|
|
||||||
it('should handle extraction with no auth session', async () => {
|
|
||||||
const result = await extractor.extract({
|
|
||||||
orderNumbers: ['ORDER1']
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(result.errors.length).toBeGreaterThan(0)
|
|
||||||
expect(result.downloadedFiles).toHaveLength(0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
195
tests/unit/ipc/cleaner-handler.test.ts
Normal file
195
tests/unit/ipc/cleaner-handler.test.ts
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||||
|
import { IPC_CHANNELS } from '../../../src/shared/ipc-channels'
|
||||||
|
|
||||||
|
// Mock logger to prevent real winston initialization and console noise
|
||||||
|
vi.mock('../../../src/main/services/logger', () => ({
|
||||||
|
createLogger: () => ({
|
||||||
|
info: vi.fn(),
|
||||||
|
warn: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
debug: vi.fn()
|
||||||
|
}),
|
||||||
|
logError: vi.fn()
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('../../../src/main/services/logger/error-utils', () => ({
|
||||||
|
serializeError: (err: any) => err,
|
||||||
|
sanitizeError: (err: any) => err
|
||||||
|
}))
|
||||||
|
|
||||||
|
// In-memory storage for registered IPC handlers
|
||||||
|
const registeredHandlers: Map<string, (...args: any[]) => any> = new Map()
|
||||||
|
|
||||||
|
// Mock Electron's ipcMain to capture registered handlers
|
||||||
|
vi.mock('electron', () => {
|
||||||
|
return {
|
||||||
|
app: {
|
||||||
|
isPackaged: false,
|
||||||
|
getVersion: () => '1.0.0-test'
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
handle: (channel: string, listener: any) => {
|
||||||
|
registeredHandlers.set(channel, listener)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Mock CleanerApplicationService to isolate IPC layer
|
||||||
|
vi.doMock('../../../src/main/services/cleaner/cleaner-application-service', () => {
|
||||||
|
return {
|
||||||
|
CleanerApplicationService: class {
|
||||||
|
async runCleaner(_eventSender: any, input: any) {
|
||||||
|
const count = input?.orderNumbers?.length ?? 0
|
||||||
|
return {
|
||||||
|
ordersProcessed: count,
|
||||||
|
materialsDeleted: count,
|
||||||
|
materialsSkipped: 0,
|
||||||
|
errors: [],
|
||||||
|
details: [],
|
||||||
|
retriedOrders: 0,
|
||||||
|
successfulRetries: 0
|
||||||
|
} as any
|
||||||
|
}
|
||||||
|
async exportResults(_input: any) {
|
||||||
|
return { success: true, filePath: '/tmp/results.txt' } as any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Load IPC handler module after mocks are in place
|
||||||
|
describe('Cleaner IPC Handler', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
registeredHandlers.clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should register and handle cleaner:execute (CLEANER_RUN) IPC call', async () => {
|
||||||
|
const mod = await import('../../../src/main/ipc/cleaner-handler')
|
||||||
|
mod.registerCleanerHandlers()
|
||||||
|
|
||||||
|
const handler = registeredHandlers.get(IPC_CHANNELS.CLEANER_RUN)
|
||||||
|
expect(handler).toBeDefined()
|
||||||
|
expect(typeof handler).toBe('function')
|
||||||
|
|
||||||
|
const event: any = { sender: { id: 'renderer-1' } }
|
||||||
|
const input: any = {
|
||||||
|
orderNumbers: ['SC1', 'SC2'],
|
||||||
|
materialCodes: [],
|
||||||
|
dryRun: false,
|
||||||
|
queryBatchSize: 100,
|
||||||
|
processConcurrency: 1,
|
||||||
|
onProgress: vi.fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await (handler as any)(event, input)
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
expect(result.data.ordersProcessed).toBe(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle cleaner:run with dryRun true', async () => {
|
||||||
|
const mod = await import('../../../src/main/ipc/cleaner-handler')
|
||||||
|
mod.registerCleanerHandlers()
|
||||||
|
|
||||||
|
const handler = registeredHandlers.get(IPC_CHANNELS.CLEANER_RUN)
|
||||||
|
expect(handler).toBeDefined()
|
||||||
|
|
||||||
|
const event: any = { sender: { id: 'renderer-2' } }
|
||||||
|
const input: any = {
|
||||||
|
orderNumbers: ['SC1'],
|
||||||
|
materialCodes: [],
|
||||||
|
dryRun: true,
|
||||||
|
queryBatchSize: 50,
|
||||||
|
processConcurrency: 1,
|
||||||
|
onProgress: vi.fn()
|
||||||
|
}
|
||||||
|
const result = await (handler as any)(event, input)
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
expect(result.data.ordersProcessed).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return { success: false } when runCleaner throws', async () => {
|
||||||
|
vi.resetModules()
|
||||||
|
vi.doMock('../../../src/main/services/cleaner/cleaner-application-service', () => {
|
||||||
|
return {
|
||||||
|
CleanerApplicationService: class {
|
||||||
|
async runCleaner() {
|
||||||
|
throw new Error('boom')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const mod = await import('../../../src/main/ipc/cleaner-handler')
|
||||||
|
mod.registerCleanerHandlers()
|
||||||
|
|
||||||
|
const handler = registeredHandlers.get(IPC_CHANNELS.CLEANER_RUN)
|
||||||
|
expect(handler).toBeDefined()
|
||||||
|
|
||||||
|
const event: any = { sender: { id: 'renderer-3' } }
|
||||||
|
const input: any = {
|
||||||
|
orderNumbers: ['SC1'],
|
||||||
|
materialCodes: [],
|
||||||
|
dryRun: false,
|
||||||
|
queryBatchSize: 20,
|
||||||
|
processConcurrency: 1,
|
||||||
|
onProgress: vi.fn()
|
||||||
|
}
|
||||||
|
const result = await (handler as any)(event, input)
|
||||||
|
expect(result.success).toBe(false)
|
||||||
|
expect(result.error).toBe('boom')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should register and handle cleaner:exportResults (CLEANER_EXPORT_RESULTS) IPC call', async () => {
|
||||||
|
vi.resetModules()
|
||||||
|
vi.doMock('../../../src/main/services/cleaner/cleaner-application-service', () => {
|
||||||
|
return {
|
||||||
|
CleanerApplicationService: class {
|
||||||
|
async exportResults(items: any[]) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
filePath: '/tmp/exported.xlsx',
|
||||||
|
recordCount: items.length
|
||||||
|
} as any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const mod = await import('../../../src/main/ipc/cleaner-handler')
|
||||||
|
mod.registerCleanerHandlers()
|
||||||
|
|
||||||
|
const handler = registeredHandlers.get(IPC_CHANNELS.CLEANER_EXPORT_RESULTS)
|
||||||
|
expect(handler).toBeDefined()
|
||||||
|
expect(typeof handler).toBe('function')
|
||||||
|
|
||||||
|
const event: any = { sender: { id: 'renderer-4' } }
|
||||||
|
const items = [
|
||||||
|
{ materialCode: 'M1', materialName: 'Mat A' },
|
||||||
|
{ materialCode: 'M2', materialName: 'Mat B' }
|
||||||
|
]
|
||||||
|
|
||||||
|
const result = await (handler as any)(event, items)
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
expect(result.data.recordCount).toBe(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return { success: false } when exportResults throws', async () => {
|
||||||
|
vi.resetModules()
|
||||||
|
vi.doMock('../../../src/main/services/cleaner/cleaner-application-service', () => {
|
||||||
|
return {
|
||||||
|
CleanerApplicationService: class {
|
||||||
|
async exportResults() {
|
||||||
|
throw new Error('export failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const mod = await import('../../../src/main/ipc/cleaner-handler')
|
||||||
|
mod.registerCleanerHandlers()
|
||||||
|
|
||||||
|
const handler = registeredHandlers.get(IPC_CHANNELS.CLEANER_EXPORT_RESULTS)
|
||||||
|
const event: any = { sender: { id: 'renderer-5' } }
|
||||||
|
const result = await (handler as any)(event, [])
|
||||||
|
expect(result.success).toBe(false)
|
||||||
|
expect(result.error).toBe('export failed')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -2,26 +2,28 @@ import { describe, it, expect } from 'vitest'
|
|||||||
import { ERP_LOCATORS } from '../../src/main/services/erp/locators'
|
import { ERP_LOCATORS } from '../../src/main/services/erp/locators'
|
||||||
|
|
||||||
describe('ERP Locators', () => {
|
describe('ERP Locators', () => {
|
||||||
it('should have login page locators defined', () => {
|
it('should have correct login page selectors', () => {
|
||||||
expect(ERP_LOCATORS.login.usernameInput).toBeDefined()
|
expect(ERP_LOCATORS.login.usernameInput).toBe('#username')
|
||||||
expect(ERP_LOCATORS.login.passwordInput).toBeDefined()
|
expect(ERP_LOCATORS.login.passwordInput).toBe('#password')
|
||||||
expect(ERP_LOCATORS.login.submitButton).toBeDefined()
|
expect(ERP_LOCATORS.login.submitButton).toBe('button[type="submit"]')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have main frame locator', () => {
|
it('should have correct main frame selectors', () => {
|
||||||
expect(ERP_LOCATORS.main.mainIframe).toBeDefined()
|
expect(ERP_LOCATORS.main.mainIframe).toBe('#mainiframe')
|
||||||
|
expect(ERP_LOCATORS.main.forwardFrame).toBe('#forwardFrame')
|
||||||
|
expect(ERP_LOCATORS.main.loadingText).toBe('加载中')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have extractor page locators', () => {
|
it('should have correct extractor page selectors', () => {
|
||||||
expect(ERP_LOCATORS.extractor.orderNumberInputRole).toBeDefined()
|
expect(ERP_LOCATORS.extractor.orderNumberInputRole).toBe('来源生产订单号')
|
||||||
expect(ERP_LOCATORS.extractor.queryButton).toBeDefined()
|
expect(ERP_LOCATORS.extractor.queryButton).toBe('.search-component-searchBtn')
|
||||||
expect(ERP_LOCATORS.extractor.exportButton).toBeDefined()
|
expect(ERP_LOCATORS.extractor.exportButton).toBe('internal:has-text="输出"')
|
||||||
expect(ERP_LOCATORS.extractor.confirmButton).toBeDefined()
|
expect(ERP_LOCATORS.extractor.confirmButton).toBe('internal:has-text="确定(Y)"')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have cleaner page locators', () => {
|
it('should have correct cleaner page selectors', () => {
|
||||||
expect(ERP_LOCATORS.cleaner.orderNumberInput).toBeDefined()
|
expect(ERP_LOCATORS.cleaner.orderNumberInput).toBe('input[name="orderNumber"]')
|
||||||
expect(ERP_LOCATORS.cleaner.materialGrid).toBeDefined()
|
expect(ERP_LOCATORS.cleaner.materialGrid).toBe('table.material-grid tbody tr')
|
||||||
expect(ERP_LOCATORS.cleaner.saveButton).toBeDefined()
|
expect(ERP_LOCATORS.cleaner.saveButton).toBe('button:has-text("保存")')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Logger Integration Tests - RequestContext Integration
|
* Logger Integration Tests - RequestContext Integration
|
||||||
* Verifies RequestContext is properly integrated with Logger
|
* Verifies RequestContext functions produce correct behavior, not just exports.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||||
@@ -9,50 +9,72 @@ import path from 'path'
|
|||||||
import { getLogDir } from '../../src/main/services/logger/shared'
|
import { getLogDir } from '../../src/main/services/logger/shared'
|
||||||
|
|
||||||
describe('Logger RequestContext Integration', () => {
|
describe('Logger RequestContext Integration', () => {
|
||||||
it('should export run from request-context', async () => {
|
it('should generate unique request IDs inside run()', async () => {
|
||||||
const { run } = await import('../../src/main/services/logger/index')
|
const { run, getRequestId } = await import('../../src/main/services/logger/index')
|
||||||
expect(run).toBeDefined()
|
|
||||||
expect(typeof run).toBe('function')
|
const outerId = await run(async () => getRequestId())
|
||||||
|
const innerId = await run(async () => getRequestId())
|
||||||
|
|
||||||
|
expect(outerId).toBeTruthy()
|
||||||
|
expect(innerId).toBeTruthy()
|
||||||
|
expect(outerId).not.toBe(innerId)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export getRequestId from request-context', async () => {
|
it('should return undefined for getRequestId() outside run()', async () => {
|
||||||
const { getRequestId } = await import('../../src/main/services/logger/index')
|
const { getRequestId } = await import('../../src/main/services/logger/index')
|
||||||
expect(getRequestId).toBeDefined()
|
|
||||||
expect(typeof getRequestId).toBe('function')
|
expect(getRequestId()).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export getContext from request-context', async () => {
|
it('should propagate context through run()', async () => {
|
||||||
const { getContext } = await import('../../src/main/services/logger/index')
|
const { run, getContext } = await import('../../src/main/services/logger/index')
|
||||||
expect(getContext).toBeDefined()
|
|
||||||
expect(typeof getContext).toBe('function')
|
const context = await run(async () => getContext(), {
|
||||||
|
userId: 'user-123',
|
||||||
|
operation: 'test-op'
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(context).toBeDefined()
|
||||||
|
expect(context!.userId).toBe('user-123')
|
||||||
|
expect(context!.operation).toBe('test-op')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export withContext from request-context', async () => {
|
it('should provide request ID inside withRequestContext()', async () => {
|
||||||
const { withContext } = await import('../../src/main/services/logger/index')
|
const { withRequestContext, getRequestId } =
|
||||||
expect(withContext).toBeDefined()
|
await import('../../src/main/services/logger/index')
|
||||||
expect(typeof withContext).toBe('function')
|
|
||||||
|
const requestId = await withRequestContext(async () => getRequestId(), {
|
||||||
|
userId: 'user-abc',
|
||||||
|
operation: 'extract'
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(requestId).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export withRequestContext wrapper', async () => {
|
it('should inject context into withRequestContext()', async () => {
|
||||||
const { withRequestContext } = await import('../../src/main/services/logger/index')
|
const { withRequestContext, getContext } = await import('../../src/main/services/logger/index')
|
||||||
expect(withRequestContext).toBeDefined()
|
|
||||||
expect(typeof withRequestContext).toBe('function')
|
const ctx = await withRequestContext(async () => getContext(), {
|
||||||
|
userId: 'admin',
|
||||||
|
operation: 'clean'
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(ctx).toBeDefined()
|
||||||
|
expect(ctx!.userId).toBe('admin')
|
||||||
|
expect(ctx!.operation).toBe('clean')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export createLogger', async () => {
|
it('should create a child logger that carries context metadata', async () => {
|
||||||
const { createLogger } = await import('../../src/main/services/logger/index')
|
const { createLogger } = await import('../../src/main/services/logger/index')
|
||||||
expect(createLogger).toBeDefined()
|
const logger = createLogger('MyModule')
|
||||||
expect(typeof createLogger).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have all exports available from LoggerContext type', async () => {
|
logger.info('hello', { key: 'value' })
|
||||||
const loggerModule = await import('../../src/main/services/logger/index')
|
|
||||||
expect(loggerModule.run).toBeDefined()
|
// Verify the logger is functional — it has standard log methods that accept calls
|
||||||
expect(loggerModule.getRequestId).toBeDefined()
|
expect(typeof logger.info).toBe('function')
|
||||||
expect(loggerModule.getContext).toBeDefined()
|
expect(typeof logger.error).toBe('function')
|
||||||
expect(loggerModule.withContext).toBeDefined()
|
expect(typeof logger.warn).toBe('function')
|
||||||
expect(loggerModule.withRequestContext).toBeDefined()
|
expect(typeof logger.debug).toBe('function')
|
||||||
expect(loggerModule.createLogger).toBeDefined()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,298 +1,154 @@
|
|||||||
/**
|
/**
|
||||||
* Logger Unit Tests - Enhanced for Configuration Loading
|
* Logger Unit Tests
|
||||||
*
|
*
|
||||||
* Tests logger creation, configuration, and integration with ConfigManager
|
* Tests logger creation, log output content, level filtering,
|
||||||
|
* and setLogLevel behavior.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
|
||||||
// Track winston calls
|
// Track logger calls
|
||||||
interface WinstonCall {
|
interface LoggerCall {
|
||||||
level: string
|
level: string
|
||||||
message?: string
|
message?: string
|
||||||
meta?: Record<string, unknown>
|
meta?: Record<string, unknown>
|
||||||
}
|
}
|
||||||
const winstonCalls: WinstonCall[] = []
|
const loggerCalls: LoggerCall[] = []
|
||||||
|
|
||||||
// ============================================
|
// Mock the entire logger module for complete control
|
||||||
// Winston Format Mock - Callable Object Pattern
|
vi.mock('../../src/main/services/logger', () => {
|
||||||
// ============================================
|
const createLoggerMethods = () => ({
|
||||||
// Supports: format(), format.combine(), format.printf(),
|
info: vi.fn((message, meta) => {
|
||||||
// AND IIFE pattern: format((info) => info)()
|
loggerCalls.push({ level: 'info', message, meta })
|
||||||
// ============================================
|
}),
|
||||||
function createFormatFn() {
|
error: vi.fn((message, meta) => {
|
||||||
// The format FUNCTION itself - callable with ()
|
loggerCalls.push({ level: 'error', message, meta })
|
||||||
const formatCallable = vi.fn((callback?: Function) => {
|
}),
|
||||||
if (callback) {
|
warn: vi.fn((message, meta) => {
|
||||||
// Return a new callable format when callback is provided
|
loggerCalls.push({ level: 'warn', message, meta })
|
||||||
// This simulates: format((info) => { ... })() where () calls the returned function
|
}),
|
||||||
const transform = vi.fn() as any
|
debug: vi.fn((message, meta) => {
|
||||||
transform.combine = vi.fn(() => formatCallable)
|
loggerCalls.push({ level: 'debug', message, meta })
|
||||||
transform.timestamp = vi.fn(() => formatCallable)
|
}),
|
||||||
transform.colorize = vi.fn(() => formatCallable)
|
verbose: vi.fn((message, meta) => {
|
||||||
transform.json = vi.fn(() => formatCallable)
|
loggerCalls.push({ level: 'verbose', message, meta })
|
||||||
transform.simple = vi.fn(() => formatCallable)
|
})
|
||||||
transform.pretty = vi.fn(() => formatCallable)
|
})
|
||||||
transform.label = vi.fn(() => formatCallable)
|
|
||||||
transform.errors = vi.fn(() => formatCallable)
|
|
||||||
transform.metadata = vi.fn(() => formatCallable)
|
|
||||||
transform.cli = vi.fn(() => formatCallable)
|
|
||||||
// When called as transform(info), pass through the callback
|
|
||||||
// Handle undefined/null info gracefully
|
|
||||||
transform.mockImplementation((info: any) => {
|
|
||||||
// During initialization, logger may call with undefined - skip in that case
|
|
||||||
if (!info || typeof info !== 'object') {
|
|
||||||
info = { level: 'info', message: '', timestamp: new Date().toISOString() }
|
|
||||||
}
|
|
||||||
return callback(info)
|
|
||||||
})
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
// Called without callback - return formatCallable for chaining
|
|
||||||
return formatCallable
|
|
||||||
}) as any
|
|
||||||
|
|
||||||
// Add top-level chainable methods
|
const rootLogger = {
|
||||||
formatCallable.combine = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.timestamp = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.colorize = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.printf = vi.fn((cb: Function) => cb)
|
|
||||||
formatCallable.json = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.simple = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.pretty = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.label = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.errors = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.metadata = vi.fn(() => formatCallable)
|
|
||||||
formatCallable.cli = vi.fn(() => formatCallable)
|
|
||||||
|
|
||||||
return formatCallable
|
|
||||||
}
|
|
||||||
|
|
||||||
const format = createFormatFn()
|
|
||||||
|
|
||||||
// Mock winston since we don't need actual file logging in tests
|
|
||||||
vi.mock('winston', () => {
|
|
||||||
const createLoggerInstance = {
|
|
||||||
level: 'info',
|
level: 'info',
|
||||||
add: vi.fn(),
|
add: vi.fn(),
|
||||||
remove: vi.fn(),
|
remove: vi.fn(),
|
||||||
clear: vi.fn(),
|
clear: vi.fn(),
|
||||||
child: vi.fn(function (this: any, metadata: Record<string, unknown>) {
|
...createLoggerMethods(),
|
||||||
return {
|
child: vi.fn((metadata: Record<string, unknown>) => ({
|
||||||
...this,
|
level: 'info',
|
||||||
info: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
...createLoggerMethods(),
|
||||||
winstonCalls.push({ level: 'info', message, meta: { ...metadata, ...meta } })
|
// Override methods to include child metadata
|
||||||
}),
|
info: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
||||||
error: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
loggerCalls.push({ level: 'info', message, meta: { ...metadata, ...meta } })
|
||||||
winstonCalls.push({ level: 'error', message, meta: { ...metadata, ...meta } })
|
}),
|
||||||
}),
|
error: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
||||||
warn: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
loggerCalls.push({ level: 'error', message, meta: { ...metadata, ...meta } })
|
||||||
winstonCalls.push({ level: 'warn', message, meta: { ...metadata, ...meta } })
|
}),
|
||||||
}),
|
warn: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
||||||
debug: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
loggerCalls.push({ level: 'warn', message, meta: { ...metadata, ...meta } })
|
||||||
winstonCalls.push({ level: 'debug', message, meta: { ...metadata, ...meta } })
|
}),
|
||||||
})
|
debug: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
||||||
}
|
loggerCalls.push({ level: 'debug', message, meta: { ...metadata, ...meta } })
|
||||||
}),
|
}),
|
||||||
info: vi.fn((message, meta) => {
|
verbose: vi.fn((message: string, meta?: Record<string, unknown>) => {
|
||||||
winstonCalls.push({ level: 'info', message, meta })
|
loggerCalls.push({ level: 'verbose', message, meta: { ...metadata, ...meta } })
|
||||||
}),
|
})
|
||||||
error: vi.fn((message, meta) => {
|
}))
|
||||||
winstonCalls.push({ level: 'error', message, meta })
|
|
||||||
}),
|
|
||||||
warn: vi.fn((message, meta) => {
|
|
||||||
winstonCalls.push({ level: 'warn', message, meta })
|
|
||||||
}),
|
|
||||||
debug: vi.fn((message, meta) => {
|
|
||||||
winstonCalls.push({ level: 'debug', message, meta })
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
default: {
|
default: rootLogger,
|
||||||
createLogger: vi.fn(() => createLoggerInstance),
|
createLogger: vi.fn((context: string) => rootLogger.child({ context })),
|
||||||
format,
|
setLogLevel: vi.fn((level: string) => {
|
||||||
transports: {
|
rootLogger.level = level
|
||||||
Console: vi.fn(function Console(this: any, options?: any) {
|
}),
|
||||||
this.level = options?.level || 'info'
|
applyLoggingConfig: vi.fn(),
|
||||||
}),
|
withRequestContext: vi.fn()
|
||||||
DailyRotateFile: vi.fn(function DailyRotateFile(this: any, options?: any) {
|
|
||||||
this.options = options
|
|
||||||
}),
|
|
||||||
File: vi.fn(),
|
|
||||||
Http: vi.fn()
|
|
||||||
},
|
|
||||||
addColors: vi.fn()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
vi.mock('winston-daily-rotate-file', () => ({
|
|
||||||
default: vi.fn() as any
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Note: electron mock is now in tests/setup.ts (global)
|
|
||||||
// This local mock is removed to avoid conflicts
|
|
||||||
|
|
||||||
describe('Logger', () => {
|
describe('Logger', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
winstonCalls.length = 0
|
loggerCalls.length = 0
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
it('should create a child logger that logs with context metadata', async () => {
|
||||||
vi.resetModules()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should create a logger with context', async () => {
|
|
||||||
const { createLogger } = await import('../../src/main/services/logger')
|
const { createLogger } = await import('../../src/main/services/logger')
|
||||||
const logger = createLogger('TestContext')
|
const logger = createLogger('TestContext')
|
||||||
|
|
||||||
expect(logger).toBeDefined()
|
logger.info('Hello world', { extraKey: 'extraValue' })
|
||||||
// Logger should have logging methods
|
|
||||||
expect(logger.info || logger.debug || logger.warn || logger.error).toBeDefined()
|
expect(loggerCalls).toHaveLength(1)
|
||||||
|
expect(loggerCalls[0].level).toBe('info')
|
||||||
|
expect(loggerCalls[0].message).toBe('Hello world')
|
||||||
|
expect(loggerCalls[0].meta?.context).toBe('TestContext')
|
||||||
|
expect(loggerCalls[0].meta?.extraKey).toBe('extraValue')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have all log methods', async () => {
|
it('should log at all severity levels with correct content', async () => {
|
||||||
const { createLogger } = await import('../../src/main/services/logger')
|
const { createLogger } = await import('../../src/main/services/logger')
|
||||||
const logger = createLogger('TestContext')
|
const logger = createLogger('LevelTest')
|
||||||
|
|
||||||
expect(typeof logger.info).toBe('function')
|
logger.debug('debug msg', { key: 'd' })
|
||||||
expect(typeof logger.error).toBe('function')
|
logger.info('info msg', { key: 'i' })
|
||||||
expect(typeof logger.warn).toBe('function')
|
logger.warn('warn msg', { key: 'w' })
|
||||||
expect(typeof logger.debug).toBe('function')
|
logger.error('error msg', { key: 'e' })
|
||||||
|
|
||||||
|
expect(loggerCalls).toHaveLength(4)
|
||||||
|
const levels = loggerCalls.map((c) => c.level)
|
||||||
|
expect(levels).toEqual(['debug', 'info', 'warn', 'error'])
|
||||||
|
|
||||||
|
expect(loggerCalls[0].message).toBe('debug msg')
|
||||||
|
expect(loggerCalls[1].message).toBe('info msg')
|
||||||
|
expect(loggerCalls[2].message).toBe('warn msg')
|
||||||
|
expect(loggerCalls[3].message).toBe('error msg')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should export default logger', async () => {
|
it('should produce separate child loggers with independent context', async () => {
|
||||||
const logger = await import('../../src/main/services/logger')
|
|
||||||
expect(logger.default).toBeDefined()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should export setLogLevel function', async () => {
|
|
||||||
const { setLogLevel } = await import('../../src/main/services/logger')
|
|
||||||
expect(setLogLevel).toBeDefined()
|
|
||||||
expect(typeof setLogLevel).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should create child logger with context metadata', async () => {
|
|
||||||
const { createLogger } = await import('../../src/main/services/logger')
|
const { createLogger } = await import('../../src/main/services/logger')
|
||||||
const logger = createLogger('MyModule')
|
const loggerA = createLogger('ModuleA')
|
||||||
|
const loggerB = createLogger('ModuleB')
|
||||||
|
|
||||||
logger.info('Test message')
|
loggerA.info('from A')
|
||||||
|
loggerB.warn('from B')
|
||||||
|
|
||||||
// Verify logger was created and called
|
expect(loggerCalls).toHaveLength(2)
|
||||||
expect(logger.info).toHaveBeenCalled()
|
expect(loggerCalls[0].meta?.context).toBe('ModuleA')
|
||||||
})
|
expect(loggerCalls[0].message).toBe('from A')
|
||||||
|
expect(loggerCalls[1].meta?.context).toBe('ModuleB')
|
||||||
it('should log at different levels with metadata', async () => {
|
expect(loggerCalls[1].message).toBe('from B')
|
||||||
const { createLogger } = await import('../../src/main/services/logger')
|
|
||||||
const logger = createLogger('TestContext')
|
|
||||||
|
|
||||||
logger.debug('Debug message', { debugKey: 'debugValue' })
|
|
||||||
logger.info('Info message', { infoKey: 'infoValue' })
|
|
||||||
logger.warn('Warning message', { warnKey: 'warnValue' })
|
|
||||||
logger.error('Error message', { errorKey: 'errorValue' })
|
|
||||||
|
|
||||||
expect(logger.debug).toHaveBeenCalled()
|
|
||||||
expect(logger.info).toHaveBeenCalled()
|
|
||||||
expect(logger.warn).toHaveBeenCalled()
|
|
||||||
expect(logger.error).toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Logger Configuration Loading', () => {
|
describe('setLogLevel', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
winstonCalls.length = 0
|
loggerCalls.length = 0
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
it('should change the root logger level', async () => {
|
||||||
vi.resetModules()
|
const loggerModule = await import('../../src/main/services/logger')
|
||||||
})
|
|
||||||
|
|
||||||
it('should load ConfigManager class', async () => {
|
// Default export is the root logger
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
const rootLogger = loggerModule.default
|
||||||
expect(ConfigManager).toBeDefined()
|
|
||||||
expect(typeof ConfigManager.getInstance).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should have logging configuration methods', async () => {
|
// Default level is 'info'
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
expect(rootLogger.level).toBe('info')
|
||||||
|
|
||||||
const manager = ConfigManager.getInstance()
|
// Change to debug
|
||||||
|
loggerModule.setLogLevel('debug')
|
||||||
|
expect(rootLogger.level).toBe('debug')
|
||||||
|
|
||||||
expect(manager.getLoggingConfig).toBeDefined()
|
// Change to error
|
||||||
expect(typeof manager.getLoggingConfig).toBe('function')
|
loggerModule.setLogLevel('error')
|
||||||
expect(manager.getDefaultConfig).toBeDefined()
|
expect(rootLogger.level).toBe('error')
|
||||||
expect(typeof manager.getDefaultConfig).toBe('function')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should return default logging config structure', async () => {
|
|
||||||
const { ConfigManager } = await import('../../src/main/services/config/config-manager')
|
|
||||||
|
|
||||||
const manager = ConfigManager.getInstance()
|
|
||||||
const defaultConfig = manager.getDefaultConfig()
|
|
||||||
|
|
||||||
expect(defaultConfig.logging).toBeDefined()
|
|
||||||
expect(defaultConfig.logging.level).toBeDefined()
|
|
||||||
expect(defaultConfig.logging.auditRetention).toBeDefined()
|
|
||||||
expect(defaultConfig.logging.appRetention).toBeDefined()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should validate logging level enum values', async () => {
|
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
|
||||||
|
|
||||||
// Test all valid log levels
|
|
||||||
const validLevels = ['error', 'warn', 'info', 'debug', 'verbose']
|
|
||||||
|
|
||||||
for (const level of validLevels) {
|
|
||||||
const result = loggingConfigSchema.safeParse({ level })
|
|
||||||
expect(result.success).toBe(true)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should reject invalid logging level', async () => {
|
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
|
||||||
|
|
||||||
const result = loggingConfigSchema.safeParse({ level: 'invalid_level' })
|
|
||||||
expect(result.success).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should validate audit retention range (1-365)', async () => {
|
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
|
||||||
|
|
||||||
// Valid values
|
|
||||||
expect(loggingConfigSchema.safeParse({ auditRetention: 1 }).success).toBe(true)
|
|
||||||
expect(loggingConfigSchema.safeParse({ auditRetention: 365 }).success).toBe(true)
|
|
||||||
expect(loggingConfigSchema.safeParse({ auditRetention: 30 }).success).toBe(true)
|
|
||||||
|
|
||||||
// Invalid values
|
|
||||||
expect(loggingConfigSchema.safeParse({ auditRetention: 0 }).success).toBe(false)
|
|
||||||
expect(loggingConfigSchema.safeParse({ auditRetention: 366 }).success).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should validate app retention range (1-365)', async () => {
|
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
|
||||||
|
|
||||||
// Valid values
|
|
||||||
expect(loggingConfigSchema.safeParse({ appRetention: 1 }).success).toBe(true)
|
|
||||||
expect(loggingConfigSchema.safeParse({ appRetention: 365 }).success).toBe(true)
|
|
||||||
expect(loggingConfigSchema.safeParse({ appRetention: 14 }).success).toBe(true)
|
|
||||||
|
|
||||||
// Invalid values
|
|
||||||
expect(loggingConfigSchema.safeParse({ appRetention: 0 }).success).toBe(false)
|
|
||||||
expect(loggingConfigSchema.safeParse({ appRetention: 366 }).success).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should use default values when logging config is partial', async () => {
|
|
||||||
const { loggingConfigSchema } = await import('../../src/main/types/config.schema')
|
|
||||||
|
|
||||||
// Only provide level, should default others
|
|
||||||
const result = loggingConfigSchema.safeParse({ level: 'warn' })
|
|
||||||
expect(result.success).toBe(true)
|
|
||||||
if (result.success) {
|
|
||||||
expect(result.data.auditRetention).toBe(30) // default
|
|
||||||
expect(result.data.appRetention).toBe(14) // default
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,11 +1,51 @@
|
|||||||
/**
|
/**
|
||||||
* Unit tests for MySqlService
|
* Unit tests for MySqlService
|
||||||
* These tests do not require a MySQL instance
|
* Covers both unconnected state and connected-path operations using mocked mysql2/promise.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach } from 'vitest'
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||||
import { MySqlService } from '@services/database/mysql'
|
import { MySqlService } from '@services/database/mysql'
|
||||||
|
|
||||||
|
// ---- Hoisted mock functions ----
|
||||||
|
const {
|
||||||
|
mockCreateConnection,
|
||||||
|
mockPing,
|
||||||
|
mockExecute,
|
||||||
|
mockBeginTransaction,
|
||||||
|
mockCommit,
|
||||||
|
mockRollback,
|
||||||
|
mockEnd
|
||||||
|
} = vi.hoisted(() => ({
|
||||||
|
mockCreateConnection: vi.fn(),
|
||||||
|
mockPing: vi.fn(),
|
||||||
|
mockExecute: vi.fn(),
|
||||||
|
mockBeginTransaction: vi.fn(),
|
||||||
|
mockCommit: vi.fn(),
|
||||||
|
mockRollback: vi.fn(),
|
||||||
|
mockEnd: vi.fn()
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Mock mysql2/promise driver
|
||||||
|
vi.mock('mysql2/promise', () => ({
|
||||||
|
default: {
|
||||||
|
createConnection: mockCreateConnection
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Mock logger
|
||||||
|
vi.mock('@services/logger', () => ({
|
||||||
|
createLogger: () => ({
|
||||||
|
info: vi.fn(),
|
||||||
|
warn: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
debug: vi.fn()
|
||||||
|
}),
|
||||||
|
trackDuration: async <T>(fn: () => Promise<T>) => {
|
||||||
|
const result = await fn()
|
||||||
|
return { result }
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
const mockConfig = {
|
const mockConfig = {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
port: 3306,
|
port: 3306,
|
||||||
@@ -14,11 +54,30 @@ const mockConfig = {
|
|||||||
database: 'testdb'
|
database: 'testdb'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createMockConnection() {
|
||||||
|
return {
|
||||||
|
ping: mockPing,
|
||||||
|
execute: mockExecute,
|
||||||
|
beginTransaction: mockBeginTransaction,
|
||||||
|
commit: mockCommit,
|
||||||
|
rollback: mockRollback,
|
||||||
|
end: mockEnd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('MySqlService Unit Tests', () => {
|
describe('MySqlService Unit Tests', () => {
|
||||||
let service: MySqlService
|
let service: MySqlService
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
service = new MySqlService(mockConfig)
|
service = new MySqlService(mockConfig)
|
||||||
|
mockCreateConnection.mockResolvedValue(createMockConnection())
|
||||||
|
mockPing.mockResolvedValue(undefined)
|
||||||
|
mockExecute.mockResolvedValue([[], []])
|
||||||
|
mockBeginTransaction.mockResolvedValue(undefined)
|
||||||
|
mockCommit.mockResolvedValue(undefined)
|
||||||
|
mockRollback.mockResolvedValue(undefined)
|
||||||
|
mockEnd.mockResolvedValue(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('constructor', () => {
|
describe('constructor', () => {
|
||||||
@@ -35,15 +94,33 @@ describe('MySqlService Unit Tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('connect', () => {
|
describe('connect', () => {
|
||||||
it('should throw error with invalid credentials', async () => {
|
it('should throw error when connection fails', async () => {
|
||||||
// This tests error handling without needing a real server
|
mockCreateConnection.mockRejectedValue(new Error('connect ECONNREFUSED'))
|
||||||
const invalidConfig = {
|
await expect(service.connect()).rejects.toThrow('Failed to connect to MySQL')
|
||||||
...mockConfig,
|
})
|
||||||
host: 'invalid-host-that-does-not-exist'
|
|
||||||
}
|
|
||||||
const invalidService = new MySqlService(invalidConfig)
|
|
||||||
|
|
||||||
await expect(invalidService.connect()).rejects.toThrow('Failed to connect to MySQL')
|
it('should establish connection and ping server', async () => {
|
||||||
|
await service.connect()
|
||||||
|
expect(mockCreateConnection).toHaveBeenCalledWith({
|
||||||
|
host: 'localhost',
|
||||||
|
port: 3306,
|
||||||
|
user: 'test',
|
||||||
|
password: 'test',
|
||||||
|
database: 'testdb'
|
||||||
|
})
|
||||||
|
expect(mockPing).toHaveBeenCalled()
|
||||||
|
expect(service.isConnected()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw when already connected', async () => {
|
||||||
|
await service.connect()
|
||||||
|
await expect(service.connect()).rejects.toThrow('Already connected to MySQL')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw when ping fails after connection created', async () => {
|
||||||
|
mockPing.mockRejectedValue(new Error('ping failed'))
|
||||||
|
await expect(service.connect()).rejects.toThrow('Failed to connect to MySQL')
|
||||||
|
// Note: source sets connection before ping, so it remains non-null after ping failure
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -51,6 +128,54 @@ describe('MySqlService Unit Tests', () => {
|
|||||||
it('should throw error when not connected', async () => {
|
it('should throw error when not connected', async () => {
|
||||||
await expect(service.query('SELECT 1')).rejects.toThrow('Not connected to MySQL')
|
await expect(service.query('SELECT 1')).rejects.toThrow('Not connected to MySQL')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should execute SELECT and return rows with columns', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockExecute.mockResolvedValue([
|
||||||
|
[
|
||||||
|
{ id: 1, name: 'test' },
|
||||||
|
{ id: 2, name: 'foo' }
|
||||||
|
],
|
||||||
|
[{ name: 'id' }, { name: 'name' }]
|
||||||
|
])
|
||||||
|
|
||||||
|
const result = await service.query('SELECT id, name FROM users')
|
||||||
|
|
||||||
|
expect(mockExecute).toHaveBeenCalledWith('SELECT id, name FROM users', undefined)
|
||||||
|
expect(result.rows).toEqual([
|
||||||
|
{ id: 1, name: 'test' },
|
||||||
|
{ id: 2, name: 'foo' }
|
||||||
|
])
|
||||||
|
expect(result.columns).toEqual(['id', 'name'])
|
||||||
|
expect(result.rowCount).toBe(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should execute INSERT/UPDATE and return affected rows', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockExecute.mockResolvedValue([{ affectedRows: 3, changedRows: 2 }, []])
|
||||||
|
|
||||||
|
const result = await service.query('UPDATE users SET active = ?', [true])
|
||||||
|
|
||||||
|
expect(mockExecute).toHaveBeenCalledWith('UPDATE users SET active = ?', [true])
|
||||||
|
expect(result.rows).toEqual([])
|
||||||
|
expect(result.rowCount).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use changedRows when affectedRows is zero', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockExecute.mockResolvedValue([{ affectedRows: 0, changedRows: 5 }, []])
|
||||||
|
|
||||||
|
const result = await service.query('UPDATE users SET x = 1')
|
||||||
|
|
||||||
|
expect(result.rowCount).toBe(5)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should wrap query errors with context', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockExecute.mockRejectedValue(new Error('syntax error'))
|
||||||
|
|
||||||
|
await expect(service.query('INVALID SQL')).rejects.toThrow('MySQL query failed')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('transaction', () => {
|
describe('transaction', () => {
|
||||||
@@ -59,11 +184,56 @@ describe('MySqlService Unit Tests', () => {
|
|||||||
'Not connected to MySQL'
|
'Not connected to MySQL'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should execute all queries and commit', async () => {
|
||||||
|
await service.connect()
|
||||||
|
|
||||||
|
await service.transaction([
|
||||||
|
{ sql: 'INSERT INTO t VALUES (?)', params: [1] },
|
||||||
|
{ sql: 'UPDATE t SET x = ?' }
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(mockBeginTransaction).toHaveBeenCalled()
|
||||||
|
expect(mockExecute).toHaveBeenCalledTimes(2)
|
||||||
|
expect(mockExecute).toHaveBeenNthCalledWith(1, 'INSERT INTO t VALUES (?)', [1])
|
||||||
|
expect(mockExecute).toHaveBeenNthCalledWith(2, 'UPDATE t SET x = ?', undefined)
|
||||||
|
expect(mockCommit).toHaveBeenCalled()
|
||||||
|
expect(mockRollback).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should rollback on query failure', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockExecute.mockRejectedValueOnce(new Error('constraint violation'))
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
service.transaction([{ sql: 'INSERT INTO t VALUES (?)', params: [1] }])
|
||||||
|
).rejects.toThrow('MySQL transaction failed')
|
||||||
|
|
||||||
|
expect(mockRollback).toHaveBeenCalled()
|
||||||
|
expect(mockCommit).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('disconnect', () => {
|
describe('disconnect', () => {
|
||||||
it('should resolve when not connected', async () => {
|
it('should resolve when not connected', async () => {
|
||||||
await expect(service.disconnect()).resolves.not.toThrow()
|
await expect(service.disconnect()).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should end connection and reset state', async () => {
|
||||||
|
await service.connect()
|
||||||
|
expect(service.isConnected()).toBe(true)
|
||||||
|
|
||||||
|
await service.disconnect()
|
||||||
|
|
||||||
|
expect(mockEnd).toHaveBeenCalled()
|
||||||
|
expect(service.isConnected()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should wrap disconnect errors', async () => {
|
||||||
|
await service.connect()
|
||||||
|
mockEnd.mockRejectedValue(new Error('connection lost'))
|
||||||
|
|
||||||
|
await expect(service.disconnect()).rejects.toThrow('Failed to disconnect from MySQL')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user