<% Class cls_ishion_showPage
    Private ishion_PageSize, ishion_currentPage
    Private ishion_DbType, ishion_RecType, ishion_RecSql, ishion_RecTerm, ishion_CookieName
    Private S_Order, ishion_JsUrl
    Private ishion_Sql, ishion_Field, ishion_Table, ishion_Where, ishion_OrderBy, ishion_Id
    Private ishion_RecCount, ishion_PageCount, ResultSet_Sql
    Private ishion_Rs, ishion_Cm, ishion_WhereOther, ishion_Order, ishion_Size, ishion_Mm 'MSSQL用

    '================================================================
    ' Class_Initialize 类的初始化
    '================================================================
    Private Sub Class_Initialize()
        ishion_PageSize = 10 '设定每页记录条数的默认值为10
        'ishion_CurrentPage=1 '获取当前面的值
        ishion_Order = ">" '默认排序
        ishion_Size = "MAX" '默认排序
        ishion_WhereOther = "" '默认条件
        ishion_CookieName = "ishion"
    End Sub
    '================================================================
    ' Class_Terminate 类注销
    '================================================================
    Private Sub Class_Terminate()

    End Sub

    '================================================================
    ' DbType 得到数据库类型
    '================================================================
    Public Property Let DbType(ByVal strDbType)
        ishion_DbType = strDbType
    End Property

    '================================================================
    ' RecType 取记录总数方法(0执行count,1自写sql语句取,2固定值)
    '================================================================
    Public Property Let RecType(ByVal intRecType)
        ishion_RecType = intRecType
    End Property
    
    '================================================================
    ' RecSql '如果RecType＝1则=取记录sql语句，如果是2=数值，等于0=""
    '================================================================
    Public Property Let RecSql(ByVal strRecSql)
        ishion_RecSql = strRecSql
    End Property

    '================================================================
    ' RecTerm 搜索条件是否变化(0无变化,1有变化)
    '================================================================
    Public Property Let RecTerm(ByVal intRecTerm)
        ishion_RecTerm = intRecTerm
    End Property

    '================================================================
    ' CookieName 取得cookiename
    '================================================================
    Public Property Let CookieName(ByVal strCookieName)
        ishion_CookieName = strCookieName
    End Property

    '================================================================
    ' Order 排序(0顺序,1降序)
    '================================================================
    Public Property Let Order(ByVal intOrder)
        S_Order = intOrder
        If S_Order = 1 Then
            ishion_Order = "<"
            ishion_Size = "MIN"
        End If
    End Property

    '================================================================
    ' PageSize 设置每一页记录条数,默认10记录
    '================================================================
    Public Property Let PageSize(ByVal intPageSize)
        ishion_PageSize = CheckNum(intPageSize, ishion_PageSize, -1)
    End Property
    
    '================================================================
    ' CurrentPage 设置当前页，默认为第一页
    '================================================================
    Public Property Let currentPage(ByVal intCurrentPage)
        ishion_currentPage = CheckNum(intCurrentPage, 1, -1)
    End Property
    '================================================================
    ' Sql 取得sql所需表字段条件排序,输入:字段,表,条件,排序,主ID
    '================================================================
    Public Property Let sql(ByVal str_sql)
        ishion_Sql = Split(str_sql, "$")
        ishion_Field = ishion_Sql(0)
        ishion_Table = ishion_Sql(1)
        ishion_Where = ishion_Sql(2)
        ishion_OrderBy = ishion_Sql(3)
        ishion_Id = ishion_Sql(4)
        If Len(ishion_Where) >= 3 Then
            ishion_WhereOther = " And " & ishion_Where
            ishion_Where = " Where " & ishion_Where
        End If
        If Len(ishion_OrderBy) > 3 Then ishion_OrderBy = " ORDER BY " & ishion_OrderBy
    End Property

    '================================================================
    ' GetRecCount 取得记录总数
    '================================================================
    Private Function GetRecCount()
	Dim tRs
    If Not IsObject(Conn) Then ConnectionDatabase
        Select Case ishion_RecType
            Case 1
                GetRecCount = Conn.Execute(ishion_RecSql, 0, 1)(0)
            Case 2
                GetRecCount = ishion_RecSql
            Case 3				
				Set tRs = Conn.Execute("SELECT top 1 " & ishion_Field & "  FROM " & ishion_Table & " " & ishion_Where)
				If tRs.Eof and tRs.Bof then
					GetRecCount = 0
				Else
					GetRecCount = Conn.Execute("SELECT Count(*) FROM " & ishion_Table & " " & ishion_Where)(0)
				End If
				Set tRs = Nothing
				'Response.Write GetRecCount
				If Int(GetRecCount) > Int(ishion_RecSql) then GetRecCount = ishion_RecSql
            Case Else
                GetRecCount = Conn.Execute("SELECT Count(*) FROM " & ishion_Table & " " & ishion_Where)(0)
        End Select
    End Function
    
    '================================================================
    ' RecCount 修正记录总数
    '================================================================
    Public Property Get RecCount()
		On Error Resume Next
        RecCount = Request.Cookies("ishion_showPage")(ishion_CookieName)
        RecCount = CheckNum(RecCount, 0, -1)
        Select Case ishion_RecTerm
            Case 1
                RecCount = GetRecCount()
                Response.Cookies("ishion_showPage")(ishion_CookieName) = RecCount
            Case 2
                RecCount = GetRecCount()
            Case Else
                If RecCount = 0 Then
                    RecCount = GetRecCount()
                    Response.Cookies("ishion_showPage")(ishion_CookieName) = RecCount
                End If
        End Select
    End Property
    '================================================================
    ' ResultSet 返回分页后的记录集
    '================================================================
    Public Property Get ResultSet()
        If Not IsObject(Conn) Then ConnectionDatabase
        ResultSet = Null
        '记录总数
        ishion_RecCount = RecCount()
        '当前页
        If ishion_RecCount > 0 Then
        '页数
            If (ishion_RecCount Mod ishion_PageSize) = 0 Then
                ishion_PageCount = ishion_RecCount \ ishion_PageSize
            Else
                ishion_PageCount = ishion_RecCount \ ishion_PageSize + 1
            End If
            '当前页
            ishion_currentPage = CheckNum(ishion_currentPage, 1, ishion_PageCount)
            Select Case ishion_DbType
                Case "AC" 'ac数据库
                    Set ResultSet = Server.CreateObject("adodb.RecordSet")
                    ResultSet_Sql = "SELECT " & ishion_Field & " FROM " & ishion_Table & " " & ishion_Where & " " & ishion_OrderBy
                    ResultSet.open ResultSet_Sql, Conn, 1, 1, &H1
                    ResultSet.AbsolutePosition = (ishion_currentPage - 1) * ishion_PageSize + 1
                    
                Case "MSSQL" 'sqlserver2000数据库
                    If ishion_currentPage = 1 Then
                        ResultSet_Sql = "SELECT TOP " & ishion_PageSize & " " & ishion_Field & " FROM " & ishion_Table & ishion_Where & " " & ishion_OrderBy
                         
                    Else
                        ResultSet_Sql = "SELECT " & ishion_Size & "(" & ishion_Id & ") FROM (SELECT TOP " & (ishion_currentPage - 1) * ishion_PageSize & " " & ishion_Id & " FROM " & ishion_Table & ishion_Where & " " & ishion_OrderBy & ") AS tmpTable"
                        ishion_Mm = Conn.Execute(ResultSet_Sql, 0, 1)(0)
                        ResultSet_Sql = "SELECT TOP " & ishion_PageSize & " " & ishion_Field & " FROM " & ishion_Table & " WHERE " & ishion_Id & ishion_Order & "'"& ishion_Mm &"'" & ishion_WhereOther & " " & ishion_OrderBy
                       ' response.Write ishion_Mm
                       ' response.End
                    End If
                    Set ResultSet = Conn.Execute(ResultSet_Sql)
                Case "MSSQL_SP" 'sqlserver2000数据库存储过程版
                    'Set ishion_Rs=server.CreateObject("Adodb.RecordSet")
                    Set ishion_Cm = Server.CreateObject("Adodb.Command")
                    ishion_Cm.CommandType = 4
                    ishion_Cm.ActiveConnection = Conn
                    ishion_Cm.CommandText = "SP_ishion_showPage"
                    ishion_Cm.Parameters(1) = ishion_currentPage
                    ishion_Cm.Parameters(2) = ishion_PageSize
                    ishion_Cm.Parameters(3) = ishion_Field
                    ishion_Cm.Parameters(4) = ishion_Table
                    ishion_Cm.Parameters(5) = ishion_Where
                    ishion_Cm.Parameters(6) = ishion_WhereOther
                    ishion_Cm.Parameters(7) = ishion_OrderBy
                    ishion_Cm.Parameters(8) = ishion_Id
                    ishion_Cm.Parameters(9) = ishion_Size
                    ishion_Cm.Parameters(10) = ishion_Order
                    Set ResultSet = ishion_Cm.Execute
                Case Else '其他情况按最原始的方法处理
                    Set ResultSet = Server.CreateObject("adodb.RecordSet")
                    ResultSet_Sql = "SELECT " & ishion_Field & " FROM " & ishion_Table & " " & ishion_Where & " " & ishion_OrderBy
                    ResultSet.open ResultSet_Sql, Conn, 1, 1, &H1
                    ResultSet.AbsolutePosition = (ishion_currentPage - 1) * ishion_PageSize + 1
            End Select
        End If
    End Property

    '================================================================
    ' 输入:检查字段,开始数字(默认数字),结束数字(为-1则不检查大小)
    '================================================================
    Private Function CheckNum(ByVal strStr, ByVal intStartNum, ByVal intEndNum)
        CheckNum = intStartNum
        If IsNumeric(strStr) Then CheckNum = CLng(strStr)
        If intEndNum > -1 Then
            If CheckNum < intStartNum Then CheckNum = intStartNum
            If CheckNum > intEndNum Then CheckNum = intEndNum
        End If
    End Function

    '================================================================
    ' 上下页部分
    '================================================================
    Public Function showPage(FileName)
        Const Btn_First = "<font face=""webdings"" color=red Title=""First Page"">9</font>" '定义第一页按钮显示样式
        Const Btn_Prev = "<font face=""webdings"" color=red Title=""Prev Page"">3</font>"   '定义前一页按钮显示样式
        Const Btn_Next = "<font face=""webdings"" color=red Title=""Next Page"">4</font>"   '定义下一页按钮显示样式
        Const Btn_Last = "<font face=""webdings"" color=red Title=""Last Page"">:</font>"   '定义最后一页按钮显示样式
        Const BtnNum_Count = 6      '定义最多显示多少页分页
        Const Btn_more = ".."   '定义更多按钮
        Dim sPageInfo, totalRecord, totalPage
        Dim showNumBtn  '数字导航
        Dim showFirstPrev   '首页、前一页
        Dim showNextLast    '下一页、末页
        totalRecord = RecCount()
        If (totalRecord Mod ishion_PageSize) = 0 Then
            totalPage = totalRecord \ ishion_PageSize
        Else
            totalPage = totalRecord \ ishion_PageSize + 1
        End If
        sPageInfo = "页次:<b><font color=red>" & ishion_currentPage & "</font></b>/" & totalPage & " 每页:<font color=red>" & ishion_PageSize & "</font> 共计:<font color=red>" & totalRecord & "</font>"
        
        '数字导航
        Dim i, str_tmp
        Dim start_p, end_p
        Dim Str_start, str_end
        If totalPage < BtnNum_Count Then
            For i = 1 To totalPage
				If i = ishion_CurrentPage Then
					str_tmp = str_tmp & "<b><font color=red>" & i & "</font></b> "
				Else
					str_tmp = str_tmp & "[<a href=" & Replace(FileName, "{$pages}", i) & ">" & i & "</a>] "
				End If
            Next
        Else
            If ishion_currentPage <= BtnNum_Count Then
                start_p = 1
                end_p = BtnNum_Count
                Str_start = ""
                str_end = "<a href=" & Replace(FileName, "{$pages}", end_p + 1) & ">" & Btn_more & "</a> "
            ElseIf ishion_currentPage > (totalPage \ BtnNum_Count) * BtnNum_Count Then
                start_p = (totalPage \ BtnNum_Count) * BtnNum_Count + 1
                end_p = totalPage
                Str_start = "<a href=" & Replace(FileName, "{$pages}", start_p - 1) & ">" & Btn_more & "</a> "
                str_end = ""
            Else
                If (ishion_currentPage Mod BtnNum_Count) = 0 Then
                    start_p = ishion_currentPage - BtnNum_Count + 1
                    end_p = ishion_currentPage
                Else
                    start_p = (ishion_currentPage \ BtnNum_Count) * BtnNum_Count + 1
                    end_p = (ishion_currentPage \ BtnNum_Count) * BtnNum_Count + BtnNum_Count
                End If
                Str_start = "<a href=" & Replace(FileName, "{$pages}", start_p - 1) & " >" & Btn_more & "</a> "
                str_end = "<a href=" & Replace(FileName, "{$pages}", end_p + 1) & ">" & Btn_more & "</a> "
            End If
            For i = start_p To end_p
                If i = ishion_currentPage Then
                    str_tmp = str_tmp & "<b><font color=red>" & i & "</font></b> "
                Else
                    str_tmp = str_tmp & "[<a href=" & Replace(FileName, "{$pages}", i) & ">" & i & "</a>] "
                End If
            Next
                str_tmp = Str_start & str_tmp & str_end
        End If
        showNumBtn = str_tmp
        i = Empty: str_tmp = Empty
        start_p = Empty: end_p = Empty
        Str_start = Empty: str_end = Empty
        '首页、前一页
        If ishion_currentPage = 1 Then
            showFirstPrev = Btn_First & " " & Btn_Prev & " "
        Else
            showFirstPrev = "<a href=" & Replace(FileName, "{$pages}", "1") & ">" & Btn_First & "</a> <a href=" & Replace(FileName, "{$pages}", ishion_currentPage - 1) & "> " & Btn_Prev & "</a> "
        End If
        '下一页、末页
        If ishion_currentPage >= totalPage Then
            showNextLast = Btn_Next & " " & Btn_Last & " "
        Else
            showNextLast = "<a href=" & Replace(FileName, "{$pages}", ishion_currentPage + 1) & ">" & Btn_Next & "</a> <a href=" & Replace(FileName, "{$pages}", totalPage) & ">" & Btn_Last & "</a> "
        End If
    
        showPage = showPage & "<table width=""100%"" border=""0"" style=""BORDER-COLLAPSE: collapse"" borderColor=""#111111"">" & vbCrLf
        showPage = showPage & "<form onsubmit=""window.location=this.goto.options[this.goto.selectedIndex].value; return false;"">" & vbCrLf
        showPage = showPage & "  <tr>" & vbCrLf
        showPage = showPage & "    <td style=""font-family: Verdana,宋体; font-size: 12px; line-height: 15px"">" & sPageInfo & "</td>" & vbCrLf
        showPage = showPage & "    <td align=""right"" style=""font-family: Verdana,宋体; font-size: 12px; line-height: 15px"">" & showFirstPrev & showNumBtn & showNextLast & "" & vbCrLf
        showPage = showPage & " <Script Language=""JavaScript"">" & vbCrLf
        showPage = showPage & " document.write (""Go:<select name=\""select\"" onchange=\""javascript:window.location.href=this.options[this.selectedIndex].value\"">"");" & vbCrLf
        showPage = showPage & " var url=""" & FileName & """;" & vbCrLf
        showPage = showPage & " for(var iPage = 1; iPage < " & totalPage & "+1; iPage++)  {" & vbCrLf
        showPage = showPage & " document.write (""<option value=\""""+url.replace(""{$pages}"",iPage)+""\"""");" & vbCrLf
        showPage = showPage & "     if (iPage == " & ishion_currentPage & ")" & vbCrLf
        showPage = showPage & "     document.write ("" selected"");" & vbCrLf
        showPage = showPage & "     else" & vbCrLf
        showPage = showPage & "     document.write ("""");" & vbCrLf
        showPage = showPage & " document.write ("">第""+iPage+""页</option>"");" & vbCrLf
        showPage = showPage & " }" & vbCrLf
        showPage = showPage & " document.write (""</select>"")" & vbCrLf
        showPage = showPage & " </Script>" & vbCrLf
        showPage = showPage & " </td>" & vbCrLf
        showPage = showPage & "  </tr>" & vbCrLf
        showPage = showPage & "  </form>" & vbCrLf
        showPage = showPage & "</table> " & vbCrLf
    End Function
End Class %>

