
Imports System.Text
Imports System.Xml

Namespace UTSModule.Station
    Public Class StationPackagePlan
        Inherits StationPlan

        Sub New(packet As StationPacket)
            MyBase.New(packet)

            CreateStationPlan()
        End Sub

        ''' <summary>
        ''' 校验条码未测试后处理方式
        ''' </summary>
        ''' <returns></returns>
        Public Property CheckSnTested As CheckStatus

        ''' <summary>
        ''' 校验条码已参与包装后处理方式
        ''' </summary>
        ''' <returns></returns>
        Public Property CheckSnPackaged As CheckStatus

        ''' <summary>
        ''' 校验条码不属于指定MO后处理方式
        ''' </summary>
        ''' <returns></returns>
        Public Property CheckSnMO As CheckStatus

        ''' <summary>
        ''' 保存图像的数量
        ''' </summary>
        ''' <returns></returns>
        Public Property PictureCount As Integer

        ''' <summary>
        ''' 解锁密码
        ''' </summary>
        ''' <returns></returns>
        Public Property UnlockKey As String

        ''' <summary>
        ''' 条码类型
        ''' </summary>
        ''' <returns></returns>
        Public Property BarCodeType As BarcodeTypes

        ''' <summary>
        ''' 缺失条码数量后的处理方式
        ''' </summary>
        ''' <returns></returns>
        Public Property MissingQuantity As MissCountStatus


        ''' <summary>
        ''' 缺失条码数量后的解锁密码
        ''' </summary>
        ''' <returns></returns>
        Public Property MissingQuantityKey As String

        ''' <summary>
        ''' 是否自动打印
        ''' </summary>
        ''' <returns></returns>
        Public Property AutoPrint As Boolean

        ''' <summary>
        ''' 发生错误后报警文件
        ''' </summary>
        ''' <returns></returns>
        Public Property ErrorSound As String

        ''' <summary>
        ''' 模板文件名
        ''' </summary>
        ''' <returns></returns>
        Public Property TemplateFile As String

        ''' <summary>
        ''' 箱号编辑密码
        ''' </summary>
        ''' <returns></returns>
        Public Property BoxMumEditKey As String

        ''' <summary>
        ''' 公司Logo文件
        ''' </summary>
        ''' <returns></returns>
        Public Property CompanyLogo As String

        ''' <summary>
        ''' 公司名称
        ''' </summary>
        ''' <returns></returns>
        Public Property CompanyName As String

        ''' <summary>
        ''' 箱号规则
        ''' </summary>
        ''' <returns></returns>
        Public Property BoxNumText As String


        ''' <summary>
        ''' 箱号条码
        ''' </summary>
        ''' <returns></returns>
        Public Property BoxBarCode As String

        ''' <summary>
        ''' 前置站位
        ''' </summary>
        ''' <returns></returns>
        Public Property ForwardPosition As String

        ''' <summary>
        ''' 后置站位
        ''' </summary>
        ''' <returns></returns>
        Public Property RearPosition As String
        ''' <summary>
        ''' 软件版本
        ''' </summary>
        ''' <returns></returns>
        Public Property Versions As String



        Public Overrides Sub CreateStationPlan()
            CheckSnTested = CheckStatus.Fail
            CheckSnPackaged = CheckStatus.Fail
            CheckSnMO = CheckStatus.Fail
            UnlockKey = ""
            BarCodeType = BarcodeTypes.AsTemplate
            MissingQuantity = MissCountStatus.Key
            MissingQuantityKey = ""
            AutoPrint = False
            ErrorSound = ""
            TemplateFile = ""
            BoxMumEditKey = ""
            CompanyLogo = ""
            CompanyName = ""
            BoxNumText = ""
            BoxBarCode = ""
            ForwardPosition = ""
            RearPosition = ""
            Versions = ""
        End Sub

        Public Overrides Sub SaveFile(path As String)
            Dim xws As New XmlWriterSettings
            With xws
                .Indent = True
                .NewLineOnAttributes = False
                .Encoding = New UTF8Encoding(False)
            End With

            Using xw As XmlWriter = XmlWriter.Create(path, xws)
                xw.WriteStartDocument()
                xw.WriteStartElement($"UtsStationPlan") '创建跟节点

                xw.WriteStartElement($"Properties") '创建表属性节点

                '测试流程的属性
                xw.WriteElementString("CheckSnTested", CheckSnTested.ToString)
                xw.WriteElementString("CheckSnPackaged", CheckSnPackaged.ToString)
                xw.WriteElementString("CheckSnMO", CheckSnMO.ToString)
                xw.WriteElementString("PictureCount", PictureCount.ToString)
                xw.WriteElementString("UnlockKey", UnlockKey)
                xw.WriteElementString("BarCodeType", BarCodeType.ToString)
                xw.WriteElementString("MissingQuantity", MissingQuantity.ToString)
                xw.WriteElementString("MissingQuantityKey", MissingQuantityKey)
                xw.WriteElementString("AutoPrint", AutoPrint.ToString)
                xw.WriteElementString("ErrorSound", ErrorSound)
                xw.WriteElementString("TemplateFile", TemplateFile)
                xw.WriteElementString("BoxMumEditKey", BoxMumEditKey)
                xw.WriteElementString("CompanyLogo", CompanyLogo)
                xw.WriteElementString("CompanyName", CompanyName)
                xw.WriteElementString("BoxNumText", BoxNumText)
                xw.WriteElementString("ForwardPosition", ForwardPosition)
                xw.WriteElementString("RearPosition", RearPosition)
                xw.WriteElementString("Versions", Versions)
                xw.WriteEndElement()

                xw.WriteEndElement()
                xw.WriteEndDocument()
            End Using
        End Sub


        ''' <summary>
        ''' 读取测试站属性
        ''' </summary>
        ''' <param name="nodeList"></param>
        Private Sub LoadProperties(nodeList As XmlNodeList)
            Dim xe As XmlElement
            For Each node As XmlNode In nodeList
                xe = CType(node, XmlElement)
                Select Case xe.LocalName
                    Case "CheckSnTested"
                        If [Enum].TryParse(xe.InnerText, CheckSnTested) = False Then
                            CheckSnTested = CheckStatus.Fail
                        End If
                    Case "CheckSnPackaged"
                        If [Enum].TryParse(xe.InnerText, CheckSnPackaged) = False Then
                            CheckSnPackaged = CheckStatus.Fail
                        End If
                    Case "CheckSnMO"
                        If [Enum].TryParse(xe.InnerText, CheckSnMO) = False Then
                            CheckSnMO = CheckStatus.Fail
                        End If
                    Case "PictureCount"
                        If Integer.TryParse(xe.InnerText, PictureCount) = False Then
                            PictureCount = 1
                        End If
                    Case "UnlockKey"
                        UnlockKey = xe.InnerText
                    Case "BarCodeType"
                        If [Enum].TryParse(xe.InnerText, BarCodeType) = False Then
                            BarCodeType = BarcodeTypes.AsTemplate
                        End If
                    Case "MissingQuantity"
                        If [Enum].TryParse(xe.InnerText, MissingQuantity) = False Then
                            MissingQuantity = MissCountStatus.Key
                        End If
                    Case "MissingQuantityKey"
                        MissingQuantityKey = xe.InnerText
                    Case "AutoPrint"
                        If Boolean.TryParse(xe.InnerText, AutoPrint) = False Then
                            AutoPrint = False
                        End If
                    Case "ErrorSound"
                        ErrorSound = xe.InnerText
                    Case "TemplateFile"
                        TemplateFile = xe.InnerText
                    Case "BoxMumEditKey"
                        BoxMumEditKey = xe.InnerText
                    Case "CompanyLogo"
                        CompanyLogo = xe.InnerText
                    Case "CompanyName"
                        CompanyName = xe.InnerText
                    Case "BoxNumText"
                        BoxNumText = xe.InnerText
                    Case "BoxBarCode"
                        BoxBarCode = xe.InnerText
                    Case "RearPosition"
                        RearPosition = xe.InnerText
                    Case "ForwardPosition"
                        ForwardPosition = xe.InnerText
                    Case "Versions"
                        Versions = xe.InnerText
                    Case Else
                        'Throw New Exception($"StationPackagePlan LoadProperties Unknown LocalName:{xe.LocalName}")
                End Select
            Next
        End Sub



        Public Overrides Sub LoadFile(path As String)
            Dim xd As New XmlDocument()
            xd.Load(path)

            Dim xe As XmlElement
            Dim nodeList As XmlNodeList = xd.SelectSingleNode($"UtsStationPlan").ChildNodes
            For Each node As XmlNode In nodeList
                xe = CType(node, XmlElement)
                Select Case xe.LocalName
                    Case "Properties"
                        LoadProperties(xe.ChildNodes)
                    Case Else
                        Throw New Exception($"StationPackagePlan LoadXml Unknown LocalName:{xe.LocalName}")
                End Select
            Next
        End Sub



        ''' <summary>
        ''' 校验条码失败后处理方式
        ''' </summary>
        Enum CheckStatus
            ''' <summary>
            ''' 不处理
            ''' </summary>
            None
            ''' <summary>
            ''' 校验失败
            ''' </summary>
            Fail
            ''' <summary>
            ''' 程序锁定，需要管理员密码解锁，并写入注册表
            ''' </summary>
            Lock
            ''' <summary>
            ''' 忽略错误继续录入
            ''' </summary>
            Ignore
        End Enum

        ''' <summary>
        ''' 条码总数缺少的处理方式
        ''' </summary>
        Enum MissCountStatus
            ''' <summary>
            ''' 允许录入
            ''' </summary>
            Yes
            ''' <summary>
            ''' 不允许录入
            ''' </summary>
            No
            ''' <summary>
            ''' 需要输入密码解锁
            ''' </summary> 
            Key
        End Enum


        ''' <summary>
        ''' 条码单元格的类型
        ''' </summary>
        Enum BarcodeTypes
            ''' <summary>
            ''' 以模板内容为准
            ''' </summary>
            AsTemplate

            ''' <summary>
            ''' CODE39支持的字符：空格, 数字, 大写字母, $, %, *, +, -, ., /
            ''' </summary>
            CODE39

            ''' <summary>
            ''' CODE128A支持的字符：空格, 数字, 大写字母, 标点符号, @, #, $, %, ^, +, -, *, /
            ''' </summary>
            CODE128A

            ''' <summary>
            ''' CODE128B支持的字符：空格, 数字, 小写字母, 大写字母, 标点符号, @, #, $, %, ^, +, -, *, /, ~
            ''' </summary>
            CODE128B

            ''' <summary>
            ''' CODE128C只能包含数字
            ''' </summary>
            CODE128C

            ''' <summary>
            ''' EAN128只能包含数字
            ''' </summary>
            EAN128

            ''' <summary>
            ''' EAN13是商品编码，必须是13位数字，第13位是根据前面12位运算后得到的
            ''' </summary>
            EAN13

            ''' <summary>
            ''' Interleaved2of5只能包含数字，数字的长度必须为偶数
            ''' </summary>
            Interleaved2of5
        End Enum

    End Class
End Namespace
