Imports System.Data.SQLite
Imports UTS_Core.Database.Sqlite
Imports UTS_Core.UTSModule.DatabaseTable

Namespace UTSModule.Project
    Public Class StationPlanCommandHelper
        Sub New()
            Params = New List(Of Station.CommandParam)()
        End Sub


        Public Property CommandIndex() As String

        Public Property CommandType() As String

        Public Property CommandName() As String

        Public Property CommandDesc() As String

        Public Property Retry() As String

        Public Property RetryInterval() As String

        Public Property Record() As String

        Public Property RecordName() As String

        Public Property PassGoto() As String

        Public Property FailGoto() As String

        Public Property ErrorCode() As String

        Public Property ErrorMessage() As String

        Public Property Help() As String

        Public Property Params() As List(Of Station.CommandParam)

        Private Shared Function SearchCommand(path As String, password As String) As DataTable
            Dim connectString As String = CommandHelpers.ConnectionString(path, password)
            Using sqliteConn As New SQLiteConnection(connectString)
                sqliteConn.Open()
                Dim tableName As String = CommandHelperTable.TableName
                Dim colNames As New List(Of String)
                colNames.Add(CommandHelperTable.ColNamesEnum.ID.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.CommandIndex.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.CommandType.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.CommandName.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.CommandDesc.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.Retry.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.RetryInterval.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.Record.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.RecordName.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.PassGoto.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.FailGoto.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ErrorCode.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ErrorMessage.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.Help.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc1.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType1.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower1.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper1.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue1.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc2.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType2.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower2.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper2.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue2.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc3.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType3.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower3.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper3.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue3.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc4.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType4.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower4.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper4.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue4.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc5.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType5.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower5.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper5.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue5.ToString())

                colNames.Add(CommandHelperTable.ColNamesEnum.ParamDesc6.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamType6.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamLower6.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamUpper6.ToString())
                colNames.Add(CommandHelperTable.ColNamesEnum.ParamValue6.ToString())

                Return Executor.Search(sqliteConn, tableName, colNames)
                sqliteConn.Close()
            End Using
        End Function

        Public Function InitCommandHelper(dtRow As DataRow) As Boolean
            CommandIndex = dtRow.Item(CommandHelperTable.ColNamesEnum.CommandIndex).ToString()
            CommandType = dtRow.Item(CommandHelperTable.ColNamesEnum.CommandType).ToString()
            CommandName = dtRow.Item(CommandHelperTable.ColNamesEnum.CommandName).ToString()
            CommandDesc = dtRow.Item(CommandHelperTable.ColNamesEnum.CommandDesc).ToString()

            Retry = dtRow.Item(CommandHelperTable.ColNamesEnum.Retry).ToString()
            RetryInterval = dtRow.Item(CommandHelperTable.ColNamesEnum.RetryInterval).ToString()
            Record = dtRow.Item(CommandHelperTable.ColNamesEnum.Record).ToString()
            RecordName = dtRow.Item(CommandHelperTable.ColNamesEnum.RecordName).ToString()
            PassGoto = dtRow.Item(CommandHelperTable.ColNamesEnum.PassGoto).ToString()
            FailGoto = dtRow.Item(CommandHelperTable.ColNamesEnum.FailGoto).ToString()
            ErrorCode = dtRow.Item(CommandHelperTable.ColNamesEnum.ErrorCode).ToString()
            ErrorMessage = dtRow.Item(CommandHelperTable.ColNamesEnum.ErrorMessage).ToString()
            Help = dtRow.Item(CommandHelperTable.ColNamesEnum.Help).ToString()

            Params.Clear()

            Dim param As New Station.CommandParam
            param.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc1).ToString()
            param.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType1).ToString()
            param.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower1).ToString()
            param.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper1).ToString()
            param.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue1).ToString()
          
            if String.IsNullOrEmpty(param.Desc) Then param.Desc = "未使用"
            Params.Add(param)

            Dim param2 As New Station.CommandParam
            param2.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc2).ToString()
            param2.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType2).ToString()
            param2.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower2).ToString()
            param2.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper2).ToString()
            param2.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue2).ToString()
          
            if String.IsNullOrEmpty(param2.Desc) Then param2.Desc = "未使用"
            Params.Add(param2)

            Dim param3 As New Station.CommandParam
            param3.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc3).ToString()
            param3.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType3).ToString()
            param3.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower3).ToString()
            param3.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper3).ToString()
            param3.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue3).ToString()
           
            if String.IsNullOrEmpty(param3.Desc) Then param3.Desc = "未使用"
            Params.Add(param3)

            Dim param4 As New Station.CommandParam
            param4.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc4).ToString()
            param4.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType4).ToString()
            param4.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower5).ToString()
            param4.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper5).ToString()
            param4.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue5).ToString()
            
            if String.IsNullOrEmpty(param4.Desc) Then param4.Desc = "未使用"
            Params.Add(param4)

            Dim param5 As New Station.CommandParam
            param5.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc5).ToString()
            param5.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType5).ToString()
            param5.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower5).ToString()
            param5.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper5).ToString()
            param5.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue5).ToString()
            
            if String.IsNullOrEmpty(param5.Desc) Then param5.Desc = "未使用"
            Params.Add(param5)

            Dim param6 As New Station.CommandParam
            param6.Desc = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamDesc6).ToString()
            param6.Type = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamType6).ToString()
            param6.LowerLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamLower6).ToString()
            param6.UpperLimit = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamUpper6).ToString()
            param6.Value = dtRow.Item(CommandHelperTable.ColNamesEnum.ParamValue6).ToString()
           
            if String.IsNullOrEmpty(param6.Desc) Then param6.Desc = "未使用"
            Params.Add(param6)

            Return True
        End Function

        Public Shared Function InitCommandHelper(dtCommand As DataTable) As Dictionary(Of String, StationPlanCommandHelper)
            Dim dicCommandHelpers As New Dictionary(Of String, StationPlanCommandHelper)

            For row As Integer = 0 To dtCommand.Rows.Count - 1
                Dim commandHelper As New StationPlanCommandHelper
                commandHelper.InitCommandHelper(dtCommand.Rows(row))

                dicCommandHelpers.Add(commandHelper.CommandIndex, commandHelper)
            Next

            Return dicCommandHelpers
        End Function


        ''' <summary>
        ''' 初始化流程站帮助信息
        ''' 从本地数据库中读取
        ''' </summary>
        ''' <param name="path">本地数据库路径</param>
        ''' <param name="password">本地数据库密码</param>
        ''' <returns>初始化是否成功</returns>
        Public Shared Function InitCommandHelper(path As String, password As String) As Dictionary(Of String, StationPlanCommandHelper)
            '查询数据表
            Dim dtCommand As DataTable = SearchCommand(path, password)

            '赋值
            Return InitCommandHelper(dtCommand)
        End Function
    End Class
End Namespace