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

create PROCEDURE pr_down_item_info
(	@item_no u_item_no  ,
	@item_subno varchar (15)  ,
	@item_name varchar (40)  ,
	@item_subname varchar (20)  ,
	@item_clsno u_item_clsno  ,
	@unit_no char (4)  ,
	@item_size varchar (20)  ,
	@price u_price  ,
	@base_price u_price  ,
	@sale_price u_price  ,
	@combine_sta char (1)  ,		--商品类型
	@status char (1)  ,	
	@main_supcust char (8)  ,
	@item_stock char (1)  ,			--管理库存	
	@item_rem varchar (10)  ,
	@vip_price u_price  ,
	@vip_acc_flag char (1)  ,
	@vip_acc_num numeric(16, 4)  ,
	@sale_flag char (1)  ,			--商品属性
	@scheme_price u_price  ,
	@product_area char (18)  ,
	@measure_flag char (1)  ,		--计价方式
	@item_sup_flag char (1)  ,
	@branch_price	char(1),		--分店变价
	@change_price	char(1),		--前台议价
	@en_dis			char(1),		--前台折扣
	@en_gift		char(1),		--前台赠送
	@outParam varchar(20) output
)
AS
BEGIN
	--下传商品信息
	--通过t_sys_cm_breakpoint中的com_table值为t_sys_cm_data的判断大于bk_flow的值在t_sys_cm_datta
	--在t_sys_cm_datta里找com_no>bk_flow的 com_table='t_bd_item_info'的数据，key1是货号，这里面有多个key1只处理一个就可以
	--更新完，再把当前最大的com_no更新到bk_flow，以及bk_datetime为当前日期

	if not exists(select * from t_bd_item_info where item_no=@item_no)
	begin
		insert into t_bd_item_info(item_no,item_subno,item_name,item_subname,item_clsno,item_brand,item_brandname,unit_no,item_size,price,base_price,sale_price,combine_sta,status,
				display_flag,po_cycle,so_cycle,automin_flag,en_dis,change_price,en_gift,purchase_spec,shipment_spec,main_supcust,item_stock,build_date,branch_price,item_rem,vip_price,vip_acc_flag,vip_acc_num,sale_flag,scheme_price,product_area,measure_flag,item_sup_flag,new_oper_id)
				values(@item_no,@item_subno,@item_name,@item_subname,@item_clsno,'PP','PM',@unit_no,@item_size,@scheme_price,@base_price,@sale_price,@combine_sta,@status,
				'1','0','0','1',@en_dis,@change_price,@en_gift,1,1,@main_supcust,'0',getdate(),@branch_price,@item_rem,@vip_price,@vip_acc_flag,@vip_acc_num,@sale_flag,@scheme_price,@product_area,@measure_flag,@item_sup_flag,'1001')
	end 
	else
	begin
		if exists(select *  from t_sys_system where sys_var_id='auto_down_notprice' and isnull(sys_var_value,'0')='1')
			begin
			update t_bd_item_info
				set item_subno 	=@item_subno  ,
					item_name 	=@item_name  ,
					item_subname =@item_subname  ,
					item_clsno 	=@item_clsno  ,
					unit_no 	=@unit_no ,
					item_size	= @item_size ,
					price 		= @scheme_price ,
					--base_price	=@base_price   ,
					--sale_price 	=@sale_price  ,
					combine_sta =@combine_sta  ,
					status 		=@status ,	
					main_supcust	=@main_supcust  ,
					item_stock 	=@item_stock  ,	
					item_rem 	=@item_rem  ,
					--vip_price 	=@vip_price  ,
					vip_acc_flag 	=@vip_acc_flag  ,
					vip_acc_num 	=@vip_acc_num  ,
					sale_flag 		=@sale_flag ,
					scheme_price 	=@scheme_price  ,
					product_area 	=@product_area  ,
					measure_flag 	=@measure_flag ,
					item_sup_flag 	=@item_sup_flag,
					branch_price	=@branch_price, 
					change_price	=@change_price,
					en_dis			=@en_dis,
					en_gift			=@en_gift
				where item_no=@item_no
			end
		  else
			begin
				update t_bd_item_info
				set item_subno 	=@item_subno  ,
					item_name 	=@item_name  ,
					item_subname =@item_subname  ,
					item_clsno 	=@item_clsno  ,
					unit_no 	=@unit_no ,
					item_size	= @item_size ,
					price 		= @scheme_price ,
					--base_price	=@base_price   ,
					--sale_price 	=@sale_price  ,
					combine_sta =@combine_sta  ,
					status 		=@status ,	
					main_supcust	=@main_supcust  ,
					item_stock 	=@item_stock  ,	
					item_rem 	=@item_rem  ,
					--vip_price 	=@vip_price  ,
					vip_acc_flag 	=@vip_acc_flag  ,
					vip_acc_num 	=@vip_acc_num  ,
					sale_flag 		=@sale_flag ,
					scheme_price 	=@scheme_price  ,
					product_area 	=@product_area  ,
					measure_flag 	=@measure_flag ,
					item_sup_flag 	=@item_sup_flag,
					branch_price	=@branch_price, 
					change_price	=@change_price,
					en_dis			=@en_dis,
					en_gift			=@en_gift
				where item_no=@item_no
			end
	end

	select @outParam=''
End
go
