Public Class Form_QueryStorageRecord
    Dim m_Table As New System.Data.DataTable
    Dim 仓库列表 As New ArrayList
    Dim m_PartNumber As New CPartNumber
    Dim m_InitOver As Boolean = False

    Public Enum COLS
        序号
        日期
        时间
        仓库
        库位
        入库
        出库
        库位库存
        总结余
        用途
        操作员ID
        max
    End Enum

    Public Shared m_COLS_NAME() As String = {
        "序号",
        "日期",
        "时间",
        "仓库",
        "库位",
        "入库",
        "出库",
        "库位库存",
        "总结余",
        "用途",
        "操作员ID"
        }


    Private Sub Set_GridTitles()
        Grid1.NewFile()

        Grid1.Rows = 1
        Grid1.Cols = COLS.max

        For i As Integer = 0 To COLS.max - 1
            Grid1.Cell(0, i).Text = m_COLS_NAME(i)
        Next

        Grid1.FixedRows = 1
        Grid1.FixedCols = 1
    End Sub

    Private Sub Form_QueryStorage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = My.Settings.FORM_QUERYSTORAGE_WIDTH
        Me.Height = My.Settings.FORM_QUERYSTORAGE_HEIGHT
        Me.Left = My.Settings.FORM_QUERYSTORAGE_X
        Me.Top = My.Settings.FORM_QUERYSTORAGE_Y
        Me.WindowState = My.Settings.FORM_QUERYSTORAGE_WINSTATE

        If CBool(Current_Rights(COL_RIGHTS.料号管理) And RIGHTS.READ) = False OrElse
            CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.READ) = False Then
            bt_Query.Enabled = False
        End If

        Set_GridTitles()

        m_InitOver = True
    End Sub

    Private Function QueryStorageInOutRecords(ByVal PartNumber As String, ByRef dataTable As Data.DataTable) As Boolean
        ' Dim selectItem As String = "`日期`,`时间`,`仓库`,`库位`,`入库出库`,`数量`,`用途`,`操作员ID`"

        '=================================================================================================
        '获取需要的数据列
        Dim selectItem As String = "`日期`,`时间`,`仓库`,`库位`,`入库出库`,`数量`,`用途`,`操作员ID`,`库位库存`"
        '=================================================================================================
        Dim strSelect As String = "SELECT " & selectItem
        strSelect &= " FROM `" & CMaterialInOut.cst_TABLE_NAME & "` "
        ' strSelect &= " WHERE `" & CMaterialInOut.m_COLS_NAME(CMaterialInOut.COLS.料号PN) & "` COLLATE gbk_chinese_ci = '" & PartNumber & "' "
        strSelect &= " WHERE `" & CMaterialInOut.m_COLS_NAME(CMaterialInOut.COLS.料号PN) & "`= '" & PartNumber & "' "
        '查询入库数量
        Dim result As Boolean = False
        Dim strSQL As String = strSelect
        dataTable.Rows.Clear()
        dataTable.Columns.Clear()
        If SQL_Query(COL_RIGHTS.原料库存管理, strSQL, dataTable) = True Then
            result = True
        End If

        Return result
    End Function

    Private Sub bt_Query_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Query.Click
        lbl_Result.Text = ""

        If tb_PN.Text.Length = 0 Then
            MsgBox("请指定料号!")
            Return
        End If

        Dim row As Integer
        Dim totalCount As Long = 0
        Dim inOutFlag As Boolean = False

        Set_GridTitles()

        If QueryStorageInOutRecords(tb_PN.Text, m_Table) = True Then
            If m_Table.Rows.Count > 0 Then
                Grid1.Rows = m_Table.Rows.Count + 1
                For row = 0 To m_Table.Rows.Count - 1
                    Dim insertRow As Integer = row + 1

                    Grid1.Cell(insertRow, COLS.序号).Text = row + 1
                    Grid1.Cell(insertRow, COLS.日期).Text = m_Table.Rows.Item(row).Item(0).ToString
                    Grid1.Cell(insertRow, COLS.时间).Text = m_Table.Rows.Item(row).Item(1).ToString
                    Grid1.Cell(insertRow, COLS.仓库).Text = m_Table.Rows.Item(row).Item(2).ToString
                    Grid1.Cell(insertRow, COLS.库位).Text = m_Table.Rows.Item(row).Item(3).ToString

                    inOutFlag = CBool(m_Table.Rows.Item(row).Item(4))
                    If inOutFlag = True Then
                        Grid1.Cell(insertRow, COLS.入库).Text = "0"
                        Grid1.Cell(insertRow, COLS.出库).Text = m_Table.Rows.Item(row).Item(5)
                    Else
                        Grid1.Cell(insertRow, COLS.入库).Text = m_Table.Rows.Item(row).Item(5)
                        Grid1.Cell(insertRow, COLS.出库).Text = "0"
                    End If
                    ' Grid1.Cell(insertRow, COLS.数量).Text = m_Table.Rows.Item(row).Item(5)

                    '=================================================================================================
                    '添加内容至表格
                    Grid1.Cell(insertRow, COLS.库位库存).Text = m_Table.Rows.Item(row).Item(8)

                    '=================================================================================================
                    Dim numCount As Long = 0
                    Try
                        numCount = CLng(m_Table.Rows.Item(row).Item(5))
                        If inOutFlag = True Then
                            totalCount -= numCount
                        Else
                            totalCount += numCount
                        End If
                    Catch ex As Exception
                        MsgBox("库存信息错误!")
                        Return
                    End Try

                    Grid1.Cell(insertRow, COLS.总结余).Text = totalCount
                    Grid1.Cell(insertRow, COLS.用途).Text = m_Table.Rows.Item(row).Item(6)
                    Grid1.Cell(insertRow, COLS.操作员ID).Text = m_Table.Rows.Item(row).Item(7)
                Next
            End If

            lbl_Result.Text = "查询完成: 共" & m_Table.Rows.Count & "条"
        End If
    End Sub

    Private Sub Grid1_ColWidthChange(ByVal Sender As System.Object, ByVal e As FlexCell.Grid.ColWidthChangeEventArgs) Handles Grid1.ColWidthChange
        CGirdInfo.SaveGirdInfo("物料库存查询", Grid1)
    End Sub

    Private Sub Form_QueryStorage_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If m_InitOver = True Then
            My.Settings.FORM_QUERYSTORAGE_WIDTH = Me.Width
            My.Settings.FORM_QUERYSTORAGE_HEIGHT = Me.Height
            My.Settings.FORM_QUERYSTORAGE_WINSTATE = Me.WindowState
            My.Settings.Save()
        End If
    End Sub

    Private Sub Form_QueryStorage_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        If m_InitOver = True Then
            My.Settings.FORM_QUERYSTORAGE_X = Me.Left
            My.Settings.FORM_QUERYSTORAGE_Y = Me.Top
            My.Settings.Save()
        End If
    End Sub
    ' V1.6.0.0 Momo 库存页面增加模糊查询
    Private Sub QueryData_Quick()
        Dim strSQL As String = "SELECT `" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "`"
        strSQL &= " FROM `" & CPartNumber.cst_TABLE_NAME & "`"

        If tb_PN.Text.Length > 0 Then
            strSQL &= " WHERE "

            Dim strSplit() As String
            Dim firstLike As Boolean = True
            If tb_PN.Text.Length > 0 Then
                strSplit = Split(tb_PN.Text, ",")
                For i As Integer = 0 To strSplit.Length - 1
                    If firstLike = True Then
                        firstLike = False
                    Else
                        strSQL &= " AND "
                    End If
                    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "`"
                    strSQL &= " COLLATE gbk_chinese_ci "
                    strSQL &= " LIKE '%"
                    strSQL &= form_QuerySim.GetLikeString(strSplit(i)) & "%'"
                    strSQL &= " escape '/'"
                Next
            End If
        End If
        SQL_Query(COL_RIGHTS.料号管理, strSQL, m_Table)
    End Sub

    Private Sub btn_SaveAs_Click(sender As Object, e As EventArgs) Handles btn_SaveAs.Click
        Dim dlg As New SaveFileDialog()
        dlg.DefaultExt = "xls"
        dlg.Filter = "Excel File|*.xls"
        If dlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return

        If Grid1.ExportToExcel(dlg.FileName, True, True) Then
            MsgBox("保存为Excel成功：" & dlg.FileName)
        Else
            MsgBox("保存失败!")
        End If
    End Sub

End Class