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

--exec sys_Sql_genprochead @parseid,@procType,@procname,@heads output,@sql_text output,
-- @sql_params output,@varList output,@comVars output,@scripts output
create procedure [dbo].[sys_Sql_genprochead](
@parseid int,
@procType varchar(50),
@procname varchar(50),
@heads varchar(4000) output,
@sql_text varchar(4000) output,
@sql_params varchar(4000) output,
@varList varchar(4000) output,
@comVars varchar(4000) output,--通用变量，可能包含GLOBAL变量的定义，。。
@scripts varchar(4000) output
)
as
begin 
--                                             depart branch,company 工作流?, job
--通用变量全集： @cmpid,@useruid,@bizid,@ordid,@userdpid,@branchid, @cpid,   @wrkid,  @jbid,
-- @thisbiztype,@thisbizid,@thiskeyval,@action,@error
Declare @glparams varchar(2000),@glvar varchar(2000),@gl_sql_text varchar(2000),@gl_Sql_params varchar(2000),@gl_varList varchar(2000)
,@isNew int,@i int,@inout1 int,@outputStr varchar(10)

select @scripts='',@sql_text='',@sql_params='',@varList='',@comVars=''

--GLOBAL参数（变量）                 全局变量作为参数定义    全局变量作为局部变量定义
exec  sys_sql_genvar_global @parseid,@glparams output,      @glvar output,
@gl_sql_text output,@gl_sql_params output,@gl_varList output
 
-- 存储过程头
select @heads = 
' create procedure ' + @procname +' (' +char(13) 

--1 根据数据源建立表单
if (@procType='bizbysrc')  --
begin   
select @heads = @heads +
'    @cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) + 
'    ,@stype__ varchar(10) ' + char(13) +
'    ,@sbizid__ int ' + char(13) + 
'    ,@sordids__ varchar(max) ' + char(13) +
'    ,@sord2tag__ int ' + char(13) +
'    ,@sord2id__ bigint ' + char(13)+
'    ,@newordid_s__ varchar(2000) output ' + char(13) +  --新生成的表单ID
'    ,@jbid  bigint' + char(13)  --新生成的表单ID

,@sql_text=@sql_text + '{cmpid},{useruid},{stype__},{sbizid__},{sordids__},{sord2tag__},{sord2id__},{newordid_s__ output},{jbid}'
,@sql_params=@sql_params+'int|int|varchar(10)|int|varchar(max)|int|bigint|varchar(8000)|bigint'
,@varList=@varList+'@cmpid,@useruid,@stype,@sbizid,@sordids,@sord2tag,@sord2id,@newordid_s,@jbid ' 
--变量定义
exec sys_sql_addvar '@inIdentity__','bigint',0,0,@isNew output
exec sys_sql_addvar '@inOrdid__','bigint',0,0,@isNew output
exec sys_sql_addvar '@inOrd2id__','bigint',0,0,@isNew output
exec sys_sql_addvar '@sordid__','bigint',0,0,@isNew output 
exec sys_sql_addvar '@action__','varchar',40,0,@isNew output
exec sys_sql_addvar '@error__','varchar',4000,0,@isNew output  
exec sys_sql_addvar '@stagid__','int',0,0,@isNew output
exec sys_sql_addvar '@newordid__','bigint',0,0,@isNew output
exec sys_sql_addvar '@i__','int',0,0,@isNew output
exec sys_sql_addvar '@parseid__','int',0,0,@isNew output
 

select @scripts='  set @sordid__=0 ' + char(13) +
' if (charindex('','',@sordids__)=0 and @sordids__>'''')' +char(13) +
' begin '+char(13) +
'set @sordid__ = convert(bigint , @sordids__) ' + char(13)+
' end ' +char(13) +
' set @newordid_s__=''''' +char(13)
end

--3 客户定义存储过程
if (@procType='custfun')  --
begin 
--取客户定义参数
Declare @tmpID int,@glparseid int,@custparams varchar(200),@cfunid int
select @tmpid = tmpid,@glparseid=parseid
from sys_sql_inf 
where parseid=@parseid

select @custparams=a.custParams,@cfunid=cfunid
from sys_sql_custFun a,sys_sql_inf b
where b.glparseid=@glparseid and a.parseid=b.parseid

