Compare commits
14 Commits
NEW_BOM
...
DEV_YTHN-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c597a6cf75 | ||
|
|
1e995b5485 | ||
|
|
d4f8e77c66 | ||
|
|
7d59e489b1 | ||
|
|
658f8a7160 | ||
|
|
04099d25bc | ||
|
|
9b543ab466 | ||
|
|
36c00befa5 | ||
|
|
596a2d0ad2 | ||
|
|
7e2710da3f | ||
|
|
c119d2370b | ||
|
|
1747af046b | ||
|
|
787b3f56ab | ||
|
|
318d1c31f7 |
@@ -1,31 +0,0 @@
|
||||
name: NTFY Notification
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
notify:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Send to NTFY
|
||||
run: |
|
||||
FULL_REPO="${{ github.repository }}"
|
||||
# 从 owner/repo 格式中提取仓库名称
|
||||
REPO=$(echo "$FULL_REPO" | cut -d'/' -f2)
|
||||
SHA="${{ github.sha }}"
|
||||
MSG="${{ github.event.head_commit.message }}"
|
||||
AUTHOR="${{ github.actor }}"
|
||||
BRANCH="${{ github.ref_name }}"
|
||||
|
||||
# 将多行消息替换为空格,避免shell解析错误
|
||||
MSG=$(echo "$MSG" | tr '\n' ' ' | sed 's/"/\\"/g')
|
||||
|
||||
curl -d "🚀 $REPO
|
||||
New commit $SHA
|
||||
📝 Message
|
||||
$MSG
|
||||
👤 Author: $AUTHOR
|
||||
🌿 Branch: $BRANCH" \
|
||||
-H "Title: Git Push Notification" \
|
||||
-H "Priority: default" \
|
||||
-H "Tags: gitea,push,$REPO" \
|
||||
-H "Authorization: Bearer ${{ secrets.NTFY_TOKEN }}" \
|
||||
https://ntfy.server10086.icu/gitea
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -6,8 +6,11 @@ log
|
||||
*.spec
|
||||
temp
|
||||
|
||||
# Claude 临时文件
|
||||
# AI Agent 临时文件
|
||||
.claude/
|
||||
.sisyphus/
|
||||
|
||||
|
||||
tmpclaude-*
|
||||
*.log
|
||||
*workspace*
|
||||
@@ -15,3 +18,4 @@ tmpclaude-*
|
||||
data/
|
||||
*.xlsm
|
||||
*.xlsx
|
||||
|
||||
|
||||
131
AGENTS.md
Normal file
131
AGENTS.md
Normal file
@@ -0,0 +1,131 @@
|
||||
# AutoBOM Knowledge Base
|
||||
|
||||
**Generated:** 2026-03-11
|
||||
**Stack:** VBA (Excel) + Python automation
|
||||
|
||||
## OVERVIEW
|
||||
|
||||
VBA-based BOM extraction system that parses product model strings and matches components from a platform configuration list. Core workflow: parse model → extract conditions → match BOM items → export results.
|
||||
|
||||
## STRUCTURE
|
||||
|
||||
```
|
||||
AutoBOM/
|
||||
├── VBA/ # Core VBA modules
|
||||
│ ├── ClassModules/ # Data models + logic classes
|
||||
│ ├── Modules/ # Entry points + workflows
|
||||
│ └── DocumentModules/ # Sheet-specific code
|
||||
├── docs/ # Flowcharts + execution plans
|
||||
├── reference_docs/ # External product references
|
||||
└── *.xlsm/*.xlsx # Excel workbooks with embedded macros
|
||||
```
|
||||
|
||||
## WHERE TO LOOK
|
||||
|
||||
| Task | Location | Notes |
|
||||
|------|----------|-------|
|
||||
| Parse product model | `VBA/ClassModules/ProductModelParser.cls` | Extracts conditions from model string |
|
||||
| Match BOM items | `VBA/ClassModules/BomExtractor.cls` | Core matching logic with assembly hierarchy |
|
||||
| BOM data model | `VBA/ClassModules/BomItem.cls` | Row structure for platform configuration |
|
||||
| Condition evaluation | `VBA/ClassModules/ConditionEvaluator.cls` | Evaluates selection conditions |
|
||||
| Main workflow | `VBA/Modules/MainModule.bas` | `ProcessProductModels()` entry point |
|
||||
| BIP upload | `VBA/Modules/BIPUploadModule.bas` | Upload results to BIP system |
|
||||
| Component check | `VBA/Modules/ComponentInventoryCheckModule.bas` | Inventory verification |
|
||||
| Excel→Markdown | `.claude/skills/excel-to-markdown/scripts/excel_to_markdown.py` | Python utility |
|
||||
|
||||
## CODE MAP
|
||||
|
||||
### Core Classes
|
||||
|
||||
| Symbol | Type | Location | Role |
|
||||
|--------|------|----------|------|
|
||||
| `BomExtractor` | Class | `ClassModules/BomExtractor.cls` | Extracts matching BOM items based on conditions |
|
||||
| `BomItem` | Class | `ClassModules/BomItem.cls` | Data model for BOM row (11 fields) |
|
||||
| `ProductModelParser` | Class | `ClassModules/ProductModelParser.cls` | Parses model string like `Y-100-M203.316SS` |
|
||||
| `ConditionEvaluator` | Class | `ClassModules/ConditionEvaluator.cls` | Evaluates condition expressions |
|
||||
| `MainModule` | Module | `Modules/MainModule.bas` | Orchestrates end-to-end workflow |
|
||||
|
||||
### Key Functions
|
||||
|
||||
| Function | Location | Description |
|
||||
|----------|----------|-------------|
|
||||
| `ProcessProductModels()` | `MainModule.bas` | Main entry point |
|
||||
| `ExtractBom(conditions)` | `BomExtractor.cls` | Returns matched `Collection` of `BomItem` |
|
||||
| `Parse(modelString)` | `ProductModelParser.cls` | Extracts `azxs`, `bkxs`, `gclj`, `jycz`, `lcfw`, `fjgn` |
|
||||
| `Evaluate(condition, conditions)` | `ConditionEvaluator.cls` | Returns `Boolean` match result |
|
||||
|
||||
## CONVENTIONS
|
||||
|
||||
### VBA Structure
|
||||
- **ClassModules**: Domain logic classes (`*.cls`)
|
||||
- **Modules**: Procedural workflows (`*.bas`)
|
||||
- **DocumentModules**: Sheet-specific event handlers (`Sheet9.cls`)
|
||||
|
||||
### Model String Format
|
||||
```
|
||||
[Header]-[Spec1].[Spec2].[Spec3].[Spec4].[Spec5]|[Detail1]|[Detail2]
|
||||
Example: Y-100-M203.316SS.L100.N2
|
||||
```
|
||||
|
||||
### Condition Config
|
||||
```vba
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
||||
```
|
||||
|
||||
## ANTI-PATTERNS (THIS PROJECT)
|
||||
|
||||
- **DO NOT** modify row indexing in `BomExtractor.LoadBomData()` — starts at row 4 (header is row 3)
|
||||
- **DO NOT** change `BomItem` field order — hardcoded column mapping in `LoadFromRow()`
|
||||
- **DO NOT** remove `On Error Resume Next` in `BomItem.LoadFromRow()` — handles type conversion failures
|
||||
- **NEVER** skip assembly logic in `ApplyAssemblyLogic()` — handles parent/child component hierarchy
|
||||
- **NEVER** hardcode workbook names — use `ThisWorkbook` reference
|
||||
|
||||
## UNIQUE STYLES
|
||||
|
||||
### Assembly Logic (Parent/Child Override)
|
||||
If parent category matches (e.g., "部件") AND all children match (e.g., "接头", "弹性元件"), parent overrides children in output. Controlled by `ApplyAssemblyLogic()`.
|
||||
|
||||
### Batch Output Pattern
|
||||
Results collected in `Collection` → converted to 2D array → single `Range.Value` write for performance.
|
||||
|
||||
### Condition Expression Syntax
|
||||
```vba
|
||||
' Format: "field=value" or "field1=value1|field2=value2"
|
||||
' Example: "azxs=M|azxs=L" (vertical OR horizontal)
|
||||
```
|
||||
|
||||
## COMMANDS
|
||||
|
||||
### Python Virtual Environment
|
||||
```bash
|
||||
# Activate (Windows)
|
||||
.venv\Scripts\activate
|
||||
|
||||
# Run Excel→Markdown converter
|
||||
python .claude/skills/excel-to-markdown/scripts/excel_to_markdown.py input.xlsx -o output.md
|
||||
```
|
||||
|
||||
### Excel Macros
|
||||
```
|
||||
1. Open *.xlsm workbook
|
||||
2. Press Alt+F11 to open VBA editor
|
||||
3. Run: MainModule.ProcessProductModels
|
||||
```
|
||||
|
||||
## NOTES
|
||||
|
||||
### Workbook Requirements
|
||||
- Input sheet: `产品订单` (product orders)
|
||||
- BOM sheet: `平台配置清单` (platform configuration)
|
||||
- Output sheet: `BOM 提取结果` (auto-created)
|
||||
|
||||
### Git Configuration
|
||||
`.gitignore` excludes `*.xlsm`, `*.xlsx`, `*.png`, `.venv/`, `build/`, `dist/`. Binary Excel files not tracked.
|
||||
|
||||
### ConditionEvaluator Dependencies
|
||||
VBA `Scripting.Dictionary` required (Windows only). Not compatible with Mac Excel.
|
||||
|
||||
### Error Handling
|
||||
- Parse failures → `extractNote = "解析失败:..."`
|
||||
- No matches → `extractNote = "未匹配到任何物料"`
|
||||
- Multi-match → logged but outputs all (data quality flag)
|
||||
539
CLAUDE.md
539
CLAUDE.md
@@ -1,539 +0,0 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
AutoBOM is an Excel-based Bill of Materials (BOM) management system for pressure product manufacturing. The project includes TWO functional modules:
|
||||
|
||||
1. **BOM Configuration System** (VBA/) - Parses conditional logic rules and generates categorized BOM configurations from Excel spreadsheets based on "平台配置清单" worksheet
|
||||
2. **BOM Extraction System** (VBA_BOMConverter/) - Parses product model strings and automatically extracts matching materials from BOM库.xlsx based on "产品型号" worksheet
|
||||
|
||||
## Architecture
|
||||
|
||||
### BOM Configuration System (VBA/Modules/)
|
||||
|
||||
The BOM Configuration system follows a modular architecture with clear separation of concerns:
|
||||
|
||||
- **M01_Main.bas** - Entry point and orchestration. Run `RunBOMConversion()` to execute the full workflow.
|
||||
- **M02_DataIO.bas** - Data input/output operations. Reads source data from "平台配置清单" worksheet and generates categorized output workbooks.
|
||||
- **M03_Logic.bas** - Core recursive parser for conditional expressions. Handles logical operators (AND/OR), nested parentheses, and key=value/key!=val conditions. Implements Cartesian products for set operations.
|
||||
- **M04_Config.bas** - Column mapping and header priorities. Defines source data columns (CODE, NAME, QTY, CONDITION, CATEGORY) and standard ordering for output.
|
||||
- **M05_PreProcessor.bas** - Preprocessing module for condition transformation. Handles category-specific preprocessing: "接头" (azxs+lcfw mapping, OR merging, parentheses simplification), "部件" (azxs mapping only, OR merging, parentheses simplification). Uses mapping data from "对照表" worksheet.
|
||||
- **M99_TestRunner.bas** - Unit testing framework. Run `RunAllTests()` in VBA Immediate window to execute tests.
|
||||
|
||||
### BOM Extraction System (VBA_BOMConverter/Modules/)
|
||||
|
||||
The BOM Extraction system follows a modular architecture with clear separation of concerns:
|
||||
|
||||
- **M09_BOMExtractor.bas** - Main orchestration and entry point. Run `RunBOMExtraction()` to execute the full workflow. Implements two-phase validation (collect → validate) and generates output worksheets.
|
||||
- **M06_ModelParser.bas** - Product model string parsing. Extracts parameters from full model strings (e.g., `YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3`). Applies value mapping via M06A_Mapper.
|
||||
- **M06A_Mapper.bas** - Value mapping module. Maps azxs and lcfw parameters to their descriptive values from "对照表" worksheet. Returns dual-value format (raw,mapped) for flexible matching.
|
||||
- **M07_BOMMatcher.bas** - BOM library matching. Matches extracted parameters against BOM库.xlsx worksheets using parameter-based rules (empty cells = wildcard, "!=" prefix = negative match, fjgn field = substring matching, dual-value matching for mapped parameters).
|
||||
- **M08_ComponentProcessor.bas** - Special handling for "部件" (component) materials. Handles component inventory logic (component vs sub-components selection).
|
||||
- **M06B_TestRunner.bas** - Unit testing framework for BOM Extraction system. Run tests in VBA Immediate window.
|
||||
|
||||
### Class Module
|
||||
|
||||
- **clsErrorLogger.cls** - Error and warning handling. Tracks conflicts, parsing failures, and provides detailed context. Supports both errors (blocking) and warnings (non-blocking) with color-coded reporting.
|
||||
|
||||
### Data Flow
|
||||
|
||||
**BOM Configuration System Flow**:
|
||||
```
|
||||
Excel "平台配置清单" → M01_Main → M02_DataIO.LoadSourceData() →
|
||||
M05_PreProcessor.PreprocessCondition() → M03_Logic.ParseRule() →
|
||||
Category Dictionary → M02_DataIO.CreateOutputWorkbook() →
|
||||
New Excel Workbook + Error Report
|
||||
```
|
||||
|
||||
**BOM Extraction System Flow**:
|
||||
```
|
||||
"产品型号" worksheet → M09_BOMExtractor.RunBOMExtraction()
|
||||
→ M06A_Mapper.InitMapper() (load mapping table from "对照表" worksheet)
|
||||
→ M06_ModelParser.ParseProductModel()
|
||||
→ Extract raw parameters: azxs="A0", lcfw="M01"
|
||||
→ M06A_Mapper.MapAzxs() / MapLcfw()
|
||||
→ Return dual values: azxs="A0,径向", lcfw="M01,低压"
|
||||
→ M07_BOMMatcher.MatchBOMRecord()
|
||||
→ EvaluateCellCondition() supports dual-value matching
|
||||
→ Match if BOM库 contains "A0" OR "径向"
|
||||
→ M08_ComponentProcessor.ProcessComponentRecord() (special component handling if applicable)
|
||||
→ Two-phase validation (collect all matches → validate with cross-worksheet rules)
|
||||
→ "BOM提取结果" worksheet + "错误报告" worksheet
|
||||
```
|
||||
|
||||
## Key Concepts
|
||||
|
||||
---
|
||||
|
||||
## BOM Configuration System (VBA/)
|
||||
|
||||
### Preprocessing (M05_PreProcessor)
|
||||
|
||||
Before parsing conditions, the system applies category-specific preprocessing:
|
||||
|
||||
**Supported Categories**:
|
||||
|
||||
| Category | azxs Mapping | lcfw Mapping | OR Merging | Parentheses Simplification |
|
||||
|----------|-------------|--------------|------------|----------------------------|
|
||||
| 接头 | ✓ | ✓ | ✓ | ✓ |
|
||||
| 部件 | ✓ | ✗ | ✓ | ✓ |
|
||||
| Other | ✗ | ✗ | ✗ | ✗ |
|
||||
|
||||
**Value Mapping**:
|
||||
- **azxs** (安装形式): Maps codes to descriptive values
|
||||
- A0, AT, AH → 径向
|
||||
- B0, BT, BZ, BH → 下轴向
|
||||
- Z0, ZT, ZZ, ZH → 中轴向
|
||||
- **lcfw** (量程范围): Maps range codes to categories (接头 category only)
|
||||
- M01-M11 → 低压
|
||||
- M12-M16 → 高压
|
||||
|
||||
**OR Condition Merging**:
|
||||
- Duplicate OR conditions are automatically merged
|
||||
- Example: `azxs=径向 OR azxs=径向` → `azxs=径向`
|
||||
- Different values preserve OR structure: `lcfw=低压 OR lcfw=高压`
|
||||
|
||||
**Parentheses Handling**:
|
||||
- Simplified single-value expressions: `(azxs=A0 OR azxs=AT)` → `azxs=径向`
|
||||
- Preserves parentheses when needed: `(lcfw=M01 OR lcfw=M15)` → `(lcfw=低压 OR lcfw=高压)`
|
||||
|
||||
Mapping data is loaded from "对照表" worksheet (columns A:B for lcfw, D:E for azxs).
|
||||
|
||||
### Conditional Logic Syntax (M03_Logic)
|
||||
|
||||
Conditions use a specific syntax for product selection:
|
||||
- **Atoms**: `key=value` or `key!=value` (e.g., `gclj=M20`, `jycz=1`)
|
||||
- **AND**: Cartesian product of sets (e.g., `gclj=M20 AND jycz=1`)
|
||||
- **OR**: Union of sets (e.g., `azxs=A0 OR azxs=AT`)
|
||||
- **Parentheses**: Nested grouping (e.g., `(azxs=A0 OR azxs=AT) AND jycz=1`)
|
||||
|
||||
Example: `gclj=M20 AND jycz=1 AND lcfw=M01 AND (azxs=A0 OR azxs=AT OR azxs=AH)`
|
||||
|
||||
### Column Mapping (M04_Config)
|
||||
|
||||
Source data must have columns in this order:
|
||||
- Column C (3): CODE (代号)
|
||||
- Column D (4): NAME (名称)
|
||||
- Column E (5): QTY (数量)
|
||||
- Column F (6): CONDITION (选择条件)
|
||||
- Column H (8): CATEGORY (类别)
|
||||
|
||||
Data starts from row 4.
|
||||
|
||||
### Category-Based Output
|
||||
|
||||
Results are automatically organized by category (部件, 接头, 弹性元件, etc.). Each category gets a separate worksheet in the output workbook with columns dynamically ordered based on detected configuration keys.
|
||||
|
||||
---
|
||||
|
||||
## BOM Extraction System (VBA_BOMConverter/)
|
||||
|
||||
### Model String Structure (M06_ModelParser)
|
||||
|
||||
Product models follow a structured format that the parser can extract parameters from:
|
||||
|
||||
**Full Model**: `[表头]|[表盘]|[附件]|[法兰隔膜]`
|
||||
|
||||
**Table Header Format**: `[型号]-[公称外径].[安装形式].[壳体形式].[过程连接&接液材质].[量程范围].[仪表特性]`
|
||||
|
||||
**Example**: `YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3`
|
||||
- Table header: `YTHN-100.A0.531.G123.M04.Y3`
|
||||
- Parameters extracted:
|
||||
- `xh` = YTHN (型号)
|
||||
- `gcwj` = 100 (公称外径)
|
||||
- `azxs` = A0 (安装形式)
|
||||
- `bkxs` = 531 (表壳形式)
|
||||
- `gclj` = G12 (过程连接)
|
||||
- `jycz` = 3 (接液材质)
|
||||
- `lcfw` = M04 (量程范围)
|
||||
- `fjgn` = Y3 (附加功能)
|
||||
|
||||
**Parsing Logic**:
|
||||
- Only processes table header portion (before first `|`)
|
||||
- Segments are split by `.` delimiter
|
||||
- `gclj` and `jycz` are extracted from single segment (last char is `jycz`)
|
||||
- `fjgn` combines all segments from position 5 onwards with comma separator
|
||||
|
||||
### Value Mapping (M06A_Mapper)
|
||||
|
||||
After parsing raw parameter values, the system applies value mapping from "对照表" worksheet:
|
||||
|
||||
**Dual-Value Format**:
|
||||
- Parameters are stored as "raw,mapped" to support flexible matching
|
||||
- Example: `azxs="A0,径向"` means the parameter can match either "A0" or "径向"
|
||||
- Example: `lcfw="M01,低压"` means the parameter can match either "M01" or "低压"
|
||||
|
||||
**Mapping Table Configuration** (M04_Config):
|
||||
- `MAPPING_SHEET_NAME` = "对照表"
|
||||
- `MAPPING_COL_LCFW_KEY` = 1 (A列) - lcfw raw values
|
||||
- `MAPPING_COL_LCFW_VAL` = 2 (B列) - lcfw mapped values
|
||||
- `MAPPING_COL_AZXS_KEY` = 4 (D列) - azxs raw values
|
||||
- `MAPPING_COL_AZXS_VAL` = 5 (E列) - azxs mapped values
|
||||
- `MAPPING_START_ROW` = 3
|
||||
|
||||
**Graceful Degradation**:
|
||||
- If "对照表" worksheet is not found, system logs a warning and uses raw values only
|
||||
- If a value is not found in mapping table, only the raw value is used (no error)
|
||||
|
||||
**Example Mapping Data**:
|
||||
| Row | A列 (lcfw key) | B列 (lcfw val) | D列 (azxs key) | E列 (azxs val) |
|
||||
|-----|----------------|----------------|----------------|----------------|
|
||||
| 3 | M01 | 低压 | A0 | 径向 |
|
||||
| 4 | M12 | 高压 | AT | 径向 |
|
||||
| 5 | - | - | B0 | 下轴向 |
|
||||
|
||||
### BOM Matching Rules (M07_BOMMatcher)
|
||||
|
||||
The system matches extracted parameters against BOM库.xlsx worksheets using these rules:
|
||||
|
||||
**Cell Matching Rules**:
|
||||
- **Empty cell**: Wildcard - matches all values
|
||||
- **"!=" prefix**: Negative match - matches when parameter ≠ value (e.g., `!=A0`)
|
||||
- **fjgn field**: Substring matching - matches when fjgn list contains cell value (InStr check)
|
||||
- **Dual-value parameters**: Matches if ANY value matches (e.g., `azxs="A0,径向"` matches both `azxs=A0` and `azxs=径向`)
|
||||
- **Normal values**: Exact match - parameter must equal cell value
|
||||
|
||||
**Logic**: AND across all parameters - all conditions must be satisfied
|
||||
|
||||
**Examples**:
|
||||
|
||||
1. **Dual-value matching**:
|
||||
- Parameter: `azxs="A0,径向"`
|
||||
- BOM库 has `azxs=径向` → MATCH (because "径向" is in the parameter)
|
||||
- BOM库 has `azxs=A0` → MATCH (because "A0" is in the parameter)
|
||||
- BOM库 has `azxs=AT` → NO MATCH
|
||||
|
||||
2. **Standard matching**:
|
||||
- BOM库 row has `azxs=A0`, `bkxs=`, `gclj=G12`, `fjgn=N1`
|
||||
- Matches: `azxs=A0,径向`, `bkxs=531`, `gclj=G12`, `fjgn=N1,N2`
|
||||
- Reason: `azxs` contains "A0", `bkxs` is empty (wildcard), `gclj` matches exactly, `fjgn` contains "N1"
|
||||
|
||||
### Component Special Handling (M08_ComponentProcessor)
|
||||
|
||||
"部件" (component) materials contain three types of materials in one record:
|
||||
|
||||
**Record Structure**:
|
||||
1. Component material itself (部件)
|
||||
2. Joint material (接头) - sub-component 1
|
||||
3. Elastic element material (弹性元件) - sub-component 2
|
||||
|
||||
**Selection Strategy**:
|
||||
- **Inventory sufficient** → Return component material (1 item)
|
||||
- **Inventory insufficient** → Return sub-components (1 joint + 1 elastic element)
|
||||
|
||||
**Validation Rules**:
|
||||
- Valid: 1 component OR 1 joint + 1 elastic element
|
||||
- Invalid: Any other combination (e.g., component + joint together, only joint, only element, multiple components)
|
||||
|
||||
**Note**: Inventory checking interface is reserved for future ERP integration. Current version always returns inventory sufficient.
|
||||
|
||||
### Two-Phase Validation (M09_BOMExtractor)
|
||||
|
||||
The system uses two-phase validation to enable cross-worksheet validation:
|
||||
|
||||
**Phase 1: Collection**
|
||||
- Iterate through all BOM库 worksheets
|
||||
- Collect match results WITHOUT recording errors
|
||||
- Store results for each worksheet: success, rowCount, rowNums, materials
|
||||
|
||||
**Phase 2: Validation**
|
||||
- Validate all collected results together
|
||||
- Apply cross-worksheet rules (e.g., component/joint/element exclusivity)
|
||||
- Distinguish between **errors** (blocking) and **warnings** (non-blocking)
|
||||
|
||||
**Cross-Worksheet Validation Rules**:
|
||||
1. **Component sheet returns component** → Ignore independent joint/element worksheets (warning)
|
||||
2. **Component sheet returns sub-components** → Ignore independent joint/element worksheets (warning)
|
||||
3. **Component sheet has no match** → Use independent joint/element worksheets
|
||||
4. **Non-special worksheets** → Must match exactly 1 record
|
||||
|
||||
**Error vs Warning**:
|
||||
- **Error**: Blocking issues (0 matches, 2+ matches, invalid combinations)
|
||||
- **Warning**: Non-blocking issues (conflicting matches that were resolved)
|
||||
|
||||
## Development Commands
|
||||
|
||||
### Running BOM Configuration System
|
||||
|
||||
1. Open `YTHN-100.xlsm` in Excel
|
||||
2. Ensure "平台配置清单" worksheet exists with proper data
|
||||
3. Run `M01_Main.RunBOMConversion()` or execute from the Excel interface
|
||||
|
||||
**Output**: New Excel workbook with categorized BOM data + error report
|
||||
|
||||
### Running BOM Extraction System
|
||||
|
||||
1. Open the main Excel workbook (e.g., `YTHN-100.xlsm`)
|
||||
2. Ensure "产品型号" worksheet exists with product model data
|
||||
3. Ensure `BOM库.xlsx` is in the same directory as the main workbook
|
||||
4. (Optional) Ensure "对照表" worksheet exists for azxs/lcfw value mapping
|
||||
5. Run `M09_BOMExtractor.RunBOMExtraction()` or execute from the Excel interface
|
||||
|
||||
**Output**:
|
||||
- "BOM提取结果" worksheet - Contains extracted materials with parameters
|
||||
- "错误报告_[timestamp]" worksheet - Generated only if errors/warnings exist
|
||||
|
||||
### Running Tests
|
||||
|
||||
In Excel VBA Immediate Window (Ctrl+G):
|
||||
|
||||
**For BOM Configuration System (M99_TestRunner)**:
|
||||
```
|
||||
RunAllTests
|
||||
```
|
||||
|
||||
This runs unit tests for:
|
||||
- Simple atom parsing (`key=value`)
|
||||
- AND operations with Cartesian products
|
||||
- OR operations with union operations
|
||||
- Nested parentheses handling
|
||||
- Logic conflict detection
|
||||
- **Preprocessing tests** (Test_PP_01 to Test_PP_12):
|
||||
- azxs mapping load (12 values)
|
||||
- lcfw mapping load (M01-M16)
|
||||
- Value replacement
|
||||
- OR condition merging
|
||||
- Full integration with M03_Logic
|
||||
- Non-"接头"/"部件" category handling
|
||||
- Unmapped value handling
|
||||
- "部件" category azxs mapping (PP_09)
|
||||
- "部件" category lcfw unchanged (PP_10)
|
||||
- "部件" category OR merging (PP_11)
|
||||
- "部件" category parentheses simplification (PP_12)
|
||||
|
||||
**For BOM Extraction System (M06B_TestRunner)**:
|
||||
```
|
||||
RunAllTests
|
||||
```
|
||||
|
||||
This runs unit tests for:
|
||||
- M06_ModelParser tests (model string parsing)
|
||||
- M07_BOMMatcher tests (BOM library matching)
|
||||
- M08_ComponentProcessor tests (component handling)
|
||||
- M09_BOMExtractor tests (full extraction workflow)
|
||||
|
||||
### Python Skills (Claude Code Integration)
|
||||
|
||||
The project includes custom skills in `.claude/skills/`:
|
||||
|
||||
- **excel-to-markdown**: Convert Excel files to Markdown tables
|
||||
```bash
|
||||
python3 .claude/skills/excel-to-markdown/scripts/excel_to_markdown.py <file.xlsx>
|
||||
```
|
||||
|
||||
- **mermaid-diagrams**: Generate architecture diagrams using Mermaid syntax
|
||||
|
||||
## Configuration
|
||||
|
||||
### System Constants (M04_Config)
|
||||
|
||||
**File Configuration**:
|
||||
- `BOMLIB_FILENAME` = "BOM库.xlsx" - BOM library file name
|
||||
- `OUTPUT_SHEET_NAME` = "BOM提取结果" - Output worksheet name
|
||||
- `BOMLIB_START_ROW` = 2 - BOM库 data starts from row 2 (row 1 is header)
|
||||
|
||||
**Mapping Table Configuration**:
|
||||
- `MAPPING_SHEET_NAME` = "对照表" - Mapping table worksheet name
|
||||
- `MAPPING_COL_LCFW_KEY` = 1 - Column A for lcfw raw values
|
||||
- `MAPPING_COL_LCFW_VAL` = 2 - Column B for lcfw mapped values
|
||||
- `MAPPING_COL_AZXS_KEY` = 4 - Column D for azxs raw values
|
||||
- `MAPPING_COL_AZXS_VAL` = 5 - Column E for azxs mapped values
|
||||
- `MAPPING_START_ROW` = 3 - Mapping data starts from row 3
|
||||
|
||||
**Input Column Configuration**:
|
||||
- `INPUT_COL_MODEL` = "型号"
|
||||
- `INPUT_COL_PRODUCT_MODEL` = "产品型号"
|
||||
|
||||
**BOM库 Worksheet Names**:
|
||||
- `BOMLIB_SHEET_JOINT` = "接头"
|
||||
- `BOMLIB_SHEET_ELEMENT` = "弹性元件"
|
||||
- `BOMLIB_SHEET_MOVEMENT` = "机芯"
|
||||
- `BOMLIB_SHEET_COMPONENT` = "部件"
|
||||
- `BOMLIB_SHEET_EDGE` = "边"
|
||||
|
||||
**BOM库 Column Names**:
|
||||
- `BOMLIB_COL_NAME` = "名称"
|
||||
- `BOMLIB_COL_CODE` = "编码"
|
||||
- `BOMLIB_COL_QTY` = "数量"
|
||||
- `BOMLIB_COL_JOINT_NAME` = "接头名称"
|
||||
- `BOMLIB_COL_JOINT_CODE` = "接头编码"
|
||||
- `BOMLIB_COL_JOINT_QTY` = "接头数量"
|
||||
- `BOMLIB_COL_ELEMENT_NAME` = "弹性元件名称"
|
||||
- `BOMLIB_COL_ELEMENT_CODE` = "弹性元件编码"
|
||||
- `BOMLIB_COL_ELEMENT_QTY` = "弹性元件数量"
|
||||
|
||||
**Model Parsing Constants**:
|
||||
- `MODEL_SEPARATOR_PIPELINE` = "|"
|
||||
- `MODEL_SEPARATOR_DOT` = "."
|
||||
- `MODEL_HEADER_MIN_SEGMENTS` = 6
|
||||
|
||||
### Header Priority Ordering
|
||||
|
||||
Output columns are sorted according to priority defined in `M04_Config.GetHeaderPriority()`:
|
||||
1. azxs (安装形式)
|
||||
2. bkxs (表壳形式)
|
||||
3. gclj (过程连接)
|
||||
4. jycz (介质材质)
|
||||
5. lcdw (量程单位)
|
||||
6. lcfw (量程范围)
|
||||
7. fjgn (非公/耐震)
|
||||
8. btcy (表头尺寸)
|
||||
9. bp (表盘)
|
||||
10. dskd (度视宽度)
|
||||
11. nqlc (耐震连接)
|
||||
12. bptx (表盘图形)
|
||||
13. jddj (精度等级)
|
||||
14. cpdm (产品代码)
|
||||
15. tsjz (特殊基准)
|
||||
16. tsyq (特殊要求)
|
||||
17. bpts (特殊表盘)
|
||||
18. kdxh (壳体型号)
|
||||
|
||||
Unknown keys are assigned priority 999 and appear last.
|
||||
|
||||
## Git Workflow
|
||||
|
||||
### Branches
|
||||
|
||||
- **Main branch**: `DEV_YTHN-100`
|
||||
- **Current branch**: `NEW_BOM`
|
||||
|
||||
### Claude Code Permissions
|
||||
|
||||
Configured in `.claude/settings.local.json`:
|
||||
- Git operations: push, checkout, add
|
||||
- Python execution
|
||||
- Tree viewing and search utilities
|
||||
|
||||
### NTFY Notifications
|
||||
|
||||
The repository uses NTFY for git push notifications. Repository name is extracted dynamically from `github.repository` in workflow files.
|
||||
|
||||
## File Structure
|
||||
|
||||
```
|
||||
AutoBOM/
|
||||
├── VBA_BOMConverter/
|
||||
│ ├── Modules/
|
||||
│ │ ├── M01_Main.bas # BOM Configuration System - Entry point
|
||||
│ │ ├── M02_DataIO.bas # BOM Configuration System - Data I/O
|
||||
│ │ ├── M03_Logic.bas # BOM Configuration System - Conditional logic parser
|
||||
│ │ ├── M04_Config.bas # Shared - Constants and configuration
|
||||
│ │ ├── M05_PreProcessor.bas # BOM Configuration System - Preprocessing
|
||||
│ │ ├── M06_ModelParser.bas # BOM Extraction System - Model parsing
|
||||
│ │ ├── M06A_Mapper.bas # BOM Extraction System - Value mapping (NEW)
|
||||
│ │ ├── M06B_TestRunner.bas # BOM Extraction System - Unit tests
|
||||
│ │ ├── M07_BOMMatcher.bas # BOM Extraction System - BOM matching
|
||||
│ │ ├── M08_ComponentProcessor.bas # BOM Extraction System - Component handling
|
||||
│ │ ├── M09_BOMExtractor.bas # BOM Extraction System - Main orchestration
|
||||
│ │ └── M99_TestRunner.bas # BOM Configuration System - Unit tests
|
||||
│ └── ClassModules/
|
||||
│ └── clsErrorLogger.cls # Shared - Error/warning logging
|
||||
├── .claude/
|
||||
│ └── skills/ # Claude Code integration skills
|
||||
├── docs/ # Code-related documentation
|
||||
│ └── Test_PP_06_FullIntegration_流程详解.md
|
||||
├── reference_docs/ # Business-related documentation and examples
|
||||
└── YTHN-100.xlsm/.xlsx # Main workbook files
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
The system uses `clsErrorLogger` for comprehensive error and warning tracking:
|
||||
|
||||
**Error vs Warning Distinction**:
|
||||
- **Errors**: Blocking issues that indicate failure (red highlighting in report)
|
||||
- Examples: No match found, multiple matches, invalid combinations
|
||||
- Properties: `HasErrors`, `pErrors.Count`
|
||||
- **Warnings**: Non-blocking issues (yellow highlighting in report)
|
||||
- Examples: Conflicting matches that were resolved, fallback scenarios
|
||||
- Properties: `HasWarnings`, `pWarnings.Count`
|
||||
- **HasIssues**: True if either errors or warnings exist
|
||||
|
||||
**Error Recording**:
|
||||
```vba
|
||||
' Record error
|
||||
logger.Record rowIndex, sourceFunction, errorType, description, context
|
||||
|
||||
' Record warning
|
||||
logger.RecordWarning rowIndex, sourceFunction, warningType, description, context
|
||||
```
|
||||
|
||||
**Error Report Output**:
|
||||
- Creates "错误报告_[timestamp]" worksheet
|
||||
- Color-coded rows: red for errors, yellow for warnings
|
||||
- Columns: Type, Row Number, Source Module, Error Type, Description, Context
|
||||
- Only generated if issues exist
|
||||
|
||||
## Important Notes
|
||||
|
||||
### BOM Configuration System
|
||||
|
||||
- **Late Binding**: VBA modules use late binding (CreateObject) to avoid external reference dependencies
|
||||
- **Operator Precedence**: AND is processed before OR, parentheses override default precedence
|
||||
- **Recursive Parsing**: Nested expressions are handled recursively in M03_Logic
|
||||
- **Dynamic Columns**: Output workbooks detect and include only relevant configuration keys
|
||||
|
||||
### BOM Extraction System
|
||||
|
||||
- **rowCount vs materials.count**: Critical distinction in BOM matching
|
||||
- `rowCount` = Number of worksheet rows matched in BOM库
|
||||
- `materials.count` = Number of actual materials returned
|
||||
- These can differ when components fall back to sub-components (1 row → 2 materials)
|
||||
|
||||
- **Performance Optimizations**:
|
||||
- Array-based processing instead of cell-by-cell operations
|
||||
- Header mapping cached for each worksheet
|
||||
- Progress updates every 10 models
|
||||
- Screen updating and calculation disabled during execution
|
||||
|
||||
- **Two-Phase Matching**: Separates collection from validation to enable cross-worksheet validation rules that would be impossible with immediate error reporting
|
||||
|
||||
- **Late Binding**: VBA modules use late binding (CreateObject) to avoid external reference dependencies
|
||||
|
||||
- **Dynamic Columns**: Output workbooks detect and include only relevant configuration keys based on extracted parameters
|
||||
|
||||
## Documentation Guidelines
|
||||
|
||||
### Document Storage Policy
|
||||
|
||||
When creating documentation for this project, follow these guidelines:
|
||||
|
||||
**Code-Related Documentation** → Save in `docs/` directory:
|
||||
- Technical specifications
|
||||
- Algorithm explanations
|
||||
- Code flow diagrams
|
||||
- Test documentation
|
||||
- API/reference documentation for code modules
|
||||
- Implementation guides
|
||||
|
||||
Examples:
|
||||
- `docs/Test_PP_06_FullIntegration_流程详解.md` ✓
|
||||
- `docs/M03_Logic_Algorithm.md` ✓
|
||||
- `docs/API_Reference.md` ✓
|
||||
|
||||
**Business-Related Documentation** → Save in `reference_docs/` directory:
|
||||
- Business requirements
|
||||
- User manuals
|
||||
- Product specifications
|
||||
- Industry standards
|
||||
- Configuration examples
|
||||
- Business process documentation
|
||||
|
||||
Examples:
|
||||
- `reference_docs/BOM_Requirements.md` ✓
|
||||
- `reference_docs/Product_Catalog.xlsx` ✓
|
||||
- `reference_docs/User_Guide.pdf` ✓
|
||||
|
||||
**Decision Tree**:
|
||||
```
|
||||
Is it about code implementation or technical details?
|
||||
├─ Yes → docs/
|
||||
└─ No → Is it about business logic or user-facing content?
|
||||
├─ Yes → reference_docs/
|
||||
└─ No → Ask for clarification
|
||||
```
|
||||
|
||||
**Note**: When in doubt, prefer `docs/` for technical content and `reference_docs/` for business content.
|
||||
95
VBA/AGENTS.md
Normal file
95
VBA/AGENTS.md
Normal file
@@ -0,0 +1,95 @@
|
||||
# VBA Module Knowledge Base
|
||||
|
||||
**Scope:** `VBA/` directory - Core automation logic
|
||||
|
||||
## OVERVIEW
|
||||
|
||||
VBA modules for BOM extraction: ClassModules (domain logic), Modules (workflows), DocumentModules (sheet events).
|
||||
|
||||
## STRUCTURE
|
||||
|
||||
```
|
||||
VBA/
|
||||
├── ClassModules/ # 4 classes: BomExtractor, BomItem, ProductModelParser, ConditionEvaluator
|
||||
├── Modules/ # 4 modules: MainModule, BIPUploadModule, ComponentInventoryCheckModule, TestModule
|
||||
├── DocumentModules/ # Sheet9.cls (event handlers)
|
||||
└── vba_metadata.json # Module metadata
|
||||
```
|
||||
|
||||
## WHERE TO LOOK
|
||||
|
||||
| Task | Location | Notes |
|
||||
|------|----------|-------|
|
||||
| Main entry point | `Modules/MainModule.bas` | `ProcessProductModels()` |
|
||||
| BOM extraction | `ClassModules/BomExtractor.cls` | `ExtractBom()` with assembly logic |
|
||||
| Model parsing | `ClassModules/ProductModelParser.cls` | `Parse()` extracts 6 conditions |
|
||||
| Condition logic | `ClassModules/ConditionEvaluator.cls` | Expression evaluation |
|
||||
| BIP upload | `Modules/BIPUploadModule.bas` | Upload to BIP system |
|
||||
| Inventory check | `Modules/ComponentInventoryCheckModule.bas` | Component verification |
|
||||
|
||||
## CODE MAP
|
||||
|
||||
| Symbol | Type | Location | Role |
|
||||
|--------|------|----------|------|
|
||||
| `ProcessProductModels()` | Sub | `MainModule.bas` | Main entry point |
|
||||
| `ExtractBom()` | Function | `BomExtractor.cls` | Core matching logic |
|
||||
| `Parse()` | Function | `ProductModelParser.cls` | Model string parser |
|
||||
| `Evaluate()` | Function | `ConditionEvaluator.cls` | Condition evaluator |
|
||||
| `LoadFromRow()` | Sub | `BomItem.cls` | Row data loader |
|
||||
|
||||
## CONVENTIONS
|
||||
|
||||
### Module Organization
|
||||
- **ClassModules**: `*.cls` files with `Option Explicit`, public methods, private state
|
||||
- **Modules**: `*.bas` files with public subs, helper functions
|
||||
- **DocumentModules**: Sheet-specific event handlers (e.g., `Sheet9.cls`)
|
||||
|
||||
### Error Handling Pattern
|
||||
```vba
|
||||
On Error GoTo ErrorHandler
|
||||
' ... logic ...
|
||||
Exit Sub
|
||||
ErrorHandler:
|
||||
' Handle error
|
||||
```
|
||||
|
||||
### Data Loading
|
||||
- `BomExtractor.LoadBomData()` starts at row 4 (row 3 is header)
|
||||
- `BomItem.LoadFromRow()` uses `On Error Resume Next` for type conversions
|
||||
|
||||
## ANTI-PATTERNS (THIS PROJECT)
|
||||
|
||||
- **NEVER** change row indexing in `LoadBomData()` — hardcoded to start at row 4
|
||||
- **NEVER** reorder `BomItem` fields — column mapping is hardcoded (11 fields)
|
||||
- **NEVER** remove `On Error Resume Next` in `LoadFromRow()` — handles type conversion
|
||||
- **NEVER** bypass `ApplyAssemblyLogic()` — parent/child override is critical
|
||||
- **NEVER** hardcode workbook names — always use `ThisWorkbook`
|
||||
|
||||
## UNIQUE STYLES
|
||||
|
||||
### Assembly Logic (总成逻辑)
|
||||
Parent category overrides children if:
|
||||
1. Parent matches exactly 1 item
|
||||
2. All children match at least 1 item
|
||||
3. Result: parent output, children hidden
|
||||
|
||||
### Batch Output Pattern
|
||||
```vba
|
||||
' Collect in Collection
|
||||
' Convert to 2D array
|
||||
' Single Range.Value write
|
||||
```
|
||||
|
||||
### Condition Syntax
|
||||
- Format: `"field=value|field2=value2"` (pipe = OR)
|
||||
- Example: `"azxs=M|azxs=L"` (vertical OR horizontal installation)
|
||||
|
||||
## NOTES
|
||||
|
||||
### Scripting.Dictionary Dependency
|
||||
All classes use `CreateObject("Scripting.Dictionary")` - Windows only, not Mac-compatible.
|
||||
|
||||
### Workbook Sheets Required
|
||||
- `产品订单` - Input orders
|
||||
- `平台配置清单` - BOM configuration (starts at row 4)
|
||||
- `BOM 提取结果` - Auto-created output
|
||||
148
VBA/ClassModules/AGENTS.md
Normal file
148
VBA/ClassModules/AGENTS.md
Normal file
@@ -0,0 +1,148 @@
|
||||
# ClassModules Knowledge Base
|
||||
|
||||
**Scope:** `VBA/ClassModules/` - Domain logic classes
|
||||
|
||||
## OVERVIEW
|
||||
|
||||
4 classes: `ProductModelParser` (parse model strings), `BomItem` (data model), `ConditionEvaluator` (evaluate conditions), `BomExtractor` (core matching logic).
|
||||
|
||||
## STRUCTURE
|
||||
|
||||
```
|
||||
ClassModules/
|
||||
├── ProductModelParser.cls # Parse model strings → extract 6 conditions
|
||||
├── BomItem.cls # BOM data model (11 fields)
|
||||
├── ConditionEvaluator.cls # Evaluate condition expressions
|
||||
└── BomExtractor.cls # Core matching + assembly logic
|
||||
```
|
||||
|
||||
## WHERE TO LOOK
|
||||
|
||||
| Task | Location | Notes |
|
||||
|------|----------|-------|
|
||||
| Parse model string | `ProductModelParser.cls` | `Parse()` method |
|
||||
| Extract conditions | `ProductModelParser.cls` | Returns `azxs`, `bkxs`, `gclj`, `jycz`, `lcfw`, `fjgn` |
|
||||
| Load BOM row | `BomItem.cls` | `LoadFromRow()` with type conversion |
|
||||
| Evaluate condition | `ConditionEvaluator.cls` | `Evaluate(condition, conditions)` |
|
||||
| Match BOM items | `BomExtractor.cls` | `ExtractBom()` with 4-step process |
|
||||
| Assembly logic | `BomExtractor.cls` | `ApplyAssemblyLogic()` parent/child override |
|
||||
|
||||
## CODE MAP
|
||||
|
||||
### ProductModelParser
|
||||
| Method | Role |
|
||||
|--------|------|
|
||||
| `Parse(modelString)` | Entry point, validates format |
|
||||
| `ParseHeader()` | Splits model string by `-` and `.` |
|
||||
| `ExtractConnectionAndMaterial()` | Separates `gclj` and `jycz` from code |
|
||||
| `ExtractAdditionalFeatures()` | Removes oil-fill type (Y+digit) from `fjgn` |
|
||||
|
||||
### BomItem
|
||||
| Property | Type | Column |
|
||||
|----------|------|--------|
|
||||
| `RowNumber` | Long | A |
|
||||
| `Module` | String | B |
|
||||
| `code` | String | C |
|
||||
| `Name` | String | D |
|
||||
| `quantity` | Double | E |
|
||||
| `SelectCondition` | String | F |
|
||||
| `Remark` | String | G |
|
||||
| `category` | String | H |
|
||||
| `ParentCategory` | String | I |
|
||||
| `CategoryCondition` | String | J |
|
||||
| `Code66` | String | K |
|
||||
|
||||
### BomExtractor
|
||||
| Method | Role |
|
||||
|--------|------|
|
||||
| `LoadBomData()` | Loads from row 4 (header row 3) |
|
||||
| `ExtractBom(conditions)` | 4-step: determine categories → match → apply assembly → validate |
|
||||
| `ApplyAssemblyLogic()` | Parent overrides children if all match |
|
||||
| `ValidateResult()` |双向覆盖检查 (parent/child coverage) |
|
||||
|
||||
## CONVENTIONS
|
||||
|
||||
### Class Structure
|
||||
```vba
|
||||
Option Explicit
|
||||
|
||||
' Private state
|
||||
Private pPropertyName As Type
|
||||
|
||||
' Initialize
|
||||
Private Sub Class_Initialize()
|
||||
Set pProperty = New Collection
|
||||
End Sub
|
||||
|
||||
' Public methods
|
||||
Public Function MethodName() As ReturnType
|
||||
On Error GoTo ErrorHandler
|
||||
' Logic
|
||||
Exit Function
|
||||
ErrorHandler:
|
||||
' Handle
|
||||
End Function
|
||||
```
|
||||
|
||||
### Condition Dictionary
|
||||
All condition methods accept `Scripting.Dictionary` with keys: `azxs`, `bkxs`, `gclj`, `jycz`, `lcfw`, `fjgn`.
|
||||
|
||||
### Error Collection Pattern
|
||||
```vba
|
||||
Private pErrorMessages As Collection
|
||||
Public Function GetErrorSummary() As String
|
||||
' Join all errors with "; "
|
||||
End Function
|
||||
```
|
||||
|
||||
## ANTI-PATTERNS (THIS PROJECT)
|
||||
|
||||
- **NEVER** change `BomItem.LoadFromRow()` column indices — hardcoded to match platform configuration sheet
|
||||
- **NEVER** remove `On Error Resume Next` in `LoadFromRow()` — type conversions fail gracefully
|
||||
- **NEVER** skip validation in `ValidateResult()` —双向覆盖检查 prevents false negatives
|
||||
- **DO NOT** modify `BomExtractor.LoadBomData()` row start — must be row 4 (row 3 is header)
|
||||
|
||||
## UNIQUE STYLES
|
||||
|
||||
### Assembly Logic Algorithm
|
||||
```vba
|
||||
' 1. Build parent→child map from CategoryHierarchy
|
||||
' 2. Count matches per category
|
||||
' 3. If parent=1 AND all children≥1 → parent covers children
|
||||
' 4. Output parent only, hide children
|
||||
```
|
||||
|
||||
### Model String Format
|
||||
```
|
||||
[Header]-[Spec1].[Spec2].[Spec3].[Spec4].[Spec5]|[Detail1]|[Detail2]
|
||||
Example: Y-100-M203.316SS.L100.N2
|
||||
|
||||
Parsed conditions:
|
||||
- azxs (安装形式): M (vertical)
|
||||
- bkxs (表壳形式): 3
|
||||
- gclj (过程连接): M20
|
||||
- jycz (接液材质): 3 (316SS)
|
||||
- lcfw (量程范围): 0.L100
|
||||
- fjgn (附加功能): N2 (from .316SS.L100.N2)
|
||||
```
|
||||
|
||||
### Condition Expression Syntax
|
||||
```vba
|
||||
' Single: "field=value"
|
||||
' OR: "field1=value1|field2=value2"
|
||||
' Example: "azxs=M|azxs=L" → vertical OR horizontal
|
||||
```
|
||||
|
||||
## NOTES
|
||||
|
||||
### Category Hierarchy
|
||||
Built from `ParentCategory` field (column I). Example:
|
||||
- "接头" → "部件"
|
||||
- "弹性元件" → "部件"
|
||||
- Result: if "部件" matches AND both children match → output "部件" only
|
||||
|
||||
### Type Conversion
|
||||
`BomItem.LoadFromRow()` uses `CLng()`, `CStr()`, `CDbl()` with `On Error Resume Next` — invalid conversions become 0 or empty string.
|
||||
|
||||
### Scripting.Dictionary
|
||||
Windows-only. All dictionary usage via `CreateObject("Scripting.Dictionary")`.
|
||||
490
VBA/ClassModules/BomExtractor.cls
Normal file
490
VBA/ClassModules/BomExtractor.cls
Normal file
@@ -0,0 +1,490 @@
|
||||
'=====================================================================
|
||||
' 类名: BomExtractor
|
||||
' 功能: BOM提取器,从平台配置清单中提取匹配的物料
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
Private pWorksheet As Worksheet
|
||||
Private pConditionEvaluator As ConditionEvaluator
|
||||
Private pAllItems As Collection ' 所有BOM项
|
||||
Private pMatchedItems As Collection ' 匹配的BOM项
|
||||
Private pRequiredCategories As Collection ' 需要的类别
|
||||
Private pCategoryHierarchy As Object ' 类别层次结构 Dictionary(子类别->父类别)
|
||||
Private pErrorMessages As Collection
|
||||
Private pExcludeCategories As Collection ' 需要排除的类别
|
||||
|
||||
'=====================================================================
|
||||
' 方法: Class_Initialize
|
||||
' 功能: 初始化类
|
||||
'=====================================================================
|
||||
Private Sub Class_Initialize()
|
||||
Set pConditionEvaluator = New ConditionEvaluator
|
||||
Set pAllItems = New Collection
|
||||
Set pMatchedItems = New Collection
|
||||
Set pRequiredCategories = New Collection
|
||||
Set pCategoryHierarchy = CreateObject("Scripting.Dictionary")
|
||||
Set pErrorMessages = New Collection
|
||||
Set pExcludeCategories = New Collection
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: SetWorksheet
|
||||
' 功能: 设置BOM数据源工作表
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Public Sub SetWorksheet(ws As Worksheet)
|
||||
Set pWorksheet = ws
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: LoadBomData
|
||||
' 功能: 加载BOM数据
|
||||
' 返回: Boolean - 成功返回True
|
||||
'=====================================================================
|
||||
Public Function LoadBomData() As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If pWorksheet Is Nothing Then
|
||||
pErrorMessages.Add "未设置工作表"
|
||||
LoadBomData = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 清空现有数据
|
||||
Set pAllItems = New Collection
|
||||
Set pCategoryHierarchy = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 从第4行开始读取(第3行是表头)
|
||||
Dim lastRow As Long
|
||||
lastRow = pWorksheet.Cells(pWorksheet.Rows.count, 1).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
Dim item As BomItem
|
||||
|
||||
For i = 4 To lastRow
|
||||
' 检查行号是否为空
|
||||
If Trim(pWorksheet.Cells(i, 1).value) <> "" Then
|
||||
Set item = New BomItem
|
||||
item.LoadFromRow pWorksheet, i
|
||||
|
||||
' 只添加有效物料(类别不为空)
|
||||
If item.IsValidItem Then
|
||||
pAllItems.Add item
|
||||
|
||||
' 构建类别层次结构
|
||||
If item.HasParentCategory Then
|
||||
If Not pCategoryHierarchy.Exists(item.category) Then
|
||||
pCategoryHierarchy.Add item.category, item.ParentCategory
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
' 非有效物料也添加,但标记为特殊类别
|
||||
pAllItems.Add item
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
LoadBomData = True
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
pErrorMessages.Add "加载BOM数据异常: " & Err.Description
|
||||
LoadBomData = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: SetExcludeCategories
|
||||
' 功能: 设置需要排除的类别
|
||||
' 参数: categories - 类别集合
|
||||
'=====================================================================
|
||||
Public Sub SetExcludeCategories(categories As Collection)
|
||||
Set pExcludeCategories = categories
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ClearExcludeCategories
|
||||
' 功能: 清空排除类别列表
|
||||
'=====================================================================
|
||||
Public Sub ClearExcludeCategories()
|
||||
Set pExcludeCategories = New Collection
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ClearErrorMessages
|
||||
' 功能: 清空错误信息列表
|
||||
'=====================================================================
|
||||
Public Sub ClearErrorMessages()
|
||||
Set pErrorMessages = New Collection
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ExtractBom
|
||||
' 功能: 根据产品条件提取BOM
|
||||
' 参数: productConditions - 产品条件字典
|
||||
' 返回: Collection - 匹配的BOM项集合
|
||||
'=====================================================================
|
||||
Public Function ExtractBom(productConditions As Object) As Collection
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' 清空结果
|
||||
Set pMatchedItems = New Collection
|
||||
Set pRequiredCategories = New Collection
|
||||
Set pErrorMessages = New Collection
|
||||
|
||||
' 第一步:确定需要的类别
|
||||
DetermineRequiredCategories productConditions
|
||||
|
||||
' 第二步:匹配物料
|
||||
MatchItems productConditions
|
||||
|
||||
' 第三步:应用总成逻辑(父类别优先)
|
||||
ApplyAssemblyLogic
|
||||
|
||||
' 第四步:验证结果
|
||||
ValidateResult
|
||||
|
||||
Set ExtractBom = pMatchedItems
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
pErrorMessages.Add "提取BOM异常: " & Err.Description
|
||||
Set ExtractBom = pMatchedItems
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: DetermineRequiredCategories
|
||||
' 功能: 确定需要的类别
|
||||
' 参数: productConditions - 产品条件字典
|
||||
'=====================================================================
|
||||
Private Sub DetermineRequiredCategories(productConditions As Object)
|
||||
Dim item As BomItem
|
||||
Dim uniqueCategories As Object
|
||||
Set uniqueCategories = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 遍历所有有效物料,获取唯一类别
|
||||
For Each item In pAllItems
|
||||
If item.IsValidItem Then
|
||||
' 检查是否在排除列表中
|
||||
Dim isExcluded As Boolean
|
||||
isExcluded = False
|
||||
Dim excludeCat As Variant
|
||||
For Each excludeCat In pExcludeCategories
|
||||
If item.category = CStr(excludeCat) Then
|
||||
isExcluded = True
|
||||
Exit For
|
||||
End If
|
||||
Next excludeCat
|
||||
|
||||
' 如果不在排除列表中,继续处理
|
||||
If Not isExcluded Then
|
||||
' 检查类别选用条件
|
||||
Dim categoryRequired As Boolean
|
||||
If Trim(item.CategoryCondition) = "" Then
|
||||
' 无条件,必需类别
|
||||
categoryRequired = True
|
||||
Else
|
||||
' 有条件,评估条件
|
||||
categoryRequired = pConditionEvaluator.Evaluate(item.CategoryCondition, productConditions)
|
||||
End If
|
||||
|
||||
If categoryRequired Then
|
||||
If Not uniqueCategories.Exists(item.category) Then
|
||||
uniqueCategories.Add item.category, True
|
||||
pRequiredCategories.Add item.category
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next item
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: MatchItems
|
||||
' 功能: 匹配物料
|
||||
' 修改说明: 当未匹配到物料(Count=0)时不再立即报错,而是留给 ValidateResult
|
||||
' 进行综合判断(因为可能存在父子覆盖或散件满足的情况)。
|
||||
'=====================================================================
|
||||
Private Sub MatchItems(productConditions As Object)
|
||||
Dim item As BomItem
|
||||
Dim category As Variant
|
||||
|
||||
' 遍历每个需要的类别
|
||||
For Each category In pRequiredCategories
|
||||
Dim categoryMatches As Collection
|
||||
Set categoryMatches = New Collection
|
||||
|
||||
' 查找该类别下所有匹配的物料
|
||||
For Each item In pAllItems
|
||||
If item.category = category Then
|
||||
' 评估选择条件
|
||||
Dim matched As Boolean
|
||||
If Trim(item.SelectCondition) = "" Then
|
||||
' 无选择条件,无条件匹配
|
||||
matched = True
|
||||
Else
|
||||
' 有选择条件,评估
|
||||
matched = pConditionEvaluator.Evaluate(item.SelectCondition, productConditions)
|
||||
End If
|
||||
|
||||
If matched Then
|
||||
item.IsMatched = True
|
||||
categoryMatches.Add item
|
||||
End If
|
||||
End If
|
||||
Next item
|
||||
|
||||
' 检查匹配结果
|
||||
If categoryMatches.count = 0 Then
|
||||
' ---------------------------------------------------------
|
||||
' CHANGE: 这里不再立即报错
|
||||
' 理由: 未匹配到可能是正常的(例如:父类别缺失但子类别齐全,或者子类别被父类别覆盖)
|
||||
' 具体的缺失检查移交到 ValidateResult 方法中统一处理
|
||||
' ---------------------------------------------------------
|
||||
ElseIf categoryMatches.count = 1 Then
|
||||
' 正常:匹配到1条
|
||||
pMatchedItems.Add categoryMatches(1)
|
||||
Else
|
||||
' 异常:匹配到多条 (这个依然需要报错,因为这是数据源的不确定性错误)
|
||||
Dim multiMsg As String
|
||||
multiMsg = "类别[" & category & "]匹配到多条物料(" & categoryMatches.count & "条)"
|
||||
pErrorMessages.Add multiMsg
|
||||
|
||||
' 临时处理:输出所有匹配的
|
||||
Dim tempItem As BomItem
|
||||
For Each tempItem In categoryMatches
|
||||
tempItem.MatchError = multiMsg
|
||||
pMatchedItems.Add tempItem
|
||||
Next tempItem
|
||||
End If
|
||||
Next category
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ApplyAssemblyLogic
|
||||
' 功能: 应用总成逻辑(父类别优先)
|
||||
' 修改说明: 重构了算法,解决了以下问题:
|
||||
' 1. 当某类别匹配到多条物料时,能够保留所有匹配项,而不是只输出第一条。
|
||||
' 2. 解决了因列表顺序不同导致子类别可能未被正确覆盖的潜在隐患。
|
||||
'=====================================================================
|
||||
Private Sub ApplyAssemblyLogic()
|
||||
' 1. 构建父类别->子类别映射
|
||||
Dim parentToChildren As Object
|
||||
Set parentToChildren = CreateObject("Scripting.Dictionary")
|
||||
Dim parentCat As Variant
|
||||
Dim childCat As Variant
|
||||
Dim key As Variant
|
||||
For Each key In pCategoryHierarchy.Keys
|
||||
|
||||
|
||||
childCat = CStr(key)
|
||||
parentCat = pCategoryHierarchy(key)
|
||||
|
||||
If Not parentToChildren.Exists(parentCat) Then
|
||||
Set parentToChildren(parentCat) = CreateObject("Scripting.Dictionary")
|
||||
End If
|
||||
parentToChildren(parentCat)(childCat) = True
|
||||
Next key
|
||||
|
||||
' 2. 统计每个类别的匹配数量
|
||||
Dim categoryCounts As Object
|
||||
Set categoryCounts = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim item As BomItem
|
||||
For Each item In pMatchedItems
|
||||
If Not categoryCounts.Exists(item.category) Then
|
||||
categoryCounts(item.category) = 0
|
||||
End If
|
||||
categoryCounts(item.category) = categoryCounts(item.category) + 1
|
||||
Next item
|
||||
|
||||
' 3. 识别符合"总成优先"条件的父类别
|
||||
' 定义:如果父类别有且仅有1条匹配,且其所有子类别都有匹配,则视为满足总成逻辑
|
||||
Dim coveredCategories As Object
|
||||
Set coveredCategories = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim satisfiedParentItems As Collection
|
||||
Set satisfiedParentItems = New Collection
|
||||
|
||||
For Each parentCat In parentToChildren.Keys
|
||||
' 只有当该父类别确实有匹配物料时才进行检查
|
||||
If categoryCounts.Exists(parentCat) Then
|
||||
' 条件1: 父类别只匹配到1条 (如果匹配多条,存在歧义,不应用覆盖逻辑,而是全部输出以供排查)
|
||||
If categoryCounts(parentCat) = 1 Then
|
||||
' 条件2: 所有子类别都匹配到(至少1条)
|
||||
Dim childrenMatched As Boolean
|
||||
childrenMatched = True
|
||||
|
||||
For Each childCat In parentToChildren(parentCat).Keys
|
||||
If Not categoryCounts.Exists(childCat) Then
|
||||
childrenMatched = False
|
||||
Exit For
|
||||
End If
|
||||
Next childCat
|
||||
|
||||
If childrenMatched Then
|
||||
' 满足总成条件: 找到那个父类别项
|
||||
Dim pItem As BomItem
|
||||
For Each item In pMatchedItems
|
||||
If item.category = parentCat Then
|
||||
satisfiedParentItems.Add item
|
||||
Exit For
|
||||
End If
|
||||
Next item
|
||||
|
||||
' 标记覆盖的类别(父类别自己和所有子类别都标记为已处理)
|
||||
' 这样做的目的是:在步骤4中,我们会先添加 satisfiedParentItems,
|
||||
' 然后跳过 coveredCategories 中的项,从而实现"父类覆盖子类"且"父类不重复添加"
|
||||
coveredCategories(parentCat) = True
|
||||
For Each childCat In parentToChildren(parentCat).Keys
|
||||
coveredCategories(childCat) = True
|
||||
Next childCat
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next parentCat
|
||||
|
||||
' 4. 构建新的结果集
|
||||
Dim newMatchedItems As Collection
|
||||
Set newMatchedItems = New Collection
|
||||
|
||||
' 4.1 先添加满足条件的父类别项 (总成)
|
||||
For Each item In satisfiedParentItems
|
||||
newMatchedItems.Add item
|
||||
Next item
|
||||
|
||||
' 4.2 再添加未被覆盖的其他项 (散件 或 有问题的多条匹配项)
|
||||
For Each item In pMatchedItems
|
||||
' 如果该项所属的类别不在"被覆盖"列表中,则保留
|
||||
' 关键点:这里不再去重!如果同一个Category有5条记录,这5条都会因为不在coveredCategories中而被添加
|
||||
If Not coveredCategories.Exists(item.category) Then
|
||||
newMatchedItems.Add item
|
||||
End If
|
||||
Next item
|
||||
|
||||
' 更新结果
|
||||
Set pMatchedItems = newMatchedItems
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ValidateResult
|
||||
' 功能: 验证提取结果
|
||||
' 修改说明: 实现了双向覆盖检查:
|
||||
' 1. 子类别缺失,但父类别存在 -> 视为正常 (总成优先)
|
||||
' 2. 父类别缺失,但所有必需子类别都存在 -> 视为正常 (散件满足)
|
||||
'=====================================================================
|
||||
Private Sub ValidateResult()
|
||||
' 检查所有需要的类别是否都匹配
|
||||
Dim category As Variant
|
||||
Dim categoryMatched As Object
|
||||
Set categoryMatched = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 统计已匹配的类别
|
||||
Dim item As BomItem
|
||||
For Each item In pMatchedItems
|
||||
If Not categoryMatched.Exists(item.category) Then
|
||||
categoryMatched(item.category) = 0
|
||||
End If
|
||||
categoryMatched(item.category) = categoryMatched(item.category) + 1
|
||||
Next item
|
||||
|
||||
' 检查未匹配的类别
|
||||
For Each category In pRequiredCategories
|
||||
' 如果结果集中不存在该必需类别
|
||||
If Not categoryMatched.Exists(category) Then
|
||||
|
||||
Dim isResolved As Boolean
|
||||
isResolved = False
|
||||
|
||||
' ---------------------------------------------------------
|
||||
' 检查 1: 被父类别覆盖 (总成逻辑)
|
||||
' 场景: 匹配到了部件(父),自动隐藏了接头(子),接头不应报错
|
||||
' ---------------------------------------------------------
|
||||
If pCategoryHierarchy.Exists(category) Then
|
||||
Dim parentCat As String
|
||||
parentCat = pCategoryHierarchy(category)
|
||||
|
||||
If categoryMatched.Exists(parentCat) Then
|
||||
isResolved = True
|
||||
End If
|
||||
End If
|
||||
|
||||
' ---------------------------------------------------------
|
||||
' 检查 2: 被子类别覆盖 (散件逻辑)
|
||||
' 场景: 部件(父)没匹配到(或被移除),但接头(子)和弹性元件(子)都齐了,部件不应报错
|
||||
' ---------------------------------------------------------
|
||||
If Not isResolved Then
|
||||
Dim hasRequiredChildren As Boolean
|
||||
Dim allChildrenMatched As Boolean
|
||||
|
||||
hasRequiredChildren = False
|
||||
allChildrenMatched = True
|
||||
|
||||
' 遍历所有"必需"的类别,寻找当前缺失category的子类别
|
||||
Dim reqCat As Variant
|
||||
For Each reqCat In pRequiredCategories
|
||||
' 如果 reqCat 是当前 category 的子类别
|
||||
If pCategoryHierarchy.Exists(reqCat) Then
|
||||
If pCategoryHierarchy(reqCat) = category Then
|
||||
hasRequiredChildren = True
|
||||
|
||||
' 检查这个子类别是否在结果集中
|
||||
If Not categoryMatched.Exists(reqCat) Then
|
||||
allChildrenMatched = False
|
||||
Exit For ' 只要缺一个子类别,父类别就无法被视为"满足"
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next reqCat
|
||||
|
||||
' 只有当存在必需子类别,且它们全都匹配时,才算通过
|
||||
If hasRequiredChildren And allChildrenMatched Then
|
||||
isResolved = True
|
||||
End If
|
||||
End If
|
||||
|
||||
' ---------------------------------------------------------
|
||||
' 最终判断
|
||||
' ---------------------------------------------------------
|
||||
If Not isResolved Then
|
||||
pErrorMessages.Add "必需类别[" & category & "]未匹配"
|
||||
End If
|
||||
|
||||
End If
|
||||
Next category
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: GetErrorMessages
|
||||
' 功能: 获取错误信息集合
|
||||
' 返回: Collection
|
||||
'=====================================================================
|
||||
Public Function GetErrorMessages() As Collection
|
||||
Set GetErrorMessages = pErrorMessages
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: GetErrorSummary
|
||||
' 功能: 获取错误信息摘要
|
||||
' 返回: String
|
||||
'=====================================================================
|
||||
Public Function GetErrorSummary() As String
|
||||
If pErrorMessages.count = 0 Then
|
||||
GetErrorSummary = ""
|
||||
Else
|
||||
Dim result As String
|
||||
Dim msg As Variant
|
||||
For Each msg In pErrorMessages
|
||||
result = result & CStr(msg) & "; "
|
||||
Next msg
|
||||
GetErrorSummary = result
|
||||
End If
|
||||
End Function
|
||||
'=====================================================================
|
||||
' 方法: GetAllItems
|
||||
' 功能: 获取所有加载的BOM物料 (暴露数据池供异常分析溯源算法使用)
|
||||
' 返回: Collection
|
||||
'=====================================================================
|
||||
Public Function GetAllItems() As Collection
|
||||
Set GetAllItems = pAllItems
|
||||
End Function
|
||||
89
VBA/ClassModules/BomItem.cls
Normal file
89
VBA/ClassModules/BomItem.cls
Normal file
@@ -0,0 +1,89 @@
|
||||
'=====================================================================
|
||||
' 类名:BomItem
|
||||
' 功能:BOM 物料项数据模型
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
' 物料属性
|
||||
Public RowNumber As Long ' 行号
|
||||
Public Module As String ' 模块
|
||||
Public code As String ' 代号
|
||||
Public Name As String ' 名称
|
||||
Public Quantity As Double ' 数量
|
||||
Public SelectCondition As String ' 选择条件
|
||||
Public Remark As String ' 备注
|
||||
Public category As String ' 类别
|
||||
Public ParentCategory As String ' 上层类别
|
||||
Public CategoryCondition As String ' 类别选用条件
|
||||
Public Code66 As String ' 66 代码
|
||||
Public BipRowNumberBase As Long ' BIP 行号基数
|
||||
|
||||
' 匹配状态
|
||||
Public IsMatched As Boolean ' 是否匹配
|
||||
Public MatchError As String ' 匹配错误信息
|
||||
|
||||
'=====================================================================
|
||||
' 方法:Class_Initialize
|
||||
' 功能:初始化类
|
||||
'=====================================================================
|
||||
Private Sub Class_Initialize()
|
||||
IsMatched = False
|
||||
MatchError = ""
|
||||
BipRowNumberBase = 7000
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法:LoadFromRow
|
||||
' 功能:从工作表行加载数据
|
||||
' 参数:ws - 工作表对象
|
||||
' row - 行号
|
||||
'=====================================================================
|
||||
Public Sub LoadFromRow(ws As Worksheet, row As Long)
|
||||
On Error Resume Next
|
||||
|
||||
Me.RowNumber = CLng(ws.Cells(row, 1).value) ' A 列:行号
|
||||
Me.Module = CStr(ws.Cells(row, 2).value) ' B 列:模块
|
||||
Me.code = CStr(ws.Cells(row, 3).value) ' C 列:代号
|
||||
Me.Name = CStr(ws.Cells(row, 4).value) ' D 列:名称
|
||||
Me.Quantity = CDbl(ws.Cells(row, 5).value) ' E 列:数量
|
||||
Me.SelectCondition = CStr(ws.Cells(row, 6).value) ' F 列:选择条件
|
||||
Me.Remark = CStr(ws.Cells(row, 7).value) ' G 列:备注
|
||||
Me.category = CStr(ws.Cells(row, 8).value) ' H 列:类别
|
||||
Me.ParentCategory = CStr(ws.Cells(row, 9).value) ' I 列:上层类别
|
||||
Me.CategoryCondition = CStr(ws.Cells(row, 10).value) ' J 列:类别选用条件
|
||||
Me.Code66 = CStr(ws.Cells(row, 11).value) ' K 列:66 代码
|
||||
Me.BipRowNumberBase = CLng(ws.Cells(row, 12).value) ' L 列:BIP 行号基数
|
||||
|
||||
On Error GoTo 0
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法:IsValidItem
|
||||
' 功能:判断是否为有效物料 (类别字段不为空)
|
||||
' 返回:Boolean
|
||||
'=====================================================================
|
||||
Public Function IsValidItem() As Boolean
|
||||
IsValidItem = (Trim(Me.category) <> "")
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法:HasParentCategory
|
||||
' 功能:判断是否有父类别
|
||||
' 返回:Boolean
|
||||
'=====================================================================
|
||||
Public Function HasParentCategory() As Boolean
|
||||
HasParentCategory = (Trim(Me.ParentCategory) <> "")
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法:ToString
|
||||
' 功能:转换为字符串描述
|
||||
' 返回:String
|
||||
'=====================================================================
|
||||
Public Function ToString() As String
|
||||
ToString = "行号:" & Me.RowNumber & _
|
||||
" | 类别:" & Me.category & _
|
||||
" | 代号:" & Me.code & _
|
||||
" | 名称:" & Me.Name
|
||||
End Function
|
||||
224
VBA/ClassModules/ConditionEvaluator.cls
Normal file
224
VBA/ClassModules/ConditionEvaluator.cls
Normal file
@@ -0,0 +1,224 @@
|
||||
'=====================================================================
|
||||
' 类名: ConditionEvaluator
|
||||
' 功能: 解析和评估条件表达式
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 方法: Evaluate
|
||||
' 功能: 评估条件表达式
|
||||
' 参数: expression - 条件表达式字符串
|
||||
' productConditions - 产品条件字典(Dictionary对象)
|
||||
' 返回: Boolean - True表示条件满足,False表示不满足
|
||||
' 说明: 支持AND、OR、!=运算符和括号嵌套
|
||||
' 特殊规则:如果表达式中要求!=某值,而产品条件中不存在该变量,视为满足条件
|
||||
'=====================================================================
|
||||
Public Function Evaluate(expression As String, productConditions As Object) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' 空条件视为满足
|
||||
If Trim(expression) = "" Then
|
||||
Evaluate = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 递归解析表达式
|
||||
Evaluate = EvaluateExpression(Trim(expression), productConditions)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
' 解析错误时返回False
|
||||
Evaluate = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: EvaluateExpression
|
||||
' 功能: 递归评估表达式
|
||||
' 参数: expr - 表达式
|
||||
' conditions - 条件字典
|
||||
' 返回: Boolean
|
||||
'=====================================================================
|
||||
Private Function EvaluateExpression(expr As String, conditions As Object) As Boolean
|
||||
expr = Trim(expr)
|
||||
|
||||
' 处理最外层括号
|
||||
If Left(expr, 1) = "(" And Right(expr, 1) = ")" Then
|
||||
If IsMatchedParentheses(expr) Then
|
||||
expr = Mid(expr, 2, Len(expr) - 2)
|
||||
expr = Trim(expr)
|
||||
End If
|
||||
End If
|
||||
|
||||
' 处理OR运算符(优先级最低)
|
||||
Dim orResult As Variant
|
||||
orResult = SplitByOperator(expr, " OR ", conditions)
|
||||
If Not IsEmpty(orResult) Then
|
||||
EvaluateExpression = orResult
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 处理AND运算符
|
||||
Dim andResult As Variant
|
||||
andResult = SplitByOperator(expr, " AND ", conditions)
|
||||
If Not IsEmpty(andResult) Then
|
||||
EvaluateExpression = andResult
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 处理单个条件
|
||||
EvaluateExpression = EvaluateSingleCondition(expr, conditions)
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: SplitByOperator
|
||||
' 功能: 按指定运算符分割并评估表达式
|
||||
' 参数: expr - 表达式
|
||||
' operator - 运算符(" OR " 或 " AND ")
|
||||
' conditions - 条件字典
|
||||
' 返回: Variant - 评估结果或Empty
|
||||
'=====================================================================
|
||||
Private Function SplitByOperator(expr As String, operator As String, conditions As Object) As Variant
|
||||
Dim pos As Long
|
||||
Dim leftPart As String
|
||||
Dim rightPart As String
|
||||
Dim depth As Long
|
||||
Dim i As Long
|
||||
Dim char As String
|
||||
|
||||
' 寻找不在括号内的运算符
|
||||
depth = 0
|
||||
For i = 1 To Len(expr) - Len(operator) + 1
|
||||
char = Mid(expr, i, 1)
|
||||
|
||||
If char = "(" Then
|
||||
depth = depth + 1
|
||||
ElseIf char = ")" Then
|
||||
depth = depth - 1
|
||||
ElseIf depth = 0 Then
|
||||
' 检查是否匹配运算符
|
||||
If Mid(expr, i, Len(operator)) = operator Then
|
||||
leftPart = Trim(Left(expr, i - 1))
|
||||
rightPart = Trim(Mid(expr, i + Len(operator)))
|
||||
|
||||
' 根据运算符类型评估
|
||||
If operator = " OR " Then
|
||||
SplitByOperator = EvaluateExpression(leftPart, conditions) Or _
|
||||
EvaluateExpression(rightPart, conditions)
|
||||
ElseIf operator = " AND " Then
|
||||
SplitByOperator = EvaluateExpression(leftPart, conditions) And _
|
||||
EvaluateExpression(rightPart, conditions)
|
||||
End If
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 未找到运算符
|
||||
SplitByOperator = Empty
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: EvaluateSingleCondition
|
||||
' 功能: 评估单个条件(如 azxs=A0 或 azxs!=AH)
|
||||
' 参数: condition - 单个条件字符串
|
||||
' conditions - 条件字典
|
||||
' 返回: Boolean
|
||||
'=====================================================================
|
||||
Private Function EvaluateSingleCondition(condition As String, conditions As Object) As Boolean
|
||||
Dim varName As String
|
||||
Dim operator As String
|
||||
Dim value As String
|
||||
Dim actualValue As String
|
||||
|
||||
condition = Trim(condition)
|
||||
|
||||
' 检查!=运算符
|
||||
If InStr(condition, "!=") > 0 Then
|
||||
Dim parts() As String
|
||||
parts = Split(condition, "!=")
|
||||
If UBound(parts) >= 1 Then
|
||||
varName = Trim(parts(0))
|
||||
value = Trim(parts(1))
|
||||
|
||||
' 特殊规则:如果产品条件中不存在该变量,视为满足!=条件
|
||||
If Not conditions.Exists(varName) Then
|
||||
EvaluateSingleCondition = True
|
||||
Else
|
||||
actualValue = conditions(varName)
|
||||
|
||||
' fjgn字段特殊处理(多值匹配)
|
||||
If varName = "fjgn" Then
|
||||
' fjgn!=N1:检查actualValue中是否不包含value
|
||||
EvaluateSingleCondition = (InStr(actualValue, value) = 0)
|
||||
Else
|
||||
' 其他字段使用精确匹配
|
||||
EvaluateSingleCondition = (actualValue <> value)
|
||||
End If
|
||||
End If
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
|
||||
' 检查=运算符
|
||||
If InStr(condition, "=") > 0 Then
|
||||
Dim eqParts() As String
|
||||
eqParts = Split(condition, "=")
|
||||
If UBound(eqParts) >= 1 Then
|
||||
varName = Trim(eqParts(0))
|
||||
value = Trim(eqParts(1))
|
||||
|
||||
If Not conditions.Exists(varName) Then
|
||||
EvaluateSingleCondition = False
|
||||
Else
|
||||
actualValue = conditions(varName)
|
||||
|
||||
' fjgn字段特殊处理(多值匹配)
|
||||
If varName = "fjgn" Then
|
||||
' fjgn=N1:检查actualValue中是否包含value
|
||||
EvaluateSingleCondition = (InStr(actualValue, value) > 0)
|
||||
Else
|
||||
' 其他字段使用精确匹配
|
||||
EvaluateSingleCondition = (actualValue = value)
|
||||
End If
|
||||
End If
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
|
||||
' 无法解析的条件返回False
|
||||
EvaluateSingleCondition = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: IsMatchedParentheses
|
||||
' 功能: 检查字符串最外层括号是否匹配
|
||||
' 参数: str - 字符串
|
||||
' 返回: Boolean
|
||||
'=====================================================================
|
||||
Private Function IsMatchedParentheses(str As String) As Boolean
|
||||
If Left(str, 1) <> "(" Or Right(str, 1) <> ")" Then
|
||||
IsMatchedParentheses = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim depth As Long
|
||||
Dim i As Long
|
||||
|
||||
depth = 0
|
||||
For i = 1 To Len(str)
|
||||
If Mid(str, i, 1) = "(" Then
|
||||
depth = depth + 1
|
||||
ElseIf Mid(str, i, 1) = ")" Then
|
||||
depth = depth - 1
|
||||
End If
|
||||
|
||||
' 如果在中间某处深度归零,说明最外层括号不匹配
|
||||
If depth = 0 And i < Len(str) Then
|
||||
IsMatchedParentheses = False
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
IsMatchedParentheses = (depth = 0)
|
||||
End Function
|
||||
303
VBA/ClassModules/ProductModelParser.cls
Normal file
303
VBA/ClassModules/ProductModelParser.cls
Normal file
@@ -0,0 +1,303 @@
|
||||
'=====================================================================
|
||||
' 类名: ProductModelParser
|
||||
' 功能: 解析产品型号并提取物料选择条件
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
Private pFullModel As String
|
||||
Private pHeaderModel As String
|
||||
Private pConditions As Object ' Dictionary
|
||||
Private pErrorMessage As String
|
||||
|
||||
'=====================================================================
|
||||
' 属性: FullModel - 完整产品型号
|
||||
'=====================================================================
|
||||
Public Property Get FullModel() As String
|
||||
FullModel = pFullModel
|
||||
End Property
|
||||
|
||||
Public Property Let FullModel(value As String)
|
||||
pFullModel = value
|
||||
End Property
|
||||
|
||||
'=====================================================================
|
||||
' 属性: HeaderModel - 表头型号
|
||||
'=====================================================================
|
||||
Public Property Get HeaderModel() As String
|
||||
HeaderModel = pHeaderModel
|
||||
End Property
|
||||
|
||||
'=====================================================================
|
||||
' 属性: Conditions - 提取的条件字典
|
||||
'=====================================================================
|
||||
Public Property Get conditions() As Object
|
||||
Set conditions = pConditions
|
||||
End Property
|
||||
|
||||
'=====================================================================
|
||||
' 属性: ErrorMessage - 错误信息
|
||||
'=====================================================================
|
||||
Public Property Get ErrorMessage() As String
|
||||
ErrorMessage = pErrorMessage
|
||||
End Property
|
||||
|
||||
'=====================================================================
|
||||
' 方法: Class_Initialize
|
||||
' 功能: 初始化类
|
||||
'=====================================================================
|
||||
Private Sub Class_Initialize()
|
||||
Set pConditions = CreateObject("Scripting.Dictionary")
|
||||
pErrorMessage = ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 方法: Parse
|
||||
' 功能: 解析产品型号
|
||||
' 参数: modelString - 完整产品型号字符串
|
||||
' 返回: Boolean - True表示解析成功,False表示失败
|
||||
'=====================================================================
|
||||
Public Function Parse(modelString As String) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
pFullModel = Trim(modelString)
|
||||
pConditions.RemoveAll
|
||||
pErrorMessage = ""
|
||||
|
||||
' 提取表头部分(|之前的部分)
|
||||
Dim parts() As String
|
||||
parts = Split(pFullModel, "|")
|
||||
|
||||
If UBound(parts) < 0 Then
|
||||
pErrorMessage = "型号格式错误:缺少表头部分"
|
||||
Parse = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
pHeaderModel = Trim(parts(0))
|
||||
|
||||
' 解析表头型号
|
||||
If Not ParseHeader() Then
|
||||
Parse = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Parse = True
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
pErrorMessage = "解析异常: " & Err.Description
|
||||
Parse = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ParseHeader
|
||||
' 功能: 解析表头型号结构
|
||||
' 返回: Boolean - True表示解析成功
|
||||
' 说明: 表头结构 [型号]-[公称外径].[安装形式].[壳体形式].[过程连接&接液材质].[量程范围].[仪表特性]
|
||||
'=====================================================================
|
||||
Private Function ParseHeader() As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' 分离型号和其余部分
|
||||
Dim dashParts() As String
|
||||
dashParts = Split(pHeaderModel, "-")
|
||||
|
||||
If UBound(dashParts) < 1 Then
|
||||
pErrorMessage = "表头格式错误:缺少'-'分隔符"
|
||||
ParseHeader = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 分离各个字段(用.分隔)
|
||||
Dim dotParts() As String
|
||||
dotParts = Split(dashParts(1), ".")
|
||||
|
||||
' 验证结构完整性:至少需要5个部分(公称外径、安装形式、壳体形式、过程连接、量程)
|
||||
If UBound(dotParts) < 4 Then
|
||||
pErrorMessage = "表头结构不完整:缺少必要字段"
|
||||
ParseHeader = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 提取各个条件
|
||||
' 安装形式 - 第2个位置(索引1)
|
||||
Dim azxs As String
|
||||
azxs = Trim(dotParts(1))
|
||||
pConditions.Add "azxs", azxs
|
||||
|
||||
' 表壳形式 - 第3个位置(索引2)
|
||||
Dim bkxs As String
|
||||
bkxs = Trim(dotParts(2))
|
||||
pConditions.Add "bkxs", bkxs
|
||||
|
||||
' 过程连接和接液材质 - 第4个位置(索引3)
|
||||
Dim connectionCode As String
|
||||
connectionCode = Trim(dotParts(3))
|
||||
|
||||
Dim gclj As String
|
||||
Dim jycz As String
|
||||
If Not ExtractConnectionAndMaterial(connectionCode, gclj, jycz) Then
|
||||
ParseHeader = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
pConditions.Add "gclj", gclj
|
||||
pConditions.Add "jycz", jycz
|
||||
|
||||
' 量程范围 - 第5个位置(索引4)
|
||||
Dim lcfw As String
|
||||
lcfw = Trim(dotParts(4))
|
||||
pConditions.Add "lcfw", lcfw
|
||||
|
||||
' 仪表特性 - 第6个位置(索引5)及之后的所有部分
|
||||
' 因为仪表特性中可能包含.分隔符(如N3.N2.Y3),所以需要合并从索引5开始的所有部分
|
||||
Dim fjgn As String
|
||||
If UBound(dotParts) >= 5 Then
|
||||
Dim instrumentFeature As String
|
||||
Dim i As Long
|
||||
instrumentFeature = ""
|
||||
|
||||
' 合并从索引5开始的所有部分,用.连接
|
||||
For i = 5 To UBound(dotParts)
|
||||
If instrumentFeature = "" Then
|
||||
instrumentFeature = dotParts(i)
|
||||
Else
|
||||
instrumentFeature = instrumentFeature & "." & dotParts(i)
|
||||
End If
|
||||
Next i
|
||||
|
||||
instrumentFeature = Trim(instrumentFeature)
|
||||
fjgn = ExtractAdditionalFeatures(instrumentFeature)
|
||||
Else
|
||||
' 如果没有仪表特性字段,fjgn为空
|
||||
fjgn = ""
|
||||
End If
|
||||
pConditions.Add "fjgn", fjgn
|
||||
|
||||
ParseHeader = True
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
pErrorMessage = "解析表头异常: " & Err.Description
|
||||
ParseHeader = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ExtractConnectionAndMaterial
|
||||
' 功能: 从过程连接代码中提取过程连接和接液材质
|
||||
' 参数: code - 过程连接代码(如M203)
|
||||
' outConnection - 输出:过程连接(如M20)
|
||||
' outMaterial - 输出:接液材质(如3)
|
||||
' 返回: Boolean - True表示提取成功
|
||||
' 说明: 材质代码为最后一位数字,其余为螺纹代码
|
||||
'=====================================================================
|
||||
Private Function ExtractConnectionAndMaterial(code As String, _
|
||||
ByRef outConnection As String, _
|
||||
ByRef outMaterial As String) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If Len(code) < 2 Then
|
||||
pErrorMessage = "过程连接代码格式错误:长度不足"
|
||||
ExtractConnectionAndMaterial = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 材质代码是最后一位数字
|
||||
Dim lastChar As String
|
||||
lastChar = Right(code, 1)
|
||||
|
||||
' 验证最后一位是否为数字
|
||||
If Not IsNumeric(lastChar) Then
|
||||
pErrorMessage = "过程连接代码格式错误:最后一位不是数字"
|
||||
ExtractConnectionAndMaterial = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
outMaterial = lastChar
|
||||
outConnection = Left(code, Len(code) - 1)
|
||||
|
||||
ExtractConnectionAndMaterial = True
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
pErrorMessage = "提取过程连接和材质异常: " & Err.Description
|
||||
ExtractConnectionAndMaterial = False
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: GetConditionValue
|
||||
' 功能: 获取指定条件的值
|
||||
' 参数: conditionName - 条件名称
|
||||
' 返回: String - 条件值,如果不存在返回空字符串
|
||||
'=====================================================================
|
||||
Public Function GetConditionValue(conditionName As String) As String
|
||||
If pConditions.Exists(conditionName) Then
|
||||
GetConditionValue = pConditions(conditionName)
|
||||
Else
|
||||
GetConditionValue = ""
|
||||
End If
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: GetAllConditions
|
||||
' 功能: 获取所有条件的描述文本
|
||||
' 返回: String - 条件描述文本
|
||||
'=====================================================================
|
||||
Public Function GetAllConditions() As String
|
||||
Dim result As String
|
||||
Dim key As Variant
|
||||
|
||||
result = ""
|
||||
For Each key In pConditions.Keys
|
||||
result = result & key & "=" & pConditions(key) & "; "
|
||||
Next key
|
||||
|
||||
GetAllConditions = result
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 方法: ExtractAdditionalFeatures
|
||||
' 功能: 从仪表特性中提取附加功能
|
||||
' 参数: instrumentFeature - 仪表特性字符串(如"N2,N3.Y3"或"Y3")
|
||||
' 返回: String - 附加功能字符串,多个功能用逗号分隔
|
||||
' 说明:
|
||||
' 1. 识别并去除充油类型(位于最后,格式为Y+一位数字)
|
||||
' 2. 统一分隔符处理(将.替换为,)
|
||||
' 3. 去除可能的后缀分隔符
|
||||
'=====================================================================
|
||||
Private Function ExtractAdditionalFeatures(instrumentFeature As String) As String
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim result As String
|
||||
result = Trim(instrumentFeature)
|
||||
|
||||
' 1. 检查是否以Y+数字结尾(充油类型)
|
||||
If Len(result) >= 2 Then
|
||||
Dim lastTwoChars As String
|
||||
lastTwoChars = Right(result, 2)
|
||||
|
||||
' 检查最后两位是否为Y+数字
|
||||
If UCase(Left(lastTwoChars, 1)) = "Y" And IsNumeric(Right(lastTwoChars, 1)) Then
|
||||
' 去掉充油类型
|
||||
result = Left(result, Len(result) - 2)
|
||||
result = Trim(result)
|
||||
End If
|
||||
End If
|
||||
|
||||
' 2. 处理可能的分隔符(,或.)
|
||||
' 将可能的.替换为,,统一处理
|
||||
result = Replace(result, ".", ",")
|
||||
|
||||
' 3. 去除可能的后缀分隔符
|
||||
If Len(result) > 0 And Right(result, 1) = "," Then
|
||||
result = Left(result, Len(result) - 1)
|
||||
End If
|
||||
|
||||
ExtractAdditionalFeatures = Trim(result)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
' 如果解析出错,返回空字符串
|
||||
ExtractAdditionalFeatures = ""
|
||||
End Function
|
||||
32
VBA/DocumentModules/Sheet9.cls
Normal file
32
VBA/DocumentModules/Sheet9.cls
Normal file
@@ -0,0 +1,32 @@
|
||||
'=====================================================================
|
||||
' 主按钮点击事件
|
||||
' 功能: 执行BOM提取和BIP上传
|
||||
'=====================================================================
|
||||
Private Sub CommandButton1_Click()
|
||||
Call ProcessProductModels
|
||||
Call ValidateOrderMaterials
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 部件库存核对按钮点击事件
|
||||
' 功能: 执行部件库存核对,标记库存不足的订单
|
||||
'=====================================================================
|
||||
Private Sub CommandButton2_Click()
|
||||
Call CheckComponentInventory
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 数据提取按钮点击事件
|
||||
' 功能:基于总排号查询相关字段数据
|
||||
'=====================================================================
|
||||
Private Sub CommandButton3_Click()
|
||||
Call FetchDataFromAccess
|
||||
End Sub
|
||||
|
||||
Private Sub CommandButton4_Click()
|
||||
ThisWorkbook.Worksheets("产品订单").Range("A2:G10000").ClearContents
|
||||
End Sub
|
||||
|
||||
Private Sub CommandButton5_Click()
|
||||
Call ProcessOrdersToBIP
|
||||
End Sub
|
||||
142
VBA/Modules/AGENTS.md
Normal file
142
VBA/Modules/AGENTS.md
Normal file
@@ -0,0 +1,142 @@
|
||||
# Modules Knowledge Base
|
||||
|
||||
**Scope:** `VBA/Modules/` - Procedural workflows and entry points
|
||||
|
||||
## OVERVIEW
|
||||
|
||||
4 modules: `MainModule` (main workflow), `BIPUploadModule` (upload results), `ComponentInventoryCheckModule` (inventory check), `TestModule` (testing utilities).
|
||||
|
||||
## STRUCTURE
|
||||
|
||||
```
|
||||
Modules/
|
||||
├── MainModule.bas # Main entry point + orchestration
|
||||
├── BIPUploadModule.bas # Upload to BIP system
|
||||
├── ComponentInventoryCheckModule.bas # Component inventory verification
|
||||
└── TestModule.bas # Testing utilities
|
||||
```
|
||||
|
||||
## WHERE TO LOOK
|
||||
|
||||
| Task | Location | Notes |
|
||||
|------|----------|-------|
|
||||
| Run BOM extraction | `MainModule.bas` | `ProcessProductModels()` |
|
||||
| Upload results | `BIPUploadModule.bas` | BIP system integration |
|
||||
| Check inventory | `ComponentInventoryCheckModule.bas` | Component count verification |
|
||||
| Test parsing | `TestModule.bas` | Unit test utilities |
|
||||
|
||||
## CODE MAP
|
||||
|
||||
### MainModule
|
||||
| Function | Role |
|
||||
|----------|------|
|
||||
| `ProcessProductModels()` | Main entry point, orchestrates workflow |
|
||||
| `ProcessSingleModel()` | Process one model, collect output |
|
||||
| `CreateOutputRowArray()` | Build row data array |
|
||||
| `WriteBatchData()` | Bulk write to worksheet |
|
||||
| `GetInputSheet()` | Get `产品订单` sheet |
|
||||
| `GetBomSheet()` | Get `平台配置清单` sheet |
|
||||
| `CreateOutputSheet()` | Create/reset `BOM 提取结果` |
|
||||
|
||||
### BIPUploadModule
|
||||
| Function | Role |
|
||||
|----------|------|
|
||||
| `UploadToBIP()` | Upload BOM results to BIP system |
|
||||
|
||||
### ComponentInventoryCheckModule
|
||||
| Function | Role |
|
||||
|----------|------|
|
||||
| `CheckComponentInventory()` | Verify component counts match |
|
||||
|
||||
## CONVENTIONS
|
||||
|
||||
### Module Structure
|
||||
```vba
|
||||
Option Explicit
|
||||
|
||||
' Constants
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|..."
|
||||
|
||||
' Public entry points
|
||||
Public Sub MainEntryPoint()
|
||||
On Error GoTo ErrorHandler
|
||||
' Orchestration
|
||||
Exit Sub
|
||||
ErrorHandler:
|
||||
MsgBox "Error: " & Err.Description
|
||||
End Sub
|
||||
|
||||
' Private helpers
|
||||
Private Sub HelperFunction()
|
||||
' Implementation
|
||||
End Sub
|
||||
```
|
||||
|
||||
### Workflow Pattern
|
||||
1. Get input sheet (`产品订单`)
|
||||
2. Get BOM sheet (`平台配置清单`)
|
||||
3. Create output sheet (`BOM 提取结果`)
|
||||
4. Initialize `BomExtractor`, load data
|
||||
5. Loop through models, call `ProcessSingleModel()`
|
||||
6. Batch write results
|
||||
7. Format output, show message
|
||||
|
||||
### Batch Output Pattern
|
||||
```vba
|
||||
' Collect in Collection
|
||||
Dim outputData As Collection
|
||||
Set outputData = New Collection
|
||||
|
||||
' Add arrays
|
||||
outputData.Add CreateOutputRowArray(...)
|
||||
|
||||
' Convert to 2D array
|
||||
ReDim resultData(1 To rowCount, 1 To colCount)
|
||||
' Fill array...
|
||||
|
||||
' Single write
|
||||
ws.Range("A2").Resize(rowCount, colCount).Value = resultData
|
||||
```
|
||||
|
||||
## ANTI-PATTERNS (THIS PROJECT)
|
||||
|
||||
- **NEVER** hardcode sheet names — use `GetInputSheet()`, `GetBomSheet()` helpers
|
||||
- **NEVER** write row-by-row — always batch write via 2D array
|
||||
- **NEVER** skip error handling — all public subs use `On Error GoTo ErrorHandler`
|
||||
- **DO NOT** change output column order — must match header definition in `WriteOutputHeader()`
|
||||
|
||||
## UNIQUE STYLES
|
||||
|
||||
### Condition Config Constant
|
||||
```vba
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
||||
```
|
||||
Format: `code,label|code,label|...` — parsed dynamically for header generation.
|
||||
|
||||
### Component Priority Flag
|
||||
From column E of input sheet:
|
||||
- "是" (or "1", "TRUE") → include all categories
|
||||
- "否" (or "0", "FALSE") → exclude "部件" (components) category
|
||||
|
||||
### Output Row Structure
|
||||
```
|
||||
Column 1: 生产订单号 (order number)
|
||||
Column 2: 产品型号 (full model)
|
||||
Columns 3-8: 6 conditions (azxs, bkxs, gclj, jycz, lcfw, fjgn)
|
||||
Columns 9-16: BOM fields (行号,模块,代号,名称,数量,类别,66 代码,备注)
|
||||
```
|
||||
|
||||
## NOTES
|
||||
|
||||
### Output Sheet Formatting
|
||||
- Row 1: Bold, gray background (RGB 217,217,217), centered
|
||||
- Data starts at row 2
|
||||
- AutoFit columns (commented out in current code)
|
||||
|
||||
### Error Messages
|
||||
- Parse failure: `"解析失败:" + error`
|
||||
- No match: `"未匹配到任何物料"`
|
||||
- Multi-match: `"类别[X] 匹配到多条物料(N 条)"` — outputs all but flags
|
||||
|
||||
### Performance
|
||||
Batch write via `Range.Value = resultData` is 10-100x faster than cell-by-cell writes for large datasets.
|
||||
172
VBA/Modules/AccessDataModule.bas
Normal file
172
VBA/Modules/AccessDataModule.bas
Normal file
@@ -0,0 +1,172 @@
|
||||
'=====================================================================
|
||||
' 模块名: AccessDataModule
|
||||
' 功能: 连接Access数据库,根据[总排号]提取数据并填充到[产品订单]工作表
|
||||
' 特性: [安全极速版] 完美解决筛选状态下全量数组写回导致的错位 Bug
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 配置区域 (请根据你的实际情况修改以下常量)
|
||||
'=====================================================================
|
||||
' Access数据库文件的完整路径
|
||||
Private Const DB_PATH = "\\192.168.110.114\生产进度表\2025年数据\生产合同数据.accdb"
|
||||
' Access中目标数据表的名称
|
||||
Private Const TARGET_TABLE = "26年压力表合同数据"
|
||||
|
||||
'=====================================================================
|
||||
' 过程: FetchDataFromAccess
|
||||
' 功能: 主控程序,执行数据提取和回填逻辑
|
||||
'=====================================================================
|
||||
Public Sub FetchDataFromAccess()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
Dim ws As Worksheet
|
||||
Set ws = GetOrderSheet()
|
||||
If ws Is Nothing Then
|
||||
MsgBox "未找到[产品订单]工作表,请检查工作表名称。", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim lastRow As Long
|
||||
lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row
|
||||
|
||||
If lastRow < 2 Then
|
||||
MsgBox "[产品订单]工作表中没有需要处理的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 1. 获取A列中所有筛选后的(可见)单元格
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = ws.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 2. 仅收集可见行中的总排号
|
||||
Dim cell As Range
|
||||
Dim queueNums As String
|
||||
Dim currentNum As String
|
||||
|
||||
For Each cell In visibleRange
|
||||
currentNum = Trim(cell.value)
|
||||
If currentNum <> "" Then
|
||||
queueNums = queueNums & "'" & currentNum & "',"
|
||||
End If
|
||||
Next cell
|
||||
|
||||
If queueNums = "" Then
|
||||
MsgBox "可见数据中没有找到有效的总排号。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
queueNums = Left(queueNums, Len(queueNums) - 1)
|
||||
|
||||
' 3. 连接Access查询并装入字典 (内存极速匹配)
|
||||
Dim cn As Object, rs As Object
|
||||
Set cn = CreateObject("ADODB.Connection")
|
||||
Set rs = CreateObject("ADODB.Recordset")
|
||||
|
||||
Dim connStr As String
|
||||
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DB_PATH & ";"
|
||||
cn.Open connStr
|
||||
|
||||
Dim sql As String
|
||||
sql = "SELECT 总排号, 生产订单号, 产品型号, 数量, 成品物料码 " & _
|
||||
"FROM [" & TARGET_TABLE & "] " & _
|
||||
"WHERE 总排号 IN (" & queueNums & ")"
|
||||
|
||||
rs.Open sql, cn, 1, 1
|
||||
|
||||
Dim dbDict As Object
|
||||
Set dbDict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
If Not rs.EOF Then
|
||||
rs.MoveFirst
|
||||
Do Until rs.EOF
|
||||
Dim key As String
|
||||
key = Trim(rs.Fields("总排号").value)
|
||||
If Not dbDict.Exists(key) Then
|
||||
dbDict.Add key, Array( _
|
||||
rs.Fields("生产订单号").value, _
|
||||
rs.Fields("产品型号").value, _
|
||||
rs.Fields("数量").value, _
|
||||
rs.Fields("成品物料码").value _
|
||||
)
|
||||
End If
|
||||
rs.MoveNext
|
||||
Loop
|
||||
End If
|
||||
|
||||
rs.Close
|
||||
cn.Close
|
||||
Set rs = Nothing
|
||||
Set cn = Nothing
|
||||
|
||||
' 4. 【核心修复】安全且极速地回写可见数据
|
||||
Dim matchCount As Long
|
||||
matchCount = 0
|
||||
|
||||
' 关闭屏幕刷新、自动计算和事件触发,拉满单行写入性能
|
||||
Application.ScreenUpdating = False
|
||||
Application.Calculation = xlCalculationManual
|
||||
Application.EnableEvents = False
|
||||
|
||||
For Each cell In visibleRange
|
||||
currentNum = Trim(cell.value)
|
||||
|
||||
If dbDict.Exists(currentNum) Then
|
||||
Dim dbRecord As Variant
|
||||
dbRecord = dbDict(currentNum)
|
||||
|
||||
' 【神级优化点】:将4个字段装入一个微型一维数组,利用 Resize 一次性写入 B 到 E 列
|
||||
' 这样每一行只需要 1 次单元格操作,而不是 4 次!性能无限逼近全量数组写回。
|
||||
cell.Offset(0, 1).Resize(1, 4).value = Array(dbRecord(0), dbRecord(1), dbRecord(2), dbRecord(3))
|
||||
|
||||
matchCount = matchCount + 1
|
||||
End If
|
||||
Next cell
|
||||
|
||||
' 恢复应用状态
|
||||
Application.EnableEvents = True
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Application.ScreenUpdating = True
|
||||
Set dbDict = Nothing
|
||||
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
MsgBox "数据提取完成!" & vbCrLf & _
|
||||
"成功匹配并更新了 " & matchCount & " 条筛选记录。" & vbCrLf & _
|
||||
"用时: " & Format(elapsedTime, "0.00") & " 秒", vbInformation
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
Application.EnableEvents = True
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Application.ScreenUpdating = True
|
||||
On Error Resume Next
|
||||
If Not rs Is Nothing Then If rs.State = 1 Then rs.Close
|
||||
If Not cn Is Nothing Then If cn.State = 1 Then cn.Close
|
||||
On Error GoTo 0
|
||||
MsgBox "提取Access数据时发生异常: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetOrderSheet
|
||||
' 功能: 获取[产品订单]工作表
|
||||
' 返回: Worksheet - 工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetOrderSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetOrderSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
452
VBA/Modules/BIPUploadModule.bas
Normal file
452
VBA/Modules/BIPUploadModule.bas
Normal file
@@ -0,0 +1,452 @@
|
||||
'=====================================================================
|
||||
' 模块名: BIPUploadModule
|
||||
' 功能: 处理产品订单数据,提取BOM后生成[BIP上传模板]格式数据
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 常量定义
|
||||
'=====================================================================
|
||||
' 提取条件配置
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式 |bkxs,表壳形式 |gclj,过程连接 |jycz,接液材质 |lcfw,量程范围 |fjgn,附加功能"
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ProcessOrdersToBIP
|
||||
' 功能: 处理产品订单数据,生成BIP上传格式
|
||||
' 说明: 主入口程序,从[产品订单]读取数据,输出到[BIP上传模板]
|
||||
'=====================================================================
|
||||
Public Sub ProcessOrdersToBIP()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
Application.ScreenUpdating = False
|
||||
|
||||
' 准备工作表对象
|
||||
Dim orderSheet As Worksheet
|
||||
Dim bipSheet As Worksheet
|
||||
Dim bomSheet As Worksheet
|
||||
|
||||
' 获取[产品订单]工作表
|
||||
Set orderSheet = GetOrderSheet()
|
||||
If orderSheet Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "未找到[产品订单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 获取[BIP上传模板]工作表
|
||||
Set bipSheet = GetBIPUploadSheet()
|
||||
|
||||
' 获取BOM库工作表
|
||||
Set bomSheet = GetBomSheet()
|
||||
If bomSheet Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "未找到[平台配置清单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化BOM提取器
|
||||
Dim BomExtractor As BomExtractor
|
||||
Set BomExtractor = New BomExtractor
|
||||
BomExtractor.SetWorksheet bomSheet
|
||||
|
||||
If Not BomExtractor.LoadBomData Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 清空BIP上传模板数据(保留表头)
|
||||
ClearBIPSheetData bipSheet
|
||||
|
||||
' 写入BIP上传模板表头
|
||||
WriteBIPHeader bipSheet
|
||||
|
||||
' 获取订单数据行数
|
||||
Dim lastRow As Long
|
||||
lastRow = orderSheet.Cells(orderSheet.Rows.count, 1).End(xlUp).row
|
||||
|
||||
' 如果只有表头或没有数据
|
||||
If lastRow < 2 Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "[产品订单]工作表中没有数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 【性能核心】全量读入源数据
|
||||
Dim sourceDataArr As Variant
|
||||
sourceDataArr = orderSheet.Range("A2:G" & lastRow).value
|
||||
|
||||
' 【筛选核心】获取可见区域
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = orderSheet.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 收集所有输出数据
|
||||
Dim outputData As Collection
|
||||
Set outputData = New Collection
|
||||
|
||||
Dim cell As Range
|
||||
Dim arrIndex As Long
|
||||
Dim processedCount As Long
|
||||
Dim orderCount As Long
|
||||
Dim skippedCount As Long
|
||||
|
||||
processedCount = 0
|
||||
orderCount = 0
|
||||
skippedCount = 0
|
||||
|
||||
' 遍历筛选出来的可见单元格
|
||||
For Each cell In visibleRange
|
||||
' 计算内存数组索引
|
||||
arrIndex = cell.row - 1
|
||||
|
||||
' 读取订单数据
|
||||
Dim totalQueueNum As String
|
||||
Dim orderNumber As String
|
||||
Dim ProductModel As String
|
||||
Dim Quantity As String
|
||||
Dim productCode As String
|
||||
Dim componentPriority As String
|
||||
Dim isIssueMaterial As String
|
||||
|
||||
totalQueueNum = Trim(sourceDataArr(arrIndex, 1)) ' A列:总排号
|
||||
orderNumber = Trim(sourceDataArr(arrIndex, 2)) ' B列:生产订单号
|
||||
ProductModel = Trim(sourceDataArr(arrIndex, 3)) ' C列:产品型号
|
||||
Quantity = Trim(sourceDataArr(arrIndex, 4)) ' D列:数量
|
||||
productCode = Trim(sourceDataArr(arrIndex, 5)) ' E列:产品编码
|
||||
componentPriority = Trim(sourceDataArr(arrIndex, 6)) ' F列:部件优先
|
||||
isIssueMaterial = Trim(sourceDataArr(arrIndex, 7)) ' G列:是否领料
|
||||
|
||||
' 【拦截逻辑】忽略[是否领料]为"否"的订单
|
||||
If isIssueMaterial = "否" Then
|
||||
skippedCount = skippedCount + 1
|
||||
GoTo ContinueLoop
|
||||
End If
|
||||
|
||||
' 跳过空行
|
||||
If orderNumber = "" And ProductModel = "" Then
|
||||
GoTo ContinueLoop
|
||||
End If
|
||||
|
||||
' 验证必填字段
|
||||
If orderNumber = "" Then
|
||||
MsgBox "工作表第" & cell.row & "行:生产订单号为空,跳过该行!", vbExclamation
|
||||
GoTo ContinueLoop
|
||||
End If
|
||||
|
||||
If ProductModel = "" Then
|
||||
MsgBox "工作表第" & cell.row & "行:产品型号为空,跳过该行!", vbExclamation
|
||||
GoTo ContinueLoop
|
||||
End If
|
||||
|
||||
If Quantity = "" Then
|
||||
MsgBox "工作表第" & cell.row & "行:数量为空,跳过该行!", vbExclamation
|
||||
GoTo ContinueLoop
|
||||
End If
|
||||
|
||||
orderCount = orderCount + 1
|
||||
|
||||
' 处理单个订单,收集输出数据
|
||||
ProcessSingleOrder orderNumber, ProductModel, Quantity, productCode, _
|
||||
componentPriority, BomExtractor, outputData
|
||||
processedCount = processedCount + 1
|
||||
|
||||
ContinueLoop:
|
||||
Next cell
|
||||
|
||||
' 批量写入数据到工作表
|
||||
If outputData.count > 0 Then
|
||||
WriteBatchData bipSheet, outputData
|
||||
End If
|
||||
|
||||
' 格式化BIP上传模板
|
||||
FormatBIPSheet bipSheet
|
||||
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
Application.ScreenUpdating = True
|
||||
|
||||
MsgBox "处理完成!" & vbCrLf & _
|
||||
"处理有效订单数: " & orderCount & vbCrLf & _
|
||||
"忽略无效订单数: " & skippedCount & vbCrLf & _
|
||||
"生成BIP行数: " & outputData.count & vbCrLf & _
|
||||
"用时: " & Format(elapsedTime, "0.00") & "秒", vbInformation
|
||||
|
||||
' 激活BIP上传模板
|
||||
bipSheet.Activate
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "处理异常: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ProcessSingleOrder
|
||||
' 功能: 处理单个订单,提取BOM并将数据添加到输出集合
|
||||
' 参数: orderNumber - 生产订单号
|
||||
' ProductModel - 产品型号
|
||||
' Quantity - 生产数量
|
||||
' productCode - 产品编码
|
||||
' componentPriority - 部件优先标志("是"或"否")
|
||||
' BomExtractor - BOM提取器对象
|
||||
' outputData - 输出数据集合
|
||||
'=====================================================================
|
||||
Private Sub ProcessSingleOrder(orderNumber As String, _
|
||||
ProductModel As String, _
|
||||
Quantity As String, _
|
||||
productCode As String, _
|
||||
componentPriority As String, _
|
||||
BomExtractor As BomExtractor, _
|
||||
outputData As Collection)
|
||||
On Error Resume Next
|
||||
|
||||
' 根据部件优先设置排除类别
|
||||
BomExtractor.ClearExcludeCategories
|
||||
If UCase(componentPriority) = "否" Or componentPriority = "0" Or componentPriority = "FALSE" Then
|
||||
Dim excludeCats As New Collection
|
||||
excludeCats.Add "部件"
|
||||
BomExtractor.SetExcludeCategories excludeCats
|
||||
End If
|
||||
|
||||
' 解析产品型号
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
If Not parser.Parse(ProductModel) Then
|
||||
' 解析失败,添加一行空物料记录
|
||||
outputData.Add CreateBIPRowArray(orderNumber, productCode, Quantity, 1, "")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 提取BOM
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = BomExtractor.ExtractBom(parser.conditions)
|
||||
|
||||
' 输出结果
|
||||
If matchedItems.count = 0 Then
|
||||
' 没有匹配项,添加一行空物料记录
|
||||
outputData.Add CreateBIPRowArray(orderNumber, productCode, Quantity, 1, "")
|
||||
Else
|
||||
' 输出每个匹配的物料
|
||||
Dim item As BomItem
|
||||
Dim lineIndex As Long
|
||||
lineIndex = 1
|
||||
|
||||
' 创建字典跟踪每个 BIP 行号基数的当前序号
|
||||
Dim bipRowBaseDict As Object
|
||||
Set bipRowBaseDict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
For Each item In matchedItems
|
||||
' 计算实际行号:行号 = BIP 行号基数 + 组内序号 (从 1 开始)
|
||||
Dim baseValue As Long
|
||||
baseValue = item.BipRowNumberBase
|
||||
|
||||
Dim currentIndex As Long
|
||||
If bipRowBaseDict.Exists(baseValue) Then
|
||||
currentIndex = bipRowBaseDict(baseValue) + 1
|
||||
Else
|
||||
currentIndex = 1
|
||||
End If
|
||||
bipRowBaseDict(baseValue) = currentIndex
|
||||
|
||||
Dim actualRowNumber As Long
|
||||
actualRowNumber = baseValue + currentIndex
|
||||
|
||||
' 创建 BIP 行数据并添加到集合 (已移除备注参数)
|
||||
outputData.Add CreateBIPRowArray(orderNumber, productCode, Quantity, _
|
||||
actualRowNumber, item.Code66)
|
||||
|
||||
lineIndex = lineIndex + 1
|
||||
Next item
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数:CreateBIPRowArray
|
||||
' 功能:创建 BIP 上传模板一行数据的数组
|
||||
' 参数:orderNumber - 生产订单号
|
||||
' productCode - 产品编码
|
||||
' Quantity - 生产数量
|
||||
' actualRowNumber - 实际行号(BIP 行号基数 + 组内序号)
|
||||
' materialCode - 材料编码(66 编码)
|
||||
' 返回:Variant() - 包含 9 个元素的数组
|
||||
'=====================================================================
|
||||
Private Function CreateBIPRowArray(orderNumber As String, _
|
||||
productCode As String, _
|
||||
Quantity As String, _
|
||||
actualRowNumber As Long, _
|
||||
materialCode As String) As Variant()
|
||||
Dim rowData(1 To 9) As Variant
|
||||
|
||||
rowData(1) = orderNumber ' 来源单据号(生产订单号)
|
||||
rowData(2) = productCode ' 产品编码
|
||||
rowData(3) = Quantity ' 生产数量
|
||||
rowData(4) = actualRowNumber ' 行号
|
||||
rowData(5) = materialCode ' 材料编码(66 编码)
|
||||
rowData(6) = "一般发料" ' 供应方式(固定值)
|
||||
rowData(7) = Date ' 需用日期(当天日期)
|
||||
rowData(8) = "重庆布莱迪仪器仪表有限公司" ' 发料组织(固定值)
|
||||
rowData(9) = Quantity ' 计划出库数量(与生产数量一致)
|
||||
|
||||
CreateBIPRowArray = rowData
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteBatchData
|
||||
' 功能: 批量写入数据到工作表
|
||||
' 参数: ws - 工作表对象
|
||||
' outputData - 输出数据集合,每个元素是一个一维数组
|
||||
'=====================================================================
|
||||
Private Sub WriteBatchData(ws As Worksheet, outputData As Collection)
|
||||
' 如果没有数据,直接返回
|
||||
If outputData.count = 0 Then
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 创建二维数组
|
||||
Dim rowCount As Long
|
||||
rowCount = outputData.count
|
||||
|
||||
Dim resultData() As Variant
|
||||
ReDim resultData(1 To rowCount, 1 To 9)
|
||||
|
||||
' 填充数据到二维数组
|
||||
Dim i As Long
|
||||
Dim rowArray As Variant
|
||||
|
||||
For i = 1 To rowCount
|
||||
rowArray = outputData(i)
|
||||
|
||||
resultData(i, 1) = rowArray(1)
|
||||
resultData(i, 2) = rowArray(2)
|
||||
resultData(i, 3) = rowArray(3)
|
||||
resultData(i, 4) = rowArray(4)
|
||||
resultData(i, 5) = rowArray(5)
|
||||
resultData(i, 6) = rowArray(6)
|
||||
resultData(i, 7) = rowArray(7)
|
||||
resultData(i, 8) = rowArray(8)
|
||||
resultData(i, 9) = rowArray(9)
|
||||
Next i
|
||||
|
||||
' 一次性写入工作表(从第2行开始)
|
||||
ws.Range("A2").Resize(rowCount, 9).value = resultData
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteBIPHeader
|
||||
' 功能: 写入BIP上传模板表头
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Private Sub WriteBIPHeader(ws As Worksheet)
|
||||
' 第1行:主表头
|
||||
ws.Cells(1, 1).value = "来源单据号(生产订单号)"
|
||||
ws.Cells(1, 2).value = "产品编码"
|
||||
ws.Cells(1, 3).value = "生产数量"
|
||||
ws.Cells(1, 4).value = "行号"
|
||||
ws.Cells(1, 5).value = "材料编码"
|
||||
ws.Cells(1, 6).value = "供应方式"
|
||||
ws.Cells(1, 7).value = "需用日期"
|
||||
ws.Cells(1, 8).value = "发料组织"
|
||||
ws.Cells(1, 9).value = "计划出库数量"
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetOrderSheet
|
||||
' 功能: 获取[产品订单]工作表
|
||||
' 返回: Worksheet - 工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetOrderSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetOrderSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetBIPUploadSheet
|
||||
' 功能: 获取或创建[BIP上传模板]工作表
|
||||
' 返回: Worksheet - 工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetBIPUploadSheet() As Worksheet
|
||||
Dim wsName As String
|
||||
wsName = "BIP上传模板"
|
||||
|
||||
On Error Resume Next
|
||||
Set GetBIPUploadSheet = ThisWorkbook.Worksheets(wsName)
|
||||
On Error GoTo 0
|
||||
|
||||
If GetBIPUploadSheet Is Nothing Then
|
||||
' 创建新工作表
|
||||
Set GetBIPUploadSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.count))
|
||||
GetBIPUploadSheet.Name = wsName
|
||||
End If
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetBomSheet
|
||||
' 功能: 获取BOM工作表
|
||||
' 返回: Worksheet - BOM工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetBomSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetBomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ClearBIPSheetData
|
||||
' 功能: 清空BIP上传模板的数据(保留表头)
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Private Sub ClearBIPSheetData(ws As Worksheet)
|
||||
' 清空从第2行开始的所有数据
|
||||
Dim lastRow As Long
|
||||
lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row
|
||||
|
||||
If lastRow > 1 Then
|
||||
ws.Rows("2:" & lastRow).ClearContents
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: FormatBIPSheet
|
||||
' 功能: 格式化BIP上传模板工作表
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Private Sub FormatBIPSheet(ws As Worksheet)
|
||||
On Error Resume Next
|
||||
|
||||
' 设置表头格式
|
||||
With ws.Rows(1)
|
||||
.Font.Bold = True
|
||||
.Interior.Color = RGB(217, 217, 217)
|
||||
.HorizontalAlignment = xlCenter
|
||||
End With
|
||||
|
||||
' 设置所有单元格居中对齐
|
||||
With ws.UsedRange
|
||||
.HorizontalAlignment = xlCenter
|
||||
.VerticalAlignment = xlCenter
|
||||
End With
|
||||
|
||||
' 自动调整列宽
|
||||
ws.Columns.AutoFit
|
||||
|
||||
' 设置日期列格式
|
||||
ws.Columns(7).NumberFormat = "yyyy/mm/dd"
|
||||
|
||||
On Error GoTo 0
|
||||
End Sub
|
||||
513
VBA/Modules/ComponentInventoryCheckModule.bas
Normal file
513
VBA/Modules/ComponentInventoryCheckModule.bas
Normal file
@@ -0,0 +1,513 @@
|
||||
'=====================================================================
|
||||
' 模块名: ComponentInventoryCheckModule
|
||||
' 功能: 部件库存核推模块 - 自动核对产品订单中"部件"类物料的库存情况
|
||||
' 特性: [已重构] 支持仅对筛选后的数据进行处理,采用内存极速读取
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 数据结构定义 - 使用字典以支持引用更新
|
||||
'=====================================================================
|
||||
|
||||
' 订单字典键
|
||||
Private Const ORDER_ROW As String = "RowNumber"
|
||||
Private Const ORDER_MODEL As String = "ProductModel"
|
||||
Private Const ORDER_QUANTITY As String = "Quantity"
|
||||
Private Const ORDER_COMP_CODE As String = "ComponentCode"
|
||||
Private Const ORDER_COMP_QTY As String = "ComponentQty"
|
||||
Private Const ORDER_HAS_COMP As String = "HasComponent"
|
||||
Private Const ORDER_PARSE_ERR As String = "ParseError"
|
||||
|
||||
' 部件库存字典键
|
||||
Private Const INV_CODE As String = "ComponentCode"
|
||||
Private Const INV_DEMAND As String = "TotalDemand"
|
||||
Private Const INV_STOCK As String = "AvailableStock"
|
||||
Private Const INV_SHORTAGE As String = "IsShortage"
|
||||
|
||||
' 统计信息结构
|
||||
Private Type Statistics
|
||||
TotalOrders As Long ' 总订单数
|
||||
OrdersWithComponent As Long ' 包含部件的订单数
|
||||
OrdersSufficient As Long ' 库存充足订单数
|
||||
OrdersInsufficient As Long ' 库存不足订单数
|
||||
OrdersSkipped As Long ' 跳过订单数
|
||||
End Type
|
||||
|
||||
'=====================================================================
|
||||
' 主入口程序
|
||||
'=====================================================================
|
||||
Public Sub CheckComponentInventory()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
' 提升性能:关闭屏幕更新和自动计算
|
||||
Application.ScreenUpdating = False
|
||||
Application.Calculation = xlCalculationManual
|
||||
|
||||
' 获取工作表对象
|
||||
Dim orderSheet As Worksheet
|
||||
Dim inventorySheet As Worksheet
|
||||
Dim bomSheet As Worksheet
|
||||
|
||||
Set orderSheet = GetOrderSheet()
|
||||
If orderSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[产品订单]工作表!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Set inventorySheet = GetInventorySheet()
|
||||
If inventorySheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[现存量]工作表!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Set bomSheet = GetBomSheet()
|
||||
If bomSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[平台配置清单]工作表!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 检查订单数据 (调整为按C列:产品型号获取最后一行)
|
||||
Dim lastRow As Long
|
||||
lastRow = orderSheet.Cells(orderSheet.Rows.count, 3).End(xlUp).row
|
||||
If lastRow < 2 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "[产品订单]工作表没有数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 【核心重构】获取筛选后的可见单元格区域 (A列)
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = orderSheet.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化BOM提取器
|
||||
Dim BomExtractor As BomExtractor
|
||||
Set BomExtractor = New BomExtractor
|
||||
BomExtractor.SetWorksheet bomSheet
|
||||
|
||||
If Not BomExtractor.LoadBomData Then
|
||||
RestoreAppStatus
|
||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 读取库存数据到字典
|
||||
Dim inventoryData As Object
|
||||
Set inventoryData = LoadInventoryData(inventorySheet)
|
||||
|
||||
If inventoryData.count = 0 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "[现存量]工作表没有有效数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 【核心重构】传递可见区域和总行数,仅读取可见订单数据
|
||||
Dim orders As Collection
|
||||
Set orders = LoadOrderData(orderSheet, visibleRange, lastRow)
|
||||
|
||||
If orders.count = 0 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "可见区域中没有有效的订单数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 解析所有订单的BOM
|
||||
ParseAllOrdersBOM orders, BomExtractor
|
||||
|
||||
' 统计部件总需求
|
||||
Dim componentDemands As Object
|
||||
Set componentDemands = CalculateComponentDemand(orders)
|
||||
|
||||
If componentDemands.count = 0 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "筛选的订单中没有包含'部件'类别物料,无需处理库存!", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 验证库存
|
||||
Dim validationWarnings As Collection
|
||||
Set validationWarnings = ValidateInventory(componentDemands, inventoryData)
|
||||
|
||||
' 按订单顺序分配库存并标记
|
||||
Dim stats As Statistics
|
||||
AllocateInventory orders, componentDemands, orderSheet, stats
|
||||
|
||||
' 恢复应用状态
|
||||
RestoreAppStatus
|
||||
|
||||
' 输出结果统计
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
Dim resultMsg As String
|
||||
resultMsg = "部件库存核对完成!" & vbCrLf & vbCrLf
|
||||
resultMsg = resultMsg & "处理筛选订单数: " & stats.TotalOrders & vbCrLf
|
||||
resultMsg = resultMsg & "包含部件订单: " & stats.OrdersWithComponent & vbCrLf
|
||||
resultMsg = resultMsg & "库存充足订单: " & stats.OrdersSufficient & vbCrLf
|
||||
resultMsg = resultMsg & "库存不足订单: " & stats.OrdersInsufficient & vbCrLf
|
||||
If stats.OrdersSkipped > 0 Then
|
||||
resultMsg = resultMsg & "跳过订单数: " & stats.OrdersSkipped & vbCrLf
|
||||
End If
|
||||
resultMsg = resultMsg & vbCrLf & "耗时: " & Format(elapsedTime, "0.00") & "秒"
|
||||
|
||||
' 显示警告信息(如果有)
|
||||
If validationWarnings.count > 0 Then
|
||||
resultMsg = resultMsg & vbCrLf & vbCrLf & "警告信息:" & vbCrLf
|
||||
resultMsg = resultMsg & JoinCollection(validationWarnings, vbCrLf)
|
||||
End If
|
||||
|
||||
MsgBox resultMsg, vbInformation
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
RestoreAppStatus
|
||||
MsgBox "部件库存核对异常: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 辅助过程: RestoreAppStatus
|
||||
' 功能: 恢复Excel应用程序的状态
|
||||
'=====================================================================
|
||||
Private Sub RestoreAppStatus()
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Application.ScreenUpdating = True
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: LoadOrderData
|
||||
' 功能: 读取订单数据
|
||||
' 参数: ws - [产品订单]工作表
|
||||
' 返回: Collection - 每个元素是字典对象,包含订单信息
|
||||
'=====================================================================
|
||||
Private Function LoadOrderData(ws As Worksheet, visibleRange As Range, lastRow As Long) As Collection
|
||||
Set LoadOrderData = New Collection
|
||||
|
||||
' 全量读入内存数组提升速度
|
||||
Dim sourceDataArr As Variant
|
||||
sourceDataArr = ws.Range("A2:F" & lastRow).value
|
||||
|
||||
Dim cell As Range
|
||||
Dim arrIndex As Long
|
||||
|
||||
' 仅遍历可见的单元格
|
||||
For Each cell In visibleRange
|
||||
Dim model As String
|
||||
Dim qty As Variant
|
||||
|
||||
' 数组索引 = Excel行号 - 1
|
||||
arrIndex = cell.row - 1
|
||||
|
||||
' 从内存数组中提取数据
|
||||
model = Trim(sourceDataArr(arrIndex, 3)) ' C列: 产品型号
|
||||
qty = sourceDataArr(arrIndex, 4) ' D列: 产品数量
|
||||
|
||||
' 跳过空行
|
||||
If model <> "" Then
|
||||
Dim order As Object
|
||||
Set order = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 记录真实的Excel行号,用于后续库存不足时精准写入F列
|
||||
order.Add ORDER_ROW, CLng(cell.row)
|
||||
order.Add ORDER_MODEL, CStr(model)
|
||||
order.Add ORDER_QUANTITY, CDbl(IIf(IsNull(qty) Or IsEmpty(qty), 0, qty))
|
||||
order.Add ORDER_COMP_CODE, ""
|
||||
order.Add ORDER_COMP_QTY, 0
|
||||
order.Add ORDER_HAS_COMP, False
|
||||
order.Add ORDER_PARSE_ERR, ""
|
||||
|
||||
LoadOrderData.Add order
|
||||
End If
|
||||
Next cell
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: LoadInventoryData
|
||||
' 功能: 读取库存数据
|
||||
' 参数: ws - [现存量]工作表
|
||||
' 返回: Dictionary(物料编码 -> 库存数量)
|
||||
'=====================================================================
|
||||
Private Function LoadInventoryData(ws As Worksheet) As Object
|
||||
Set LoadInventoryData = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 从第4行开始读取(第3行是表头)
|
||||
Dim lastRow As Long
|
||||
lastRow = ws.Cells(ws.Rows.count, 2).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
For i = 4 To lastRow
|
||||
Dim code As String
|
||||
Dim qty As Variant
|
||||
|
||||
code = Trim(ws.Cells(i, 2).value) ' B列: 物料编码
|
||||
qty = ws.Cells(i, 10).value ' J列: 结存主数量
|
||||
|
||||
If code <> "" And Not IsEmpty(qty) Then
|
||||
If Not LoadInventoryData.Exists(code) Then
|
||||
LoadInventoryData.Add code, CDbl(qty)
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ParseAllOrdersBOM
|
||||
' 功能: 解析所有订单的BOM
|
||||
' 参数: orders - 订单集合(每个元素是字典)
|
||||
' bomExtractor - BOM提取器
|
||||
'=====================================================================
|
||||
Private Sub ParseAllOrdersBOM(orders As Collection, BomExtractor As BomExtractor)
|
||||
Dim i As Long
|
||||
For i = 1 To orders.count
|
||||
Dim order As Object
|
||||
Set order = orders(i)
|
||||
ParseOrderBOM order, BomExtractor
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ParseOrderBOM
|
||||
' 功能: 解析单个订单的BOM,识别部件类别物料
|
||||
' 参数: orderInfo - 订单信息字典(ByRef)
|
||||
' bomExtractor - BOM提取器
|
||||
'=====================================================================
|
||||
Private Sub ParseOrderBOM(ByRef orderInfo As Object, BomExtractor As BomExtractor)
|
||||
On Error Resume Next
|
||||
|
||||
' 解析型号
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
If Not parser.Parse(orderInfo(ORDER_MODEL)) Then
|
||||
orderInfo(ORDER_PARSE_ERR) = "解析失败: " & parser.ErrorMessage
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 提取BOM
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = BomExtractor.ExtractBom(parser.conditions)
|
||||
|
||||
' 查找"部件"类别物料
|
||||
Dim item As BomItem
|
||||
For Each item In matchedItems
|
||||
If item.category = "部件" Then
|
||||
orderInfo(ORDER_COMP_CODE) = item.Code66
|
||||
orderInfo(ORDER_COMP_QTY) = item.Quantity
|
||||
orderInfo(ORDER_HAS_COMP) = True
|
||||
Exit For
|
||||
End If
|
||||
Next item
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: CalculateComponentDemand
|
||||
' 功能: 统计部件总需求
|
||||
' 参数: orders - 订单集合
|
||||
' 返回: Dictionary(部件编码 -> 库存信息字典)
|
||||
'=====================================================================
|
||||
Private Function CalculateComponentDemand(orders As Collection) As Object
|
||||
Dim demands As Object
|
||||
Set demands = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim i As Long
|
||||
For i = 1 To orders.count
|
||||
Dim order As Object
|
||||
Set order = orders(i)
|
||||
|
||||
If order(ORDER_HAS_COMP) Then
|
||||
Dim demand As Double
|
||||
demand = order(ORDER_COMP_QTY) * order(ORDER_QUANTITY)
|
||||
Dim compCode As String
|
||||
compCode = order(ORDER_COMP_CODE)
|
||||
|
||||
If demands.Exists(compCode) Then
|
||||
Dim compInv As Object
|
||||
Set compInv = demands(compCode)
|
||||
compInv(INV_DEMAND) = compInv(INV_DEMAND) + demand
|
||||
Else
|
||||
Dim newComp As Object
|
||||
Set newComp = CreateObject("Scripting.Dictionary")
|
||||
newComp.Add INV_CODE, compCode
|
||||
newComp.Add INV_DEMAND, demand
|
||||
newComp.Add INV_STOCK, 0
|
||||
newComp.Add INV_SHORTAGE, False
|
||||
demands.Add compCode, newComp
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
Set CalculateComponentDemand = demands
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: ValidateInventory
|
||||
' 功能: 验证库存数据
|
||||
' 参数: componentDemands - 部件需求字典
|
||||
' inventoryData - 库存数据字典
|
||||
' 返回: Collection - 警告信息集合(找不到的部件)
|
||||
'=====================================================================
|
||||
Private Function ValidateInventory(componentDemands As Object, _
|
||||
inventoryData As Object) As Collection
|
||||
Set ValidateInventory = New Collection
|
||||
|
||||
Dim code As Variant
|
||||
For Each code In componentDemands.Keys
|
||||
Dim compInv As Object
|
||||
Set compInv = componentDemands(code)
|
||||
|
||||
' 检查库存中是否存在该部件
|
||||
If Not inventoryData.Exists(compInv(INV_CODE)) Then
|
||||
compInv(INV_STOCK) = 0
|
||||
compInv(INV_SHORTAGE) = True
|
||||
ValidateInventory.Add "部件 '" & compInv(INV_CODE) & "' 在[现存量]中未找到"
|
||||
Else
|
||||
compInv(INV_STOCK) = inventoryData(compInv(INV_CODE))
|
||||
|
||||
If compInv(INV_DEMAND) > compInv(INV_STOCK) Then
|
||||
compInv(INV_SHORTAGE) = True
|
||||
End If
|
||||
End If
|
||||
Next code
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: AllocateInventory
|
||||
' 功能: 按订单顺序分配库存并标记
|
||||
' 参数: orders - 订单集合
|
||||
' componentDemands - 部件需求字典
|
||||
' orderSheet - 订单工作表
|
||||
' stats - 统计信息(ByRef)
|
||||
'=====================================================================
|
||||
Private Sub AllocateInventory(orders As Collection, _
|
||||
componentDemands As Object, _
|
||||
orderSheet As Worksheet, _
|
||||
ByRef stats As Statistics)
|
||||
|
||||
' 初始化统计
|
||||
stats.TotalOrders = orders.count
|
||||
stats.OrdersWithComponent = 0
|
||||
stats.OrdersSufficient = 0
|
||||
stats.OrdersInsufficient = 0
|
||||
stats.OrdersSkipped = 0
|
||||
|
||||
Dim i As Long
|
||||
For i = 1 To orders.count
|
||||
Dim order As Object
|
||||
Set order = orders(i)
|
||||
|
||||
' 跳过解析失败的订单
|
||||
If order(ORDER_PARSE_ERR) <> "" Then
|
||||
stats.OrdersSkipped = stats.OrdersSkipped + 1
|
||||
GoTo NextOrder
|
||||
End If
|
||||
|
||||
' 跳过没有部件的订单
|
||||
If Not order(ORDER_HAS_COMP) Then
|
||||
stats.OrdersSkipped = stats.OrdersSkipped + 1
|
||||
GoTo NextOrder
|
||||
End If
|
||||
|
||||
' 跳过数量为0的订单
|
||||
If order(ORDER_QUANTITY) = 0 Then
|
||||
stats.OrdersSkipped = stats.OrdersSkipped + 1
|
||||
GoTo NextOrder
|
||||
End If
|
||||
|
||||
stats.OrdersWithComponent = stats.OrdersWithComponent + 1
|
||||
|
||||
' 获取部件库存信息
|
||||
Dim compInv As Object
|
||||
Set compInv = componentDemands(order(ORDER_COMP_CODE))
|
||||
|
||||
' 计算需求量
|
||||
Dim requiredQty As Double
|
||||
requiredQty = order(ORDER_COMP_QTY) * order(ORDER_QUANTITY)
|
||||
|
||||
' 检查库存是否充足
|
||||
If compInv(INV_STOCK) >= requiredQty Then
|
||||
' 库存充足,扣减库存,保持原值
|
||||
compInv(INV_STOCK) = compInv(INV_STOCK) - requiredQty
|
||||
stats.OrdersSufficient = stats.OrdersSufficient + 1
|
||||
Else
|
||||
' --- 因为已经保存了真正的行号 ORDER_ROW, 在关闭屏幕刷新的情况下,这里直接写入是非常快的 ---
|
||||
orderSheet.Cells(order(ORDER_ROW), 6).value = "否"
|
||||
compInv(INV_STOCK) = compInv(INV_STOCK) - requiredQty
|
||||
stats.OrdersInsufficient = stats.OrdersInsufficient + 1
|
||||
End If
|
||||
|
||||
NextOrder:
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetOrderSheet
|
||||
' 功能: 获取[产品订单]工作表
|
||||
' 返回: Worksheet
|
||||
'=====================================================================
|
||||
Private Function GetOrderSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetOrderSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetInventorySheet
|
||||
' 功能: 获取[现存量]工作表
|
||||
' 返回: Worksheet
|
||||
'=====================================================================
|
||||
Private Function GetInventorySheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetInventorySheet = ThisWorkbook.Worksheets("现存量")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetBomSheet
|
||||
' 功能: 获取[平台配置清单]工作表
|
||||
' 返回: Worksheet
|
||||
'=====================================================================
|
||||
Private Function GetBomSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetBomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: JoinCollection
|
||||
' 功能: 将集合内容连接为字符串
|
||||
' 参数: coll - 集合
|
||||
' separator - 分隔符
|
||||
' 返回: String
|
||||
'=====================================================================
|
||||
Private Function JoinCollection(coll As Collection, separator As String) As String
|
||||
Dim result As String
|
||||
result = ""
|
||||
|
||||
Dim item As Variant
|
||||
Dim isFirst As Boolean
|
||||
isFirst = True
|
||||
|
||||
For Each item In coll
|
||||
If Not isFirst Then
|
||||
result = result & separator
|
||||
End If
|
||||
result = result & CStr(item)
|
||||
isFirst = False
|
||||
Next item
|
||||
|
||||
JoinCollection = result
|
||||
End Function
|
||||
586
VBA/Modules/ErrorAnalysisModule.bas
Normal file
586
VBA/Modules/ErrorAnalysisModule.bas
Normal file
@@ -0,0 +1,586 @@
|
||||
'=====================================================================
|
||||
' 模块名: ErrorAnalysisModule
|
||||
' 功能: BOM匹配异常分析模块,仅提取报错订单,拆分多行,并自动回溯"未匹配参数"
|
||||
' 特性: 采用"特征权重算法"解决模糊平局(Tie)导致的参数误报问题
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
' 提取条件配置
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
||||
' 指定输出[BOM匹配异常报表]的表头所在行号
|
||||
Private Const OUTPUT_HEADER_ROW As Long = 10
|
||||
|
||||
'=====================================================================
|
||||
' 过程: GenerateErrorAnalysisReport
|
||||
' 功能: 批量处理产品型号,输出BOM匹配异常报表
|
||||
'=====================================================================
|
||||
Public Sub GenerateErrorAnalysisReport()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
Application.ScreenUpdating = False
|
||||
Application.Calculation = xlCalculationManual
|
||||
|
||||
' 获取工作表
|
||||
Dim orderSheet As Worksheet
|
||||
Dim bomSheet As Worksheet
|
||||
Dim outputSheet As Worksheet
|
||||
|
||||
Set orderSheet = GetOrderSheet()
|
||||
If orderSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[产品订单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Set bomSheet = GetBomSheet()
|
||||
If bomSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[平台配置清单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化BOM提取器
|
||||
Dim BomExtractor As BomExtractor
|
||||
Set BomExtractor = New BomExtractor
|
||||
BomExtractor.SetWorksheet bomSheet
|
||||
|
||||
If Not BomExtractor.LoadBomData Then
|
||||
RestoreAppStatus
|
||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 获取或创建输出表
|
||||
Set outputSheet = CreateErrorOutputSheet()
|
||||
WriteOutputHeader outputSheet
|
||||
|
||||
' 获取筛选后的订单数据
|
||||
Dim lastRow As Long
|
||||
lastRow = orderSheet.Cells(orderSheet.Rows.count, 3).End(xlUp).row
|
||||
If lastRow < 2 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "[产品订单]工作表中没有数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim sourceDataArr As Variant
|
||||
sourceDataArr = orderSheet.Range("A2:F" & lastRow).value
|
||||
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = orderSheet.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化正则表达式引擎 (只初始化一次,提速)
|
||||
Dim regEx As Object
|
||||
Set regEx = CreateObject("VBScript.RegExp")
|
||||
regEx.Global = True
|
||||
regEx.IgnoreCase = True
|
||||
' 匹配如 azxs=A0, fjgn!=N1 这样的条件结构
|
||||
regEx.Pattern = "(azxs|bkxs|gclj|jycz|lcfw|fjgn)\s*(!=|=)\s*([A-Za-z0-9_]+)"
|
||||
|
||||
Dim outputData As Collection
|
||||
Set outputData = New Collection
|
||||
|
||||
Dim cell As Range
|
||||
Dim arrIndex As Long
|
||||
Dim totalProcessed As Long
|
||||
Dim errorOrdersCount As Long
|
||||
Dim errorRowsCount As Long
|
||||
|
||||
totalProcessed = 0
|
||||
errorOrdersCount = 0
|
||||
errorRowsCount = 0
|
||||
|
||||
' 遍历可见订单
|
||||
For Each cell In visibleRange
|
||||
arrIndex = cell.row - 1
|
||||
|
||||
Dim totalQueueNum As String
|
||||
Dim orderNumber As String
|
||||
Dim modelString As String
|
||||
Dim componentPriority As String
|
||||
|
||||
totalQueueNum = Trim(sourceDataArr(arrIndex, 1))
|
||||
orderNumber = Trim(sourceDataArr(arrIndex, 2))
|
||||
modelString = Trim(sourceDataArr(arrIndex, 3))
|
||||
componentPriority = Trim(sourceDataArr(arrIndex, 6))
|
||||
|
||||
If modelString <> "" Then
|
||||
totalProcessed = totalProcessed + 1
|
||||
|
||||
' 解析并匹配BOM
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
Dim hasError As Boolean
|
||||
hasError = False
|
||||
Dim errors As String
|
||||
errors = ""
|
||||
|
||||
If Not parser.Parse(modelString) Then
|
||||
hasError = True
|
||||
errors = "型号解析失败: " & parser.ErrorMessage
|
||||
Else
|
||||
' 提取逻辑
|
||||
BomExtractor.ClearExcludeCategories
|
||||
If UCase(componentPriority) = "否" Or componentPriority = "0" Or componentPriority = "FALSE" Then
|
||||
Dim excludeCats As New Collection
|
||||
excludeCats.Add "部件"
|
||||
BomExtractor.SetExcludeCategories excludeCats
|
||||
End If
|
||||
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = BomExtractor.ExtractBom(parser.conditions)
|
||||
|
||||
errors = BomExtractor.GetErrorSummary()
|
||||
If errors <> "" Or matchedItems.count = 0 Then
|
||||
hasError = True
|
||||
If errors = "" And matchedItems.count = 0 Then
|
||||
errors = "完全未匹配到物料"
|
||||
End If
|
||||
End If
|
||||
|
||||
' 深度检查BOM行自身的报错(如"匹配到多条")
|
||||
Dim item As BomItem
|
||||
For Each item In matchedItems
|
||||
If item.MatchError <> "" Then
|
||||
hasError = True
|
||||
errors = errors & item.category & ":" & item.MatchError & ";"
|
||||
End If
|
||||
Next item
|
||||
End If
|
||||
|
||||
' 如果存在错误,拆分为多行并寻找未匹配参数
|
||||
If hasError Then
|
||||
errorOrdersCount = errorOrdersCount + 1
|
||||
|
||||
Dim errArray() As String
|
||||
errArray = Split(errors, ";")
|
||||
Dim i As Long
|
||||
|
||||
For i = LBound(errArray) To UBound(errArray)
|
||||
Dim singleError As String
|
||||
singleError = Trim(errArray(i))
|
||||
|
||||
If singleError <> "" Then
|
||||
Dim unmatchedValues As String
|
||||
unmatchedValues = "无法精准定位:无法精准定位"
|
||||
|
||||
' 如果是型号解析失败,跳过溯源
|
||||
If InStr(singleError, "型号解析失败") = 0 And InStr(singleError, "完全未匹配到物料") = 0 Then
|
||||
Dim targetCategory As String
|
||||
targetCategory = ExtractCategoryName(singleError)
|
||||
|
||||
If targetCategory <> "" Then
|
||||
' 核心:调用带权重的重合度算法定位冲突参数(通过 | 分隔,内部用 : 分隔键值)
|
||||
unmatchedValues = FindUnmatchedParameter(targetCategory, parser.conditions, BomExtractor.GetAllItems(), regEx)
|
||||
End If
|
||||
End If
|
||||
|
||||
' ---> 拆分未匹配参数,避免糅合在一起
|
||||
Dim unmatchArr() As String
|
||||
unmatchArr = Split(unmatchedValues, "|")
|
||||
|
||||
Dim j As Long
|
||||
For j = LBound(unmatchArr) To UBound(unmatchArr)
|
||||
Dim singleUnmatch As String
|
||||
singleUnmatch = Trim(unmatchArr(j))
|
||||
If singleUnmatch <> "" Then
|
||||
Dim uParam As String
|
||||
Dim uValue As String
|
||||
Dim colonPos As Long
|
||||
colonPos = InStr(singleUnmatch, ":")
|
||||
|
||||
' 拆分键和值
|
||||
If colonPos > 0 Then
|
||||
uParam = Left(singleUnmatch, colonPos - 1)
|
||||
uValue = Mid(singleUnmatch, colonPos + 1)
|
||||
Else
|
||||
uParam = singleUnmatch
|
||||
uValue = singleUnmatch
|
||||
End If
|
||||
|
||||
outputData.Add CreateErrorRowArray(totalQueueNum, orderNumber, modelString, parser.conditions, uParam, uValue, singleError)
|
||||
errorRowsCount = errorRowsCount + 1
|
||||
End If
|
||||
Next j
|
||||
End If
|
||||
Next i
|
||||
End If
|
||||
End If
|
||||
Next cell
|
||||
|
||||
' 批量写入数据
|
||||
If outputData.count > 0 Then
|
||||
WriteBatchData outputSheet, outputData
|
||||
Else
|
||||
MsgBox "太棒了!所选订单均完美匹配BOM,未发现任何异常。", vbInformation
|
||||
End If
|
||||
|
||||
' 格式化表格
|
||||
FormatOutputSheet outputSheet
|
||||
|
||||
RestoreAppStatus
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
MsgBox "异常分析完成!" & vbCrLf & _
|
||||
"共检查订单: " & totalProcessed & vbCrLf & _
|
||||
"发现异常订单: " & errorOrdersCount & vbCrLf & _
|
||||
"生成异常明细: " & errorRowsCount & " 行" & vbCrLf & _
|
||||
"用时: " & Format(elapsedTime, "0.00") & "秒", vbInformation
|
||||
|
||||
outputSheet.Activate
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
RestoreAppStatus
|
||||
MsgBox "异常分析发生错误: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 核心算法: FindUnmatchedParameter (带权重的最大特征重合度算法)
|
||||
' 功能: 分析BOM库,找出与当前订单特征最相似的物料,并提取冲突(未匹配)的参数值
|
||||
'=====================================================================
|
||||
Private Function FindUnmatchedParameter(category As String, productConds As Object, allBomItems As Collection, regEx As Object) As String
|
||||
' 使用 Long 类型,因为加入权重后得分会超过 Integer 上限
|
||||
Dim maxScore As Long
|
||||
maxScore = -1
|
||||
Dim bestConflictKeys As String
|
||||
bestConflictKeys = ""
|
||||
|
||||
Dim item As BomItem
|
||||
|
||||
' 遍历BOM库中同类别的所有物料
|
||||
For Each item In allBomItems
|
||||
If item.category = category And Trim(item.SelectCondition) <> "" Then
|
||||
|
||||
Dim allowed As Object
|
||||
Set allowed = CreateObject("Scripting.Dictionary")
|
||||
Dim forbidden As Object
|
||||
Set forbidden = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 使用正则提取该物料的所有约束条件 (如 azxs=A0)
|
||||
Dim matches As Object
|
||||
Set matches = regEx.Execute(item.SelectCondition)
|
||||
|
||||
Dim match As Object
|
||||
For Each match In matches
|
||||
Dim k As String, op As String, v As String
|
||||
k = match.SubMatches(0)
|
||||
op = Trim(match.SubMatches(1))
|
||||
v = Trim(match.SubMatches(2))
|
||||
|
||||
If op = "=" Then
|
||||
If Not allowed.Exists(k) Then allowed(k) = "|"
|
||||
allowed(k) = allowed(k) & v & "|"
|
||||
ElseIf op = "!=" Or op = "<>" Then
|
||||
If Not forbidden.Exists(k) Then forbidden(k) = "|"
|
||||
forbidden(k) = forbidden(k) & v & "|"
|
||||
End If
|
||||
Next match
|
||||
|
||||
' 合并出现过的所有参数键
|
||||
Dim allRuleKeys As Object
|
||||
Set allRuleKeys = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim vKey As Variant
|
||||
For Each vKey In allowed.Keys: allRuleKeys(vKey) = True: Next vKey
|
||||
For Each vKey In forbidden.Keys: allRuleKeys(vKey) = True: Next vKey
|
||||
|
||||
Dim currentScore As Long
|
||||
currentScore = 0
|
||||
Dim currentConflicts As String
|
||||
currentConflicts = ""
|
||||
|
||||
' 计算该物料与实际订单参数的重合度得分
|
||||
Dim keyVar As Variant
|
||||
For Each keyVar In allRuleKeys.Keys
|
||||
Dim keyStr As String
|
||||
keyStr = CStr(keyVar)
|
||||
|
||||
Dim prodVal As String
|
||||
If productConds.Exists(keyStr) Then prodVal = productConds(keyStr) Else prodVal = ""
|
||||
|
||||
Dim isMatch As Boolean
|
||||
isMatch = False
|
||||
|
||||
If allowed.Exists(keyStr) Then
|
||||
' 如果实际值包含在允许值中,则得分
|
||||
If InStr(allowed(keyStr), "|" & prodVal & "|") > 0 Then
|
||||
isMatch = True
|
||||
End If
|
||||
ElseIf forbidden.Exists(keyStr) Then
|
||||
' 如果没有允许值限制,只有禁止值限制,且实际值不在禁止值中,则得分
|
||||
If InStr(forbidden(keyStr), "|" & prodVal & "|") = 0 Then
|
||||
isMatch = True
|
||||
End If
|
||||
End If
|
||||
|
||||
If isMatch Then
|
||||
' 【核心修改】引入特征权重,让系统具备业务直觉
|
||||
currentScore = currentScore + GetFeatureWeight(keyStr)
|
||||
Else
|
||||
currentConflicts = currentConflicts & keyStr & ","
|
||||
End If
|
||||
Next keyVar
|
||||
|
||||
' 更新最高得分记录
|
||||
If currentScore > maxScore Then
|
||||
maxScore = currentScore
|
||||
bestConflictKeys = currentConflicts
|
||||
ElseIf currentScore = maxScore And currentScore > 0 Then
|
||||
' 如果权重得分依然相同,合并所有可能的冲突原因
|
||||
Dim keysArray() As String
|
||||
keysArray = Split(currentConflicts, ",")
|
||||
Dim cKey As Variant
|
||||
For Each cKey In keysArray
|
||||
If Trim(cKey) <> "" And InStr(bestConflictKeys, cKey & ",") = 0 Then
|
||||
bestConflictKeys = bestConflictKeys & cKey & ","
|
||||
End If
|
||||
Next cKey
|
||||
End If
|
||||
|
||||
End If
|
||||
Next item
|
||||
|
||||
' 将最高分的冲突Key翻译为实际的参数值
|
||||
If bestConflictKeys <> "" Then
|
||||
Dim resultStr As String
|
||||
resultStr = ""
|
||||
Dim finalKeys() As String
|
||||
finalKeys = Split(bestConflictKeys, ",")
|
||||
|
||||
Dim fKey As Variant
|
||||
For Each fKey In finalKeys
|
||||
If Trim(fKey) <> "" Then
|
||||
Dim actVal As String
|
||||
If productConds.Exists(fKey) Then actVal = productConds(fKey) Else actVal = "无值"
|
||||
|
||||
' 使用 | 作为条目分隔符,使用 : 分隔键和值
|
||||
Dim pairStr As String
|
||||
pairStr = fKey & ":" & actVal
|
||||
|
||||
If resultStr = "" Then
|
||||
resultStr = pairStr
|
||||
Else
|
||||
If InStr("|" & resultStr & "|", "|" & pairStr & "|") = 0 Then
|
||||
resultStr = resultStr & "|" & pairStr
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next fKey
|
||||
|
||||
If resultStr <> "" Then
|
||||
FindUnmatchedParameter = resultStr
|
||||
Else
|
||||
FindUnmatchedParameter = "无法精准定位:无法精准定位"
|
||||
End If
|
||||
Else
|
||||
FindUnmatchedParameter = "无法精准定位:无法精准定位"
|
||||
End If
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 辅助函数: GetFeatureWeight
|
||||
' 功能: 获取字段的匹配权重,严格保证高优先级特征的决定性
|
||||
'=====================================================================
|
||||
Private Function GetFeatureWeight(keyStr As String) As Long
|
||||
Select Case LCase(Trim(keyStr))
|
||||
Case "azxs"
|
||||
GetFeatureWeight = 10000 ' 安装形式 - 决定物理结构,最重要
|
||||
Case "bkxs"
|
||||
GetFeatureWeight = 1000 ' 表壳形式
|
||||
Case "gclj"
|
||||
GetFeatureWeight = 100 ' 过程连接
|
||||
Case "jycz"
|
||||
GetFeatureWeight = 50 ' 接液材质
|
||||
Case "lcfw"
|
||||
GetFeatureWeight = 10 ' 量程范围
|
||||
Case "fjgn"
|
||||
GetFeatureWeight = 1 ' 附加功能
|
||||
Case Else
|
||||
GetFeatureWeight = 0
|
||||
End Select
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 辅助函数: ExtractCategoryName
|
||||
' 功能: 从报错文本如 "必需类别[部件]未匹配" 中提取出 "部件"
|
||||
'=====================================================================
|
||||
Private Function ExtractCategoryName(errorMsg As String) As String
|
||||
Dim startPos As Long
|
||||
Dim endPos As Long
|
||||
startPos = InStr(errorMsg, "[")
|
||||
endPos = InStr(errorMsg, "]")
|
||||
|
||||
If startPos > 0 And endPos > startPos Then
|
||||
ExtractCategoryName = Mid(errorMsg, startPos + 1, endPos - startPos - 1)
|
||||
Else
|
||||
ExtractCategoryName = ""
|
||||
End If
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteOutputHeader
|
||||
'=====================================================================
|
||||
Private Sub WriteOutputHeader(ws As Worksheet)
|
||||
Dim col As Long
|
||||
col = 1
|
||||
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "总排号": col = col + 1
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "生产订单号": col = col + 1
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "产品型号": col = col + 1
|
||||
|
||||
Dim configs() As String
|
||||
configs = Split(CONDITION_CONFIG, "|")
|
||||
Dim i As Long
|
||||
For i = LBound(configs) To UBound(configs)
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = Trim(Split(configs(i), ",")(1))
|
||||
col = col + 1
|
||||
Next i
|
||||
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "未匹配参数": col = col + 1
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "未匹配参数值": col = col + 1
|
||||
ws.Cells(OUTPUT_HEADER_ROW, col).value = "提取备注": col = col + 1
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: CreateErrorRowArray
|
||||
' 功能: 构建输出的一行数据
|
||||
'=====================================================================
|
||||
Private Function CreateErrorRowArray(totalQueueNum As String, orderNumber As String, _
|
||||
modelStr As String, conditions As Object, _
|
||||
unmatchedParam As String, unmatchedValue As String, errorNote As String) As Variant()
|
||||
Dim configs() As String
|
||||
configs = Split(CONDITION_CONFIG, "|")
|
||||
|
||||
Dim totalCols As Long
|
||||
totalCols = 3 + UBound(configs) - LBound(configs) + 1 + 3
|
||||
|
||||
ReDim rowData(1 To totalCols) As Variant
|
||||
Dim col As Long
|
||||
col = 1
|
||||
|
||||
rowData(col) = totalQueueNum: col = col + 1
|
||||
rowData(col) = orderNumber: col = col + 1
|
||||
rowData(col) = modelStr: col = col + 1
|
||||
|
||||
Dim i As Long
|
||||
For i = LBound(configs) To UBound(configs)
|
||||
Dim key As String
|
||||
key = Trim(Split(configs(i), ",")(0))
|
||||
If conditions.Exists(key) Then
|
||||
rowData(col) = conditions(key)
|
||||
Else
|
||||
rowData(col) = ""
|
||||
End If
|
||||
col = col + 1
|
||||
Next i
|
||||
|
||||
rowData(col) = unmatchedParam: col = col + 1
|
||||
rowData(col) = unmatchedValue: col = col + 1
|
||||
rowData(col) = errorNote: col = col + 1
|
||||
|
||||
CreateErrorRowArray = rowData
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteBatchData
|
||||
'=====================================================================
|
||||
Private Sub WriteBatchData(ws As Worksheet, outputData As Collection)
|
||||
Dim firstRow As Variant
|
||||
firstRow = outputData(1)
|
||||
|
||||
Dim rowCount As Long
|
||||
Dim colCount As Long
|
||||
rowCount = outputData.count
|
||||
colCount = UBound(firstRow) - LBound(firstRow) + 1
|
||||
|
||||
Dim resultData() As Variant
|
||||
ReDim resultData(1 To rowCount, 1 To colCount)
|
||||
|
||||
Dim i As Long, j As Long
|
||||
Dim rowArray As Variant
|
||||
For i = 1 To rowCount
|
||||
rowArray = outputData(i)
|
||||
For j = 1 To colCount
|
||||
resultData(i, j) = rowArray(j)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
' 数据从表头的下一行开始写入
|
||||
ws.Cells(OUTPUT_HEADER_ROW + 1, 1).Resize(rowCount, colCount).value = resultData
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 辅助过程
|
||||
'=====================================================================
|
||||
Private Function GetOrderSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetOrderSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
If GetOrderSheet Is Nothing Then Set GetOrderSheet = ActiveSheet
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
Private Function GetBomSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetBomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
Private Function CreateErrorOutputSheet() As Worksheet
|
||||
Dim wsName As String
|
||||
wsName = "BOM匹配异常报表"
|
||||
On Error Resume Next
|
||||
Set CreateErrorOutputSheet = ThisWorkbook.Worksheets(wsName)
|
||||
On Error GoTo 0
|
||||
|
||||
If CreateErrorOutputSheet Is Nothing Then
|
||||
Set CreateErrorOutputSheet = ThisWorkbook.Worksheets.Add
|
||||
CreateErrorOutputSheet.Name = wsName
|
||||
Else
|
||||
' 只清空表头及其以下的数据,保留表头以上的可能存在的内容
|
||||
CreateErrorOutputSheet.Rows(OUTPUT_HEADER_ROW & ":" & CreateErrorOutputSheet.Rows.count).Clear
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Sub FormatOutputSheet(ws As Worksheet)
|
||||
On Error Resume Next
|
||||
With ws.Rows(OUTPUT_HEADER_ROW)
|
||||
.Font.Bold = True
|
||||
.Interior.Color = RGB(244, 176, 132) ' 橙色背景,突出异常属性
|
||||
.HorizontalAlignment = xlCenter
|
||||
End With
|
||||
|
||||
' 将"未匹配参数"列和"未匹配参数值"列加粗显示,颜色标红
|
||||
Dim unmatchValCol As Long
|
||||
unmatchValCol = ws.Cells(OUTPUT_HEADER_ROW, ws.Columns.count).End(xlToLeft).Column - 1
|
||||
Dim unmatchParamCol As Long
|
||||
unmatchParamCol = unmatchValCol - 1
|
||||
|
||||
If unmatchParamCol > 0 Then
|
||||
ws.Columns(unmatchParamCol).Font.Color = RGB(255, 0, 0)
|
||||
ws.Columns(unmatchParamCol).Font.Bold = True
|
||||
ws.Columns(unmatchValCol).Font.Color = RGB(255, 0, 0)
|
||||
ws.Columns(unmatchValCol).Font.Bold = True
|
||||
End If
|
||||
|
||||
On Error GoTo 0
|
||||
End Sub
|
||||
|
||||
Private Sub RestoreAppStatus()
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Application.ScreenUpdating = True
|
||||
End Sub
|
||||
476
VBA/Modules/MainModule.bas
Normal file
476
VBA/Modules/MainModule.bas
Normal file
@@ -0,0 +1,476 @@
|
||||
'=====================================================================
|
||||
' 模块名: MainModule
|
||||
' 功能: 主控模块,处理产品型号提取和BOM匹配的上层逻辑
|
||||
' 特性: [已重构] 支持仅对筛选后的数据进行处理,采用内存极速读取
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 常量定义
|
||||
'=====================================================================
|
||||
' 提取条件配置(可灵活扩展)
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ProcessProductModels
|
||||
' 功能: 批量处理产品型号并输出结果
|
||||
' 说明: 这是主入口程序
|
||||
'=====================================================================
|
||||
Public Sub ProcessProductModels()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
' 关闭屏幕刷新提升速度
|
||||
Application.ScreenUpdating = False
|
||||
|
||||
' 准备输入输出
|
||||
Dim inputSheet As Worksheet
|
||||
Dim outputSheet As Worksheet
|
||||
Dim bomSheet As Worksheet
|
||||
|
||||
' 获取工作表
|
||||
Set inputSheet = GetInputSheet()
|
||||
If inputSheet Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "未找到输入工作表,请确保工作簿中有包含订单数据的工作表", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 获取BOM库工作表
|
||||
Set bomSheet = GetBomSheet()
|
||||
If bomSheet Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "未找到'平台配置清单'工作表,请确保BOM数据存在", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 创建或获取输出工作表
|
||||
Set outputSheet = CreateOutputSheet()
|
||||
|
||||
' 初始化BOM提取器
|
||||
Dim BomExtractor As BomExtractor
|
||||
Set BomExtractor = New BomExtractor
|
||||
BomExtractor.SetWorksheet bomSheet
|
||||
|
||||
If Not BomExtractor.LoadBomData Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim lastRow As Long
|
||||
lastRow = inputSheet.Cells(inputSheet.Rows.count, 1).End(xlUp).row
|
||||
|
||||
If lastRow < 2 Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "[产品订单]工作表中没有数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 【性能核心】将输入数据全量读入内存数组
|
||||
Dim sourceDataArr As Variant
|
||||
sourceDataArr = inputSheet.Range("A2:F" & lastRow).value
|
||||
|
||||
' 【筛选核心】获取可见的单元格区域
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = inputSheet.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 写入输出表头
|
||||
WriteOutputHeader outputSheet
|
||||
|
||||
' 收集所有输出数据
|
||||
Dim outputData As Collection
|
||||
Set outputData = New Collection
|
||||
|
||||
Dim cell As Range
|
||||
Dim arrIndex As Long
|
||||
Dim modelString As String
|
||||
Dim processedCount As Long
|
||||
|
||||
processedCount = 0
|
||||
|
||||
' 仅遍历筛选出来的可见行
|
||||
For Each cell In visibleRange
|
||||
Dim totalQueueNum As String
|
||||
Dim orderNumber As String
|
||||
Dim componentPriority As String
|
||||
|
||||
' 将工作表行号映射到数组索引
|
||||
arrIndex = cell.row - 1
|
||||
|
||||
' 从内存数组中极速读取对应字段
|
||||
totalQueueNum = Trim(sourceDataArr(arrIndex, 1)) ' A列:总排号
|
||||
orderNumber = Trim(sourceDataArr(arrIndex, 2)) ' B列:生产订单号
|
||||
modelString = Trim(sourceDataArr(arrIndex, 3)) ' C列:产品型号
|
||||
componentPriority = Trim(sourceDataArr(arrIndex, 6)) ' F列:部件优先
|
||||
|
||||
If modelString <> "" Then
|
||||
' 处理单个型号,收集数据
|
||||
ProcessSingleModel totalQueueNum, orderNumber, modelString, componentPriority, BomExtractor, outputData
|
||||
processedCount = processedCount + 1
|
||||
End If
|
||||
Next cell
|
||||
|
||||
' 批量写入数据到工作表
|
||||
If outputData.count > 0 Then
|
||||
WriteBatchData outputSheet, outputData
|
||||
End If
|
||||
|
||||
' 格式化输出表
|
||||
FormatOutputSheet outputSheet
|
||||
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
Application.ScreenUpdating = True
|
||||
|
||||
MsgBox "处理完成!" & vbCrLf & _
|
||||
"处理筛选型号数: " & processedCount & vbCrLf & _
|
||||
"用时: " & Format(elapsedTime, "0.00") & "秒", vbInformation
|
||||
|
||||
' 激活输出表
|
||||
outputSheet.Activate
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
Application.ScreenUpdating = True
|
||||
MsgBox "处理异常: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ProcessSingleModel
|
||||
' 功能: 处理单个产品型号,将数据添加到输出集合
|
||||
' 参数: orderNumber - 生产订单号
|
||||
' modelString - 产品型号字符串
|
||||
' componentPriority - 部件优先标志("是"或"否")
|
||||
' bomExtractor - BOM提取器对象
|
||||
' outputData - 输出数据集合
|
||||
'=====================================================================
|
||||
Private Sub ProcessSingleModel(totalQueueNum As String, _
|
||||
orderNumber As String, _
|
||||
modelString As String, _
|
||||
componentPriority As String, _
|
||||
BomExtractor As BomExtractor, _
|
||||
outputData As Collection)
|
||||
On Error Resume Next
|
||||
|
||||
' 根据部件优先设置排除类别
|
||||
BomExtractor.ClearExcludeCategories
|
||||
If UCase(componentPriority) = "否" Or componentPriority = "0" Or componentPriority = "FALSE" Then
|
||||
Dim excludeCats As New Collection
|
||||
excludeCats.Add "部件"
|
||||
BomExtractor.SetExcludeCategories excludeCats
|
||||
End If
|
||||
|
||||
' 解析产品型号
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
Dim extractNote As String
|
||||
extractNote = ""
|
||||
|
||||
If Not parser.Parse(modelString) Then
|
||||
' 解析失败
|
||||
extractNote = "解析失败: " & parser.ErrorMessage
|
||||
outputData.Add CreateOutputRowArray(totalQueueNum, orderNumber, modelString, parser.conditions, extractNote, Nothing)
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 提取BOM
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = BomExtractor.ExtractBom(parser.conditions)
|
||||
|
||||
' 获取错误信息
|
||||
Dim bomErrors As String
|
||||
bomErrors = BomExtractor.GetErrorSummary
|
||||
If bomErrors <> "" Then
|
||||
extractNote = bomErrors
|
||||
End If
|
||||
|
||||
' 输出结果
|
||||
If matchedItems.count = 0 Then
|
||||
' 没有匹配项
|
||||
If extractNote = "" Then
|
||||
extractNote = "未匹配到任何物料"
|
||||
End If
|
||||
outputData.Add CreateOutputRowArray(totalQueueNum, orderNumber, modelString, parser.conditions, extractNote, Nothing)
|
||||
Else
|
||||
' 输出每个匹配的物料
|
||||
Dim item As BomItem
|
||||
Dim isFirst As Boolean
|
||||
isFirst = True
|
||||
|
||||
For Each item In matchedItems
|
||||
Dim itemNote As String
|
||||
itemNote = extractNote
|
||||
|
||||
' 添加物料特定的错误
|
||||
If item.MatchError <> "" Then
|
||||
If itemNote <> "" Then itemNote = itemNote & "; "
|
||||
itemNote = itemNote & item.MatchError
|
||||
End If
|
||||
|
||||
If isFirst Then
|
||||
' 首行保留总排号和订单号
|
||||
outputData.Add CreateOutputRowArray(totalQueueNum, orderNumber, modelString, parser.conditions, itemNote, item)
|
||||
isFirst = False
|
||||
Else
|
||||
' 同一个型号的后续BOM项,总排号和订单号留空以保持报表整洁
|
||||
outputData.Add CreateOutputRowArray("", "", modelString, parser.conditions, itemNote, item)
|
||||
End If
|
||||
Next item
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteOutputHeader
|
||||
' 功能: 写入输出表头
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Private Sub WriteOutputHeader(ws As Worksheet)
|
||||
Dim col As Long
|
||||
col = 1
|
||||
|
||||
' 新增总排号表头
|
||||
ws.Cells(1, col).value = "总排号": col = col + 1
|
||||
ws.Cells(1, col).value = "生产订单号": col = col + 1
|
||||
ws.Cells(1, col).value = "产品型号": col = col + 1
|
||||
|
||||
' 写入条件字段表头
|
||||
Dim conditions() As String
|
||||
Dim labels() As String
|
||||
GetConditionConfig conditions, labels
|
||||
|
||||
Dim i As Long
|
||||
For i = LBound(conditions) To UBound(conditions)
|
||||
ws.Cells(1, col).value = labels(i)
|
||||
col = col + 1
|
||||
Next i
|
||||
|
||||
' BOM字段表头
|
||||
ws.Cells(1, col).value = "行号": col = col + 1
|
||||
ws.Cells(1, col).value = "模块": col = col + 1
|
||||
' ws.Cells(1, col).Value = "代号": col = col + 1 <-- 已移除
|
||||
ws.Cells(1, col).value = "名称": col = col + 1
|
||||
ws.Cells(1, col).value = "数量": col = col + 1
|
||||
ws.Cells(1, col).value = "类别": col = col + 1
|
||||
ws.Cells(1, col).value = "66代码": col = col + 1
|
||||
ws.Cells(1, col).value = "提取备注": col = col + 1
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: CreateOutputRowArray
|
||||
' 功能: 创建输出行数据的数组
|
||||
' 参数: orderNumber - 生产订单号
|
||||
' FullModel - 完整型号
|
||||
' Conditions - 条件字典
|
||||
' note - 备注
|
||||
' item - BOM项(可为Nothing)
|
||||
' 返回: Variant() - 行数据数组
|
||||
'=====================================================================
|
||||
Private Function CreateOutputRowArray(totalQueueNum As String, _
|
||||
orderNumber As String, _
|
||||
FullModel As String, _
|
||||
conditions As Object, _
|
||||
note As String, _
|
||||
item As BomItem) As Variant()
|
||||
' 获取条件配置
|
||||
Dim condNames() As String
|
||||
Dim labels() As String
|
||||
GetConditionConfig condNames, labels
|
||||
|
||||
' 计算总列数:3 (排号+订单+型号) + 条件数 + 7 (BOM字段减去代号后剩6个 + 1个备注)
|
||||
Dim totalCols As Long
|
||||
totalCols = 3 + (UBound(condNames) - LBound(condNames) + 1) + 7
|
||||
|
||||
' 创建数组
|
||||
ReDim rowData(1 To totalCols) As Variant
|
||||
|
||||
Dim col As Long
|
||||
col = 1
|
||||
|
||||
' 基础信息
|
||||
rowData(col) = totalQueueNum: col = col + 1
|
||||
rowData(col) = orderNumber: col = col + 1
|
||||
rowData(col) = FullModel: col = col + 1
|
||||
|
||||
' 写入条件值
|
||||
Dim i As Long
|
||||
For i = LBound(condNames) To UBound(condNames)
|
||||
If conditions.Exists(condNames(i)) Then
|
||||
rowData(col) = conditions(condNames(i))
|
||||
Else
|
||||
rowData(col) = ""
|
||||
End If
|
||||
col = col + 1
|
||||
Next i
|
||||
|
||||
' 写入BOM数据
|
||||
If Not item Is Nothing Then
|
||||
rowData(col) = item.RowNumber: col = col + 1
|
||||
rowData(col) = item.Module: col = col + 1
|
||||
' rowData(col) = item.code: col = col + 1 <-- 已移除
|
||||
rowData(col) = item.Name: col = col + 1
|
||||
rowData(col) = item.Quantity: col = col + 1
|
||||
rowData(col) = item.category: col = col + 1
|
||||
rowData(col) = item.Code66: col = col + 1
|
||||
Else
|
||||
' 跳过BOM字段 (原本是7个字段,去掉代号后变成6个字段)
|
||||
col = col + 6
|
||||
End If
|
||||
|
||||
' 备注
|
||||
rowData(col) = note
|
||||
|
||||
CreateOutputRowArray = rowData
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: WriteBatchData
|
||||
' 功能: 批量写入数据到工作表
|
||||
' 参数: ws - 工作表对象
|
||||
' outputData - 输出数据集合
|
||||
'=====================================================================
|
||||
Private Sub WriteBatchData(ws As Worksheet, outputData As Collection)
|
||||
' 如果没有数据,直接返回
|
||||
If outputData.count = 0 Then
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 获取第一行数据来确定列数
|
||||
Dim firstRow As Variant
|
||||
firstRow = outputData(1)
|
||||
|
||||
Dim rowCount As Long
|
||||
Dim colCount As Long
|
||||
rowCount = outputData.count
|
||||
colCount = UBound(firstRow) - LBound(firstRow) + 1
|
||||
|
||||
' 创建二维数组
|
||||
Dim resultData() As Variant
|
||||
ReDim resultData(1 To rowCount, 1 To colCount)
|
||||
|
||||
' 填充数据到二维数组
|
||||
Dim i As Long
|
||||
Dim j As Long
|
||||
Dim rowArray As Variant
|
||||
|
||||
For i = 1 To rowCount
|
||||
rowArray = outputData(i)
|
||||
For j = 1 To colCount
|
||||
resultData(i, j) = rowArray(j)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
' 一次性写入工作表(从第2行开始)
|
||||
ws.Range("A2").Resize(rowCount, colCount).value = resultData
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: GetConditionConfig
|
||||
' 功能: 获取条件配置
|
||||
' 参数: outNames - 输出条件名称数组
|
||||
' outLabels - 输出条件标签数组
|
||||
'=====================================================================
|
||||
Private Sub GetConditionConfig(ByRef outNames() As String, ByRef outLabels() As String)
|
||||
Dim configs() As String
|
||||
configs = Split(CONDITION_CONFIG, "|")
|
||||
|
||||
ReDim outNames(LBound(configs) To UBound(configs))
|
||||
ReDim outLabels(LBound(configs) To UBound(configs))
|
||||
|
||||
Dim i As Long
|
||||
Dim parts() As String
|
||||
|
||||
For i = LBound(configs) To UBound(configs)
|
||||
parts = Split(configs(i), ",")
|
||||
outNames(i) = Trim(parts(0))
|
||||
outLabels(i) = Trim(parts(1))
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetInputSheet
|
||||
' 功能: 获取输入工作表
|
||||
' 返回: Worksheet - 输入工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetInputSheet() As Worksheet
|
||||
' 这里假设输入数据在当前活动工作表或名为"订单"的工作表
|
||||
On Error Resume Next
|
||||
Set GetInputSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
If GetInputSheet Is Nothing Then
|
||||
Set GetInputSheet = ActiveSheet
|
||||
End If
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: GetBomSheet
|
||||
' 功能: 获取BOM工作表
|
||||
' 返回: Worksheet - BOM工作表对象
|
||||
'=====================================================================
|
||||
Private Function GetBomSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetBomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 函数: CreateOutputSheet
|
||||
' 功能: 创建或获取输出工作表
|
||||
' 返回: Worksheet - 输出工作表对象
|
||||
'=====================================================================
|
||||
Private Function CreateOutputSheet() As Worksheet
|
||||
Dim wsName As String
|
||||
wsName = "BOM提取结果"
|
||||
|
||||
On Error Resume Next
|
||||
Set CreateOutputSheet = ThisWorkbook.Worksheets(wsName)
|
||||
On Error GoTo 0
|
||||
|
||||
If CreateOutputSheet Is Nothing Then
|
||||
Set CreateOutputSheet = ThisWorkbook.Worksheets.Add
|
||||
CreateOutputSheet.Name = wsName
|
||||
Else
|
||||
' 清空现有数据
|
||||
CreateOutputSheet.Cells.Clear
|
||||
End If
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 过程: FormatOutputSheet
|
||||
' 功能: 格式化输出工作表
|
||||
' 参数: ws - 工作表对象
|
||||
'=====================================================================
|
||||
Private Sub FormatOutputSheet(ws As Worksheet)
|
||||
On Error Resume Next
|
||||
|
||||
' 设置表头格式
|
||||
With ws.Rows(1)
|
||||
.Font.Bold = True
|
||||
.Interior.Color = RGB(217, 217, 217)
|
||||
.HorizontalAlignment = xlCenter
|
||||
End With
|
||||
|
||||
' ' 自动调整列宽
|
||||
' ws.Columns.AutoFit
|
||||
'
|
||||
' ' 冻结首行
|
||||
' ws.Rows(2).Select
|
||||
' 'ActiveWindow.FreezePanes = True
|
||||
' ws.Cells(1, 1).Select
|
||||
|
||||
On Error GoTo 0
|
||||
End Sub
|
||||
217
VBA/Modules/OrderValidationModule.bas
Normal file
217
VBA/Modules/OrderValidationModule.bas
Normal file
@@ -0,0 +1,217 @@
|
||||
'=====================================================================
|
||||
' 模块名: OrderValidationModule
|
||||
' 功能: 订单物料有效性检查模块
|
||||
' 说明: 检查[产品订单]中可见行的产品型号是否能成功提取BOM。
|
||||
' 如果发生任何提取错误或无法匹配物料,则在G列[是否领料]写入"否"。
|
||||
' 特性: 采用内存极速读取,仅对筛选后的数据进行处理。
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 过程: ValidateOrderMaterials
|
||||
' 功能: 批量检查可见订单的BOM提取有效性
|
||||
'=====================================================================
|
||||
Public Sub ValidateOrderMaterials()
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim startTime As Double
|
||||
startTime = Timer
|
||||
|
||||
' 提升性能:关闭屏幕更新和自动计算
|
||||
Application.ScreenUpdating = False
|
||||
Application.Calculation = xlCalculationManual
|
||||
|
||||
' 获取工作表
|
||||
Dim orderSheet As Worksheet
|
||||
Dim bomSheet As Worksheet
|
||||
|
||||
Set orderSheet = GetOrderSheet()
|
||||
If orderSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[产品订单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Set bomSheet = GetBomSheet()
|
||||
If bomSheet Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "未找到[平台配置清单]工作表!", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化BOM提取器
|
||||
Dim BomExtractor As BomExtractor
|
||||
Set BomExtractor = New BomExtractor
|
||||
BomExtractor.SetWorksheet bomSheet
|
||||
|
||||
If Not BomExtractor.LoadBomData Then
|
||||
RestoreAppStatus
|
||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 写入G列表头
|
||||
orderSheet.Cells(1, 7).value = "是否领料"
|
||||
|
||||
Dim lastRow As Long
|
||||
lastRow = orderSheet.Cells(orderSheet.Rows.count, 3).End(xlUp).row
|
||||
|
||||
If lastRow < 2 Then
|
||||
RestoreAppStatus
|
||||
MsgBox "[产品订单]工作表中没有需要处理的数据!", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 【性能核心】将输入数据全量读入内存数组 (读取A到F列即可)
|
||||
Dim sourceDataArr As Variant
|
||||
sourceDataArr = orderSheet.Range("A2:F" & lastRow).value
|
||||
|
||||
' 【筛选核心】获取可见的单元格区域 (A列)
|
||||
Dim visibleRange As Range
|
||||
On Error Resume Next
|
||||
Set visibleRange = orderSheet.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If visibleRange Is Nothing Then
|
||||
RestoreAppStatus
|
||||
MsgBox "当前筛选状态下没有可见的数据。", vbInformation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim cell As Range
|
||||
Dim arrIndex As Long
|
||||
Dim modelString As String
|
||||
Dim componentPriority As String
|
||||
|
||||
Dim processedCount As Long
|
||||
Dim invalidCount As Long
|
||||
|
||||
processedCount = 0
|
||||
invalidCount = 0
|
||||
|
||||
' 仅遍历筛选出来的可见行
|
||||
For Each cell In visibleRange
|
||||
' 将工作表行号映射到数组索引 (数据从第2行开始,所以数组索引 = 行号 - 1)
|
||||
arrIndex = cell.row - 1
|
||||
|
||||
' 从内存数组中极速读取所需的关键字段
|
||||
modelString = Trim(sourceDataArr(arrIndex, 3)) ' C列:产品型号
|
||||
componentPriority = Trim(sourceDataArr(arrIndex, 6)) ' F列:部件优先
|
||||
|
||||
If modelString <> "" Then
|
||||
processedCount = processedCount + 1
|
||||
|
||||
' 调用校验逻辑,判断是否存在BOM提取错误
|
||||
If IsInvalidOrderBOM(modelString, componentPriority, BomExtractor) Then
|
||||
' 如果无效/有报错,直接在对应行的第7列(G列)写入"否"
|
||||
' 正常订单不做任何处理,保留原样
|
||||
orderSheet.Cells(cell.row, 7).value = "否"
|
||||
invalidCount = invalidCount + 1
|
||||
End If
|
||||
End If
|
||||
Next cell
|
||||
|
||||
' 恢复应用状态
|
||||
RestoreAppStatus
|
||||
|
||||
Dim elapsedTime As Double
|
||||
elapsedTime = Timer - startTime
|
||||
|
||||
MsgBox "有效性检查完成!" & vbCrLf & _
|
||||
"共检查了 " & processedCount & " 个筛选订单。" & vbCrLf & _
|
||||
"发现并标记了 " & invalidCount & " 个无效/报错订单。" & vbCrLf & _
|
||||
"用时: " & Format(elapsedTime, "0.00") & " 秒", vbInformation
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
RestoreAppStatus
|
||||
MsgBox "检查订单物料有效性时发生异常: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 函数: IsInvalidOrderBOM
|
||||
' 功能: 模拟BOM提取过程,判定该订单是否存在错误
|
||||
' 参数: modelString - 产品型号
|
||||
' componentPriority - 部件优先标识
|
||||
' BomExtractor - 已初始化的BOM提取器对象
|
||||
' 返回: Boolean - 只要发生任何错误或未匹配到物料,则返回 True
|
||||
'=====================================================================
|
||||
Private Function IsInvalidOrderBOM(modelString As String, _
|
||||
componentPriority As String, _
|
||||
BomExtractor As BomExtractor) As Boolean
|
||||
On Error Resume Next
|
||||
|
||||
' 默认认为它是有效的,直到发现错误
|
||||
IsInvalidOrderBOM = False
|
||||
|
||||
' 1. 根据部件优先设置排除类别
|
||||
BomExtractor.ClearExcludeCategories
|
||||
If UCase(componentPriority) = "否" Or componentPriority = "0" Or componentPriority = "FALSE" Then
|
||||
Dim excludeCats As New Collection
|
||||
excludeCats.Add "部件"
|
||||
BomExtractor.SetExcludeCategories excludeCats
|
||||
End If
|
||||
|
||||
' 2. 解析产品型号
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
If Not parser.Parse(modelString) Then
|
||||
' 解析失败,属于无效订单
|
||||
IsInvalidOrderBOM = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 3. 提取BOM
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = BomExtractor.ExtractBom(parser.conditions)
|
||||
|
||||
' 4. 检查 BOM 提取器全局错误日志
|
||||
If BomExtractor.GetErrorSummary <> "" Then
|
||||
IsInvalidOrderBOM = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 5. 检查是否完全没有匹配到物料
|
||||
If matchedItems.count = 0 Then
|
||||
IsInvalidOrderBOM = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 6. 深度检查:遍历提取出的每一项,看是否存在子项报错
|
||||
Dim item As BomItem
|
||||
For Each item In matchedItems
|
||||
If item.MatchError <> "" Then
|
||||
IsInvalidOrderBOM = True
|
||||
Exit Function
|
||||
End If
|
||||
Next item
|
||||
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
'=====================================================================
|
||||
' 辅助过程: RestoreAppStatus
|
||||
' 功能: 恢复Excel应用程序的状态
|
||||
'=====================================================================
|
||||
Private Sub RestoreAppStatus()
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Application.ScreenUpdating = True
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 辅助函数: 获取所需工作表
|
||||
'=====================================================================
|
||||
Private Function GetOrderSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetOrderSheet = ThisWorkbook.Worksheets("产品订单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
Private Function GetBomSheet() As Worksheet
|
||||
On Error Resume Next
|
||||
Set GetBomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
336
VBA/Modules/TestModule.bas
Normal file
336
VBA/Modules/TestModule.bas
Normal file
@@ -0,0 +1,336 @@
|
||||
'=====================================================================
|
||||
' 模块名: TestModule
|
||||
' 功能: 单元测试模块
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 过程: RunAllTests
|
||||
' 功能: 运行所有测试
|
||||
'=====================================================================
|
||||
Public Sub RunAllTests()
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "开始运行所有测试"
|
||||
Debug.Print "时间: " & Now
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print ""
|
||||
|
||||
' 运行各个测试
|
||||
TestProductModelParser
|
||||
TestConditionEvaluator
|
||||
TestBomExtractor
|
||||
|
||||
Debug.Print ""
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "所有测试完成"
|
||||
Debug.Print "=========================================="
|
||||
|
||||
MsgBox "所有测试完成,请查看立即窗口查看结果", vbInformation
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestProductModelParser
|
||||
' 功能: 测试产品型号解析器
|
||||
'=====================================================================
|
||||
Public Sub TestProductModelParser()
|
||||
Debug.Print ">>> 测试 ProductModelParser"
|
||||
Debug.Print ""
|
||||
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
' 测试用例1: 正常型号
|
||||
Debug.Print "测试用例1: 正常型号"
|
||||
Dim testModel1 As String
|
||||
testModel1 = "YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3"
|
||||
|
||||
If parser.Parse(testModel1) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " 表头型号: " & parser.HeaderModel
|
||||
Debug.Print " 条件:"
|
||||
Debug.Print " azxs = " & parser.GetConditionValue("azxs")
|
||||
Debug.Print " bkxs = " & parser.GetConditionValue("bkxs")
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
Debug.Print " lcfw = " & parser.GetConditionValue("lcfw")
|
||||
|
||||
' 验证结果
|
||||
AssertEquals "azxs", "A0", parser.GetConditionValue("azxs")
|
||||
AssertEquals "bkxs", "531", parser.GetConditionValue("bkxs")
|
||||
AssertEquals "gclj", "M20", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "3", parser.GetConditionValue("jycz")
|
||||
AssertEquals "lcfw", "M06", parser.GetConditionValue("lcfw")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例2: 不同材质代码
|
||||
Debug.Print "测试用例2: 不同材质代码"
|
||||
Dim testModel2 As String
|
||||
testModel2 = "YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3"
|
||||
|
||||
If parser.Parse(testModel2) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
|
||||
AssertEquals "gclj", "M20", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "1", parser.GetConditionValue("jycz")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例3: 带附件的型号
|
||||
Debug.Print "测试用例3: 带附件的型号"
|
||||
Dim testModel3 As String
|
||||
testModel3 = "YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3"
|
||||
|
||||
If parser.Parse(testModel3) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
|
||||
AssertEquals "gclj", "G12", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "3", parser.GetConditionValue("jycz")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
Debug.Print "<<< ProductModelParser 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestConditionEvaluator
|
||||
' 功能: 测试条件评估器
|
||||
'=====================================================================
|
||||
Public Sub TestConditionEvaluator()
|
||||
Debug.Print ">>> 测试 ConditionEvaluator"
|
||||
Debug.Print ""
|
||||
|
||||
Dim evaluator As ConditionEvaluator
|
||||
Set evaluator = New ConditionEvaluator
|
||||
|
||||
' 创建测试条件字典
|
||||
Dim conditions As Object
|
||||
Set conditions = CreateObject("Scripting.Dictionary")
|
||||
conditions.Add "azxs", "A0"
|
||||
conditions.Add "bkxs", "531"
|
||||
conditions.Add "gclj", "M20"
|
||||
conditions.Add "jycz", "3"
|
||||
conditions.Add "lcfw", "M06"
|
||||
|
||||
' 测试用例1: 简单等式
|
||||
Debug.Print "测试用例1: 简单等式"
|
||||
Dim expr1 As String
|
||||
expr1 = "azxs=A0"
|
||||
Debug.Print " 表达式: " & expr1
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr1, conditions)
|
||||
AssertTrue "简单等式", evaluator.Evaluate(expr1, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例2: AND运算
|
||||
Debug.Print "测试用例2: AND运算"
|
||||
Dim expr2 As String
|
||||
expr2 = "azxs=A0 AND bkxs=531"
|
||||
Debug.Print " 表达式: " & expr2
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr2, conditions)
|
||||
AssertTrue "AND运算", evaluator.Evaluate(expr2, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例3: OR运算
|
||||
Debug.Print "测试用例3: OR运算"
|
||||
Dim expr3 As String
|
||||
expr3 = "azxs=AT OR azxs=A0"
|
||||
Debug.Print " 表达式: " & expr3
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr3, conditions)
|
||||
AssertTrue "OR运算", evaluator.Evaluate(expr3, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例4: !=运算
|
||||
Debug.Print "测试用例4: !=运算"
|
||||
Dim expr4 As String
|
||||
expr4 = "azxs!=AH"
|
||||
Debug.Print " 表达式: " & expr4
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr4, conditions)
|
||||
AssertTrue "!=运算", evaluator.Evaluate(expr4, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例5: 复杂嵌套
|
||||
Debug.Print "测试用例5: 复杂嵌套"
|
||||
Dim expr5 As String
|
||||
expr5 = "(azxs=A0 OR azxs=AT) AND (bkxs=531 OR bkxs=541)"
|
||||
Debug.Print " 表达式: " & expr5
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr5, conditions)
|
||||
AssertTrue "复杂嵌套", evaluator.Evaluate(expr5, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例6: 不存在的变量(!=情况)
|
||||
Debug.Print "测试用例6: 不存在的变量(!=情况)"
|
||||
Dim expr6 As String
|
||||
expr6 = "tsyq!=SCRJ"
|
||||
Debug.Print " 表达式: " & expr6
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr6, conditions)
|
||||
AssertTrue "不存在的变量!=", evaluator.Evaluate(expr6, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例7: 实际BOM条件
|
||||
Debug.Print "测试用例7: 实际BOM条件"
|
||||
Dim expr7 As String
|
||||
expr7 = "gclj=M20 AND jycz=1 AND lcfw=M01 AND (azxs=A0 OR azxs=AT OR azxs=AH)"
|
||||
Debug.Print " 表达式: " & expr7
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr7, conditions)
|
||||
' 这个应该是False,因为jycz=3,不是1
|
||||
AssertFalse "实际BOM条件(应该False)", evaluator.Evaluate(expr7, conditions)
|
||||
Debug.Print ""
|
||||
|
||||
Debug.Print "<<< ConditionEvaluator 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestBomExtractor
|
||||
' 功能: 测试BOM提取器(需要实际的工作表数据)
|
||||
'=====================================================================
|
||||
Public Sub TestBomExtractor()
|
||||
Debug.Print ">>> 测试 BomExtractor"
|
||||
Debug.Print ""
|
||||
|
||||
On Error Resume Next
|
||||
Dim bomSheet As Worksheet
|
||||
Set bomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
|
||||
If bomSheet Is Nothing Then
|
||||
Debug.Print "警告: 未找到'平台配置清单'工作表,跳过BomExtractor测试"
|
||||
Debug.Print ""
|
||||
Exit Sub
|
||||
End If
|
||||
On Error GoTo 0
|
||||
|
||||
Dim extractor As BomExtractor
|
||||
Set extractor = New BomExtractor
|
||||
extractor.SetWorksheet bomSheet
|
||||
|
||||
If Not extractor.LoadBomData Then
|
||||
Debug.Print "加载BOM数据失败: " & extractor.GetErrorSummary
|
||||
Debug.Print ""
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Debug.Print "BOM数据加载成功"
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例: 提取BOM
|
||||
Debug.Print "测试用例: 提取BOM"
|
||||
Dim testConditions As Object
|
||||
Set testConditions = CreateObject("Scripting.Dictionary")
|
||||
testConditions.Add "azxs", "A0"
|
||||
testConditions.Add "bkxs", "531"
|
||||
testConditions.Add "gclj", "M20"
|
||||
testConditions.Add "jycz", "1"
|
||||
testConditions.Add "lcfw", "M01"
|
||||
|
||||
Dim matchedItems As Collection
|
||||
Set matchedItems = extractor.ExtractBom(testConditions)
|
||||
|
||||
Debug.Print " 匹配到 " & matchedItems.count & " 个物料"
|
||||
|
||||
If matchedItems.count > 0 Then
|
||||
Debug.Print " 匹配的物料:"
|
||||
Dim item As BomItem
|
||||
Dim i As Long
|
||||
i = 1
|
||||
For Each item In matchedItems
|
||||
Debug.Print " " & i & ". " & item.ToString
|
||||
i = i + 1
|
||||
Next item
|
||||
End If
|
||||
|
||||
Dim errors As String
|
||||
errors = extractor.GetErrorSummary
|
||||
If errors <> "" Then
|
||||
Debug.Print " 错误信息: " & errors
|
||||
End If
|
||||
|
||||
Debug.Print ""
|
||||
Debug.Print "<<< BomExtractor 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 辅助测试函数
|
||||
'=====================================================================
|
||||
|
||||
Private Sub AssertEquals(testName As String, expected As String, actual As String)
|
||||
If expected = actual Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:" & expected & ", 实际:" & actual & ")"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub AssertTrue(testName As String, value As Boolean)
|
||||
If value Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:True, 实际:False)"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub AssertFalse(testName As String, value As Boolean)
|
||||
If Not value Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:False, 实际:True)"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestWithProvidedModels
|
||||
' 功能: 使用提供的测试型号进行测试
|
||||
'=====================================================================
|
||||
Public Sub TestWithProvidedModels()
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "使用提供的测试型号进行测试"
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print ""
|
||||
|
||||
Dim testModels() As String
|
||||
testModels = Split( _
|
||||
"YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3," & _
|
||||
"YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3," & _
|
||||
"YTHN-100.BZ.531.M201.M08.Y3|BP-088.2312.M08.0A3," & _
|
||||
"YTHN-100.A0.531.M201.M08.Y3|BP-088.2312.M08.0B3," & _
|
||||
"YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3|LSG-1.14x2.M20F.M20.3^HDJ.M20F.BW.14×2×60.3^TSFJ^WHP.70X20X1.3," & _
|
||||
"YTHN-100.A0.531.M203.P21.Y3|BP-088.2312.M39.0A3|HDJ.M20F.BW.14×2×60.3^LSG-1.14x2.M20F.M20.3^TSFJ^WHP.70X20X1.3," & _
|
||||
"YTHN-100.A0.531.M201.M03.N1.Y3|BP-088.2312.M31.0A4," & _
|
||||
"YTHN-100.A0.531.M201.M04.Y3|BP-088.2312.M32.0A3," & _
|
||||
"YTHN-100.A0.531.Z121.M07.Y3|BP-088.2312.M07.0A3," & _
|
||||
"YTHN-100.A0.531.Z121.M08.Y3|BP-088.2312.M08.0A3", _
|
||||
",")
|
||||
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
Dim i As Long
|
||||
For i = LBound(testModels) To UBound(testModels)
|
||||
Debug.Print "型号 " & (i + 1) & ": " & testModels(i)
|
||||
|
||||
If parser.Parse(testModels(i)) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " 表头: " & parser.HeaderModel
|
||||
Debug.Print " 条件: " & parser.GetAllConditions
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
Next i
|
||||
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "测试完成"
|
||||
Debug.Print "=========================================="
|
||||
End Sub
|
||||
53
VBA/vba_metadata.json
Normal file
53
VBA/vba_metadata.json
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"source_file": "C:\\Users\\Administrator\\Desktop\\新BOM\\AutoBOM\\YTHN-100.xlsm",
|
||||
"modules": {
|
||||
"Sheet9.cls": {
|
||||
"name": "Sheet9",
|
||||
"type": "DocumentModules",
|
||||
"attributes": {},
|
||||
"file": "DocumentModules\\Sheet9.cls"
|
||||
},
|
||||
"MainModule.bas": {
|
||||
"name": "MainModule",
|
||||
"type": "Modules",
|
||||
"attributes": {},
|
||||
"file": "Modules\\MainModule.bas"
|
||||
},
|
||||
"TestModule.bas": {
|
||||
"name": "TestModule",
|
||||
"type": "Modules",
|
||||
"attributes": {},
|
||||
"file": "Modules\\TestModule.bas"
|
||||
},
|
||||
"BomExtractor.cls": {
|
||||
"name": "BomExtractor",
|
||||
"type": "ClassModules",
|
||||
"attributes": {},
|
||||
"file": "ClassModules\\BomExtractor.cls"
|
||||
},
|
||||
"BomItem.cls": {
|
||||
"name": "BomItem",
|
||||
"type": "ClassModules",
|
||||
"attributes": {},
|
||||
"file": "ClassModules\\BomItem.cls"
|
||||
},
|
||||
"ConditionEvaluator.cls": {
|
||||
"name": "ConditionEvaluator",
|
||||
"type": "ClassModules",
|
||||
"attributes": {},
|
||||
"file": "ClassModules\\ConditionEvaluator.cls"
|
||||
},
|
||||
"ProductModelParser.cls": {
|
||||
"name": "ProductModelParser",
|
||||
"type": "ClassModules",
|
||||
"attributes": {},
|
||||
"file": "ClassModules\\ProductModelParser.cls"
|
||||
},
|
||||
"BIPUploadModule.bas": {
|
||||
"name": "BIPUploadModule",
|
||||
"type": "Modules",
|
||||
"attributes": {},
|
||||
"file": "Modules\\BIPUploadModule.bas"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 类模块: clsErrorLogger
|
||||
' 职责: 错误日志记录器 (修正版:移除UDT,使用数组存储)
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
Private pErrors As Collection
|
||||
Private pWarnings As Collection
|
||||
|
||||
Private Sub Class_Initialize()
|
||||
Set pErrors = New Collection
|
||||
Set pWarnings = New Collection
|
||||
End Sub
|
||||
|
||||
' 记录错误
|
||||
Public Sub Record(OrderNo As String, SourceFunc As String, ErrorType As String, Desc As String, Context As String)
|
||||
' 使用数组存储单条错误信息:生产订单号, 来源, 类型, 描述, 上下文
|
||||
pErrors.Add Array(OrderNo, SourceFunc, ErrorType, Desc, Context)
|
||||
End Sub
|
||||
|
||||
' 是否有错误
|
||||
Public Property Get HasErrors() As Boolean
|
||||
HasErrors = (pErrors.count > 0)
|
||||
End Property
|
||||
|
||||
' 是否有警告
|
||||
Public Property Get HasWarnings() As Boolean
|
||||
HasWarnings = (pWarnings.count > 0)
|
||||
End Property
|
||||
|
||||
' 是否有问题(错误或警告)
|
||||
Public Property Get HasIssues() As Boolean
|
||||
HasIssues = (pErrors.count > 0 Or pWarnings.count > 0)
|
||||
End Property
|
||||
|
||||
' 记录警告
|
||||
Public Sub RecordWarning(OrderNo As String, SourceFunc As String, WarningType As String, Desc As String, Context As String)
|
||||
pWarnings.Add Array(OrderNo, SourceFunc, WarningType, Desc, Context)
|
||||
End Sub
|
||||
|
||||
' 输出报告到新工作表
|
||||
Public Sub PrintReport(targetWb As Workbook)
|
||||
If pErrors.count = 0 And pWarnings.count = 0 Then Exit Sub
|
||||
|
||||
Dim ws As Worksheet
|
||||
Set ws = targetWb.Worksheets.Add(After:=targetWb.Worksheets(targetWb.Worksheets.count))
|
||||
ws.Name = "错误报告_" & Format(Now, "hhmmss")
|
||||
|
||||
' 表头 (增加"类型"列)
|
||||
ws.Range("A1:F1").Value = Array("类型", "生产订单号", "来源模块", "错误类型", "详细描述", "原始数据")
|
||||
ws.Range("A1:F1").Font.Bold = True
|
||||
ws.Range("A1:F1").Interior.Color = RGB(217, 217, 217)
|
||||
|
||||
' 准备输出数组
|
||||
Dim totalIssues As Long
|
||||
totalIssues = pErrors.count + pWarnings.count
|
||||
|
||||
Dim arrOutput() As Variant
|
||||
ReDim arrOutput(1 To totalIssues, 1 To 6)
|
||||
|
||||
Dim i As Long
|
||||
Dim vItem As Variant
|
||||
|
||||
' 先输出错误
|
||||
For i = 1 To pErrors.count
|
||||
vItem = pErrors(i)
|
||||
arrOutput(i, 1) = "错误"
|
||||
arrOutput(i, 2) = vItem(0)
|
||||
arrOutput(i, 3) = vItem(1)
|
||||
arrOutput(i, 4) = vItem(2)
|
||||
arrOutput(i, 5) = vItem(3)
|
||||
arrOutput(i, 6) = vItem(4)
|
||||
Next i
|
||||
|
||||
' 再输出警告
|
||||
For i = 1 To pWarnings.count
|
||||
vItem = pWarnings(i)
|
||||
arrOutput(pErrors.count + i, 1) = "警告"
|
||||
arrOutput(pErrors.count + i, 2) = vItem(0)
|
||||
arrOutput(pErrors.count + i, 3) = vItem(1)
|
||||
arrOutput(pErrors.count + i, 4) = vItem(2)
|
||||
arrOutput(pErrors.count + i, 5) = vItem(3)
|
||||
arrOutput(pErrors.count + i, 6) = vItem(4)
|
||||
Next i
|
||||
|
||||
ws.Range("A2").Resize(totalIssues, 6).Value = arrOutput
|
||||
|
||||
' 标记错误和警告行
|
||||
If totalIssues > 0 Then
|
||||
Dim rngErrors As Range
|
||||
Dim rngWarnings As Range
|
||||
|
||||
If pErrors.count > 0 Then
|
||||
Set rngErrors = ws.Range("A2").Resize(pErrors.count, 6)
|
||||
rngErrors.Interior.Color = RGB(255, 200, 200)
|
||||
End If
|
||||
|
||||
If pWarnings.count > 0 Then
|
||||
Set rngWarnings = ws.Range("A" & (pErrors.count + 2)).Resize(pWarnings.count, 6)
|
||||
rngWarnings.Interior.Color = RGB(255, 255, 200)
|
||||
End If
|
||||
End If
|
||||
|
||||
ws.Columns.AutoFit
|
||||
End Sub
|
||||
@@ -1,3 +0,0 @@
|
||||
Private Sub CommandButton1_Click()
|
||||
Call RunBOMConversion
|
||||
End Sub
|
||||
@@ -1,3 +0,0 @@
|
||||
Private Sub CommandButton1_Click()
|
||||
Call RunBOMExtraction
|
||||
End Sub
|
||||
@@ -1,677 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M01_Main
|
||||
' 职责: 程序入口,调度器,UI交互(进度条)
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 入口1: 运行BOM转换系统(原功能)
|
||||
' 功能: 从"平台配置清单"读取数据,解析条件规则,生成分类BOM
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub RunBOMConversion()
|
||||
Dim wsSrc As Worksheet
|
||||
Dim arrData As Variant
|
||||
Dim logger As New clsErrorLogger
|
||||
Dim i As Long
|
||||
' 使用 Dictionary 替代 Collection 来存储类别,方便查找
|
||||
Dim catData As Object
|
||||
Set catData = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 1. 环境初始化
|
||||
On Error GoTo MainErrorHandler
|
||||
'Application.ScreenUpdating = False
|
||||
Application.Calculation = xlCalculationManual
|
||||
|
||||
' 2. 检查工作表
|
||||
On Error Resume Next
|
||||
Set wsSrc = ActiveWorkbook.Sheets("平台配置清单")
|
||||
On Error GoTo MainErrorHandler
|
||||
|
||||
If wsSrc Is Nothing Then
|
||||
MsgBox "错误:未找到名为 '平台配置清单' 的工作表。", vbCritical
|
||||
GoTo ExitHandler
|
||||
End If
|
||||
|
||||
' 读取数据
|
||||
Application.StatusBar = "正在读取源数据..."
|
||||
arrData = M02_DataIO.ReadSourceData(wsSrc)
|
||||
|
||||
If IsEmpty(arrData) Then
|
||||
MsgBox "未找到数据。", vbExclamation
|
||||
GoTo ExitHandler
|
||||
End If
|
||||
|
||||
' 3. 初始化逻辑模块
|
||||
M03_Logic.InitLogic logger
|
||||
|
||||
' 3.1 初始化预处理模块
|
||||
Dim wsMapping As Worksheet
|
||||
On Error Resume Next
|
||||
Set wsMapping = ActiveWorkbook.Sheets("对照表")
|
||||
On Error GoTo MainErrorHandler
|
||||
|
||||
If wsMapping Is Nothing Then
|
||||
MsgBox "警告:未找到 [对照表] 工作表,预处理功能将禁用。", vbExclamation
|
||||
Else
|
||||
M05_PreProcessor.InitPreProcessor logger, wsMapping
|
||||
End If
|
||||
|
||||
' 4. 主循环
|
||||
Dim rowIdx As Long
|
||||
Dim strCat As String, strCond As String
|
||||
Dim colResult As Collection
|
||||
Dim itm As Variant
|
||||
Dim baseInfo As Variant
|
||||
|
||||
Dim totalRows As Long
|
||||
Dim pct As Long
|
||||
totalRows = UBound(arrData, 1)
|
||||
|
||||
For i = 1 To totalRows
|
||||
rowIdx = M04_Config.SRC_START_ROW + i - 1
|
||||
strCat = Trim(CStr(arrData(i, M04_Config.COL_IDX_CAT)))
|
||||
|
||||
' --- 进度条更新 ---
|
||||
pct = CLng((i / totalRows) * 100)
|
||||
Application.StatusBar = "正在处理: " & pct & "% | 行: " & rowIdx & " | 类别: " & strCat
|
||||
If i Mod 10 = 0 Then DoEvents ' 每10行刷新一次界面,防止卡顿但不过度拖慢速度
|
||||
' ------------------
|
||||
|
||||
' 忽略空类别
|
||||
If Len(strCat) > 0 Then
|
||||
strCond = CStr(arrData(i, M04_Config.COL_IDX_COND))
|
||||
If IsEmpty(arrData(i, M04_Config.COL_IDX_COND)) Then strCond = ""
|
||||
|
||||
' 4.1 预处理条件(仅对"接头"类别)
|
||||
If Len(strCond) > 0 And M05_PreProcessor.IsInitialized() Then
|
||||
strCond = M05_PreProcessor.PreprocessCondition(strCond, strCat, rowIdx)
|
||||
End If
|
||||
|
||||
' 解析
|
||||
Set colResult = M03_Logic.ParseRule(strCond, rowIdx)
|
||||
|
||||
If Not colResult Is Nothing Then
|
||||
If Not catData.Exists(strCat) Then
|
||||
catData.Add strCat, New Collection
|
||||
End If
|
||||
|
||||
' 基础信息: 66代码, 名称, 数量(使用I列的66代码作为编码)
|
||||
baseInfo = Array(arrData(i, M04_Config.COL_IDX_CODE66), _
|
||||
arrData(i, M04_Config.COL_IDX_NAME), _
|
||||
arrData(i, M04_Config.COL_IDX_QTY))
|
||||
|
||||
' 将展开的记录存入
|
||||
For Each itm In colResult
|
||||
catData(strCat).Add Array(itm, baseInfo)
|
||||
Next itm
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 5. 输出
|
||||
Application.StatusBar = "正在生成新工作簿..."
|
||||
M02_DataIO.WriteCategoryToNewBook catData
|
||||
|
||||
' 6. 错误报告
|
||||
If logger.HasErrors Then
|
||||
logger.PrintReport ActiveWorkbook
|
||||
MsgBox "转换完成,但发现部分数据存在逻辑冲突,已生成错误报告。", vbExclamation
|
||||
End If
|
||||
|
||||
ExitHandler:
|
||||
' 清理状态
|
||||
Application.StatusBar = False
|
||||
Application.ScreenUpdating = True
|
||||
Application.Calculation = xlCalculationAutomatic
|
||||
Exit Sub
|
||||
|
||||
MainErrorHandler:
|
||||
MsgBox "发生运行时错误: " & err.Description, vbCritical
|
||||
Resume ExitHandler
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 入口2: 运行BOM自动提取系统(新功能)
|
||||
' 功能: 从产品型号中提取参数,匹配BOM库,输出标准BOM清单
|
||||
'
|
||||
' 使用方法:
|
||||
' 1. 在当前工作簿中准备"产品型号"列的工作表
|
||||
' 2. 确保BOM库.xlsx在同一目录下
|
||||
' 3. 运行此函数
|
||||
'
|
||||
' 输出: 在"BOM提取结果"工作表中显示提取结果
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub RunBOMExtraction()
|
||||
Dim result As String
|
||||
result = BOMExtraction()
|
||||
MsgBox result, vbInformation, "BOM提取"
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 过程: GeneratePreprocessingReport
|
||||
' 职责: 生成预处理条件转换对比报表,展示"接头"类别的条件转换结果
|
||||
' ==============================================================================
|
||||
Public Sub GeneratePreprocessingReport()
|
||||
On Error GoTo ReportErrorHandler
|
||||
|
||||
Application.StatusBar = "正在生成预处理报表..."
|
||||
|
||||
' 1. 创建错误日志记录器
|
||||
Dim logger As New clsErrorLogger
|
||||
|
||||
' 2. 验证工作表存在
|
||||
If Not WorksheetExists("平台配置清单") Then
|
||||
MsgBox "未找到[平台配置清单]工作表!", vbCritical
|
||||
GoTo ReportExitHandler
|
||||
End If
|
||||
|
||||
Dim wsSrc As Worksheet
|
||||
Set wsSrc = ActiveWorkbook.Sheets("平台配置清单")
|
||||
|
||||
' 3. 读取源数据
|
||||
Dim arrData As Variant
|
||||
arrData = M02_DataIO.ReadSourceData(wsSrc)
|
||||
|
||||
If IsEmpty(arrData) Then
|
||||
MsgBox "没有找到数据!", vbExclamation
|
||||
GoTo ReportExitHandler
|
||||
End If
|
||||
|
||||
' 4. 初始化预处理器(如果"对照表"存在)
|
||||
If WorksheetExists("对照表") Then
|
||||
Dim wsMapping As Worksheet
|
||||
Set wsMapping = ActiveWorkbook.Sheets("对照表")
|
||||
M05_PreProcessor.InitPreProcessor logger, wsMapping
|
||||
End If
|
||||
|
||||
' 5. 处理数据并收集统计信息
|
||||
Dim results As Collection
|
||||
Set results = New Collection
|
||||
|
||||
Dim stats As Object
|
||||
Set stats = CreateObject("Scripting.Dictionary")
|
||||
stats("totalRows") = 0
|
||||
stats("processedRows") = 0
|
||||
stats("successCount") = 0
|
||||
stats("warningCount") = 0
|
||||
stats("emptyCount") = 0
|
||||
stats("orMergeCount") = 0
|
||||
|
||||
' 遍历数据
|
||||
Dim i As Long
|
||||
For i = LBound(arrData, 1) To UBound(arrData, 1)
|
||||
Dim rowIdx As Long
|
||||
rowIdx = i + M04_Config.SRC_START_ROW ' 原始表行号
|
||||
|
||||
Dim strCat As String
|
||||
strCat = CStr(arrData(i, M04_Config.COL_IDX_CAT))
|
||||
|
||||
' 只处理"接头"类别
|
||||
If strCat = "接头" Then
|
||||
stats("processedRows") = stats("processedRows") + 1
|
||||
|
||||
' 提取数据
|
||||
Dim strCode As String, strName As String
|
||||
Dim strQty As String, strOrigCond As String
|
||||
|
||||
strCode = CStr(arrData(i, M04_Config.COL_IDX_CODE))
|
||||
strName = CStr(arrData(i, M04_Config.COL_IDX_NAME))
|
||||
strQty = CStr(arrData(i, M04_Config.COL_IDX_QTY))
|
||||
strOrigCond = CStr(arrData(i, M04_Config.COL_IDX_COND))
|
||||
|
||||
' 调用预处理器
|
||||
Dim strConvCond As String
|
||||
strConvCond = M05_PreProcessor.PreprocessCondition(strOrigCond, strCat, rowIdx)
|
||||
|
||||
' 分析转换
|
||||
Dim strDetails As String
|
||||
Dim strStatus As String
|
||||
Dim nORMerges As Long
|
||||
Dim strMappingDetails As String
|
||||
|
||||
strDetails = AnalyzeConversion(strOrigCond, strConvCond, nORMerges)
|
||||
strStatus = DetermineStatus(strOrigCond, strConvCond, strCat)
|
||||
strMappingDetails = ExtractMappingDetails(strOrigCond, strConvCond)
|
||||
|
||||
' 更新统计
|
||||
If strStatus = "成功" Then stats("successCount") = stats("successCount") + 1
|
||||
If strStatus = "警告" Then stats("warningCount") = stats("warningCount") + 1
|
||||
If strStatus = "空条件" Then stats("emptyCount") = stats("emptyCount") + 1
|
||||
stats("orMergeCount") = stats("orMergeCount") + nORMerges
|
||||
|
||||
' 添加到结果集合(11列:行号, 代号, 名称, 数量, 类别, 原始条件, 转换后条件, 映射详情, 转换说明, 状态, 错误/警告)
|
||||
results.Add Array(rowIdx, strCode, strName, strQty, strCat, _
|
||||
strOrigCond, strConvCond, strMappingDetails, strDetails, strStatus, "")
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 6. 创建或清除工作表
|
||||
Dim wsReport As Worksheet
|
||||
On Error Resume Next
|
||||
Set wsReport = ActiveWorkbook.Sheets("条件转换对比表")
|
||||
On Error GoTo ReportErrorHandler
|
||||
|
||||
If wsReport Is Nothing Then
|
||||
Set wsReport = ActiveWorkbook.Worksheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.count))
|
||||
wsReport.Name = "条件转换对比表"
|
||||
Else
|
||||
wsReport.Cells.Clear
|
||||
End If
|
||||
|
||||
' 7. 写入统计信息(第1-6行)
|
||||
wsReport.Cells(1, 1).Value = "总处理行数: " & stats("processedRows")
|
||||
wsReport.Cells(2, 1).Value = "转换成功数: " & stats("successCount")
|
||||
wsReport.Cells(3, 1).Value = "警告数: " & stats("warningCount")
|
||||
wsReport.Cells(4, 1).Value = "空条件行数: " & stats("emptyCount")
|
||||
wsReport.Cells(5, 1).Value = "OR条件合并次数: " & stats("orMergeCount")
|
||||
|
||||
' 8. 写入表头(第8行)
|
||||
Dim headers As Variant
|
||||
headers = Array("行号", "代号", "名称", "数量", "类别", _
|
||||
"原始条件", "转换后条件", "映射详情", "转换说明", "状态", "错误/警告")
|
||||
|
||||
Dim col As Long
|
||||
For col = 1 To 11
|
||||
wsReport.Cells(8, col).Value = headers(col - 1)
|
||||
Next col
|
||||
|
||||
' 9. 批量写入数据(从第9行开始)
|
||||
If results.count > 0 Then
|
||||
Dim arrOutput() As Variant
|
||||
ReDim arrOutput(1 To results.count, 1 To 11)
|
||||
|
||||
Dim j As Long
|
||||
j = 1
|
||||
Dim result As Variant
|
||||
For Each result In results
|
||||
Dim k As Long
|
||||
For k = 1 To 11
|
||||
arrOutput(j, k) = result(k - 1)
|
||||
Next k
|
||||
j = j + 1
|
||||
Next result
|
||||
|
||||
wsReport.Range("A9").Resize(results.count, 11).Value = arrOutput
|
||||
End If
|
||||
|
||||
' 10. 格式化工作表
|
||||
Call FormatReportWorksheet(wsReport, results.count)
|
||||
|
||||
Application.StatusBar = False
|
||||
MsgBox "预处理报表生成完成!", vbInformation
|
||||
Exit Sub
|
||||
|
||||
ReportErrorHandler:
|
||||
Application.StatusBar = False
|
||||
MsgBox "生成报表时出错:" & vbCrLf & _
|
||||
"错误 " & err.Number & ": " & err.Description, _
|
||||
vbCritical, "报表生成错误"
|
||||
|
||||
ReportExitHandler:
|
||||
Application.StatusBar = False
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: FormatReportWorksheet
|
||||
' 职责: 格式化报表工作表
|
||||
' ==============================================================================
|
||||
Private Sub FormatReportWorksheet(ByVal wsReport As Worksheet, ByVal rowCount As Long)
|
||||
' 1. 格式化统计区域
|
||||
With wsReport.Range("A1:A6")
|
||||
.Font.Bold = True
|
||||
.Font.Size = 11
|
||||
.Interior.Color = RGB(200, 220, 255)
|
||||
End With
|
||||
|
||||
' 2. 格式化表头
|
||||
With wsReport.Range("A8:K8")
|
||||
.Font.Bold = True
|
||||
.Interior.Color = RGB(217, 217, 217)
|
||||
.HorizontalAlignment = xlCenter
|
||||
End With
|
||||
|
||||
' 3. 格式化状态列和映射详情列(根据值着色)
|
||||
Dim lastRow As Long
|
||||
lastRow = 8 + rowCount
|
||||
|
||||
If rowCount > 0 Then
|
||||
Dim r As Long
|
||||
For r = 9 To lastRow
|
||||
' 映射详情列(第8列,H列)- 有映射时使用浅绿色背景
|
||||
If Len(wsReport.Cells(r, 8).Value) > 0 Then
|
||||
wsReport.Cells(r, 8).Interior.Color = RGB(230, 255, 230)
|
||||
wsReport.Cells(r, 8).Font.Color = RGB(0, 100, 0)
|
||||
wsReport.Cells(r, 8).Font.Italic = True
|
||||
End If
|
||||
|
||||
' 状态列(第10列,J列)
|
||||
Select Case wsReport.Cells(r, 10).Value
|
||||
Case "成功"
|
||||
wsReport.Cells(r, 10).Interior.Color = RGB(200, 255, 200)
|
||||
Case "警告"
|
||||
wsReport.Cells(r, 10).Interior.Color = RGB(255, 255, 200)
|
||||
Case "未转换"
|
||||
wsReport.Cells(r, 10).Interior.Color = RGB(240, 240, 240)
|
||||
Case "空条件"
|
||||
wsReport.Cells(r, 10).Interior.Color = RGB(220, 240, 255)
|
||||
End Select
|
||||
Next r
|
||||
|
||||
' 4. 应用边框
|
||||
With wsReport.Range("A8:K" & lastRow)
|
||||
.Borders.LineStyle = xlContinuous
|
||||
.Borders.Weight = xlThin
|
||||
End With
|
||||
End If
|
||||
|
||||
' 5. 自动调整列宽
|
||||
wsReport.Columns.AutoFit
|
||||
|
||||
' 6. 设置文本换行
|
||||
wsReport.Columns("F:K").WrapText = True
|
||||
|
||||
' 7. 冻结窗格
|
||||
wsReport.Activate
|
||||
ActiveWindow.FreezePanes = False
|
||||
wsReport.Rows(9).Select
|
||||
ActiveWindow.FreezePanes = True
|
||||
|
||||
' 8. 选中和取消选中,避免选区
|
||||
wsReport.Cells(1, 1).Select
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: AnalyzeConversion
|
||||
' 职责: 分析转换内容,返回说明字符串
|
||||
' ==============================================================================
|
||||
Private Function AnalyzeConversion( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String, _
|
||||
ByRef outORMerges As Long _
|
||||
) As String
|
||||
Dim details As Collection
|
||||
Set details = New Collection
|
||||
|
||||
' 计算OR合并次数
|
||||
Dim origORCount As Long, convORCount As Long
|
||||
origORCount = CountOccurrences(strOrig, " OR ")
|
||||
convORCount = CountOccurrences(strConv, " OR ")
|
||||
outORMerges = origORCount - convORCount
|
||||
If outORMerges > 0 Then details.Add "OR合并:" & outORMerges & "次"
|
||||
|
||||
' 检测azxs映射
|
||||
If ContainsAzxsChange(strOrig, strConv) Then
|
||||
details.Add "azxs值映射"
|
||||
End If
|
||||
|
||||
' 检测lcfw映射
|
||||
If ContainsLcfwChange(strOrig, strConv) Then
|
||||
details.Add "lcfw值映射"
|
||||
End If
|
||||
|
||||
' 检测括号简化
|
||||
If CountOccurrences(strOrig, "(") > CountOccurrences(strConv, "(") Then
|
||||
details.Add "括号简化"
|
||||
End If
|
||||
|
||||
' 组合说明
|
||||
If details.count = 0 Then
|
||||
AnalyzeConversion = "无变化"
|
||||
Else
|
||||
Dim result As String
|
||||
result = ""
|
||||
Dim item As Variant
|
||||
For Each item In details
|
||||
If Len(result) > 0 Then result = result & " + "
|
||||
result = result & item
|
||||
Next item
|
||||
AnalyzeConversion = result
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: DetermineStatus
|
||||
' 职责: 确定转换状态
|
||||
' ==============================================================================
|
||||
Private Function DetermineStatus( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String, _
|
||||
ByVal strCat As String _
|
||||
) As String
|
||||
If Len(Trim(strOrig)) = 0 Then
|
||||
DetermineStatus = "空条件"
|
||||
ElseIf strCat <> "接头" Then
|
||||
DetermineStatus = "未转换"
|
||||
ElseIf strOrig <> strConv Then
|
||||
DetermineStatus = "成功"
|
||||
Else
|
||||
DetermineStatus = "未转换"
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: CountOccurrences
|
||||
' 职责: 计算子字符串在字符串中出现的次数
|
||||
' ==============================================================================
|
||||
Private Function CountOccurrences( _
|
||||
ByVal strText As String, _
|
||||
ByVal strFind As String _
|
||||
) As Long
|
||||
If Len(strText) = 0 Or Len(strFind) = 0 Then
|
||||
CountOccurrences = 0
|
||||
Exit Function
|
||||
End If
|
||||
CountOccurrences = (Len(strText) - Len(Replace(strText, strFind, ""))) / Len(strFind)
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: ContainsAzxsChange
|
||||
' 职责: 使用正则表达式检查azxs值是否变化
|
||||
' ==============================================================================
|
||||
Private Function ContainsAzxsChange( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String _
|
||||
) As Boolean
|
||||
' 使用正则表达式检查azxs值是否变化
|
||||
Dim regex As Object
|
||||
Set regex = CreateObject("VBScript.RegExp")
|
||||
|
||||
regex.Global = True
|
||||
regex.IgnoreCase = True
|
||||
regex.pattern = "(azxs)( *=|!= *)([a-zA-Z0-9]{2})"
|
||||
|
||||
' 提取原始azxs值
|
||||
Dim origMatches As Object
|
||||
Set origMatches = regex.Execute(strOrig)
|
||||
|
||||
' 提取转换后azxs值
|
||||
Dim convMatches As Object
|
||||
Set convMatches = regex.Execute(strConv)
|
||||
|
||||
' 如果数量不同,说明有变化
|
||||
If origMatches.count <> convMatches.count Then
|
||||
ContainsAzxsChange = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 比较值
|
||||
Dim i As Long
|
||||
For i = 0 To origMatches.count - 1
|
||||
If origMatches(i).SubMatches(2) <> convMatches(i).SubMatches(2) Then
|
||||
ContainsAzxsChange = True
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
ContainsAzxsChange = False
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: ContainsLcfwChange
|
||||
' 职责: 使用正则表达式检查lcfw值是否变化
|
||||
' ==============================================================================
|
||||
Private Function ContainsLcfwChange( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String _
|
||||
) As Boolean
|
||||
' 使用正则表达式检查lcfw值是否变化
|
||||
Dim regex As Object
|
||||
Set regex = CreateObject("VBScript.RegExp")
|
||||
|
||||
regex.Global = True
|
||||
regex.IgnoreCase = True
|
||||
regex.pattern = "(lcfw)( *=|!= *)([a-zA-Z]\d{1,3})"
|
||||
|
||||
' 提取原始lcfw值
|
||||
Dim origMatches As Object
|
||||
Set origMatches = regex.Execute(strOrig)
|
||||
|
||||
' 提取转换后lcfw值
|
||||
Dim convMatches As Object
|
||||
Set convMatches = regex.Execute(strConv)
|
||||
|
||||
' 如果数量不同,说明有变化
|
||||
If origMatches.count <> convMatches.count Then
|
||||
ContainsLcfwChange = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 比较值
|
||||
Dim i As Long
|
||||
For i = 0 To origMatches.count - 1
|
||||
If origMatches(i).SubMatches(2) <> convMatches(i).SubMatches(2) Then
|
||||
ContainsLcfwChange = True
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
ContainsLcfwChange = False
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: ExtractMappingDetails
|
||||
' 职责: 提取并生成映射详情字符串
|
||||
' ==============================================================================
|
||||
Private Function ExtractMappingDetails( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String _
|
||||
) As String
|
||||
Dim mappings As Collection
|
||||
Set mappings = New Collection
|
||||
|
||||
' 提取 azxs 映射
|
||||
Dim azxsMappings As String
|
||||
azxsMappings = ExtractFieldMappings(strOrig, strConv, "azxs", "(azxs)( *=|!= *)([a-zA-Z0-9]{2})")
|
||||
If Len(azxsMappings) > 0 Then
|
||||
mappings.Add azxsMappings
|
||||
End If
|
||||
|
||||
' 提取 lcfw 映射
|
||||
Dim lcfwMappings As String
|
||||
lcfwMappings = ExtractFieldMappings(strOrig, strConv, "lcfw", "(lcfw)( *=|!= *)([a-zA-Z]\d{1,3})")
|
||||
If Len(lcfwMappings) > 0 Then
|
||||
mappings.Add lcfwMappings
|
||||
End If
|
||||
|
||||
' 组合所有映射(使用换行符分隔)
|
||||
If mappings.count = 0 Then
|
||||
ExtractMappingDetails = ""
|
||||
ElseIf mappings.count = 1 Then
|
||||
ExtractMappingDetails = mappings(1)
|
||||
Else
|
||||
Dim result As String
|
||||
result = ""
|
||||
Dim item As Variant
|
||||
For Each item In mappings
|
||||
If Len(result) > 0 Then result = result & vbLf
|
||||
result = result & item
|
||||
Next item
|
||||
ExtractMappingDetails = result
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: ExtractFieldMappings
|
||||
' 职责: 提取特定字段的映射详情
|
||||
' ==============================================================================
|
||||
Private Function ExtractFieldMappings( _
|
||||
ByVal strOrig As String, _
|
||||
ByVal strConv As String, _
|
||||
ByVal fieldName As String, _
|
||||
ByVal pattern As String _
|
||||
) As String
|
||||
' 使用正则表达式提取字段映射
|
||||
Dim regex As Object
|
||||
Set regex = CreateObject("VBScript.RegExp")
|
||||
|
||||
regex.Global = True
|
||||
regex.IgnoreCase = True
|
||||
regex.pattern = pattern
|
||||
|
||||
' 从原始条件中提取所有该字段的值
|
||||
Dim origMatches As Object
|
||||
Set origMatches = regex.Execute(strOrig)
|
||||
|
||||
' 如果原始条件中没有匹配,返回空
|
||||
If origMatches.count = 0 Then
|
||||
ExtractFieldMappings = ""
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 收集所有唯一的映射
|
||||
Dim mappingDict As Object
|
||||
Set mappingDict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim i As Long
|
||||
For i = 0 To origMatches.count - 1
|
||||
Dim origValue As String
|
||||
Dim mappedValue As String
|
||||
origValue = origMatches(i).SubMatches(2)
|
||||
|
||||
' 根据字段名查询映射表
|
||||
If LCase(fieldName) = "azxs" Then
|
||||
mappedValue = M05_PreProcessor.GetAzxsMappedValue(origValue)
|
||||
ElseIf LCase(fieldName) = "lcfw" Then
|
||||
mappedValue = M05_PreProcessor.GetLcfwMappedValue(origValue)
|
||||
Else
|
||||
mappedValue = ""
|
||||
End If
|
||||
|
||||
' 只有当找到映射且值发生变化时才记录
|
||||
If Len(mappedValue) > 0 And origValue <> mappedValue Then
|
||||
Dim mapKey As String
|
||||
mapKey = fieldName & "=" & origValue & " → " & fieldName & "=" & mappedValue
|
||||
|
||||
' 去重
|
||||
If Not mappingDict.Exists(mapKey) Then
|
||||
mappingDict.Add mapKey, mapKey
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 组合结果
|
||||
If mappingDict.count = 0 Then
|
||||
ExtractFieldMappings = ""
|
||||
Else
|
||||
Dim result As String
|
||||
result = ""
|
||||
Dim key As Variant
|
||||
For Each key In mappingDict.keys
|
||||
If Len(result) > 0 Then result = result & vbLf
|
||||
result = result & key
|
||||
Next key
|
||||
ExtractFieldMappings = result
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数: WorksheetExists
|
||||
' 职责: 检查工作表是否存在
|
||||
' ==============================================================================
|
||||
Private Function WorksheetExists(ByVal sheetName As String) As Boolean
|
||||
On Error Resume Next
|
||||
Dim ws As Worksheet
|
||||
Set ws = ActiveWorkbook.Sheets(sheetName)
|
||||
WorksheetExists = Not ws Is Nothing
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
@@ -1,212 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M02_DataIO
|
||||
' 职责: 数据读写
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
Public Function ReadSourceData(ws As Worksheet) As Variant
|
||||
Dim lastRow As Long
|
||||
Dim i As Long, r As Long
|
||||
Dim arrRaw As Variant
|
||||
Dim arrResult() As Variant
|
||||
Dim cell As Range
|
||||
|
||||
' 查找C列最后一行(保持原有逻辑,使用代号列判断)
|
||||
lastRow = ws.Cells(ws.Rows.count, M04_Config.COL_IDX_CODE).End(xlUp).row
|
||||
|
||||
If lastRow < M04_Config.SRC_START_ROW Then
|
||||
ReadSourceData = Empty
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 第一步:使用数组方式读取整个数据区域(高性能)
|
||||
arrRaw = ws.Range(ws.Cells(M04_Config.SRC_START_ROW, 1), ws.Cells(lastRow, M04_Config.COL_IDX_CODE66)).Value
|
||||
|
||||
' 第二步:对66代码列(第9列)使用.Text属性重新读取,以保留前导0
|
||||
' 因为.Value会将"00123"转换为123,而.Text会保留显示的"00123"
|
||||
ReDim arrResult(1 To UBound(arrRaw, 1), 1 To UBound(arrRaw, 2))
|
||||
|
||||
' 复制原始数据
|
||||
For r = 1 To UBound(arrRaw, 1)
|
||||
For i = 1 To UBound(arrRaw, 2)
|
||||
If i = M04_Config.COL_IDX_CODE66 Then
|
||||
' 对于66代码列(I列),使用.Text属性读取显示文本
|
||||
Set cell = ws.Cells(M04_Config.SRC_START_ROW + r - 1, M04_Config.COL_IDX_CODE66)
|
||||
arrResult(r, i) = cell.Text
|
||||
Else
|
||||
' 其他列使用原始值
|
||||
arrResult(r, i) = arrRaw(r, i)
|
||||
End If
|
||||
Next i
|
||||
Next r
|
||||
|
||||
ReadSourceData = arrResult
|
||||
End Function
|
||||
|
||||
Public Sub WriteCategoryToNewBook(catData As Object)
|
||||
Dim newWb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim catName As Variant
|
||||
Dim colRows As Collection
|
||||
Dim finalArr() As Variant
|
||||
Dim headerKeys As Collection
|
||||
Dim i As Long, r As Long, c As Long
|
||||
Dim rowDict As Object
|
||||
Dim key As Variant
|
||||
Dim savePath As String
|
||||
Dim sheetToDelete As Worksheet
|
||||
|
||||
If catData.count = 0 Then Exit Sub
|
||||
|
||||
Set newWb = Workbooks.Add
|
||||
|
||||
For Each catName In catData.keys
|
||||
Set colRows = catData(catName)
|
||||
|
||||
If colRows.count > 0 Then
|
||||
' 创建新Sheet
|
||||
Set ws = newWb.Worksheets.Add
|
||||
ws.Name = CleanSheetName(CStr(catName))
|
||||
|
||||
' 1. 扫描该类别所有Key
|
||||
Dim allKeysDict As Object
|
||||
Set allKeysDict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
For i = 1 To colRows.count
|
||||
' colRows(i) 是一个 Array(Dict, BaseInfoArr)
|
||||
Set rowDict = colRows(i)(0)
|
||||
For Each key In rowDict.keys
|
||||
If Not allKeysDict.Exists(key) Then allKeysDict.Add key, 0
|
||||
Next key
|
||||
Next i
|
||||
|
||||
' 2. 排序Key
|
||||
Dim sortedHeaders() As String
|
||||
sortedHeaders = SortHeaders(allKeysDict.keys)
|
||||
|
||||
' 3. 准备输出数组
|
||||
Dim condCount As Long
|
||||
condCount = UBound(sortedHeaders) - LBound(sortedHeaders) + 1
|
||||
' 检查是否为空数组(如果全是无条件的物料)
|
||||
If sortedHeaders(0) = "" And condCount = 1 Then condCount = 0
|
||||
|
||||
Dim totalCols As Long
|
||||
totalCols = condCount + 3 ' 条件列 + 名称/编码/数量
|
||||
|
||||
ReDim finalArr(1 To colRows.count + 1, 1 To totalCols)
|
||||
|
||||
' 3.1 写表头
|
||||
Dim colOffset As Long
|
||||
colOffset = 0
|
||||
|
||||
If condCount > 0 Then
|
||||
For c = 0 To condCount - 1
|
||||
finalArr(1, c + 1) = sortedHeaders(c)
|
||||
Next c
|
||||
colOffset = condCount
|
||||
End If
|
||||
|
||||
finalArr(1, colOffset + 1) = "名称"
|
||||
finalArr(1, colOffset + 2) = "编码"
|
||||
finalArr(1, colOffset + 3) = "数量"
|
||||
|
||||
' 3.2 填充内容
|
||||
For r = 1 To colRows.count
|
||||
Dim baseInfo As Variant
|
||||
Set rowDict = colRows(r)(0)
|
||||
baseInfo = colRows(r)(1) ' Array: Code, Name, Qty
|
||||
|
||||
' 填条件
|
||||
If condCount > 0 Then
|
||||
For c = 0 To condCount - 1
|
||||
key = sortedHeaders(c)
|
||||
If rowDict.Exists(key) Then
|
||||
finalArr(r + 1, c + 1) = rowDict(key)
|
||||
End If
|
||||
Next c
|
||||
End If
|
||||
|
||||
' 填基础信息
|
||||
finalArr(r + 1, colOffset + 1) = baseInfo(1) ' Name
|
||||
finalArr(r + 1, colOffset + 2) = baseInfo(0) ' Code
|
||||
finalArr(r + 1, colOffset + 3) = baseInfo(2) ' Qty
|
||||
Next r
|
||||
|
||||
|
||||
' 4 设置编码列为文本格式(防止数字编码被转换为科学计数法或丢失前导零)
|
||||
ws.Columns(colOffset + 2).NumberFormat = "@"
|
||||
|
||||
' 4.1 写入Excel
|
||||
ws.Range("A1").Resize(UBound(finalArr, 1), UBound(finalArr, 2)).Value = finalArr
|
||||
ws.Range("A1").Resize(1, totalCols).Font.Bold = True
|
||||
|
||||
ws.Columns.AutoFit
|
||||
End If
|
||||
Next catName
|
||||
|
||||
' 5. 删除默认工作表(Workbooks.Add 创建的空白工作表)
|
||||
Application.DisplayAlerts = False ' 禁用删除确认对话框
|
||||
For Each sheetToDelete In newWb.Worksheets
|
||||
If sheetToDelete.Name Like "Sheet*" Then
|
||||
sheetToDelete.Delete
|
||||
End If
|
||||
Next sheetToDelete
|
||||
Application.DisplayAlerts = True
|
||||
|
||||
' 6. 保存工作簿为 BOM库.xlsx
|
||||
savePath = ThisWorkbook.Path & Application.PathSeparator & "BOM库.xlsx"
|
||||
|
||||
' 如果文件已存在,先删除
|
||||
If Dir(savePath) <> "" Then
|
||||
Kill savePath
|
||||
End If
|
||||
|
||||
newWb.SaveAs savePath, FileFormat:=xlOpenXMLWorkbook
|
||||
newWb.Close SaveChanges:=False
|
||||
|
||||
MsgBox "处理完成!已保存至:" & vbCrLf & savePath, vbInformation
|
||||
End Sub
|
||||
|
||||
Private Function SortHeaders(keys As Variant) As String()
|
||||
' 冒泡排序
|
||||
Dim i As Long, j As Long
|
||||
Dim temp As String
|
||||
Dim arr() As String
|
||||
Dim count As Long
|
||||
|
||||
count = UBound(keys) - LBound(keys) + 1
|
||||
|
||||
If count = 0 Then
|
||||
ReDim arr(0 To 0)
|
||||
arr(0) = ""
|
||||
SortHeaders = arr
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
ReDim arr(0 To count - 1)
|
||||
For i = 0 To count - 1
|
||||
arr(i) = keys(i)
|
||||
Next i
|
||||
|
||||
For i = LBound(arr) To UBound(arr) - 1
|
||||
For j = i + 1 To UBound(arr)
|
||||
If M04_Config.GetHeaderPriority(arr(i)) > M04_Config.GetHeaderPriority(arr(j)) Then
|
||||
temp = arr(i)
|
||||
arr(i) = arr(j)
|
||||
arr(j) = temp
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
SortHeaders = arr
|
||||
End Function
|
||||
|
||||
Private Function CleanSheetName(s As String) As String
|
||||
Dim invalid As String, i As Long
|
||||
invalid = ":\/?*[]"
|
||||
CleanSheetName = s
|
||||
For i = 1 To Len(invalid)
|
||||
CleanSheetName = Replace(CleanSheetName, Mid(invalid, i, 1), "_")
|
||||
Next i
|
||||
If Len(CleanSheetName) > 31 Then CleanSheetName = Left(CleanSheetName, 31)
|
||||
End Function
|
||||
@@ -1,289 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M03_Logic
|
||||
' 职责: 核心算法。使用后期绑定(Late Binding)避免引用错误。
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
Private g_Logger As clsErrorLogger
|
||||
|
||||
' 初始化日志引用
|
||||
Public Sub InitLogic(logger As clsErrorLogger)
|
||||
Set g_Logger = logger
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 解析规则字符串
|
||||
' 返回: Collection (包含多个 Dictionary 对象)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function ParseRule(strRule As String, rowIdx As Long) As Collection
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim cleanStr As String
|
||||
cleanStr = CleanString(strRule)
|
||||
|
||||
' 空条件处理
|
||||
If Len(cleanStr) = 0 Then
|
||||
Dim col As New Collection
|
||||
' 创建一个空字典
|
||||
col.Add CreateObject("Scripting.Dictionary")
|
||||
Set ParseRule = col
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Set ParseRule = RecursiveParse(cleanStr, rowIdx)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
g_Logger.Record CStr(rowIdx), "M03.ParseRule", "System Error", err.Description, strRule
|
||||
Set ParseRule = Nothing
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 递归解析核心
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function RecursiveParse(strExpr As String, rowIdx As Long) As Collection
|
||||
Dim splitIdx As Long
|
||||
|
||||
' 1. 查找顶层 OR (最低优先级,先拆分)
|
||||
splitIdx = FindSplitIndex(strExpr, "OR")
|
||||
If splitIdx > 0 Then
|
||||
Dim leftRes As Collection, rightRes As Collection
|
||||
' 递归左边
|
||||
Set leftRes = RecursiveParse(Trim(Left(strExpr, splitIdx - 1)), rowIdx)
|
||||
' 递归右边 (+2 是 OR 的长度)
|
||||
Set rightRes = RecursiveParse(Trim(Mid(strExpr, splitIdx + 2)), rowIdx)
|
||||
' 合并结果 (Union)
|
||||
Set RecursiveParse = UnionCollections(leftRes, rightRes)
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 2. 查找顶层 AND
|
||||
splitIdx = FindSplitIndex(strExpr, "AND")
|
||||
If splitIdx > 0 Then
|
||||
Dim leftCol As Collection, rightCol As Collection
|
||||
' 递归左边
|
||||
Set leftCol = RecursiveParse(Trim(Left(strExpr, splitIdx - 1)), rowIdx)
|
||||
' 递归右边 (+3 是 AND 的长度)
|
||||
Set rightCol = RecursiveParse(Trim(Mid(strExpr, splitIdx + 3)), rowIdx)
|
||||
' 笛卡尔积 (Intersection/Merge)
|
||||
Set RecursiveParse = CartesianProduct(leftCol, rightCol, rowIdx)
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 3. 去除外层括号
|
||||
If Left(strExpr, 1) = "(" And Right(strExpr, 1) = ")" Then
|
||||
' 防止像 (A) AND (B) 这种情况被误去括号,但这里已经被 FindSplitIndex 过滤了顶层操作符,
|
||||
' 所以如果这里首尾是括号,且中间没有暴露的操作符,说明是包裹的整体,例如 ((A AND B))
|
||||
Set RecursiveParse = RecursiveParse(Mid(strExpr, 2, Len(strExpr) - 2), rowIdx)
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 4. 原子解析 (Base Case)
|
||||
Set RecursiveParse = ParseAtom(strExpr, rowIdx)
|
||||
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 原子解析: key=val 或 key!=val
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ParseAtom(strAtom As String, rowIdx As Long) As Collection
|
||||
Dim dict As Object ' Late Binding
|
||||
Set dict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim p As Long
|
||||
Dim key As String, val As String
|
||||
|
||||
' 移除多余空格,但保留值中间可能存在的(虽然通常没有)
|
||||
strAtom = Trim(strAtom)
|
||||
|
||||
If InStr(strAtom, "!=") > 0 Then
|
||||
p = InStr(strAtom, "!=")
|
||||
key = Trim(Left(strAtom, p - 1))
|
||||
val = Trim(Mid(strAtom, p + 2))
|
||||
dict.Add key, "!=" & val
|
||||
ElseIf InStr(strAtom, "=") > 0 Then
|
||||
p = InStr(strAtom, "=")
|
||||
key = Trim(Left(strAtom, p - 1))
|
||||
val = Trim(Mid(strAtom, p + 1))
|
||||
dict.Add key, val
|
||||
Else
|
||||
' 无法解析的格式
|
||||
If Len(strAtom) > 0 Then
|
||||
g_Logger.RecordWarning CStr(rowIdx), "M03.ParseAtom", "Syntax Error", "No = or != found", strAtom
|
||||
End If
|
||||
End If
|
||||
|
||||
Dim col As New Collection
|
||||
col.Add dict
|
||||
Set ParseAtom = col
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 笛卡尔积: AND 逻辑
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function CartesianProduct(col1 As Collection, col2 As Collection, rowIdx As Long) As Collection
|
||||
Dim res As New Collection
|
||||
Dim d1 As Object, d2 As Object
|
||||
Dim merged As Object
|
||||
Dim i As Long, j As Long
|
||||
|
||||
If col1 Is Nothing Or col2 Is Nothing Then
|
||||
Set CartesianProduct = Nothing
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
For i = 1 To col1.count
|
||||
For j = 1 To col2.count
|
||||
Set d1 = col1(i)
|
||||
Set d2 = col2(j)
|
||||
Set merged = MergeDictionaries(d1, d2, rowIdx)
|
||||
If Not merged Is Nothing Then
|
||||
res.Add merged
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
Set CartesianProduct = res
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 集合合并: OR 逻辑
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function UnionCollections(col1 As Collection, col2 As Collection) As Collection
|
||||
Dim res As New Collection
|
||||
Dim item As Variant
|
||||
|
||||
If Not col1 Is Nothing Then
|
||||
For Each item In col1
|
||||
res.Add item
|
||||
Next item
|
||||
End If
|
||||
|
||||
If Not col2 Is Nothing Then
|
||||
For Each item In col2
|
||||
res.Add item
|
||||
Next item
|
||||
End If
|
||||
|
||||
Set UnionCollections = res
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 字典合并 (处理冲突和 !=)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function MergeDictionaries(d1 As Object, d2 As Object, rowIdx As Long) As Object
|
||||
Dim res As Object
|
||||
Set res = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim k As Variant
|
||||
Dim v1 As String, v2 As String
|
||||
|
||||
' 复制 d1
|
||||
For Each k In d1.keys
|
||||
res.Add k, d1(k)
|
||||
Next k
|
||||
|
||||
' 合并 d2
|
||||
For Each k In d2.keys
|
||||
If res.Exists(k) Then
|
||||
v1 = CStr(res(k))
|
||||
v2 = CStr(d2(k))
|
||||
|
||||
If v1 = v2 Then
|
||||
' 相同,无视
|
||||
ElseIf Left(v1, 2) = "!=" And Left(v2, 2) = "!=" Then
|
||||
' 都是不等于,合并
|
||||
res(k) = v1 & "," & v2
|
||||
ElseIf Left(v1, 2) = "!=" And Left(v2, 2) <> "!=" Then
|
||||
' v1!=, v2=
|
||||
If v2 = Mid(v1, 3) Then
|
||||
g_Logger.Record CStr(rowIdx), "M03.Conflict", "Logic Conflict", "Equals disallowed value", k & ": " & v1 & " AND " & v2
|
||||
Set MergeDictionaries = Nothing: Exit Function
|
||||
Else
|
||||
res(k) = v2
|
||||
End If
|
||||
ElseIf Left(v1, 2) <> "!=" And Left(v2, 2) = "!=" Then
|
||||
' v1=, v2!=
|
||||
If v1 = Mid(v2, 3) Then
|
||||
g_Logger.Record CStr(rowIdx), "M03.Conflict", "Logic Conflict", "Equals disallowed value", k & ": " & v1 & " AND " & v2
|
||||
Set MergeDictionaries = Nothing: Exit Function
|
||||
Else
|
||||
res(k) = v1
|
||||
End If
|
||||
Else
|
||||
' 都是等于,但值不同
|
||||
g_Logger.Record CStr(rowIdx), "M03.Conflict", "Logic Conflict", "Mutually Exclusive", k & "=" & v1 & " AND " & v2
|
||||
Set MergeDictionaries = Nothing: Exit Function
|
||||
End If
|
||||
Else
|
||||
res.Add k, d2(k)
|
||||
End If
|
||||
Next k
|
||||
|
||||
Set MergeDictionaries = res
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 查找逻辑分割点 (忽略括号内容)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function FindSplitIndex(strExpr As String, delimiter As String) As Long
|
||||
Dim i As Long
|
||||
Dim bracketLevel As Long
|
||||
Dim subStr As String
|
||||
Dim lenDelim As Long
|
||||
Dim checkStr As String
|
||||
|
||||
bracketLevel = 0
|
||||
lenDelim = Len(delimiter)
|
||||
|
||||
' 预处理:为了防止匹配到变量名里的字符,我们检查 " AND " (带空格)
|
||||
' 或者简单起见,我们假设变量名不包含 AND/OR 且大小写敏感
|
||||
' 这里采用严格括号计数
|
||||
|
||||
For i = 1 To Len(strExpr) - lenDelim + 1
|
||||
subStr = Mid(strExpr, i, 1)
|
||||
|
||||
If subStr = "(" Then
|
||||
bracketLevel = bracketLevel + 1
|
||||
ElseIf subStr = ")" Then
|
||||
bracketLevel = bracketLevel - 1
|
||||
ElseIf bracketLevel = 0 Then
|
||||
' 只有在第0层括号时才匹配逻辑符
|
||||
checkStr = Mid(strExpr, i, lenDelim)
|
||||
|
||||
' 关键修正:确保匹配的是独立单词,而不是变量名的一部分
|
||||
' 简单判断:前后字符是空格,或者处于字符串边界
|
||||
If UCase(checkStr) = delimiter Then
|
||||
Dim isWord As Boolean
|
||||
isWord = True
|
||||
|
||||
' 检查前一个字符
|
||||
If i > 1 Then
|
||||
If Mid(strExpr, i - 1, 1) <> " " And Mid(strExpr, i - 1, 1) <> ")" Then isWord = False
|
||||
End If
|
||||
|
||||
' 检查后一个字符
|
||||
If i + lenDelim <= Len(strExpr) Then
|
||||
If Mid(strExpr, i + lenDelim, 1) <> " " And Mid(strExpr, i + lenDelim, 1) <> "(" Then isWord = False
|
||||
End If
|
||||
|
||||
If isWord Then
|
||||
FindSplitIndex = i
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
FindSplitIndex = 0
|
||||
End Function
|
||||
|
||||
Private Function CleanString(s As String) As String
|
||||
' 移除多余的空格,将换行符替换为空格
|
||||
Dim temp As String
|
||||
temp = Replace(s, vbCrLf, " ")
|
||||
temp = Replace(temp, vbCr, " ")
|
||||
temp = Replace(temp, vbLf, " ")
|
||||
temp = Trim(temp)
|
||||
CleanString = temp
|
||||
End Function
|
||||
@@ -1,147 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M04_Config
|
||||
' 职责: 系统配置、常量定义
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 源数据列号定义 (根据你的描述)
|
||||
Public Const COL_IDX_CODE As Long = 3 ' 代号 (C列)
|
||||
Public Const COL_IDX_NAME As Long = 4 ' 名称 (D列)
|
||||
Public Const COL_IDX_QTY As Long = 5 ' 数量 (E列)
|
||||
Public Const COL_IDX_COND As Long = 6 ' 选择条件 (F列)
|
||||
Public Const COL_IDX_CAT As Long = 8 ' 类别 (H列)
|
||||
Public Const COL_IDX_CODE66 As Long = 9 ' 66代码 (I列) - NEW
|
||||
Public Const SRC_START_ROW As Long = 4 ' 数据起始行
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' BOM自动提取系统配置常量
|
||||
' ------------------------------------------------------------------------------
|
||||
|
||||
' BOM库配置
|
||||
Public Const BOMLIB_FILENAME As String = "BOM库.xlsx"
|
||||
Public Const BOMLIB_START_ROW As Long = 2 ' BOM库数据起始行(第1行是表头)
|
||||
Public Const OUTPUT_SHEET_NAME As String = "BOM提取结果"
|
||||
|
||||
' BIP上传工作表配置
|
||||
Public Const BIP_UPLOAD_SHEET_NAME As String = "BIP上传"
|
||||
Public Const BIP_ROW_NUMBER_BASE As Long = 7000
|
||||
Public Const BIP_SUPPLY_MODE As String = "一般发料"
|
||||
Public Const BIP_ISSUE_ORG As String = "重庆布莱迪仪器仪表有限公司"
|
||||
|
||||
' 库存比对工作表配置
|
||||
Public Const INVENTORY_COMPARISON_SHEET_NAME As String = "库存比对"
|
||||
|
||||
' 输入列名称配置
|
||||
Public Const INPUT_COL_MODEL As String = "型号"
|
||||
Public Const INPUT_COL_PRODUCT_MODEL As String = "产品型号"
|
||||
|
||||
' 输出列枚举
|
||||
Public Enum OutputColumns
|
||||
oc_ProductionOrderNo = 1 ' 生产订单号
|
||||
oc_OriginalModel = 2 ' 原始产品型号
|
||||
oc_Azxs = 3 ' 安装形式
|
||||
oc_Bkxs = 4 ' 表壳形式
|
||||
oc_Gclj = 5 ' 过程连接
|
||||
oc_Jycz = 6 ' 接液材质
|
||||
oc_Lcfw = 7 ' 量程范围
|
||||
oc_Fjgn = 8 ' 附加功能
|
||||
oc_MaterialType = 9 ' 物料类型
|
||||
oc_MaterialName = 10 ' 物料名称
|
||||
oc_MaterialCode = 11 ' 物料编码
|
||||
oc_MaterialQty = 12 ' 物料数量
|
||||
oc_Remarks = 13 ' 提取备注
|
||||
End Enum
|
||||
|
||||
' 型号解析常量
|
||||
Public Const MODEL_SEPARATOR_PIPELINE As String = "|" ' 管道符分隔符
|
||||
Public Const MODEL_SEPARATOR_DOT As String = "." ' 点号分隔符
|
||||
Public Const MODEL_SEPARATOR_CARET As String = "^" ' 插入符分隔符(法兰隔膜)
|
||||
Public Const MODEL_HEADER_MIN_SEGMENTS As Long = 6 ' 表头最小段数
|
||||
|
||||
' BOM库工作表列表(需要遍历的工作表)
|
||||
Public Const BOMLIB_SHEET_JOINT As String = "接头"
|
||||
Public Const BOMLIB_SHEET_ELEMENT As String = "弹性元件"
|
||||
Public Const BOMLIB_SHEET_MOVEMENT As String = "机芯"
|
||||
Public Const BOMLIB_SHEET_COMPONENT As String = "部件"
|
||||
Public Const BOMLIB_SHEET_EDGE As String = "边"
|
||||
|
||||
' BOM库物料列名常量
|
||||
Public Const BOMLIB_COL_NAME As String = "名称"
|
||||
Public Const BOMLIB_COL_CODE As String = "编码"
|
||||
Public Const BOMLIB_COL_QTY As String = "数量"
|
||||
Public Const BOMLIB_COL_JOINT_NAME As String = "接头名称"
|
||||
Public Const BOMLIB_COL_JOINT_CODE As String = "接头编码"
|
||||
Public Const BOMLIB_COL_JOINT_QTY As String = "接头数量"
|
||||
Public Const BOMLIB_COL_ELEMENT_NAME As String = "弹性元件名称"
|
||||
Public Const BOMLIB_COL_ELEMENT_CODE As String = "弹性元件编码"
|
||||
Public Const BOMLIB_COL_ELEMENT_QTY As String = "弹性元件数量"
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 现存量工作表配置常量
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Const INVENTORY_SHEET_NAME As String = "现存量"
|
||||
Public Const INVENTORY_HEADER_ROW As Long = 3 ' 表头在第3行
|
||||
Public Const INVENTORY_COL_CODE As String = "B" ' B列 = 物料编码
|
||||
Public Const INVENTORY_COL_QTY As String = "J" ' J列 = 库存数量
|
||||
|
||||
' BOM库条件字段列表(所有可能的条件字段)
|
||||
Public Function GetBOMConditionFields() As Variant
|
||||
GetBOMConditionFields = Array( _
|
||||
"azxs", "bkxs", "gclj", "jycz", "lcdw", "lcfw", _
|
||||
"fjgn", "btcy", "bp", "dskd", "nqlc", "bptx", _
|
||||
"jddj", "cpdm", "tsjz", "tsyq", "bpts", "kdxh" _
|
||||
)
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 映射表配置常量
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Const MAPPING_SHEET_NAME As String = "对照表"
|
||||
Public Const MAPPING_COL_LCFW_KEY As Long = 1 ' A列 - lcfw原始值
|
||||
Public Const MAPPING_COL_LCFW_VAL As Long = 2 ' B列 - lcfw映射值
|
||||
Public Const MAPPING_COL_AZXS_KEY As Long = 4 ' D列 - azxs原始值
|
||||
Public Const MAPPING_COL_AZXS_VAL As Long = 5 ' E列 - azxs映射值
|
||||
Public Const MAPPING_START_ROW As Long = 3 ' 数据起始行
|
||||
|
||||
' 检查列名是否为条件列
|
||||
Public Function IsConditionField(ByVal colName As String) As Boolean
|
||||
Dim fields As Variant
|
||||
fields = GetBOMConditionFields()
|
||||
|
||||
Dim i As Long
|
||||
Dim lowerColName As String
|
||||
lowerColName = LCase(Trim(colName))
|
||||
|
||||
For i = LBound(fields) To UBound(fields)
|
||||
If lowerColName = LCase(fields(i)) Then
|
||||
IsConditionField = True
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
IsConditionField = False
|
||||
End Function
|
||||
|
||||
' 获取表头排序索引 (越小越靠前)
|
||||
Public Function GetHeaderPriority(key As String) As Long
|
||||
Dim vList As Variant
|
||||
Dim i As Long
|
||||
|
||||
' 定义标准排序顺序
|
||||
vList = Array("azxs", "bkxs", "gclj", "jycz", "lcdw", "lcfw", _
|
||||
"fjgn", "btcy", "bp", "dskd", "nqlc", "bptx", _
|
||||
"jddj", "cpdm", "tsjz", "tsyq", "bpts", "kdxh")
|
||||
|
||||
Dim sKey As String
|
||||
sKey = LCase(Trim(key))
|
||||
|
||||
For i = LBound(vList) To UBound(vList)
|
||||
If sKey = LCase(vList(i)) Then
|
||||
GetHeaderPriority = i
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 未知变量排在最后
|
||||
GetHeaderPriority = 999
|
||||
End Function
|
||||
@@ -1,632 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M05_PreProcessor
|
||||
' 职责: 预处理条件表达式,支持不同类别的差异化处理
|
||||
' - "接头"类别: 完整预处理(azxs映射 + lcfw映射 + OR合并 + 括号简化)
|
||||
' - "部件"类别: 部分预处理(azxs映射 + OR合并 + 括号简化,不处理lcfw)
|
||||
' - 其他类别: 不进行预处理
|
||||
' 使用正则表达式实现高效的值映射和OR条件合并
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 模块级变量
|
||||
Private g_LcfwMapping As Object ' Scripting.Dictionary - lcfw映射表
|
||||
Private g_AzxsMapping As Object ' Scripting.Dictionary - azxs映射表
|
||||
Private g_Logger As clsErrorLogger
|
||||
Private g_IsInitialized As Boolean
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化预处理器
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitPreProcessor(logger As clsErrorLogger, wsMapping As Worksheet)
|
||||
Set g_Logger = logger
|
||||
Set g_LcfwMapping = CreateObject("Scripting.Dictionary")
|
||||
Set g_AzxsMapping = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 从[对照表]加载映射
|
||||
Call LoadLcfwMapping(wsMapping)
|
||||
Call LoadAzxsMapping(wsMapping)
|
||||
|
||||
g_IsInitialized = True
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 检查是否已初始化
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function IsInitialized() As Boolean
|
||||
IsInitialized = g_IsInitialized
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口:预处理条件表达式
|
||||
' 支持的类别:
|
||||
' - "接头": 完整预处理(azxs映射 + lcfw映射 + OR合并 + 括号简化)
|
||||
' - "部件": 部分预处理(azxs映射 + OR合并 + 括号简化,不处理lcfw)
|
||||
' - 其他: 不进行预处理
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function PreprocessCondition( _
|
||||
ByVal strCondition As String, _
|
||||
ByVal strCategory As String, _
|
||||
ByVal rowIdx As Long _
|
||||
) As String
|
||||
If Not g_IsInitialized Then
|
||||
PreprocessCondition = strCondition
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 根据类别选择处理策略
|
||||
If strCategory = "接头" Then
|
||||
' 完整预处理:azxs + lcfw + OR合并 + 括号简化
|
||||
PreprocessCondition = ApplyPreprocessing(strCondition, rowIdx)
|
||||
ElseIf strCategory = "部件" Then
|
||||
' 部分预处理:azxs + OR合并 + 括号简化(不处理 lcfw)
|
||||
PreprocessCondition = ApplyPreprocessingWithoutLcfw(strCondition, rowIdx)
|
||||
Else
|
||||
' 其他类别:不处理
|
||||
PreprocessCondition = strCondition
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 加载lcfw映射(从A列:B列,列1:2)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub LoadLcfwMapping(wsMapping As Worksheet)
|
||||
Dim lastRow As Long
|
||||
lastRow = wsMapping.Cells(wsMapping.Rows.count, 1).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
Dim key As String, val As String
|
||||
|
||||
' 从第3行开始读取
|
||||
For i = 3 To lastRow
|
||||
key = Trim(CStr(wsMapping.Cells(i, 1).Value))
|
||||
val = Trim(CStr(wsMapping.Cells(i, 2).Value))
|
||||
|
||||
If Len(key) > 0 And Len(val) > 0 Then
|
||||
If Not g_LcfwMapping.Exists(key) Then
|
||||
g_LcfwMapping.Add key, val
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 加载azxs映射(从D列:E列,列4:5)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub LoadAzxsMapping(wsMapping As Worksheet)
|
||||
Dim lastRow As Long
|
||||
lastRow = wsMapping.Cells(wsMapping.Rows.count, 4).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
Dim key As String, val As String
|
||||
|
||||
' 从第3行开始读取
|
||||
For i = 3 To lastRow
|
||||
key = Trim(CStr(wsMapping.Cells(i, 4).Value))
|
||||
val = Trim(CStr(wsMapping.Cells(i, 5).Value))
|
||||
|
||||
If Len(key) > 0 And Len(val) > 0 Then
|
||||
If Not g_AzxsMapping.Exists(key) Then
|
||||
g_AzxsMapping.Add key, val
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 应用预处理:使用正则表达式进行值映射和OR去重
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ApplyPreprocessing( _
|
||||
ByVal strCondition As String, _
|
||||
ByVal rowIdx As Long _
|
||||
) As String
|
||||
' 步骤1: 应用 azxs 映射
|
||||
' 正则模式: (azxs)( *=|!= *)([a-zA-Z0-9]{2})
|
||||
' 捕获组: key, operator, value (2位字母数字)
|
||||
strCondition = ApplyRegexMapping( _
|
||||
strCondition, _
|
||||
"(azxs)( *=|!= *)([a-zA-Z0-9]{2})", _
|
||||
g_AzxsMapping, _
|
||||
rowIdx, _
|
||||
"azxs" _
|
||||
)
|
||||
|
||||
' 步骤2: 应用 lcfw 映射
|
||||
' 正则模式: (lcfw)( *=|!= *)([a-zA-Z]\d{1,3})(?=[ \(\)]?)
|
||||
' 捕获组: key, operator, value (字母+1-3位数字)
|
||||
' 使用正向先行断言 (?=...) 确保不消耗后续字符
|
||||
strCondition = ApplyRegexMapping( _
|
||||
strCondition, _
|
||||
"(lcfw)( *=|!= *)([a-zA-Z]\d{1,3})(?=[ \(\)]?)", _
|
||||
g_LcfwMapping, _
|
||||
rowIdx, _
|
||||
"lcfw" _
|
||||
)
|
||||
|
||||
' 步骤3: 递归处理嵌套括号内的表达式(合并OR,简化括号)
|
||||
strCondition = ProcessNestedExpressions(strCondition, rowIdx)
|
||||
|
||||
' 步骤4: 合并顶层重复的OR条件
|
||||
strCondition = MergeDuplicateORConditions(strCondition)
|
||||
|
||||
' 步骤5: 简化不必要的括号
|
||||
strCondition = SimplifyParentheses(strCondition)
|
||||
|
||||
ApplyPreprocessing = strCondition
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 应用预处理(不含 lcfw 映射):用于"部件"类别
|
||||
' 执行步骤:azxs 映射 → 嵌套表达式处理 → OR合并 → 括号简化
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ApplyPreprocessingWithoutLcfw( _
|
||||
ByVal strCondition As String, _
|
||||
ByVal rowIdx As Long _
|
||||
) As String
|
||||
' 步骤1: 应用 azxs 映射
|
||||
strCondition = ApplyRegexMapping( _
|
||||
strCondition, _
|
||||
"(azxs)( *=|!= *)([a-zA-Z0-9]{2})", _
|
||||
g_AzxsMapping, _
|
||||
rowIdx, _
|
||||
"azxs" _
|
||||
)
|
||||
|
||||
' 步骤2: 递归处理嵌套括号内的表达式(合并OR,简化括号)
|
||||
strCondition = ProcessNestedExpressions(strCondition, rowIdx)
|
||||
|
||||
' 步骤3: 合并顶层重复的OR条件
|
||||
strCondition = MergeDuplicateORConditions(strCondition)
|
||||
|
||||
' 步骤4: 简化不必要的括号
|
||||
strCondition = SimplifyParentheses(strCondition)
|
||||
|
||||
ApplyPreprocessingWithoutLcfw = strCondition
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 递归处理嵌套表达式:先预处理括号内的内容,再进行OR合并
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ProcessNestedExpressions( _
|
||||
ByVal strCondition As String, _
|
||||
ByVal rowIdx As Long _
|
||||
) As String
|
||||
Dim result As String
|
||||
result = ""
|
||||
Dim i As Long
|
||||
Dim bracketLevel As Long
|
||||
bracketLevel = 0
|
||||
Dim inBracket As Boolean
|
||||
inBracket = False
|
||||
Dim bracketContent As String
|
||||
bracketContent = ""
|
||||
|
||||
For i = 1 To Len(strCondition)
|
||||
Dim char As String
|
||||
char = Mid(strCondition, i, 1)
|
||||
|
||||
If char = "(" Then
|
||||
bracketLevel = bracketLevel + 1
|
||||
If bracketLevel = 1 Then
|
||||
inBracket = True
|
||||
bracketContent = ""
|
||||
Else
|
||||
bracketContent = bracketContent & char
|
||||
End If
|
||||
ElseIf char = ")" Then
|
||||
If bracketLevel = 1 Then
|
||||
' 递归处理括号内的内容
|
||||
Dim processedContent As String
|
||||
processedContent = ProcessNestedExpressions(bracketContent, rowIdx)
|
||||
|
||||
' 对处理后的内容进行OR合并和简化
|
||||
processedContent = MergeDuplicateORConditions(processedContent)
|
||||
processedContent = SimplifyIfAllSame(processedContent)
|
||||
|
||||
' 重新组装:决定是否需要保留括号
|
||||
Dim needsParens As Boolean
|
||||
needsParens = HasTopLevelOperator(processedContent, " OR ") Or _
|
||||
HasTopLevelOperator(processedContent, " AND ")
|
||||
|
||||
If needsParens Then
|
||||
result = result & "(" & processedContent & ")"
|
||||
Else
|
||||
result = result & processedContent
|
||||
End If
|
||||
|
||||
inBracket = False
|
||||
Else
|
||||
bracketContent = bracketContent & char
|
||||
End If
|
||||
bracketLevel = bracketLevel - 1
|
||||
ElseIf inBracket Then
|
||||
bracketContent = bracketContent & char
|
||||
Else
|
||||
result = result & char
|
||||
End If
|
||||
Next i
|
||||
|
||||
ProcessNestedExpressions = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 使用正则表达式应用值映射
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ApplyRegexMapping( _
|
||||
ByVal strCondition As String, _
|
||||
ByVal pattern As String, _
|
||||
ByVal mapping As Object, _
|
||||
ByVal rowIdx As Long, _
|
||||
ByVal keyName As String _
|
||||
) As String
|
||||
' 创建 RegExp 对象 (Late Binding)
|
||||
Dim regex As Object
|
||||
Set regex = CreateObject("VBScript.RegExp")
|
||||
|
||||
With regex
|
||||
.Global = True ' 全局匹配
|
||||
.IgnoreCase = True ' 不区分大小写
|
||||
.pattern = pattern
|
||||
End With
|
||||
|
||||
' 执行匹配
|
||||
Dim matches As Object
|
||||
Set matches = regex.Execute(strCondition)
|
||||
|
||||
' 如果没有匹配,直接返回原字符串
|
||||
If matches.count = 0 Then
|
||||
ApplyRegexMapping = strCondition
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 执行替换:从后向前替换,避免位置偏移问题
|
||||
Dim result As String
|
||||
result = strCondition
|
||||
|
||||
Dim i As Long
|
||||
For i = matches.count - 1 To 0 Step -1
|
||||
Dim match As Object
|
||||
Set match = matches(i)
|
||||
|
||||
Dim originalValue As String
|
||||
originalValue = match.SubMatches(2)
|
||||
|
||||
' 查询映射表
|
||||
If mapping.Exists(originalValue) Then
|
||||
Dim mappedValue As String
|
||||
Dim replacementStr As String
|
||||
|
||||
mappedValue = mapping(originalValue)
|
||||
' 构建替换字符串,保留原始格式(空格等)
|
||||
replacementStr = match.SubMatches(0) & match.SubMatches(1) & mappedValue
|
||||
|
||||
' 使用正则对象的 Replace 方法进行精确替换
|
||||
' 创建精确匹配当前 match 的模式
|
||||
Dim exactPattern As String
|
||||
exactPattern = EscapeForRegex(match.Value)
|
||||
|
||||
Dim exactRegex As Object
|
||||
Set exactRegex = CreateObject("VBScript.RegExp")
|
||||
With exactRegex
|
||||
.Global = False ' 只替换第一个匹配(从后向前,每次只处理一个)
|
||||
.IgnoreCase = True
|
||||
.pattern = exactPattern
|
||||
End With
|
||||
|
||||
result = exactRegex.Replace(result, replacementStr)
|
||||
Else
|
||||
' 记录警告
|
||||
g_Logger.RecordWarning CStr(rowIdx), "M05.PreProcessor", "Mapping Warning", _
|
||||
"Value not found in mapping table: " & keyName & "=" & originalValue, match.Value
|
||||
End If
|
||||
Next i
|
||||
|
||||
ApplyRegexMapping = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 转义字符串用于正则表达式(转义特殊字符)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function EscapeForRegex(ByVal str As String) As String
|
||||
' 转义正则表达式特殊字符: . \ + * ? [ ] { } ( ) ^ $ |
|
||||
Dim result As String
|
||||
result = str
|
||||
|
||||
' 必须按顺序转义 \ 先转义
|
||||
result = Replace(result, "\", "\\")
|
||||
result = Replace(result, ".", "\.")
|
||||
result = Replace(result, "+", "\+")
|
||||
result = Replace(result, "*", "\*")
|
||||
result = Replace(result, "?", "\?")
|
||||
result = Replace(result, "[", "\[")
|
||||
result = Replace(result, "]", "\]")
|
||||
result = Replace(result, "{", "\{")
|
||||
result = Replace(result, "}", "\}")
|
||||
result = Replace(result, "(", "\(")
|
||||
result = Replace(result, ")", "\)")
|
||||
result = Replace(result, "^", "\^")
|
||||
result = Replace(result, "$", "\$")
|
||||
result = Replace(result, "|", "\|")
|
||||
|
||||
EscapeForRegex = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 合并重复的OR条件
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function MergeDuplicateORConditions( _
|
||||
ByVal strCondition As String _
|
||||
) As String
|
||||
' 按顶层OR分割
|
||||
Dim orSegments As Collection
|
||||
Set orSegments = SplitTopLevel(strCondition, " OR ")
|
||||
|
||||
' 如果只有一个分段或没有OR,直接返回
|
||||
If orSegments.count <= 1 Then
|
||||
MergeDuplicateORConditions = strCondition
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 使用Dictionary去重(标准化后比较)
|
||||
Dim uniqueSegments As Object
|
||||
Set uniqueSegments = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim segment As Variant
|
||||
For Each segment In orSegments
|
||||
Dim segStr As String
|
||||
segStr = CStr(segment)
|
||||
|
||||
' 标准化字符串用于比较(去除多余空格)
|
||||
Dim normalized As String
|
||||
normalized = NormalizeWhitespace(segStr)
|
||||
|
||||
If Not uniqueSegments.Exists(normalized) Then
|
||||
uniqueSegments.Add normalized, segStr
|
||||
End If
|
||||
Next segment
|
||||
|
||||
' 重新组合
|
||||
Dim result As String
|
||||
result = ""
|
||||
|
||||
Dim key As Variant
|
||||
Dim isFirst As Boolean
|
||||
isFirst = True
|
||||
|
||||
For Each key In uniqueSegments.keys
|
||||
If isFirst Then
|
||||
result = uniqueSegments(key)
|
||||
isFirst = False
|
||||
Else
|
||||
result = result & " OR " & uniqueSegments(key)
|
||||
End If
|
||||
Next key
|
||||
|
||||
MergeDuplicateORConditions = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 简化不必要的括号
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function SimplifyParentheses( _
|
||||
ByVal strCondition As String _
|
||||
) As String
|
||||
strCondition = Trim(strCondition)
|
||||
|
||||
' 如果没有外层括号,直接返回
|
||||
If Left(strCondition, 1) <> "(" Or Right(strCondition, 1) <> ")" Then
|
||||
SimplifyParentheses = strCondition
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 去掉外层括号,检查内容
|
||||
Dim innerContent As String
|
||||
innerContent = Mid(strCondition, 2, Len(strCondition) - 2)
|
||||
innerContent = Trim(innerContent)
|
||||
|
||||
' 检查内容是否包含顶层OR或AND
|
||||
Dim hasTopLevelOR As Boolean
|
||||
Dim hasTopLevelAND As Boolean
|
||||
hasTopLevelOR = HasTopLevelOperator(innerContent, " OR ")
|
||||
hasTopLevelAND = HasTopLevelOperator(innerContent, " AND ")
|
||||
|
||||
' 如果没有顶层操作符,可以去掉括号
|
||||
If Not hasTopLevelOR And Not hasTopLevelAND Then
|
||||
SimplifyParentheses = innerContent
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 如果有OR操作符但所有分段都相同,可以简化
|
||||
If hasTopLevelOR And Not hasTopLevelAND Then
|
||||
Dim simplified As String
|
||||
simplified = SimplifyIfAllSame(innerContent)
|
||||
|
||||
' 如果简化后没有括号,返回简化结果
|
||||
If Left(simplified, 1) <> "(" Then
|
||||
SimplifyParentheses = simplified
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
|
||||
' 保留括号
|
||||
SimplifyParentheses = strCondition
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 如果所有OR分段都相同,则简化为单个分段
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function SimplifyIfAllSame( _
|
||||
ByVal strExpr As String _
|
||||
) As String
|
||||
' 检查是否包含OR
|
||||
If Not HasTopLevelOperator(strExpr, " OR ") Then
|
||||
SimplifyIfAllSame = strExpr
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 分割OR分段
|
||||
Dim segments As Collection
|
||||
Set segments = SplitTopLevel(strExpr, " OR ")
|
||||
|
||||
If segments.count <= 1 Then
|
||||
SimplifyIfAllSame = strExpr
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 检查所有分段是否相同
|
||||
Dim allSame As Boolean
|
||||
allSame = True
|
||||
Dim firstSegment As String
|
||||
firstSegment = NormalizeWhitespace(CStr(segments(1)))
|
||||
|
||||
Dim i As Long
|
||||
For i = 2 To segments.count
|
||||
Dim segment As String
|
||||
segment = NormalizeWhitespace(CStr(segments(i)))
|
||||
If segment <> firstSegment Then
|
||||
allSame = False
|
||||
Exit For
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 如果所有分段都相同,返回第一个分段
|
||||
If allSame Then
|
||||
SimplifyIfAllSame = segments(1)
|
||||
Else
|
||||
SimplifyIfAllSame = strExpr
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 检查字符串是否包含顶层操作符(不在括号内的操作符)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function HasTopLevelOperator( _
|
||||
ByVal strExpr As String, _
|
||||
ByVal operator As String _
|
||||
) As Boolean
|
||||
Dim bracketLevel As Long
|
||||
bracketLevel = 0
|
||||
Dim i As Long
|
||||
Dim lenOp As Long
|
||||
lenOp = Len(operator)
|
||||
|
||||
For i = 1 To Len(strExpr) - lenOp + 1
|
||||
Dim char As String
|
||||
char = Mid(strExpr, i, 1)
|
||||
|
||||
If char = "(" Then
|
||||
bracketLevel = bracketLevel + 1
|
||||
ElseIf char = ")" Then
|
||||
bracketLevel = bracketLevel - 1
|
||||
ElseIf bracketLevel = 0 Then
|
||||
If Mid(strExpr, i, lenOp) = operator Then
|
||||
HasTopLevelOperator = True
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
HasTopLevelOperator = False
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 顶层分割(尊重括号嵌套)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function SplitTopLevel( _
|
||||
ByVal strExpr As String, _
|
||||
ByVal delimiter As String _
|
||||
) As Collection
|
||||
Dim result As New Collection
|
||||
Dim currentSegment As String
|
||||
currentSegment = ""
|
||||
|
||||
Dim i As Long
|
||||
Dim bracketLevel As Long
|
||||
bracketLevel = 0
|
||||
|
||||
Dim lenDelim As Long
|
||||
lenDelim = Len(delimiter)
|
||||
|
||||
i = 1
|
||||
Do While i <= Len(strExpr)
|
||||
Dim char As String
|
||||
char = Mid(strExpr, i, 1)
|
||||
|
||||
If char = "(" Then
|
||||
bracketLevel = bracketLevel + 1
|
||||
currentSegment = currentSegment & char
|
||||
ElseIf char = ")" Then
|
||||
bracketLevel = bracketLevel - 1
|
||||
currentSegment = currentSegment & char
|
||||
ElseIf bracketLevel = 0 Then
|
||||
' 检查是否匹配分隔符
|
||||
If i + lenDelim - 1 <= Len(strExpr) Then
|
||||
Dim checkStr As String
|
||||
checkStr = Mid(strExpr, i, lenDelim)
|
||||
|
||||
If UCase(checkStr) = delimiter Then
|
||||
' 找到分隔符,保存当前分段
|
||||
result.Add Trim(currentSegment)
|
||||
currentSegment = ""
|
||||
i = i + lenDelim - 1 ' 跳过分隔符
|
||||
Else
|
||||
currentSegment = currentSegment & char
|
||||
End If
|
||||
Else
|
||||
currentSegment = currentSegment & char
|
||||
End If
|
||||
Else
|
||||
currentSegment = currentSegment & char
|
||||
End If
|
||||
|
||||
i = i + 1
|
||||
Loop
|
||||
|
||||
' 添加最后一个分段
|
||||
If Len(Trim(currentSegment)) > 0 Then
|
||||
result.Add Trim(currentSegment)
|
||||
End If
|
||||
|
||||
Set SplitTopLevel = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 标准化空白字符
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function NormalizeWhitespace(ByVal str As String) As String
|
||||
' 去除多余空格
|
||||
Dim result As String
|
||||
result = Trim(str)
|
||||
|
||||
' 将连续多个空格替换为单个空格
|
||||
Do While InStr(result, " ") > 0
|
||||
result = Replace(result, " ", " ")
|
||||
Loop
|
||||
|
||||
' 标准化 " AND " 和 " OR "
|
||||
result = Replace(result, " AND ", " AND ")
|
||||
result = Replace(result, " OR ", " OR ")
|
||||
|
||||
NormalizeWhitespace = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试辅助函数:获取lcfw映射值
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function GetLcfwMappedValue(key As String) As String
|
||||
If g_LcfwMapping.Exists(key) Then
|
||||
GetLcfwMappedValue = g_LcfwMapping(key)
|
||||
Else
|
||||
GetLcfwMappedValue = ""
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试辅助函数:获取azxs映射值
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function GetAzxsMappedValue(key As String) As String
|
||||
If g_AzxsMapping.Exists(key) Then
|
||||
GetAzxsMappedValue = g_AzxsMapping(key)
|
||||
Else
|
||||
GetAzxsMappedValue = ""
|
||||
End If
|
||||
End Function
|
||||
@@ -1,210 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M06A_Mapper
|
||||
' 职责: 值映射模块,为BOM Extraction System提供azxs和lcfw参数映射
|
||||
'
|
||||
' 功能:
|
||||
' - 从"对照表"工作表加载映射数据
|
||||
' - 将原始值转换为"原始值,映射值"格式
|
||||
' - 支持azxs(安装形式)和lcfw(量程范围)映射
|
||||
'
|
||||
' 使用场景:
|
||||
' 产品型号解析后,调用映射器为参数添加映射值,支持双值匹配
|
||||
'
|
||||
' 示例:
|
||||
' 输入: azxs="A0"
|
||||
' 输出: azxs="A0,径向"
|
||||
'
|
||||
' BOM库中azxs="A0"或"径向"均可匹配成功
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 模块级常量 - 映射表配置
|
||||
Private Const MAPPING_COL_LCFW_KEY As Long = 1 ' A列 - lcfw原始值
|
||||
Private Const MAPPING_COL_LCFW_VAL As Long = 2 ' B列 - lcfw映射值
|
||||
Private Const MAPPING_COL_AZXS_KEY As Long = 4 ' D列 - azxs原始值
|
||||
Private Const MAPPING_COL_AZXS_VAL As Long = 5 ' E列 - azxs映射值
|
||||
Private Const MAPPING_START_ROW As Long = 3 ' 数据起始行
|
||||
|
||||
' 模块级变量
|
||||
Private g_LcfwMapping As Object ' Scripting.Dictionary - lcfw映射表
|
||||
Private g_AzxsMapping As Object ' Scripting.Dictionary - azxs映射表
|
||||
Private g_Logger As clsErrorLogger
|
||||
Private g_IsInitialized As Boolean
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化映射器
|
||||
'
|
||||
' 输入:
|
||||
' logger - 错误记录器
|
||||
' wsMapping - 对照表工作表
|
||||
'
|
||||
' 说明:
|
||||
' 从对照表工作表加载lcfw和azxs映射数据
|
||||
' - lcfw映射: A列:B列 (M01->低压, M12->高压)
|
||||
' - azxs映射: D列:E列 (A0->径向, AT->径向, etc.)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitMapper(logger As clsErrorLogger, wsMapping As Worksheet)
|
||||
Set g_Logger = logger
|
||||
Set g_LcfwMapping = CreateObject("Scripting.Dictionary")
|
||||
Set g_AzxsMapping = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 从[对照表]加载映射
|
||||
Call LoadLcfwMapping(wsMapping)
|
||||
Call LoadAzxsMapping(wsMapping)
|
||||
|
||||
g_IsInitialized = True
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 检查是否已初始化
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function IsInitialized() As Boolean
|
||||
IsInitialized = g_IsInitialized
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 映射azxs值
|
||||
'
|
||||
' 输入:
|
||||
' rawValue - 原始azxs值(如"A0")
|
||||
'
|
||||
' 输出:
|
||||
' String - "raw,mapped"格式(如"A0,径向")或原始值(如果未找到映射)
|
||||
'
|
||||
' 示例:
|
||||
' MapAzxs("A0") = "A0,径向"
|
||||
' MapAzxs("AT") = "AT,径向"
|
||||
' MapAzxs("XX") = "XX" (未找到映射)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function MapAzxs(ByVal rawValue As String) As String
|
||||
rawValue = Trim(rawValue)
|
||||
|
||||
' 如果未初始化或值为空,直接返回原始值
|
||||
If Not g_IsInitialized Or Len(rawValue) = 0 Then
|
||||
MapAzxs = rawValue
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 查找映射值
|
||||
If g_AzxsMapping.Exists(rawValue) Then
|
||||
Dim mappedValue As String
|
||||
mappedValue = g_AzxsMapping(rawValue)
|
||||
MapAzxs = rawValue & "," & mappedValue
|
||||
Else
|
||||
' 未找到映射,返回原始值
|
||||
MapAzxs = rawValue
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 映射lcfw值
|
||||
'
|
||||
' 输入:
|
||||
' rawValue - 原始lcfw值(如"M01")
|
||||
'
|
||||
' 输出:
|
||||
' String - "raw,mapped"格式(如"M01,低压")或原始值(如果未找到映射)
|
||||
'
|
||||
' 示例:
|
||||
' MapLcfw("M01") = "M01,低压"
|
||||
' MapLcfw("M12") = "M12,高压"
|
||||
' MapLcfw("XX") = "XX" (未找到映射)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function MapLcfw(ByVal rawValue As String) As String
|
||||
rawValue = Trim(rawValue)
|
||||
|
||||
' 如果未初始化或值为空,直接返回原始值
|
||||
If Not g_IsInitialized Or Len(rawValue) = 0 Then
|
||||
MapLcfw = rawValue
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 查找映射值
|
||||
If g_LcfwMapping.Exists(rawValue) Then
|
||||
Dim mappedValue As String
|
||||
mappedValue = g_LcfwMapping(rawValue)
|
||||
MapLcfw = rawValue & "," & mappedValue
|
||||
Else
|
||||
' 未找到映射,返回原始值
|
||||
MapLcfw = rawValue
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 加载lcfw映射(从A列:B列,列1:2)
|
||||
'
|
||||
' 数据格式:
|
||||
' Row 3+: M01 | 低压
|
||||
' M12 | 高压
|
||||
' etc.
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub LoadLcfwMapping(wsMapping As Worksheet)
|
||||
Dim lastRow As Long
|
||||
lastRow = wsMapping.Cells(wsMapping.Rows.count, 1).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
Dim key As String, val As String
|
||||
|
||||
' 从第3行开始读取
|
||||
For i = MAPPING_START_ROW To lastRow
|
||||
key = Trim(CStr(wsMapping.Cells(i, MAPPING_COL_LCFW_KEY).Value))
|
||||
val = Trim(CStr(wsMapping.Cells(i, MAPPING_COL_LCFW_VAL).Value))
|
||||
|
||||
If Len(key) > 0 And Len(val) > 0 Then
|
||||
If Not g_LcfwMapping.Exists(key) Then
|
||||
g_LcfwMapping.Add key, val
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 加载azxs映射(从D列:E列,列4:5)
|
||||
'
|
||||
' 数据格式:
|
||||
' Row 3+: A0 | 径向
|
||||
' AT | 径向
|
||||
' B0 | 下轴向
|
||||
' etc.
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub LoadAzxsMapping(wsMapping As Worksheet)
|
||||
Dim lastRow As Long
|
||||
lastRow = wsMapping.Cells(wsMapping.Rows.count, MAPPING_COL_AZXS_KEY).End(xlUp).row
|
||||
|
||||
Dim i As Long
|
||||
Dim key As String, val As String
|
||||
|
||||
' 从第3行开始读取
|
||||
For i = MAPPING_START_ROW To lastRow
|
||||
key = Trim(CStr(wsMapping.Cells(i, MAPPING_COL_AZXS_KEY).Value))
|
||||
val = Trim(CStr(wsMapping.Cells(i, MAPPING_COL_AZXS_VAL).Value))
|
||||
|
||||
If Len(key) > 0 And Len(val) > 0 Then
|
||||
If Not g_AzxsMapping.Exists(key) Then
|
||||
g_AzxsMapping.Add key, val
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试辅助函数:获取lcfw映射值
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function GetLcfwMappedValue(key As String) As String
|
||||
If g_LcfwMapping.Exists(key) Then
|
||||
GetLcfwMappedValue = g_LcfwMapping(key)
|
||||
Else
|
||||
GetLcfwMappedValue = ""
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试辅助函数:获取azxs映射值
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function GetAzxsMappedValue(key As String) As String
|
||||
If g_AzxsMapping.Exists(key) Then
|
||||
GetAzxsMappedValue = g_AzxsMapping(key)
|
||||
Else
|
||||
GetAzxsMappedValue = ""
|
||||
End If
|
||||
End Function
|
||||
@@ -1,412 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M06B_TestRunner
|
||||
' 职责: BOM自动提取系统的单元测试
|
||||
'
|
||||
' 测试模块:
|
||||
' - M06_ModelParser: 型号解析测试
|
||||
' - M07_BOMMatcher: BOM匹配测试
|
||||
' - M08_ComponentProcessor: 部件处理测试
|
||||
' - M09_BOMExtractor: 提取流程测试
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
Private m_Logger As clsErrorLogger
|
||||
Private m_FailCount As Long
|
||||
Private m_PassCount As Long
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 运行所有BOM提取测试
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub RunBOMExtractionTests()
|
||||
' 初始化环境
|
||||
Set m_Logger = New clsErrorLogger
|
||||
M06_ModelParser.InitModelParser m_Logger
|
||||
M07_BOMMatcher.InitBOMMatcher m_Logger
|
||||
M08_ComponentProcessor.InitComponentProcessor m_Logger
|
||||
|
||||
m_FailCount = 0
|
||||
m_PassCount = 0
|
||||
|
||||
Debug.Print String(60, "=")
|
||||
Debug.Print "开始运行BOM自动提取系统测试: " & Now
|
||||
Debug.Print String(60, "-")
|
||||
|
||||
' M06_ModelParser 测试
|
||||
Debug.Print vbCrLf & "[M06_ModelParser 测试]"
|
||||
Debug.Print String(60, "-")
|
||||
Test_MP_01_简单型号解析
|
||||
Test_MP_02_复杂型号带管道符
|
||||
Test_MP_03_过程连接与材质分离
|
||||
Test_MP_04_量程范围提取
|
||||
Test_MP_05_附加功能提取
|
||||
Test_MP_06_多个附加功能
|
||||
Test_MP_07_不完整型号验证
|
||||
Test_MP_08_空型号处理
|
||||
|
||||
' M07_BOMMatcher 测试
|
||||
Debug.Print vbCrLf & "[M07_BOMMatcher 测试]"
|
||||
Debug.Print String(60, "-")
|
||||
Test_BM_01_精确匹配
|
||||
Test_BM_02_空值通配符匹配
|
||||
Test_BM_03_否定条件匹配
|
||||
Test_BM_04_附加功能包含匹配
|
||||
Test_BM_05_fjgn包含逻辑
|
||||
|
||||
' M08_ComponentProcessor 测试
|
||||
Debug.Print vbCrLf & "[M08_ComponentProcessor 测试]"
|
||||
Debug.Print String(60, "-")
|
||||
Test_CP_01_验证仅部件
|
||||
Test_CP_02_验证接头加弹性元件
|
||||
Test_CP_03_无效组合缺少弹性元件
|
||||
Test_CP_04_无效组合重复类型
|
||||
Test_CP_05_空物料列表验证
|
||||
|
||||
' 汇总结果
|
||||
Debug.Print String(60, "-")
|
||||
If m_FailCount = 0 Then
|
||||
Debug.Print "测试结果: ALL PASS! (共 " & m_PassCount & " 个测试点)"
|
||||
Else
|
||||
Debug.Print "测试结果: 失败 " & m_FailCount & " 个, 通过 " & m_PassCount & " 个"
|
||||
End If
|
||||
Debug.Print String(60, "=")
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' M06_ModelParser 测试用例
|
||||
' ==============================================================================
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_01: 简单型号解析
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_01_简单型号解析()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M04.Y3")
|
||||
|
||||
Assert_NotNull params, "MP01_Params_Not_Null"
|
||||
Assert_Equal params.count, 8, "MP01_Params_Count"
|
||||
|
||||
Assert_Equal params("xh"), "YTHN", "MP01_xh"
|
||||
Assert_Equal params("gcwj"), "100", "MP01_gcwj"
|
||||
Assert_Equal params("azxs"), "A0", "MP01_azxs"
|
||||
Assert_Equal params("bkxs"), "531", "MP01_bkxs"
|
||||
Assert_Equal params("gclj"), "G12", "MP01_gclj"
|
||||
Assert_Equal params("jycz"), "3", "MP01_jycz"
|
||||
Assert_Equal params("lcfw"), "M04", "MP01_lcfw"
|
||||
Assert_Equal params("fjgn"), "Y3", "MP01_fjgn"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_02: 复杂型号带管道符
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_02_复杂型号带管道符()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3")
|
||||
|
||||
Assert_NotNull params, "MP02_Params_Not_Null"
|
||||
Assert_Equal params("azxs"), "A0", "MP02_azxs"
|
||||
Assert_Equal params("bkxs"), "531", "MP02_bkxs"
|
||||
Assert_Equal params("lcfw"), "M04", "MP02_lcfw"
|
||||
Assert_Equal params("fjgn"), "Y3", "MP02_fjgn"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_03: 过程连接与材质分离
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_03_过程连接与材质分离()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("YTHN-100.BZ.531.M201.M09.Y3")
|
||||
|
||||
Assert_Equal params("gclj"), "M20", "MP03_gclj"
|
||||
Assert_Equal params("jycz"), "1", "MP03_jycz"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_04: 量程范围提取
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_04_量程范围提取()
|
||||
Dim params1 As Object
|
||||
Set params1 = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M06.Y3")
|
||||
Assert_Equal params1("lcfw"), "M06", "MP04_lcfw_M06"
|
||||
|
||||
Dim params2 As Object
|
||||
Set params2 = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M16.Y3")
|
||||
Assert_Equal params2("lcfw"), "M16", "MP04_lcfw_M16"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_05: 附加功能提取
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_05_附加功能提取()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M04.N1.Y3")
|
||||
|
||||
Assert_Equal params("fjgn"), "N1,Y3", "MP05_fjgn_N1"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_06: 多个附加功能
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_06_多个附加功能()
|
||||
Dim params1 As Object
|
||||
Set params1 = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M04.N1,N2.Y3")
|
||||
Assert_Equal params1("fjgn"), "N1,N2,Y3", "MP06_fjgn_Comma"
|
||||
|
||||
Dim params2 As Object
|
||||
Set params2 = M06_ModelParser.ParseProductModel("YTHN-100.A0.531.G123.M04.N1.N2.Y3")
|
||||
Assert_Equal params2("fjgn"), "N1,N2,Y3", "MP06_fjgn_Dot"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_07: 不完整型号验证
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_07_不完整型号验证()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("YTHN-100.A0")
|
||||
|
||||
' 不完整型号仍应返回字典,但字段较少
|
||||
Assert_NotNull params, "MP07_Params_Not_Null"
|
||||
Assert_True params.Exists("azxs"), "MP07_Has_azxs"
|
||||
Assert_False params.Exists("bkxs"), "MP07_No_bkxs"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 MP_08: 空型号处理
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_MP_08_空型号处理()
|
||||
Dim params As Object
|
||||
Set params = M06_ModelParser.ParseProductModel("")
|
||||
|
||||
Assert_NotNull params, "MP08_Params_Not_Null"
|
||||
Assert_Equal params.count, 0, "MP08_Empty_Count"
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' M07_BOMMatcher 测试用例
|
||||
' ==============================================================================
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 BM_01: 精确匹配
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_BM_01_精确匹配()
|
||||
Dim cellValue As String
|
||||
cellValue = "A0"
|
||||
Dim paramValue As String
|
||||
paramValue = "A0"
|
||||
|
||||
Dim result As Boolean
|
||||
result = M07_BOMMatcher.EvaluateCellCondition(cellValue, paramValue, "azxs")
|
||||
|
||||
Assert_Equal result, True, "BM01_Exact_Match"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 BM_02: 空值通配符匹配
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_BM_02_空值通配符匹配()
|
||||
Dim cellValue As Variant
|
||||
cellValue = ""
|
||||
Dim paramValue As String
|
||||
paramValue = "A0"
|
||||
|
||||
Dim result As Boolean
|
||||
result = M07_BOMMatcher.EvaluateCellCondition(cellValue, paramValue, "azxs")
|
||||
|
||||
Assert_Equal result, True, "BM02_Wildcard_Match"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 BM_03: 否定条件匹配
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_BM_03_否定条件匹配()
|
||||
Dim cellValue As String
|
||||
cellValue = "!=A0"
|
||||
Dim paramValue As String
|
||||
paramValue = "B0"
|
||||
|
||||
Dim result As Boolean
|
||||
result = M07_BOMMatcher.EvaluateCellCondition(cellValue, paramValue, "azxs")
|
||||
|
||||
Assert_Equal result, True, "BM03_Not_Equal_Match"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 BM_04: 附加功能包含匹配
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_BM_04_附加功能包含匹配()
|
||||
Dim cellValue As String
|
||||
cellValue = "N1"
|
||||
Dim fjgnList As String
|
||||
fjgnList = "N1,N2,Y3"
|
||||
|
||||
Dim result As Boolean
|
||||
result = M07_BOMMatcher.CheckFjgnMatch(cellValue, fjgnList)
|
||||
|
||||
Assert_Equal result, True, "BM04_Fjgn_Contains_N1"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 BM_05: fjgn包含逻辑
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_BM_05_fjgn包含逻辑()
|
||||
' 测试包含
|
||||
Assert_True M07_BOMMatcher.CheckFjgnMatch("N3", "N3,N2"), "BM05_Contains_N3"
|
||||
Assert_True M07_BOMMatcher.CheckFjgnMatch("N2", "N3,N2"), "BM05_Contains_N2"
|
||||
|
||||
' 测试不包含
|
||||
Assert_False M07_BOMMatcher.CheckFjgnMatch("N1", "N3,N2"), "BM05_Not_Contains_N1"
|
||||
|
||||
' 测试空列表
|
||||
Assert_False M07_BOMMatcher.CheckFjgnMatch("N1", ""), "BM05_Empty_List"
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' M08_ComponentProcessor 测试用例
|
||||
' ==============================================================================
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 CP_01: 验证仅部件
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_CP_01_验证仅部件()
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
Dim mat1 As Object
|
||||
Set mat1 = CreateMaterial("部件", "部件A", "C001", 1)
|
||||
materials.Add mat1
|
||||
|
||||
Dim validation As Object
|
||||
Set validation = M08_ComponentProcessor.ValidateComponentCombination(materials)
|
||||
|
||||
Assert_Equal validation("valid"), True, "CP01_Valid_Component"
|
||||
Assert_True InStr(validation("message"), "1个部件") > 0, "CP01_Message_Content"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 CP_02: 验证接头加弹性元件
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_CP_02_验证接头加弹性元件()
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
Dim mat1 As Object
|
||||
Set mat1 = CreateMaterial("接头", "接头A", "J001", 1)
|
||||
materials.Add mat1
|
||||
|
||||
Dim mat2 As Object
|
||||
Set mat2 = CreateMaterial("弹性元件", "元件A", "E001", 1)
|
||||
materials.Add mat2
|
||||
|
||||
Dim validation As Object
|
||||
Set validation = M08_ComponentProcessor.ValidateComponentCombination(materials)
|
||||
|
||||
Assert_Equal validation("valid"), True, "CP02_Valid_Joint_Element"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 CP_03: 无效组合缺少弹性元件
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_CP_03_无效组合缺少弹性元件()
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
Dim mat1 As Object
|
||||
Set mat1 = CreateMaterial("接头", "接头A", "J001", 1)
|
||||
materials.Add mat1
|
||||
|
||||
Dim validation As Object
|
||||
Set validation = M08_ComponentProcessor.ValidateComponentCombination(materials)
|
||||
|
||||
Assert_Equal validation("valid"), False, "CP03_Invalid_No_Element"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 CP_04: 无效组合重复类型
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_CP_04_无效组合重复类型()
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
Dim mat1 As Object
|
||||
Set mat1 = CreateMaterial("部件", "部件A", "C001", 1)
|
||||
materials.Add mat1
|
||||
|
||||
Dim mat2 As Object
|
||||
Set mat2 = CreateMaterial("接头", "接头A", "J001", 1)
|
||||
materials.Add mat2
|
||||
|
||||
Dim validation As Object
|
||||
Set validation = M08_ComponentProcessor.ValidateComponentCombination(materials)
|
||||
|
||||
Assert_Equal validation("valid"), False, "CP04_Invalid_Both"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 CP_05: 空物料列表验证
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_CP_05_空物料列表验证()
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
Dim validation As Object
|
||||
Set validation = M08_ComponentProcessor.ValidateComponentCombination(materials)
|
||||
|
||||
Assert_Equal validation("valid"), False, "CP05_Empty_List"
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助函数
|
||||
' ==============================================================================
|
||||
|
||||
' 辅助断言函数
|
||||
Private Sub Assert_Equal(actual As Variant, expected As Variant, testName As String)
|
||||
If CStr(actual) = CStr(expected) Then
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Expected: " & expected & ", Actual: " & actual
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub Assert_True(actual As Boolean, testName As String)
|
||||
If actual Then
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Expected: True, Actual: False"
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub Assert_False(actual As Boolean, testName As String)
|
||||
If Not actual Then
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Expected: False, Actual: True"
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub Assert_NotNull(obj As Object, testName As String)
|
||||
If Not obj Is Nothing Then
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Object is Nothing"
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' 创建测试物料对象
|
||||
Private Function CreateMaterial( _
|
||||
ByVal matType As String, _
|
||||
ByVal matName As String, _
|
||||
ByVal matCode As String, _
|
||||
ByVal matQty As Long _
|
||||
) As Object
|
||||
Dim mat As Object
|
||||
Set mat = CreateObject("Scripting.Dictionary")
|
||||
mat("materialType") = matType
|
||||
mat("materialName") = matName
|
||||
mat("materialCode") = matCode
|
||||
mat("materialQty") = matQty
|
||||
mat("remarks") = ""
|
||||
Set CreateMaterial = mat
|
||||
End Function
|
||||
@@ -1,335 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M06_ModelParser
|
||||
' 职责: 产品型号解析,从完整产品型号中提取关键参数
|
||||
'
|
||||
' 产品型号结构: [表头]|[表盘]|[附件]|[法兰隔膜]
|
||||
' 表头结构: [型号]-[公称外径].[安装形式].[壳体形式].[过程连接&接液材质].[量程范围].[仪表特性]
|
||||
'
|
||||
' 示例: YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3
|
||||
' 表头: YTHN-100.A0.531.G123.M04.Y3
|
||||
' azxs: A0, bkxs: 531, gclj: G12, jycz: 3, lcfw: M04, fjgn: Y3
|
||||
'
|
||||
' 注意: 仅处理表头部分,其他部分(表盘、附件、法兰隔膜)暂时丢弃
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 模块级变量 - 错误记录器
|
||||
Private g_Logger As clsErrorLogger
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化型号解析器
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitModelParser(logger As clsErrorLogger)
|
||||
Set g_Logger = logger
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 解析产品型号
|
||||
'
|
||||
' 输入:
|
||||
' modelString - 完整的产品型号字符串
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 包含提取的参数
|
||||
' 键值对: "xh"->型号, "gcwj"->公称外径, "azxs"->安装形式,
|
||||
' "bkxs"->表壳形式, "gclj"->过程连接, "jycz"->接液材质,
|
||||
' "lcfw"->量程范围, "fjgn"->附加功能
|
||||
'
|
||||
' 示例:
|
||||
' Set params = ParseProductModel("YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3")
|
||||
' ' params("azxs") = "A0"
|
||||
' ' params("gclj") = "G12"
|
||||
' ' params("jycz") = "3"
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function ParseProductModel(ByVal modelString As String) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim params As Object
|
||||
Set params = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 预处理:去除前后空格
|
||||
modelString = Trim(modelString)
|
||||
|
||||
' 如果型号为空,返回空字典
|
||||
If Len(modelString) = 0 Then
|
||||
Set ParseProductModel = params
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤1: 提取表头部分(第一个管道符之前的部分)
|
||||
Dim headerPart As String
|
||||
headerPart = ExtractHeaderPart(modelString)
|
||||
|
||||
If Len(headerPart) = 0 Then
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M06.ParseProductModel", "ModelParseError", _
|
||||
"无法提取表头部分,型号可能为空或格式错误", modelString
|
||||
End If
|
||||
Set ParseProductModel = params
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤2: 分割表头为段
|
||||
Dim segments As Variant
|
||||
segments = SplitHeaderPart(headerPart)
|
||||
|
||||
If Not IsArray(segments) Then
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M06.ParseProductModel", "ModelParseError", _
|
||||
"表头分割失败", headerPart
|
||||
End If
|
||||
Set ParseProductModel = params
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤3: 验证段数是否足够
|
||||
If UBound(segments) - LBound(segments) + 1 < MODEL_HEADER_MIN_SEGMENTS Then
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M06.ParseProductModel", "ModelParseError", _
|
||||
"表头段数不足,需要至少" & MODEL_HEADER_MIN_SEGMENTS & "段,实际" & _
|
||||
(UBound(segments) - LBound(segments) + 1) & "段", headerPart
|
||||
End If
|
||||
End If
|
||||
|
||||
' 步骤4: 提取型号和公称外径(预留,暂不参与BOM匹配)
|
||||
Call ExtractModelAndSize(headerPart, params)
|
||||
|
||||
' 步骤5: 提取各参数
|
||||
' 注意:segments(0)是"型号-公称外径",需要从segments(1)开始提取参数
|
||||
|
||||
' 提取原始值
|
||||
Dim azxsRaw As String, bkxsRaw As String
|
||||
Dim gcljRaw As String, jyczRaw As String
|
||||
Dim lcfwRaw As String, fjgnRaw As String
|
||||
|
||||
If UBound(segments) - LBound(segments) + 1 >= 2 Then
|
||||
azxsRaw = ExtractAzxs(segments(1))
|
||||
End If
|
||||
|
||||
If UBound(segments) - LBound(segments) + 1 >= 3 Then
|
||||
bkxsRaw = ExtractBkxs(segments(2))
|
||||
End If
|
||||
|
||||
If UBound(segments) - LBound(segments) + 1 >= 4 Then
|
||||
Call ExtractGcljAndJycz(segments(3), gcljRaw, jyczRaw)
|
||||
End If
|
||||
|
||||
If UBound(segments) - LBound(segments) + 1 >= 5 Then
|
||||
lcfwRaw = ExtractLcfw(segments(4))
|
||||
End If
|
||||
|
||||
If UBound(segments) - LBound(segments) + 1 >= 6 Then
|
||||
fjgnRaw = ExtractFjgn(segments, 5)
|
||||
End If
|
||||
|
||||
' 步骤6: 应用映射(如果映射器已初始化)
|
||||
If M06A_Mapper.IsInitialized() Then
|
||||
' 应用azxs和lcfw映射,返回双值格式(raw,mapped)
|
||||
If Len(azxsRaw) > 0 Then
|
||||
params("azxs") = M06A_Mapper.MapAzxs(azxsRaw)
|
||||
End If
|
||||
If Len(lcfwRaw) > 0 Then
|
||||
params("lcfw") = M06A_Mapper.MapLcfw(lcfwRaw)
|
||||
End If
|
||||
Else
|
||||
' 映射器未初始化,使用原始值
|
||||
If Len(azxsRaw) > 0 Then
|
||||
params("azxs") = azxsRaw
|
||||
End If
|
||||
If Len(lcfwRaw) > 0 Then
|
||||
params("lcfw") = lcfwRaw
|
||||
End If
|
||||
End If
|
||||
|
||||
' 添加其他参数(不需要映射)
|
||||
If Len(bkxsRaw) > 0 Then
|
||||
params("bkxs") = bkxsRaw
|
||||
End If
|
||||
If Len(gcljRaw) > 0 Then
|
||||
params("gclj") = gcljRaw
|
||||
End If
|
||||
If Len(jyczRaw) > 0 Then
|
||||
params("jycz") = jyczRaw
|
||||
End If
|
||||
If Len(fjgnRaw) > 0 Then
|
||||
params("fjgn") = fjgnRaw
|
||||
End If
|
||||
|
||||
Set ParseProductModel = params
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M06.ParseProductModel", "SystemError", _
|
||||
"解析过程发生错误: " & err.Description, modelString
|
||||
End If
|
||||
Set ParseProductModel = CreateObject("Scripting.Dictionary")
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取表头部分(管道符之前)
|
||||
'
|
||||
' 输入: YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3
|
||||
' 输出: YTHN-100.A0.531.G123.M04.Y3
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractHeaderPart(ByVal fullModel As String) As String
|
||||
Dim pipePos As Long
|
||||
pipePos = InStr(fullModel, MODEL_SEPARATOR_PIPELINE)
|
||||
|
||||
If pipePos > 0 Then
|
||||
ExtractHeaderPart = Left(fullModel, pipePos - 1)
|
||||
Else
|
||||
ExtractHeaderPart = fullModel
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 分割表头部分为段数组
|
||||
'
|
||||
' 输入: YTHN-100.A0.531.G123.M04.Y3
|
||||
' 输出: Array("YTHN-100", "A0", "531", "G123", "M04", "Y3")
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function SplitHeaderPart(ByVal headerPart As String) As Variant
|
||||
' 按点号分割
|
||||
Dim rawSegments As Variant
|
||||
rawSegments = Split(headerPart, MODEL_SEPARATOR_DOT)
|
||||
|
||||
' 如果分割失败或结果为空
|
||||
If Not IsArray(rawSegments) Then
|
||||
SplitHeaderPart = Null
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 去除每个段的空格
|
||||
Dim i As Long
|
||||
For i = LBound(rawSegments) To UBound(rawSegments)
|
||||
rawSegments(i) = Trim(CStr(rawSegments(i)))
|
||||
Next i
|
||||
|
||||
SplitHeaderPart = rawSegments
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取型号和公称外径(预留字段)
|
||||
'
|
||||
' 输入: YTHN-100
|
||||
' 输出: params("xh")="YTHN", params("gcwj")="100"
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub ExtractModelAndSize(ByVal firstSegment As String, ByRef params As Object)
|
||||
Dim dashPos As Long
|
||||
dashPos = InStr(firstSegment, "-")
|
||||
|
||||
If dashPos > 1 Then
|
||||
params("xh") = Left(firstSegment, dashPos - 1)
|
||||
Dim afterDash As String
|
||||
afterDash = Mid(firstSegment, dashPos + 1)
|
||||
|
||||
' 提取-号后的数字部分(公称外径)
|
||||
' 因为firstSegment是"YTHN-100.A0...",需要去掉后面的点号内容
|
||||
Dim dotPos As Long
|
||||
dotPos = InStr(afterDash, ".")
|
||||
If dotPos > 0 Then
|
||||
params("gcwj") = Left(afterDash, dotPos - 1)
|
||||
Else
|
||||
params("gcwj") = afterDash
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取安装形式
|
||||
'
|
||||
' 输入: A0
|
||||
' 输出: A0
|
||||
'
|
||||
' 规则: 直接返回第一段的值
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractAzxs(ByVal segment As String) As String
|
||||
ExtractAzxs = Trim(segment)
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取表壳形式
|
||||
'
|
||||
' 输入: 531
|
||||
' 输出: 531
|
||||
'
|
||||
' 规则: 直接返回第二段的值
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractBkxs(ByVal segment As String) As String
|
||||
ExtractBkxs = Trim(segment)
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取过程连接和接液材质
|
||||
'
|
||||
' 输入: G123
|
||||
' 输出: gclj="G12", jycz="3"
|
||||
'
|
||||
' 规则:
|
||||
' - 过程连接: 去除最后一位
|
||||
' - 接液材质: 最后一位
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub ExtractGcljAndJycz(ByVal segment As String, ByRef gclj As String, ByRef jycz As String)
|
||||
segment = Trim(segment)
|
||||
|
||||
If Len(segment) >= 1 Then
|
||||
jycz = Right(segment, 1)
|
||||
If Len(segment) > 1 Then
|
||||
gclj = Left(segment, Len(segment) - 1)
|
||||
Else
|
||||
gclj = ""
|
||||
End If
|
||||
Else
|
||||
gclj = ""
|
||||
jycz = ""
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取量程范围
|
||||
'
|
||||
' 输入: M04
|
||||
' 输出: M04
|
||||
'
|
||||
' 规则: 直接返回第四段的值
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractLcfw(ByVal segment As String) As String
|
||||
ExtractLcfw = Trim(segment)
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取附加功能
|
||||
'
|
||||
' 输入: segments数组,起始索引为4
|
||||
' 输出: Y3 或 N1,N2 或 N1.N2
|
||||
'
|
||||
' 规则:
|
||||
' - 从第五段开始,所有段合并为附加功能
|
||||
' - 用逗号或点号分隔的多个功能,保留原分隔符
|
||||
' - 示例: Y3 -> Y3
|
||||
' - 示例: N1,N2.Y3 -> N1,N2,Y3
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractFjgn(ByRef segments As Variant, ByVal startIndex As Long) As String
|
||||
Dim result As String
|
||||
result = ""
|
||||
|
||||
Dim i As Long
|
||||
For i = startIndex To UBound(segments)
|
||||
Dim segment As String
|
||||
segment = Trim(CStr(segments(i)))
|
||||
|
||||
If Len(segment) > 0 Then
|
||||
' 替换点号为逗号(统一分隔符)
|
||||
segment = Replace(segment, MODEL_SEPARATOR_DOT, ",")
|
||||
|
||||
If Len(result) > 0 Then
|
||||
result = result & "," & segment
|
||||
Else
|
||||
result = segment
|
||||
End If
|
||||
End If
|
||||
Next i
|
||||
|
||||
ExtractFjgn = result
|
||||
End Function
|
||||
@@ -1,464 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M07_BOMMatcher
|
||||
' 职责: BOM库匹配,根据提取的参数在BOM库中查找匹配的物料记录
|
||||
'
|
||||
' 匹配规则:
|
||||
' - 空单元格: 通配符,匹配所有值
|
||||
' - 单元格以"!="开头: 否定匹配,提取值不等于该值时匹配
|
||||
' - 普通值: 精确匹配
|
||||
' - fjgn字段: 包含匹配(InStr判断)
|
||||
'
|
||||
' 匹配逻辑: AND逻辑,所有条件列都必须满足
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 模块级变量 - 错误记录器
|
||||
Private g_Logger As clsErrorLogger
|
||||
|
||||
' BOM库工作表数据缓存(用于性能优化)
|
||||
Private g_BOMCache As Object
|
||||
Private g_CacheWorkbookName As String
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化BOM匹配器
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitBOMMatcher(logger As clsErrorLogger)
|
||||
Set g_Logger = logger
|
||||
Set g_BOMCache = CreateObject("Scripting.Dictionary")
|
||||
g_CacheWorkbookName = ""
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 在BOM库中匹配物料记录
|
||||
'
|
||||
' 输入:
|
||||
' ws - BOM库工作表(如"接头"、"弹性元件"等)
|
||||
' params - 从产品型号中提取的参数字典(包含azxs, bkxs, gclj, jycz, lcfw, fjgn等)
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 匹配结果
|
||||
' 键值对: "success"->Boolean, "rowCount"->Long, "rowNums"->Collection, "message"->String
|
||||
'
|
||||
' - success: 是否恰好匹配到1条记录
|
||||
' - rowCount: 匹配到的记录数量
|
||||
' - rowNums: 匹配到的行号集合
|
||||
' - message: 匹配结果描述(成功/失败原因)
|
||||
'
|
||||
' 示例:
|
||||
' Set result = MatchBOMRecord(wsJoint, params)
|
||||
' ' If result("success") Then
|
||||
' ' ' 使用匹配到的记录
|
||||
' ' Else
|
||||
' ' ' 记录错误到备注
|
||||
' ' End If
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function MatchBOMRecord(ByVal ws As Worksheet, ByVal params As Object) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim result As Object
|
||||
Set result = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 验证输入
|
||||
If ws Is Nothing Then
|
||||
result("success") = False
|
||||
result("rowCount") = 0
|
||||
result("rowNums") = New Collection
|
||||
result("message") = "工作表为空"
|
||||
Set MatchBOMRecord = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If params Is Nothing Or params.count = 0 Then
|
||||
result("success") = False
|
||||
result("rowCount") = 0
|
||||
result("rowNums") = New Collection
|
||||
result("message") = "参数字典为空"
|
||||
Set MatchBOMRecord = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 读取工作表数据到数组(性能优化)
|
||||
Dim bomData As Variant
|
||||
Dim headerRow As Variant
|
||||
Dim lastRow As Long
|
||||
Dim lastCol As Long
|
||||
|
||||
lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row
|
||||
lastCol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
|
||||
|
||||
' 如果没有数据行
|
||||
If lastRow < BOMLIB_START_ROW Then
|
||||
result("success") = False
|
||||
result("rowCount") = 0
|
||||
result("rowNums") = New Collection
|
||||
result("message") = "工作表无数据"
|
||||
Set MatchBOMRecord = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 读取数据到数组
|
||||
bomData = ws.Range(ws.Cells(BOMLIB_START_ROW, 1), ws.Cells(lastRow, lastCol)).Value
|
||||
headerRow = ws.Range(ws.Cells(1, 1), ws.Cells(1, lastCol)).Value
|
||||
|
||||
' 构建表头映射(列名 -> 列索引)
|
||||
Dim headerMap As Object
|
||||
Set headerMap = BuildHeaderMapping(headerRow, lastCol)
|
||||
|
||||
' 遍历所有行,查找匹配
|
||||
Dim matchingRows As Collection
|
||||
Set matchingRows = New Collection
|
||||
|
||||
Dim r As Long
|
||||
Dim RowIndex As Long
|
||||
|
||||
For r = LBound(bomData, 1) To UBound(bomData, 1)
|
||||
RowIndex = BOMLIB_START_ROW + (r - LBound(bomData, 1))
|
||||
|
||||
' 评估该行是否匹配
|
||||
If EvaluateConditionRow(bomData, r, headerMap, params) Then
|
||||
matchingRows.Add RowIndex
|
||||
End If
|
||||
Next r
|
||||
|
||||
' 构建结果
|
||||
result("rowCount") = matchingRows.count
|
||||
Set result("rowNums") = matchingRows
|
||||
|
||||
' 判断匹配结果
|
||||
If matchingRows.count = 0 Then
|
||||
result("success") = False
|
||||
result("message") = "未找到匹配记录"
|
||||
ElseIf matchingRows.count = 1 Then
|
||||
result("success") = True
|
||||
result("message") = "匹配成功"
|
||||
Else
|
||||
result("success") = False
|
||||
result("message") = "匹配到" & matchingRows.count & "条记录(需要恰好1条)"
|
||||
End If
|
||||
|
||||
Set MatchBOMRecord = result
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M07.MatchBOMRecord", "SystemError", _
|
||||
"匹配过程发生错误: " & err.Description, ws.Name
|
||||
End If
|
||||
|
||||
result("success") = False
|
||||
result("rowCount") = 0
|
||||
Set result("rowNums") = New Collection
|
||||
result("message") = "系统错误: " & err.Description
|
||||
Set MatchBOMRecord = result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 评估单行数据是否匹配参数
|
||||
'
|
||||
' 输入:
|
||||
' bomData - BOM库数据数组
|
||||
' rowIdx - 数组行索引
|
||||
' headerMap - 表头映射(列名 -> 列索引)
|
||||
' params - 提取的参数字典
|
||||
'
|
||||
' 输出:
|
||||
' Boolean - True表示该行匹配,False表示不匹配
|
||||
'
|
||||
' 逻辑:
|
||||
' - 对于参数字典中的每个键,在工作表中查找对应列
|
||||
' - 评估该列的单元格条件是否满足
|
||||
' - 所有条件都满足时返回True(AND逻辑)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function EvaluateConditionRow( _
|
||||
ByRef bomData As Variant, _
|
||||
ByVal rowIdx As Long, _
|
||||
ByVal headerMap As Object, _
|
||||
ByVal params As Object _
|
||||
) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim paramKey As Variant
|
||||
|
||||
' 遍历所有参数
|
||||
For Each paramKey In params.keys
|
||||
Dim paramValue As String
|
||||
paramValue = CStr(params(paramKey))
|
||||
|
||||
' 检查BOM库中是否有该列
|
||||
If headerMap.Exists(CStr(paramKey)) Then
|
||||
Dim colIdx As Long
|
||||
colIdx = headerMap(CStr(paramKey))
|
||||
|
||||
' 获取单元格值
|
||||
Dim cellValue As Variant
|
||||
cellValue = bomData(rowIdx, colIdx)
|
||||
|
||||
' 评估单元格条件
|
||||
If Not EvaluateCellCondition(cellValue, paramValue, CStr(paramKey)) Then
|
||||
' 只要有一个条件不满足,该行就不匹配
|
||||
EvaluateConditionRow = False
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Next paramKey
|
||||
|
||||
' 所有条件都满足
|
||||
EvaluateConditionRow = True
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
EvaluateConditionRow = False
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 评估单个单元格条件是否满足
|
||||
'
|
||||
' 输入:
|
||||
' cellValue - BOM库单元格的值
|
||||
' paramValue - 从产品型号中提取的参数值
|
||||
' fieldName - 字段名称(用于特殊处理)
|
||||
'
|
||||
' 输出:
|
||||
' Boolean - True表示条件满足,False表示不满足
|
||||
'
|
||||
' 匹配规则:
|
||||
' 1. 空单元格或IsEmpty: 通配符,匹配所有值(返回True)
|
||||
' 2. 单元格以"!="开头: 否定匹配,paramValue不等于该值时返回True
|
||||
' 3. fjgn字段: 包含匹配,paramValue包含cellValue时返回True
|
||||
' 4. 普通值: 精确匹配,paramValue等于cellValue时返回True
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function EvaluateCellCondition( _
|
||||
ByVal cellValue As Variant, _
|
||||
ByVal paramValue As String, _
|
||||
ByVal fieldName As String _
|
||||
) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' 处理空单元格(通配符)
|
||||
If IsEmpty(cellValue) Or Len(Trim(CStr(cellValue))) = 0 Then
|
||||
EvaluateCellCondition = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim cellStr As String
|
||||
cellStr = Trim(CStr(cellValue))
|
||||
|
||||
' 处理否定条件 (!=开头)
|
||||
If Left(cellStr, 2) = "!=" Then
|
||||
Dim notValue As String
|
||||
notValue = Trim(Mid(cellStr, 3))
|
||||
EvaluateCellCondition = (paramValue <> notValue)
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 处理fjgn字段(包含匹配)
|
||||
If LCase(fieldName) = "fjgn" Then
|
||||
EvaluateCellCondition = CheckFjgnMatch(cellStr, paramValue)
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 支持双值匹配(如 "A0,径向" 可以匹配 "A0" 或 "径向")
|
||||
' 检查参数值是否包含逗号(表示有映射值)
|
||||
If InStr(paramValue, ",") > 0 Then
|
||||
Dim paramValues As Variant
|
||||
paramValues = Split(paramValue, ",")
|
||||
|
||||
' 只要参数中的任意一个值匹配单元格值,即认为匹配
|
||||
Dim i As Long
|
||||
For i = LBound(paramValues) To UBound(paramValues)
|
||||
If Trim(CStr(paramValues(i))) = cellStr Then
|
||||
EvaluateCellCondition = True
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
' 所有值都不匹配
|
||||
EvaluateCellCondition = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 单值精确匹配
|
||||
EvaluateCellCondition = (paramValue = cellStr)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
EvaluateCellCondition = False
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 检查附加功能(fjgn)是否匹配
|
||||
'
|
||||
' 输入:
|
||||
' cellValue - BOM库中的fjgn值(如: "N1" 或 "N3")
|
||||
' fjgnList - 从产品型号中提取的fjgn列表(如: "N1,N2" 或 "Y3")
|
||||
'
|
||||
' 输出:
|
||||
' Boolean - True表示fjgnList中包含cellValue
|
||||
'
|
||||
' 逻辑:
|
||||
' - 使用InStr判断fjgnList中是否包含cellValue
|
||||
' - 支持逗号分隔的多个功能
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function CheckFjgnMatch(ByVal cellValue As String, ByVal fjgnList As String) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
cellValue = Trim(cellValue)
|
||||
fjgnList = Trim(fjgnList)
|
||||
|
||||
' 如果fjgn列表为空,不匹配
|
||||
If Len(fjgnList) = 0 Then
|
||||
CheckFjgnMatch = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 检查fjgnList中是否包含cellValue
|
||||
' 使用InStr进行包含匹配
|
||||
CheckFjgnMatch = (InStr(fjgnList, cellValue) > 0)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
CheckFjgnMatch = False
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 构建表头映射(列名 -> 列索引)
|
||||
'
|
||||
' 输入:
|
||||
' headerRow - 表头行数据数组(二维)
|
||||
' lastCol - 最后一列的索引
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 表头映射字典
|
||||
' 键: 列名(小写),值: 列索引(从1开始)
|
||||
'
|
||||
' 示例:
|
||||
' headerRow = Array("azxs", "bkxs", "gclj", "物料名称", "物料编码")
|
||||
' 返回: {"azxs":1, "bkxs":2, "gclj":3, "物料名称":4, "物料编码":5}
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function BuildHeaderMapping(ByRef headerRow As Variant, ByVal lastCol As Long) As Object
|
||||
Dim headerMap As Object
|
||||
Set headerMap = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim c As Long
|
||||
For c = 1 To lastCol
|
||||
Dim colName As String
|
||||
colName = Trim(CStr(headerRow(1, c)))
|
||||
|
||||
If Len(colName) > 0 Then
|
||||
' 使用小写作为键,避免大小写问题
|
||||
Dim colKey As String
|
||||
colKey = LCase(colName)
|
||||
|
||||
If Not headerMap.Exists(colKey) Then
|
||||
headerMap.Add colKey, c
|
||||
End If
|
||||
End If
|
||||
Next c
|
||||
|
||||
Set BuildHeaderMapping = headerMap
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 从匹配行中提取物料信息
|
||||
'
|
||||
' 输入:
|
||||
' ws - BOM库工作表
|
||||
' rowNum - 匹配到的行号
|
||||
' headerMap - 表头映射
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 物料信息
|
||||
' 键值对: "materialName"->物料名称, "materialCode"->物料编码,
|
||||
' "materialQty"->物料数量, "materialType"->物料类型(工作表名)
|
||||
'
|
||||
' 逻辑:
|
||||
' - 通过列名查找物料信息列(不依赖列位置)
|
||||
' - 支持"名称"、"编码"、"数量"列
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function ExtractMaterialInfo( _
|
||||
ByVal ws As Worksheet, _
|
||||
ByVal rowNum As Long, _
|
||||
ByVal headerMap As Object _
|
||||
) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim materialInfo As Object
|
||||
Set materialInfo = CreateObject("Scripting.Dictionary")
|
||||
materialInfo("materialType") = ws.Name
|
||||
|
||||
' 通过列名查找物料信息列
|
||||
Dim nameKey As String, codeKey As String, qtyKey As String
|
||||
nameKey = LCase(BOMLIB_COL_NAME)
|
||||
codeKey = LCase(BOMLIB_COL_CODE)
|
||||
qtyKey = LCase(BOMLIB_COL_QTY)
|
||||
|
||||
' 提取物料名称
|
||||
If headerMap.Exists(nameKey) Then
|
||||
Dim nameCol As Long
|
||||
nameCol = headerMap(nameKey)
|
||||
materialInfo("materialName") = Trim(CStr(ws.Cells(rowNum, nameCol).Value))
|
||||
Else
|
||||
materialInfo("materialName") = ""
|
||||
End If
|
||||
|
||||
' 提取物料编码
|
||||
If headerMap.Exists(codeKey) Then
|
||||
Dim codeCol As Long
|
||||
codeCol = headerMap(codeKey)
|
||||
materialInfo("materialCode") = Trim(CStr(ws.Cells(rowNum, codeCol).Value))
|
||||
Else
|
||||
materialInfo("materialCode") = ""
|
||||
End If
|
||||
|
||||
' 提取物料数量
|
||||
If headerMap.Exists(qtyKey) Then
|
||||
Dim qtyCol As Long
|
||||
qtyCol = headerMap(qtyKey)
|
||||
Dim qtyValue As Variant
|
||||
qtyValue = ws.Cells(rowNum, qtyCol).Value
|
||||
If IsNumeric(qtyValue) Then
|
||||
materialInfo("materialQty") = CLng(qtyValue)
|
||||
Else
|
||||
materialInfo("materialQty") = 1
|
||||
End If
|
||||
Else
|
||||
materialInfo("materialQty") = 1
|
||||
End If
|
||||
|
||||
Set ExtractMaterialInfo = materialInfo
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M07.ExtractMaterialInfo", "SystemError", _
|
||||
"提取物料信息失败: " & err.Description, ws.Name
|
||||
End If
|
||||
Set ExtractMaterialInfo = CreateObject("Scripting.Dictionary")
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 构建工作表的表头映射
|
||||
'
|
||||
' 输入:
|
||||
' ws - BOM库工作表
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 表头映射字典
|
||||
'
|
||||
' 说明:
|
||||
' - 公开函数,用于外部构建表头映射
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function BuildWorksheetHeaderMap(ByVal ws As Worksheet) As Object
|
||||
If ws Is Nothing Then
|
||||
Set BuildWorksheetHeaderMap = CreateObject("Scripting.Dictionary")
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim lastCol As Long
|
||||
lastCol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
|
||||
|
||||
Dim headerRow As Variant
|
||||
headerRow = ws.Range(ws.Cells(1, 1), ws.Cells(1, lastCol)).Value
|
||||
|
||||
Set BuildWorksheetHeaderMap = BuildHeaderMapping(headerRow, lastCol)
|
||||
End Function
|
||||
@@ -1,769 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M08_ComponentProcessor
|
||||
' 职责: 处理"部件"物料的特殊逻辑
|
||||
'
|
||||
' 部件物料特性:
|
||||
' - 每条"部件"记录包含三个物料的数据:
|
||||
' 1. 部件物料本身
|
||||
' 2. 接头物料(子件1)
|
||||
' 3. 弹性元件物料(子件2)
|
||||
'
|
||||
' 选择策略:
|
||||
' - 优先选择"部件"物料
|
||||
' - 当"部件"物料库存不足时,选择"接头"+"弹性元件"
|
||||
' - 库存检查接口预留,当前默认返回True(库存充足)
|
||||
'
|
||||
' 验证规则:
|
||||
' - 正常组合1: 1个部件
|
||||
' - 正常组合2: 1个接头 + 1个弹性元件
|
||||
' - 异常: 其他组合(如只有接头、只有弹性元件、同时有部件和接头等)
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
' 模块级变量 - 错误记录器
|
||||
Private g_Logger As clsErrorLogger
|
||||
|
||||
' 模块级变量 - 库存追踪
|
||||
Private g_InventoryDict As Object ' 部件编码 -> 现存量
|
||||
Private g_AccumulatedDemandDict As Object ' 部件编码 -> 累计需求量
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化部件处理器
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitComponentProcessor(logger As clsErrorLogger)
|
||||
Set g_Logger = logger
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 初始化部件处理器(带库存校验)
|
||||
'
|
||||
' 输入:
|
||||
' logger - 错误记录器
|
||||
' inventoryWb - 包含现存量工作表的工作簿(通常是ThisWorkbook)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub InitComponentProcessorWithInventory( _
|
||||
logger As clsErrorLogger, _
|
||||
inventoryWb As Workbook _
|
||||
)
|
||||
Set g_Logger = logger
|
||||
Call LoadInventoryData(inventoryWb)
|
||||
Set g_AccumulatedDemandDict = CreateObject("Scripting.Dictionary")
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 加载库存数据
|
||||
'
|
||||
' 输入:
|
||||
' inventoryWb - 包含现存量工作表的工作簿
|
||||
'
|
||||
' 逻辑:
|
||||
' 1. 查找[现存量]工作表
|
||||
' 2. 如果未找到,记录警告并使用空字典(所有库存视为0)
|
||||
' 3. 从第4行开始读取数据(表头在第3行)
|
||||
' 4. B列 = 物料编码,J列 = 库存数量
|
||||
' 5. 存储到 g_InventoryDict 中
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub LoadInventoryData(ByVal inventoryWb As Workbook)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Set g_InventoryDict = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 检查现存量工作表是否存在
|
||||
Dim wsInventory As Worksheet
|
||||
On Error Resume Next
|
||||
Set wsInventory = inventoryWb.Sheets(INVENTORY_SHEET_NAME)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
If wsInventory Is Nothing Then
|
||||
' 未找到现存量工作表,记录警告并使用空字典(所有库存视为0)
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.RecordWarning "", "M08.LoadInventoryData", "InventorySheetMissing", _
|
||||
"未找到[" & INVENTORY_SHEET_NAME & "]工作表,所有部件库存将视为0", ""
|
||||
End If
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 查找最后一行
|
||||
Dim lastRow As Long
|
||||
lastRow = wsInventory.Cells(wsInventory.Rows.count, 2).End(xlUp).row ' B列
|
||||
|
||||
If lastRow < INVENTORY_HEADER_ROW + 1 Then
|
||||
Exit Sub ' 没有数据
|
||||
End If
|
||||
|
||||
' 从第4行开始读取数据(表头在第3行)
|
||||
Dim i As Long
|
||||
For i = INVENTORY_HEADER_ROW + 1 To lastRow
|
||||
Dim materialCode As String
|
||||
Dim stockQty As Variant
|
||||
|
||||
materialCode = Trim(CStr(wsInventory.Cells(i, INVENTORY_COL_CODE).Value))
|
||||
stockQty = wsInventory.Cells(i, INVENTORY_COL_QTY).Value
|
||||
|
||||
If Len(materialCode) > 0 And IsNumeric(stockQty) Then
|
||||
g_InventoryDict(materialCode) = CLng(stockQty)
|
||||
End If
|
||||
Next i
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.RecordWarning "", "M08.LoadInventoryData", "LoadError", _
|
||||
"加载库存数据失败: " & err.Description, ""
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 处理"部件"记录,返回物料集合
|
||||
'
|
||||
' 输入:
|
||||
' wsComponent - "部件"工作表
|
||||
' params - 从产品型号中提取的参数字典
|
||||
' logger - 错误记录器
|
||||
' matchedRowNum - 匹配到的行号(由调用者传入,避免重复匹配)
|
||||
'
|
||||
' 输出:
|
||||
' Collection - 物料集合
|
||||
' 每个元素是一个字典,包含: materialName, materialCode, materialQty, materialType, remarks
|
||||
'
|
||||
' 逻辑流程:
|
||||
' 1. 使用传入的matchedRowNum定位匹配记录
|
||||
' 2. 检查"部件"物料库存
|
||||
' 3. 如果有库存,返回部件物料
|
||||
' 4. 如果无库存,提取子件(接头+弹性元件)
|
||||
'
|
||||
' 示例:
|
||||
' Set materials = ProcessComponentRecord(wsComponent, params, logger, 5)
|
||||
' ' materials(1) - 部件物料 或 接头物料
|
||||
' ' materials(2) - 弹性元件物料(如果选择子件)
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function ProcessComponentRecord( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal params As Object, _
|
||||
ByVal logger As clsErrorLogger, _
|
||||
ByVal matchedRowNum As Long, _
|
||||
ByVal orderQty As Long, _
|
||||
ByVal productionOrderNo As String _
|
||||
) As Collection
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim materials As Collection
|
||||
Set materials = New Collection
|
||||
|
||||
' 步骤1: 验证传入的行号
|
||||
If matchedRowNum <= 0 Then
|
||||
' 无效行号,返回错误物料
|
||||
Dim errorMaterial As Object
|
||||
Set errorMaterial = CreateObject("Scripting.Dictionary")
|
||||
errorMaterial("materialType") = "部件"
|
||||
errorMaterial("materialName") = ""
|
||||
errorMaterial("materialCode") = ""
|
||||
errorMaterial("materialQty") = 0
|
||||
errorMaterial("remarks") = "无效的匹配行号"
|
||||
materials.Add errorMaterial
|
||||
|
||||
Set ProcessComponentRecord = materials
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤2: 使用传入的行号
|
||||
|
||||
' 步骤3: 构建表头映射
|
||||
Dim headerMap As Object
|
||||
Set headerMap = M07_BOMMatcher.BuildWorksheetHeaderMap(wsComponent)
|
||||
|
||||
' 步骤4: 检查部件库存
|
||||
Dim componentInfo As Object
|
||||
If CheckComponentInventory(wsComponent, matchedRowNum, headerMap, orderQty, productionOrderNo) Then
|
||||
' 库存充足,返回部件物料
|
||||
Set componentInfo = ExtractComponentInfo(wsComponent, matchedRowNum, headerMap, "部件")
|
||||
componentInfo("remarks") = ""
|
||||
componentInfo("isStockSufficient") = True
|
||||
|
||||
If Not componentInfo Is Nothing Then
|
||||
materials.Add componentInfo
|
||||
End If
|
||||
Else
|
||||
' 【关键修复】库存不足时,同时返回部件(标记)和子件
|
||||
' 步骤1: 返回部件信息(用于库存比对表)
|
||||
Set componentInfo = ExtractComponentInfo(wsComponent, matchedRowNum, headerMap, "部件")
|
||||
componentInfo("remarks") = "部件无库存,使用子件"
|
||||
componentInfo("isStockSufficient") = False
|
||||
|
||||
If Not componentInfo Is Nothing Then
|
||||
materials.Add componentInfo
|
||||
End If
|
||||
|
||||
' 步骤2: 返回子件信息(用于BOM提取结果和BIP上传)
|
||||
Dim subComponents As Collection
|
||||
Set subComponents = ExtractSubComponents(wsComponent, matchedRowNum, headerMap)
|
||||
|
||||
Dim subComp As Variant
|
||||
For Each subComp In subComponents
|
||||
materials.Add subComp
|
||||
Next subComp
|
||||
End If
|
||||
|
||||
Set ProcessComponentRecord = materials
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not logger Is Nothing Then
|
||||
logger.Record productionOrderNo, "M08.ProcessComponentRecord", "SystemError", _
|
||||
"处理部件记录失败: " & err.Description, ""
|
||||
End If
|
||||
|
||||
' 返回错误物料
|
||||
Dim errorMat As Object
|
||||
Set errorMat = CreateObject("Scripting.Dictionary")
|
||||
errorMat("materialType") = "部件"
|
||||
errorMat("materialName") = ""
|
||||
errorMat("materialCode") = ""
|
||||
errorMat("materialQty") = 0
|
||||
errorMat("remarks") = "系统错误: " & err.Description
|
||||
|
||||
Dim errorCol As New Collection
|
||||
errorCol.Add errorMat
|
||||
Set ProcessComponentRecord = errorCol
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 检查部件库存状态(集成库存校验)
|
||||
'
|
||||
' 输入:
|
||||
' wsComponent - "部件"工作表
|
||||
' rowNum - 匹配到的行号
|
||||
' headerMap - 表头映射
|
||||
' orderQty - 订单数量
|
||||
'
|
||||
' 输出:
|
||||
' Boolean - True表示库存充足,False表示库存不足
|
||||
'
|
||||
' 逻辑:
|
||||
' 1. 提取部件编码
|
||||
' 2. 获取BOM需求量(部件工作表中的数量列)
|
||||
' 3. 检查库存数据是否存在
|
||||
' 4. 计算累计需求量 = 订单数量 × BOM需求量 + 之前累计需求
|
||||
' 5. 比较库存和需求,更新累计需求量
|
||||
'
|
||||
' 示例:
|
||||
' 订单1、订单3、订单6都用部件A,每个订单数量=2,BOM需求量=1,现存量=5
|
||||
' - 订单1:累计需求 = 2×1 + 0 = 2,5 >= 2 ?? true(使用部件)
|
||||
' - 订单3:累计需求 = 2×1 + 2 = 4,5 >= 4 ?? true(使用部件)
|
||||
' - 订单6:累计需求 = 2×1 + 4 = 6,5 < 6 ?? false(使用子件)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function CheckComponentInventory( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal rowNum As Long, _
|
||||
ByVal headerMap As Object, _
|
||||
ByVal orderQty As Long, _
|
||||
ByVal productionOrderNo As String _
|
||||
) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' 步骤1: 提取部件编码
|
||||
Dim codeCol As Long
|
||||
Dim codeKey As String
|
||||
codeKey = LCase(BOMLIB_COL_CODE)
|
||||
|
||||
If Not headerMap.Exists(codeKey) Then
|
||||
' 没有编码列,默认返回True(库存充足)
|
||||
CheckComponentInventory = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
codeCol = headerMap(codeKey)
|
||||
Dim componentCode As String
|
||||
componentCode = Trim(CStr(wsComponent.Cells(rowNum, codeCol).Value))
|
||||
|
||||
If Len(componentCode) = 0 Then
|
||||
' 没有编码,默认返回True
|
||||
CheckComponentInventory = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤2: 获取BOM需求量(部件工作表中的数量列)
|
||||
Dim bomQty As Long
|
||||
bomQty = 1 ' 默认为1
|
||||
|
||||
Dim qtyKey As String
|
||||
qtyKey = LCase(BOMLIB_COL_QTY)
|
||||
|
||||
If headerMap.Exists(qtyKey) Then
|
||||
Dim qtyCol As Long
|
||||
Dim qtyValue As Variant
|
||||
qtyCol = headerMap(qtyKey)
|
||||
qtyValue = wsComponent.Cells(rowNum, qtyCol).Value
|
||||
|
||||
If IsNumeric(qtyValue) Then
|
||||
bomQty = CLng(qtyValue)
|
||||
End If
|
||||
End If
|
||||
|
||||
' 步骤3: 检查库存数据是否存在
|
||||
If g_InventoryDict Is Nothing Or Not g_InventoryDict.Exists(componentCode) Then
|
||||
' 未找到库存数据,记录警告并返回False(库存不足)
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.RecordWarning productionOrderNo, "M08.CheckComponentInventory", "InventoryNotFound", _
|
||||
"部件[" & componentCode & "]未找到库存数据,视为库存不足", ""
|
||||
End If
|
||||
CheckComponentInventory = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 步骤4: 计算累计需求量
|
||||
Dim currentDemand As Long
|
||||
currentDemand = orderQty * bomQty
|
||||
|
||||
Dim accumulatedDemand As Long
|
||||
If g_AccumulatedDemandDict.Exists(componentCode) Then
|
||||
accumulatedDemand = g_AccumulatedDemandDict(componentCode)
|
||||
End If
|
||||
|
||||
Dim totalDemand As Long
|
||||
totalDemand = accumulatedDemand + currentDemand
|
||||
|
||||
' 步骤5: 比较库存和需求
|
||||
Dim stockQty As Long
|
||||
stockQty = g_InventoryDict(componentCode)
|
||||
|
||||
If stockQty >= totalDemand Then
|
||||
' 库存充足,更新累计需求量
|
||||
g_AccumulatedDemandDict(componentCode) = totalDemand
|
||||
CheckComponentInventory = True
|
||||
Else
|
||||
' 库存不足,记录警告(不是错误)
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.RecordWarning productionOrderNo, "M08.CheckComponentInventory", "InsufficientInventory", _
|
||||
"部件[" & componentCode & "]库存不足。库存=" & stockQty & ", 累计需求=" & totalDemand, ""
|
||||
End If
|
||||
CheckComponentInventory = False
|
||||
End If
|
||||
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record productionOrderNo, "M08.CheckComponentInventory", "SystemError", _
|
||||
"库存检查失败: " & err.Description, ""
|
||||
End If
|
||||
' 出错时返回False(库存不足)
|
||||
CheckComponentInventory = False
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取部件物料信息
|
||||
'
|
||||
' 输入:
|
||||
' wsComponent - "部件"工作表
|
||||
' rowNum - 匹配到的行号
|
||||
' headerMap - 表头映射
|
||||
' componentType - 部件类型("部件")
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 部件物料信息
|
||||
' 键值对: materialName, materialCode, materialQty, materialType, remarks
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractComponentInfo( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal rowNum As Long, _
|
||||
ByVal headerMap As Object, _
|
||||
ByVal componentType As String _
|
||||
) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim componentInfo As Object
|
||||
Set componentInfo = CreateObject("Scripting.Dictionary")
|
||||
componentInfo("materialType") = componentType
|
||||
|
||||
' 通过列名常量查找物料信息列
|
||||
Dim nameKey As String, codeKey As String, qtyKey As String
|
||||
nameKey = LCase(BOMLIB_COL_NAME)
|
||||
codeKey = LCase(BOMLIB_COL_CODE)
|
||||
qtyKey = LCase(BOMLIB_COL_QTY)
|
||||
|
||||
' 查找物料名称列
|
||||
If headerMap.Exists(nameKey) Then
|
||||
Dim nameCol As Long
|
||||
nameCol = headerMap(nameKey)
|
||||
componentInfo("materialName") = Trim(CStr(wsComponent.Cells(rowNum, nameCol).Value))
|
||||
Else
|
||||
componentInfo("materialName") = ""
|
||||
End If
|
||||
|
||||
' 查找物料编码列
|
||||
If headerMap.Exists(codeKey) Then
|
||||
Dim codeCol As Long
|
||||
codeCol = headerMap(codeKey)
|
||||
componentInfo("materialCode") = Trim(CStr(wsComponent.Cells(rowNum, codeCol).Value))
|
||||
Else
|
||||
componentInfo("materialCode") = ""
|
||||
End If
|
||||
|
||||
' 查找物料数量列
|
||||
If headerMap.Exists(qtyKey) Then
|
||||
Dim qtyCol As Long
|
||||
qtyCol = headerMap(qtyKey)
|
||||
Dim qtyValue As Variant
|
||||
qtyValue = wsComponent.Cells(rowNum, qtyCol).Value
|
||||
If IsNumeric(qtyValue) Then
|
||||
componentInfo("materialQty") = CLng(qtyValue)
|
||||
Else
|
||||
componentInfo("materialQty") = 1
|
||||
End If
|
||||
Else
|
||||
componentInfo("materialQty") = 1
|
||||
End If
|
||||
|
||||
componentInfo("remarks") = ""
|
||||
componentInfo("isStockSufficient") = True ' 默认值,会在 ProcessComponentRecord 中被覆盖
|
||||
|
||||
Set ExtractComponentInfo = componentInfo
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M08.ExtractComponentInfo", "SystemError", _
|
||||
"提取部件信息失败: " & err.Description, componentType
|
||||
End If
|
||||
Set ExtractComponentInfo = Nothing
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取子部件信息(接头+弹性元件)
|
||||
'
|
||||
' 输入:
|
||||
' wsComponent - "部件"工作表
|
||||
' rowNum - 匹配到的行号
|
||||
' headerMap - 表头映射
|
||||
'
|
||||
' 输出:
|
||||
' Collection - 子部件集合
|
||||
' 包含2个元素: 接头物料、弹性元件物料
|
||||
'
|
||||
' 注意:
|
||||
' - "部件"工作表中,子件信息存储在特定列中
|
||||
' - 需要根据实际的BOM库结构调整列名
|
||||
' - 默认查找"接头_物料名称"、"接头_物料编码"、"接头_物料数量"等列
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractSubComponents( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal rowNum As Long, _
|
||||
ByVal headerMap As Object _
|
||||
) As Collection
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim subComponents As Collection
|
||||
Set subComponents = New Collection
|
||||
|
||||
' 提取接头信息
|
||||
Dim jointInfo As Object
|
||||
Set jointInfo = ExtractSingleSubComponent(wsComponent, rowNum, headerMap, "接头")
|
||||
|
||||
If Not jointInfo Is Nothing Then
|
||||
subComponents.Add jointInfo
|
||||
End If
|
||||
|
||||
' 提取弹性元件信息
|
||||
Dim elementInfo As Object
|
||||
Set elementInfo = ExtractSingleSubComponent(wsComponent, rowNum, headerMap, "弹性元件")
|
||||
|
||||
If Not elementInfo Is Nothing Then
|
||||
subComponents.Add elementInfo
|
||||
End If
|
||||
|
||||
Set ExtractSubComponents = subComponents
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M08.ExtractSubComponents", "SystemError", _
|
||||
"提取子件信息失败: " & err.Description, ""
|
||||
End If
|
||||
Set ExtractSubComponents = New Collection
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 提取单个子部件信息
|
||||
'
|
||||
' 输入:
|
||||
' wsComponent - "部件"工作表
|
||||
' rowNum - 匹配到的行号
|
||||
' headerMap - 表头映射
|
||||
' subComponentType - 子件类型("接头" 或 "弹性元件")
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 子件物料信息
|
||||
'
|
||||
' 逻辑:
|
||||
' - 通过列名查找子部件信息(不依赖列位置)
|
||||
' - 接头: 查找"接头名称"、"接头编码"、"接头数量"
|
||||
' - 弹性元件: 查找"弹性元件名称"、"弹性元件编码"、"弹性元件数量"
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Function ExtractSingleSubComponent( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal rowNum As Long, _
|
||||
ByVal headerMap As Object, _
|
||||
ByVal subComponentType As String _
|
||||
) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim subInfo As Object
|
||||
Set subInfo = CreateObject("Scripting.Dictionary")
|
||||
subInfo("materialType") = subComponentType
|
||||
|
||||
' 根据子件类型确定列名
|
||||
Dim nameKey As String, codeKey As String, qtyKey As String
|
||||
|
||||
If subComponentType = "接头" Then
|
||||
nameKey = LCase(BOMLIB_COL_JOINT_NAME)
|
||||
codeKey = LCase(BOMLIB_COL_JOINT_CODE)
|
||||
qtyKey = LCase(BOMLIB_COL_JOINT_QTY)
|
||||
ElseIf subComponentType = "弹性元件" Then
|
||||
nameKey = LCase(BOMLIB_COL_ELEMENT_NAME)
|
||||
codeKey = LCase(BOMLIB_COL_ELEMENT_CODE)
|
||||
qtyKey = LCase(BOMLIB_COL_ELEMENT_QTY)
|
||||
Else
|
||||
' 未知类型,返回空信息
|
||||
subInfo("materialName") = ""
|
||||
subInfo("materialCode") = ""
|
||||
subInfo("materialQty") = 0
|
||||
subInfo("remarks") = "未知子件类型: " & subComponentType
|
||||
Set ExtractSingleSubComponent = subInfo
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 提取物料名称
|
||||
If headerMap.Exists(nameKey) Then
|
||||
Dim nameCol As Long
|
||||
nameCol = headerMap(nameKey)
|
||||
subInfo("materialName") = Trim(CStr(wsComponent.Cells(rowNum, nameCol).Value))
|
||||
Else
|
||||
subInfo("materialName") = ""
|
||||
End If
|
||||
|
||||
' 提取物料编码
|
||||
If headerMap.Exists(codeKey) Then
|
||||
Dim codeCol As Long
|
||||
codeCol = headerMap(codeKey)
|
||||
subInfo("materialCode") = Trim(CStr(wsComponent.Cells(rowNum, codeCol).Value))
|
||||
Else
|
||||
subInfo("materialCode") = ""
|
||||
End If
|
||||
|
||||
' 提取物料数量
|
||||
If headerMap.Exists(qtyKey) Then
|
||||
Dim qtyCol As Long
|
||||
qtyCol = headerMap(qtyKey)
|
||||
Dim qtyValue As Variant
|
||||
qtyValue = wsComponent.Cells(rowNum, qtyCol).Value
|
||||
If IsNumeric(qtyValue) Then
|
||||
subInfo("materialQty") = CLng(qtyValue)
|
||||
Else
|
||||
subInfo("materialQty") = 1
|
||||
End If
|
||||
Else
|
||||
subInfo("materialQty") = 1
|
||||
End If
|
||||
|
||||
subInfo("remarks") = "部件无库存,使用子件"
|
||||
|
||||
Set ExtractSingleSubComponent = subInfo
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
If Not g_Logger Is Nothing Then
|
||||
g_Logger.Record "", "M08.ExtractSingleSubComponent", "SystemError", _
|
||||
"提取子件[" & subComponentType & "]失败: " & err.Description, ""
|
||||
End If
|
||||
Set ExtractSingleSubComponent = Nothing
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 获取部件库存信息(用于库存比对工作表)
|
||||
'
|
||||
' 输入:
|
||||
' componentCode - 部件编码
|
||||
' bomQty - BOM需求量(单个产品)
|
||||
' orderQty - 订单数量
|
||||
'
|
||||
' 输出:
|
||||
' Object - 库存信息字典
|
||||
' .stockQty - 库存数量
|
||||
' .requiredQty - 所需数量 (orderQty × bomQty)
|
||||
' .accumulatedDemand - 累计需求量
|
||||
' .totalDemand - 总需求量 (累计需求 + 当前需求)
|
||||
' .isSufficient - 库存是否充足 (True/False)
|
||||
'
|
||||
' 注意:
|
||||
' - 此函数是只读的,不会修改 g_AccumulatedDemandDict
|
||||
' - 库存充足性判断基于累计需求量,但仅返回结果,不更新状态
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function GetComponentInventoryInfo( _
|
||||
ByVal componentCode As String, _
|
||||
ByVal bomQty As Long, _
|
||||
ByVal orderQty As Long _
|
||||
) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim invInfo As Object
|
||||
Set invInfo = CreateObject("Scripting.Dictionary")
|
||||
|
||||
' 步骤1: 获取库存数量
|
||||
Dim stockQty As Long
|
||||
stockQty = 0 ' 默认值为0
|
||||
|
||||
If Not g_InventoryDict Is Nothing And g_InventoryDict.Exists(componentCode) Then
|
||||
stockQty = g_InventoryDict(componentCode)
|
||||
End If
|
||||
|
||||
' 步骤2: 计算需求量
|
||||
Dim requiredQty As Long
|
||||
requiredQty = orderQty * bomQty
|
||||
|
||||
Dim accumulatedDemand As Long
|
||||
accumulatedDemand = 0
|
||||
|
||||
If Not g_AccumulatedDemandDict Is Nothing Then
|
||||
If g_AccumulatedDemandDict.Exists(componentCode) Then
|
||||
accumulatedDemand = g_AccumulatedDemandDict(componentCode)
|
||||
End If
|
||||
End If
|
||||
|
||||
Dim totalDemand As Long
|
||||
totalDemand = accumulatedDemand + requiredQty
|
||||
|
||||
' 步骤3: 判断库存是否充足
|
||||
Dim isSufficient As Boolean
|
||||
isSufficient = (stockQty >= totalDemand)
|
||||
|
||||
' 步骤4: 返回库存信息
|
||||
invInfo("stockQty") = stockQty
|
||||
invInfo("requiredQty") = requiredQty
|
||||
invInfo("accumulatedDemand") = accumulatedDemand
|
||||
invInfo("totalDemand") = totalDemand
|
||||
invInfo("isSufficient") = isSufficient
|
||||
|
||||
Set GetComponentInventoryInfo = invInfo
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
' 出错时返回默认值(库存不足)
|
||||
Dim errorInfo As Object
|
||||
Set errorInfo = CreateObject("Scripting.Dictionary")
|
||||
errorInfo("stockQty") = 0
|
||||
errorInfo("requiredQty") = orderQty * bomQty
|
||||
errorInfo("accumulatedDemand") = 0
|
||||
errorInfo("totalDemand") = orderQty * bomQty
|
||||
errorInfo("isSufficient") = False
|
||||
|
||||
Set GetComponentInventoryInfo = errorInfo
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 验证部件组合是否有效
|
||||
'
|
||||
' 输入:
|
||||
' materials - 物料集合(包含所有类型的物料)
|
||||
'
|
||||
' 输出:
|
||||
' Object (Scripting.Dictionary) - 验证结果
|
||||
' 键值对: "valid"->Boolean, "message"->String
|
||||
'
|
||||
' 验证规则:
|
||||
' - 正确组合1: 1个部件
|
||||
' - 正确组合2: 1个接头 + 1个弹性元件
|
||||
' - 异常: 其他组合
|
||||
'
|
||||
' 示例:
|
||||
' Set validation = ValidateComponentCombination(materials)
|
||||
' ' If Not validation("valid") Then
|
||||
' ' ' 记录验证错误
|
||||
' ' End If
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Function ValidateComponentCombination(ByVal materials As Collection) As Object
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim result As Object
|
||||
Set result = CreateObject("Scripting.Dictionary")
|
||||
|
||||
If materials Is Nothing Or materials.count = 0 Then
|
||||
result("valid") = False
|
||||
result("message") = "物料列表为空"
|
||||
Set ValidateComponentCombination = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 统计各类型物料数量
|
||||
Dim componentCount As Long
|
||||
Dim jointCount As Long
|
||||
Dim elementCount As Long
|
||||
Dim otherCount As Long
|
||||
|
||||
componentCount = 0
|
||||
jointCount = 0
|
||||
elementCount = 0
|
||||
otherCount = 0
|
||||
|
||||
Dim mat As Variant
|
||||
For Each mat In materials
|
||||
Dim matType As String
|
||||
matType = CStr(mat("materialType"))
|
||||
|
||||
Select Case matType
|
||||
Case "部件"
|
||||
componentCount = componentCount + 1
|
||||
Case "接头"
|
||||
jointCount = jointCount + 1
|
||||
Case "弹性元件"
|
||||
elementCount = elementCount + 1
|
||||
Case Else
|
||||
otherCount = otherCount + 1
|
||||
End Select
|
||||
Next mat
|
||||
|
||||
' 验证组合规则
|
||||
' 规则1: 只有1个部件,没有接头和弹性元件
|
||||
If componentCount = 1 And jointCount = 0 And elementCount = 0 Then
|
||||
result("valid") = True
|
||||
result("message") = "验证通过:1个部件"
|
||||
Set ValidateComponentCombination = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 规则2: 没有部件,恰好1个接头和1个弹性元件
|
||||
If componentCount = 0 And jointCount = 1 And elementCount = 1 Then
|
||||
result("valid") = True
|
||||
result("message") = "验证通过:1个接头+1个弹性元件"
|
||||
Set ValidateComponentCombination = result
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 其他情况都是异常
|
||||
Dim errorMsg As String
|
||||
errorMsg = "部件组合异常: "
|
||||
|
||||
If componentCount > 1 Then
|
||||
errorMsg = errorMsg & "部件数量为" & componentCount & "(应为1)"
|
||||
ElseIf componentCount = 1 And (jointCount > 0 Or elementCount > 0) Then
|
||||
errorMsg = errorMsg & "同时存在部件和子件(不应共存)"
|
||||
ElseIf jointCount <> elementCount Then
|
||||
errorMsg = errorMsg & "接头数量(" & jointCount & ")≠弹性元件数量(" & elementCount & ")"
|
||||
ElseIf jointCount = 0 And elementCount = 0 Then
|
||||
errorMsg = errorMsg & "缺少部件和子件"
|
||||
Else
|
||||
errorMsg = errorMsg & "未知异常组合"
|
||||
End If
|
||||
|
||||
result("valid") = False
|
||||
result("message") = errorMsg
|
||||
|
||||
Set ValidateComponentCombination = result
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
result("valid") = False
|
||||
result("message") = "验证过程发生错误: " & err.Description
|
||||
Set ValidateComponentCombination = result
|
||||
End Function
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,493 +0,0 @@
|
||||
' ==============================================================================
|
||||
' 模块: M99_TestRunner
|
||||
' 职责: 单元测试,验证 M03_Logic 的核心算法
|
||||
' 依赖: M03_Logic, clsErrorLogger (无需引用 Scripting Runtime)
|
||||
' ==============================================================================
|
||||
Option Explicit
|
||||
|
||||
Private m_Logger As clsErrorLogger
|
||||
Private m_FailCount As Long
|
||||
Private m_PassCount As Long
|
||||
Private m_wsMapping As Worksheet ' 用于测试的映射表工作表
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 主入口: 运行所有测试
|
||||
' ------------------------------------------------------------------------------
|
||||
Public Sub RunAllTests()
|
||||
' 初始化环境
|
||||
Set m_Logger = New clsErrorLogger
|
||||
M03_Logic.InitLogic m_Logger
|
||||
m_FailCount = 0
|
||||
m_PassCount = 0
|
||||
|
||||
Debug.Print String(50, "=")
|
||||
Debug.Print "开始运行单元测试: " & Now
|
||||
Debug.Print String(50, "-")
|
||||
|
||||
' 执行测试用例
|
||||
Test_01_SimpleAtom
|
||||
Test_02_SimpleAND
|
||||
Test_03_SimpleOR
|
||||
Test_04_CartesianProduct ' 核心:测试 (A OR B) AND C
|
||||
Test_05_InequalityMerge ' 核心:测试 !=A AND !=B
|
||||
Test_06_LogicConflict ' 核心:测试 A=1 AND A=2
|
||||
Test_07_ComplexNested ' 核心:多层括号
|
||||
|
||||
' 新增预处理测试
|
||||
Debug.Print String(50, "-")
|
||||
Debug.Print "新增预处理测试:"
|
||||
Debug.Print String(50, "-")
|
||||
|
||||
Test_PP_01_AzxsMappingLoad
|
||||
Test_PP_02_LcfwMappingLoad
|
||||
Test_PP_03_AzxsReplacement
|
||||
Test_PP_04_LcfwReplacement
|
||||
Test_PP_05_ORMerging
|
||||
Test_PP_06_FullIntegration
|
||||
Test_PP_07_NonJointCategory
|
||||
Test_PP_08_UnmappedValues
|
||||
Test_PP_09_ComponentCategory_AzxsMapping
|
||||
Test_PP_10_ComponentCategory_LcfwUnchanged
|
||||
Test_PP_11_ComponentCategory_ORMerging
|
||||
Test_PP_12_ComponentCategory_ParenthesesSimplification
|
||||
|
||||
' 汇总结果
|
||||
Debug.Print String(50, "-")
|
||||
If m_FailCount = 0 Then
|
||||
Debug.Print "测试结果: ALL PASS! (共 " & m_PassCount & " 个测试点)"
|
||||
Else
|
||||
Debug.Print "测试结果: 失败 " & m_FailCount & " 个, 通过 " & m_PassCount & " 个"
|
||||
End If
|
||||
Debug.Print String(50, "=")
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 01: 简单赋值
|
||||
' 输入: gclj=M20
|
||||
' 期望: 1行数据, gclj字段为M20
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_01_SimpleAtom()
|
||||
Dim col As Collection
|
||||
Dim row As Object ' Dictionary
|
||||
|
||||
Set col = M03_Logic.ParseRule("gclj=M20", 1)
|
||||
|
||||
Assert_NotNull col, "T01_Col_Not_Null"
|
||||
Assert_Equal col.count, 1, "T01_Count"
|
||||
|
||||
Set row = col(1)
|
||||
Assert_Equal row("gclj"), "M20", "T01_Value_Check"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 02: AND 逻辑 (属性合并)
|
||||
' 输入: gclj=M20 AND jycz=1
|
||||
' 期望: 1行数据, 包含两个字段
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_02_SimpleAND()
|
||||
Dim col As Collection
|
||||
Dim row As Object
|
||||
|
||||
Set col = M03_Logic.ParseRule("gclj=M20 AND jycz=1", 2)
|
||||
|
||||
Assert_Equal col.count, 1, "T02_Count"
|
||||
Set row = col(1)
|
||||
Assert_Equal row("gclj"), "M20", "T02_Key1"
|
||||
Assert_Equal row("jycz"), "1", "T02_Key2"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 03: OR 逻辑 (记录分裂)
|
||||
' 输入: azxs=A0 OR azxs=A1
|
||||
' 期望: 2行数据
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_03_SimpleOR()
|
||||
Dim col As Collection
|
||||
|
||||
Set col = M03_Logic.ParseRule("azxs=A0 OR azxs=A1", 3)
|
||||
|
||||
Assert_Equal col.count, 2, "T03_Count"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 04: 笛卡尔积 (AND 连接 OR)
|
||||
' 输入: gclj=M20 AND (azxs=A0 OR azxs=A1)
|
||||
' 期望: 2行数据。行1(gclj=M20, azxs=A0), 行2(gclj=M20, azxs=A1)
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_04_CartesianProduct()
|
||||
Dim col As Collection
|
||||
Dim r1 As Object, r2 As Object
|
||||
|
||||
Set col = M03_Logic.ParseRule("gclj=M20 AND (azxs=A0 OR azxs=A1)", 4)
|
||||
|
||||
Assert_Equal col.count, 2, "T04_Count"
|
||||
|
||||
Set r1 = col(1)
|
||||
Set r2 = col(2)
|
||||
|
||||
' 验证公共部分
|
||||
Assert_Equal r1("gclj"), "M20", "T04_Row1_Common"
|
||||
Assert_Equal r2("gclj"), "M20", "T04_Row2_Common"
|
||||
|
||||
' 验证差异部分
|
||||
Dim hasA0 As Boolean, hasA1 As Boolean
|
||||
If r1("azxs") = "A0" Or r2("azxs") = "A0" Then hasA0 = True
|
||||
If r1("azxs") = "A1" Or r2("azxs") = "A1" Then hasA1 = True
|
||||
|
||||
Assert_Equal hasA0, True, "T04_Has_A0"
|
||||
Assert_Equal hasA1, True, "T04_Has_A1"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 05: 不等于逻辑合并
|
||||
' 输入: gclj!=M20 AND gclj!=M30
|
||||
' 期望: 1行数据, gclj字段为 "!=M20,!=M30"
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_05_InequalityMerge()
|
||||
Dim col As Collection
|
||||
Dim row As Object
|
||||
|
||||
Set col = M03_Logic.ParseRule("gclj!=M20 AND gclj!=M30", 5)
|
||||
|
||||
Assert_Equal col.count, 1, "T05_Count"
|
||||
Set row = col(1)
|
||||
Assert_Equal row("gclj"), "!=M20,!=M30", "T05_Value_Merge"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 06: 逻辑冲突检测 (修正版)
|
||||
' 输入: gclj=M20 AND gclj=M30
|
||||
' 期望: 返回 Nothing 或 空集合,且 Logger 中有记录
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_06_LogicConflict()
|
||||
Dim col As Collection
|
||||
|
||||
' 重置 Logger
|
||||
Set m_Logger = New clsErrorLogger
|
||||
M03_Logic.InitLogic m_Logger
|
||||
|
||||
' 此时 ParseRule 内部会捕捉冲突
|
||||
Set col = M03_Logic.ParseRule("gclj=M20 AND gclj=M30", 6)
|
||||
|
||||
' 检查结果: 应该是 Nothing 或者 Count=0
|
||||
Dim isInvalid As Boolean
|
||||
If col Is Nothing Then
|
||||
isInvalid = True
|
||||
Else
|
||||
If col.count = 0 Then isInvalid = True Else isInvalid = False
|
||||
End If
|
||||
|
||||
Assert_Equal isInvalid, True, "T06_Should_Return_Empty_Or_Nothing"
|
||||
Assert_Equal m_Logger.HasErrors, True, "T06_Should_Log_Error"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 07: 复杂嵌套
|
||||
' 输入: A=1 AND (B=1 OR (B=2 AND C=3))
|
||||
' 期望: 2行
|
||||
' Row 1: A=1, B=1
|
||||
' Row 2: A=1, B=2, C=3
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_07_ComplexNested()
|
||||
Dim col As Collection
|
||||
Set col = M03_Logic.ParseRule("A=1 AND (B=1 OR (B=2 AND C=3))", 7)
|
||||
|
||||
Assert_Equal col.count, 2, "T07_Count"
|
||||
|
||||
Dim count2 As Long, count3 As Long
|
||||
Dim i As Long
|
||||
Dim r As Object
|
||||
|
||||
For i = 1 To col.count
|
||||
Set r = col(i)
|
||||
If r.count = 2 Then count2 = count2 + 1
|
||||
If r.count = 3 Then count3 = count3 + 1
|
||||
Next i
|
||||
|
||||
Assert_Equal count2, 1, "T07_Row_With_2_Keys"
|
||||
Assert_Equal count3, 1, "T07_Row_With_3_Keys"
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 辅助断言函数
|
||||
' ==============================================================================
|
||||
Private Sub Assert_Equal(actual As Variant, expected As Variant, testName As String)
|
||||
If CStr(actual) = CStr(expected) Then
|
||||
' Debug.Print " [PASS] " & testName
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Expected: " & expected & ", Actual: " & actual
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub Assert_NotNull(obj As Object, testName As String)
|
||||
If Not obj Is Nothing Then
|
||||
m_PassCount = m_PassCount + 1
|
||||
Else
|
||||
Debug.Print " [FAIL] " & testName & " | Object is Nothing"
|
||||
m_FailCount = m_FailCount + 1
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' ==============================================================================
|
||||
' 预处理测试用例
|
||||
' ==============================================================================
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_01: 测试azxs映射加载
|
||||
' 验证所有12个azxs值是否正确映射
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_01_AzxsMappingLoad()
|
||||
SetupPreProcessorTest
|
||||
|
||||
' 测试径向映射
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("A0"), "径向", "PP01_A0_To_径向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("AT"), "径向", "PP01_AT_To_径向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("AH"), "径向", "PP01_AH_To_径向"
|
||||
|
||||
' 测试下轴向映射
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("B0"), "下轴向", "PP01_B0_To_下轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BT"), "下轴向", "PP01_BT_To_下轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BZ"), "下轴向", "PP01_BZ_To_下轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BH"), "下轴向", "PP01_BH_To_下轴向"
|
||||
|
||||
' 测试中轴向映射
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("Z0"), "中轴向", "PP01_Z0_To_中轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZT"), "中轴向", "PP01_ZT_To_中轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZZ"), "中轴向", "PP01_ZZ_To_中轴向"
|
||||
Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZH"), "中轴向", "PP01_ZH_To_中轴向"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_02: 测试lcfw映射加载
|
||||
' 验证M01-M11都映射到"低压"
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_02_LcfwMappingLoad()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M01"), "低压", "PP02_M01_To_低压"
|
||||
Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M02"), "低压", "PP02_M02_To_低压"
|
||||
Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M03"), "低压", "PP02_M03_To_低压"
|
||||
Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M11"), "低压", "PP02_M11_To_低压"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_03: 测试azxs值替换
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_03_AzxsReplacement()
|
||||
SetupPreProcessorTest
|
||||
|
||||
' 单个原子
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("azxs=A0", "接头", 1)
|
||||
Assert_Equal result1, "azxs=径向", "PP03_Single_Azxs_Replacement"
|
||||
|
||||
' 与AND结合
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND azxs=B0", "接头", 2)
|
||||
Assert_Equal result2, "gclj=M20 AND azxs=下轴向", "PP03_Azxs_With_AND"
|
||||
|
||||
' 在OR中
|
||||
Dim result3 As String
|
||||
result3 = M05_PreProcessor.PreprocessCondition("azxs=A0 OR azxs=AT", "接头", 3)
|
||||
Assert_Equal result3, "azxs=径向", "PP03_Azxs_With_OR_Merged"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_04: 测试lcfw值替换
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_04_LcfwReplacement()
|
||||
SetupPreProcessorTest
|
||||
|
||||
' 单个原子
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("lcfw=M01", "接头", 1)
|
||||
Assert_Equal result1, "lcfw=低压", "PP04_Single_Lcfw_Replacement"
|
||||
|
||||
' 与AND结合
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND lcfw=M02", "接头", 2)
|
||||
Assert_Equal result2, "gclj=M20 AND lcfw=低压", "PP04_Lcfw_With_AND"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_05: 测试OR条件合并
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_05_ORMerging()
|
||||
SetupPreProcessorTest
|
||||
|
||||
' 精确重复
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("lcfw=低压 OR lcfw=低压", "接头", 1)
|
||||
Assert_Equal result1, "lcfw=低压", "PP05_Exact_Duplicate_Merging"
|
||||
|
||||
' 多次重复
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("azxs=径向 OR azxs=径向 OR azxs=径向", "接头", 2)
|
||||
Assert_Equal result2, "azxs=径向", "PP05_Multiple_Duplicate_Merging"
|
||||
|
||||
' 混合情况(保留不同的)
|
||||
Dim result3 As String
|
||||
result3 = M05_PreProcessor.PreprocessCondition("lcfw=低压 OR lcfw=高压", "接头", 3)
|
||||
' 注意:高压不会在映射表中,所以保持原值
|
||||
Dim hasLow As Boolean, hasHigh As Boolean
|
||||
hasLow = InStr(result3, "lcfw=低压") > 0
|
||||
hasHigh = InStr(result3, "lcfw=高压") > 0
|
||||
Assert_Equal hasLow, True, "PP05_Mixed_Has_Low"
|
||||
Assert_Equal hasHigh, True, "PP05_Mixed_Has_High"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_06: 测试完整集成
|
||||
' 验证预处理后的结果能被M03_Logic正确解析
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_06_FullIntegration()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim inputCond As String
|
||||
inputCond = "gclj=M16 AND (azxs=A0 OR azxs=AT) AND (lcfw=M01 OR lcfw=M15)"
|
||||
|
||||
' 预处理
|
||||
Dim preprocessed As String
|
||||
preprocessed = M05_PreProcessor.PreprocessCondition(inputCond, "接头", 1)
|
||||
|
||||
' Debug output
|
||||
Debug.Print "PP06 Debug:"
|
||||
Debug.Print " Input: " & inputCond
|
||||
Debug.Print " Expected: gclj=M16 AND azxs=径向 AND (lcfw=低压 OR lcfw=高压)"
|
||||
Debug.Print " Actual: " & preprocessed
|
||||
|
||||
' 解析预处理后的条件
|
||||
Dim col As Collection
|
||||
Set col = M03_Logic.ParseRule(preprocessed, 1)
|
||||
|
||||
Assert_NotNull col, "PP06_Result_Not_Null"
|
||||
Assert_Equal col.count, 2, "PP06_Count_After_Preprocessing"
|
||||
|
||||
Dim row As Object
|
||||
Set row = col(1)
|
||||
Assert_Equal row("gclj"), "M16", "PP06_gclj_Value"
|
||||
Assert_Equal row("azxs"), "径向", "PP06_azxs_Mapped_Value"
|
||||
Assert_Equal row("lcfw"), "低压", "PP06_lcfw_Mapped_Value"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_07: 测试非"接头"/"部件"类别
|
||||
' 验证其他类别(如"弹性元件")不受预处理影响
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_07_NonJointCategory()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim inputCond As String
|
||||
inputCond = "gclj=M20 AND lcfw=M01"
|
||||
|
||||
' 使用"弹性元件"类别
|
||||
Dim result As String
|
||||
result = M05_PreProcessor.PreprocessCondition(inputCond, "弹性元件", 1)
|
||||
|
||||
' 应该保持不变
|
||||
Assert_Equal result, inputCond, "PP07_NonJoint_Unchanged"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_08: 测试未映射的值
|
||||
' 验证不在映射表中的值保持原样并记录警告
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_08_UnmappedValues()
|
||||
SetupPreProcessorTest
|
||||
|
||||
' 重置logger以捕获警告
|
||||
Set m_Logger = New clsErrorLogger
|
||||
M05_PreProcessor.InitPreProcessor m_Logger, m_wsMapping
|
||||
|
||||
Dim result As String
|
||||
result = M05_PreProcessor.PreprocessCondition("lcfw=INVALID", "接头", 1)
|
||||
|
||||
' 值应该保持不变
|
||||
Assert_Equal result, "lcfw=INVALID", "PP08_Unmapped_Value_Unchanged"
|
||||
|
||||
' 应该记录警告(如果实现了)
|
||||
' 注意:这个测试可能需要根据实际日志记录行为调整
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_09: 测试"部件"类别的azxs映射
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_09_ComponentCategory_AzxsMapping()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("azxs=A0 AND gclj=M20", "部件", 1)
|
||||
Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP09_Component_Azxs_Mapping"
|
||||
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND azxs=B0", "部件", 2)
|
||||
Assert_Equal result2, "gclj=M20 AND azxs=下轴向", "PP09_Component_Azxs_With_AND"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_10: 测试"部件"类别的lcfw条件保持不变
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_10_ComponentCategory_LcfwUnchanged()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("lcfw=M01 AND gclj=M20", "部件", 1)
|
||||
Assert_Equal result1, "lcfw=M01 AND gclj=M20", "PP10_Component_Lcfw_Unchanged"
|
||||
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND lcfw=M01", "部件", 2)
|
||||
Assert_Equal result2, "gclj=M20 AND lcfw=M01", "PP10_Component_Lcfw_Unchanged_Reverse"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_11: 测试"部件"类别的OR条件合并
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_11_ComponentCategory_ORMerging()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("(azxs=A0 OR azxs=AT) AND gclj=M20", "部件", 1)
|
||||
Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP11_Component_OR_Merging_Azxs"
|
||||
|
||||
' 测试精确重复的OR条件合并
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("azxs=径向 OR azxs=径向", "部件", 2)
|
||||
Assert_Equal result2, "azxs=径向", "PP11_Component_Exact_Duplicate_Merging"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 测试用例 PP_12: 测试"部件"类别的括号简化
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub Test_PP_12_ComponentCategory_ParenthesesSimplification()
|
||||
SetupPreProcessorTest
|
||||
|
||||
Dim result1 As String
|
||||
result1 = M05_PreProcessor.PreprocessCondition("(azxs=A0 OR azxs=AT) AND gclj=M20", "部件", 1)
|
||||
Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP12_Component_Parentheses_Simplified"
|
||||
|
||||
Dim result2 As String
|
||||
result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND (azxs=A0 OR azxs=AT)", "部件", 2)
|
||||
Assert_Equal result2, "gclj=M20 AND azxs=径向", "PP12_Component_Parentheses_Simplified_Reverse"
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------------
|
||||
' 辅助函数:设置预处理测试环境
|
||||
' ------------------------------------------------------------------------------
|
||||
Private Sub SetupPreProcessorTest()
|
||||
' 查找[对照表]工作表
|
||||
On Error Resume Next
|
||||
Set m_wsMapping = ActiveWorkbook.Sheets("对照表")
|
||||
On Error GoTo 0
|
||||
|
||||
If m_wsMapping Is Nothing Then
|
||||
Debug.Print " [SKIP] 预处理测试 - 未找到[对照表]工作表"
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' 初始化预处理器
|
||||
Set m_Logger = New clsErrorLogger
|
||||
M03_Logic.InitLogic m_Logger
|
||||
M05_PreProcessor.InitPreProcessor m_Logger, m_wsMapping
|
||||
End Sub
|
||||
226
docs/BIPUploadModule流程图.md
Normal file
226
docs/BIPUploadModule流程图.md
Normal file
@@ -0,0 +1,226 @@
|
||||
# BIPUploadModule 流程图文档
|
||||
|
||||
## 模块概述
|
||||
|
||||
**模块名**: BIPUploadModule
|
||||
**功能**: 处理产品订单数据,提取BOM后生成[BIP上传模板]格式数据
|
||||
**主入口**: ProcessOrdersToBIP
|
||||
|
||||
## ProcessOrdersToBIP 流程图(简化版)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始处理]) --> Init["初始化<br/>1. 获取工作表<br/>2. 加载BOM库"]
|
||||
|
||||
Init --> Loop["循环处理每个订单"]
|
||||
|
||||
Loop --> Read["读取订单信息<br/>- 生产订单号<br/>- 产品型号<br/>- 数量<br/>- 产品编码<br/>- 部件优先标志"]
|
||||
|
||||
Read --> Parse["解析产品型号<br/>提取规格参数"]
|
||||
|
||||
Parse --> Extract["提取BOM物料<br/>根据规格参数匹配物料库"]
|
||||
|
||||
Extract --> CheckPriority{"部件优先=否?"}
|
||||
CheckPriority -->|是| SkipComponent["排除'部件'类别<br/>只提取子类别物料"]
|
||||
CheckPriority -->|否| KeepAll["保留所有物料"]
|
||||
SkipComponent --> Generate
|
||||
KeepAll --> Generate["生成BIP上传数据<br/>- 订单信息<br/>- 物料清单<br/>- 行号编码"]
|
||||
|
||||
Generate --> Collect["收集到内存集合"]
|
||||
|
||||
Collect --> NextOrder{"还有订单?"}
|
||||
NextOrder -->|是| Loop
|
||||
NextOrder -->|否| BatchWrite["批量写入[BIP上传模板]"]
|
||||
|
||||
BatchWrite --> Format["格式化表格"]
|
||||
|
||||
Format --> End([完成])
|
||||
|
||||
style Init fill:#e1f5e1
|
||||
style Parse fill:#fff3cd
|
||||
style Extract fill:#d1ecf1
|
||||
style Generate fill:#d1ecf1
|
||||
style BatchWrite fill:#f8d7da
|
||||
style End fill:#e1f5e1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ProcessSingleOrder 子流程(详细版)
|
||||
|
||||
> 注:以下为技术人员提供详细流程图
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始 ProcessSingleOrder]) --> ClearExclude["清空排除类别<br/>ClearExcludeCategories"]
|
||||
|
||||
ClearExclude --> CheckPriority{"部件优先?"}
|
||||
CheckPriority -->|否/0/FALSE| AddExclude["创建排除类别集合<br/>添加: 部件"]
|
||||
AddExclude --> SetExclude["设置排除类别"]
|
||||
SetExclude --> ParseModel
|
||||
CheckPriority -->|是| ParseModel["解析产品型号<br/>ProductModelParser.Parse"]
|
||||
|
||||
ParseModel --> CheckParse{"解析成功?"}
|
||||
CheckParse -->|否| CreateError1["创建错误行数据<br/>备注: 解析失败信息"]
|
||||
CreateError1 --> AddError1["添加到outputData"] --> End1([返回])
|
||||
|
||||
CheckParse -->|是| ExtractBOM["提取BOM<br/>BomExtractor.ExtractBom"]
|
||||
ExtractBOM --> GetErrors["获取错误信息摘要"]
|
||||
|
||||
GetErrors --> CheckMatch{"匹配到物料?"}
|
||||
CheckMatch -->|否| SetError1["设置备注: 未匹配到任何物料"]
|
||||
SetError1 --> CreateError2["创建空记录行数据"]
|
||||
CreateError2 --> AddError2["添加到outputData"] --> End2([返回])
|
||||
|
||||
CheckMatch -->|是| InitLine["初始化行号索引 lineIndex = 1"]
|
||||
InitLine --> StartLoop["开始循环物料"]
|
||||
|
||||
StartLoop --> GetItem["获取物料项"]
|
||||
GetItem --> InitNote["初始化备注 = 提取错误"]
|
||||
|
||||
InitNote --> CheckItemError{"物料有错误?"}
|
||||
CheckItemError -->|是| AppendError["追加物料错误信息"]
|
||||
CheckItemError -->|否| CreateRow
|
||||
AppendError --> CreateRow["创建BIP行数据<br/>CreateBIPRowArray"]
|
||||
|
||||
CreateRow --> AddRow["添加到outputData"]
|
||||
AddRow --> IncLine["lineIndex++"]
|
||||
IncLine --> NextLoop{"还有物料?"}
|
||||
NextLoop -->|是| StartLoop
|
||||
NextLoop -->|否| End3([返回])
|
||||
```
|
||||
|
||||
## WriteBatchData 批量写入流程
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始 WriteBatchData]) --> CheckData{"outputData为空?"}
|
||||
CheckData -->|是| End1([直接返回])
|
||||
CheckData -->|否| GetRowCount["获取行数 rowCount"]
|
||||
|
||||
GetRowCount --> CreateArray["创建二维数组<br/>resultData1 to rowCount, 1 to 10"]
|
||||
|
||||
CreateArray --> StartLoop["开始循环: i = 1 to rowCount"]
|
||||
StartLoop --> GetRowArray["获取行数组 rowArray = outputDatai"]
|
||||
|
||||
GetRowArray --> FillArray["填充二维数组<br/>resultDatai, 1 to 10 = rowArray1 to 10"]
|
||||
FillArray --> NextLoop{"i++ 还有数据?"}
|
||||
NextLoop -->|是| StartLoop
|
||||
NextLoop -->|否| WriteSheet["一次性写入工作表<br/>A2单元格.Resize rowCount, 10"]
|
||||
|
||||
WriteSheet --> End([完成])
|
||||
```
|
||||
|
||||
## 数据结构说明
|
||||
|
||||
### BIP上传模板字段 (10列)
|
||||
|
||||
| 列号 | 字段名 | 说明 | 数据来源 |
|
||||
|------|--------|------|----------|
|
||||
| 1 | 来源单据号(生产订单号) | 订单唯一标识 | [产品订单] A列 |
|
||||
| 2 | 产品编码 | 产品代码 | [产品订单] D列 |
|
||||
| 3 | 生产数量 | 生产数量 | [产品订单] C列 |
|
||||
| 4 | 行号 | BOM行号 = 7000 + 索引 | 自动生成(7001, 7002...) |
|
||||
| 5 | 材料编码 | 物料66编码 | BOM提取结果 |
|
||||
| 6 | 供应方式 | 固定值 | "一般发料" |
|
||||
| 7 | 需用日期 | 日期 | 当天日期 Date |
|
||||
| 8 | 发料组织 | 固定值 | "重庆布莱迪仪器仪表有限公司" |
|
||||
| 9 | 计划出库数量 | 数量 | 与生产数量一致 |
|
||||
| 10 | 备注 | 异常信息 | BOM提取错误/异常 |
|
||||
|
||||
### [产品订单] 工作表结构 (5列)
|
||||
|
||||
| 列号 | 字段名 | 说明 | 必填 |
|
||||
|------|--------|------|------|
|
||||
| A | 生产订单号 | 订单唯一标识 | 是 |
|
||||
| B | 产品型号 | 产品完整型号 | 是 |
|
||||
| C | 数量 | 生产数量 | 是 |
|
||||
| D | 产品编码 | 产品代码 | 否 |
|
||||
| E | 部件优先 | 是否优先提取部件类别 | 否 |
|
||||
|
||||
## 关键特性
|
||||
|
||||
### 1. 性能优化
|
||||
- **批量写入**: 使用数组一次性写入所有数据,而非逐个单元格写入
|
||||
- **内存缓存**: 所有数据先收集到Collection对象,最后统一输出
|
||||
- **预期性能提升**: 10-100倍(取决于数据量)
|
||||
|
||||
### 2. 部件优先功能
|
||||
- 当`部件优先` = "否"/"0"/"FALSE"时:
|
||||
- 排除"部件"类别的物料
|
||||
- 只提取"部件"的子类别物料(如"接头"、"弹性元件"等)
|
||||
- 当`部件优先` = "是"或其他值时:
|
||||
- 正常提取所有物料,包括"部件"类别及其子类别
|
||||
|
||||
### 3. 错误处理
|
||||
- **必填字段验证**: 生产订单号、产品型号、数量
|
||||
- **解析失败处理**: 记录错误信息到备注列
|
||||
- **BOM提取异常**: 记录匹配失败和异常信息
|
||||
- **空行处理**: 自动跳过完全空白的行
|
||||
|
||||
### 4. 数据完整性
|
||||
- 每次运行前清空旧数据(保留表头)
|
||||
- 自动创建[BIP上传模板]工作表(如不存在)
|
||||
- 行号自动生成(7001, 7002, 7003...)
|
||||
- 日期自动填充为当天日期
|
||||
|
||||
## 执行示例
|
||||
|
||||
### 正常流程示例
|
||||
|
||||
```
|
||||
输入: [产品订单] 3行数据
|
||||
- 订单001, 型号MD-100, 数量10, 编码P001, 部件优先=是
|
||||
- 订单002, 型号MD-200, 数量20, 编码P002, 部件优先=否
|
||||
- 订单003, 型号MD-300, 数量30, 编码P003, 部件优先=是
|
||||
|
||||
处理流程:
|
||||
1. 读取3行订单数据
|
||||
2. 逐个解析产品型号
|
||||
3. 根据部件优先设置提取BOM
|
||||
4. 订单001: 提取5种物料(包括部件)
|
||||
5. 订单002: 提取4种物料(排除部件,只提取子类别)
|
||||
6. 订单003: 提取6种物料(包括部件)
|
||||
7. 批量写入15行数据到[BIP上传模板]
|
||||
8. 显示完成信息: 处理3个订单,生成15行BOM数据
|
||||
|
||||
输出: [BIP上传模板] 15行数据
|
||||
```
|
||||
|
||||
### 异常处理示例
|
||||
|
||||
```
|
||||
输入: [产品订单] 包含异常数据
|
||||
- 订单A01, 型号INVALID, 数量5 ← 型号解析失败
|
||||
- 订单A02, 型号MD-100, 数量0 ← 未匹配到任何物料
|
||||
- 订单A03, 型号MD-200, 数量10 ← 正常提取3种物料
|
||||
|
||||
输出:
|
||||
第1行: 订单A01, ..., 备注: "解析失败: 无效的型号格式"
|
||||
第2行: 订单A02, ..., 备注: "未匹配到任何物料"
|
||||
第3-5行: 订单A03, 3种物料数据
|
||||
```
|
||||
|
||||
## 相关模块
|
||||
|
||||
### 依赖的类模块
|
||||
- **BomExtractor**: BOM提取器,负责从平台配置清单中提取匹配的物料
|
||||
- **ProductModelParser**: 产品型号解析器,解析型号字符串为结构化条件
|
||||
- **BomItem**: BOM物料项数据模型
|
||||
- **ConditionEvaluator**: 条件评估器,评估选择条件和类别条件
|
||||
|
||||
### 相关模块
|
||||
- **MainModule**: 主控模块,处理产品型号提取和BOM匹配
|
||||
- 类似的处理逻辑
|
||||
- 输出到[BOM提取结果]工作表
|
||||
- 支持部件优先功能
|
||||
- 使用相同的批量写入优化
|
||||
|
||||
## 版本历史
|
||||
|
||||
| 版本 | 日期 | 说明 |
|
||||
|------|------|------|
|
||||
| 1.0 | 2026-02-01 | 初始版本,实现基本的订单处理和BOM提取功能 |
|
||||
| 1.1 | 2026-02-01 | 添加部件优先功能,支持排除特定类别 |
|
||||
| 1.2 | 2026-02-01 | 性能优化,使用数组批量写入替代逐个单元格写入 |
|
||||
| 1.3 | 2026-02-01 | 修复Mermaid流程图方括号转义问题 |
|
||||
@@ -1,212 +0,0 @@
|
||||
# BOM匹配验证逻辑修复总结
|
||||
|
||||
## 问题描述
|
||||
|
||||
### Bug表现
|
||||
当[部件]工作表匹配到**1条记录**但返回**2个子件**(接头+弹性元件)时,系统错误地报告"[部件]工作表匹配到2条记录"。
|
||||
|
||||
### 具体场景
|
||||
1. 某个型号在[部件]工作表匹配到 **1条记录**
|
||||
2. 该记录的"部件"物料库存不足
|
||||
3. M08_ComponentProcessor 返回 **2个子件**:1个接头 + 1个弹性元件
|
||||
4. 验证逻辑误报:`[部件]工作表匹配到2条记录` ❌ **这是误报!**
|
||||
|
||||
### 根本原因
|
||||
**位置:** `M09_BOMExtractor.bas` 第428行(修复前)
|
||||
|
||||
**错误代码:**
|
||||
```vba
|
||||
' 当处理[部件]工作表时
|
||||
matchResult("rowCount") = componentMaterials.count ' ❌ 错误:这是物料数量,不是匹配行数
|
||||
```
|
||||
|
||||
**问题分析:**
|
||||
- `rowCount` 应该表示**工作表匹配的行数**(应该是1)
|
||||
- 但代码错误地使用了**返回的物料数量**(变成2)
|
||||
- 当库存不足返回2个子件时,`rowCount=2` 被错误理解为"匹配了2行"
|
||||
|
||||
## 解决方案
|
||||
|
||||
### 修改文件1: `M08_ComponentProcessor.bas`
|
||||
|
||||
#### 修改内容:函数签名变更
|
||||
|
||||
**修改前:**
|
||||
```vba
|
||||
Public Function ProcessComponentRecord( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal params As Object, _
|
||||
ByVal logger As clsErrorLogger _
|
||||
) As Collection
|
||||
```
|
||||
|
||||
**修改后:**
|
||||
```vba
|
||||
Public Function ProcessComponentRecord( _
|
||||
ByVal wsComponent As Worksheet, _
|
||||
ByVal params As Object, _
|
||||
ByVal logger As clsErrorLogger, _
|
||||
ByVal matchedRowNum As Long _ ' ✅ 新增参数:匹配的行号
|
||||
) As Collection
|
||||
```
|
||||
|
||||
#### 修改原因
|
||||
- 传递匹配行号,让 ProcessComponentRecord 知道要处理哪一行
|
||||
- 避免内部重复调用 `MatchBOMRecord`(去除冗余匹配逻辑)
|
||||
- 使用传入的行号而不是内部重新匹配
|
||||
|
||||
#### 内部逻辑变更
|
||||
- **移除:** 内部的 `MatchBOMRecord` 调用
|
||||
- **新增:** `matchedRowNum` 参数验证
|
||||
- **修改:** 所有 `rowNum` 变量引用改为 `matchedRowNum`
|
||||
- **修改:** 错误处理器使用传入的 `matchedRowNum`
|
||||
|
||||
### 修改文件2: `M09_BOMExtractor.bas`
|
||||
|
||||
#### 修改位置:`MatchAllMaterialTypesWithValidation` 函数 (约第417-448行)
|
||||
|
||||
**修改前逻辑:**
|
||||
```vba
|
||||
' "部件"工作表特殊处理
|
||||
If sheetName = BOMLIB_SHEET_COMPONENT Then
|
||||
Debug.Print " -> 使用部件处理逻辑"
|
||||
|
||||
Dim componentMaterials As Collection
|
||||
Set componentMaterials = M08_ComponentProcessor.ProcessComponentRecord( _
|
||||
ws, params, logger)
|
||||
|
||||
Debug.Print " -> 返回物料数: " & componentMaterials.count
|
||||
|
||||
matchResult("success") = (componentMaterials.count > 0)
|
||||
matchResult("rowCount") = componentMaterials.count ' ❌ 错误:物料数量
|
||||
|
||||
' 添加物料...
|
||||
End If
|
||||
```
|
||||
|
||||
**修改后逻辑:**
|
||||
```vba
|
||||
' "部件"工作表特殊处理
|
||||
If sheetName = BOMLIB_SHEET_COMPONENT Then
|
||||
Debug.Print " -> 使用部件处理逻辑"
|
||||
|
||||
' 步骤1: 先调用标准匹配获取匹配行数(这是工作表匹配的行数,不是物料数量)
|
||||
Dim bomMatchResult As Object
|
||||
Set bomMatchResult = M07_BOMMatcher.MatchBOMRecord(ws, params)
|
||||
|
||||
Debug.Print " -> 标准匹配: success=" & bomMatchResult("success") & ", rowCount=" & bomMatchResult("rowCount")
|
||||
|
||||
' 步骤2: 如果标准匹配成功,调用部件处理器
|
||||
Dim componentMaterials As Collection
|
||||
Set componentMaterials = New Collection
|
||||
|
||||
If bomMatchResult("success") Then
|
||||
Set componentMaterials = M08_ComponentProcessor.ProcessComponentRecord( _
|
||||
ws, params, logger, bomMatchResult("rowNums")(1))
|
||||
|
||||
Debug.Print " -> 返回物料数: " & componentMaterials.count
|
||||
End If
|
||||
|
||||
' 步骤3: 创建匹配结果对象,使用标准匹配的rowCount(工作表行数,不是物料数)
|
||||
matchResult("success") = bomMatchResult("success")
|
||||
matchResult("rowCount") = bomMatchResult("rowCount") ' ✅ 修复:使用标准匹配的rowCount
|
||||
Set matchResult("rowNums") = bomMatchResult("rowNums")
|
||||
|
||||
' 步骤4: 添加物料到匹配结果
|
||||
Dim compMat As Variant
|
||||
For Each compMat In componentMaterials
|
||||
Debug.Print " [" & compMat("materialType") & "] 名称=[" & compMat("materialName") & "] 编码=[" & compMat("materialCode") & "]"
|
||||
matchResult("materials").Add compMat
|
||||
Next compMat
|
||||
End If
|
||||
```
|
||||
|
||||
### 关键变化
|
||||
|
||||
1. **两阶段匹配流程:**
|
||||
- **Phase 1:** 调用 `MatchBOMRecord` 获取标准匹配结果(包含正确的 `rowCount`)
|
||||
- **Phase 2:** 如果匹配成功,调用 `ProcessComponentRecord` 处理部件逻辑
|
||||
|
||||
2. **rowCount 修复:**
|
||||
- `matchResult("rowCount")` 使用标准匹配的 `rowCount`(始终是工作表行数)
|
||||
- 不再使用 `componentMaterials.count`(这是物料数量)
|
||||
|
||||
3. **参数传递:**
|
||||
- 将匹配的行号 `bomMatchResult("rowNums")(1)` 传递给 `ProcessComponentRecord`
|
||||
- 避免了部件处理器内部的重复匹配
|
||||
|
||||
## 预期效果对比
|
||||
|
||||
### 修复前
|
||||
```
|
||||
场景:[部件]匹配1行,库存不足,返回2个子件
|
||||
结果:rowCount = 2 (物料数量)
|
||||
验证:❌ "[部件]工作表匹配到2条记录" (误报)
|
||||
```
|
||||
|
||||
### 修复后
|
||||
```
|
||||
场景:[部件]匹配1行,库存不足,返回2个子件
|
||||
结果:rowCount = 1 (工作表行数) ✅
|
||||
验证:✅ 正确识别为1行匹配,返回2个物料
|
||||
```
|
||||
|
||||
## 验证清单
|
||||
|
||||
### 场景1:部件工作表返回1个部件
|
||||
- [ ] 匹配1行 → rowCount=1 ✅
|
||||
- [ ] 返回1个部件物料 → materials.count=1
|
||||
- [ ] 验证通过 ✅
|
||||
|
||||
### 场景2:部件工作表返回2个子件(核心修复场景)
|
||||
- [ ] 匹配1行 → rowCount=1 ✅ (不再误报为2)
|
||||
- [ ] 返回2个子件(接头+弹性元件)→ materials.count=2
|
||||
- [ ] 验证通过 ✅ (不再报错"匹配到2条记录")
|
||||
|
||||
### 场景3:部件工作表匹配0条
|
||||
- [ ] 匹配0行 → rowCount=0
|
||||
- [ ] 返回0个物料 → materials.count=0
|
||||
- [ ] 正确报错:"[部件]工作表未匹配到记录" ✅
|
||||
|
||||
### 场景4:部件工作表匹配2条
|
||||
- [ ] 匹配2行 → rowCount=2
|
||||
- [ ] 正确报错:"[部件]工作表匹配到2条记录" ✅
|
||||
|
||||
### 场景5:其他工作表的多条匹配
|
||||
- [ ] [接头]工作表匹配2条 → 正确报错 ✅
|
||||
- [ ] [弹性元件]工作表匹配2条 → 正确报错 ✅
|
||||
- [ ] 验证逻辑不受影响 ✅
|
||||
|
||||
## 技术要点
|
||||
|
||||
### rowCount 的语义
|
||||
- **定义:** `rowCount` 表示**工作表中匹配的行数**
|
||||
- **不是:** 返回的物料数量
|
||||
- **关键区别:** 1行匹配可能返回0个、1个或多个物料
|
||||
|
||||
### 两阶段匹配模式
|
||||
```
|
||||
阶段1: 标准匹配 → MatchBOMRecord
|
||||
├─ 返回: success, rowCount, rowNums
|
||||
└─ rowCount = 工作表行数 (始终正确)
|
||||
|
||||
阶段2: 部件处理 → ProcessComponentRecord(matchedRowNum)
|
||||
├─ 输入: matchedRowNum (从阶段1获取)
|
||||
├─ 返回: Collection of materials
|
||||
└─ materials.count = 物料数量 (可能≠rowCount)
|
||||
```
|
||||
|
||||
### 避免重复匹配
|
||||
- **旧模式:** `M09` → `M08.ProcessComponentRecord` → `M07.MatchBOMRecord`
|
||||
- **新模式:** `M09` → `M07.MatchBOMRecord` → `M08.ProcessComponentRecord(rowNum)`
|
||||
- **优势:** 减少冗余匹配调用,代码更清晰
|
||||
|
||||
## 相关文档
|
||||
|
||||
- `docs/BOM匹配错误判断机制详解.md` - 完整的验证规则说明
|
||||
- `docs/M09_BOMExtractor_Flow.md` - BOM提取流程图
|
||||
- `docs/M08_ComponentProcessor_Design.md` - 部件处理器设计文档
|
||||
|
||||
## 修改历史
|
||||
|
||||
- **2025-02-12:** 初始版本 - 修复部件工作表rowCount误报问题
|
||||
File diff suppressed because it is too large
Load Diff
443
docs/ComponentInventoryCheckModule流程图.md
Normal file
443
docs/ComponentInventoryCheckModule流程图.md
Normal file
@@ -0,0 +1,443 @@
|
||||
# ComponentInventoryCheckModule 流程图文档
|
||||
|
||||
## 模块概述
|
||||
|
||||
**模块名**: ComponentInventoryCheckModule
|
||||
**功能**: 部件库存核对模块 - 自动核对产品订单中"部件"类物料的库存情况
|
||||
**主入口**: CheckComponentInventory
|
||||
**说明**: 当库存不足时,按订单顺序将超出部分的订单的"部件优先"字段标记为"否"
|
||||
|
||||
---
|
||||
|
||||
## 业务流程图(面向非技术人员)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始核对部件库存]) --> Step1["1. 读取数据<br/>读取产品订单表<br/>读取库存表<br/>读取物料配置表"]
|
||||
|
||||
Step1 --> Step2["2. 分析订单<br/>查看每个订单需要哪些部件<br/>计算每个订单需要多少个部件"]
|
||||
|
||||
Step2 --> Step3["3. 汇总需求<br/>统计所有订单总共需要多少个部件<br/>按部件种类分别统计"]
|
||||
|
||||
Step3 --> Step4["4. 检查库存<br/>对比库存数量和需求数量<br/>找出库存不够的部件"]
|
||||
|
||||
Step4 --> Step5["5. 分配库存<br/>按订单从上到下依次分配<br/>库存够的订单→保持不变<br/>库存不够的订单→标记为'否'"]
|
||||
|
||||
Step5 --> Step6["6. 完成处理<br/>显示处理结果<br/>多少订单能发货<br/>多少订单缺部件"]
|
||||
|
||||
Step6 --> End([完成])
|
||||
|
||||
style Step1 fill:#e1f5e1
|
||||
style Step2 fill:#d1ecf1
|
||||
style Step3 fill:#fff3cd
|
||||
style Step4 fill:#ffe5b4
|
||||
style Step5 fill:#f8d7da
|
||||
style Step6 fill:#e1f5e1
|
||||
```
|
||||
|
||||
### 业务流程说明
|
||||
|
||||
| 步骤 | 做什么 | 为什么 |
|
||||
|------|--------|--------|
|
||||
| **1. 读取数据** | 从Excel表格中读取订单、库存、配置信息 | 获取处理所需的所有数据 |
|
||||
| **2. 分析订单** | 查看每个订单需要什么部件、多少个 | 了解每个订单的部件需求 |
|
||||
| **3. 汇总需求** | 把所有订单的相同部件需求加在一起 | 算出总共需要多少部件 |
|
||||
| **4. 检查库存** | 对比总需求和实际库存 | 判断库存是否够用 |
|
||||
| **5. 分配库存** | 先来先得,库存不够的标记为"否" | 确定哪些订单能按时发货 |
|
||||
| **6. 完成处理** | 显示统计结果 | 让用户了解处理情况 |
|
||||
|
||||
### 举例说明
|
||||
|
||||
假设有3个订单,都需要同一个部件A:
|
||||
|
||||
| 订单 | 需要部件A数量 | 库存分配过程 | 最终状态 |
|
||||
|------|--------------|--------------|---------|
|
||||
| 订单1 | 2个 | 库存剩5个,够用 | ✓ 保持原值 |
|
||||
| 订单2 | 2个 | 库存剩3个,够用 | ✓ 保持原值 |
|
||||
| 订单3 | 2个 | 库存剩1个,不够 | ✗ 标记为"否" |
|
||||
|
||||
> **技术说明**:标记为"否"的订单,在生成BIP上传数据时会跳过部件类物料,优先保证子部件供应。
|
||||
|
||||
---
|
||||
|
||||
## CheckComponentInventory 主流程图(技术版)
|
||||
|
||||
> 以下流程图面向技术人员,展示完整的错误处理和验证步骤。
|
||||
|
||||
---
|
||||
|
||||
## CheckComponentInventory 主流程图(详细版)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start(["开始 CheckComponentInventory"]) --> GetSheet1["获取[产品订单]工作表"]
|
||||
GetSheet1 --> CheckSheet1{工作表存在?}
|
||||
CheckSheet1 -->|否| ShowMsg1["显示错误消息"] --> End1(["结束"])
|
||||
CheckSheet1 -->|是| GetSheet2["获取[现存量]工作表"]
|
||||
|
||||
GetSheet2 --> CheckSheet2{工作表存在?}
|
||||
CheckSheet2 -->|否| ShowMsg2["显示错误消息"] --> End2(["结束"])
|
||||
CheckSheet2 -->|是| GetSheet3["获取[平台配置清单]工作表"]
|
||||
|
||||
GetSheet3 --> CheckSheet3{工作表存在?}
|
||||
CheckSheet3 -->|否| ShowMsg3["显示错误消息"] --> End3(["结束"])
|
||||
CheckSheet3 -->|是| CheckData{订单数据存在?}
|
||||
|
||||
CheckData -->|否| ShowMsg4["显示无数据消息"] --> End4(["结束"])
|
||||
CheckData -->|是| InitBOM["初始化BOM提取器<br/>LoadBomData"]
|
||||
|
||||
InitBOM --> CheckBOM{BOM加载成功?}
|
||||
CheckBOM -->|否| ShowMsg5["显示BOM错误"] --> End5(["结束"])
|
||||
CheckBOM -->|是| LoadInv["LoadInventoryData<br/>读取库存数据到字典"]
|
||||
|
||||
LoadInv --> CheckInv{库存数据有效?}
|
||||
CheckInv -->|否| ShowMsg6["显示无库存消息"] --> End6(["结束"])
|
||||
CheckInv -->|是| LoadOrders["LoadOrderData<br/>读取订单数据"]
|
||||
|
||||
LoadOrders --> CheckOrders{订单数据有效?}
|
||||
CheckOrders -->|否| ShowMsg7["显示无订单消息"] --> End7(["结束"])
|
||||
CheckOrders -->|是| ParseAll["ParseAllOrdersBOM<br/>解析所有订单的BOM"]
|
||||
|
||||
ParseAll --> CalcDemand["CalculateComponentDemand<br/>统计部件总需求"]
|
||||
|
||||
CalcDemand --> CheckDemand{有部件需求?}
|
||||
CheckDemand -->|否| ShowMsg8["显示无部件消息"] --> End8(["结束"])
|
||||
CheckDemand -->|是| Validate["ValidateInventory<br/>验证库存数据"]
|
||||
|
||||
Validate --> Allocate["AllocateInventory<br/>按订单顺序分配库存"]
|
||||
|
||||
Allocate --> ShowResult["显示结果统计<br/>总订单数、包含部件订单数<br/>库存充足/不足订单数"]
|
||||
|
||||
ShowResult --> End9(["完成"])
|
||||
|
||||
style InitBOM fill:#e1f5e1
|
||||
style LoadInv fill:#d1ecf1
|
||||
style LoadOrders fill:#d1ecf1
|
||||
style ParseAll fill:#fff3cd
|
||||
style CalcDemand fill:#fff3cd
|
||||
style Validate fill:#ffe5b4
|
||||
style Allocate fill:#f8d7da
|
||||
style End9 fill:#e1f5e1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## AllocateInventory 库存分配详细流程
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
AllocStart([开始 AllocateInventory]) --> InitStats[初始化统计信息<br/>TotalOrders, OrdersWithComponent<br/>OrdersSufficient, OrdersInsufficient, OrdersSkipped]
|
||||
|
||||
InitStats --> LoopStart{遍历订单<br/>i = 1 to orders.Count}
|
||||
|
||||
LoopStart -->|有订单| GetOrder[获取订单 order = orders i]
|
||||
GetOrder --> CheckParse{解析失败?}
|
||||
CheckParse -->|ORDER_PARSE_ERR <> ""| Skip1[跳过订单<br/>OrdersSkipped++]
|
||||
CheckParse -->|无错误| CheckHasComp{包含部件?}
|
||||
|
||||
CheckHasComp -->|ORDER_HAS_COMP = False| Skip2[跳过订单<br/>OrdersSkipped++]
|
||||
CheckHasComp -->|HasComponent = True| CheckQty{数量为0?}
|
||||
|
||||
CheckQty -->|ORDER_QUANTITY = 0| Skip3[跳过订单<br/>OrdersSkipped++]
|
||||
CheckQty -->|数量 > 0| UpdateComp[OrdersWithComponent++]
|
||||
|
||||
UpdateComp --> GetComp[获取部件库存信息<br/>compInv = componentDemands compCode]
|
||||
GetComp --> CalcReq[计算需求量<br/>requiredQty = CompQty × OrderQty]
|
||||
|
||||
CalcReq --> CheckStock{库存充足?<br/>compInv.STOCK >= requiredQty}
|
||||
|
||||
CheckStock -->|是| DeductSufficient[扣减库存<br/>compInv.STOCK -= requiredQty<br/>OrdersSufficient++]
|
||||
CheckStock -->|否| MarkNo[标记E列为"否"<br/>orderSheet.Cells Row, 5 = "否"<br/>compInv.STOCK -= requiredQty<br/>OrdersInsufficient++]
|
||||
|
||||
DeductSufficient --> NextOrder[继续下一个订单]
|
||||
MarkNo --> NextOrder
|
||||
Skip1 --> NextOrder
|
||||
Skip2 --> NextOrder
|
||||
Skip3 --> NextOrder
|
||||
|
||||
NextOrder --> LoopStart
|
||||
|
||||
style UpdateComp fill:#e1f5e1
|
||||
style DeductSufficient fill:#e1f5e1
|
||||
style MarkNo fill:#f8d7da
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ParseOrderBOM 解析详细流程
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
ParseStart([开始 ParseOrderBOM]) --> CreateParser[创建ProductModelParser]
|
||||
CreateParser --> ParseModel[解析型号<br/>parser.Parse model]
|
||||
|
||||
ParseModel --> CheckParse{解析成功?}
|
||||
CheckParse -->|否| SetError[设置解析错误<br/>ORDER_PARSE_ERR = "解析失败: ..."]
|
||||
SetError --> ParseEnd([结束返回])
|
||||
|
||||
CheckParse -->|是| ExtractBOM[提取BOM<br/>bomExtractor.ExtractBom conditions]
|
||||
|
||||
ExtractBOM --> LoopComp{遍历BOM项目<br/>For Each item In matchedItems}
|
||||
|
||||
LoopComp -->|有项目| CheckCat{item.category = "部件"?}
|
||||
|
||||
CheckCat -->|否| NextItem[继续下一个项目]
|
||||
CheckCat -->|是| SetCompInfo[设置部件信息<br/>ORDER_COMP_CODE = item.Code66<br/>ORDER_COMP_QTY = item.quantity<br/>ORDER_HAS_COMP = True]
|
||||
|
||||
SetCompInfo --> ParseEnd
|
||||
NextItem --> LoopComp
|
||||
|
||||
style SetCompInfo fill:#d1ecf1
|
||||
style SetError fill:#f8d7da
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 数据处理序列图
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Main as CheckComponentInventory
|
||||
participant Loader as 数据加载器
|
||||
participant Parser as BOM解析器
|
||||
participant Calc as 需求计算器
|
||||
participant Validator as 库存验证器
|
||||
participant Allocator as 库存分配器
|
||||
|
||||
Main->>Loader: LoadInventoryData(ws)
|
||||
Loader-->>Main: 库存字典 Dictionary(编码 -> 数量)
|
||||
|
||||
Main->>Loader: LoadOrderData(ws)
|
||||
Loader-->>Main: 订单集合 Collection(Of Dictionary)
|
||||
|
||||
loop 每个订单
|
||||
Main->>Parser: ParseOrderBOM(order, bomExtractor)
|
||||
Parser->>Parser: ProductModelParser.Parse(model)
|
||||
Parser->>Parser: BomExtractor.ExtractBom(conditions)
|
||||
Parser->>Parser: 查找 category="部件" 的物料
|
||||
Parser-->>Main: 设置订单部件信息
|
||||
end
|
||||
|
||||
Main->>Calc: CalculateComponentDemand(orders)
|
||||
Calc->>Calc: 累加每个订单的部件需求
|
||||
Note over Calc: 需求 = CompQty × OrderQty
|
||||
Calc-->>Main: 部件需求字典 Dictionary(编码 -> 库存信息)
|
||||
|
||||
Main->>Validator: ValidateInventory(demands, inventoryData)
|
||||
Validator->>Validator: 检查每个部件是否存在
|
||||
Validator->>Validator: 计算是否短缺
|
||||
Validator-->>Main: 警告集合 Collection
|
||||
|
||||
Main->>Allocator: AllocateInventory(orders, demands, sheet)
|
||||
loop 按订单顺序遍历
|
||||
Allocator->>Allocator: 获取订单部件需求
|
||||
Allocator->>Allocator: 检查库存是否充足
|
||||
alt 库存充足
|
||||
Allocator->>Allocator: 扣减库存,保持原值
|
||||
Note over Allocator: OrdersSufficient++
|
||||
else 库存不足
|
||||
Allocator->>Allocator: 标记 E5 = "否"
|
||||
Note over Allocator: OrdersInsufficient++
|
||||
end
|
||||
end
|
||||
Allocator-->>Main: 统计信息 Statistics
|
||||
|
||||
Main-->>Main: 显示结果消息框
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 数据结构说明
|
||||
|
||||
### 订单字典结构 (Dictionary Object)
|
||||
|
||||
| 键名 | 常量 | 类型 | 说明 |
|
||||
|------|------|------|------|
|
||||
| RowNumber | ORDER_ROW | Long | 订单所在行号 |
|
||||
| ProductModel | ORDER_MODEL | String | 产品型号 |
|
||||
| Quantity | ORDER_QUANTITY | Double | 产品数量 |
|
||||
| ComponentCode | ORDER_COMP_CODE | String | 部件66编码 |
|
||||
| ComponentQty | ORDER_COMP_QTY | Double | 部件BOM数量 |
|
||||
| HasComponent | ORDER_HAS_COMP | Boolean | 是否包含部件 |
|
||||
| ParseError | ORDER_PARSE_ERR | String | 解析错误信息 |
|
||||
|
||||
### 部件库存字典结构 (Dictionary Object)
|
||||
|
||||
| 键名 | 常量 | 类型 | 说明 |
|
||||
|------|------|------|------|
|
||||
| ComponentCode | INV_CODE | String | 部件66编码 |
|
||||
| TotalDemand | INV_DEMAND | Double | 总需求量 |
|
||||
| AvailableStock | INV_STOCK | Double | 可用库存(动态扣减) |
|
||||
| IsShortage | INV_SHORTAGE | Boolean | 是否短缺 |
|
||||
|
||||
### 统计信息结构 (Statistics Type)
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| TotalOrders | Long | 总订单数 |
|
||||
| OrdersWithComponent | Long | 包含部件的订单数 |
|
||||
| OrdersSufficient | Long | 库存充足订单数 |
|
||||
| OrdersInsufficient | Long | 库存不足订单数 |
|
||||
| OrdersSkipped | Long | 跳过订单数 |
|
||||
|
||||
---
|
||||
|
||||
## 关键特性
|
||||
|
||||
### 1. 库存分配策略
|
||||
- **按订单顺序分配**: 从上到下遍历订单,先到先得
|
||||
- **动态库存扣减**: 库存充足时扣减,不足时标记为负数
|
||||
- **只标记不跳过**: 即使库存不足也继续处理后续订单
|
||||
|
||||
### 2. 部件识别机制
|
||||
- 通过 `BomExtractor` 提取完整BOM
|
||||
- 遍历BOM项目,查找 `category = "部件"` 的物料
|
||||
- 只记录第一个匹配到的部件(假设每个产品只有一个主部件)
|
||||
|
||||
### 3. 错误处理
|
||||
- **工作表缺失**: 提前验证,友好提示
|
||||
- **BOM加载失败**: 显示错误摘要,终止处理
|
||||
- **型号解析失败**: 记录错误,跳过该订单
|
||||
- **部件找不到库存**: 添加警告,继续处理(库存设为0)
|
||||
|
||||
### 4. 数据完整性
|
||||
- 使用 `Dictionary` 实现库存的引用更新
|
||||
- 使用 `Collection` 保持订单顺序
|
||||
- 统计跳过的订单(解析失败、无部件、数量为0)
|
||||
|
||||
---
|
||||
|
||||
## 执行示例
|
||||
|
||||
### 正常流程示例
|
||||
|
||||
```
|
||||
输入数据:
|
||||
[产品订单]
|
||||
第2行: MD-100, 数量2, 部件A(编码661001, 用量1)
|
||||
第3行: MD-200, 数量2, 部件A(编码661001, 用量1)
|
||||
第4行: MD-300, 数量2, 部件A(编码661001, 用量1)
|
||||
|
||||
[现存量]
|
||||
661001: 5
|
||||
|
||||
处理流程:
|
||||
1. 读取3个订单
|
||||
2. 解析BOM,识别部件661001
|
||||
3. 统计需求: 1×2 + 1×2 + 1×2 = 6
|
||||
4. 验证库存: 6 > 5,短缺
|
||||
5. 分配库存:
|
||||
- 订单2: 需求2,库存5→3,充足
|
||||
- 订单3: 需求2,库存3→1,充足
|
||||
- 订单4: 需求2,库存1→-1,不足,标记E4="否"
|
||||
|
||||
输出结果:
|
||||
[产品订单]
|
||||
第2行: E列保持原值
|
||||
第3行: E列保持原值
|
||||
第4行: E列 = "否"
|
||||
|
||||
统计信息:
|
||||
处理订单数: 3
|
||||
包含部件订单: 3
|
||||
库存充足订单: 2
|
||||
库存不足订单: 1
|
||||
```
|
||||
|
||||
### 异常处理示例
|
||||
|
||||
```
|
||||
输入数据:
|
||||
[产品订单]
|
||||
第2行: INVALID-MODEL, 数量5 ← 型号解析失败
|
||||
第3行: MD-100, 数量0 ← 数量为0
|
||||
第4行: MD-200, 数量2, 部件669999 (库存中不存在)
|
||||
|
||||
[现存量]
|
||||
661001: 10
|
||||
|
||||
输出结果:
|
||||
第2行: 跳过(解析失败)
|
||||
第3行: 跳过(数量为0)
|
||||
第4行: 标记E4="否",但添加警告 "部件 '669999' 在[现存量]中未找到"
|
||||
|
||||
统计信息:
|
||||
处理订单数: 3
|
||||
包含部件订单: 1
|
||||
库存充足订单: 0
|
||||
库存不足订单: 1
|
||||
跳过订单数: 2
|
||||
|
||||
警告信息:
|
||||
部件 '669999' 在[现存量]中未找到
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 工作表结构要求
|
||||
|
||||
### [产品订单] 工作表
|
||||
|
||||
| 列号 | 字段名 | 说明 | 必填 |
|
||||
|------|--------|------|------|
|
||||
| A | 生产订单号 | 订单唯一标识 | 否 |
|
||||
| B | 产品型号 | 产品完整型号 | 是 |
|
||||
| C | 数量 | 生产数量 | 是 |
|
||||
| D | 产品编码 | 产品代码 | 否 |
|
||||
| E | 部件优先 | 是否优先提取部件类别(输出字段) | - |
|
||||
|
||||
- **表头**: 第1行
|
||||
- **数据起始**: 第2行
|
||||
- **输出位置**: E列(第5列)
|
||||
|
||||
### [现存量] 工作表
|
||||
|
||||
| 列号 | 字段名 | 说明 |
|
||||
|------|--------|------|
|
||||
| B | 物料编码 | 对应BOM中的66代码 |
|
||||
| J | 结存主数量 | 库存数量 |
|
||||
|
||||
- **表头**: 第3行
|
||||
- **数据起始**: 第4行
|
||||
|
||||
### [平台配置清单] 工作表
|
||||
|
||||
标准的BOM配置清单,用于提取物料信息。
|
||||
|
||||
---
|
||||
|
||||
## 相关模块
|
||||
|
||||
### 依赖的类模块
|
||||
- **BomExtractor**: BOM提取器,负责从平台配置清单中提取匹配的物料
|
||||
- **ProductModelParser**: 产品型号解析器,解析型号字符串为结构化条件
|
||||
- **BomItem**: BOM物料项数据模型
|
||||
|
||||
### 相关模块
|
||||
- **BIPUploadModule**: 生成BIP上传模板
|
||||
- 读取"部件优先"字段
|
||||
- 当值为"否"时排除"部件"类别物料
|
||||
- 与本模块功能互补
|
||||
|
||||
---
|
||||
|
||||
## 算法复杂度
|
||||
|
||||
| 操作 | 时间复杂度 | 说明 |
|
||||
|------|-----------|------|
|
||||
| LoadInventoryData | O(n) | n = 库存行数 |
|
||||
| LoadOrderData | O(m) | m = 订单行数 |
|
||||
| ParseAllOrdersBOM | O(m × p) | p = 平均BOM物料数 |
|
||||
| CalculateComponentDemand | O(m) | 遍历订单统计需求 |
|
||||
| ValidateInventory | O(k) | k = 不同部件数 |
|
||||
| AllocateInventory | O(m) | 遍历订单分配库存 |
|
||||
|
||||
**总体复杂度**: O(m × p),主要由BOM解析决定
|
||||
|
||||
---
|
||||
|
||||
## 版本历史
|
||||
|
||||
| 版本 | 日期 | 说明 |
|
||||
|------|------|------|
|
||||
| 1.0 | 2026-02-03 | 初始版本,实现部件库存核对功能 |
|
||||
@@ -1,145 +0,0 @@
|
||||
# Preprocessing Implementation Summary
|
||||
|
||||
## Implementation Complete ✓
|
||||
|
||||
The preprocessing functionality for BOM conditions has been successfully implemented according to the plan.
|
||||
|
||||
## Files Created
|
||||
|
||||
### 1. M05_PreProcessor.bas
|
||||
**Location**: `VBA/Modules/M05_PreProcessor.bas`
|
||||
|
||||
**Key Features**:
|
||||
- Loads mapping tables from [对照表] worksheet
|
||||
- lcfw mapping from columns A:B (rows 3+)
|
||||
- azxs mapping from columns D:E (rows 3+)
|
||||
- Preprocesses conditions for "接头" category only
|
||||
- Replaces values using mappings
|
||||
- Merges duplicate OR conditions
|
||||
- Comprehensive error handling with logging
|
||||
|
||||
**Public Functions**:
|
||||
- `InitPreProcessor(logger, wsMapping)` - Initialize the preprocessor
|
||||
- `IsInitialized()` - Check initialization status
|
||||
- `PreprocessCondition(strCondition, strCategory, rowIdx)` - Main entry point
|
||||
- `GetLcfwMappedValue(key)` - Test helper for lcfw mapping
|
||||
- `GetAzxsMappedValue(key)` - Test helper for azxs mapping
|
||||
|
||||
## Files Modified
|
||||
|
||||
### 1. M01_Main.bas
|
||||
**Changes**:
|
||||
- Added preprocessor initialization (lines 43-53)
|
||||
- Checks for [对照表] worksheet
|
||||
- Shows warning if worksheet not found
|
||||
- Initializes M05_PreProcessor
|
||||
- Added preprocessing call (lines 81-84)
|
||||
- Preprocesses conditions before parsing
|
||||
- Only for "接头" category
|
||||
- Only when preprocessor is initialized
|
||||
|
||||
### 2. M99_TestRunner.bas
|
||||
**Changes**:
|
||||
- Added module-level variable for test mapping worksheet
|
||||
- Added 8 comprehensive test cases:
|
||||
- `Test_PP_01_AzxsMappingLoad` - Tests all 12 azxs values
|
||||
- `Test_PP_02_LcfwMappingLoad` - Tests lcfw mapping
|
||||
- `Test_PP_03_AzxsReplacement` - Tests azxs value replacement
|
||||
- `Test_PP_04_LcfwReplacement` - Tests lcfw value replacement
|
||||
- `Test_PP_05_ORMerging` - Tests OR condition merging
|
||||
- `Test_PP_06_FullIntegration` - Tests integration with M03_Logic
|
||||
- `Test_PP_07_NonJointCategory` - Tests non-"接头" categories
|
||||
- `Test_PP_08_UnmappedValues` - Tests unmapped value handling
|
||||
- Added `SetupPreProcessorTest()` helper function
|
||||
|
||||
## Value Mappings
|
||||
|
||||
### azxs Mapping (12 values)
|
||||
**径向** (3 values): A0, AT, AH
|
||||
**下轴向** (4 values): B0, BT, BZ, BH
|
||||
**中轴向** (4 values): Z0, ZT, ZZ, ZH
|
||||
|
||||
### lcfw Mapping
|
||||
**低压**: M01 through M11
|
||||
|
||||
## How to Test
|
||||
|
||||
### Manual Testing
|
||||
1. Open `YTHN-100.xlsm` in Excel
|
||||
2. Ensure [对照表] worksheet exists with proper mapping data:
|
||||
- Columns A:B: lcfw mapping (M01-M11 → 低压)
|
||||
- Columns D:E: azxs mapping (A0/AT/AH → 径向, etc.)
|
||||
3. Ensure [平台配置清单] has "接头" category data
|
||||
4. Run `M01_Main.RunBOMConversion()`
|
||||
5. Verify output:
|
||||
- azxs values are transformed (e.g., A0 → 径向)
|
||||
- lcfw values are transformed (e.g., M01 → 低压)
|
||||
- duplicate OR conditions are merged
|
||||
- non-"接头" categories are unchanged
|
||||
|
||||
### Automated Testing
|
||||
1. Open VBA Editor (Alt+F11)
|
||||
2. Open Immediate Window (Ctrl+G)
|
||||
3. Run `RunAllTests`
|
||||
4. Verify all tests pass
|
||||
|
||||
## Test Coverage Checklist
|
||||
- [x] All 12 azxs values tested (A0, AT, AH, B0, BT, BZ, BH, Z0, ZT, ZZ, ZH)
|
||||
- [x] lcfw mapping tested (M01-M11 → 低压)
|
||||
- [x] OR merging tested with various scenarios
|
||||
- [x] Integration with M03_Logic tested
|
||||
- [x] Non-"接头" category tested
|
||||
- [x] Error handling tested (unmapped values, missing worksheet)
|
||||
|
||||
## Architecture Notes
|
||||
|
||||
### Late Binding
|
||||
- Uses `CreateObject("Scripting.Dictionary")` to avoid external reference dependencies
|
||||
- Consistent with existing M03_Logic.bas pattern
|
||||
|
||||
### Error Handling
|
||||
- Graceful degradation if [对照表] is missing
|
||||
- Warning messages for unmapped values
|
||||
- Non-blocking errors (processing continues)
|
||||
|
||||
### String Processing
|
||||
- Parentheses-aware splitting for OR/AND operators
|
||||
- Whitespace normalization for duplicate detection
|
||||
- Preserves original condition structure
|
||||
|
||||
## Integration Points
|
||||
|
||||
1. **M01_Main.bas** (lines 43-53, 81-84)
|
||||
- Initializes preprocessor after M03_Logic
|
||||
- Calls preprocessor before ParseRule
|
||||
|
||||
2. **M03_Logic.bas**
|
||||
- Receives preprocessed conditions
|
||||
- No changes required
|
||||
|
||||
3. **clsErrorLogger.cls**
|
||||
- Logs preprocessing warnings
|
||||
- No changes required
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Test with real data** - Run manual testing with actual BOM data
|
||||
2. **Verify output** - Check that transformed values match expected results
|
||||
3. **Update documentation** - Update CLAUDE.md if needed to reflect preprocessing functionality
|
||||
4. **Commit changes** - Create git commit with implementation
|
||||
|
||||
## Git Commit Message
|
||||
|
||||
```
|
||||
feat: add preprocessing for BOM conditions in "接头" category
|
||||
|
||||
- Add M05_PreProcessor module for value mapping and OR merging
|
||||
- Map azxs values (A0/AT/AH→径向, B0/BT/BZ/BH→下轴向, Z0/ZT/ZZ/ZH→中轴向)
|
||||
- Map lcfw values (M01-M11→低压)
|
||||
- Merge duplicate OR conditions automatically
|
||||
- Add comprehensive unit tests (8 test cases)
|
||||
- Integrate with M01_Main workflow
|
||||
- Graceful degradation if [对照表] is missing
|
||||
|
||||
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
||||
```
|
||||
@@ -1,236 +0,0 @@
|
||||
# 库存校验功能集成总结
|
||||
|
||||
## 实施日期
|
||||
2026-02-24
|
||||
|
||||
## 功能概述
|
||||
在 `RunBOMExtraction` 过程中集成了库存校验功能,针对部件物料进行库存检查。根据订单顺序累加需求量,当库存充足时使用部件,不足时使用子件(接头+弹性元件)。
|
||||
|
||||
## 修改的文件
|
||||
|
||||
### 1. M04_Config.bas
|
||||
**修改内容:** 添加现存量工作表配置常量
|
||||
|
||||
```vba
|
||||
' 现存量工作表配置常量
|
||||
Public Const INVENTORY_SHEET_NAME As String = "现存量"
|
||||
Public Const INVENTORY_HEADER_ROW As Long = 3 ' 表头在第3行
|
||||
Public Const INVENTORY_COL_CODE As String = "B" ' B列 = 物料编码
|
||||
Public Const INVENTORY_COL_QTY As String = "J" ' J列 = 库存数量
|
||||
```
|
||||
|
||||
### 2. M08_ComponentProcessor.bas
|
||||
**修改内容:**
|
||||
|
||||
#### 2.1 添加库存追踪模块级变量
|
||||
```vba
|
||||
' 模块级变量 - 库存追踪
|
||||
Private g_InventoryDict As Object ' 部件编码 -> 现存量
|
||||
Private g_AccumulatedDemandDict As Object ' 部件编码 -> 累计需求量
|
||||
```
|
||||
|
||||
#### 2.2 新增 `LoadInventoryData` 函数
|
||||
- 从[现存量]工作表加载库存数据
|
||||
- 表头在第3行,从第4行开始读取
|
||||
- B列 = 物料编码,J列 = 库存数量
|
||||
- 如果未找到工作表,记录警告并使用空字典(所有库存视为0)
|
||||
|
||||
#### 2.3 新增 `InitComponentProcessorWithInventory` 函数
|
||||
- 初始化部件处理器,同时加载库存数据
|
||||
- 创建累计需求量字典
|
||||
- 调用 `LoadInventoryData` 加载库存
|
||||
|
||||
#### 2.4 重写 `CheckComponentInventory` 函数
|
||||
**输入参数:**
|
||||
- `wsComponent` - "部件"工作表
|
||||
- `rowNum` - 匹配到的行号
|
||||
- `headerMap` - 表头映射
|
||||
- `orderQty` - 订单数量(新增)
|
||||
|
||||
**逻辑流程:**
|
||||
1. 提取部件编码
|
||||
2. 获取BOM需求量(部件工作表中的数量列)
|
||||
3. 检查库存数据是否存在
|
||||
4. 计算累计需求量 = 订单数量 × BOM需求量 + 之前累计需求
|
||||
5. 比较库存和需求:
|
||||
- 库存 >= 累计需求 → 返回 True,更新累计需求量
|
||||
- 库存 < 累计需求 → 返回 False,记录错误
|
||||
|
||||
#### 2.5 修改 `ProcessComponentRecord` 函数签名
|
||||
**新增参数:** `orderQty As Long`
|
||||
|
||||
**修改调用:** 传递 `orderQty` 给 `CheckComponentInventory`
|
||||
|
||||
### 3. M09_BOMExtractor.bas
|
||||
**修改内容:**
|
||||
|
||||
#### 3.1 修改 `ReadInputModels` 函数
|
||||
**当前实现:** 读取2列(生产订单号、产品型号)
|
||||
|
||||
**注意:** 计划中提到读取3列(包括数量),但当前代码仍使用 `ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, colIdx)).Value`,需要确保 `colIdx` 变量包含第3列(数量列)。
|
||||
|
||||
**建议修改:**
|
||||
```vba
|
||||
' 确保读取到第3列(数量)
|
||||
ReadInputModels = ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, 3)).Value
|
||||
```
|
||||
|
||||
#### 3.2 修改 `RunBOMExtraction` 主循环
|
||||
**修改点1:** 提取数量列
|
||||
```vba
|
||||
Dim orderQty As Long
|
||||
orderQty = CLng(inputModels(i, 3)) ' Column C: 数量
|
||||
```
|
||||
|
||||
**修改点2:** 初始化部件处理器时传递库存工作簿
|
||||
```vba
|
||||
Dim invWorkbook As Workbook
|
||||
Set invWorkbook = ThisWorkbook ' 现存量在主工作簿中
|
||||
M08_ComponentProcessor.InitComponentProcessorWithInventory g_Logger, invWorkbook
|
||||
```
|
||||
|
||||
**修改点3:** 传递数量给 `ProcessSingleModel`
|
||||
```vba
|
||||
Set modelResults = ProcessSingleModel(modelString, g_BOMWorkbook, g_Logger, productionOrderNo, orderQty)
|
||||
```
|
||||
|
||||
#### 3.3 修改 `ProcessSingleModel` 函数签名
|
||||
**新增参数:** `orderQty As Long`
|
||||
|
||||
**修改调用:** 传递 `orderQty` 给 `MatchAllMaterialTypesWithValidation`
|
||||
|
||||
#### 3.4 修改 `MatchAllMaterialTypesWithValidation` 函数签名
|
||||
**新增参数:** `orderQty As Long`
|
||||
|
||||
**修改调用:** 传递 `orderQty` 给 `ProcessComponentRecord`
|
||||
|
||||
## 数据流程
|
||||
|
||||
### 输入数据
|
||||
**[产品型号]工作表:**
|
||||
| 生产订单号 | 产品型号 | 数量 |
|
||||
|-----------|---------|------|
|
||||
| PO-001 | YTHN-...| 2 |
|
||||
| PO-003 | YTHN-...| 2 |
|
||||
| PO-006 | YTHN-...| 2 |
|
||||
|
||||
**[现存量]工作表:**
|
||||
(第3行表头)
|
||||
| ... | 物料编码 | ... | 库存数量 |
|
||||
| ... | COMP001 | ... | 5 |
|
||||
|
||||
### 处理逻辑
|
||||
1. **初始化阶段:**
|
||||
- `InitComponentProcessorWithInventory` 加载库存数据到 `g_InventoryDict`
|
||||
- 创建空的 `g_AccumulatedDemandDict` 用于追踪累计需求
|
||||
|
||||
2. **订单处理阶段(按顺序):**
|
||||
- PO-001: 累计需求 = 2×1 + 0 = 2,库存5 >= 2 ✓ → 返回部件
|
||||
- PO-003: 累计需求 = 2×1 + 2 = 4,库存5 >= 4 ✓ → 返回部件
|
||||
- PO-006: 累计需求 = 2×1 + 4 = 6,库存5 < 6 ✗ → 返回子件(接头+弹性元件)
|
||||
|
||||
3. **错误记录:**
|
||||
- 库存不足时记录错误(Blocking Error)
|
||||
- 未找到库存数据时记录警告(Non-blocking Warning)
|
||||
|
||||
### 输出结果
|
||||
**BOM提取结果:**
|
||||
| 生产订单号 | 原始产品型号 | 物料类型 | 物料名称 | 物料编码 | 物料数量 | 提取备注 |
|
||||
|-----------|-------------|---------|---------|---------|---------|---------|
|
||||
| PO-001 | YTHN-... | 部件 | 部件A | COMP001 | 1 | |
|
||||
| PO-003 | YTHN-... | 部件 | 部件A | COMP001 | 1 | |
|
||||
| PO-006 | YTHN-... | 接头 | 接头B | JOINT01 | 1 | 部件无库存,使用子件 |
|
||||
| PO-006 | YTHN-... | 弹性元件| 元件C | ELEM01 | 1 | 部件无库存,使用子件 |
|
||||
|
||||
## 测试要点
|
||||
|
||||
### 功能测试
|
||||
1. **正常库存场景:**
|
||||
- 准备测试数据:3个订单,库存=5
|
||||
- 验证前2个订单返回部件
|
||||
- 验证第3个订单返回子件
|
||||
|
||||
2. **边界测试:**
|
||||
- 库存=0,所有订单应返回子件
|
||||
- 库存充足(>=累计需求),所有订单返回部件
|
||||
- 库存恰好等于累计需求,应返回部件
|
||||
|
||||
3. **异常测试:**
|
||||
- 现存量工作表不存在 → 记录警告,所有订单返回子件
|
||||
- 部件编码在现存量中不存在 → 记录警告,该订单返回子件
|
||||
- 数量列为空或非数字 → 应有错误处理
|
||||
|
||||
### 数据验证
|
||||
1. **累计需求计算:**
|
||||
- 验证累计需求 = 订单数量 × BOM需求量 + 之前累计
|
||||
- 验证每次处理后累计需求量正确更新
|
||||
|
||||
2. **错误报告:**
|
||||
- 检查错误报告工作表是否生成
|
||||
- 验证库存不足错误正确记录
|
||||
- 验证警告正确记录
|
||||
|
||||
## 注意事项
|
||||
|
||||
### 关键假设
|
||||
1. **[现存量]工作表结构:**
|
||||
- 表头在第3行
|
||||
- 数据从第4行开始
|
||||
- B列 = 物料编码
|
||||
- J列 = 库存数量
|
||||
|
||||
2. **[产品型号]工作表结构:**
|
||||
- A列 = 生产订单号
|
||||
- B列 = 产品型号
|
||||
- C列 = 数量
|
||||
|
||||
3. **BOM库[部件]工作表:**
|
||||
- 必须包含"编码"列(部件编码)
|
||||
- 必须包含"数量"列(BOM需求量)
|
||||
|
||||
### 已知限制
|
||||
1. **库存检查时机:**
|
||||
- 库存在初始化时加载一次
|
||||
- 处理过程中库存不更新(不考虑库存增加)
|
||||
|
||||
2. **错误处理:**
|
||||
- 未找到库存数据时返回 False(使用子件)
|
||||
- 不会中断整个流程,继续处理下一个订单
|
||||
|
||||
3. **数量列处理:**
|
||||
- 当前代码假设数量列总是存在且为数字
|
||||
- 如果数量列为空或非数字,可能导致运行时错误
|
||||
|
||||
## 后续改进建议
|
||||
|
||||
1. **增强错误处理:**
|
||||
- 在 `ReadInputModels` 中验证数量列是否存在
|
||||
- 处理数量列为空或非数字的情况
|
||||
|
||||
2. **性能优化:**
|
||||
- 如果库存数据很大,考虑只加载需要的部件编码
|
||||
- 添加日志记录库存使用情况
|
||||
|
||||
3. **功能扩展:**
|
||||
- 支持库存实时更新(如果需要)
|
||||
- 支持按批次或其他维度分组计算需求
|
||||
- 添加库存预留功能(预留库存给特定订单)
|
||||
|
||||
## 相关文档
|
||||
- BOM提取系统架构:`CLAUDE.md`
|
||||
- 库存校验计划:原始计划文档
|
||||
- 测试用例:需要单独创建
|
||||
|
||||
## 实施验证清单
|
||||
- [x] M04_Config.bas 添加常量
|
||||
- [x] M08_ComponentProcessor.bas 添加变量
|
||||
- [x] M08_ComponentProcessor.bas 实现 LoadInventoryData
|
||||
- [x] M08_ComponentProcessor.bas 重写 CheckComponentInventory
|
||||
- [x] M08_ComponentProcessor.bas 添加 InitComponentProcessorWithInventory
|
||||
- [x] M08_ComponentProcessor.bas 修改 ProcessComponentRecord 签名
|
||||
- [x] M09_BOMExtractor.bas 修改 RunBOMExtraction(初始化、提取数量)
|
||||
- [x] M09_BOMExtractor.bas 修改 ProcessSingleModel(添加参数)
|
||||
- [x] M09_BOMExtractor.bas 修改 MatchAllMaterialTypesWithValidation(添加参数)
|
||||
- [ ] 验证 ReadInputModels 读取3列(需要确认)
|
||||
- [ ] 端到端测试
|
||||
- [ ] 错误场景测试
|
||||
@@ -1,161 +0,0 @@
|
||||
# Fix: Multiple Match Materials Now Appear in BOM Output
|
||||
|
||||
## Problem
|
||||
|
||||
When a product model matched **multiple materials of the same type** (e.g., 2 records in "机芯" sheet), those materials **did NOT appear** in the [BOM提取结果] worksheet. Only an error appeared in the error report.
|
||||
|
||||
**Expected**: All matched materials should appear with error message in "提取备注" column.
|
||||
**Actual**: Materials were completely missing from output.
|
||||
|
||||
## Root Cause
|
||||
|
||||
**File**: `VBA_BOMConverter/Modules/M09_BOMExtractor.bas`
|
||||
**Function**: `MatchAllMaterialTypesWithValidation()`
|
||||
**Lines**: 496-515 (before fix)
|
||||
|
||||
### Bug Description
|
||||
|
||||
The original code only extracted materials when `bomMatchResult("success") = True`:
|
||||
|
||||
```vba
|
||||
' OLD BUGGY CODE
|
||||
If bomMatchResult("success") Then
|
||||
rowNum = bomMatchResult("rowNums")(1) ' Only first match
|
||||
Set materialInfo = M07_BOMMatcher.ExtractMaterialInfo(ws, rowNum, headerMap)
|
||||
matchResult("materials").Add materialInfo ' Only one material
|
||||
End If
|
||||
```
|
||||
|
||||
**Problem Flow**:
|
||||
1. M07_BOMMatcher finds 2 matching rows → Returns `success=False, rowCount=2, rowNums={row5, row8}`
|
||||
2. Line 496 checks `If bomMatchResult("success")` → **FALSE** (because rowCount > 1)
|
||||
3. Lines 497-514 **SKIPPED** → No materials extracted
|
||||
4. `matchResult("materials")` remains **EMPTY**
|
||||
5. Phase 3 tries to collect materials but finds empty collection
|
||||
6. **Result**: No materials in output, only error in error report
|
||||
|
||||
## Solution
|
||||
|
||||
Changed the condition from checking `success` to checking `rowCount > 0`, and added a loop to process **all matching rows**.
|
||||
|
||||
### Code Changes
|
||||
|
||||
**File**: `VBA_BOMConverter/Modules/M09_BOMExtractor.bas`
|
||||
**Lines**: 495-521
|
||||
|
||||
```vba
|
||||
' NEW FIXED CODE
|
||||
' 提取物料信息(支持多条匹配)
|
||||
If bomMatchResult("rowCount") > 0 Then
|
||||
' 步骤1: 构建表头映射(只需构建一次)
|
||||
Dim headerMap As Object
|
||||
Set headerMap = M07_BOMMatcher.BuildWorksheetHeaderMap(ws)
|
||||
|
||||
' 步骤2: 遍历所有匹配行
|
||||
Dim i As Long
|
||||
Dim rowNum As Long
|
||||
For i = 1 To bomMatchResult("rowCount")
|
||||
rowNum = bomMatchResult("rowNums")(i)
|
||||
Debug.Print " -> 处理匹配行 " & i & "/" & bomMatchResult("rowCount") & ": 行号=" & rowNum
|
||||
|
||||
' 步骤3: 提取物料信息
|
||||
Dim materialInfo As Object
|
||||
Set materialInfo = M07_BOMMatcher.ExtractMaterialInfo(ws, rowNum, headerMap)
|
||||
|
||||
If Not materialInfo Is Nothing Then
|
||||
matchResult("materials").Add materialInfo
|
||||
Debug.Print " 已添加: 名称=[" & materialInfo("materialName") & "] 编码=[" & materialInfo("materialCode") & "]"
|
||||
Else
|
||||
Debug.Print " ERROR: materialInfo为Nothing"
|
||||
End If
|
||||
Next i
|
||||
|
||||
Debug.Print " -> 共添加 " & matchResult("materials").Count & " 个物料"
|
||||
End If
|
||||
```
|
||||
|
||||
### Key Improvements
|
||||
|
||||
1. **Changed condition**: `If bomMatchResult("success")` → `If bomMatchResult("rowCount") > 0`
|
||||
- Now processes materials even when there are multiple matches (success=False)
|
||||
|
||||
2. **Added loop**: `For i = 1 To bomMatchResult("rowCount")`
|
||||
- Processes ALL matching rows instead of just the first one
|
||||
|
||||
3. **Optimized performance**: Moved `BuildWorksheetHeaderMap()` outside the loop
|
||||
- Build header map once, reuse for all rows in the same worksheet
|
||||
|
||||
4. **Enhanced logging**: Added detailed debug prints
|
||||
- Shows which row is being processed
|
||||
- Shows total count of materials added
|
||||
|
||||
## Test Cases
|
||||
|
||||
### 1. Single Match Scenario (Current Working Case)
|
||||
- **Input**: Product matches exactly 1 record in "机芯" sheet
|
||||
- **Expected**: 1 material row appears in output with no error message
|
||||
- **Status**: ✅ Should still work (rowCount=1, loop runs once)
|
||||
|
||||
### 2. Multiple Match Scenario (Fixed Bug)
|
||||
- **Input**: Product matches 2 records in "机芯" sheet
|
||||
- **Expected**:
|
||||
- 2 material rows appear in [BOM提取结果]
|
||||
- Both show "机芯 匹配到2条记录" in 提取备注 column
|
||||
- Error report also logs the error
|
||||
- **Status**: ✅ Now fixed (loop runs twice)
|
||||
|
||||
### 3. No Match Scenario
|
||||
- **Input**: Product matches 0 records in "机芯" sheet
|
||||
- **Expected**: Error message in error report, no materials for that type
|
||||
- **Status**: ✅ Should still work (rowCount=0, loop doesn't run)
|
||||
|
||||
### 4. Component Sheet Scenario
|
||||
- **Input**: Product matches component sheet (uses different code path)
|
||||
- **Expected**: No regression, component handling still works correctly
|
||||
- **Status**: ✅ Not affected (uses lines 452-484, different code path)
|
||||
|
||||
## Impact Analysis
|
||||
|
||||
### Affected Components:
|
||||
- ✅ **"机芯" (Movement)** - Primary fix target
|
||||
- ✅ **"边" (Edge)** - Will also benefit from fix
|
||||
- ✅ **Any other non-special sheets** - All use this code path
|
||||
|
||||
### Not Affected:
|
||||
- ✅ **"部件" (Component)** - Uses separate code path (lines 452-484)
|
||||
- ✅ **"接头" (Joint) / "弹性元件" (Element)** from component sheet
|
||||
|
||||
### Risk Assessment: **LOW**
|
||||
- Change is isolated to one specific code block
|
||||
- Only affects material collection logic when rowCount > 0
|
||||
- Component sheet (complex logic) uses different code path
|
||||
- Single-match case (rowCount = 1) still works the same way
|
||||
- No change to validation logic or error reporting
|
||||
|
||||
## Benefits
|
||||
|
||||
1. **Visibility**: Users can now see ALL matched materials when ambiguity occurs
|
||||
2. **Debugging**: Easier to identify why multiple matches occurred
|
||||
3. **Manual Resolution**: Users can manually select correct material from output
|
||||
4. **Data Quality**: Highlights ambiguous BOM library entries that need cleanup
|
||||
|
||||
## Verification Steps
|
||||
|
||||
1. Open YTHN-100.xlsm workbook
|
||||
2. Ensure BOM库.xlsx exists with test data (create 2 records in "机芯" sheet with same matching criteria)
|
||||
3. Run `M09_BOMExtractor.RunBOMExtraction()`
|
||||
4. Check [BOM提取结果] worksheet:
|
||||
- ✅ Both matched materials should appear
|
||||
- ✅ "提取备注" column should show "机芯 匹配到2条记录" on both rows
|
||||
- ✅ Error report should also log the error
|
||||
5. Verify single-match sheets still work correctly
|
||||
|
||||
## Related Files
|
||||
|
||||
- **Modified**: `VBA_BOMConverter/Modules/M09_BOMExtractor.bas` (lines 495-521)
|
||||
- **Related**: `VBA_BOMConverter/Modules/M07_BOMMatcher.bas` (no changes needed)
|
||||
- **Related**: `VBA_BOMConverter/Modules/M08_ComponentProcessor.bas` (no changes needed)
|
||||
|
||||
## Date
|
||||
|
||||
**Implemented**: 2025-02-26
|
||||
File diff suppressed because it is too large
Load Diff
391
docs/执行计划-部件数量核对.md
Normal file
391
docs/执行计划-部件数量核对.md
Normal file
@@ -0,0 +1,391 @@
|
||||
# 执行计划:部件数量核对功能
|
||||
|
||||
## 一、需求概述
|
||||
|
||||
### 1.1 功能描述
|
||||
自动核对产品订单中"部件"类物料的库存情况,当库存不足时,按订单顺序将超出部分的订单的"部件优先"字段标记为"否"。
|
||||
|
||||
### 1.2 数据源
|
||||
|
||||
#### [产品订单]工作表
|
||||
- **表头位置**:第1行
|
||||
- **数据起始行**:第2行
|
||||
- **关键列**:
|
||||
- B列:产品型号
|
||||
- C列:产品数量
|
||||
- E列:部件优先(输出字段)
|
||||
|
||||
#### [现存量]工作表
|
||||
- **表头位置**:第3行
|
||||
- **数据起始行**:第4行
|
||||
- **关键列**:
|
||||
- B列:物料编码(对应BOM中的66代码)
|
||||
- J列:结存主数量(库存数量)
|
||||
|
||||
---
|
||||
|
||||
## 二、业务逻辑
|
||||
|
||||
### 2.1 核心流程
|
||||
|
||||
```
|
||||
1. 读取所有订单 → 2. 解析型号提取BOM → 3. 识别部件物料
|
||||
↓
|
||||
4. 统计部件总需求 → 5. 查询库存 → 6. 库存充足性检查
|
||||
↓
|
||||
7. 按订单顺序分配库存 → 8. 修改部件优先字段
|
||||
```
|
||||
|
||||
### 2.2 计算规则
|
||||
|
||||
#### 部件总需求量
|
||||
```
|
||||
部件总需求 = Σ(订单i的BOM中部件数量 × 订单i的产品数量)
|
||||
```
|
||||
|
||||
#### 示例
|
||||
```
|
||||
订单1:产品数量=2,部件A的BOM数量=1 → 需求 = 1 × 2 = 2
|
||||
订单2:产品数量=2,部件A的BOM数量=1 → 需求 = 1 × 2 = 2
|
||||
订单3:产品数量=2,部件A的BOM数量=1 → 需求 = 1 × 2 = 2
|
||||
--------------------------------------------------------
|
||||
部件A总需求 = 2 + 2 + 2 = 6
|
||||
部件A库存 = 5
|
||||
库存不足 = 6 - 5 = 1
|
||||
```
|
||||
|
||||
### 2.3 分配策略
|
||||
|
||||
**原则**:按订单自上而下的顺序分配库存
|
||||
|
||||
| 订单 | 需求量 | 分配前库存 | 分配后库存 | 部件优先 |
|
||||
|------|--------|-----------|-----------|---------|
|
||||
| 订单1 | 2 | 5 | 3 | 保持不变 |
|
||||
| 订单2 | 2 | 3 | 1 | 保持不变 |
|
||||
| 订单3 | 2 | 1 | -1(不足) | **改为"否"** |
|
||||
|
||||
---
|
||||
|
||||
## 三、处理规则
|
||||
|
||||
### 3.1 正常情况
|
||||
- 订单包含"部件"物料,库存充足 → 保持原值
|
||||
- 订单包含"部件"物料,库存不足 → 改为"否"
|
||||
- 订单不包含"部件"物料 → **保持原值不变**
|
||||
|
||||
### 3.2 异常情况
|
||||
- **部件在[现存量]中找不到** → 报错提示,终止处理
|
||||
- **型号解析失败** → 跳过该订单,记录错误
|
||||
- **未匹配到任何物料** → 跳过该订单(无部件)
|
||||
|
||||
### 3.3 字段修改规则
|
||||
- **基于现有值进行修改**(不清空原有值)
|
||||
- **只修改E列"部件优先"字段**
|
||||
- 修改格式:`是`、`否`、`1`、`0`、`TRUE`、`FALSE` 均可识别
|
||||
|
||||
---
|
||||
|
||||
## 四、数据结构设计
|
||||
|
||||
### 4.1 订单数据结构
|
||||
```vba
|
||||
Type OrderInfo
|
||||
RowNumber As Long ' 行号
|
||||
ProductModel As String ' 产品型号
|
||||
Quantity As Long ' 产品数量
|
||||
ComponentCode As String ' 部件66编码
|
||||
ComponentQty As Double ' 部件BOM数量
|
||||
HasComponent As Boolean ' 是否包含部件
|
||||
ParseError As String ' 解析错误信息
|
||||
End Type
|
||||
```
|
||||
|
||||
### 4.2 部件库存结构
|
||||
```vba
|
||||
Type ComponentInventory
|
||||
ComponentCode As String ' 部件66编码
|
||||
TotalDemand As Double ' 总需求量
|
||||
AvailableStock As Double ' 可用库存
|
||||
IsShortage As Boolean ' 是否短缺
|
||||
End Type
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 五、实现方案
|
||||
|
||||
### 5.1 新建模块
|
||||
|
||||
**文件名**:`ComponentInventoryCheckModule.bas`
|
||||
|
||||
**主要过程**:
|
||||
```vba
|
||||
Public Sub CheckComponentInventory()
|
||||
' 主入口程序
|
||||
End Sub
|
||||
```
|
||||
|
||||
### 5.2 核心函数
|
||||
|
||||
#### 5.2.1 读取订单数据
|
||||
```vba
|
||||
Private Function LoadOrderData(ws As Worksheet) As Collection
|
||||
' 返回 Collection(Of OrderInfo)
|
||||
' 读取[产品订单]的B、C列数据
|
||||
End Function
|
||||
```
|
||||
|
||||
#### 5.2.2 读取库存数据
|
||||
```vba
|
||||
Private Function LoadInventoryData(ws As Worksheet) As Object
|
||||
' 返回 Dictionary(物料编码 -> 库存数量)
|
||||
' 读取[现存量]的B、J列数据
|
||||
End Function
|
||||
```
|
||||
|
||||
#### 5.2.3 解析订单BOM
|
||||
```vba
|
||||
Private Sub ParseOrderBOM(orderInfo As OrderInfo, _
|
||||
bomExtractor As BomExtractor, _
|
||||
parser As ProductModelParser)
|
||||
' 解析型号,提取BOM
|
||||
' 识别"部件"类别物料
|
||||
' 填充 orderInfo.ComponentCode 和 orderInfo.ComponentQty
|
||||
End Sub
|
||||
```
|
||||
|
||||
#### 5.2.4 统计部件需求
|
||||
```vba
|
||||
Private Function CalculateComponentDemand( _
|
||||
orders As Collection) As Object
|
||||
' 返回 Dictionary(部件编码 -> ComponentInventory)
|
||||
' 累加所有订单的部件需求量
|
||||
End Function
|
||||
```
|
||||
|
||||
#### 5.2.5 检查库存充足性
|
||||
```vba
|
||||
Private Sub ValidateInventory( _
|
||||
componentDemands As Object, _
|
||||
inventoryData As Object)
|
||||
' 检查每个部件的库存是否充足
|
||||
' 如果找不到或不足,报错提示
|
||||
End Sub
|
||||
```
|
||||
|
||||
#### 5.2.6 分配库存并标记
|
||||
```vba
|
||||
Private Sub AllocateInventory( _
|
||||
orders As Collection, _
|
||||
componentDemands As Object, _
|
||||
orderSheet As Worksheet)
|
||||
' 按订单顺序分配库存
|
||||
' 库存不足时,修改E列"部件优先"为"否"
|
||||
End Sub
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 六、详细实现步骤
|
||||
|
||||
### 6.1 主流程(CheckComponentInventory)
|
||||
|
||||
```
|
||||
1. 获取工作表对象
|
||||
├─ [产品订单]工作表
|
||||
├─ [现存量]工作表
|
||||
└─ [平台配置清单]工作表
|
||||
|
||||
2. 初始化BOM提取器
|
||||
└─ 加载BOM数据
|
||||
|
||||
3. 读取库存数据到字典
|
||||
└─ Dictionary(66编码 -> 库存数量)
|
||||
|
||||
4. 读取订单数据
|
||||
└─ Collection(Of OrderInfo)
|
||||
|
||||
5. 解析所有订单的BOM
|
||||
└─ 识别部件,填充ComponentCode和ComponentQty
|
||||
|
||||
6. 统计部件总需求
|
||||
└─ Dictionary(部件编码 -> ComponentInventory)
|
||||
|
||||
7. 验证库存
|
||||
└─ 检查部件是否存在于[现存量]中
|
||||
|
||||
8. 按订单顺序分配库存
|
||||
└─ 修改E列"部件优先"字段
|
||||
|
||||
9. 输出结果统计
|
||||
└─ MsgBox显示处理结果
|
||||
```
|
||||
|
||||
### 6.2 分配算法(伪代码)
|
||||
|
||||
```vba
|
||||
For Each order In orders
|
||||
If order.HasComponent Then
|
||||
Dim demand As ComponentInventory
|
||||
demand = componentDemands(order.ComponentCode)
|
||||
|
||||
Dim requiredQty As Double
|
||||
requiredQty = order.ComponentQty * order.Quantity
|
||||
|
||||
If demand.AvailableStock >= requiredQty Then
|
||||
' 库存充足,保持原值
|
||||
demand.AvailableStock = demand.AvailableStock - requiredQty
|
||||
Else
|
||||
' 库存不足,标记为"否"
|
||||
orderSheet.Cells(order.RowNumber, 5).Value = "否"
|
||||
demand.AvailableStock = demand.AvailableStock - requiredQty
|
||||
End If
|
||||
End If
|
||||
Next order
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 七、边界情况处理
|
||||
|
||||
### 7.1 无部件的订单
|
||||
```
|
||||
条件:订单的BOM中没有"类别=部件"的物料
|
||||
处理:跳过该订单,E列保持原值
|
||||
```
|
||||
|
||||
### 7.2 多个订单使用不同部件
|
||||
```
|
||||
订单1:部件A
|
||||
订单2:部件B
|
||||
处理:分别统计A和B的需求,独立核算库存
|
||||
```
|
||||
|
||||
### 7.3 库存为0的情况
|
||||
```
|
||||
条件:[现存量]中某部件的结存主数量 = 0
|
||||
处理:所有需要该部件的订单都标记为"否"
|
||||
```
|
||||
|
||||
### 7.4 产品数量为0的情况
|
||||
```
|
||||
条件:订单的C列产品数量 = 0
|
||||
处理:该订单的部件需求 = 0,不影响库存
|
||||
```
|
||||
|
||||
### 7.5 部件优先字段原有值
|
||||
```
|
||||
可能的值:"是"、"否"、1、0、TRUE、FALSE、空
|
||||
处理:基于现有值修改,不清空
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 八、测试用例
|
||||
|
||||
### 8.1 基本功能测试
|
||||
|
||||
| 测试场景 | 订单数 | 部件 | 总需求 | 库存 | 预期结果 |
|
||||
|---------|--------|------|--------|------|---------|
|
||||
| 库存充足 | 3 | A(1) | 6 | 10 | 全部保持原值 |
|
||||
| 库存不足 | 3 | A(1) | 6 | 5 | 第3个订单改为"否" |
|
||||
| 库存刚好 | 3 | A(1) | 6 | 6 | 全部保持原值 |
|
||||
| 无部件订单 | 2 | - | - | - | 保持原值 |
|
||||
| 库存为0 | 2 | A(1) | 4 | 0 | 全部改为"否" |
|
||||
|
||||
### 8.2 异常情况测试
|
||||
|
||||
| 测试场景 | 预期行为 |
|
||||
|---------|---------|
|
||||
| 部件在[现存量]中不存在 | 报错提示,终止处理 |
|
||||
| 型号解析失败 | 跳过该订单,记录错误 |
|
||||
| [产品订单]为空 | 提示无数据,退出 |
|
||||
| [现存量]为空 | 报错提示,退出 |
|
||||
|
||||
### 8.3 边界值测试
|
||||
|
||||
| 测试场景 | 输入值 | 预期结果 |
|
||||
|---------|--------|---------|
|
||||
| 产品数量为1 | 所有订单数量=1 | 正常计算 |
|
||||
| 产品数量为大数 | 订单数量=1000 | 正常计算 |
|
||||
| 部件BOM数量为小数 | 0.5 | 正确计算总需求 |
|
||||
| 库存数量为小数 | 2.5 | 正确判断库存 |
|
||||
|
||||
---
|
||||
|
||||
## 九、用户界面设计
|
||||
|
||||
### 9.1 执行入口
|
||||
|
||||
建议在主界面添加按钮:
|
||||
```
|
||||
[部件库存核对]
|
||||
```
|
||||
|
||||
### 9.2 结果提示
|
||||
|
||||
执行完成后显示:
|
||||
```
|
||||
✓ 部件库存核对完成!
|
||||
|
||||
处理订单数:10
|
||||
包含部件订单:8
|
||||
库存充足订单:6
|
||||
库存不足订单:2
|
||||
|
||||
耗时:0.50秒
|
||||
```
|
||||
|
||||
### 9.3 错误提示
|
||||
|
||||
格式:
|
||||
```
|
||||
✗ 部件库存核对失败!
|
||||
|
||||
错误信息:
|
||||
- 订单第5行:部件 '661234' 在[现存量]中未找到
|
||||
|
||||
请检查数据后重试。
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 十、实施步骤
|
||||
|
||||
1. **创建新模块**
|
||||
- 文件名:`ComponentInventoryCheckModule.bas`
|
||||
- 位置:`VBA/Modules/`
|
||||
|
||||
2. **实现核心函数**
|
||||
- LoadOrderData
|
||||
- LoadInventoryData
|
||||
- ParseOrderBOM
|
||||
- CalculateComponentDemand
|
||||
- ValidateInventory
|
||||
- AllocateInventory
|
||||
|
||||
3. **实现主流程**
|
||||
- CheckComponentInventory
|
||||
|
||||
4. **测试验证**
|
||||
- 基本功能测试
|
||||
- 异常情况测试
|
||||
- 边界值测试
|
||||
|
||||
5. **集成到主界面**
|
||||
- 添加按钮或菜单项
|
||||
|
||||
---
|
||||
|
||||
## 十一、风险评估
|
||||
|
||||
| 风险 | 影响 | 缓解措施 |
|
||||
|------|------|---------|
|
||||
| 库存数据不准确 | 导致错误的分配结果 | 执行前提示用户确认库存数据 |
|
||||
| 订单量大导致性能问题 | 处理时间长 | 优化算法,使用批量操作 |
|
||||
| BOM解析失败 | 无法识别部件 | 记录错误日志,跳过该订单 |
|
||||
| 部件编码不一致 | 无法匹配库存 | 严格验证,报错提示 |
|
||||
|
||||
---
|
||||
|
||||
**是否批准此执行计划?确认后我将开始代码实现。**
|
||||
199
docs/执行计划-附加功能支持.md
Normal file
199
docs/执行计划-附加功能支持.md
Normal file
@@ -0,0 +1,199 @@
|
||||
# 执行计划:添加附加功能(fjgn)支持
|
||||
|
||||
## 一、需求概述
|
||||
|
||||
### 1.1 新增条件字段
|
||||
- **条件代码**:`fjgn`
|
||||
- **条件名称**:`附加功能`
|
||||
- **提取位置**:从型号表头的[仪表特性]字段中提取(第6个位置,索引5)
|
||||
|
||||
### 1.2 型号结构
|
||||
```
|
||||
[型号]-[公称外径].[安装形式].[壳体形式].[过程连接&接液材质].[量程范围].[仪表特性]
|
||||
```
|
||||
|
||||
### 1.3 仪表特性解析规则
|
||||
- **充油类型**:位于最后,格式为 `Y`+一位数字(如 Y3)
|
||||
- **附加功能**:在充油类型前面的内容
|
||||
- 分隔符可能是 `,` 或 `.`(如 `N2,N3` 或 `N2.N3`)
|
||||
- 可能为空(如仪表特性只有 `Y3`)
|
||||
|
||||
### 1.4 示例
|
||||
| 型号 | 仪表特性 | 充油类型 | 附加功能 |
|
||||
|------|----------|----------|----------|
|
||||
| PYTHN-100.A0.541.M201.M06.N2,N3.Y3 | N2,N3.Y3 | Y3 | N2,N3 |
|
||||
| YTHN-100.A0.531.M201.M08.Y3 | Y3 | Y3 | (空) |
|
||||
|
||||
---
|
||||
|
||||
## 二、条件评估规则
|
||||
|
||||
### 2.1 fjgn 条件的特殊处理
|
||||
|
||||
| 条件表达式 | 型号中的 fjgn | 评估结果 | 说明 |
|
||||
|-----------|--------------|---------|------|
|
||||
| fjgn=N1 | N1,N2 | ✅ True | 包含 N1 |
|
||||
| fjgn=N1 | N2,N3 | ❌ False | 不包含 N1 |
|
||||
| fjgn=N1 | (空) | ❌ False | 空值不包含任何值 |
|
||||
| fjgn!=N1 | N1,N2 | ❌ False | 包含 N1,不满足!= |
|
||||
| fjgn!=N1 | N2,N3 | ✅ True | 不包含 N1 |
|
||||
| fjgn!=N1 | (空) | ✅ True | 空值不包含 N1 |
|
||||
|
||||
### 2.2 匹配逻辑
|
||||
- **等值匹配(fjgn=XX)**:fjgn 字符串中包含指定值即为真
|
||||
- **不等匹配(fjgn!=XX)**:fjgn 字符串中不包含指定值即为真
|
||||
- **空值处理**:空字符串不包含任何值
|
||||
|
||||
---
|
||||
|
||||
## 三、代码修改方案
|
||||
|
||||
### 3.1 ProductModelParser.cls
|
||||
|
||||
**修改位置**:`ParseHeader()` 方法
|
||||
|
||||
**新增内容**:
|
||||
```vba
|
||||
' 仪表特性 - 第6个位置(索引5),可能不存在
|
||||
If UBound(dotParts) >= 5 Then
|
||||
Dim instrumentFeature As String
|
||||
instrumentFeature = Trim(dotParts(5))
|
||||
|
||||
' 提取附加功能
|
||||
Dim fjgn As String
|
||||
fjgn = ExtractAdditionalFeatures(instrumentFeature)
|
||||
|
||||
pConditions.Add "fjgn", fjgn
|
||||
Else
|
||||
' 如果没有仪表特性字段,fjgn为空
|
||||
pConditions.Add "fjgn", ""
|
||||
End If
|
||||
```
|
||||
|
||||
**新增方法**:`ExtractAdditionalFeatures()`
|
||||
```vba
|
||||
Private Function ExtractAdditionalFeatures(instrumentFeature As String) As String
|
||||
' 1. 检查是否以Y+数字结尾(充油类型)
|
||||
Dim lastTwoChars As String
|
||||
If Len(instrumentFeature) >= 2 Then
|
||||
lastTwoChars = Right(instrumentFeature, 2)
|
||||
If UCase(Left(lastTwoChars, 1)) = "Y" And IsNumeric(Right(lastTwoChars, 1)) Then
|
||||
' 去掉充油类型
|
||||
instrumentFeature = Left(instrumentFeature, Len(instrumentFeature) - 2)
|
||||
instrumentFeature = Trim(instrumentFeature)
|
||||
End If
|
||||
End If
|
||||
|
||||
' 2. 处理可能的分隔符(,或.)
|
||||
' 将可能的.替换为,,统一处理
|
||||
instrumentFeature = Replace(instrumentFeature, ".", ",")
|
||||
|
||||
' 3. 去除可能的后缀分隔符
|
||||
If Right(instrumentFeature, 1) = "," Then
|
||||
instrumentFeature = Left(instrumentFeature, Len(instrumentFeature) - 1)
|
||||
End If
|
||||
|
||||
ExtractAdditionalFeatures = Trim(instrumentFeature)
|
||||
End Function
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3.2 BIPUploadModule.bas
|
||||
|
||||
**修改位置**:第12行常量定义
|
||||
|
||||
**修改内容**:
|
||||
```vba
|
||||
' 修改前
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围"
|
||||
|
||||
' 修改后
|
||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3.3 ConditionEvaluator.cls
|
||||
|
||||
**修改位置**:`EvaluateSingleCondition()` 方法
|
||||
|
||||
**修改内容**:在现有的 `=` 和 `!=` 运算符处理后,添加 fjgn 特殊处理
|
||||
|
||||
```vba
|
||||
' 在现有的 actualValue = Conditions(varName) 之后添加
|
||||
actualValue = Conditions(varName)
|
||||
|
||||
' fjgn 字段特殊处理(多值匹配)
|
||||
If varName = "fjgn" Then
|
||||
If operator = "=" Then
|
||||
' fjgn=N1:检查actualValue中是否包含value
|
||||
EvaluateSingleCondition = InStr(actualValue, value) > 0
|
||||
ElseIf operator = "!=" Then
|
||||
' fjgn!=N1:检查actualValue中是否不包含value
|
||||
EvaluateSingleCondition = InStr(actualValue, value) = 0
|
||||
End If
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' 其他字段使用原有逻辑
|
||||
EvaluateSingleCondition = (actualValue = value)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 四、测试用例
|
||||
|
||||
### 4.1 解析测试
|
||||
|
||||
| 测试型号 | 预期 fjgn | 预期其他条件 |
|
||||
|---------|----------|-------------|
|
||||
| PYTHN-100.A0.541.M201.M06.N2,N3.Y3 | N2,N3 | azxs=A0, bkxs=541, gclj=M20, jycz=1, lcfw=M06 |
|
||||
| YTHN-100.A0.531.M201.M08.Y3 | (空) | azxs=A0, bkxs=531, gclj=M20, jycz=1, lcfw=M08 |
|
||||
| BP-088.2312.M08.0A3.N1.N2.Y2 | N1,N2 | azxs=23, bkxs=12, gclj=M08, jycz=0, lcfw=A3 |
|
||||
| BP-088.2312.M08.0A3.Y2 | (空) | azxs=23, bkxs=12, gclj=M08, jycz=0, lcfw=A3 |
|
||||
|
||||
### 4.2 条件评估测试
|
||||
|
||||
| fjgn值 | 选用条件 | 预期结果 |
|
||||
|-------|---------|---------|
|
||||
| N1,N2 | fjgn=N1 | True |
|
||||
| N1,N2 | fjgn=N3 | False |
|
||||
| (空) | fjgn=N1 | False |
|
||||
| N1,N2 | fjgn!=N1 | False |
|
||||
| N1,N2 | fjgn!=N3 | True |
|
||||
| (空) | fjgn!=N1 | True |
|
||||
|
||||
---
|
||||
|
||||
## 五、实施步骤
|
||||
|
||||
1. **修改 ProductModelParser.cls**
|
||||
- 在 `ParseHeader()` 方法中添加仪表特性提取逻辑
|
||||
- 新增 `ExtractAdditionalFeatures()` 方法
|
||||
|
||||
2. **修改 BIPUploadModule.bas**
|
||||
- 更新 `CONDITION_CONFIG` 常量
|
||||
|
||||
3. **修改 ConditionEvaluator.cls**
|
||||
- 在 `EvaluateSingleCondition()` 方法中添加 fjgn 特殊处理逻辑
|
||||
|
||||
4. **测试验证**
|
||||
- 测试型号解析是否正确
|
||||
- 测试条件评估是否符合预期
|
||||
- 测试边界情况(空值、多个附加功能等)
|
||||
|
||||
---
|
||||
|
||||
## 六、风险评估
|
||||
|
||||
| 风险 | 影响 | 缓解措施 |
|
||||
|------|------|---------|
|
||||
| 旧型号缺少仪表特性字段 | 解析失败 | 判断字段是否存在,不存在时 fjgn 为空 |
|
||||
| 充油类型识别错误 | 提取错误 | 严格匹配 Y+一位数字的格式 |
|
||||
| 分隔符不一致 | 解析错误 | 统一将 `.` 替换为 `,` 处理 |
|
||||
| 条件评估逻辑错误 | 匹配错误 | 充分测试各种边界情况 |
|
||||
|
||||
---
|
||||
|
||||
**是否批准此执行计划?确认后我将开始代码实现。**
|
||||
444
docs/提取备注数据来源.md
Normal file
444
docs/提取备注数据来源.md
Normal file
@@ -0,0 +1,444 @@
|
||||
# 提取备注字段数据来源分析
|
||||
|
||||
**文档生成时间**: 2026-03-13
|
||||
**入口函数**: `MainModule.ProcessProductModels()`
|
||||
**输出位置**: `BOM 提取结果` 工作表的"提取备注"列(最后一列)
|
||||
|
||||
---
|
||||
|
||||
## 核心数据流图
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[ProcessProductModels<br/>主入口] --> B[ProcessSingleModel<br/>处理单个型号]
|
||||
|
||||
B --> C{解析产品型号<br/>parser.Parse}
|
||||
|
||||
C -->|解析失败 | D["extractNote = <br/>解析失败: + ErrorMessage"]
|
||||
C -->|解析成功 | E[BomExtractor.ExtractBom<br/>提取 BOM]
|
||||
|
||||
E --> F[DetermineRequiredCategories<br/>确定必需类别]
|
||||
F --> G[MatchItems<br/>匹配物料]
|
||||
|
||||
G --> H{匹配数量?}
|
||||
H -->|0 条 | I[不立即报错<br/>移交 ValidateResult]
|
||||
H -->|1 条 | J[正常添加到结果集]
|
||||
H -->|多条 | K["记录错误到 pErrorMessages<br/>类别 X 匹配到多条物料 N 条"]
|
||||
K --> L[设置 item.MatchError<br/>并添加所有匹配项]
|
||||
|
||||
I --> M[ValidateResult<br/>双向覆盖检查]
|
||||
L --> M
|
||||
J --> M
|
||||
|
||||
M --> N{必需类别存在?}
|
||||
N -->|被子类覆盖 | O[视为正常<br/>不报错]
|
||||
N -->|被父类覆盖 | O
|
||||
N -->|确实缺失 | P["记录错误<br/>必需类别 X 未匹配"]
|
||||
|
||||
O --> Q[GetErrorSummary<br/>汇总错误]
|
||||
P --> Q
|
||||
|
||||
Q --> R{bomErrors 为空?}
|
||||
R -->|非空 | S[extractNote = bomErrors]
|
||||
R -->|空 | T[extractNote 保持空]
|
||||
|
||||
S --> U{matchedItems 为空?}
|
||||
T --> U
|
||||
|
||||
U -->|是 | V{extractNote 为空?}
|
||||
U -->|否 | W[遍历每个 item]
|
||||
|
||||
V -->|是 | X["extractNote = <br/>未匹配到任何物料"]
|
||||
V -->|否 | Y[保持现有 extractNote]
|
||||
|
||||
X --> Z1[CreateOutputRowArray<br/>创建输出行]
|
||||
Y --> Z1
|
||||
|
||||
W --> AA[For Each item<br/>itemNote = extractNote]
|
||||
AA --> AB{item.MatchError<br/>非空?}
|
||||
AB -->|是 | AC["拼接:itemNote += <br/>; + MatchError"]
|
||||
AB -->|否 | AD[保持 itemNote]
|
||||
|
||||
AC --> AE[CreateOutputRowArray<br/>创建输出行]
|
||||
AD --> AE
|
||||
|
||||
Z1 --> AF[输出到 BOM 提取结果<br/>提取备注列]
|
||||
AE --> AF
|
||||
|
||||
style D fill:#ff6b6b
|
||||
style K fill:#ffa94d
|
||||
style P fill:#ff6b6b
|
||||
style X fill:#51cf66
|
||||
style AF fill:#339af0,color:#fff
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 详细数据来源
|
||||
|
||||
### 来源 1: 产品型号解析失败
|
||||
|
||||
**位置**: `MainModule.bas` 第 184-188 行
|
||||
**触发条件**: `ProductModelParser.Parse()` 返回 `False`
|
||||
**错误来源**: `ProductModelParser.ErrorMessage`
|
||||
|
||||
```vba
|
||||
' MainModule.bas:184-188
|
||||
If Not parser.Parse(modelString) Then
|
||||
' 解析失败
|
||||
extractNote = "解析失败:" & parser.ErrorMessage
|
||||
outputData.Add CreateOutputRowArray(..., extractNote, Nothing)
|
||||
Exit Sub
|
||||
End If
|
||||
```
|
||||
|
||||
**可能的错误消息**(来自 `ProductModelParser.cls`):
|
||||
|
||||
| 错误场景 | 错误消息示例 | 源码位置 |
|
||||
|---------|-------------|---------|
|
||||
| 缺少表头部分 | `型号格式错误:缺少表头部分` | Parse() L73 |
|
||||
| 缺少'-'分隔符 | `表头格式错误:缺少'-'分隔符` | ParseHeader() L108 |
|
||||
| 表头结构不完整 | `表头结构不完整:缺少必要字段` | ParseHeader() L114 |
|
||||
| 过程连接代码格式错误 | `过程连接代码格式错误:长度不足` | ExtractConnectionAndMaterial() L205 |
|
||||
| 最后一位不是数字 | `过程连接代码格式错误:最后一位不是数字` | ExtractConnectionAndMaterial() L213 |
|
||||
| 解析异常 | `解析表头异常:[VBA 错误描述]` | ParseHeader() ErrorHandler L131 |
|
||||
|
||||
---
|
||||
|
||||
### 来源 2: BOM 提取器错误汇总
|
||||
|
||||
**位置**: `MainModule.bas` 第 197-200 行
|
||||
**触发条件**: `BomExtractor.GetErrorSummary()` 返回非空字符串
|
||||
**错误来源**: `BomExtractor.pErrorMessages` 集合
|
||||
|
||||
```vba
|
||||
' MainModule.bas:197-200
|
||||
Dim bomErrors As String
|
||||
bomErrors = BomExtractor.GetErrorSummary
|
||||
If bomErrors <> "" Then
|
||||
extractNote = bomErrors
|
||||
End If
|
||||
```
|
||||
|
||||
**错误汇总逻辑**(`BomExtractor.cls` L471-481):
|
||||
|
||||
```vba
|
||||
Public Function GetErrorSummary() As String
|
||||
If pErrorMessages.Count = 0 Then
|
||||
GetErrorSummary = ""
|
||||
Else
|
||||
Dim result As String
|
||||
Dim msg As Variant
|
||||
For Each msg In pErrorMessages
|
||||
result = result & CStr(msg) & "; " ' 使用"; " 连接
|
||||
Next msg
|
||||
GetErrorSummary = result
|
||||
End If
|
||||
End Function
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 来源 3: 多匹配错误
|
||||
|
||||
**位置**: `BomExtractor.cls` 第 247-258 行
|
||||
**触发条件**: 同一类别匹配到多条物料
|
||||
**错误消息**: `类别 [X] 匹配到多条物料 (N 条)`
|
||||
|
||||
```vba
|
||||
' BomExtractor.cls:247-258
|
||||
ElseIf categoryMatches.Count = 1 Then
|
||||
' 正常:匹配到 1 条
|
||||
pMatchedItems.Add categoryMatches(1)
|
||||
Else
|
||||
' 异常:匹配到多条
|
||||
Dim multiMsg As String
|
||||
multiMsg = "类别 [" & category & "] 匹配到多条物料 (" & categoryMatches.Count & "条)"
|
||||
pErrorMessages.Add multiMsg
|
||||
|
||||
' 临时处理:输出所有匹配的
|
||||
Dim tempItem As BomItem
|
||||
For Each tempItem In categoryMatches
|
||||
tempItem.MatchError = multiMsg ' ← 设置到 item
|
||||
pMatchedItems.Add tempItem
|
||||
Next tempItem
|
||||
End If
|
||||
```
|
||||
|
||||
**特性**:
|
||||
- 错误同时添加到 `pErrorMessages`(进入 GetErrorSummary)
|
||||
- 同时设置到 `item.MatchError`(逐行附加)
|
||||
- 输出所有匹配项,但每条都带警告
|
||||
|
||||
---
|
||||
|
||||
### 来源 4: 必需类别缺失
|
||||
|
||||
**位置**: `BomExtractor.cls` 第 449-451 行
|
||||
**触发条件**: ValidateResult 检测到必需类别未匹配且无覆盖
|
||||
**错误消息**: `必需类别 [X] 未匹配`
|
||||
|
||||
```vba
|
||||
' BomExtractor.cls:449-451
|
||||
If Not isResolved Then
|
||||
pErrorMessages.Add "必需类别 [" & category & "] 未匹配"
|
||||
End If
|
||||
```
|
||||
|
||||
**双向覆盖检查逻辑**:
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
A[必需类别 X 缺失] --> B{检查 1: 有父类?}
|
||||
B -->|是 | C{父类别已匹配?}
|
||||
C -->|是 | D[被父类覆盖<br/>不报错]
|
||||
C -->|否 | E[检查 2]
|
||||
B -->|否 | E
|
||||
|
||||
E --> F{检查 2: 有子类?}
|
||||
F -->|是 | G{所有子类都匹配?}
|
||||
G -->|是 | H[被子类覆盖<br/>不报错]
|
||||
G -->|否 | I[确实缺失<br/>报错]
|
||||
F -->|否 | I
|
||||
|
||||
D --> J[Continue]
|
||||
H --> J
|
||||
I --> K[添加到 pErrorMessages]
|
||||
|
||||
style D fill:#51cf66
|
||||
style H fill:#51cf66
|
||||
style K fill:#ff6b6b
|
||||
```
|
||||
|
||||
**覆盖场景示例**:
|
||||
|
||||
| 场景 | 父类别 | 子类别 1 | 子类别 2 | 结果 |
|
||||
|------|--------|---------|---------|------|
|
||||
| 总成优先 | ✅ 部件 (1 条) | ✅ 接头 (2 条) | ✅ 弹性元件 (1 条) | 输出"部件",子类不报错 |
|
||||
| 散件满足 | ❌ 部件 (缺失) | ✅ 接头 (2 条) | ✅ 弹性元件 (1 条) | 输出子类,父类不报错 |
|
||||
| 确实缺失 | ❌ 部件 (缺失) | ❌ 接头 (缺失) | ✅ 弹性元件 (1 条) | 报错:"必需类别 [接头] 未匹配" |
|
||||
|
||||
---
|
||||
|
||||
### 来源 5: 无匹配物料
|
||||
|
||||
**位置**: `MainModule.bas` 第 203-208 行
|
||||
**触发条件**: `matchedItems.Count = 0` 且 `extractNote` 为空
|
||||
**错误消息**: `未匹配到任何物料`
|
||||
|
||||
```vba
|
||||
' MainModule.bas:203-208
|
||||
If matchedItems.Count = 0 Then
|
||||
' 没有匹配项
|
||||
If extractNote = "" Then
|
||||
extractNote = "未匹配到任何物料"
|
||||
End If
|
||||
outputData.Add CreateOutputRowArray(..., extractNote, Nothing)
|
||||
```
|
||||
|
||||
**注意**: 如果已有其他错误(如解析错误),则不会覆盖。
|
||||
|
||||
---
|
||||
|
||||
### 来源 6: 物料级 MatchError
|
||||
|
||||
**位置**: `MainModule.bas` 第 215-223 行
|
||||
**触发条件**: `BomItem.MatchError` 非空
|
||||
**错误来源**: 由 `BomExtractor.MatchItems()` 设置(见来源 3)
|
||||
|
||||
```vba
|
||||
' MainModule.bas:215-223
|
||||
For Each item In matchedItems
|
||||
Dim itemNote As String
|
||||
itemNote = extractNote
|
||||
|
||||
' 添加物料特定的错误
|
||||
If item.MatchError <> "" Then
|
||||
If itemNote <> "" Then itemNote = itemNote & "; "
|
||||
itemNote = itemNote & item.MatchError
|
||||
End If
|
||||
|
||||
' ... 添加到输出
|
||||
Next item
|
||||
```
|
||||
|
||||
**特性**:
|
||||
- 每条 BOM 物料单独输出时附加
|
||||
- 使用 `"; "` 分隔符拼接
|
||||
- 首行继承全局 `extractNote`,后续行只继承不含物料级错误
|
||||
|
||||
---
|
||||
|
||||
## 错误消息拼接规则
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant M as MainModule
|
||||
participant P as ProductModelParser
|
||||
participant E as BomExtractor
|
||||
participant I as BomItem
|
||||
|
||||
M->>P: Parse modelString
|
||||
alt 解析失败
|
||||
P-->>M: 返回 ErrorMessage
|
||||
Note over M: extractNote = 解析失败 + ErrorMessage
|
||||
M->>M: 输出单行后结束
|
||||
else 解析成功
|
||||
M->>E: ExtractBom conditions
|
||||
Note over E: 收集错误到 pErrorMessages
|
||||
Note over E: 设置 item.MatchError
|
||||
E-->>M: GetErrorSummary
|
||||
|
||||
alt bomErrors 非空
|
||||
Note over M: extractNote = bomErrors
|
||||
else bomErrors 为空
|
||||
Note over M: extractNote 保持空
|
||||
end
|
||||
|
||||
alt matchedItems = 0
|
||||
Note over M: 设为 未匹配到任何物料
|
||||
M->>M: 输出单行后结束
|
||||
else matchedItems > 0
|
||||
loop For Each item
|
||||
M->>M: itemNote = extractNote
|
||||
alt item.MatchError 非空
|
||||
Note over M: itemNote += MatchError
|
||||
end
|
||||
M->>M: CreateOutputRowArray
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Note over M: 输出到提取备注列
|
||||
```
|
||||
|
||||
**拼接规则总结**:
|
||||
|
||||
| 场景 | 拼接方式 | 示例 |
|
||||
|------|---------|------|
|
||||
| 全局错误汇总 | 分号连接 | 错误 1; 错误 2 |
|
||||
| 物料级附加 | 检查非空后加分号 | extractNote + MatchError |
|
||||
| 多类别缺失 | 逐个添加到集合后汇总 | 必需类别 A 未匹配; 必需类别 B 未匹配 |
|
||||
| 解析失败 + 其他 | 解析失败时立即退出不叠加 | 解析失败:... |
|
||||
|
||||
---
|
||||
|
||||
## 输出列定义
|
||||
|
||||
**位置**: `MainModule.bas` 第 270 行
|
||||
|
||||
```vba
|
||||
' MainModule.bas:242-270
|
||||
Private Sub WriteOutputHeader(ws As Worksheet)
|
||||
' ... 前面的列 ...
|
||||
ws.Cells(1, col).Value = "66 代码": col = col + 1
|
||||
ws.Cells(1, col).Value = "提取备注": col = col + 1 ' ← 最后一列
|
||||
End Sub
|
||||
```
|
||||
|
||||
**数据写入**: `CreateOutputRowArray()` 函数的最后一个元素
|
||||
|
||||
```vba
|
||||
' MainModule.bas:283-339
|
||||
Private Function CreateOutputRowArray(..., note As String, ...) As Variant()
|
||||
' ... 填充前面的列 ...
|
||||
|
||||
' 备注(最后一列)
|
||||
rowData(col) = note
|
||||
|
||||
CreateOutputRowArray = rowData
|
||||
End Function
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 完整错误消息类型汇总表
|
||||
|
||||
| 错误类型 | 错误消息模板 | 触发条件 | 源码位置 | 是否可叠加 |
|
||||
|---------|-------------|---------|---------|----------|
|
||||
| **解析错误** | `解析失败:[具体原因]` | Parse() 失败 | MainModule.bas:186 | ❌ 单独输出 |
|
||||
| **多匹配** | `类别 [X] 匹配到多条物料 (N 条)` | 同类别匹配>1 | BomExtractor.cls:250 | ✅ 可叠加 |
|
||||
| **必需类别缺失** | `必需类别 [X] 未匹配` | ValidateResult 检测缺失 | BomExtractor.cls:450 | ✅ 可叠加 |
|
||||
| **无匹配** | `未匹配到任何物料` | matchedItems.Count = 0 | MainModule.bas:207 | ❌ 仅当无其他错误 |
|
||||
| **物料级多匹配** | `类别 [X] 匹配到多条物料 (N 条)` | item.MatchError | MainModule.bas:221 | ✅ 逐行附加 |
|
||||
|
||||
---
|
||||
|
||||
## 典型输出示例
|
||||
|
||||
### 示例 1: 解析失败
|
||||
```
|
||||
产品型号:Y-100-M203.316SS
|
||||
提取备注:解析失败:表头结构不完整:缺少必要字段
|
||||
```
|
||||
|
||||
### 示例 2: 正常匹配(无错误)
|
||||
```
|
||||
产品型号:Y-100-M203.316SS.L100.N2
|
||||
提取备注:(空)
|
||||
```
|
||||
|
||||
### 示例 3: 多匹配错误
|
||||
```
|
||||
产品型号:Y-100-M203.316SS.L100.N2
|
||||
提取备注:类别 [接液材质] 匹配到多条物料 (3 条); 类别 [量程范围] 匹配到多条物料 (2 条);
|
||||
```
|
||||
|
||||
### 示例 4: 必需类别缺失
|
||||
```
|
||||
产品型号:Y-100-M203.316SS.L100.N2
|
||||
提取备注:必需类别 [安装形式] 未匹配; 必需类别 [表壳形式] 未匹配;
|
||||
```
|
||||
|
||||
### 示例 5: 无匹配
|
||||
```
|
||||
产品型号:INVALID-MODEL
|
||||
提取备注:未匹配到任何物料
|
||||
```
|
||||
|
||||
### 示例 6: 物料级错误附加
|
||||
```
|
||||
产品型号:Y-100-M203.316SS.L100.N2
|
||||
行号 | 类别 | 提取备注
|
||||
-----|------|----------
|
||||
1 | 接头 | 类别 [接液材质] 匹配到多条物料 (3 条); 类别 [接液材质] 匹配到多条物料 (3 条);
|
||||
2 | 弹性元件 | 类别 [接液材质] 匹配到多条物料 (3 条);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 关键代码路径索引
|
||||
|
||||
| 功能 | 文件 | 行号范围 |
|
||||
|------|------|---------|
|
||||
| 主入口 | MainModule.bas | 20-150 |
|
||||
| 单型号处理 | MainModule.bas | 161-235 |
|
||||
| 输出表头定义 | MainModule.bas | 242-271 |
|
||||
| 行数据创建 | MainModule.bas | 283-339 |
|
||||
| 型号解析 | ProductModelParser.cls | 60-91 |
|
||||
| BOM 提取 | BomExtractor.cls | 130-152 |
|
||||
| 匹配物料 | BomExtractor.cls | 208-261 |
|
||||
| 总成逻辑 | BomExtractor.cls | 270-367 |
|
||||
| 结果验证 | BomExtractor.cls | 376-455 |
|
||||
| 错误汇总 | BomExtractor.cls | 471-482 |
|
||||
| 物料数据模型 | BomItem.cls | 1-89 |
|
||||
|
||||
---
|
||||
|
||||
## 设计特点
|
||||
|
||||
✅ **优点**:
|
||||
1. 错误信息分层清晰(解析层、匹配层、验证层)
|
||||
2. 支持多错误叠加,不丢失任何警告
|
||||
3. 双向覆盖检查避免误报(总成/散件场景)
|
||||
4. 物料级错误逐行附加,便于定位问题
|
||||
|
||||
⚠️ **注意事项**:
|
||||
1. 解析失败时立即退出,不执行后续 BOM 提取
|
||||
2. 多匹配错误会输出所有匹配项(数据不确定时保留全部)
|
||||
3. 错误消息使用 `"; "` 分隔,末尾可能有多余分隔符
|
||||
4. "未匹配到任何物料"仅在无其他错误时显示
|
||||
|
||||
---
|
||||
|
||||
**文档结束**
|
||||
1203
docs/错误处理机制详解.md
1203
docs/错误处理机制详解.md
File diff suppressed because it is too large
Load Diff
@@ -1,310 +0,0 @@
|
||||
# 布莱迪压力表产品BOM自动提取系统
|
||||
|
||||
# 需求规格说明书
|
||||
|
||||
---
|
||||
|
||||
**文档版本**: V1.0
|
||||
**创建日期**: 2025年2月
|
||||
**编制部门**: 信息技术部
|
||||
|
||||
---
|
||||
|
||||
## 1. 项目概述
|
||||
|
||||
### 1.1 项目背景
|
||||
|
||||
布莱迪公司在压力表产品生产过程中,需要根据客户订单的产品型号提取对应的BOM(物料清单)。当前手工提取流程存在效率低、易出错等问题。为提高工作效率,减少人为错误,需要开发一套基于Excel VBA的自动化BOM提取系统。
|
||||
|
||||
### 1.2 项目目标
|
||||
|
||||
本系统旨在实现以下目标:
|
||||
|
||||
1. 自动解析产品型号,提取关键参数
|
||||
2. 基于提取参数,自动匹配BOM库中的物料
|
||||
3. 输出标准化的BOM清单,便于物料领用
|
||||
4. 记录异常情况,便于人工复核
|
||||
5. 提供代码接口,支持未来功能扩展
|
||||
|
||||
---
|
||||
|
||||
## 2. 功能需求
|
||||
|
||||
### 2.1 产品型号解析功能
|
||||
|
||||
#### 2.1.1 型号结构识别
|
||||
|
||||
系统需要从完整产品型号中识别并提取表头部分。产品型号的完整格式为:
|
||||
|
||||
```
|
||||
[表头]|[表盘]|[附件]|[法兰隔膜]
|
||||
```
|
||||
|
||||
其中,仅提取表头部分,其余部分暂时丢弃。表头部分的标准结构为:
|
||||
|
||||
```
|
||||
[型号]-[公称外径].[安装形式].[壳体形式].[过程连接&接液材质].[量程范围].[仪表特性]
|
||||
```
|
||||
|
||||
#### 2.1.2 参数提取规则
|
||||
|
||||
系统需要按照以下规则提取各项参数:
|
||||
|
||||
| 参数名称 | 变量名 | 提取规则 |
|
||||
|---------|--------|---------|
|
||||
| 型号 | xh | '-'符号之前的字母组合(预留,暂不参与BOM匹配) |
|
||||
| 公称外径 | gcwj | '-'符号之后,第一个'.'之前的数字(预留,暂不参与BOM匹配) |
|
||||
| 安装形式 | azxs | 第一段(如:A0) |
|
||||
| 表壳形式 | bkxs | 第二段(如:531),包含表壳和罩壳代码 |
|
||||
| 过程连接 | gclj | 第三段去除最后一位数字(如:M203→M20) |
|
||||
| 接液材质 | jycz | 第三段的最后一位数字(如:M203→3) |
|
||||
| 量程范围 | lcfw | 第四段(如:M06) |
|
||||
| 附加功能 | fjgn | 量程范围之后的所有段(用','或'.'分隔,如:N3,N2) |
|
||||
|
||||
#### 2.1.3 型号完整性验证
|
||||
|
||||
系统需要验证表头型号是否包含完整的结构。如果型号缺少必要的段落,应记录为异常情况,并在提取备注中说明。
|
||||
|
||||
#### 2.1.4 测试用例
|
||||
|
||||
系统需要能够正确处理以下测试型号:
|
||||
|
||||
- `YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3`
|
||||
- `YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3`
|
||||
- `YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3|LSG-1.14x2.M20F.M20.3^HDJ.M20F.BW.14×2×60.3^TSFJ^WHP.70X20X1.3`
|
||||
- `YTHN-100.A0.531.M203.P21.Y3|BP-088.2312.M39.0A3|HDJ.M20F.BW.14×2×60.3^LSG-1.14x2.M20F.M20.3^TSFJ^WHP.70X20X1.3`
|
||||
- `YTHN-100.A0.531.M201.M03.N1.Y3|BP-088.2312.M31.0A4`
|
||||
- `YTHN-100.A0.531.M201.M04.N1,N2.Y3|BP-088.2312.M32.0A3`
|
||||
- `YTHN-100.A0.531.M201.M04.N1.N2.Y3|BP-088.2312.M32.0A3`
|
||||
- `YTHN-100.A0.531.Z121.M07.Y3|BP-088.2312.M07.0A3`
|
||||
- `YTHN-100.A0.531.Z121.M08.Y3|BP-088.2312.M08.0A3`
|
||||
|
||||
---
|
||||
|
||||
### 2.2 BOM匹配功能
|
||||
|
||||
#### 2.2.1 BOM库文件结构
|
||||
|
||||
BOM库数据保存在与当前工作簿同目录下的**BOM库.xlsx**文件中。该文件包含多个工作表,每个工作表代表一类物料,常见的工作表包括:
|
||||
|
||||
- 接头
|
||||
- 弹性元件
|
||||
- 机芯
|
||||
- 部件
|
||||
- 边
|
||||
|
||||
#### 2.2.2 匹配规则
|
||||
|
||||
系统采用AND逻辑进行匹配,即BOM库记录中的所有条件列都必须满足才算匹配成功。具体规则如下:
|
||||
|
||||
| 规则类型 | 说明 |
|
||||
|---------|------|
|
||||
| 空值匹配 | BOM库单元格为空时,视为通配符(Match All),条件成立 |
|
||||
| 否定匹配 | BOM库单元格以'!='开头(如:!=SCRJ),则提取值不等于该值时条件成立 |
|
||||
| 精确匹配 | BOM库单元格为普通值时,提取值必须完全相等 |
|
||||
| 包含匹配(fjgn) | 对于附加功能字段,只要提取的功能列表包含BOM库中的值即可(如:提取出N3,N2,BOM库中是N3,则匹配成功) |
|
||||
|
||||
#### 2.2.3 部件物料特殊处理
|
||||
|
||||
'部件'工作表中的记录比较特殊,每条记录包含三个物料的数据:
|
||||
|
||||
1. 部件物料本身
|
||||
2. 接头物料
|
||||
3. 弹性元件物料
|
||||
|
||||
**选择策略:**
|
||||
|
||||
- 优先选择'部件'物料
|
||||
- 当'部件'物料库存不足时,选择'接头'和'弹性元件'物料
|
||||
- 库存检查接口预留,当前默认库存充足(返回True)
|
||||
|
||||
**部件物料验证规则:**
|
||||
|
||||
针对"部件"这个物料和其子物料"接头"和"弹性元件",在提取出的物料中,只有满足以下两种情况的一种,才算正常:
|
||||
|
||||
- 一条"部件"物料
|
||||
- 一条"接头"物料 + 一条"弹性元件"物料
|
||||
|
||||
如果出现其他组合(如:只有接头没有弹性元件,或同时有部件和接头),则视为异常,需要在提取备注中记录。
|
||||
|
||||
#### 2.2.4 匹配结果验证
|
||||
|
||||
对于每个产品型号,在各个物料类别工作表中应该匹配到恰好1条记录。以下情况视为异常:
|
||||
|
||||
- 某个类别未匹配到任何记录(0条)
|
||||
- 某个类别匹配到多条记录(>1条)
|
||||
|
||||
所有异常情况需要记录到**提取备注**字段中,便于后续人工复核。
|
||||
|
||||
---
|
||||
|
||||
### 2.3 数据输入输出
|
||||
|
||||
#### 2.3.1 输入数据源
|
||||
|
||||
订单数据来源于当前工作簿中的某个工作表。该工作表应包含产品型号列,系统需要能够识别并读取该列数据。
|
||||
|
||||
#### 2.3.2 输出结果格式
|
||||
|
||||
系统将提取结果输出到当前工作簿的新工作表中。输出采用纵向展开格式,即每个物料一行,一个订单可能对应多行记录。
|
||||
|
||||
**输出字段包括:**
|
||||
|
||||
| 字段名称 | 说明 |
|
||||
|---------|------|
|
||||
| 原始产品型号 | 完整的产品型号字符串 |
|
||||
| 提取条件值 | azxs、bkxs、gclj、jycz、lcfw、fjgn等参数的值 |
|
||||
| 物料类型 | 如:机芯、部件、接头、弹性元件、边等 |
|
||||
| 物料名称 | 从BOM库中提取的物料名称 |
|
||||
| 物料编码 | 从BOM库中提取的物料编码 |
|
||||
| 物料数量 | 从BOM库中提取的物料数量 |
|
||||
| 提取备注 | 记录提取过程中的异常信息,如:型号不完整、匹配失败、匹配多条等 |
|
||||
|
||||
---
|
||||
|
||||
## 3. 技术规格要求
|
||||
|
||||
### 3.1 代码架构要求
|
||||
|
||||
#### 3.1.1 模块化设计
|
||||
|
||||
系统采用模块化设计,各功能模块之间保持解耦。主要模块包括:
|
||||
|
||||
- **型号解析模块**:负责解析产品型号,提取参数
|
||||
- **BOM匹配模块**:负责根据参数匹配BOM库中的物料
|
||||
- **数据输出模块**:负责将结果写入工作表
|
||||
- **库存查询模块**:负责查询物料库存状态(预留接口)
|
||||
|
||||
#### 3.1.2 接口设计
|
||||
|
||||
所有关键函数需要明确定义输入和输出接口。接口定义应包括:
|
||||
|
||||
- 函数名称和用途说明
|
||||
- 输入参数:名称、类型、说明
|
||||
- 输出结果:类型、说明
|
||||
- 异常处理:可能的错误情况及处理方式
|
||||
|
||||
#### 3.1.3 代码注释
|
||||
|
||||
所有函数、过程必须包含详细的注释,说明:
|
||||
|
||||
- 功能描述
|
||||
- 参数说明
|
||||
- 返回值说明
|
||||
- 使用示例
|
||||
- 注意事项
|
||||
|
||||
---
|
||||
|
||||
### 3.2 质量保证要求
|
||||
|
||||
#### 3.2.1 单元测试
|
||||
|
||||
关键接口需要编写单元测试。测试应覆盖:
|
||||
|
||||
- 正常输入情况
|
||||
- 边界值情况
|
||||
- 异常输入情况
|
||||
- 特殊字符处理
|
||||
|
||||
#### 3.2.2 异常处理
|
||||
|
||||
系统采用容错设计,遇到异常时继续运行,将异常信息记录在提取备注中。异常情况包括但不限于:
|
||||
|
||||
- 型号格式不完整
|
||||
- BOM库文件不存在或无法打开
|
||||
- 某类物料匹配失败(0条或多条)
|
||||
- 数据类型不匹配
|
||||
- 部件物料组合异常(不符合验证规则)
|
||||
|
||||
---
|
||||
|
||||
### 3.3 扩展性要求
|
||||
|
||||
#### 3.3.1 参数扩展
|
||||
|
||||
系统设计应支持未来新增提取参数。当前提取的参数包括:azxs、bkxs、gclj、jycz、lcfw、fjgn。
|
||||
|
||||
未来可能需要提取的参数包括但不限于:
|
||||
|
||||
- lcdw(量程单位)
|
||||
- btcy(表头充油)
|
||||
- bp(表盘)
|
||||
- dskd(单双刻度)
|
||||
- 其他业务参数
|
||||
|
||||
#### 3.3.2 接口预留
|
||||
|
||||
系统需要预留以下接口:
|
||||
|
||||
| 接口名称 | 功能说明 |
|
||||
|---------|---------|
|
||||
| 库存查询接口 | 输入:物料编码;输出:布尔值(True=有库存,False=无库存)。当前默认返回True |
|
||||
| 部件选择接口 | 输入:部件库存状态;输出:选择结果(部件或接头+弹性元件) |
|
||||
|
||||
---
|
||||
|
||||
## 4. 非功能性需求
|
||||
|
||||
### 4.1 性能要求
|
||||
|
||||
系统应能够在合理时间内完成BOM提取任务。对于包含100条订单记录的工作表,处理时间应控制在1分钟以内。
|
||||
|
||||
### 4.2 可维护性
|
||||
|
||||
代码结构清晰,注释完整,便于后续维护和功能扩展。变量命名规范,使用有意义的名称。
|
||||
|
||||
### 4.3 可靠性
|
||||
|
||||
系统采用容错设计,即使遇到异常数据也能继续运行,确保已处理的数据不会丢失。所有异常情况都应被记录,便于追溯和排查问题。
|
||||
|
||||
---
|
||||
|
||||
## 5. 附录
|
||||
|
||||
### 5.1 可能的选择条件字段
|
||||
|
||||
以下是BOM库工作表中可能出现的所有选择条件字段,系统在识别条件列时应灵活处理:
|
||||
|
||||
| 变量名 | 变量标签 |
|
||||
|-------|---------|
|
||||
| azxs | 安装形式 |
|
||||
| bkxs | 表壳形式 |
|
||||
| gclj | 过程连接 |
|
||||
| jycz | 接液材质 |
|
||||
| lcdw | 量程单位 |
|
||||
| lcfw | 量程范围 |
|
||||
| fjgn | 附加功能 |
|
||||
| btcy | 表头充油 |
|
||||
| bp | 表盘 |
|
||||
| dskd | 单双刻度 |
|
||||
| nqlc | 内圈量程 |
|
||||
| bptx | 表盘特性 |
|
||||
| jddj | 精度等级 |
|
||||
| cpdm | 产品代码 |
|
||||
| tsjz | 特殊介质 |
|
||||
| tsyq | 特殊要求 |
|
||||
| bpts | 表盘套色 |
|
||||
| kdxh | 刻度线红色 |
|
||||
|
||||
---
|
||||
|
||||
### 5.2 测试型号示例
|
||||
|
||||
以下测试型号可用于验证系统功能:
|
||||
|
||||
| 编号 | 产品型号 |
|
||||
|-----|---------|
|
||||
| 1 | `YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3` |
|
||||
| 2 | `YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3` |
|
||||
| 3 | `YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3|LSG-1.14x2.M20F.M20.3^HDJ.M20F.BW.14×2×60.3^TSFJ^WHP.70X20X1.3` |
|
||||
| 4 | `YTHN-100.A0.531.M203.P21.Y3|BP-088.2312.M39.0A3|HDJ.M20F.BW.14×2×60.3^LSG-1.14x2.M20F.M20.3^TSFJ^WHP.70X20X1.3` |
|
||||
| 5 | `YTHN-100.A0.531.M201.M03.N1.Y3|BP-088.2312.M31.0A4` |
|
||||
| 6 | `YTHN-100.A0.531.M201.M04.N1,N2.Y3|BP-088.2312.M32.0A3` |
|
||||
| 7 | `YTHN-100.A0.531.M201.M04.N1.N2.Y3|BP-088.2312.M32.0A3` |
|
||||
| 8 | `YTHN-100.A0.531.Z121.M07.Y3|BP-088.2312.M07.0A3` |
|
||||
| 9 | `YTHN-100.A0.531.Z121.M08.Y3|BP-088.2312.M08.0A3` |
|
||||
|
||||
---
|
||||
|
||||
**文档结束**
|
||||
115
reference_docs/平台配置清单-demo.md
Normal file
115
reference_docs/平台配置清单-demo.md
Normal file
@@ -0,0 +1,115 @@
|
||||
| 代号 | Y-100 | 描述 | | 英文名称 | | | | | | | | |
|
||||
|------|---------|--------------|------------|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----|------|------|--------|-------------|---------|-----------------|
|
||||
| 名称 | 压力表 | 负责人 | | 备注 | | | | | | | | |
|
||||
| 行号 | 模块 | 代号 | 名称 | 数量 | 选择条件 | 备注 | 类别 | 上层类别 | 类别选用条件 | 66代码 | BIP行号基数 | 修改备注 |
|
||||
| 10 | 表壳 | 01091004644 | 轴向表壳(黑色) | 1 | (azxs=B0 OR azxs=BT) AND (bkxs=200 OR bkxs=210) | | 表壳 | | | 66071004644 | 7100 | |
|
||||
| 20 | 表壳 | 01091004647 | 轴向表壳(抛光) | 1 | azxs=B0 AND bkxs=500 | | 表壳 | | | 66071004647 | 7100 | |
|
||||
| 30 | 表壳 | 01091004643 | 径向表壳(黑色) | 1 | (azxs=A0 OR azxs=AT) AND (bkxs=200 OR bkxs=210) | | 表壳 | | | 66071004643 | 7100 | |
|
||||
| 40 | 表壳 | 01091004648 | 径向表壳(抛光) | 1 | azxs=A0 AND bkxs=500 | | 表壳 | | | 66071004648 | 7100 | |
|
||||
| 50 | 表壳 | 01081013918 | 带后边表壳(黑色) | 1 | azxs=AH AND (bkxs=200 OR bkxs=210) | | 表壳 | | | 66051013918 | 7100 | |
|
||||
| 60 | 罩壳 | 01091004768 | 罩壳(抛光) | 1 | azxs=A0 AND bkxs=500 | | 罩壳 | | | 66071004768 | 7100 | |
|
||||
| 70 | 罩壳 | 01091005032 | 罩壳(抛光) | 1 | azxs=B0 AND bkxs=500 | | 罩壳 | | | 66071005032 | 7100 | 改为 66071005032 |
|
||||
| 80 | 罩壳 | 01091004624 | 罩壳(黑色) | 1 | azxs=AT AND bkxs=200 | | 罩壳 | | | 66071004624 | 7100 | |
|
||||
| 90 | 罩壳 | 01091004623 | 罩壳(亮) | 1 | azxs=A0 AND bkxs=210 | | 罩壳 | | | 66071004623 | 7100 | |
|
||||
| 100 | 罩壳 | 01091004621 | 罩壳(亮) | 1 | azxs=BT AND bkxs=210 | | 罩壳 | | | 66071004621 | 7100 | |
|
||||
| 110 | 罩壳 | 01091004620 | 罩壳(黑色) | 1 | azxs=BT AND bkxs=200 | | 罩壳 | | | 66071004620 | 7100 | |
|
||||
| 120 | 罩壳 | 01091004619 | 罩壳(黑色) | 1 | azxs=B0 AND bkxs=200 | | 罩壳 | | | 66071004619 | 7100 | |
|
||||
| 130 | 罩壳 | 01091004618 | 罩壳(黑色) | 1 | azxs=A0 AND bkxs=200 | | 罩壳 | | | 66071004618 | 7100 | |
|
||||
| 140 | 罩壳 | 01091004617 | 罩壳(亮) | 1 | azxs=B0 AND bkxs=210 | | 罩壳 | | | 66071004617 | 7100 | |
|
||||
| 150 | 玻璃 | 01111001334 | 表玻璃 | 1 | fjgn!=N1 | | 玻璃 | | | 66201001334 | 7100 | |
|
||||
| 160 | 定位型玻璃部件 | 01111001335 | 表玻璃 | 1 | fjgn=N1 | | 玻璃 | | | 66201001202 | 7100 | 改为 66201001202 |
|
||||
| 170 | 定位型玻璃部件 | 01091004453 | 100 红色定位指针 | 1 | fjgn= N1 | | | | | 66071004453 | 7100 | |
|
||||
| 180 | 定位型玻璃部件 | 01091004451 | 100 绿色定位指针 | 1 | fjgn=N1 | | | | | 66071004451 | 7100 | |
|
||||
| 190 | 定位型玻璃部件 | 01081014387 | 定位钉 | 1 | fjgn=N1 | | | | | 66051014387 | 7100 | |
|
||||
| 200 | 定位型玻璃部件 | 01201002619 | 固定套 | 1 | fjgn=N1 | | | | | 66991922619 | 7100 | |
|
||||
| 210 | 定位型玻璃部件 | 01121002259 | 橡胶垫<2> | 1 | fjgn=N1 | | | | | 66181002259 | 7100 | |
|
||||
| 220 | 定位型玻璃部件 | 01121002258 | 橡胶垫<1> | 1 | fjgn=N1 | | | | | 66181002258 | 7100 | |
|
||||
| 230 | 定位型玻璃部件 | 01140003351 | O型圈 | 1 | fjgn=N1 | | | | | 66220003351 | 7100 | |
|
||||
| 240 | 定位型玻璃部件 | 01140003353 | O型圈 | 1 | fjgn=N1 | | | | | 66220003353 | 7100 | |
|
||||
| 250 | 衬圈 | 01121002271 | 衬圈 | 1 | | | 衬圈 | | | 66181002271 | 7100 | |
|
||||
| 260 | 表壳螺钉 | 01140002660 | 表壳螺钉 | 3 | | | | | | 260 | | |
|
||||
| 270 | 罩壳螺钉 | 01140002701 | 罩壳螺钉 | 2 | | | | | | 270 | | |
|
||||
| 280 | 铅封螺钉 | 01081008107 | 铅封螺钉 | 1 | | | | | | 66051008107 | | |
|
||||
| 290 | 表盘螺钉 | 01140002695 | 表盘螺钉 | 2 | | | | | | 290 | | |
|
||||
| 300 | 盘止钉 | 01081005277 | 盘止钉 | 1 | lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16 OR lcfw=M30 | | | | | 300 | | |
|
||||
| 310 | 接头部件 | 01011019173 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND (lcfw=M01 OR lcfw=K78) AND gclj=M20 | | 部件 | | | 66021019173 | 7200 | |
|
||||
| 320 | 接头部件 | 01011019174 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M02 AND gclj=M20 | | 部件 | | | 66021019174 | 7200 | |
|
||||
| 330 | 接头部件 | 01011019175 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M03 AND gclj=M20 | | 部件 | | | 66021019175 | 7200 | |
|
||||
| 340 | 接头部件 | 01011019176 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M04 AND gclj=M20 | | 部件 | | | 66021019176 | 7200 | |
|
||||
| 350 | 接头部件 | 01011019177 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M05 AND gclj=M20 | | 部件 | | | 66021019177 | 7200 | |
|
||||
| 360 | 接头部件 | 01011019178 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M06 AND gclj=M20 | | 部件 | | | 66021019178 | 7200 | |
|
||||
| 370 | 接头部件 | 01011019179 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M07 AND gclj=M20 | | 部件 | | | 66021019179 | 7200 | |
|
||||
| 380 | 接头部件 | 01011019180 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M08 AND gclj=M20 | | 部件 | | | 66021019180 | 7200 | |
|
||||
| 390 | 接头部件 | 01011019181 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M09 AND gclj=M20 | | 部件 | | | 66021019181 | 7200 | |
|
||||
| 400 | 接头部件 | 01011019182 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M10 AND gclj=M20 | | 部件 | | | 66021019182 | 7200 | |
|
||||
| 410 | 接头部件 | 01011019183 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M11 AND gclj=M20 | | 部件 | | | 66021019183 | 7200 | |
|
||||
| 420 | 接头部件 | 01011019184 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M12 AND gclj=M20 | | 部件 | | | 66021019184 | 7200 | |
|
||||
| 430 | 接头部件 | 01011019185 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M13 AND gclj=M20 | | 部件 | | | 66021019185 | 7200 | |
|
||||
| 440 | 接头部件 | 01011019186 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M14 AND gclj=M20 | | 部件 | | | 66021019186 | 7200 | |
|
||||
| 450 | 接头部件 | 01011019187 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M15 AND gclj=M20 | | 部件 | | | 66021019187 | 7200 | |
|
||||
| 460 | 接头部件 | 01011019188 | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M16 AND gclj=M20 | | 部件 | | | 66021019188 | 7200 | |
|
||||
| 470 | 接头部件 | 01011019189 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND (lcfw=M02 OR lcfw=K107) AND gclj=M20 | | 部件 | | | 66021019189 | 7200 | |
|
||||
| 480 | 接头部件 | 01011019190 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M03 AND gclj=M20 | | 部件 | | | 66021019190 | 7200 | |
|
||||
| 490 | 接头部件 | 01011019191 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M04 AND gclj=M20 | | 部件 | | | 66021019191 | 7200 | |
|
||||
| 500 | 接头部件 | 01011019192 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M05 AND gclj=M20 | | 部件 | | | 66021019192 | 7200 | |
|
||||
| 510 | 接头部件 | 01011019193 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M06 AND gclj=M20 | | 部件 | | | 66021019193 | 7200 | |
|
||||
| 520 | 接头部件 | 01011019194 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M07 AND gclj=M20 | | 部件 | | | 66021019194 | 7200 | |
|
||||
| 530 | 接头部件 | 01011019195 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M08 AND gclj=M20 | | 部件 | | | 66021019195 | 7200 | |
|
||||
| 540 | 接头部件 | 01011019196 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M09 AND gclj=M20 | | 部件 | | | 66021019196 | 7200 | |
|
||||
| 550 | 接头部件 | 01011019197 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M10 AND gclj=M20 | | 部件 | | | 66021019197 | 7200 | |
|
||||
| 560 | 接头部件 | 01011019198 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M11 AND gclj=M20 | | 部件 | | | 66021019198 | 7200 | |
|
||||
| 570 | 接头部件 | 01011019199 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M12 AND gclj=M20 | | 部件 | | | 66021019199 | 7200 | |
|
||||
| 580 | 接头部件 | 01011019200 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M13 AND gclj=M20 | | 部件 | | | 66021019200 | 7200 | |
|
||||
| 590 | 接头部件 | 01011019201 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M14 AND gclj=M20 | | 部件 | | | 66021019201 | 7200 | |
|
||||
| 600 | 接头部件 | 01011019172 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M15 AND gclj=M20 | | | | | 66021019172 | 7200 | 删除 |
|
||||
| 610 | 接头部件 | 01011019202 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M01 AND gclj=M20 | | 部件 | | | 66021019202 | 7200 | |
|
||||
| 620 | 接头部件 | 01011019203 | 轴向部件 | 1 | (azxs=B0 OR azxs=BT) AND lcfw=M16 AND gclj=M20 | | 部件 | | | 66021019203 | 7200 | |
|
||||
| 630 | 接头 | 01081013687 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=M14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 ) | | 接头 | 部件 | | 66051013687 | 7200 | |
|
||||
| 640 | 接头 | 01081013686 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=Z14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013686 | 7200 | |
|
||||
| 650 | 接头 | 01081013685 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=G14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013685 | 7200 | |
|
||||
| 660 | 接头 | 01081013684 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=G12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013684 | 7200 | |
|
||||
| 670 | 接头 | 01081013683 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=G38 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013683 | 7200 | |
|
||||
| 680 | 接头 | 01081013682 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=R12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013682 | 7200 | |
|
||||
| 690 | 接头 | 01081013681 | 径向低压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=Z12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11) | | 接头 | 部件 | | 66051013681 | 7200 | |
|
||||
| 700 | 接头 | 01081014361 | 径向高压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=Z14 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66码缺失 | 7200 | |
|
||||
| 710 | 接头 | 01081013694 | 径向高压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=G12 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013694 | 7200 | |
|
||||
| 720 | 接头 | 01081013692 | 径向高压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=M14 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013692 | 7200 | |
|
||||
| 730 | 接头 | 01081013691 | 径向高压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=R12 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013691 | 7200 | |
|
||||
| 740 | 接头 | 01081013690 | 径向高压接头 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND gclj=Z12 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013690 | 7200 | |
|
||||
| 750 | 接头 | 01081013702 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=R12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107 ) | | 接头 | 部件 | | 66051013702 | 7200 | |
|
||||
| 760 | 接头 | 01081013701 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=M14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107) | | 接头 | 部件 | | 66051013701 | 7200 | |
|
||||
| 770 | 接头 | 01081013700 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=Z12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107) | | 接头 | 部件 | | 66051013700 | 7200 | |
|
||||
| 780 | 接头 | 01081013698 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=G14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107) | | 接头 | 部件 | | 66051013698 | 7200 | |
|
||||
| 790 | 接头 | 01081013697 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=Z14 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107) | | 接头 | 部件 | | 66051013697 | 7200 | |
|
||||
| 800 | 接头 | 01081013696 | 下轴向低压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=G12 AND (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107) | | 接头 | 部件 | | 66051013696 | 7200 | |
|
||||
| 810 | 接头 | 01081013709 | 下轴向高压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=M14 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013709 | 7200 | |
|
||||
| 820 | 接头 | 01081013707 | 下轴向高压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=G14 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013707 | 7200 | |
|
||||
| 830 | 接头 | 01081013706 | 下轴向高压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=Z14 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013706 | 7200 | |
|
||||
| 840 | 接头 | 01081013705 | 下轴向高压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=G12 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013705 | 7200 | |
|
||||
| 850 | 接头 | 01081013704 | 下轴向高压接头 | 1 | (azxs=B0 OR azxs=BT) AND gclj=R12 AND (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16) | | 接头 | 部件 | | 66051013704 | 7200 | |
|
||||
| 860 | 弹性元件 | 01041005171 | 弹簧管 | 1 | lcfw=M01 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005171 | 7200 | |
|
||||
| 870 | 弹性元件 | 01041005169 | 弹簧管 | 1 | lcfw=M02 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005169 | 7200 | |
|
||||
| 880 | 弹性元件 | 01041005170 | 弹簧管 | 1 | lcfw=M03 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005170 | 7200 | |
|
||||
| 890 | 弹性元件 | 01041005453 | 弹簧管 | 1 | lcfw=M04 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005453 | 7200 | |
|
||||
| 900 | 弹性元件 | 01041005235 | 弹簧管 | 1 | lcfw=M05 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005235 | 7200 | |
|
||||
| 910 | 弹性元件 | 01041005114 | 弹簧管 | 1 | lcfw=M06 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005114 | 7200 | |
|
||||
| 920 | 弹性元件 | 01041005230 | 弹簧管 | 1 | lcfw=M07 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005230 | 7200 | |
|
||||
| 930 | 弹性元件 | 01041005233 | 弹簧管 | 1 | lcfw=M08 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005233 | 7200 | |
|
||||
| 940 | 弹性元件 | 01041005229 | 弹簧管 | 1 | lcfw=M09 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005229 | 7200 | |
|
||||
| 950 | 弹性元件 | 01041005172 | 弹簧管 | 1 | lcfw=M10 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005172 | 7200 | |
|
||||
| 960 | 弹性元件 | 01041005452 | 弹簧管 | 1 | lcfw=M11 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005452 | 7200 | |
|
||||
| 970 | 弹性元件 | 01041005005 | 螺旋管 | 1 | lcfw=M12 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005005 | 7200 | |
|
||||
| 980 | 弹性元件 | 01041005006 | 螺旋管 | 1 | lcfw=M13 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005006 | 7200 | |
|
||||
| 990 | 弹性元件 | 01041005007 | 螺旋管 | 1 | lcfw=M14 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005007 | 7200 | |
|
||||
| 1000 | 弹性元件 | 01041005008 | 螺旋管 | 1 | lcfw=M15 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005008 | 7200 | |
|
||||
| 1010 | 弹性元件 | 01041005009 | 螺旋管 | 1 | lcfw=M16 AND gclj!=M20 | | 弹性元件 | 部件 | | 66131005009 | 7200 | |
|
||||
| 1020 | 封口片 | 01091003680 | 高压封口片 | 1 | (lcfw=M12 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16)AND gclj!=M20 | | | | | 66071003680 | | |
|
||||
| 1030 | 封口片 | 01091003663 | 低压封口片 | 1 | (lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11)AND gclj!=M20 | | | | | 66071003663 | | |
|
||||
| 1040 | 连接螺钉 | 01081008105 | 连接螺钉 | 1 | | | | | | 66051008105 | | |
|
||||
| 1050 | 垫片 | 01091004354 | 垫片 | 1 | lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=K107 | | | | | 66071004354 | | |
|
||||
| 1060 | 机芯螺钉 | 01140003413 | 机芯螺钉 | 2 | | | | | | 66220003413 | | |
|
||||
| 1070 | 机芯部件 | 01101000574 | 机芯 | 1 | lcfw=M01 OR lcfw=M02 OR lcfw=M03 OR lcfw=M04 OR lcfw=K107 | | 机芯 | | | 66151000574 | 7100 | 删除 OR lcfw=M12 |
|
||||
| 1080 | 机芯部件 | 01101000575 | 机芯 | 1 | lcfw=M05 OR lcfw=M06 OR lcfw=M07 OR lcfw=M08 OR lcfw=M09 OR lcfw=M10 OR lcfw=M11 OR lcfw=M13 OR lcfw=M14 OR lcfw=M15 OR lcfw=M16 OR lcfw=M12 | | 机芯 | | | 66151000575 | 7100 | 增加 OR lcfw=M12 |
|
||||
| 1090 | 接头部件 | | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M05 AND gclj=G12 | | 部件 | | | 66021019206 | 7200 | 新增物料 |
|
||||
| 1100 | 接头部件 | | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M09 AND gclj=Z12 | | 部件 | | | 66021019212 | 7200 | 新增物料 |
|
||||
| 1120 | 接头部件 | | 径向部件 | 1 | (azxs=A0 OR azxs=AT OR azxs=AH) AND lcfw=M08 AND gclj=G12 | | 部件 | | | 66021019208 | 7200 | 新增物料 |
|
||||
@@ -1,23 +0,0 @@
|
||||
| | A | B | C | D | E | F | G | H |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| 1 | 代号 | YTHN-100 | 描述 | | 英文名称 | | | |
|
||||
| 2 | 名称 | (不锈钢)耐震压力表 | 负责人 | | 备注 | | | |
|
||||
| 3 | 行号 | 模块 | 代号 | 名称 | 数量 | 选择条件 | 备注 | 类别 |
|
||||
| 4 | 10 | 316L部件 | 01011009557 | 径向低压接头部件 | 1.0 | gclj=M20 AND jycz=1 AND lcfw=M01 AND (azxs=A0 OR azxs=AT OR azxs=AH) | | 部件 |
|
||||
| 5 | 20 | 316L部件 | 01011009520 | 径向低压接头部件 | 1.0 | gclj=M20 AND jycz=1 AND lcfw=M02 AND (azxs=A0 OR azxs=AT OR azxs=AH) | | 部件 |
|
||||
| 6 | 330 | 316L部件 | 01011013929 | 下轴向低压接头部件 | 1.0 | gclj=M20 AND jycz=1 AND lcfw=M01 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) | | 部件 |
|
||||
| 7 | 340 | 316L部件 | 01011013978 | 下轴向低压接头部件 | 1.0 | gclj=M20 AND jycz=1 AND lcfw=M02 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) | | 部件 |
|
||||
| 8 | 650 | 304部件 | 01011019001 | 径向低压接头部件 | 1.0 | gclj=M20 AND jycz=3 AND lcfw=M02 AND (azxs=A0 OR azxs=AT OR azxs=AH) | | 部件 |
|
||||
| 9 | 660 | 304部件 | 01011019002 | 径向低压接头部件 | 1.0 | gclj=M20 AND jycz=3 AND lcfw=M03 AND (azxs=A0 OR azxs=AT OR azxs=AH) | | 部件 |
|
||||
| 10 | 800 | 316L接头 | 01081012669 | 径向低压接头 | 1.0 | gclj=M16 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1 AND (lcfw=M01 OR lcfw=M02 ) | | 接头 |
|
||||
| 11 | 810 | 316L接头 | 01081007411 | 径向低压接头 | 1.0 | gclj=M14 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1 AND (lcfw=M01 OR lcfw=M02) | | 接头 |
|
||||
| 12 | 1080 | 316L接头 | 01081016737 | 径向高压接头(Ф7管专用) | 1.0 | gclj=M20 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1 AND lcfw=M18 | | 接头 |
|
||||
| 13 | 1090 | 316L接头 | 01081016738 | 径向高压接头(Ф7管专用) | 1.0 | gclj=M16 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1 AND lcfw=M18 | | 接头 |
|
||||
| 14 | 1200 | 316L接头 | 01081013841 | 下轴向低压接头 | 1.0 | gclj=M16 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) AND jycz=1 AND (lcfw=M01 OR lcfw=M02) | | 接头 |
|
||||
| 15 | 1210 | 316L接头 | 01081013839 | 下轴向低压接头 | 1.0 | gclj=M14 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) AND jycz=1 AND (lcfw=M01 OR lcfw=M02) | | 接头 |
|
||||
| 16 | 1420 | 316L接头 | 01081016761 | 下轴向高压接头(Ф7管专用) | 1.0 | gclj=M20 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) AND jycz=1 AND lcfw=M18 | | 接头 |
|
||||
| 17 | 1430 | 316L接头 | 01081016762 | 下轴向高压接头(Ф7管专用) | 1.0 | gclj=M16 AND (azxs=B0 OR azxs=BT OR azxs=BZ OR azxs=BH) AND jycz=1 AND lcfw=M18 | | 接头 |
|
||||
| 18 | 1540 | 316L接头 | 01081011491 | 中轴向低压接头 | 1.0 | gclj=M20 AND (azxs=Z0 OR azxs=ZT OR azxs=ZZ OR azxs=ZH) AND (bkxs=531 OR bkxs=631) AND jycz=1 AND (lcfw=M01 OR lcfw=M02) | | 接头 |
|
||||
| 19 | 1550 | 316L接头 | 01081011490 | 中轴向低压接头 | 1.0 | gclj=M16 AND (azxs=Z0 OR azxs=ZT OR azxs=ZZ OR azxs=ZH) AND (bkxs=531 OR bkxs=631) AND jycz=1 AND (lcfw=M01 OR lcfw=M02) | | 接头 |
|
||||
| 20 | 2710 | 弹性元件 | 01041001939 | 弹簧管 | 1.0 | lcfw=M01 AND ((gclj=KT06 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1) OR (gclj=KT08 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1)) | | 弹性元件 |
|
||||
| 21 | 2720 | 弹性元件 | 01041001940 | 弹簧管 | 1.0 | lcfw=M02 AND ((gclj=KT06 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1) OR (gclj=KT08 AND (azxs=A0 OR azxs=AT OR azxs=AH) AND jycz=1)) | | 弹性元件 |
|
||||
Reference in New Issue
Block a user