IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_import_biz_details]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_import_biz_details]
GO
 
CREATE procedure [dbo].[mps_import_biz_details](
@cmpid int,
@branchid int, 
@userdpid int, 
@userotid int, 
@useruid int, 
@bizid int,
@ordid bigint,
@ord2tag int,
@ord2group nvarchar(50) ,
@keyflds varchar(500),
@dataflds varchar(1000) ,
@datas nvarchar(MAX) 
) as
begin

declare @dbname varchar(30) 
declare @tbname varchar(50) 
declare @pkname varchar(50) 

exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,@ord2tag,@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 
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+'(ord2_ordid,ord2_tag,ord2_group,'+@fldlist+') '+
'values('+convert(varchar(20),@ordid)+' , '+convert(varchar(10),@ord2tag)+
' , '''+@ord2group+''' , '+@vallist+')    set @neword2id=@@Identity  ' 
end else 
begin
set @sql = 'if (not exists (select 1 from '+@tbname+' where ord2_ordid='+convert(varchar(20),@ordid)+
' and ord2_tag='+convert(varchar(10),@ord2tag)+' and ord2_group='''+@ord2group+''' and '+@keycond+
')) '+
'insert into '+@tbname+'(ord2_ordid,ord2_tag,ord2_group,'+@fldlist+') '+
'values('+convert(varchar(20),@ordid)+' , '+convert(varchar(10),@ord2tag)+
' , '''+@ord2group+''' , '+@vallist+')     set @neword2id=@@Identity  ' 
end
if (@sql>'') 
begin
set @neword2id = 0 
print @sql 
exec sp_executesql @sql, N'@neword2id bigint out', @neword2id out  
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('crm.beforeneword2,crm.afterneword2',@bizid ,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.ord2_id=b.ord2id '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+
'select a.* ,null recid into #new from '+@tbname+' a,#ord2 b where a.ord2_id=b.ord2id '+char(13) 
end 

if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#account','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) 
end 


if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid,0,'#new','insert,update')=0)
begin
set @sql = @sql+N'update #new set recid=ord2_id '+CHAR(13) 
end

if (dbo.mps_sys_checkkeywords('crm.beforeneword2',@bizid ,0,'','')=0) 
begin
set @sql = @sql +N'exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag)+
'_newdetail_before '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@branchid)+','+
convert(varchar(10),@userdpid)+','+convert(varchar(10),@useruid)+','+convert(varchar(10),@userotid)+
','+convert(varchar(10),0)+','+convert(varchar(10),@ordid)+' ,'+convert(varchar(10),@ord2tag)+','''+
@ord2group+''' ,'+convert(varchar(10),0)+',@operation output,@errorinfo output  '+char(13) 
  end 

set @sql = @sql +N'
if (@operation=''continue'')
begin
set @operation=@operation 
'
if (dbo.mps_sys_checkkeywords('crm.afterneword2',@bizid ,0,'','')=0) 
begin
set @sql = @sql +N'exec order_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag)+
'_newdetail_after '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@branchid)+','+
convert(varchar(10),@userdpid)+','+convert(varchar(10),@useruid)+','+convert(varchar(10),@userotid)+
','+convert(varchar(10),0)+','+convert(varchar(10),@ordid)+' ,'+convert(varchar(10),@ord2tag)+','''+
@ord2group+''' ,'+convert(varchar(10),0)+' '+char(13) 
  end 
  
  set @sql = @sql +N'
end else 
begin
delete '+@tbname+' from #ord2 where ord2_id=ord2id 
end 
'

if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#account','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#account'' '+char(13) 
end 
execute(@sql) 


end