IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizfull]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizfull]
GO
create procedure [dbo].[mps_gen_bizfull](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @tbname1 varchar(50)
declare @dbname1 varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_full')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_full(
@cmpid int,  
    @ordid bigint, 
    @useruid int, 
    @jbid bigint ) 
as  
begin  
set nocount on 
-- 首先取模板  t1 t2 
declare @tmpid int 
declare @ordfinished int 
declare @ordbizid int 
declare @ord_ftype varchar(20),@ord_fbizid int,@ord_ford2tag int, @ord_ford2id bigint,@ord_fordid bigint 
declare @ord_rtype varchar(20),@ord_rbizid int,@ord_rord2tag int, @ord_rord2id bigint,@ord_rordid bigint 
declare @fordid bigint  
  ' 
  declare @ord2tag int  
  declare @params varchar(4000) 
  set @params = '@ord_fordid,@ord_rord2id,@ord_fbizid,@ord_rbizid,@ord_ford2tag,@ord_rord2tag,@ord_ford2id,@ord_rord2id,' 
  declare @newparam1 varchar(4000) 
  set @newparam1 = '' 
  declare @newparam2 varchar(4000) 
  set @newparam2 = '' 
  
  declare @bizs_source nvarchar(100) , @bizs_sourcefield varchar(50), @bizs_sourcetype varchar(30), @bizs_ctgid int, @bizs_ctg2id int 
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bizs_source , bizs_sourcefield , bizs_sourcetype, bizs_ctgid, bizs_ctg2id  from mps_biztype_source  
where bizs_type='biz' and bizs_bizid=@bizid  and bizs_ord2tag=0 and (bizs_source<>'上级' and bizs_source<>'上上级' and bizs_source<>'根级')
open c1
 
FETCH NEXT FROM c1 INTO @bizs_source , @bizs_sourcefield , @bizs_sourcetype, @bizs_ctgid, @bizs_ctg2id 
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (charindex('@'+@bizs_sourcefield+',',@params)<=0 and dbo.checkfield(@cmpid,@tbname,@bizs_sourcefield)=0) 
begin
if (@newparam1>'') set @newparam1=@newparam1+',' 
set @newparam1=@newparam1+'@'+@bizs_sourcefield+' bigint '  

if (@newparam2>'') set @newparam2=@newparam2+',' 
set @newparam2=@newparam2+'@'+@bizs_sourcefield+'='+@bizs_sourcefield   

set @params=@params+'@'+@bizs_sourcefield +',' 
end 
 FETCH NEXT FROM c1 INTO @bizs_source , @bizs_sourcefield , @bizs_sourcetype, @bizs_ctgid, @bizs_ctg2id 
END
CLOSE c1
DEALLOCATE c1 

if (@newparam1>'') set @newparam1='declare '+@newparam1 
if (@newparam2>'') set @newparam2=', '+@newparam2 

set @sql = @sql+char(13)+char(10)+@newparam1+char(13)+char(10) 
set @sql = @sql+N'
select  @fordid = ord_fordid, @ordbizid = ord_bizid, @tmpid = ord_templateid, @ordfinished = ord_finished  ,
  @ord_ftype=ord_ftype,@ord_fbizid=ord_fbizid,@ord_ford2tag=ord_ford2tag, @ord_ford2id=ord_ford2id,@ord_fordid =ord_fordid ,
  @ord_rtype=ord_rtype,@ord_rbizid=ord_rbizid,@ord_rord2tag=ord_rord2tag, @ord_rord2id=ord_rord2id,@ord_rordid =ord_rordid '+@newparam2 +N'
from '+@dbname+@tbname+N' where ord_id = @ordid and ord_cmpid = @cmpid 
declare @jbtmpid int   --保存工作步骤的模板 
declare @jbwsid int 
declare @jbwrkid int 
if (@ordfinished <= 1)  --业务还没有完 
begin 
select @jbwrkid=jb_wrkid,@jbtmpid = jb_templateid, @jbwsid = jb_wsid  from  mps_job where jb_id = @jbid and jb_cmpid = @cmpid 
end 
set @jbtmpid = isnull(@jbtmpid, 0) 
set @jbwsid = isnull(@jbwsid, -1) 
set @jbwrkid = isnull(@jbwrkid, 0) 
if (@jbtmpid>0) --步骤中有模板 
begin 
if (@tmpid = 0)  --没有子模板，则直接取步骤中的模板 
begin 
select * from mps_template where tmp_id = @jbtmpid  
select 0 none where 0 = 1  --没有子模板 
end else 
begin 
-- 先取主模板，再取子模板 
select  * from mps_template where tmp_id = @jbtmpid 
select  * from mps_template where tmp_id = @tmpid 
--select  0  as ord2id_page  
--select  * from mps_template where tmp_id = @jbtmpid 
end  
end else 
    begin 
      if (@tmpid = 0)  --没有子模板，则获取主模板 
      begin 
        select top 1 * from mps_template where tmp_cmpid = @cmpid and tmp_type = ''biz'' and tmp_objid = @ordbizid and tmp_caption=''缺省模板'' order by tmp_id 
        select 0 none where 0 = 1  --没有子模板 
      end else 
      begin 
        select top 1 * from mps_template where tmp_cmpid = @cmpid and tmp_type = ''biz'' and tmp_objid = @ordbizid  and tmp_caption=''缺省模板'' order by tmp_id 
        select  * from mps_template where tmp_id = @tmpid 
      end 
    end 
    -- 返回工作记录模板 t3
    if (@jbwrkid>0) 
    begin
