IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizimport_web]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizimport_web]
GO
create procedure [dbo].[mps_gen_bizimport_web](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail nvarchar(100) 
declare @ds1 varchar(100)
declare @dss varchar(MAX)  
declare @detailold nvarchar(100) 
declare @order2 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @ord2tag2 int 
declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 
declare @ss1 nvarchar(MAX) 
declare @cntname varchar(1000) 

declare @tbext varchar(1000) 
set @tbext = 'o_'+convert(varchar(10),@bizid)+'_imp_'

declare @web varchar(1000) 
select top 1 @web=biz_auto_webservice from mps_biztype with(nolock) where biz_id=@bizid 
set @web = isnull(@web,'')
if (@web='') 
begin
return 
end
set @web = @web+'_biz_'+convert(varchar(10),@bizid)
if (dbo.mps_sys_checkkeywords('crm.import',@bizid,0,'','')<>0) 
begin
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@web+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 
set @ss='drop procedure '+@web
execute(@ss) 
delete mps_sql where sql_cmpid=@cmpid and sql_name=@web 
return 
end

set @sql = dbo.mps_sys_getusercode('crm.import',@bizid,0)
set @sql = ltrim(@sql) 
 
set @i = 0 
 
set @ss = @sql 

BEGIN
set @ss1 = @ss 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_master'
set @sql = '' 
set @sql = @sql+' 
 set @cmpid= '+convert(varchar(10),@cmpid)+'
     declare @bizid int ='+convert(varchar(10),@bizid)+'
     declare @operation varchar(50) ='''' 
     declare @errorinfo nvarchar(4000) ='''' 
     declare @genordnoauto int=1 
     declare @autoapplyjob varchar(500)=''''
     
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @newordid bigint  
 declare @i int 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 set @operation = ''continue''
 set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 '
 -- 对@ss 进行一些函数替换
 set @ss = replace(@ss,'deletetask()','delete mps_task_kernel where tsk_id=@taskid ') 
 set @ss = replace(@ss,'genordno(','set @genordnoauto=dbo.genordno(') 
 set @ss = replace(@ss,'applycurrentflow(','set @autoapplyjob=dbo.applycurrentflow(') 
 set @ss = replace(@ss,'applycurrentflow()','applycurrentflow(0,0,0)') 
 if (charindex('#master',@ss1)>0) 
 begin
 set @ss=REPLACE(@ss,'#master',@tbext) 
 set @ds1 = @dbname+@tbname 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select *   into '+@tbext+' from '+@dbname+@tbname+N' where ord_id=0 '+char(13)+char(10)
exec mps_gen_default_code @ds1 , @tbext , 'ord_id' , @dss output 
 
set @sql = @sql+@dss+char(13)+char(10) 
 
 end
 if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_task'
 set @ss=REPLACE(@ss,'#task',@tbext) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'create table '+@tbext+'task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000),jbid bigint) '
 end
 if (charindex('#account',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_account'
 set @ss=REPLACE(@ss,'#account',@tbext) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'create table '+@tbext+'(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,available int,actual int) '+char(13)+char(10) 
 end
 
 
 -- 以下代码是生成子表临时表
 
 declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c11

FETCH NEXT FROM c11 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 

if (charindex(@detail,@ss1)>0) 
begin
set @ss=REPLACE(@ss,@detail,@tbext) 
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10)+
'select * into '+@tbext+' from '+@order2+N' where ord2_ordid=0'+char(13)+char(10) 
 
 set @ds1 = @order2
 set @dss = ''
 exec mps_gen_default_code @order2 , @tbext , 'ord2_id' , @dss output 
 set @sql = @sql+@dss+char(13)+char(10) 
 end
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 

set @sql=@sql+N'
-----  用户自定义代码开始
 '+@ss+'
 -----  用户自定义代码结束'+char(13)+char(10) 
 
 -- 如果是最后一条本SQL  就将执行提交操作
   
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_master'
 set @sql=@sql+N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate '+CHAR(13)+CHAR(10)
 if (charindex('#master',@ss1)>0) 
 begin
 set @sql=@sql+N'
 -- 修正主表数据
 if (@useruid=0) 
 begin
 select top  1 @useruid =bp_cuid from mps_biztype_users with(nolock) where bp_type=''biznew'' and bp_bizid='+convert(varchar(10),@bizid)+' 
 end 
 set @useruid = isnull(@useruid,0) 
 if (@useruid=0) 
 begin
 select top  1 @useruid =cu_id from mps_login with(nolock) where cu_cmpid='+convert(varchar(10),@cmpid)+' 
 end 
 declare @mic bigint
select @mic=max(ord_id) from '+@dbname+@tbname+' with(nolock) 

 update '+@tbext+' set ord_cmpid='+CONVERT(varchar(10),@cmpid)+',ord_bizid='+CONVERT(varchar(10),@bizid)+',
 ord_date=case when ord_date<''1920-01-01'' then getdate() else ord_date end ,
 ord_updated=case when ord_updated<''1920-01-01'' then getdate() else ord_updated end ,
 ord_finished=case when ord_finished=0 then  1 else ord_finished end, ord_style=biz_formstyle,ord_type=biz_title,
 ord_cuid=case when ord_cuid>0 then ord_cuid else cu_id end,ord_createcuid=case when ord_createcuid>0 then ord_createcuid else cu_id end  
 from mps_biztype with(nolock) , mps_login with(nolock) where biz_id='+convert(varchar(10),@bizid)+' and cu_id=@useruid 
 update '+@tbext+' set ord_createdepart=cu_depart , ord_bcid=case when ord_bcid>0 then ord_bcid else cu_bcid end, ord_branch=case when ord_bcid>0 then ord_branch else cu_branch end ,
 ord_createdpid=cu_dpid,ord_creator=cu_callname from mps_login with(nolock) where cu_id=ord_createcuid 
 update '+@tbext+' set ord_depart=cu_depart ,  
 ord_dpid=cu_dpid,ord_empname=cu_callname from mps_login with(nolock) where cu_id=ord_createcuid '+CHAR(10)+CHAR(10) 
 
 
 set @ds1=@dbname+@tbname  
exec mps_gen_insert_code @ds1,'','ord_id,ord_fjbid',@dss output 

set @sql = @sql+char(13)+char(10)+
' insert into '+@ds1+'(ord_fjbid,'+@dss+') '+char(13)+char(10) 
exec mps_gen_insert_code @ds1,'new','ord_id,ord_fjbid',@dss output 
set @sql = @sql+char(13)+char(10)+
' select  new.ord_id * (-1), '+@dss+'  from '+@tbext+' new '+char(13)+char(10) +'
declare @nic int 
select @nic=count(1) from '+@tbext +'

  declare @ords table(ordid bigint,index1 int,cuid int) 
  insert into @ords(ordid,index1,cuid)
select top(@nic) ord_id , ord_fjbid * (-1) ,ord_createcuid from '+@ds1+' with(nolock) where ord_id>@mic and ord_fjbid<0 and ord_fjbid>=(-1)*@nic 
update '+@ds1+' set ord_fjbid=0 from @ords where ord_id=ordid '+char(10)+char(13)
end 
--- 处理明细表
declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c11

FETCH NEXT FROM c11 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (charindex(@detail,@ss1)>0) 
begin
-- 清理一下明细数据
set @sql = @sql+char(13)+char(10)+
'update '+@tbext+' set ord2_ordid=case when ord2_ordid=0 then  1 else ord2_ordid end , ord2_date=case when ord2_date<''1920-01-01'' then getdate() else ord2_date end,ord2_updated=case when ord2_updated<''1920-01-01'' then getdate() else ord2_updated end  '+char(13)+char(10) 
exec mps_gen_insert_code @order2,'','ord2_id,ord2_tag,ord2_group,ord2_ordid',@dss output 
 set @sql = @sql+char(13)+char(10)+
'insert into  '+@order2+'(ord2_tag,ord2_group,ord2_ordid,'+@dss+') '+char(13)+char(10) 
exec mps_gen_insert_code @order2,'new','ord2_id,ord2_tag,ord2_group,ord2_ordid',@dss output 
set @sql = @sql +'
select '+convert(varchar(10),@ord2tag2)+',biz2_caption,ordid,'+@dss+'
from mps_biztype2 with(nolock) , @ords , '+@tbext+' new 
where biz2_bizid='+convert(varchar(10),@bizid)+' and biz2_tag='+convert(varchar(10),@ord2tag2)+'
and new.ord2_ordid=index1  '+char(13)+char(10) 
 end
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 


set @sql = @sql +N'
end 
--- 清除临时表 
'

 
if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_task'
 set @sql = @sql+char(13)+char(10)+
 ' delete '+@tbext+' where cuid is null or info is null or objid is null  or ctgid is null '+
 ' insert into mps_task(tsk_cmpid,tsk_bcid,tsk_branch,tsk_dpid,tsk_depart,tsk_cuid,tsk_cuname,tsk_date,tsk_type,tsk_objtype,tsk_ctgid,tsk_objid,tsk_objid2,tsk_message,tsk_jbid) '+char(13)+char(10)+
 ' select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),'+char(13)+char(10)+
 'case when infotype is null then ''普通任务'' else infotype end , '+char(13)+char(10)+
 'case when objtype is null or objtype='''' then ''biz'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then '+convert(varchar(10),@bizid)+' else ctgid end , '+char(13)+char(10)+
 'case when objid  is null or objid=0 then @newordid else objid end , '+char(13)+char(10)+
 'case when objid2  is null then 0 else objid2 end , '+char(13)+char(10)+
 'case when info  is null then '''' else info end , '+char(13)+char(10)+
 'case when jbid  is null or jbid=0 then 0 else jbid end   '+char(13)+char(10)+
 'from '+@tbext+' , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table '''+@tbext+''' '+char(13)+char(10) 
 end
 
if (charindex('#master',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_'+convert(varchar(10),@bizid)+'_imp_master''' + char(13)+char(10) 
end 
 
 declare c11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c11
FETCH NEXT FROM c11 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @tbext = '#o_'+convert(varchar(10),@bizid)+'_'+convert(varchar(10),@ord2tag2)+'_imp_detail'
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (charindex(@detail,@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
end
 FETCH NEXT FROM c11 INTO @ord2tag2 
END
CLOSE c11
DEALLOCATE c11 
if (charindex('#account',@ss1)>0) 
 begin
 set @tbext = '#o_'+convert(varchar(10),@bizid)+'_imp_account'
 set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
 end
 
end
end  
  
   
 if (charindex('#master',@ss1)>0)  
 begin
 set @ss = N'
 insert into '+@dbname+@tbname+'_users(ordp_type,ordp_ordid,ordp_cuid,ordp_otid) 
  select distinct ''bizview'',ordid,cuid,-1 from @ords  
  declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct ordid,cuid, ord_createdpid,ord_bcid,ord_type,ord_title,ord_no,ord_date,ord_creator,
ord_ftype , ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id ,
ord_rtype , ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id 
from @ords,'+@dbname+@tbname+' with(nolock)  where ord_id=ordid order by ordid 
open c1
declare @ordid bigint 
declare @bizcode nvarchar(200) 
select @bizcode=biz_code from mps_biztype with(nolock) where biz_id=@bizid 
declare @ordno nvarchar(50)

declare @ordbcid int,@ordtype nvarchar(100),@ordtitle nvarchar(100),@orddate datetime,@ordcreator nvarchar(50), 
          @ftype varchar(30), @fbizid int, @fordid bigint, @ford2tag int, @ford2id int, 
          @rtype varchar(30), @rbizid int, @rordid bigint, @rord2tag int, @rord2id int, @wrkid int
  
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) ,mps_bizflow   with(nolock) 
  where flw_bizid=@bizid and flw_title=''缺省流程'' and wrk_bizid=@bizid and wrk_flwid=flw_id and wrk_shape like ''begin%''  
  set @wrkid=isnull(@wrkid,0)   
  if (@wrkid=0)   
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) ,mps_bizflow    with(nolock) 
  where flw_bizid=@bizid and flw_title=''缺省流程'' and wrk_bizid=@bizid and wrk_flwid=flw_id order by wrk_id   
if(@wrkid = 0) 
begin 
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) , mps_bizflow  with(nolock) 
  where flw_bizid = @bizid and flw_title = '''' and wrk_bizid = @bizid and wrk_flwid = flw_id and wrk_shape like ''begin%'' 
  set @wrkid = isnull(@wrkid, 0) 
end 
if (@wrkid = 0) 
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) , mps_bizflow  with(nolock) 
  where flw_bizid = @bizid and flw_title = '''' and wrk_bizid = @bizid and wrk_flwid = flw_id order by wrk_id 