select @heads = @heads +
'    @cmpid int ' + char(13) + --公司ID
'    ,@branchid int ' + char(13) +  --分公司ID
'    ,@userdpid int ' + char(13) + --部门ID
'    ,@useruid int ' + char(13) + -- 用户ID
'    ,@userotid int ' + char(13) +  --岗位ID
'    ,@usersecurity int ' + char(13)  -- 密级
,@sql_text=@sql_text + '{cmpid},{branchid},{userdpid},{useruid},{userotid},{usersecurity}'
,@sql_params=@sql_params+'int|int|int|int|int|int'
,@varList=@varList+'@cmpid,@branchid,@userdpid,@useruid,@userotid,@usersecurity '

declare params Cursor For
select paramname,typelen
from sys_sql_params
where @cfunid=cfunid 
order by sn
Open params
declare @paramname varchar(50),@ptype varchar(20)
Fetch next From params Into @paramname,@ptype
While (@@Fetch_status=0)
Begin
if (@paramname is not null and @ptype is not null)
select @heads = @heads + '    ,' +@paramname +' ' + @ptype 
,@sql_text=@sql_text + ',{' + substring(@paramname,2,50) +'}'
,@sql_params=@sql_params+'|' + @ptype
,@varList=@varList+',' +@paramname 

Fetch next From params Into @paramname,@ptype
End
Close params
Deallocate params

exec sys_sql_addvar '@jbid','bigint',0,0,@isNew output

select @scripts= @scripts + 
' set nocount on ' + char(13) +
' set @jbid=0 ' + char(13)

/*
--把传入参数作为资源保存在表中
select @scripts= @scripts +
'  declare @source table(objid bigint) ' + char(13)+
'  select @curInd=1,@strlen=LEN(@sobjids) ' + char(13)+
'  while (@curInd<=@strlen) ' + char(13)+
'  begin ' + char(13)+
'  select @i=charindex('','',@sobjids,@curInd) ' + char(13)+
'  if (@i>0) ' + char(13)+
'  begin ' + char(13)+
'  select @ordid_s=SUBSTRING(@sobjids,@curInd,@i-@curInd) ' + char(13)+
'  insert into @source(objid) values(convert(bigint,@ordid_s)) ' + char(13)+
'  set @curInd=@i+1 ' + char(13)+
'  end ' + char(13)+
'  else if (@curInd<=@strlen) ' + char(13)+
'  begin ' + char(13)+
'  select @ordid=SUBSTRING(@s,@curInd,@strlen-@curInd+1) ' + char(13)+
'  insert into @source(objid) values(convert(bigint,@ordid_s))' + char(13)+
'  break ' + char(13)+
'  end ' + char(13)+
'  end ' + char(13) +
'  -- 设计人员代码 ' + char(13)
*/
end


--4 客户定义事件存储过程 
if (@procType='event')  --
begin 
--取客户定义参数
--Declare @tmpID int,@glparseid int,@custparams varchar(200),@cfunid int
select @tmpid = tmpid,@glparseid=parseid
from sys_sql_inf 
where parseid=@parseid

select @custparams=a.custParams,@cfunid=cfunid
from sys_sql_custFun a,sys_sql_inf b
where b.glparseid=@glparseid and a.parseid=b.parseid


declare params Cursor For
select paramname,typelen,inout1
from sys_sql_params
where @cfunid=cfunid 
order by sn
Open params
--declare @paramname varchar(50),@ptype varchar(20)
set @i = 0
Fetch next From params Into @paramname,@ptype,@inout1
While (@@Fetch_status=0)
Begin
if (@inout1 = 0)
set @outputStr=' output'
else
set @outputStr=''

if (@paramname is not null and @ptype is not null)
begin
select @heads = @heads + '    '
if (@i>0)
select @heads = @heads + ','
,@sql_text=@sql_text + ','
,@sql_params=@sql_params+'|' 
,@varList=@varList+',' 

select @heads = @heads  +@paramname +' ' + @ptype + @outputStr 
,@sql_text=@sql_text + '{' + substring(@paramname,2,50) + @outputStr + '}'
,@sql_params=@sql_params + @ptype
,@varList=@varList +@paramname 