if (exists (select * from mps_template where tmp_cmpid=@cmpid and tmp_type=''wrklog'' and tmp_objid=@jbwrkid)) 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type=''wrklog'' and tmp_objid=@jbwrkid 
end else 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type=''bizlog'' and tmp_objid=@ordbizid 
end
    end else
    begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type=''bizlog'' and tmp_objid=@ordbizid 
    end
    -- 返回当前业务定义t4 
    select * from mps_biztype where biz_id = @ordbizid 
    -- 返回业务单中的特殊权限  t5 
    select * from '+@dbname+@tbname+N'_users where(ordp_type = ''bizaudit'' or ordp_type = ''bizdelete'' or ordp_type = ''bizabolish'') and ordp_ordid = @ordid and ordp_cuid=@useruid 
    -- 返回工作中的特殊权限  t6 
    if (@jbid>0 and @ordfinished <= 1) 
    select * from mps_job_users where jp_jbid = @jbid and jp_cuid=@useruid 
    else select 0 none where 0 = 1
    --返回业务单数据 t7 
    select * from '+@dbname+@tbname+N' where ord_id = @ordid 
    -- 获取业务表主根据扩展数据  t8 
    select  * from '+@dbname+@tbname+N'_0 where ord3_ordid = @ordid 
    -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t9  
    if (@jbwsid = 0 and @ordfinished <= 1) 
    select a.*, b.* from mps_job a, mps_flowwork b where jb_id = @jbid and wrk_id = jb_wrkid 
    else select 0 none where 0 = 1 
    --返回所有附件  t10 
    select *, da_categoryid as datagroup from mps_attach where da_cmpid = @cmpid and 
    (da_type = ''biz' + ltrim(rtrim(str(@bizid)))+N''' )
    and da_objid = @ordid   order by da_categoryid 
    -- 返回需要签字的工作记录。  t11 
    if (@ordfinished <= 1) 
    select a.* from mps_job a  where jb_crmid = @ordbizid and jb_ordid = @ordid and  jb_wsid>0 and jb_sign = 0  and    jb_back = 1 
    else select a.* from mps_job a   where jb_crmid = @ordbizid and jb_ordid = @ordid and jb_wsid>0 and jb_sign = 0  and jb_back = 1  

--明细表数据
 '+char(13)+char(10)
 
 declare @ord2page int 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
 -- 大于100 不自动获取，转到用户自定义 afterload 中
select distinct  biz2_tag , biz2_page   from mps_biztype2  where   biz2_bizid=@bizid   and biz2_tag<100 
open c1
FETCH NEXT FROM c1 INTO @ord2tag , @ord2page  
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (@ord2page>1) 
begin
set @sql=@sql+N'select top '+CONVERT(varchar(10),@ord2page)+N' * from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag)+'  where ord2_ordid=@ordid   order by ord2_order, ord2_id '+char(13)+char(10)+
'select ord2_id  as ord2id_page from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag) +' where ord2_ordid=@ordid  order by ord2_order, ord2_id '+char(13)+char(10)
end else 
begin
set @sql=@sql+N'select   * from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag)+'  where ord2_ordid=@ordid   order by ord2_order, ord2_id '+char(13)+char(10)+
'select 0 as ord2id_page  '+char(13)+char(10)
end
 FETCH NEXT FROM c1 INTO @ord2tag , @ord2page 
END
CLOSE c1
DEALLOCATE c1

set @sql = @sql+N'----以下是数据源 ' 

 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bizs_source , bizs_sourcefield , bizs_sourcetype, bizs_ctgid, bizs_ctg2id  from mps_biztype_source  
where bizs_type='biz' and bizs_bizid=@bizid  and bizs_ord2tag=0 and bizs_sourcefield>'' and 
  (bizs_source<>'上级' and bizs_source<>'上上级' and bizs_source<>'根级')
open c1
declare @tableexists int 
FETCH NEXT FROM c1 INTO @bizs_source , @bizs_sourcefield , @bizs_sourcetype, @bizs_ctgid, @bizs_ctg2id 
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (charindex('@'+@bizs_sourcefield+',',@params)>0 and dbo.checkfield(@cmpid,@tbname,@bizs_sourcefield)=0) 
begin
exec sys_sql_getTableNamePk @cmpid,@bizs_sourcetype,@bizs_ctgid,0,@tbname1 output,@fkey output ,@dbname1 output 
set @tbname1 = @dbname1+@tbname1
set @tableexists = 1 
exec mps_sys_checktable @tbname1 , @tableexists output 
if (@tableexists=0) 
begin
if (@tbname1>'') set @sql=@sql+N'
select '''+@bizs_source+''' datasource, * from '+@tbname1+N'
where '+@fkey+'=@'+@bizs_sourcefield   
end 
end
 FETCH NEXT FROM c1 INTO @bizs_source , @bizs_sourcefield , @bizs_sourcetype, @bizs_ctgid, @bizs_ctg2id 
END
CLOSE c1
DEALLOCATE c1
set @sql=@sql+char(13)+char(10)+N'
--- 以下获取用户自定义SQL afterloaded 
'
if (dbo.mps_sys_checkkeywords('crm.afterload',@bizid,0,'','')=0)   -- 没有事件
begin
set @sql=@sql+char(13)+char(10)+dbo.mps_sys_getusercode('crm.afterload',@bizid,0)+char(13)+char(10) 
end

set @sql=@sql+char(13)+char(10)+N'
--- 以上获取用户自定义SQL afterloaded 
set nocount off
end 
'+char(13)+char(10) 
exec myprint @sql 
execute(@sql)
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_full'',
''@cmpid int,@ordid bigint,@useruid int,@jbid bigint''' 
execute(@sql)
set nocount off 
end