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



CREATE procedure pr_wxsf_distribution_plan_detail  
@plan_no nvarchar(50) --方案编号

WITH ENCRYPTION
as

begin
 
  declare @push_type nvarchar(10) --分销类型
  
  create table #resulttb
  (
  no varchar(50),
  name varchar(50),
  percentage decimal(18,4),
  distribution_type varchar(50),
  percentage_type varchar(50) 
  )
  
  if(@plan_no='') 
   begin
   
    return
   end
	
	insert into #resulttb(no,name,percentage,distribution_type,percentage_type)
      select 
		dt.code_no ,
		CASE dt.push_type
			WHEN 'C' THEN (SELECT cls.item_clsname FROM t_bd_item_cls cls WHERE cls.item_clsno = dt.code_no)
			WHEN 'B' THEN (SELECT brand.code_name FROM t_bd_base_code brand WHERE brand.code_id = dt.code_no AND brand.type_no = 'PP')
			WHEN 'I' THEN (SELECT item.item_no FROM t_bd_item_info item WHERE item.item_no = dt.code_no)
		end,
		dt.push_amt,
		distribution_type  = CASE dt.push_type WHEN 'C' THEN '按类别' WHEN 'B' THEN '按品牌' WHEN 'I' THEN '按商品' ELSE '' END,
		percentage_type  = CASE dt.push_way WHEN '0' THEN '按比例' WHEN '1' THEN '按金额' ELSE '' END
		from t_vip_father_card_detail dt 
		where dt.plan_no=@plan_no
      
  /*
  select @push_type=push_type from t_vip_father_card_detail ms where ms.plan_no=@plan_no
  if(@push_type='C')
     begin
      insert into #resulttb(no,name,percentage)
      select dt.code_no ,cls.item_clsname,dt.push_amt from t_vip_father_card_detail dt inner join t_bd_item_cls cls
      on dt.code_no=cls.item_clsno where dt.plan_no=@plan_no
     end
   else if(@push_type='B')   
     begin
      insert into #resulttb(no,name,percentage)
      select dt.code_no ,brand.item_brandname,dt.push_amt from t_vip_father_card_detail dt inner join t_bd_item_brand brand
      on dt.code_no=brand.item_brandno where dt.plan_no=@plan_no
     end
    else if(@push_type='I')   
     begin
      insert into #resulttb(no,name,percentage)
      select dt.code_no ,item.item_name,dt.push_amt from t_vip_father_card_detail dt inner join t_bd_item_info item
      on dt.code_no=item.item_no where dt.plan_no=@plan_no
     end 
    */
    select * from #resulttb
end
go