IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_import_prd]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_import_prd]
GO
create procedure [dbo].[mps_import_prd](
@cmpid int, 
@branchid int, 
@userdpid int, 
@userotid int, 
@useruid int, 
@ctid int,
@allowupdate int , 
@keyflds varchar(500),
@dataflds varchar(1000) ,
@datas nvarchar(MAX) 
) as
begin

declare @dbname varchar(30) 
declare @tbname varchar(50) 
declare @pkname varchar(50) 
declare @mctid int 
set @mctid = @ctid * (-1) 

if (@keyflds='') set @keyflds='prd_code' 

declare @cc_branch nvarchar(50) 
declare @cc_depart nvarchar(50) 
declare @cc_cuname nvarchar(50) 
declare @cc_tag int  
declare @cc_caption nvarchar(50) 
declare @cc_type nvarchar(50) 

select top 1 @cc_branch = bc_company from mps_groupbranch where bc_id=@branchid and bc_cmpid=@cmpid 
select top 1 @cc_depart=dp_depart from mps_depart where dp_id=@userdpid 
select top 1 @cc_tag = pc_tag , @cc_caption=pc_caption , @cc_type=pc_type from mps_prdctg where pc_id=@ctid 
select top 1 @cc_cuname = cu_callname from mps_login where cu_id=@useruid 


exec sys_sql_getTableNamePk @cmpid , 'prd',@mctid,0,@tbname output , @pkname output ,@dbname output 
set @tbname  = @dbname + @tbname 
declare @sql nvarchar(MAX) 
set @sql = '' 

declare @ts1 varchar(1000) 
declare @v1 nvarchar(500) 
declare @j int 
declare @f1 nvarchar(500) 
declare @keycond nvarchar(4000)
declare @vallist nvarchar(4000)
declare @fldlist nvarchar(4000)

declare @i int,@curInd int,@strlen int,@tmps nvarchar(4000)  

select @curInd=1,@strlen=LEN(@datas) 
create table #ord2(ord2id bigint) 
declare @neword2id bigint 
declare @maxord2id bigint 
while (@curInd<=@strlen) 
begin 
set @sql = '' 
select @i=charindex('///',@datas,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@datas,@curInd,@i-@curInd) 
set @curInd=@i+3 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@datas,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
set @keycond = '' 
set @vallist = '' 
set @fldlist = '' 
set @f1 = ''
set @ts1 = @dataflds 
while (@tmps>'') 
begin
set @j = charindex('||',@tmps) 
if (@j>0) 
begin
set @v1 = substring(@tmps,1,@j-1) 
set @tmps = substring(@tmps,@j+2, len(@tmps) - @j - 1) 
end else 
begin
set @v1 = @tmps 
set @tmps = '' 
end

