' ========================================
' 类模块: clsConditionExtractor
' 用途: 从型号解析器中提取物料选择条件
' ========================================
Option Explicit

' ========================================
' 私有成员变量
' ========================================
Private m_Conditions As Object      ' Dictionary: 变量名 -> 条件值
Private m_ExtractionRules As Object ' Dictionary: 提取规则配置

' ========================================
' 类初始化
' ========================================
Private Sub Class_Initialize()
    Set m_Conditions = CreateObject("Scripting.Dictionary")
    Set m_ExtractionRules = CreateObject("Scripting.Dictionary")

    ' 初始化提取规则
    InitializeRules
End Sub

' ========================================
' InitializeRules 方法 (私有)
' 功能: 初始化条件提取规则
' 说明: 这里配置所有需要提取的条件及其提取方法
' ========================================
Private Sub InitializeRules()
    ' 规则格式: Dictionary(变量名) = Array(源字段, 提取方法)

    ' 规则1: 过程连接 (gclj)
    ' 从 ConnectionCode 中提取，去掉最后一位数字
    m_ExtractionRules("gclj") = Array("ConnectionCode", "RemoveLastDigit")

    ' 规则2: 接液材质 (jycz)
    ' 从 ConnectionCode 中提取，取最后一位数字
    m_ExtractionRules("jycz") = Array("ConnectionCode", "GetLastDigit")

    ' 规则3: 量程范围 (lcfw)
    ' 从 RangeCode 中直接提取
    m_ExtractionRules("lcfw") = Array("RangeCode", "Direct")

    ' 未来可以在这里添加更多提取规则...
    ' 例如:
    ' m_ExtractionRules("bplx") = Array("DialCode", "Direct")  ' 表盘类型
End Sub

' ========================================
' ExtractConditions 方法
' 功能: 从型号解析器中提取所有条件
' 参数: parser - clsModelParser对象
' 返回: Dictionary对象，包含所有提取的条件
' ========================================
Public Function ExtractConditions(parser As clsModelParser) As Object
    ' 清空现有条件
    Set m_Conditions = CreateObject("Scripting.Dictionary")

    ' 验证解析器有效性
    If Not parser.IsValid Then
        Set ExtractConditions = m_Conditions
        Exit Function
    End If

    ' 遍历所有提取规则
    Dim varName As Variant
    For Each varName In m_ExtractionRules.Keys
        Dim ruleInfo As Variant
        ruleInfo = m_ExtractionRules(varName)

        Dim sourceField As String
        Dim extractMethod As String
        sourceField = ruleInfo(0)
        extractMethod = ruleInfo(1)

        ' 提取条件值
        Dim conditionValue As String
        conditionValue = ExtractValue(parser, sourceField, extractMethod)

        ' 添加到条件字典
        If conditionValue <> "" Then
            m_Conditions(CStr(varName)) = conditionValue
        End If
    Next varName

    Set ExtractConditions = m_Conditions
End Function

' ========================================
' ExtractValue 方法 (私有)
' 功能: 根据规则从解析器中提取单个值
' 参数:
'   parser - clsModelParser对象
'   sourceField - 源字段名称
'   extractMethod - 提取方法名称
' 返回: 提取的条件值
' ========================================
Private Function ExtractValue(parser As clsModelParser, _
                              sourceField As String, _
                              extractMethod As String) As String
    Dim sourceValue As String

    ' 获取源字段值
    Select Case sourceField
        Case "ConnectionCode"
            sourceValue = parser.ConnectionCode
        Case "RangeCode"
            sourceValue = parser.RangeCode
        Case "ModelType"
            sourceValue = parser.ModelType
        Case "Diameter"
            sourceValue = parser.Diameter
        Case "InstallForm"
            sourceValue = parser.InstallForm
        Case "ShellForm"
            sourceValue = parser.ShellForm
        Case "Characteristics"
            sourceValue = parser.Characteristics
        Case Else
            sourceValue = ""
    End Select

    ' 应用提取方法
    Select Case extractMethod
        Case "Direct"
            ' 直接使用
            ExtractValue = sourceValue

        Case "RemoveLastDigit"
            ' 去掉最后一位字符
            If Len(sourceValue) > 1 Then
                ExtractValue = Left(sourceValue, Len(sourceValue) - 1)
            Else
                ExtractValue = sourceValue
            End If

        Case "GetLastDigit"
            ' 取最后一位字符
            If Len(sourceValue) > 0 Then
                ExtractValue = Right(sourceValue, 1)
            Else
                ExtractValue = ""
            End If

        Case "GetFirstChar"
            ' 取第一个字符
            If Len(sourceValue) > 0 Then
                ExtractValue = Left(sourceValue, 1)
            Else
                ExtractValue = ""
            End If

        Case Else
            ' 未知方法，返回空
            ExtractValue = ""
    End Select
End Function

' ========================================
' GetConditionValue 方法
' 功能: 获取单个条件值
' 参数: varName - 变量名
' 返回: 条件值，如果不存在返回空字符串
' ========================================
Public Function GetConditionValue(varName As String) As String
    If m_Conditions.Exists(varName) Then
        GetConditionValue = m_Conditions(varName)
    Else
        GetConditionValue = ""
    End If
End Function

' ========================================
' AddCondition 方法
' 功能: 手动添加条件 (用于特殊情况)
' 参数:
'   varName - 变量名
'   value - 条件值
' ========================================
Public Sub AddCondition(varName As String, value As String)
    m_Conditions(varName) = value
End Sub

' ========================================
' GetConditions 属性
' 功能: 获取所有条件的Dictionary对象
' ========================================
Public Property Get conditions() As Object
    Set conditions = m_Conditions
End Property

' ========================================
' ToString 方法
' 功能: 返回条件的字符串表示 (用于调试)
' ========================================
Public Function ToString() As String
    Dim result As String
    result = "【提取的条件】" & vbCrLf

    If m_Conditions.Count = 0 Then
        result = result & "  (无条件)" & vbCrLf
    Else
        Dim key As Variant
        For Each key In m_Conditions.Keys
            result = result & "  " & key & " = " & m_Conditions(key) & vbCrLf
        Next key
    End If

    ToString = result
End Function

' ========================================
' AddExtractionRule 方法
' 功能: 动态添加新的提取规则 (用于扩展)
' 参数:
'   varName - 变量名
'   sourceField - 源字段名称
'   extractMethod - 提取方法名称
' 示例: extractor.AddExtractionRule "bplx", "DialCode", "Direct"
' ========================================
Public Sub AddExtractionRule(varName As String, _
                             sourceField As String, _
                             extractMethod As String)
    m_ExtractionRules(varName) = Array(sourceField, extractMethod)
End Sub

' ========================================
' GetExtractionRules 方法
' 功能: 获取当前所有提取规则 (用于调试)
' 返回: Dictionary对象
' ========================================
Public Function GetExtractionRules() As Object
    Set GetExtractionRules = m_ExtractionRules
End Function