feat: implement Extractor service with download capability

Implement Task 3.1: Core Extractor Logic with TDD approach.

Changes:
- Add ExtractorService class with batch processing and download support
- Update ERP_LOCATORS with extractor-specific selectors from Python reference
- Add integration tests for single and multiple order extraction
- Add unit tests for batch creation logic
- Update existing tests to skip gracefully without ERP credentials

Features:
- Navigate to discrete material plan page with nested iframes
- Setup query interface (search icon, order query, limit settings)
- Batch download with configurable batch size (default: 100)
- Progress callback support for real-time updates
- Error handling for individual batch failures
- File download handling with proper wait strategies

Reference: playwrite/utils/discrete_material_plan_extractor.py

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-03-01 12:46:15 +08:00
parent f045d66b65
commit f6b19b50f3
7 changed files with 429 additions and 9 deletions

View File

@@ -0,0 +1,210 @@
import path from 'path';
import fs from 'fs/promises';
import { ERP_LOCATORS } from './locators';
import { ErpAuthService } from './erp-auth';
import type { ExtractorInput, ExtractorResult } from '../../types/extractor.types';
import type { ErpSession } from '../../types/erp.types';
/**
* ERP Data Extractor Service
* Downloads material plan data for given order numbers
*
* Reference: playwrite/utils/discrete_material_plan_extractor.py
*/
export class ExtractorService {
private authService: ErpAuthService;
private downloadDir: string;
constructor(authService: ErpAuthService, downloadDir = './downloads') {
this.authService = authService;
this.downloadDir = downloadDir;
// Ensure download directory exists
fs.mkdir(downloadDir, { recursive: true }).catch(() => {});
}
/**
* Extract data for given order numbers
*/
async extract(input: ExtractorInput): Promise<ExtractorResult> {
const result: ExtractorResult = {
downloadedFiles: [],
mergedFile: null,
recordCount: 0,
errors: [],
};
try {
const session = this.authService.getSession();
// Navigate to extractor page
const popupPage = await this.navigateToExtractorPage(session);
// Process orders in batches
const batchSize = input.batchSize || 100;
const batches = this.createBatches(input.orderNumbers, batchSize);
for (let i = 0; i < batches.length; i++) {
const batch = batches[i];
const progress = ((i + 1) / batches.length) * 100;
input.onProgress?.(`Processing batch ${i + 1}/${batches.length}`, progress);
try {
const filePath = await this.downloadBatch(session, popupPage, batch, i, batches.length);
result.downloadedFiles.push(filePath);
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
result.errors.push(`Batch ${i + 1}: ${message}`);
}
}
// TODO: Merge files (implement in separate task)
// result.mergedFile = await this.mergeFiles(result.downloadedFiles);
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
result.errors.push(`Extraction failed: ${message}`);
}
return result;
}
/**
* Navigate to extractor/query page
* Reference: Python extract() method lines 266-276
*/
private async navigateToExtractorPage(session: ErpSession): Promise<any> {
const { page } = session;
// Wait for main iframe
await page.waitForSelector(ERP_LOCATORS.main.mainIframe);
// Get main frame
const mainFrame = page.frameLocator(ERP_LOCATORS.main.mainIframe);
// Click icon to open menu (line 266)
await mainFrame.locator('i').first().click();
// Click discrete material plan menu item and expect popup (lines 267-271)
const popupPromise = page.waitForEvent('popup');
await mainFrame.getByTitle('离散备料计划维护', { exact: true }).first().click();
const popupPage = await popupPromise;
// Get nested frame structure (lines 273-276)
const forwardFrameLocator = popupPage.locator(ERP_LOCATORS.main.forwardFrame);
const innerFrameLocator = forwardFrameLocator.frameLocator(ERP_LOCATORS.main.innerIframe);
// Wait for inner iframe to be visible
await innerFrameLocator.waitFor({ state: 'visible', timeout: 15000 });
// Setup query interface (line 278)
await this.setupQueryInterface(innerFrameLocator);
return popupPage;
}
/**
* Setup query interface
* Reference: Python setup_query_interface() method lines 231-239
*/
private async setupQueryInterface(innerFrame: any): Promise<void> {
// Click search icon (line 233)
await innerFrame.locator(ERP_LOCATORS.menu.searchIcon).click();
// Click "订单号查询" menu item (line 234)
await innerFrame.locator(ERP_LOCATORS.menu.orderQuery).click();
// Click "全部" tab (line 235)
await innerFrame.getByRole('tab', { name: '全部' }).click();
// Set limit to 5000 (lines 237-239)
const inputBox = innerFrame.locator(ERP_LOCATORS.menu.selectInput);
await inputBox.fill('5000');
await inputBox.press('Enter');
}
/**
* Download a single batch of orders
* Reference: Python download_batch() method lines 133-175
*/
private async downloadBatch(
session: ErpSession,
popupPage: any,
orderNumbers: string[],
batchIndex: number,
totalBatches: number
): Promise<string> {
const forwardFrameLocator = popupPage.locator(ERP_LOCATORS.main.forwardFrame);
const workFrame = forwardFrameLocator.frameLocator(ERP_LOCATORS.main.innerIframe);
// Fill order numbers (lines 143-145)
const textbox = workFrame.getByRole('textbox', { name: ERP_LOCATORS.extractor.orderNumberInputRole });
await textbox.fill('');
await textbox.fill(orderNumbers.join(','));
// Click search button (line 147)
await workFrame.locator(ERP_LOCATORS.extractor.queryButton).click();
// Wait for loading (lines 148-153)
await this.waitForLoading(workFrame);
// Click first row checkbox (line 155)
await workFrame
.locator(ERP_LOCATORS.extractor.firstRowSelector)
.getByLabel('')
.click();
// Hover and click "更多" button (lines 156-157)
await workFrame.getByRole('button', { name: '更多' }).hover();
await workFrame.getByText('输出', { exact: true }).click();
// Set threshold (lines 159-164)
const thresholdBox = workFrame
.locator('div')
.filter({ hasText: /^行数阈值$/ })
.locator('input[type="text"]');
await thresholdBox.fill('300000');
// Setup download handler and click confirm (lines 166-172)
const downloadPath = path.join(
this.downloadDir,
`temp_batch_${batchIndex + 1}.xlsx`
);
const downloadPromise = popupPage.waitForEvent('download');
await workFrame.getByRole('button', { name: '确定(Y)' }).click();
const download = await downloadPromise;
await download.saveAs(downloadPath);
return downloadPath;
}
/**
* Wait for loading overlay to disappear
* Reference: Python lines 148-153
*/
private async waitForLoading(workFrame: any): Promise<void> {
const loadingLocator = workFrame.locator('div').filter({ hasText: ERP_LOCATORS.extractor.loadingText }).nth(1);
try {
await loadingLocator.waitFor({ state: 'visible', timeout: 3000 });
await loadingLocator.waitFor({ state: 'hidden', timeout: 0 });
} catch {
// Loading completed quickly or never appeared
}
}
/**
* Split array into batches
* Reference: Python group_order_ids() method lines 128-131
*/
private createBatches<T>(items: T[], batchSize: number): T[][] {
const batches: T[][] = [];
for (let i = 0; i < items.length; i += batchSize) {
batches.push(items.slice(i, i + batchSize));
}
return batches;
}
}

