' ========================================
' 类模块: clsCategory
' 用途: 表示物料类别及其层级关系
' ========================================
Option Explicit

Public categoryName As String
Public ParentCategoryName As String
Public materials As collection            ' 存储 clsMaterialItem 对象
Public SubCategories As collection        ' 存储子类别 clsCategory 对象
Public IsLeafCategory As Boolean          ' 是否叶子类别(无子类别)
Public CategorySelectCondition As String  ' 类别选用条件（用于判断该类别是否需要）

Private Sub Class_Initialize()
    Set materials = New collection
    Set SubCategories = New collection
    IsLeafCategory = True
End Sub

' 添加物料
Public Sub AddMaterial(mat As clsMaterialItem)
    materials.Add mat, mat.code
End Sub

' 添加子类别
Public Sub AddSubCategory(cat As clsCategory)
    SubCategories.Add cat, cat.categoryName
    IsLeafCategory = False
End Sub

' 获取物料(按代号)
Public Function GetMaterial(code As String) As clsMaterialItem
    On Error Resume Next
    Set GetMaterial = materials(code)
    On Error GoTo 0
End Function

' 检查是否有子类别
Public Function HasSubCategories() As Boolean
    HasSubCategories = (SubCategories.Count > 0)
End Function

' ========================================
' IsRequiredForModel 方法
' 功能: 根据型号条件判断该类别是否需要
' 参数:
'   conditions - 从型号提取的条件字典 (Dictionary对象)
' 返回:
'   True  - 该类别需要
'   False - 该类别不需要
'
' 判断逻辑:
'   1. 如果 CategorySelectCondition 为空字符串
'      → 该类别总是需要（无条件限制）
'   2. 如果 CategorySelectCondition 不为空
'      → 使用 clsConditionMatcher 匹配条件
'      → 匹配成功 → 需要
'      → 匹配失败 → 不需要
'
' 示例:
'   假设型号条件为: lcfw=M16, gclj=M20
'
'   情况1: CategorySelectCondition = ""
'     → IsRequiredForModel(conditions) = True
'
'   情况2: CategorySelectCondition = "lcfw=M16"
'     → IsRequiredForModel(conditions) = True (匹配)
'
'   情况3: CategorySelectCondition = "lcfw=M02"
'     → IsRequiredForModel(conditions) = False (不匹配)
'
'   情况4: CategorySelectCondition = "gclj=M20 AND jycz=1"
'     → IsRequiredForModel(conditions) = True (匹配)
' ========================================
Public Function IsRequiredForModel(conditions As Object) As Boolean
    If Me.CategorySelectCondition = "" Then
        ' 选用条件为空，该类别总是需要
        IsRequiredForModel = True
    Else
        ' 需要用 conditions 去匹配 CategorySelectCondition
        Dim matcher As New clsConditionMatcher
        IsRequiredForModel = matcher.IsMatch(Me.CategorySelectCondition, conditions)
    End If
End Function