' ========================================
' 类模块: clsModelParser
' 用途: 解析产品型号，提取各部分代码
' 示例: YTHN-100.A0.532.M203.M16.Y3|BP-095.2312.M16.PA3
' ========================================
Option Explicit

' ========================================
' 公共属性
' ========================================
Public RawModel As String           ' 原始完整型号
Public HeaderModel As String        ' 表头型号部分
Public DialModel As String          ' 表盘型号部分
Public AccessoryModel As String     ' 附件型号部分
Public FlangeModel As String        ' 法兰隔膜型号部分

' 表头各部分
Public ModelType As String          ' 型号 (如 YTHN)
Public Diameter As String           ' 公称外径 (如 100)
Public InstallForm As String        ' 安装形式 (如 A0)
Public ShellForm As String          ' 壳体形式 (如 532)
Public ConnectionCode As String     ' 过程连接&材质代码 (如 M203)
Public RangeCode As String          ' 量程范围代码 (如 M16)
Public Characteristics As String    ' 仪表特性 (如 Y3)

' ========================================
' 私有变量
' ========================================
Private m_IsValid As Boolean        ' 解析是否成功
Private m_ErrorMessage As String    ' 错误信息

' ========================================
' ParseModel 方法
' 功能: 解析产品型号字符串
' 参数: modelStr - 完整的产品型号字符串
' 返回: Boolean - 解析是否成功
' 示例: parser.ParseModel("YTHN-100.A0.532.M203.M16.Y3|BP-095.2312.M16.PA3")
' ========================================
Public Function ParseModel(modelStr As String) As Boolean
    On Error GoTo ErrorHandler

    ' 初始化
    m_IsValid = False
    m_ErrorMessage = ""
    RawModel = Trim(modelStr)

    ' 验证输入
    If RawModel = "" Then
        m_ErrorMessage = "型号字符串为空"
        ParseModel = False
        Exit Function
    End If

    ' 第一步: 按 | 分割各部分
    Dim parts() As String
    parts = Split(RawModel, "|")

    If UBound(parts) >= 0 Then HeaderModel = Trim(parts(0))
    If UBound(parts) >= 1 Then DialModel = Trim(parts(1))
    If UBound(parts) >= 2 Then AccessoryModel = Trim(parts(2))
    If UBound(parts) >= 3 Then FlangeModel = Trim(parts(3))

    ' 第二步: 解析表头部分 (必须存在)
    If HeaderModel = "" Then
        m_ErrorMessage = "表头型号为空"
        ParseModel = False
        Exit Function
    End If

    ' 解析表头
    If Not ParseHeader(HeaderModel) Then
        ParseModel = False
        Exit Function
    End If

    m_IsValid = True
    ParseModel = True
    Exit Function

ErrorHandler:
    m_ErrorMessage = "解析出错: " & Err.description
    m_IsValid = False
    ParseModel = False
End Function

' ========================================
' ParseHeader 方法 (私有)
' 功能: 解析表头型号部分
' 格式: [型号]-[公称外径].[安装形式].[壳体形式].[过程连接&材质].[量程范围].[仪表特性]
' 示例: YTHN-100.A0.532.M203.M16.Y3
' ========================================
Private Function ParseHeader(headerStr As String) As Boolean
    On Error GoTo ErrorHandler

    ' 按 - 分割型号和参数部分
    Dim mainParts() As String
    mainParts = Split(headerStr, "-")

    If UBound(mainParts) < 1 Then
        m_ErrorMessage = "表头格式错误: 缺少 - 分隔符"
        ParseHeader = False
        Exit Function
    End If

    ' 提取型号
    ModelType = Trim(mainParts(0))

    ' 按 . 分割参数部分
    Dim params() As String
    params = Split(mainParts(1), ".")

    ' 验证参数数量 (至少应该有5个部分)
    If UBound(params) < 4 Then
        m_ErrorMessage = "表头参数不足: 需要至少5个参数段"
        ParseHeader = False
        Exit Function
    End If

    ' 提取各参数
    Diameter = Trim(params(0))              ' 公称外径
    InstallForm = Trim(params(1))           ' 安装形式
    ShellForm = Trim(params(2))             ' 壳体形式
    ConnectionCode = Trim(params(3))        ' 过程连接&材质
    RangeCode = Trim(params(4))             ' 量程范围

    ' 仪表特性 (可选)
    If UBound(params) >= 5 Then
        Characteristics = Trim(params(5))
    Else
        Characteristics = ""
    End If

    ParseHeader = True
    Exit Function

ErrorHandler:
    m_ErrorMessage = "解析表头出错: " & Err.description
    ParseHeader = False
End Function

' ========================================
' GetThreadCode 方法
' 功能: 从过程连接代码中提取螺纹代码
' 规则: 去掉最后一位数字
' 示例: M203 -> M20
' ========================================
Public Function GetThreadCode() As String
    If ConnectionCode = "" Then
        GetThreadCode = ""
        Exit Function
    End If

    ' 去掉最后一位字符 (假设最后一位是材质代码)
    If Len(ConnectionCode) > 1 Then
        GetThreadCode = Left(ConnectionCode, Len(ConnectionCode) - 1)
    Else
        GetThreadCode = ConnectionCode
    End If
End Function

' ========================================
' GetMaterialCode 方法
' 功能: 从过程连接代码中提取材质代码
' 规则: 取最后一位数字
' 示例: M203 -> 3
' ========================================
Public Function GetMaterialCode() As String
    If ConnectionCode = "" Then
        GetMaterialCode = ""
        Exit Function
    End If

    ' 取最后一位字符
    GetMaterialCode = Right(ConnectionCode, 1)
End Function

' ========================================
' GetRangeCode 方法
' 功能: 获取量程代码
' 规则: 直接返回
' 示例: M16 -> M16
' ========================================
Public Function GetRangeCode() As String
    GetRangeCode = RangeCode
End Function

' ========================================
' IsValid 属性
' 功能: 返回解析是否成功
' ========================================
Public Property Get IsValid() As Boolean
    IsValid = m_IsValid
End Property

' ========================================
' ErrorMessage 属性
' 功能: 返回错误信息
' ========================================
Public Property Get ErrorMessage() As String
    ErrorMessage = m_ErrorMessage
End Property

' ========================================
' ToString 方法
' 功能: 返回解析结果的字符串表示 (用于调试)
' ========================================
Public Function ToString() As String
    Dim result As String
    result = "【型号解析结果】" & vbCrLf
    result = result & "原始型号: " & RawModel & vbCrLf
    result = result & "表头型号: " & HeaderModel & vbCrLf
    result = result & "表盘型号: " & DialModel & vbCrLf
    result = result & vbCrLf
    result = result & "【表头各部分】" & vbCrLf
    result = result & "  型号: " & ModelType & vbCrLf
    result = result & "  公称外径: " & Diameter & vbCrLf
    result = result & "  安装形式: " & InstallForm & vbCrLf
    result = result & "  壳体形式: " & ShellForm & vbCrLf
    result = result & "  过程连接&材质: " & ConnectionCode & vbCrLf
    result = result & "  量程范围: " & RangeCode & vbCrLf
    result = result & "  仪表特性: " & Characteristics & vbCrLf
    result = result & vbCrLf
    result = result & "【提取代码】" & vbCrLf
    result = result & "  螺纹代码: " & GetThreadCode() & vbCrLf
    result = result & "  材质代码: " & GetMaterialCode() & vbCrLf
    result = result & "  量程代码: " & GetRangeCode() & vbCrLf

    ToString = result
End Function