set @j = charindex(',',@ts1) 
if (@j>0) 
begin
set @f1 = substring(@ts1,1,@j-1) 
set @ts1 = substring(@ts1,@j+1, len(@ts1) - @j ) 
end else 
begin
set @f1 = @ts1 
set @ts1 = '' 
end
if (@f1>'') 
begin
if (@fldlist>'') set @fldlist=@fldlist+','
set @fldlist=@fldlist +@f1 
if (charindex(','+@f1+',',','+@keyflds+',')>0) 
begin
if (@keycond>'') set @keycond=@keycond+' and ' 
set @keycond = @keycond+' '+@f1+'='''+@v1+''' ' 
end
if (@vallist>'') set @vallist=@vallist+',' 
set @vallist=@vallist+''''+@v1+'''' 
end 
end
if (@keyflds='') 
begin
set @sql = 
'insert into '+@tbname+'(prd_cmpid,prd_bcid,prd_dpid,prd_cuid,prd_tag,prd_pcid,prd_createdpid,prd_createcuid,'+@fldlist+') '+
'values('+convert(varchar(20),@cmpid)+' , '+convert(varchar(20),@branchid)+ ','+
convert(varchar(20),@userdpid)+','+convert(varchar(20),@useruid)+
','+convert(varchar(20),@cc_tag)+','+convert(varchar(20),@ctid)+','+convert(varchar(20),@userdpid)+
','+convert(varchar(20),@useruid)+' '+
' , '+@vallist+')    '+char(13)+char(10)+
' select top 1 @neword2id=prd_id from ' +@tbname +' where prd_pcid='+CONVERT(varchar(10),@ctid)+
' and prd_cuid='+CONVERT(varchar(10),@useruid)+' and '+' prd_createcuid='+CONVERT(varchar(10),@useruid) +
' order  by prd_id desc '+CHAR(13)+CHAR(10)+
' update '+@tbname+' set prd_branch='''+@cc_branch+''',prd_depart='''+@cc_depart+''',prd_cuname='''+@cc_cuname+''','+
' prd_caption='''+@cc_caption+''',prd_type='''+@cc_type+''',prd_createdepart='''+@cc_depart+''','+
' prd_creator='''+@cc_cuname+''',prd_createtime=prd_updated  where prd_id=@neword2id ' +char(13)+char(10)+
' insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prdview'',@neword2id,@useruid,0  ' +char(13)+char(10)+
' insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prdedit'',@neword2id,@useruid,0  '+char(13)+char(10)+
' insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prddelete'',@neword2id,@useruid,0  '  
end else 
begin
set @sql = ' set @neword2id = 0 
select top 1 @neword2id=prd_id from '+@tbname+' where prd_pcid='+convert(varchar(20),@ctid)+' and '+@keycond +' 
set @neword2id = isnull(@neword2id , 0 ) 
if (@neword2id<=0) '+
'begin '+char(10)+char(10)+
'insert into '+@tbname+'(prd_cmpid,prd_bcid,prd_dpid,prd_cuid,prd_tag,prd_pcid,prd_createdpid,prd_createcuid,'+@fldlist+') '+
'values('+convert(varchar(20),@cmpid)+' , '+convert(varchar(20),@branchid)+ ','+
convert(varchar(20),@userdpid)+','+convert(varchar(20),@useruid)+
','+convert(varchar(20),@cc_tag)+','+convert(varchar(20),@ctid)+','+convert(varchar(20),@userdpid)+
','+convert(varchar(20),@useruid)+' '+
' , '+@vallist+')  '+char(13)+char(10)+
'select top 1 @neword2id=prd_id from ' +@tbname +' where prd_pcid='+CONVERT(varchar(10),@ctid)+
'and prd_cuid='+CONVERT(varchar(10),@useruid)+' and '+' prd_createcuid='+CONVERT(varchar(10),@useruid) +
'order  by prd_id desc '+CHAR(13)+CHAR(10)+
'insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prdview'',@neword2id,@useruid,0  ' +CHAR(13)+CHAR(10)+
'insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prdedit'',@neword2id,@useruid,0  ' +CHAR(13)+CHAR(10)+
'insert into '+@tbname+'_users(prdp_type,prdp_prdid,prdp_cuid,prdp_otid) select distinct ''prddelete'',@neword2id,@useruid,0  ' +CHAR(13)+CHAR(10)+
'end '+CHAR(13)+CHAR(10) 
if (@allowupdate=0) 
set @sql = @sql +CHAR(13)+CHAR(10)+ 
'update '+@tbname+' set prd_branch='''+@cc_branch+''',prd_depart='''+@cc_depart+''',prd_cuname='''+@cc_cuname+''','+
' prd_caption='''+@cc_caption+''',prd_type='''+@cc_type+''',prd_createdepart='''+@cc_depart+''','+
' prd_creator='''+@cc_cuname+''',prd_createtime=prd_updated where prd_id=@neword2id ' +char(13)+char(10) 
end
if (@sql>'') 
begin
set @neword2id = 0 
print @sql 
exec sp_executesql @sql, N'@neword2id bigint out,@useruid int', @neword2id out  , @useruid 
set @neword2id = isnull(@neword2id,0) 
if (@neword2id>0) 
begin
insert into #ord2(ord2id) values(@neword2id) 
end 
end
end

SET @sql=N'
declare @operation varchar(50)
declare @errorinfo nvarchar(4000)
set @operation =''continue'' 
set @errorinfo ='''' 

' 
if (dbo.mps_sys_checkkeywords('prd.beforecreate,prd.aftercreate',@ctid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13)+
'select a.* into #old from '+@tbname+' a,#ord2 b where a.prd_id=b.ord2id '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('prd.beforecreate,prd.aftercreate',@ctid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+
'select a.* ,null recid ,'''' sourcefield into #new from '+@tbname+' a,#ord2 b where a.prd_id=b.ord2id '+char(13) 
end 

if (dbo.mps_sys_checkkeywords('prd.beforecreate,prd.aftercreate',@ctid,0,'#new','insert,update')=0)
begin
set @sql = @sql+N'update #new set recid=prd_id '+CHAR(13) 
end

if (dbo.mps_sys_checkkeywords('prd.beforecreate',@ctid ,0,'','')=0) 
begin
-- set @sql = @sql +N'exec mps_customer_'+convert(varchar(10),ctid)+'_newdetail_before '
set @cc_branch=@cc_branch
    end 

set @sql = @sql +N'
if (@operation=''continue'')
begin
set @operation=@operation 
'
if (dbo.mps_sys_checkkeywords('prd.aftercreate',@ctid ,0,'','')=0) 
begin
/*
set @sql = @sql +N'exec mps_customer_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag)+
'_newdetail_after '+convert(varchar(10),@cmpid)+','+convert(varchar(10),0)+','+
convert(varchar(10),0)+','+convert(varchar(10),0)+','+convert(varchar(10),0)+
','+convert(varchar(10),0)+','+convert(varchar(10),@ordid)+' ,'+convert(varchar(10),@ord2tag)+','''+
@ord2group+''' ,'+convert(varchar(10),0)+' '+char(13) 
*/
set @cc_branch=@cc_branch
  end 
  
  set @sql = @sql +N'
end
'

if (dbo.mps_sys_checkkeywords('prd.beforecreate,prd.aftercreate',@ctid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('prd.beforecreate,prd.aftercreate',@ctid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13) 
end 

execute(@sql) 
drop table #ord2


end