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



CREATE procedure pr_wxsf_distribution_ordercount  
@cardid nvarchar(50) --分销员卡号

WITH ENCRYPTION
as

declare @settlement_count int --待结算数量
declare @cashing_count int --可提现数量
declare @cashed_count int --已提现数量
declare @expired_count int --已过期数量
declare @day int --可领取的天数
begin

   if not exists(SELECT  1   from t_sys_system where sys_var_id='FXConsumeAfterDay')
   begin
     set @day=0
   end
   else
   begin
    SELECT  @day= isnull(sys_var_value,0) from t_sys_system where sys_var_id='FXConsumeAfterDay'
   end
  
  --获取待结算数量
  select @settlement_count=COUNT(1) from (select res.flow_no from t_vip_fathercard_deduct_flow res
  where res.father_card=@cardid and ISNULL(res.Fa_status,'0')='0' and   DATEDIFF (day,res.oper_date,GETDATE())<CONVERT(int,@day) 
  GROUP BY res.flow_no)t 
  
  --可提现数量
  select @cashing_count=COUNT(1) from (select res.flow_no from t_vip_fathercard_deduct_flow res
  where res.father_card=@cardid and ISNULL(res.Fa_status,'0')='0' 
  and  DATEDIFF (day,res.oper_date,GETDATE())>=CONVERT(int,@day) 
  GROUP BY res.flow_no)t 
  
    
  --已提现数量
  select @cashed_count=COUNT(1) from (select res.flow_no from t_vip_fathercard_deduct_flow res
  where res.father_card=@cardid and ISNULL(res.Fa_status,'0')='1' 
  GROUP BY res.flow_no)t
  
    --已提现数量
  select @expired_count=COUNT(1) from (select res.flow_no from t_vip_fathercard_deduct_flow res
  where res.father_card=@cardid and ISNULL(res.Fa_status,'0')='2' 
  GROUP BY res.flow_no)t
  
  
  select @settlement_count as settlement_count,@cashing_count as cashing_count,@cashed_count as cashed_count,
  @expired_count as expired_count
  
end
go