Public Class Form_备份管理
    Dim m_Table As New System.Data.DataTable
    Dim m_InitOver As Boolean = False
    Public Const cst_TABLE_NAME As String = "备份记录表"
    Dim m_BackupManage As New CBackupManage

    Dim m_FixTableNames() As String = {
        "帐号表", "仓库表", "原料库存表", "员工信息表", "库位表_大岭山", "库位表_松山湖", "料号表"
        }

    Private Function CopyTable(ByVal srcTable As String, ByVal aimTable As String) As Boolean
        Dim strCreate As String = "CREATE TABLE `" & aimTable & "` LIKE `" & srcTable & "`"
        Dim strInsert As String = "INSERT INTO `" & aimTable & "` SELECT * FROM `" & srcTable & "`"

        Dim result As Boolean = False
        If SQL_ExeCommand(COL_RIGHTS.BOM管理, strCreate) = True AndAlso
            SQL_ExeCommand(COL_RIGHTS.BOM管理, strInsert) = True Then
            result = True
        End If

        Return result
    End Function

    Private Function CheckTableExist(ByVal table As String, ByRef isExist As Boolean) As Boolean
        Dim strSQL As String = "CHECK TABLE `" & table & "`" 'CHECK TABLE `BOM_U01-ATMEGA328P-TQFP32`
        Dim result As Boolean = False
        m_Table.Rows.Clear()
        m_Table.Columns.Clear()
        isExist = False
        If SQL_Query(COL_RIGHTS.BOM管理, strSQL, m_Table) = True Then
            If m_Table.Rows.Count > 0 Then
                If m_Table.Rows(0).Item(3) = "OK" Then
                    isExist = True
                End If
            End If
            result = True
        End If

        Return result
    End Function
    Private Function DropTable(ByVal tableName As String) As Boolean
        Dim strSQL As String = "DROP TABLE IF EXISTS `" & tableName & "`"
        Dim result As Boolean = False
        If SQL_ExeCommand(COL_RIGHTS.BOM管理, strSQL) = True Then
            result = True
        End If

        Return result
    End Function

    Private Function GetAllBomTableNames(ByRef nameList As ArrayList) As Boolean
        Dim strQuery As String = "SELECT `" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "` FROM `" & CPartNumber.cst_TABLE_NAME & "`"
        Dim rTable As New System.Data.DataTable
        If SQL_Query(COL_RIGHTS.BOM管理, strQuery, rTable) = False Then
            Return False
        End If

        Dim result As Boolean = True
        nameList.Clear()
        For i As Integer = 0 To rTable.Rows.Count - 1
            Try
                Dim name As String = rTable.Rows.Item(i).Item(0)
                nameList.Add(name)
            Catch ex As Exception
                result = False
            End Try
        Next
        Return result
    End Function


    Private Sub bt_BackupNow_Click(sender As Object, e As EventArgs) Handles bt_BackupNow.Click
        Dim result As Boolean = True

        Dim timeNow As Date = Now
        Dim backName As String = "bak_"
        '备份固定表
        For Each pTableName As String In m_FixTableNames
            Dim newTablename As String = backName & pTableName
            DropTable(newTablename)
            If CopyTable(pTableName, newTablename) = False Then
                MsgBox("备份失败,文件:" & pTableName)
                result = False
                Exit For
            End If
        Next

        '备份BOM表
        If result Then
            Dim bomNameList As New ArrayList
            If GetAllBomTableNames(bomNameList) = True Then
                Dim totalCount = bomNameList.Count
                ProgressBar1.Visible = True
                ProgressBar1.Maximum = 0
                ProgressBar1.Maximum = totalCount
                Dim index As Integer = 0
                Dim isExist As Boolean = False
                For Each pTableName As String In bomNameList
                    pTableName = Strings.Trim(pTableName)
                    Dim bomName = "bom_" & pTableName
                    If CheckTableExist(bomName, isExist) = False Then
                        MsgBox("备份失败,文件:" & bomName)
                        result = False
                        Exit For
                    End If

                    If isExist = True Then
                        Dim newTablename As String = backName & bomName
                        DropTable(newTablename)
                        If CopyTable(bomName, newTablename) = False Then
                            MsgBox("备份失败,文件:" & bomName)
                            result = False
                            Exit For
                        End If
                    End If

                    index += 1
                    ProgressBar1.Value = index
                    ProgressBar1.Refresh()
                    Application.DoEvents()
                Next

                ProgressBar1.Visible = False
            End If

            bomNameList.Clear()
        End If

        If result Then
            Dim timeStr As String = timeNow.ToString("yyyyMMddHHmmss")

            m_BackupManage.SetBackupTime(timeStr)

            RefreshUpdateTime()

            MsgBox("备份完成")
        Else
            MsgBox("备份失败")
        End If
    End Sub

    Private Sub RefreshUpdateTime()
        Dim rTimeStr As String = ""
        If m_BackupManage.GetBackupTime(rTimeStr) = True Then
            lbl_LatestTime.Text = Strings.Left(rTimeStr, 4) & "年" & Strings.Mid(rTimeStr, 5, 2) & "月" & Strings.Mid(rTimeStr, 7, 2) & "日" &
                Strings.Mid(rTimeStr, 9, 2) & ":" & Strings.Mid(rTimeStr, 11, 2) & ":" & Strings.Mid(rTimeStr, 13, 2)
        Else
            lbl_LatestTime.Text = "----"
        End If
    End Sub

    Private Sub Form_备份管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RefreshUpdateTime()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Dim timeNow As Date = Now
        Dim timeStr As String = timeNow.ToString("yyyyMMddHHmmss")

        m_BackupManage.SetBackupTime(timeStr)

        RefreshUpdateTime()
    End Sub
End Class