IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_prd2_newprd]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_prd2_newprd]
GO

-- 生成时间:2018-11-23 16:44:50 by ebs-core  
CREATE  procedure mps_prd2_newprd(
     @cmpid int, 
     @branchid int, 
     @userdpid int, 
     @userotid int, 
     @useruid int, 
     @pcid int,
     @returndatasets int ,  
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @newprdid bigint output  
  ) 
  as  
  begin  
  set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  
 
  if ((@extparam1 is null) or (@extparam1='_none_'))  set @extparam1 = '' 
  if ((@extparam2 is null) or (@extparam2='_none_'))  set @extparam2 = '' 
  if ((@extparam3 is null) or (@extparam3='_none_'))  set @extparam3 = '' 
  
  
  set @newprdid = 0  
  
  declare @bizcode nvarchar(100)    
  select   @bizcode=pc_bmcode  from mps_prdctg where  pc_id=@pcid    
  declare @ccno varchar(50)     
select top 1 @newprdid=prd_id from mps_prd2 where prd_cmpid=@cmpid and prd_pcid=@pcid and prd_createcuid=@useruid and (prd_code like '%临%' or prd_code = '') 
  set @newprdid = ISNULL(@newprdid,0) 
  if (@newprdid>0) 
  begin
update mps_prd2 set prd_createtime = GETDATE() , prd_updated = GETDATE() where prd_id=@newprdid 
  end else 
  begin
  insert into mps_prd2(prd_cmpid,prd_bcid,prd_branch,prd_dpid,prd_depart,prd_cuid,prd_cuname,prd_tag,prd_caption,prd_pcid,prd_type,prd_title,
  prd_createdpid,prd_createdepart,prd_createcuid,prd_creator,prd_createtime,prd_updated) 
  select @cmpid,bc_id,bc_company,dp_id,dp_depart,cu_id,cu_callname,pc_tag,pc_caption,pc_id,pc_type, '',
  dp_id,dp_depart,cu_id,cu_callname,getdate(),getdate() 
from mps_prdctg,mps_login,mps_depart,mps_groupbranch 
where pc_id=@pcid and cu_id=@useruid and dp_id=@userdpid and bc_id=@branchid  and bc_cmpid=@cmpid  
select top 1 @newprdid = prd_id from mps_prd2 where prd_cmpid=@cmpid and prd_pcid=@pcid and prd_createcuid=@useruid and prd_title='' order by prd_id desc  
  
set @newprdid = isnull(@newprdid,0) 
-- 生成订单编号（临时） 
if (@bizcode>'') 
begin
exec mps_bm_getorset @cmpid,'prd',@pcid,@newprdid,@useruid,@bizcode,2,@ccno output,1   
end else 
begin
set @ccno = '' 
end 
update mps_prd2 set prd_code = @ccno where prd_id = @newprdid   
   
  -- why here otid=-1 , it's a secret , I don't know yet 
  if (not exists (select 1 from mps_prd2_users where prdp_type='prdview' and prdp_prdid=@newprdid and prdp_cuid=@useruid)) 
  insert into mps_prd2_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) 
  select distinct 'prdview',@newprdid,@useruid,-1  
  if (not exists (select 1 from mps_prd2_users where prdp_type='prdedit' and prdp_prdid=@newprdid and prdp_cuid=@useruid)) 
  insert into mps_prd2_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) 
  select distinct 'prdedit',@newprdid,@useruid,-1  
  end 
   
  -- 返回客户数据  

  if (@returndatasets=0) select * from  mps_prd2 where prd_id=@newprdid 
  if (@returndatasets=2) select @newprdid newccid   
  set nocount off  
end