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

declare @params varchar(4000) 
set @params = '@prd_fprdid,' 
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,  
    @prdid bigint, 
    @useruid int,
    @canedit int ) 
as  
begin  
set nocount on 
-- 首先取模板  t1  
declare @pcid int 
declare @pctag int
select @pctag=prd_tag,@pcid=prd_pcid from '+@dbname+@tbname+' where prd_id=@prdid 

declare @tmpid int 

declare @ctidf int 
declare @ctid2 int 
declare @flagstr varchar(30) 
set @flagstr=''prd'' 
if (@canedit=1) set @flagstr=''prddetails'' 

set @tmpid = 0 
set @ctid2 = @pcid 
select top 1 @tmpid=tmp_id from mps_template where  tmp_cmpid=@cmpid and tmp_type=@flagstr and tmp_objid=@pcid  

while (@tmpid=0 and @ctid2>0) 
begin
set @ctidf = 0 
select @ctidf = pc_father from mps_prdctg where pc_id=@ctid2 
if (@ctidf>0) 
begin
select top 1 @tmpid=tmp_id from mps_template where  tmp_cmpid=@cmpid and tmp_type=@flagstr and tmp_objid=@ctidf 
set @ctid2 = @ctidf 
end else 
begin
set @ctid2 = 0 
end 
end 

select  * from mps_template where  tmp_id=@tmpid 

-- 返回客户定义表t2  
   select * from mps_prdctg where pc_id=@pcid 
   
  
  -- 返回客户的特殊权限  t3 
    select * from '+@dbname+@tbname+'_users where prdp_prdid=@prdid and prdp_cuid=@useruid 
    
    --返回业务单数据 t4
    select * from '+@dbname+@tbname+' where prd_id = @prdid 
    -- 获取业务表主根据扩展数据  t5
    select  * from '+@dbname+@tbname+'_0 where prd3_prdid = @prdid 
    
    --返回所有附件  t6
    select *, da_categoryid as datagroup from mps_attach where da_cmpid = @cmpid and da_type = ''prd''+convert(varchar(10),@pctag)   and da_objid = @prdid 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  pc2_tag , pc2_page   from mps_prdctg2  where   pc2_pcid=@pcid   and pc2_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 prd2_prdid=@prdid   order by prd2_order, prd2_id '+char(13)+char(10)+
'select prd2_id  as ord2id_page from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag) +' where prd2_prdid=@prdid  order by prd2_order, prd2_id '+char(13)+char(10)
end else 
begin
set @sql=@sql+N'select   * from '+@dbname+@tbname+'_'+
CONVERT(varchar(10),@ord2tag)+'  where prd2_prdid=@prdid   order by prd2_order, prd2_id '+char(13)+char(10)+
'select 0 as prd2id_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='prd' and bizs_bizid=@pcid  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('prd.afterload',@pcid,0,'','')=0)   -- 没有事件
begin
set @sql=@sql+char(13)+char(10)+dbo.mps_sys_getusercode('prd.afterload',@pcid,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,@prdid bigint,@useruid int,@canedit int''' 
execute(@sql)
set nocount off 
end