Imports System.Web
Imports System.Web.Script.Serialization
Public Class clsTable
    Public Sub EditField()
        Dim mFieldNote As String = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request("FieldNote"))
        Dim mFieldID As Integer = 0
        If mFieldNote IsNot Nothing AndAlso mFieldNote.Length > 0 AndAlso Integer.TryParse(HttpContext.Current.Request("FieldID"), mFieldID) Then
            Dim clsSQL As New DB_SQL.clsTableFields
            If clsSQL.Save(mFieldID, mFieldNote) > 0 Then
                HttpContext.Current.Response.Write("1")
            Else
                HttpContext.Current.Response.Write("0")
            End If
        Else
            HttpContext.Current.Response.Write("0")
        End If
    End Sub
    Public Sub DelField()
        Dim mFieldID As Integer = 0
        If Integer.TryParse(HttpContext.Current.Request("FieldID"), mFieldID) Then
            Dim clsSQL As New DB_SQL.clsTableFields
            If clsSQL.Delete(mFieldID) Then
                HttpContext.Current.Response.Write("1")
            Else
                HttpContext.Current.Response.Write("0")
            End If
        Else
            HttpContext.Current.Response.Write("0")
        End If
    End Sub
    Public Sub GetFieldList()
        Dim mTableID As Integer = 0
        If Integer.TryParse(HttpContext.Current.Request("TableID"), mTableID) Then
            Dim clsSQL As New DB_SQL.clsTableData
            Dim mTable As DB_SQL.TableData = clsSQL.GetSingle(mTableID)
            If mTable IsNot Nothing Then
                Dim mFieldsList = XrenCache.XrCache.GetList(New DB_SQL.DB_SQLDataContext, "TableFields", DB_SQL.clsTableFields.GetLinq(), XrenCache.XrCache.CacheLifeType.File, HttpContext.Current.Request.PhysicalApplicationPath & "CacheFile\TableFields.txt", , , , CType(BaseFunction.PublicMethod.GetApplication("SecCache"), Boolean))
                Dim mList = (From f In mFieldsList _
                             Where f.TableName = mTable.TableName _
                             Order By f.ID _
                             Select f).ToList
                Dim mJson As New JavaScriptSerializer
                HttpContext.Current.Response.Write(mJson.Serialize(mList))
            End If
        End If
    End Sub
    Public Sub EditTable()
        Dim mTableNote As String = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request("TableNote"))
        Dim mTableID As Integer = 0
        If mTableNote IsNot Nothing AndAlso mTableNote.Length > 0 AndAlso Integer.TryParse(HttpContext.Current.Request("TableID"), mTableID) Then
            Dim clsSQL As New DB_SQL.clsTableData
            If clsSQL.Save(mTableID, mTableNote) > 0 Then
                HttpContext.Current.Response.Write("1")
            Else
                HttpContext.Current.Response.Write("0")
            End If
        Else
            HttpContext.Current.Response.Write("0")
        End If
    End Sub
    Public Sub DelTable()
        Dim mTableID As Integer = 0
        If Integer.TryParse(HttpContext.Current.Request("TableID"), mTableID) Then
            Dim clsSQL As New DB_SQL.clsTableData
            If clsSQL.Delete(mTableID) Then
                HttpContext.Current.Response.Write("1")
            Else
                HttpContext.Current.Response.Write("0")
            End If
        Else
            HttpContext.Current.Response.Write("0")
        End If
    End Sub
    Public Sub GetTableList()
        Dim mTableList = XrenCache.XrCache.GetList(New DB_SQL.DB_SQLDataContext, "TableData", DB_SQL.clsTableData.GetLinq.OrderBy(Function(c) c.TableName), XrenCache.XrCache.CacheLifeType.File, HttpContext.Current.Request.PhysicalApplicationPath & "CacheFile\TableData.txt", , , , CType(BaseFunction.PublicMethod.GetApplication("SecCache"), Boolean))
        Dim mJson As New JavaScriptSerializer
        HttpContext.Current.Response.Write(mJson.Serialize(mTableList))
    End Sub
    Public Sub GetTablsFields()
        Dim strJosn = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request("JsonTables"))
        If strJosn Is Nothing Then Exit Sub
        Dim mJson As New JavaScriptSerializer
        Dim SelectTables As List(Of clsTableInfo) = mJson.Deserialize(Of List(Of clsTableInfo))(strJosn)
        Dim mTableList = XrenCache.XrCache.GetList(New DB_SQL.DB_SQLDataContext, "TableData", DB_SQL.clsTableData.GetLinq.OrderBy(Function(c) c.TableName), XrenCache.XrCache.CacheLifeType.File, HttpContext.Current.Request.PhysicalApplicationPath & "CacheFile\TableData.txt", , , , CType(BaseFunction.PublicMethod.GetApplication("SecCache"), Boolean))
        Dim mFieldsList = XrenCache.XrCache.GetList(New DB_SQL.DB_SQLDataContext, "TableFields", DB_SQL.clsTableFields.GetLinq(), XrenCache.XrCache.CacheLifeType.File, HttpContext.Current.Request.PhysicalApplicationPath & "CacheFile\TableFields.txt", , , , CType(BaseFunction.PublicMethod.GetApplication("SecCache"), Boolean))
        Dim mList = (From f In mFieldsList _
                    Join t In mTableList On f.TableName Equals t.TableName _
                    Join s In SelectTables On s.Name Equals f.TableName _
                    Select New With {.ID = f.ID, .TableName = f.TableName, .TableNote = t.TableNote, .FieldNote = f.FieldNote, .FieldName = f.FieldName, .TableID = t.ID}).ToList
        HttpContext.Current.Response.Write(mJson.Serialize(mList))
    End Sub
End Class
Public Class clsTableInfo
    Dim _ID As Integer
    Dim _Name As String
    Dim _Note As String
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Public Property Note() As String
        Get
            Return _Note
        End Get
        Set(ByVal value As String)
            _Note = value
        End Set
    End Property
End Class
