IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_cstfull]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_cstfull]
GO
create procedure [dbo].[mps_gen_cstfull](
@cmpid int,
@ctid 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) 
declare @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 

declare @params varchar(4000) 
set @params = '@cc_fccid,' 
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,  
    @ccid bigint, 
    @useruid int,
    @canedit int ) 
as  
begin  
set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
-- 首先取模板  t1  
declare @ctid int 
declare @cctag int
select @cctag=cc_tag,@ctid=cc_ctid from '+@dbname+@tbname+' where cc_id=@ccid 
declare @tmpid int 
declare @ctidf int 
declare @flagstr varchar(30) 
set @flagstr=''cst'' 
if (@canedit=1) set @flagstr=''cstdisp'' 
set @tmpid = 0 
set @ctidf = @ctid 
select top 1 @tmpid=tmp_id from mps_template where  tmp_cmpid=@cmpid and tmp_type=@flagstr and tmp_objid=@ctid  
select  * from mps_template where  tmp_id=@tmpid 



-- 返回客户定义表t2  
   select * from mps_csttype where ct_id=@ctid 
   
  
  -- 返回客户的特殊权限  t3 
    select * from '+@dbname+@tbname+'_users where ccp_ccid=@ccid and ccp_cuid=@useruid 
    
    --返回业务单数据 t4
    select * from '+@dbname+@tbname+' where cc_id = @ccid 
    -- 获取业务表主根据扩展数据  t5
    select  * from '+@dbname+@tbname+'_0 where cc3_ccid = @ccid 
    
    --返回所有附件  t6
declare @type1 varchar(50)
    if @cctag = 0 set @type1 = ''cst''
    else set @type1 = ''cst''+convert(varchar(10),@cctag)
    select *, da_categoryid as datagroup from mps_attach where da_cmpid = @cmpid 
    and da_type =  @type1  and da_objid = @ccid and da_objid2 = 0 order by da_categoryid 
      
     

--明细表数据
 '+char(13)+char(10)
 
 declare @ord2page int 
 declare @ord2tag int 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
 -- 大于100 不自动获取，转到用户自定义 afterload 中
select distinct  ct2_tag , ct2_page   from mps_csttype2  where   ct2_ctid=@ctid   and ct2_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 cc2_ccid=@ccid   order by cc2_order, cc2_id '+char(13)+char(10)+
'select cc2_id  as ord2id_page from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag) +' where cc2_ccid=@ccid  order by cc2_order, cc2_id '+char(13)+char(10)
end else 
begin
set @sql=@sql+N'select   * from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag)+'  where cc2_ccid=@ccid   order by cc2_order, cc2_id '+char(13)+char(10)+
'select 0 as cc2id_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='cst' and bizs_bizid=@ctid  and bizs_ord2tag=0 and bizs_sourcefield>'' and 
  (bizs_source<>'上级' and bizs_source<>'上上级' and bizs_source<>'根级')
open c1
declare @bizs_source nvarchar(100) , @bizs_sourcefield varchar(50), @bizs_sourcetype varchar(30), @bizs_ctgid int, @bizs_ctg2id int 
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('cst.afterload',@ctid,0,'','')=0)   -- 没有事件
begin
set @sql=@sql+char(13)+char(10)+dbo.mps_sys_getusercode('cst.afterload',@ctid,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,@ccid bigint,@useruid int,@canedit int''' 
execute(@sql)
set nocount off 
end