IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_shopimport_web]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_shopimport_web]
GO
create procedure [dbo].[mps_gen_shopimport_web](
@cmpid int
)
as
begin
set nocount on
declare @autotype varchar(20) = 'shop' 
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)='mps_depart'
declare @dbname varchar(50)=''
declare @fkey   varchar(50)='dp_id'  
declare @ord2tag2 int 
declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 
declare @ss1 nvarchar(MAX) 
declare @cntname varchar(1000) 

declare @web varchar(1000) 
declare @interval int 
declare @unit varchar(10) 
declare @time datetime  

select top 1 @web=auto_webservice , @sql=auto_sql , @interval=auto_interval ,  @unit=auto_unit , @time=auto_starttime from mps_auto_import with(nolock) where auto_cmpid=@cmpid and auto_type=@autotype 
set @web = isnull(@web,'')
set @sql = isnull(@sql , '' ) 
set @sql = ltrim(@sql) 

set @web = isnull(@web,'')
if (@web='')
begin
return 
end 

-- 如果有WEB，则删除SQL任务
delete mps_task_java where tsk_cmpid=@cmpid and tsk_objtype='shop' 

set @web = @web+'_shop'   
if (len(@sql)<5) 
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

declare @tbext varchar(1000) 


set @i = 0 
 
set @ss = @sql 


BEGIN
set @ss1 = @ss 

set @sql = '' 
set @sql = @sql+' set @cmpid  ='+convert(varchar(10),@cmpid)+'
     declare @operation varchar(50) ='''' 
     declare @errorinfo nvarchar(4000) ='''' 
     
     
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 @tbext = '#o_auto_master'
 
 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 dp_id=0 '+char(13)+char(10)
exec mps_gen_default_code @ds1 , @tbext , 'dp_id' , @dss output 
set @sql = @sql+@dss+char(13)+char(10) 
 end
 if (charindex('#romaster',@ss1)>0) 
 begin
 set @tbext = @tbext+'_old'  ;
 set @ss=REPLACE(@ss,'#romaster',@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 dp_cmpid= '+convert(varchar(10),@cmpid)+' and dp_isdepart=1 '+char(13)+char(10)
 end
 if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_auto_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_auto_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
 
 
set @sql=@sql+N'
 '+@ss+char(13)+char(10) 
 
 begin
 set @tbext = '#o_auto_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'
 -- 修正主表数据
 update '+@tbext+' set dp_isdepart=1,dp_cmpid='+CONVERT(varchar(10),@cmpid)+ CHAR(10)+CHAR(10) 
 
 -- 需要更新主表
 set @ds1=@dbname+@tbname  
exec mps_gen_insert_code @ds1,'','dp_id',@dss output 

if (charindex('#master',@ss1)>0 and charindex('insert',@ss1)>0 and charindex('#master',@ss1)>charindex('insert',@ss1)) 
set @sql = @sql+char(13)+char(10)+
' insert into '+@ds1+'('+@dss+') '+char(13)+char(10) 
exec mps_gen_insert_code @ds1,'new','dp_id',@dss output 
set @sql = @sql+char(13)+char(10)+
' select   '+@dss+'  from '+@tbext+' new '+char(13)+char(10)
end 

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

 
if (charindex('#task',@ss1)>0) 
 begin
 set @tbext = '#o_auto_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 ''shp'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then 0 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_auto_master''' + char(13)+char(10) 
end 
if (charindex('#romaster',@ss1)>0) 
begin
set @sql =@sql+ 'exec drop_temp_table ''#o_auto_master_old''' + char(13)+char(10) 
end 
 
  
if (charindex('#account',@ss1)>0) 
 begin
 set @tbext = '#o_auto_account'
 set @sql =@sql+ 'exec drop_temp_table '''+@tbext+'''' + char(13)+char(10) 
 end
 
end

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)

exec mps_create_task_java_item_type @cmpid,'shop'

set nocount off 
end