View File

@@ -13,19 +13,54 @@ export const ERP_LOCATORS = {
},
// Main Frame
// Reference: Nested iframe structure from Python code
main: {
// Main iframe on the page
mainIframe: '#mainiframe',
loadingOverlay: '.loading-overlay',
contentFrame: '#contentframe',
// Forward frame (nested inside main)
forwardFrame: '#forwardFrame',
// Inner iframe (inside forward frame)
innerIframe: '#mainiframe',
// Loading overlay text
loadingText: '加载中',
},
// Extractor (Data Export) Page
// Reference: playwrite/utils/discrete_material_plan_extractor.py
extractor: {
orderNumberInput: 'input[name="orderNumber"]',
queryButton: 'button:has-text("查询")',
exportButton: 'button:has-text("导出")',
// Textbox by role: get_by_role("textbox", name="来源生产订单号")
orderNumberInputRole: '来源生产订单号',
// Search button: .search-component-searchBtn
queryButton: '.search-component-searchBtn',
// Loading indicator: div with text "加载中"
loadingText: '加载中',
// First row selector (序号 row)
firstRowSelector: 'internal:role=row[name=/序号/i]',
// More button
moreButton: 'internal:has-text="更多"',
// Export button (输出)
exportButton: 'internal:has-text="输出"',
// Export dialog - threshold input
thresholdInputSelector: 'div:has-text(/^行数阈值$/) input[type="text"]',
// Confirm button
confirmButton: 'internal:has-text="确定(Y)"',
},
// Menu navigation
menu: {
// Search icon in search wrapper
searchIcon: '.search-name-wrapper .iconfont',
// Order number query menu item
orderQuery: 'internal:has-text="订单号查询"',
// "All" tab
allTab: 'internal:role=tab[name="全部"]',
// Select input for setting limits
selectInput: '#rc_select_0',
},
// Discrete material plan menu item
discreteMaterialPlan: 'internal:has-title="离散备料计划维护"',
// Cleaner (Material Delete) Page
cleaner: {
orderNumberInput: 'input[name="orderNumber"]',