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

create procedure pr_down_branch_price
(
   @branch_no varchar(10),
   @item_no varchar(20),
   @price 	numeric(16,4),
   @base_price 	numeric(16,4),
	@sale_price 	numeric(16,4),
	@vip_price 	numeric(16,4),
   	@scheme_price 	numeric(16,4),
	@vip_price2 	numeric(16,4),
	@vip_price3 	numeric(16,4),
	@vip_price4 	numeric(16,4),
	@vip_price5 	numeric(16,4),
	@price_list		varchar(20),
	@sale_flag		char(1),
   	@outParam varchar(20) output
)
as
begin
	
	declare @auto_down_branch_price varchar(1), --是不是分店更新分店价格表里面的零售价20210409
			@sale_Price_old numeric(16,4)		--原来零售价
	
	select @auto_down_branch_price=isnull(sys_var_value,'0') from t_sys_system where sys_var_id='auto_down_notprice'
	if @auto_down_branch_price='1'  
	begin
		select @sale_Price_old=sale_price from t_pc_branch_price where  branch_no=@branch_no and item_no=@item_no
		if @sale_Price_old>0 
			select @sale_price=@sale_Price_old
	end

	
	--下传分店价格表
	
    if not exists (select 1 from t_pc_branch_price where branch_no=@branch_no and item_no=@item_no)
    BEGIN
      insert into t_pc_branch_price(branch_no,item_no,price,base_price,sale_price,vip_price,scheme_price,vip_price2,vip_price3,vip_price4,vip_price5,price_list,sale_flag)
                           values (@branch_no,@item_no,@scheme_price,@base_price,@sale_price,@vip_price,@scheme_price,@vip_price2,@vip_price3,@vip_price4,@vip_price5,@price_list,@sale_flag)
    END
	else
	begin
		alter table t_pc_branch_price disable trigger all
	
		update t_pc_branch_price
		set price=case when isnull(substring(@price_list,4,1),'0')='1' then @scheme_price else price end,
			base_price=case when isnull(substring(@price_list,2,1),'0')='1' then @base_price else base_price end ,
			sale_price=case when isnull(substring(@price_list,3,1),'0')='1' then @sale_price else sale_price end ,
			vip_price=case when isnull(substring(@price_list,5,1),'0')='1' then @vip_price else vip_price end ,
			scheme_price=case when isnull(substring(@price_list,4,1),'0')='1' then @scheme_price else scheme_price end,
			vip_price2=case when isnull(substring(@price_list,6,1),'0')='1' then @vip_price2 else vip_price2 end ,
			vip_price3=case when isnull(substring(@price_list,7,1),'0')='1' then @vip_price3 else vip_price3 end,
			vip_price4=case when isnull(substring(@price_list,8,1),'0')='1' then @vip_price4 else vip_price4 end,
			vip_price5=case when isnull(substring(@price_list,9,1),'0')='1' then @vip_price5 else vip_price5 end,
			price_list=@price_list,
			sale_flag =@sale_flag
		where branch_no=@branch_no and item_no=@item_no
		
		alter table t_pc_branch_price enable trigger all
	end

	--修改商品档案
	if exists(select * from t_bd_item_info where item_no=@item_no)
	begin
		alter table t_bd_item_info disable trigger all
		update a
			set a.price=b.scheme_price,
				a.base_price=b.base_price,
				a.sale_price=b.sale_price,
				a.vip_price=b.vip_price,
				a.scheme_price=b.scheme_price,
				a.vip_price2=b.vip_price2,
				a.vip_price3=b.vip_price3,
				a.vip_price4=b.vip_price4,
				a.vip_price5=b.vip_price5,
				a.sale_flag =@sale_flag
		from t_bd_item_info a,
		t_pc_branch_price b
		where a.item_no=b.item_no
			and b.item_no=@item_no
			and b.branch_no=@branch_no

		alter table t_bd_item_info enable trigger all

		select @outParam=''
	end
	else
	begin
		--商品不存在返回商品编号
		select @outParam=@item_no
	end	
end
go

