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 mps_gen_bizfull(
@cmpid int,
@bizid int
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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, 
    @userdpid int,
    @branchid int ,
    @userotid int,
    @usertype int,
    @useradmin int,
    @userctid int,
    @jbid bigint ) 
as  
begin  
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @operation nvarchar(100)
declare @errorinfo nvarchar(4000)
-- 首先取模板  t1 t2 
declare @tmpid int 
declare @ordfinished int 
declare @ordbizid int 
declare @authority int = 0 
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  
  '+[dbo].genallowtimesql(@cmpid,'biz',@bizid) 
  
  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 

--- 这段代码生成模板选择

declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select tmp_id,tmp_main,tmp_wrks,tmp_scope  from mps_template with(nolock) 
where tmp_cmpid=@cmpid and tmp_type='biz' and tmp_objid=@bizid  order by tmp_main , tmp_scope desc , tmp_wrks desc  

declare @tmp_sql0 nvarchar(max)
declare @tmp_sql nvarchar(max)
declare @tmp_sql2 nvarchar(max)
declare @tmp_ic int 
declare @tmp_id int 
declare @tmp_scope nvarchar(4000)
declare @tmp_main int 
declare @tmp_wrks varchar(1000) 
declare @varname varchar(100) 
open c1
set @tmp_sql0 = '' 
set @tmp_sql = '' 
set @tmp_sql2 = '' 
set @tmp_ic = 0
FETCH NEXT FROM c1 INTO @tmp_id,@tmp_main,@tmp_wrks,@tmp_scope 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @tmp_scope = REPLACE(@tmp_scope,'{useruid}','@useruid')
set @tmp_scope = REPLACE(@tmp_scope,'{cmpid}','@cmpid')
set @tmp_scope = REPLACE(@tmp_scope,'{userdpid}','@userdpid')
set @tmp_scope = REPLACE(@tmp_scope,'{branchid}','@branchid')
set @tmp_scope = REPLACE(@tmp_scope,'{userotid}','@userotid')
set @tmp_scope = REPLACE(@tmp_scope,'{useradmin}','@useradmin')
set @tmp_scope = REPLACE(@tmp_scope,'{usertype}','@usertype')
set @tmp_scope = REPLACE(@tmp_scope,'{userctid}','@userctid')
set @tmp_scope = REPLACE(@tmp_scope,'{','')
set @tmp_scope = REPLACE(@tmp_scope,'}','')
if (len(@tmp_scope)<5) set @tmp_scope='' 
if (@tmp_wrks='' or @tmp_wrks='' or @tmp_wrks=',,') set @tmp_wrks = '' else set @tmp_wrks=','+@tmp_wrks+','
set @tmp_ic = @tmp_ic + 1 
if (@tmp_sql>'') set @tmp_sql = @tmp_sql +' , '+CHAR(13)+CHAR(10)+'' 
set @varname ='@'+case when @tmp_main=0 then 'main__' else 'subform__' end+CONVERT(varchar(10),@tmp_ic) 
set @tmp_sql = @tmp_sql +' '+@varname+'=' 
set @tmp_sql0 = @tmp_sql0+CHAR(13)+CHAR(10)+'declare '+@varname +' int '
if (@tmp_main=0) 
begin
set @tmp_sql2 = @tmp_sql2 +CHAR(13)+CHAR(10)+'if ('+@varname+'>0 and (not exists (select 1 from @template where ismain=0))) insert into @template(tmpid,ismain) values('+@varname+',0) '
end else
begin
set @tmp_sql2 = @tmp_sql2 +CHAR(13)+CHAR(10)+'if ('+@varname+'>0) insert into @template(tmpid,ismain) values('+@varname+','+CONVERT(varchar(10),@tmp_main)+') '
end 
if (@tmp_scope='' and @tmp_wrks='') 
begin
set @tmp_sql = @tmp_sql + CONVERT(varchar(10),@tmp_id) 
end else
if (@tmp_scope='' and @tmp_wrks>'') 
begin
set @tmp_sql = @tmp_sql + 'case when charindex('',''+@jbwrkids+'','','''+@tmp_wrks+''')>0 then '+ CONVERT(varchar(10),@tmp_id) +' else 0 end '
end else 
if (@tmp_scope>'' and @tmp_wrks='') 
begin
set @tmp_sql = @tmp_sql + 'case when '+@tmp_scope+' then '+ CONVERT(varchar(10),@tmp_id) +' else 0 end '
end else 
begin
set @tmp_sql = @tmp_sql + 'case when charindex('',''+@jbwrkids+'','','''+@tmp_wrks+''')>0 and '+@tmp_scope+'  then '+ CONVERT(varchar(10),@tmp_id) +' else 0 end '
end 
 FETCH NEXT FROM c1 INTO @tmp_id,@tmp_main,@tmp_wrks,@tmp_scope 
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  ,@authority=ord_authority_factor,
  @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' with(nolock) where ord_id = @ordid and ord_cmpid = @cmpid 
declare @jbtmpid int   --保存工作步骤的模板 
declare @jbwsid int 
declare @jbwrkid int
declare @jbwrkids varchar(100)  
if (@ordfinished <= 1)  --业务还没有完 
begin 
select @jbwrkid=jb_wrkid,@jbtmpid = jb_templateid, @jbwsid = jb_wsid  from  mps_job with(nolock)  where jb_id = @jbid and jb_cmpid = @cmpid 
end 
set @jbtmpid = isnull(@jbtmpid, 0) 
set @jbwsid = isnull(@jbwsid, -1) 
set @jbwrkid = isnull(@jbwrkid, 0) 
set @jbwrkids = convert(varchar(10),@jbwrkid) 

-- 下面的代码是计算主模板及子模板
'+@tmp_sql0+'
declare @template table(tmpid int , ismain int) 
select '+@tmp_sql+' from '+@dbname+@tbname+' with(nolock) where ord_id=@ordid 
'+@tmp_sql2+'
-- 如果没有主模板，则要缺省找一个
if (not exists (select 1 from @template where ismain=0)) 
begin
insert into @template(tmpid,ismain) select tmp_id ,0 from mps_template with(nolock) where tmp_cmpid=@cmpid and tmp_objid='+CONVERT(varchar(10),@bizid)+' and tmp_cmpid='+CONVERT(varchar(10),@cmpid)+' and (tmp_caption=''缺省模板'' or tmp_caption='''') and tmp_main=0 
end 

