CREATE PROCEDURE [dbo].[sp_xt_pagesplit]
@tbname sysname,
@fieldkey sysname,
@where nvarchar(1000),
@fieldshow nvarchar(max),
@fieldOrder nvarchar(1000),
@PageCurrent int=1,
@PageSize int=30,
@PageCount int OUTPUT,
@ItemCount int output
AS
BEGIN
    SET NOCOUNT ON

    -- 参数检查
    IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
    IF ISNULL(@PageSize,0)<1 SET @PageSize=10
    IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'

    -- 处理排序
    DECLARE @OrderBy nvarchar(1000)
    IF ISNULL(@FieldOrder,N'')=N''
        SET @OrderBy = N''
    ELSE
        SET @OrderBy = N'ORDER BY ' + LTRIM(@FieldOrder)

    -- 处理条件 - 重要：去掉WHERE关键字，只保留条件内容
    DECLARE @WhereCondition nvarchar(1000)
    IF ISNULL(@Where,N'')=N''
        SET @WhereCondition = N''
    ELSE
        SET @WhereCondition = @Where  -- 只保存条件内容，不加WHERE

    -- 清理字段名
    SET @fieldkey = LTRIM(RTRIM(@fieldkey))
    DECLARE @quotedKey NVARCHAR(128) = QUOTENAME(@fieldkey)

    -- 计算总记录数
    DECLARE @sql nvarchar(MAX)
    IF @WhereCondition = N''
        SET @sql = N'SELECT @ItemCount = COUNT(*) FROM ' + @tbname
    ELSE
        SET @sql = N'SELECT @ItemCount = COUNT(*) FROM ' + @tbname + N' WHERE ' + @WhereCondition

    EXEC sp_executesql @sql, N'@ItemCount int OUTPUT', @ItemCount OUTPUT

    -- 计算总页数
    SET @PageCount = CEILING((@ItemCount + 0.0) / @PageSize)
    IF @PageCount = 0 SET @PageCount = 1

    -- 返回当前页数据
    DECLARE @TopRows INT = (@PageCurrent - 1) * @PageSize

    IF @PageCurrent = 1
    BEGIN
        -- 第一页
        IF @WhereCondition = N''
            SET @sql = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize) + N' ' + @FieldShow + N'
                        FROM ' + @tbname + N' ' + @OrderBy
        ELSE
            SET @sql = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize) + N' ' + @FieldShow + N'
                        FROM ' + @tbname + N' WHERE ' + @WhereCondition + N' ' + @OrderBy
    END
    ELSE
    BEGIN
        -- 其他页
        IF @WhereCondition = N''
        BEGIN
            -- 无条件的情况
            SET @sql = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize) + N' ' + @FieldShow + N'
                        FROM ' + @tbname + N'
                        WHERE ' + @quotedKey + N' NOT IN (
                            SELECT TOP ' + CONVERT(NVARCHAR, @TopRows) + N' ' + @quotedKey + N'
                            FROM ' + @tbname + N' ' + @OrderBy + N'
                        ) ' + @OrderBy
        END
        ELSE
        BEGIN
            -- 有条件的情况
            SET @sql = N'SELECT TOP ' + CONVERT(NVARCHAR, @PageSize) + N' ' + @FieldShow + N'
                        FROM ' + @tbname + N'
                        WHERE ' + @quotedKey + N' NOT IN (
                            SELECT TOP ' + CONVERT(NVARCHAR, @TopRows) + N' ' + @quotedKey + N'
                            FROM ' + @tbname + N' WHERE ' + @WhereCondition + N' ' + @OrderBy + N'
                        ) AND ' + @WhereCondition + N' ' + @OrderBy
        END
    END

    -- 调试用，可以注释掉
    -- PRINT @sql

    -- 执行
    EXEC(@sql)
END