if (@wrkid = 0) 
begin 
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) , mps_bizflow  with(nolock) 
  where flw_bizid = @bizid and   wrk_bizid = @bizid and wrk_flwid = flw_id and wrk_shape like ''begin%'' 
  set @wrkid = isnull(@wrkid, 0) 
end 
if (@wrkid = 0) 
  select top 1 @wrkid = wrk_id from mps_flowwork with(nolock) , mps_bizflow with(nolock)  
  where flw_bizid = @bizid   and wrk_bizid = @bizid and wrk_flwid = flw_id order by wrk_id 
                 
 

FETCH NEXT FROM c1 INTO @ordid ,@useruid ,@userdpid,@ordbcid ,@ordtype ,@ordtitle ,@ordno,@orddate ,@ordcreator, 
          @ftype  , @fbizid , @fordid , @ford2tag , @ford2id, @rtype , @rbizid , @rordid , @rord2tag , @rord2id
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (@genordnoauto=0) 
begin
exec mps_bm_getorset @cmpid,''biz'',@bizid,@ordid,@useruid,@bizcode,2,@ordno output,1   
  update '+@dbname+@tbname+'  with(rowlock) set ord_no = @ordno where ord_id = @ordid  
  end 
  -- 生成job '+char(13)+char(10)
if (dbo.mps_sys_checkkeywords('crm.aftercreate',@bizid,0,'','')=0) 
begin
  set @ss = @ss +'
  exec order_'+convert(varchar(10),@bizid)+'_newbiz_after  @cmpid,@ordbcid,@userdpid,@useruid,0,0,@ordid,'''',0,0,0,'''' ,'''','''','''' 
  '
end 
  set @ss = @ss +'
  exec mps_new_work @cmpid ,@useruid,@bizid  ,@ordbcid, @ordid ,@ordtype ,@ordno,@ordtitle,@orddate,@ordcreator, '''', 
               @ftype, @fbizid, @fordid, @ford2tag, @ford2id, @rtype, @rbizid, @rordid, @rord2tag, @rord2id, @wrkid, 0 , 0 