select @i = @i + 1
end
Fetch next From params Into @paramname,@ptype,@inout1
End
Close params
Deallocate params

select @scripts= '  select @objid__=@ordid ' + char(13)
end


if (@procType='link_afterexecute')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@wsid int ' + char(13)
,@sql_text=@sql_text + '{@recallTimes},{cmpid},{useruid},{bizid},{ordid},{wrkid},{jbid},{wsid}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|int'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wsid' 
,@comVars='@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int,@action varchar(40),@error varchar(4000)'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='job_afterback')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@action varchar(50) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid},{operation}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|varchar(50)'
,@varList=@varList+'recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@operation'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int,@error varchar(4000)'
if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='wrk_start')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@action varchar(50) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{operation}'
,@sql_params=@sql_params+'int|int|int|int|int|int|varchar(50)'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@operation'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int,@error varchar(4000)'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='wrk_beforenew')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@action varchar(50) output ' + char(13) +
'    ,@error varchar(4000) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid},{operation},{errorinfo}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|varchar(50)|varchar(4000)'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@operation,@errorinfo'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='before_execute')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@wsstatus varchar(50) output ' + char(13) +
'    ,@action varchar(50) output ' + char(13) +
'    ,@error varchar(4000) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid},{wrkid},{wsstatus},{operation},{errorinfo}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|int|varchar(50)|varchar(50)|varchar(4000)'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wrkid,@wsstatus,@operation,@errorinfo'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='wrk_after_execute')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@wsid int ' + char(13) +
'    ,@wsstatus varchar(50) output ' + char(13) +
'    ,@action varchar(50) output ' + char(13) +
'    ,@error varchar(4000) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid},{wrkid},{wsstatus},{operation},{errorinfo}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|int|varchar(50)|varchar(50)|varchar(4000)'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wrkid,@wsstatus,@operation,@errorinfo'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='exec_wrkstatus_contidtion')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) +
'    ,@wsid int ' + char(13) +
'    ,@action varchar(50) output ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid},{wrkid},{operation}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint|int|varchar(50)'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid,@wrkid,@operation'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int,@error varchar(4000)'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end

if (@procType='wrk_after_new')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@wrkid int ' + char(13) +
'    ,@jbid bigint ' + char(13) 
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{wrkid},{jbid}'
,@sql_params=@sql_params+'int|int|int|int|int|int|bigint'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@wrkid,@jbid'
,@comVars='@recallTimes int,@userdpid int,@branchid int,@cpid int, @biztype varchar(20),@thisbiztype varchar(20),' +
'@thisbizid int,@thiskeyval int,@action varchar(50),@error varchar(4000)'

if (len(@comvars)>2)
select @comvars = @comvars + @glvar
end
 
if (@procType='java')
begin
select @heads = @heads +
'    @recallTimes int ' + char(13) +
'    ,@cmpid int ' + char(13) +
'    ,@useruid int ' + char(13) +
'    ,@bizid int ' + char(13) +
'    ,@ordid int ' + char(13) +
'    ,@branchid int' + char(13) +  --分公司ID
'    ,@userdpid int' + char(13) +   --部门ID
'    ,@jbid int' + char(13) +--job id
'    ,@thisbiztype varchar(20) ' + char(13) +
'    ,@thisbizid int'+ char(13) +
'    ,@thisval int'  +char(13) --还有web参数
,@sql_text=@sql_text + '{recallTimes},{cmpid},{userid},{bizid},{ordid},{bcid},{dpid},{jbid},{thisbiztype},{thisbizid},{thisval}'
,@sql_params=@sql_params+'int|int|int|int|int|int|int|int|varchar(20)|int|int'
,@varList=@varList+'@recallTimes,@cmpid,@useruid,@bizid,@ordid,@branchid,@userdpid,@jbid,@thisbiztype,@thisbizid,@thisval'
,@comVars='@action varchar(50),@error varchar(4000)'

if (len(@glparams)>2)
select @heads = @heads +',' + @glparams --全局变量参数
,@sql_text=@sql_text + ',' + @gl_sql_text 
,@sql_params=@sql_params+'|' + @gl_sql_params
,@varList=@varList+',' +@gl_varList

end

if (@procType='text')
begin
select @heads = @heads + '@i int'

end


end