if exists(select 1 from sysobjects where name='pr_wxsf_distribution_incomerecord')
  drop procedure pr_wxsf_distribution_incomerecord
go



CREATE procedure pr_wxsf_distribution_incomerecord  
@cardid nvarchar(50), --分销员卡号
@starttime datetime, --开始时间
@endtime datetime ,--结束时间
@pageindex int ,--页码
@pagesize int,--每页获取数量
@total int out, --总行数
@totalamount decimal(18,2) out --该时间段总提现金额
WITH ENCRYPTION
as
begin
	set @totalamount=0
	--总行数
	  
	--返回数据  
	--CREATE TABLE #temp_flow(row_id INT IDENTITY(1,1), cash_amount NUMERIC(16,2),cash_time DATETIME)          
	 
	SELECT IDENTITY(INT,1,1) AS rowid,t.cash_amount, t.fa_date AS cash_time
	INTO #temp_flow
	FROM    ( SELECT    
						CONVERT(DECIMAL(18, 2), SUM(res.deduct_amt)) AS cash_amount,
						CONVERT(NVARCHAR(16), res.fa_date, 120) AS fa_date
			  FROM      t_vip_fathercard_deduct_flow res
			  WHERE     (res.father_card = @cardid OR @cardid = '') 
			  AND CONVERT(NVARCHAR(16), res.fa_date, 120) BETWEEN CONVERT(NVARCHAR(16), @starttime, 120)
				AND CONVERT(NVARCHAR(16), @endtime, 120) 
			  AND ISNULL(res.Fa_status,'0') = '1'
			  GROUP BY  CONVERT(NVARCHAR(16), res.fa_date, 120) 
			 ) t
	
	SELECT @total = COUNT(1) FROM #temp_flow
	
	SELECT @totalamount = SUM(cash_amount) FROM #temp_flow
	
	SELECT cash_amount,cash_time FROM #temp_flow
	WHERE   RowId BETWEEN @pageSize * ( @pageIndex - 1 ) + 1
					AND     @pageSize * @pageIndex

end
go
