Imports OrderPackingProcessor.mySQLAddress

Module mySQLAddress
    Private Const mainOrderDB As String = "jomtOrderDB.jomtOrderTbl"
    Private Const testOrderDB As String = "jomtOrderTestDB.jomtOrderTbl"

    Private Const mainSalesDB As String = "jomtTesterDB.jomtSalesPackingTbl"
    Private Const testSalesDB As String = "jomtTesterServerDB.jomtSalesPackingTbl"

    Private Const mainLdmDB As String = "jomtTesterDB.jomtLDMTbl"

    Private Const mainCountDB As String = "jomtTesterDB.jomtPackingCount"
    Private Const testCountDB As String = "jomtTesterServerDB.jomtPackingCount"

    Private Const mainCartDB As String = "jomtTesterDB.jomtCartridgeTbl"
    Private Const testCartDB As String = "jomtTesterServerDB.jomtCartridgeTbl"

    Private Const mainProdDB As String = "jomtTesterDB.jomtProductTbl"
    Private Const testProdDB As String = "jomtTesterServerDB.jomtProductTbl"

    Private Const mainDivDB As String = "jomtTesterDB.jomtDeviceTbl"
    Private Const testDivDB As String = "jomtTesterServerDB.jomtDeviceTbl"

    Private Const mainPlistDB As String = "jomtTesterDB.jomtProdList"
    Private Const testPlistDB As String = "jomtTesterServerDB.jomtProdList"

    Private Const mainClistDB As String = "jomtTesterDB.jomtComponentTbl"
    Private Const testClistDB As String = "jomtTesterServerDB.jomtComponentTbl"

    Private Const mainPackEvent As String = "jomtTesterDB.jomtPackEvent"
    Private Const testPackEvent As String = "jomtTesterServerDB.jomtPackEvent"

    Private Const mainPKCodeDB As String = "jomtTesterDB.jomtPKCodeTbl"

    Private Const testHistoryDB As String = "jomtTesterServerDB.jomtPackHistoryTbl"
    Private Const testPackDB As String = "jomtTesterServerDB.jomtSalesPackingTbl_New"

    Private Const MainNonTDDB As String = "jomtTesterDB.jomtNonTDTbl"
    Private Const TestNonTDDB As String = "jomtTesterServerDB.jomtNonTDTbl"

    Private Const mainModelInfoDB As String = "jomtTesterDB.jomtModelInfoTbl"
    Private Const testModelInfoDB As String = "jomtTesterServerDB.jomtModelInfoTbl"

    Public applyOrderDB As String = mainOrderDB
    Public applySalesDB As String = mainSalesDB
    Public applyCountDB As String = mainCountDB
    Public applyCartDB As String = mainCartDB
    Public applyProdDB As String = mainProdDB
    Public applyDivDB As String = mainDivDB
    Public applyPlistDB As String = mainPlistDB
    Public applyClistDB As String = mainClistDB
    Public applyPackEvent As String = mainPackEvent
    Public applyPKCodeDB As String = mainPKCodeDB
    Public applyNonTDDB As String = MainNonTDDB
    Public ApplyModelInfoDB As String = mainModelInfoDB
    Public ApplyLdmDB As String = mainLdmDB

    Public applyPackDB As String = testPackDB
    Public applyHistoryDB As String = testHistoryDB

    Public Enum countSaveList
        normal
        autopack
        autoWeight
    End Enum

    Public Enum dbCode
        prodDBcode
        comDBcode
    End Enum

    Public Enum prodDB
        packingCount
        proset
        luxset
        proface
        luxface
        proeye
        luxeye
        probody
        luxbody
        maximum
        alphaSet
        alphaFace
        alphaEye
        alphaBody
        alto
        effect
        effectV3
        maximumAlpha
        optimum
        '신규제품추가 : 추가하는 품목의 변수 추가
    End Enum

    Public Enum comDB
        mask5
        mask10
        hairband
        gel
        minigel
        ample
        cream
        umb
        fen
        charger
        cable
        toner
        problack
        probeige
        prored
        propink
        maxblack
        maxapr
        maxmint
        maxbeige
        soothing
        car0
        car1
        car2
        car3
        car4
        car5
        car6
        bag
        cosbag
        dustbag
        maxPurple
        proSnakeBlack
        kollagen
        kollagenset
        kollagen_P
        kollagenset_P
        cleansingWater
        tonerPad
        maxSaffianoBK
        hologram1
        hologram3
        hologram5
        caseSaffianoPp
        maxYellow
        Kollagen_Pe1
        Kollagen_Pe3
        Kollagen_Pe5
        ATCircleCase
        APSquareCase
        maxPeach
        massageSoothing
        maxSMint
        meshpouch
        firmingAmp
        hyaluronicAmp
        nightshot
        morningshot
        casemaxwoodi
        Elicolltoner
        Elicollamp
        Elicollcream
        Elihyaltoner
        Elihyalamp
        Elihyalcream
        Dayshot
        Beautyshopbag
        Gummy
        Stickjelly
        Newyearbag
        Emsbelt
        Beltbag
        Dayshot2
        Elixir
        Elixirpremium
        Emsbeltcontrollset
        FabricBag
        Goldshopbag
        MorNigshotset
        CTBox
        ElesqLiftingToner
        ElesqLiftingCeam
        ElesqLiftingECream
        ElesqLiftingAmp
        ElesqAquaToner
        ElesqAquaCeam
        ElesqAquaECream
        ElesqAquaAmp
        car8
        keyring
        DoenaLiftingToner
        DoenaLiftingAmp
        DoenaLiftingCream
        FirmingCollagenGel
        keyringSally
        beautykitHairband
        beautykitMirrorS
        beautykitMirrorL
        bloomingMoistureGel150
        firmingCollagenGel150
        tweedCase
        bloomingMoistureGel250
        '신규구성품추가 : 추가하는 품목의 변수 추가
    End Enum

    Structure item_info
        Dim item_type As String 'P:Product / C:Component
        Dim prod_code As String
        Dim sales_packing_cnt_col_nm As String
        Dim sales_packing_serial_col_nm As String
        Dim cnt As Integer
        Dim serial As String
    End Structure
    Public item_product_list() As item_info
    Public item_component_list() As item_info
    Public item_product_max_index As Integer
    Public item_component_max_index As Integer

    Structure packing_count_info
        Dim item_type As String 'P:Product / C:Component
        Dim prod_code As String
        Dim packing_cnt_col_nm As String
        Dim ap_packing_cnt_col_nm As String
        Dim aw_packing_cnt_col_nm As String
        Dim db_count As Integer
        Dim com_count As Integer
    End Structure
    Public product_packing_cnt_list() As packing_count_info
    Public component_packing_cnt_list() As packing_count_info
    Public countCommand() As String

    ''데이터베이스 관련
    Private DBCmd As String

    Public Function packingCountset(dbType As Int16, prodCode As Int16, ckCount As Int16, serial As String)
        If dbType = dbCode.prodDBcode Then
            If prodCode >= 1 And prodCode <= item_product_max_index Then
                item_product_list(prodCode).cnt += ckCount
                If item_product_list(prodCode).serial = Nothing Then
                    item_product_list(prodCode).serial = serial
                Else
                    item_product_list(prodCode).serial = item_product_list(prodCode).serial & "/" & serial
                End If
            Else
                MsgBox("잘못된 데이터가 개입되었습니다", vbCritical)
                Return True
            End If
        Else
            If prodCode >= 0 And prodCode <= item_component_max_index Then
                item_component_list(prodCode).cnt += ckCount
            Else
                MsgBox("잘못된 데이터가 개입되었습니다", vbCritical)
                Return True
            End If
        End If

        Return False
    End Function

    Public Sub countCommandFunc(saveType As Int16)
        ReDim countCommand(item_product_max_index + item_component_max_index + 1)

        Select Case saveType
            Case countSaveList.normal

                countCommand(prodDB.packingCount) = "PackingCount"

                For i = 1 To item_product_max_index
                    countCommand(i) = product_packing_cnt_list(i).packing_cnt_col_nm
                Next

                For i = 0 To item_component_max_index
                    If component_packing_cnt_list(i).packing_cnt_col_nm <> Nothing Then
                        countCommand(product_packing_cnt_list.Length + i) = component_packing_cnt_list(i).packing_cnt_col_nm
                    End If
                Next

            Case countSaveList.autopack

                countCommand(prodDB.packingCount) = "AutoPackCnt"

                For i = 1 To item_product_max_index
                    countCommand(i) = product_packing_cnt_list(i).ap_packing_cnt_col_nm
                Next

                For i = 0 To item_component_max_index
                    If component_packing_cnt_list(i).ap_packing_cnt_col_nm <> Nothing Then
                        countCommand(product_packing_cnt_list.Length + i) = component_packing_cnt_list(i).ap_packing_cnt_col_nm
                    End If
                Next

            Case countSaveList.autoWeight

                countCommand(prodDB.packingCount) = "AutoWeightCnt"

                For i = 1 To item_product_max_index
                    countCommand(i) = product_packing_cnt_list(i).aw_packing_cnt_col_nm
                Next

                For i = 0 To item_component_max_index
                    If component_packing_cnt_list(i).aw_packing_cnt_col_nm <> Nothing Then
                        countCommand(product_packing_cnt_list.Length + i) = component_packing_cnt_list(i).aw_packing_cnt_col_nm
                    End If
                Next

        End Select
    End Sub

    Public Sub countDBCommand()

        DBCmd = "SELECT MAX(ProdCode) FROM " & applyPlistDB & ";"
        If DBQueryReader(DBCmd) Then
            item_product_max_index = sqlDataQuery(0)
            ReDim item_product_list(item_product_max_index)
            ReDim product_packing_cnt_list(item_product_max_index)
        End If

        DBCmd = "SELECT CAST(ProdCode AS CHAR)
                      , SalesPackingCntColNm
                      , SalesPackingSerialColNm
                      , PackingCntColNm
                      , APPackingCntColNm
                      , AWPackingCntColNm 
                   FROM " & applyPlistDB & " GROUP BY ProdCode ORDER BY ProdCode asc;"
        If DBQueryReader(DBCmd) Then
            Dim RowCount As Int16 = (sqlDataQuery.Length / 6) - 1
            Dim dataSetCount As Int32 = 0
            Dim item_data As item_info
            Dim packing_count_data As packing_count_info

            For row = 0 To RowCount
                item_data.item_type = "P"
                packing_count_data.item_type = "P"
                For col = 0 To 5
                    If col = 0 Then
                        item_data.prod_code = sqlDataQuery(dataSetCount)
                        packing_count_data.prod_code = sqlDataQuery(dataSetCount)
                    ElseIf col = 1 Then
                        item_data.sales_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 2 Then
                        item_data.sales_packing_serial_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 3 Then
                        packing_count_data.packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 4 Then
                        packing_count_data.ap_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 5 Then
                        packing_count_data.aw_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    End If
                    dataSetCount += 1
                Next
                item_product_list(item_data.prod_code) = item_data
                product_packing_cnt_list(packing_count_data.prod_code) = packing_count_data
            Next
        End If

        DBCmd = "SELECT MAX(ProdCode) FROM " & applyClistDB & ";"
        If DBQueryReader(DBCmd) Then
            item_component_max_index = sqlDataQuery(0)
            ReDim item_component_list(item_component_max_index)
            ReDim component_packing_cnt_list(item_component_max_index)
        End If

        DBCmd = "SELECT CAST(ProdCode AS CHAR)
                      , SalesPackingCntColNm
                      , PackingCntColNm
                      , APPackingCntColNm
                      , AWPackingCntColNm 
                   FROM " & applyClistDB & " GROUP BY ProdCode ORDER BY ProdCode asc;"
        If DBQueryReader(DBCmd) Then
            Dim RowCount As Int16 = (sqlDataQuery.Length / 5) - 1
            Dim dataSetCount As Int32 = 0
            Dim item_data As item_info
            Dim packing_count_data As packing_count_info

            For row = 0 To RowCount
                item_data.item_type = "C"
                packing_count_data.item_type = "C"
                For col = 0 To 4
                    If col = 0 Then
                        item_data.prod_code = sqlDataQuery(dataSetCount)
                        packing_count_data.prod_code = sqlDataQuery(dataSetCount)
                    ElseIf col = 1 Then
                        item_data.sales_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 2 Then
                        packing_count_data.packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 3 Then
                        packing_count_data.ap_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    ElseIf col = 4 Then
                        packing_count_data.aw_packing_cnt_col_nm = sqlDataQuery(dataSetCount)
                    End If
                    dataSetCount += 1
                Next
                item_component_list(item_data.prod_code) = item_data
                component_packing_cnt_list(packing_count_data.prod_code) = packing_count_data
            Next
        End If

    End Sub
End Module
