Compare commits
3 Commits
DEV_YTHN-1
...
9db957dc37
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9db957dc37 | ||
|
|
38581f72d6 | ||
|
|
726bbe2118 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,3 +14,4 @@ tmpclaude-*
|
|||||||
*.png
|
*.png
|
||||||
data/
|
data/
|
||||||
*.xlsm
|
*.xlsm
|
||||||
|
*.xlsx
|
||||||
@@ -1,482 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 类名: 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
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 类名: 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 IsMatched As Boolean ' 是否匹配
|
|
||||||
Public MatchError As String ' 匹配错误信息
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 方法: Class_Initialize
|
|
||||||
' 功能: 初始化类
|
|
||||||
'=====================================================================
|
|
||||||
Private Sub Class_Initialize()
|
|
||||||
IsMatched = False
|
|
||||||
MatchError = ""
|
|
||||||
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代码
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -1,224 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 类名: 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
|
|
||||||
@@ -1,303 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 类名: 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
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 主按钮点击事件
|
|
||||||
' 功能: 执行BOM提取和BIP上传
|
|
||||||
'=====================================================================
|
|
||||||
Private Sub CommandButton1_Click()
|
|
||||||
Call ProcessProductModels
|
|
||||||
Call ProcessOrdersToBIP
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 部件库存核对按钮点击事件
|
|
||||||
' 功能: 执行部件库存核对,标记库存不足的订单
|
|
||||||
'=====================================================================
|
|
||||||
Private Sub CommandButton2_Click()
|
|
||||||
Call CheckComponentInventory
|
|
||||||
End Sub
|
|
||||||
@@ -1,420 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 模块名: BIPUploadModule
|
|
||||||
' 功能: 处理产品订单数据,提取BOM后生成[BIP上传模板]格式数据
|
|
||||||
'=====================================================================
|
|
||||||
|
|
||||||
Option Explicit
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 常量定义
|
|
||||||
'=====================================================================
|
|
||||||
' 提取条件配置
|
|
||||||
Private Const CONDITION_CONFIG = "azxs,安装形式|bkxs,表壳形式|gclj,过程连接|jycz,接液材质|lcfw,量程范围|fjgn,附加功能"
|
|
||||||
' 行号基数
|
|
||||||
Private Const ROW_NUMBER_BASE = 7000
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 过程: ProcessOrdersToBIP
|
|
||||||
' 功能: 处理产品订单数据,生成BIP上传格式
|
|
||||||
' 说明: 主入口程序,从[产品订单]读取数据,输出到[BIP上传模板]
|
|
||||||
'=====================================================================
|
|
||||||
Public Sub ProcessOrdersToBIP()
|
|
||||||
On Error GoTo ErrorHandler
|
|
||||||
|
|
||||||
Dim startTime As Double
|
|
||||||
startTime = Timer
|
|
||||||
|
|
||||||
' 准备工作表对象
|
|
||||||
Dim orderSheet As Worksheet
|
|
||||||
Dim bipSheet As Worksheet
|
|
||||||
Dim bomSheet As Worksheet
|
|
||||||
|
|
||||||
' 获取[产品订单]工作表
|
|
||||||
Set orderSheet = GetOrderSheet()
|
|
||||||
If orderSheet Is Nothing Then
|
|
||||||
MsgBox "未找到[产品订单]工作表!", vbCritical
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 获取[BIP上传模板]工作表
|
|
||||||
Set bipSheet = GetBIPUploadSheet()
|
|
||||||
|
|
||||||
' 获取BOM库工作表
|
|
||||||
Set bomSheet = GetBomSheet()
|
|
||||||
If bomSheet Is Nothing Then
|
|
||||||
MsgBox "未找到[平台配置清单]工作表!", vbCritical
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 初始化BOM提取器
|
|
||||||
Dim BomExtractor As BomExtractor
|
|
||||||
Set BomExtractor = New BomExtractor
|
|
||||||
BomExtractor.SetWorksheet bomSheet
|
|
||||||
|
|
||||||
If Not BomExtractor.LoadBomData Then
|
|
||||||
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
|
|
||||||
MsgBox "[产品订单]工作表中没有数据!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 处理每个订单,收集所有输出数据
|
|
||||||
Dim outputData As collection
|
|
||||||
Set outputData = New collection
|
|
||||||
|
|
||||||
Dim i As Long
|
|
||||||
Dim processedCount As Long
|
|
||||||
Dim orderCount As Long
|
|
||||||
|
|
||||||
processedCount = 0
|
|
||||||
orderCount = 0
|
|
||||||
|
|
||||||
For i = 2 To lastRow
|
|
||||||
' 读取订单数据
|
|
||||||
Dim orderNumber As String
|
|
||||||
Dim productModel As String
|
|
||||||
Dim quantity As String
|
|
||||||
Dim productCode As String
|
|
||||||
Dim componentPriority As String
|
|
||||||
|
|
||||||
orderNumber = Trim(orderSheet.Cells(i, 1).value) ' A列:生产订单号
|
|
||||||
productModel = Trim(orderSheet.Cells(i, 2).value) ' B列:产品型号
|
|
||||||
quantity = Trim(orderSheet.Cells(i, 3).value) ' C列:数量
|
|
||||||
productCode = Trim(orderSheet.Cells(i, 4).value) ' D列:产品编码
|
|
||||||
componentPriority = Trim(orderSheet.Cells(i, 5).value) ' E列:部件优先
|
|
||||||
|
|
||||||
' 跳过空行
|
|
||||||
If orderNumber = "" And productModel = "" Then
|
|
||||||
GoTo ContinueLoop
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 验证必填字段
|
|
||||||
If orderNumber = "" Then
|
|
||||||
MsgBox "第" & i & "行:生产订单号为空,跳过该行!", vbExclamation
|
|
||||||
GoTo ContinueLoop
|
|
||||||
End If
|
|
||||||
|
|
||||||
If productModel = "" Then
|
|
||||||
MsgBox "第" & i & "行:产品型号为空,跳过该行!", vbExclamation
|
|
||||||
GoTo ContinueLoop
|
|
||||||
End If
|
|
||||||
|
|
||||||
If quantity = "" Then
|
|
||||||
MsgBox "第" & i & "行:数量为空,跳过该行!", vbExclamation
|
|
||||||
GoTo ContinueLoop
|
|
||||||
End If
|
|
||||||
|
|
||||||
orderCount = orderCount + 1
|
|
||||||
|
|
||||||
' 处理单个订单,收集输出数据
|
|
||||||
ProcessSingleOrder orderNumber, productModel, quantity, productCode, _
|
|
||||||
componentPriority, BomExtractor, outputData
|
|
||||||
processedCount = processedCount + 1
|
|
||||||
|
|
||||||
ContinueLoop:
|
|
||||||
Next i
|
|
||||||
|
|
||||||
' 批量写入数据到工作表
|
|
||||||
If outputData.Count > 0 Then
|
|
||||||
WriteBatchData bipSheet, outputData
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 格式化BIP上传模板
|
|
||||||
FormatBIPSheet bipSheet
|
|
||||||
|
|
||||||
Dim elapsedTime As Double
|
|
||||||
elapsedTime = Timer - startTime
|
|
||||||
|
|
||||||
MsgBox "处理完成!" & vbCrLf & _
|
|
||||||
"处理订单数: " & orderCount & vbCrLf & _
|
|
||||||
"生成BIP行数: " & outputData.Count & vbCrLf & _
|
|
||||||
"用时: " & Format(elapsedTime, "0.00") & "秒", vbInformation
|
|
||||||
|
|
||||||
' 激活BIP上传模板
|
|
||||||
bipSheet.Activate
|
|
||||||
|
|
||||||
Exit Sub
|
|
||||||
|
|
||||||
ErrorHandler:
|
|
||||||
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
|
|
||||||
|
|
||||||
Dim extractNote As String
|
|
||||||
extractNote = ""
|
|
||||||
|
|
||||||
If Not parser.Parse(productModel) Then
|
|
||||||
' 解析失败,添加一行错误记录
|
|
||||||
extractNote = "解析失败: " & parser.ErrorMessage
|
|
||||||
outputData.Add CreateBIPRowArray(orderNumber, productCode, quantity, 1, "", extractNote)
|
|
||||||
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 CreateBIPRowArray(orderNumber, productCode, quantity, 1, "", extractNote)
|
|
||||||
Else
|
|
||||||
' 输出每个匹配的物料
|
|
||||||
Dim item As BomItem
|
|
||||||
Dim lineIndex As Long
|
|
||||||
lineIndex = 1
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
' 创建BIP行数据并添加到集合
|
|
||||||
outputData.Add CreateBIPRowArray(orderNumber, productCode, quantity, _
|
|
||||||
lineIndex, item.Code66, itemNote)
|
|
||||||
|
|
||||||
lineIndex = lineIndex + 1
|
|
||||||
Next item
|
|
||||||
End If
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 函数: CreateBIPRowArray
|
|
||||||
' 功能: 创建BIP上传模板一行数据的数组
|
|
||||||
' 参数: orderNumber - 生产订单号
|
|
||||||
' productCode - 产品编码
|
|
||||||
' quantity - 生产数量
|
|
||||||
' lineIndex - 行号索引(从1开始)
|
|
||||||
' materialCode - 材料编码(66编码)
|
|
||||||
' note - 备注
|
|
||||||
' 返回: Variant() - 包含10个元素的数组
|
|
||||||
'=====================================================================
|
|
||||||
Private Function CreateBIPRowArray(orderNumber As String, _
|
|
||||||
productCode As String, _
|
|
||||||
quantity As String, _
|
|
||||||
lineIndex As Long, _
|
|
||||||
materialCode As String, _
|
|
||||||
note As String) As Variant()
|
|
||||||
Dim rowData(1 To 10) As Variant
|
|
||||||
|
|
||||||
rowData(1) = orderNumber ' 来源单据号(生产订单号)
|
|
||||||
rowData(2) = productCode ' 产品编码
|
|
||||||
rowData(3) = quantity ' 生产数量
|
|
||||||
rowData(4) = ROW_NUMBER_BASE + lineIndex ' 行号 = 基数 + 索引
|
|
||||||
rowData(5) = materialCode ' 材料编码(66编码)
|
|
||||||
rowData(6) = "一般发料" ' 供应方式(固定值)
|
|
||||||
rowData(7) = Date ' 需用日期(当天日期)
|
|
||||||
rowData(8) = "重庆布莱迪仪器仪表有限公司" ' 发料组织(固定值)
|
|
||||||
rowData(9) = quantity ' 计划出库数量(与生产数量一致)
|
|
||||||
rowData(10) = note ' 备注
|
|
||||||
|
|
||||||
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 10)
|
|
||||||
|
|
||||||
' 填充数据到二维数组
|
|
||||||
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)
|
|
||||||
resultData(i, 10) = rowArray(10)
|
|
||||||
Next i
|
|
||||||
|
|
||||||
' 一次性写入工作表(从第2行开始)
|
|
||||||
ws.Range("A2").Resize(rowCount, 10).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 = "计划出库数量"
|
|
||||||
ws.Cells(1, 10).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
|
|
||||||
@@ -1,470 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 模块名: 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
|
|
||||||
|
|
||||||
' 获取工作表对象
|
|
||||||
Dim orderSheet As Worksheet
|
|
||||||
Dim inventorySheet As Worksheet
|
|
||||||
Dim bomSheet As Worksheet
|
|
||||||
|
|
||||||
Set orderSheet = GetOrderSheet()
|
|
||||||
If orderSheet Is Nothing Then
|
|
||||||
MsgBox "未找到[产品订单]工作表!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
Set inventorySheet = GetInventorySheet()
|
|
||||||
If inventorySheet Is Nothing Then
|
|
||||||
MsgBox "未找到[现存量]工作表!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
Set bomSheet = GetBomSheet()
|
|
||||||
If bomSheet Is Nothing Then
|
|
||||||
MsgBox "未找到[平台配置清单]工作表!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 检查订单数据
|
|
||||||
Dim lastRow As Long
|
|
||||||
lastRow = orderSheet.Cells(orderSheet.Rows.Count, 2).End(xlUp).Row
|
|
||||||
If lastRow < 2 Then
|
|
||||||
MsgBox "[产品订单]工作表没有数据!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 初始化BOM提取器
|
|
||||||
Dim bomExtractor As BomExtractor
|
|
||||||
Set bomExtractor = New BomExtractor
|
|
||||||
bomExtractor.SetWorksheet bomSheet
|
|
||||||
|
|
||||||
If Not bomExtractor.LoadBomData Then
|
|
||||||
MsgBox "加载BOM数据失败:" & bomExtractor.GetErrorSummary, vbCritical
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 读取库存数据到字典
|
|
||||||
Dim inventoryData As Object
|
|
||||||
Set inventoryData = LoadInventoryData(inventorySheet)
|
|
||||||
|
|
||||||
If inventoryData.Count = 0 Then
|
|
||||||
MsgBox "[现存量]工作表没有有效数据!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 读取订单数据
|
|
||||||
Dim orders As Collection
|
|
||||||
Set orders = LoadOrderData(orderSheet)
|
|
||||||
|
|
||||||
If orders.Count = 0 Then
|
|
||||||
MsgBox "没有有效的订单数据!", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 解析所有订单的BOM
|
|
||||||
ParseAllOrdersBOM orders, bomExtractor
|
|
||||||
|
|
||||||
' 统计部件总需求
|
|
||||||
Dim componentDemands As Object
|
|
||||||
Set componentDemands = CalculateComponentDemand(orders)
|
|
||||||
|
|
||||||
If componentDemands.Count = 0 Then
|
|
||||||
MsgBox "没有订单包含'部件'类别物料,无需处理库存!", vbInformation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 验证库存
|
|
||||||
Dim validationWarnings As Collection
|
|
||||||
Set validationWarnings = ValidateInventory(componentDemands, inventoryData)
|
|
||||||
|
|
||||||
' 按订单顺序分配库存
|
|
||||||
Dim stats As Statistics
|
|
||||||
AllocateInventory orders, componentDemands, orderSheet, stats
|
|
||||||
|
|
||||||
' 输出结果统计
|
|
||||||
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:
|
|
||||||
MsgBox "部件库存核对异常: " & Err.Description, vbCritical
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 函数: LoadOrderData
|
|
||||||
' 功能: 读取订单数据
|
|
||||||
' 参数: ws - [产品订单]工作表
|
|
||||||
' 返回: Collection - 每个元素是字典对象,包含订单信息
|
|
||||||
'=====================================================================
|
|
||||||
Private Function LoadOrderData(ws As Worksheet) As Collection
|
|
||||||
Set LoadOrderData = New Collection
|
|
||||||
|
|
||||||
Dim lastRow As Long
|
|
||||||
lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
|
|
||||||
|
|
||||||
Dim i As Long
|
|
||||||
For i = 2 To lastRow
|
|
||||||
Dim model As String
|
|
||||||
Dim qty As Variant
|
|
||||||
|
|
||||||
model = Trim(ws.Cells(i, 2).Value) ' B列: 产品型号
|
|
||||||
qty = ws.Cells(i, 3).Value ' C列: 产品数量
|
|
||||||
|
|
||||||
' 跳过空行
|
|
||||||
If model <> "" Then
|
|
||||||
Dim order As Object
|
|
||||||
Set order = CreateObject("Scripting.Dictionary")
|
|
||||||
|
|
||||||
order.Add ORDER_ROW, CLng(i)
|
|
||||||
order.Add ORDER_MODEL, CStr(model)
|
|
||||||
order.Add ORDER_QUANTITY, CDbl(IIf(IsNull(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 i
|
|
||||||
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
|
|
||||||
' 库存中找不到,设置库存为0,并添加警告
|
|
||||||
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
|
|
||||||
' 库存不足,标记为"否"
|
|
||||||
orderSheet.Cells(order(ORDER_ROW), 5).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
|
|
||||||
@@ -1,430 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 模块名: 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
|
|
||||||
|
|
||||||
' 准备输入输出
|
|
||||||
Dim inputSheet As Worksheet
|
|
||||||
Dim outputSheet As Worksheet
|
|
||||||
Dim bomSheet As Worksheet
|
|
||||||
|
|
||||||
' 获取工作表
|
|
||||||
Set inputSheet = GetInputSheet()
|
|
||||||
If inputSheet Is Nothing Then
|
|
||||||
MsgBox "未找到输入工作表,请确保工作簿中有包含订单数据的工作表", vbCritical
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 获取BOM库工作表
|
|
||||||
Set bomSheet = GetBomSheet()
|
|
||||||
If bomSheet Is Nothing Then
|
|
||||||
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
|
|
||||||
MsgBox "加载BOM数据失败:" & BomExtractor.GetErrorSummary, vbCritical
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 处理每个产品型号
|
|
||||||
Dim lastRow As Long
|
|
||||||
lastRow = inputSheet.Cells(inputSheet.Rows.Count, 1).End(xlUp).row
|
|
||||||
|
|
||||||
' 写入输出表头
|
|
||||||
WriteOutputHeader outputSheet
|
|
||||||
|
|
||||||
' 收集所有输出数据
|
|
||||||
Dim outputData As collection
|
|
||||||
Set outputData = New collection
|
|
||||||
|
|
||||||
Dim i As Long
|
|
||||||
Dim modelString As String
|
|
||||||
Dim processedCount As Long
|
|
||||||
|
|
||||||
processedCount = 0
|
|
||||||
|
|
||||||
' 假设产品型号在第1列,从第2行开始
|
|
||||||
For i = 2 To lastRow
|
|
||||||
Dim orderNumber As String
|
|
||||||
orderNumber = Trim(inputSheet.Cells(i, 1).value) ' A列:生产订单号
|
|
||||||
modelString = Trim(inputSheet.Cells(i, 2).value)
|
|
||||||
Dim componentPriority As String
|
|
||||||
componentPriority = Trim(inputSheet.Cells(i, 5).value) ' E列:部件优先
|
|
||||||
|
|
||||||
If modelString <> "" Then
|
|
||||||
' 处理单个型号,收集数据
|
|
||||||
ProcessSingleModel orderNumber, modelString, componentPriority, BomExtractor, outputData
|
|
||||||
processedCount = processedCount + 1
|
|
||||||
End If
|
|
||||||
Next i
|
|
||||||
|
|
||||||
' 批量写入数据到工作表
|
|
||||||
If outputData.Count > 0 Then
|
|
||||||
WriteBatchData outputSheet, outputData
|
|
||||||
End If
|
|
||||||
|
|
||||||
' 格式化输出表
|
|
||||||
FormatOutputSheet outputSheet
|
|
||||||
|
|
||||||
Dim elapsedTime As Double
|
|
||||||
elapsedTime = Timer - startTime
|
|
||||||
|
|
||||||
MsgBox "处理完成!" & vbCrLf & _
|
|
||||||
"处理型号数: " & processedCount & vbCrLf & _
|
|
||||||
"用时: " & Format(elapsedTime, "0.00") & "秒", vbInformation
|
|
||||||
|
|
||||||
' 激活输出表
|
|
||||||
outputSheet.Activate
|
|
||||||
|
|
||||||
Exit Sub
|
|
||||||
|
|
||||||
ErrorHandler:
|
|
||||||
MsgBox "处理异常: " & Err.description, vbCritical
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
'=====================================================================
|
|
||||||
' 过程: ProcessSingleModel
|
|
||||||
' 功能: 处理单个产品型号,将数据添加到输出集合
|
|
||||||
' 参数: orderNumber - 生产订单号
|
|
||||||
' modelString - 产品型号字符串
|
|
||||||
' componentPriority - 部件优先标志("是"或"否")
|
|
||||||
' bomExtractor - BOM提取器对象
|
|
||||||
' outputData - 输出数据集合
|
|
||||||
'=====================================================================
|
|
||||||
Private Sub ProcessSingleModel(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(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(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(orderNumber, modelString, parser.Conditions, itemNote, item)
|
|
||||||
isFirst = False
|
|
||||||
Else
|
|
||||||
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
|
|
||||||
|
|
||||||
' 写入条件字段表头
|
|
||||||
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(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
|
|
||||||
|
|
||||||
' 计算总列数:2 + 条件数 + 8
|
|
||||||
Dim totalCols As Long
|
|
||||||
totalCols = 2 + (UBound(condNames) - LBound(condNames) + 1) + 8
|
|
||||||
|
|
||||||
' 创建数组
|
|
||||||
ReDim rowData(1 To totalCols) As Variant
|
|
||||||
|
|
||||||
Dim col As Long
|
|
||||||
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字段
|
|
||||||
col = col + 7
|
|
||||||
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
|
|
||||||
@@ -1,336 +0,0 @@
|
|||||||
'=====================================================================
|
|
||||||
' 模块名: 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
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
{
|
|
||||||
"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,226 +0,0 @@
|
|||||||
# 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,443 +0,0 @@
|
|||||||
# 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,391 +0,0 @@
|
|||||||
# 执行计划:部件数量核对功能
|
|
||||||
|
|
||||||
## 一、需求概述
|
|
||||||
|
|
||||||
### 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解析失败 | 无法识别部件 | 记录错误日志,跳过该订单 |
|
|
||||||
| 部件编码不一致 | 无法匹配库存 | 严格验证,报错提示 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**是否批准此执行计划?确认后我将开始代码实现。**
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
# 执行计划:添加附加功能(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+一位数字的格式 |
|
|
||||||
| 分隔符不一致 | 解析错误 | 统一将 `.` 替换为 `,` 处理 |
|
|
||||||
| 条件评估逻辑错误 | 匹配错误 | 充分测试各种边界情况 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**是否批准此执行计划?确认后我将开始代码实现。**
|
|
||||||
23
reference_docs/平台配置清单.md
Normal file
23
reference_docs/平台配置清单.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
| | 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