IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_update_product_base]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_update_product_base]
GO
CREATE proc [dbo].[mps_update_product_base]
@cmpid int,
@prd_id int,
@prd_cuid int,
@prd_pcid int,
@prd_code varchar(32),
@prd_barcode varchar(32),
@prd_title nvarchar(100),
@prd_icon1 varchar(200),
@prd_icon2 varchar(200),
@prd_factory nvarchar(100),
@prd_supplier nvarchar(100),
@prd_model nvarchar(100),
@prd_spec nvarchar(100),
@prd_bacth nvarchar(100),
@prd_pack nvarchar(100),
@prd_brand nvarchar(100),
@prd_area nvarchar(100),
@prd_unit nvarchar(50),
@prd_abstract nvarchar(200),
@prd_label nvarchar(200),
@prd_productdate varchar(50),
@prd_expireddays int,
@prd_price varchar(50),
@prd_attachi int,
@prd_attaches varchar(100),
@prd_security int

as
begin
 set nocount on;
declare @prd_tag int,
@prd_fprdid bigint,
--@prd_click int,
@prd_bcid int,
@prd_branch nvarchar(50),
@prd_creator nvarchar(50),
@prd_dpid int,
@prd_depart nvarchar(50)
--返回类别信息
select * from mps_prdctg where pc_id=@prd_pcid
--获取当前用户的信息
select @prd_bcid=cu_bcid,@prd_branch=cu_branch,@prd_creator=cu_name,@prd_dpid=cu_dpid,@prd_depart=cu_depart from mps_employee where cu_id=@prd_cuid
--获取类别相关的信息
select @prd_tag=pc_tag,@prd_fprdid=pc_father from mps_prdctg where pc_id=@prd_pcid
--如果prd_id=0 说明是新建,则创建一个新的
if(@prd_id=0)
begin
--查找是否有临时的数据,如果有则返回临时数据,如果没有则创建一条
select @prd_id=prd_id from mps_prd_base where prd_code='(临)' and prd_cmpid = @cmpid and prd_cuid=@prd_cuid and prd_pcid = @prd_pcid 
--没有找到临时的
if(@prd_id=0)
begin
insert into mps_prd_base(prd_cmpid,prd_bcid,prd_branch,prd_dpid,prd_depart,prd_cuid,prd_creator,prd_pcid,prd_security,prd_code)
values(@cmpid,@prd_bcid,@prd_branch,@prd_dpid,@prd_depart,@prd_cuid,@prd_creator,@prd_pcid,5,'(临)')
select @prd_id=@@IDENTITY
end
end 
else begin
--更新产品表
update mps_prd_base set
prd_code=case when @prd_code='_none_' then prd_code else @prd_code end,
prd_barcode=case when @prd_barcode='_none_' then prd_barcode else @prd_barcode end,
prd_title=case when @prd_title='_none_' then prd_title else @prd_title end,
prd_icon1=case when @prd_icon1='_none_' then prd_icon1 else @prd_icon1 end,
prd_icon2=case when @prd_icon2='_none_' then prd_icon2 else @prd_icon2 end,
prd_factory=case when @prd_factory='_none_' then prd_factory else @prd_factory end,
prd_supplier=case when @prd_supplier='_none_' then prd_supplier else @prd_supplier end,
prd_model=case when @prd_model='_none_' then prd_model else @prd_model end,
prd_spec=case when @prd_spec='_none_' then prd_spec else @prd_spec end,
prd_bacth=case when @prd_bacth='_none_' then prd_bacth else @prd_bacth end,
prd_pack=case when @prd_pack='_none_' then prd_pack else @prd_pack end,
prd_brand=case when @prd_brand='_none_' then prd_brand else @prd_brand end,
prd_area=case when @prd_area='_none_' then prd_area else @prd_area end,
prd_unit=case when @prd_unit='_none_' then prd_unit else @prd_unit end,
prd_abstract=case when @prd_abstract='_none_' then prd_abstract else @prd_abstract end,
prd_label=case when @prd_label='_none_' then prd_label else @prd_label end,
prd_productdate=case when @prd_productdate='_none_' then prd_productdate else @prd_productdate end,
prd_price=case when @prd_price='_none_' then prd_price else CONVERT(decimal(18,4),@prd_price) end,
prd_expireddays=case when @prd_expireddays=-999999999 then prd_expireddays else @prd_expireddays end,
prd_attachi=case when @prd_attachi=-999999999 then prd_attachi else @prd_attachi end,
prd_security=case when @prd_security=-999999999 then prd_security else @prd_security end,
prd_update=GETDATE()
where prd_id=@prd_id
end
--返回产品信息
select * from mps_prd_base where prd_id=@prd_id
--返回附件信息
select * from mps_attach where da_type='prd' and da_objid=@prd_id
end