if (@myallow__time=1)
begin
-- 返回主模板 t1
if (@errorinfo='''') set @errorinfo=''当前时间不允许访问''
select tmp_id,tmp_cmpid,tmp_type ,tmp_objid,tmp_objid2,tmp_caption,tmp_flowxml,tmp_scope,tmp_language,tmp_wrks,tmp_main,
''<div class=f24 style="text-align:center;padding-top:30px;">''+@errorinfo+''</div>'' tmp_form,'''' tmp_js,'''' tmp_css,
''<div class=f24 style="text-align:center;padding-top:30px;">''+@errorinfo+''</div>'' tmp_formmobile,'''' tmp_jsmobile,'''' tmp_cssmobile,'''' tmp_sql,'''' tmp_jssql 
from mps_template with(nolock) , @template where tmp_id=tmpid and ismain=0
-- 返回子模板 t2
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain>0 and tmp_id=-1 
end else
begin
-- 返回主模板 t1
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain=0
-- 返回子模板 t2
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain>0
end 

    -- 返回工作记录模板 t3
    if (@jbwrkid>0) 
    begin
if (exists (select * from mps_template  with(nolock) where tmp_cmpid=@cmpid and tmp_type=''wrklog'' and tmp_objid=@jbwrkid)) 
begin
select * from mps_template  with(nolock) where tmp_cmpid=@cmpid and tmp_type=''wrklog'' and tmp_objid=@jbwrkid 
end else 
begin
select * from mps_template  with(nolock)  where tmp_cmpid=@cmpid and tmp_type=''bizlog'' and tmp_objid=@ordbizid 
end
    end else
    begin
select * from mps_template  with(nolock)  where tmp_cmpid=@cmpid and tmp_type=''bizlog'' and tmp_objid=@ordbizid 
    end
    -- 返回当前业务定义t4 
    select * from mps_biztype  with(nolock) where biz_id = @ordbizid  and @myallow__time=0
    -- 返回业务单中的特殊权限  t5  
    select case orda_type when 1 then ''bizview'' when 2 then ''bizaudit'' when 3 then ''bizabolish'' when 4 then ''bizdelete'' when 5 then ''bizlog'' else '''' end ordp_type , orda_cuid , orda_cuid ordp_cuid 
    from '+@dbname+@tbname+N'_authority_control  with(nolock) where orda_ordafid=@authority and orda_cuid=@useruid and orda_type>1  and @myallow__time=0
    -- 返回工作中的特殊权限  t6 
    if (@jbid>0 and @ordfinished <= 1) 
    select * from mps_job_users  with(nolock) where jp_jbid = @jbid and jp_cuid=@useruid  and @myallow__time=0
    else select 0 none where 0 = 1
    --返回业务单数据 t7 
    select * from '+@dbname+@tbname+N' with(nolock)  where ord_id = @ordid and @myallow__time=0
    -- 获取业务表主根据扩展数据  t8 
    select  * from '+@dbname+@tbname+N'_0 with(nolock)  where ord3_ordid = @ordid and @myallow__time=0
    -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t9  
    if (@jbwsid = 0 and @ordfinished <= 1) 
    select a.*, b.* from mps_job a with(nolock) , mps_flowwork b  with(nolock) where jb_id = @jbid and wrk_id = jb_wrkid  and @myallow__time=0
    else select 0 none where 0 = 1 
    --返回所有附件  t10 
    select *, da_categoryid as datagroup from mps_attach  with(nolock) where da_cmpid = @cmpid and 
    (da_type = ''biz' + ltrim(rtrim(str(@bizid)))+N''' )
    and da_objid = @ordid  and @myallow__time=0  order by da_categoryid 
    -- 返回需要签字的工作记录。  t11 
    if (@ordfinished <= 1) 
    select a.* from mps_job a  with(nolock)  where jb_crmid = @ordbizid and jb_ordid = @ordid and  jb_wsid>0 and jb_sign = 0  and    jb_back = 1  and @myallow__time=0
    else select a.* from mps_job a  with(nolock)   where jb_crmid = @ordbizid and jb_ordid = @ordid and jb_wsid>0 and jb_sign = 0  and jb_back = 1   and @myallow__time=0

--明细表数据
 '+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  with(nolock) 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)+' with(nolock)   where ord2_ordid=@ordid  and @myallow__time=0 order by ord2_order, ord2_id '+char(13)+char(10)+
'select ord2_id  as ord2id_page from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag) +' with(nolock)  where ord2_ordid=@ordid and @myallow__time=0 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)+'  with(nolock)  where ord2_ordid=@ordid and @myallow__time=0  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' with(nolock) 
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 
-- 获取最近5条评论
--创建内存表，存储所有fm_ffmid = 0的评论
declare @forumpobjs table(nid int identity(1, 1), fmid bigint) 
insert into @forumpobjs(fmid)
select top 5 fm_id from mps_forum a with(nolock) 
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = ''biz'' and a.fm_ctgid = '+convert(varchar(50),@bizid)+' and a.fm_objid=@ordid 
order by a.fm_id desc

--查询出回复
declare @strforum varchar(max) = ''''
select @strforum = stuff((
select '' fm_id = ''+convert(varchar(100),fmid)+ '' or ''
from @forumpobjs for xml path('''')),1,1,'''')
set @strforum = isnull(@strforum,'''')

--查询出主回复的回复
declare @strforum1 varchar(max) = ''''
select @strforum1 = stuff((
select '' fm_ffmid = ''+convert(varchar(100),fmid)+'' or fm_fffmid = '' +convert(varchar(100),fmid)+ '' or ''
from @forumpobjs for xml path('''')),1,1,'''')

set @strforum1 = isnull(@strforum1,'''')

if @strforum1 > ''''
begin
set @strforum1 = @strforum +substring(@strforum1,1,len(@strforum1)-3)
set @strforum1 = '' select 1 as fm_bizforum,fm_id,fm_cuid,fm_cuname,fm_text,fm_ffmid,fm_fffmid,fm_createdtime,fm_attach from mps_forum a  with(nolock) where '' + @strforum1
 + '' order by fm_id ''
exec(@strforum1)
end

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,@userdpid int,@branchid int,@userotid int,@usertype int,@useradmin int,@userctid int,@jbid bigint''' 
execute(@sql)
set nocount off 
end