if (@autoapplyjob!='''') 
begin
    exec mps_do_execute_job_auto @cmpid ,@ordbcid ,@userdpid ,@useruid,0 , @bizid ,@ordid ,@wrkid  
end 
     
FETCH NEXT FROM c1 INTO @ordid ,@useruid ,@userdpid,@ordbcid ,@ordtype ,@ordtitle ,@ordno,@orddate ,@ordcreator, 
          @ftype  , @fbizid , @fordid , @ford2tag , @ford2id, @rtype , @rbizid , @rordid , @rord2tag , @rord2id
end  
CLOSE c1
DEALLOCATE c1 
'
  end 

 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@web+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) set @ss1='alter ' else set @ss1='create '
set @ss1 = @ss1+' procedure '+@web+char(13)+
'@cmpid int,'+ char(13)+
'@branchid int,'+ char(13)+
'@userdpid int,'+ char(13)+
'@useruid int ,'+ char(13)+
'@userotid int ,'+ char(13)+
'@taskid bigint ,'+ char(13)+
'@jbid bigint ,'+ char(13)+
'@ctgid int ,'+ char(13)+
'@ctgid2 int ,'+ char(13)+
'@objid bigint ,'+ char(13)+
'@objid2 bigint ,'+ char(13)+
'@xml nvarchar(max),'+ char(13)+
'@md5 varchar(32)'+ char(13)+
'as '+ char(13)+
'begin'+char(13) +char(9)+
'set nocount on' +char(13)+char(13) +char(9)+ @sql+char(13)+char(13) +@ss+char(13)+char(10)+char(9)+
'set nocount off'+char(13)+
'end' 

set @cntname=@web
set @web = @cntname+' {cmpid},{branchid},{userdpid},{useruid},{userotid},{taskid},{jbid},{ctgid},{ctgid2},{objid},{objid2},{xml},{md5}'
set @tbext = 'int|int|int|int|int|bigint|bigint|int|int|bigint|bigint|nvarchar(max)|varchar(32)'

if (not exists(select * from mps_sql where sql_cmpid=@cmpid and sql_name=@cntname))
begin
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_note,sql_sourcetables,sql_targettables,sql_params,sql_keyparam)
values(@cmpid,@cntname,'MSSQL',@web,'','','',@tbext,'')
end else 
begin
update mps_sql set 
sql_text=@web,
sql_sourcetables='',
sql_targettables='',
sql_params=@tbext,
sql_keyparam='' 
where sql_cmpid=@cmpid and sql_name=@cntname
end

 
exec myprint @ss1
execute(@ss1)
set nocount off 
end