nancy CThread.Acess

Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Threading


Public Class ThreadFileAcess

    Public Const SERVER_FILE_NAME As String = _
              "\\pe2950-1\製造部 - 公開\⑪レジスト\A棟1Ｆ管理\DFSR材管理\ドライフィルム在庫管理表.xls"
    Private Const OUT_TEMP_FILE_NAME As String = "\DFSR在庫チェック.xlt"
    Private Const DATAFILE As String = "DATA.BIN"
    Private Const SHT_ZAIKO_CHART As String = "DF在庫表"

    Private Const LOCAL_FILE_NAME_HALF As String = "\RAWDATA.X"

    'エクセルのスペック上のマックス行数
    Private Const MAXROW_OF_SPEC As Integer = 65536
    Private Const DELIMI_SLASH As String = "/"
    Private Const KITAKYUU_PTN As String = "[7]$"
    Private Const KITAKYUUSYUU_HIN As String = "北九州品"
    Private Const JYUURAI_HIN As String = "従来品"
    Private Const RESULT_EXCEL_START_ROW As Integer = 5
    Private Enum E_STATE As Integer

        DONOTEXIST = 0     '処理がなされる前
        NEWONE = 1         '新品　
        FIRST_KAITO = 2    '1回目解凍　
        RE_KAITO = 3        '再冷凍　
        _2ND_KAITO = 4    '二回目解凍　
        USED = 5            '使用完了

    End Enum

    Private Enum E_BAR_DETAIL As Integer

        BARCODE_FULL = 0      '0 フルバーコード
        '''ZWZB071360743        '1ZWZB071360743
        SERIAL_CD = 2           '2A9212-07-A17
        '''/495E20              '3495E20
        LOTCD = 4               '49021212
        LENGTH = 5              '5 50 
        ATSUSA = 6              '6 20  
        '''/495                 '7 495
        FILM_TYPE = 8           '8 SR1 
        EXTRA_KITAORJYURAI = 9  '9
        EXTRA_NAMEFORDISP = 10  '10

    End Enum
    Private Enum E_EXL_COL As Integer

        ZERO_IS_NO_USED = 0     'エクセルを基準にするINDEXなので、使わない
        PRODUCT_TYPE = 1        '種類
        SERIAL_NO = 2           'ｼﾘｱﾙNo	
        LOTNO = 3               'LotNo	
        LENGTH = 4              '長さ	
        ASTUMI = 5              '厚み	
        SEIZO_DATE = 6          '製造年月日	
        YUUKOUKIGEN = 7         '有効期限	
        BARCODE = 8             'バーコード	
        UKEIRE_DATE = 9         '受入日	
        UKEIRE_UID = 10         '受入作業者ＩＤ
        HIDDEN = 11             'ソースシート計算用
        FIRST_KAITOU_DATE = 12  '１回目解凍日時	
        FIRST_KAITOU_UID = 13   '１回目解凍作業者ＩＤ
        RE_REITOU_DATE = 14     '再冷凍日時
        RE_REITOU_UID = 15      '再冷凍作業者ＩＤ
        _2ND_KAITOU_DATE = 16   '２回目解凍日時
        _2ND_KAITOU_UID = 17    '２回目解凍作業者ＩＤ
        USED_KAN_DATE = 18      '使用完了日時
        USED_KAN_UID = 19       '使用完了入力者ＩＤ
        USED_KIGEN_CHK = 20     '使用期限チェック	
        USED_KIGEN_COMFIRM = 21 '使用期限確認

        NAME_FOR_DISP = 22      '22 集計時に使う名前
        FILM_STATE = 23         '23 フィルムの
        KITAKYUU_OR_JYUURAI = 24 '24 北九州か従来か

        SRC_EXCEL_END_COL = 21  '元シート最終カラム
        WORK_EXCEL_END_COL = 24  '元シート最終カラム

    End Enum

    Private Enum E_EXCEL_ROW As Integer

        _0ISNOT_EXIST = 0
        SRC_START_ROW = 12

    End Enum

    Private _mtx As Mutex
    Private mtx As Mutex

   
    Public Function FIlECOPY()

        Try
            '有ったら上書きする(第3引数 TRUE：上書き、省略またはFalseは上書き不可)
            System.IO.File.Copy(SERVER_FILE_NAME, Application.StartupPath & LOCAL_FILE_NAME_HALF, True)

        Catch

            ''ここは例外が出る MessageBox.Show("copy")

        Finally

           
        End Try

    End Function

    Private Function Serialize(ByVal obj As Object, ByVal strfilename As String)

        ' オブジェクトをファイルalice.binとしてシリアライズ
        Dim stream As Stream = File.OpenWrite(strfilename)
        Dim formatter As New BinaryFormatter
        Try
            formatter.Serialize(stream, obj)

        Catch Exp As Exception

            '問題にはならない
            'Throw Exp

        Catch

            '問題にはならない
            'Throw New System.Exception("ERROR_SR")

        Finally

            stream.Close()
            GC.Collect()

        End Try

    End Function

    '**************************************************
    ' 処理名：　MRComObject
    ' 引数  ：　エクセル生成に使ったOBJ
    ' 戻り値：　なし
    ' 説明  ：　Excelの解放
    '**************************************************
    Private Sub MRComObject(ByRef objCom As Object)
        'COM オブジェクトの使用後、明示的にCOM オブジェクトへの参照を解放する
        Dim k As Integer
        Try
            '提供されたランタイム呼び出し可能ラッパーの参照カウントをデクリメントします
            If Not objCom Is Nothing AndAlso System.Runtime.InteropServices. _
                                                      Marshal.IsComObject(objCom) Then
                Do
                    k = System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom)
                Loop Until k <= 0
            End If
        Catch
            ' なにもしない
            'がcatchはあるべき
        Finally
            '参照を解除する
            objCom = Nothing
        End Try

    End Sub

    '**************************************************************
    ' 処理名：　
    ' 引数  ：　
    ' 戻り値：　
    ' 説明  ：　
    '**************************************************************
    Private Function fileExistsChek(ByVal strFullFileName) As Boolean

        Return System.IO.File.Exists(strFullFileName)

    End Function
    '**************************************************
    ' 処理名：　エクセルからオブジェクトへ読み込み
    ' 引数  ：　なし
    ' 戻り値：　object または　NOTHING
    ' 説明  ：　元のファイルの内容をオブジェクトにコピー
    '**************************************************
    Private Function GetObjFromFile(ByVal current_filename As String) As Object(,)

        Dim xlsApp As Object         ' Excel App
        Dim xlsBook As Object        ' Excel Book
        Dim xlsSheet As Object       ' Excel Sheet
        Dim iCnt As Integer = E_EXCEL_ROW._0ISNOT_EXIST

        Const KEY_COLUMN_BARCODE As Integer = E_EXL_COL.BARCODE '8

        Try

            xlsApp = CreateObject("Excel.Application")
            xlsBook = xlsApp.Workbooks.open(current_filename)
            xlsSheet = xlsBook.Worksheets(SHT_ZAIKO_CHART)
            Dim dataCnt As Integer = 0

            For iCnt = E_EXCEL_ROW.SRC_START_ROW To MAXROW_OF_SPEC

                If (xlsSheet.Cells(iCnt, KEY_COLUMN_BARCODE).Value = Nothing) Then
                    Exit For
                End If

            Next

            Dim obj(,) As Object = xlsSheet.Range _
                                     (xlsSheet.Cells(E_EXCEL_ROW.SRC_START_ROW, 1), _
                                     xlsSheet.Cells(iCnt - 1, E_EXL_COL.WORK_EXCEL_END_COL)).Value

            Dim cnt As Integer = UBound(obj)

            For iRow As Integer = 1 To cnt

                Dim objTemp As Object = Nothing

                objTemp = Split("HEAD_POSITION" & DELIMI_SLASH & _
                            obj(iRow, E_EXL_COL.BARCODE), DELIMI_SLASH)

                Dim arr As ArrayList = New ArrayList
                Dim LOTCD As String = objTemp(E_BAR_DETAIL.LOTCD)
                Dim ATSUSA As String = objTemp(E_BAR_DETAIL.ATSUSA)
                Dim FILM_TYPE As String = objTemp(E_BAR_DETAIL.FILM_TYPE)

                Dim KITAorANOTHER As String

                Dim KITAKYUUSYUU_LOT_PTN As Regex = New Regex(KITAKYUU_PTN)

                If KITAKYUUSYUU_LOT_PTN.IsMatch(Trim(LOTCD)) = True Then
                    KITAorANOTHER = KITAKYUUSYUU_HIN
                Else
                    KITAorANOTHER = JYUURAI_HIN
                End If

                Dim nState As E_STATE = E_STATE.DONOTEXIST
                If CType(obj(iRow, E_EXL_COL.UKEIRE_UID), String) <> "" Then
                    nState = E_STATE.NEWONE
                End If
                If CType(obj(iRow, E_EXL_COL.FIRST_KAITOU_UID), String) <> "" Then
                    nState = E_STATE.FIRST_KAITO
                End If
                If CType(obj(iRow, E_EXL_COL.RE_REITOU_UID), String) <> "" Then
                    nState = E_STATE.RE_KAITO
                End If
                If CType(obj(iRow, E_EXL_COL._2ND_KAITOU_UID), String) <> "" Then
                    nState = E_STATE._2ND_KAITO
                End If
                If CType(obj(iRow, E_EXL_COL.USED_KAN_UID), String) <> "" Then
                    nState = E_STATE.USED
                End If
                If CType(obj(iRow, E_EXL_COL.USED_KIGEN_COMFIRM), String) = "使用完了" Then
                    nState = E_STATE.USED
                End If

                obj(iRow, E_EXL_COL.NAME_FOR_DISP) = Trim(FILM_TYPE) & " " & _
                                                 Trim(ATSUSA) & "μｍ " & KITAorANOTHER
                obj(iRow, E_EXL_COL.KITAKYUU_OR_JYUURAI) = KITAorANOTHER
                obj(iRow, E_EXL_COL.FILM_STATE) = nState

            Next

            Return obj

        Catch Exp As Exception

            Return Nothing

        Catch

            Return Nothing

        Finally

            'xlsBook.Close(False)　'念のため
            xlsApp.DisplayAlerts = False
            xlsApp.Quit()
            xlsApp.DisplayAlerts = True

            MRComObject(xlsSheet)
            MRComObject(xlsBook)
            MRComObject(xlsApp)

            GC.Collect()

        End Try

    End Function

    '**************************************************************
    ' 処理名：　
    ' 引数  ：　
    ' 戻り値：　
    ' 説明  ：　
    '**************************************************************
    Public Sub DoTransactions()

        Try

            mtx = New Mutex(False, "MUTEX")
            mtx.WaitOne()

            FIlECOPY()

        Catch
        Finally
            mtx.ReleaseMutex()
        End Try

    End Sub

    Public Sub DoTranGetRawData()

        Try
            _mtx = New Mutex(False, "MUTEX")
            _mtx.WaitOne()

            getdata()

        Finally
            _mtx.ReleaseMutex()
        End Try

    End Sub

    Private Sub getdata()

        '集計はシリアライズ対象外なので、注意
        Serialize(GetObjFromFile(Application.StartupPath & LOCAL_FILE_NAME_HALF), "DATATH.BIN")
     
    End Sub

End Class