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

CREATE procedure [dbo].[mps_calc_bizrelation](
@cmpid int,
@bizid int
) as
begin
declare @t table(bizid int) 
insert into @t(bizid) values(@bizid) 

declare @tb varchar(50) 
declare @tbext int
declare @ic int 
set @ic = 1 
declare @ic2 int 
while (@ic=@ic)
begin
  -- 将所有的上级加上来
insert into @t(bizid) 
select distinct bs_sbizid 
from mps_biztype_comefrom ,mps_biztype_comefrom_fieldmap,@t a
where bs_bizid=a.bizid and bs_ord2tag=0 and bs_stype='biz' and 
bs_sbizid>0 and bs_sord2tag=0 and bs_id=bsf_bsid and bsf_use=0 and 
(not exists (select * from @t b where bs_sbizid=b.bizid))

-- 将所有的下级加上来
insert into @t(bizid) 
select distinct bs_bizid  
from mps_biztype_comefrom ,mps_biztype_comefrom_fieldmap,@t a
where bs_ord2tag=0 and bs_stype='biz' and 
bs_sbizid=a.bizid and bs_sord2tag=0 and bs_id=bsf_bsid and bsf_use=0 and 
(not exists (select * from @t b where bs_bizid=b.bizid))

select @ic2=count(1) from @t 

if (@ic2=@ic) break  -- 如何平衡，则退出。
set @ic=@ic2 
end
declare @t2 table(bizid2 int) 
insert into @t2(bizid2) select bizid from @t 
delete mps_biztype_relation from @t where br_bizid=bizid 
insert into mps_biztype_relation(br_bizid,br_bizid2,br_bizcaption) 
select bizid,bizid2,biz_title 
from @t,@t2,mps_biztype
where biz_id=bizid2 and bizid<>bizid2 

declare @dbdottb varchar(200) 
set @dbdottb = '' 
declare @hastablei int 
set @hastablei = 0 
-- 以下代码将生成计算订单的关系
declare @sql nvarchar(MAX) 
declare @sql2 nvarchar(MAX) 
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bizid  from @t   
open status1
declare @bizid1 int 
declare @bizid2 int 
declare @biz2 int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50)
declare @tbname2 varchar(50)
declare @dbname2 varchar(50) 
FETCH NEXT FROM status1 INTO @bizid1 
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid1,0,@tbname output,@fkey output ,@dbname output 
if (dbo.checkprocedure(@tbname+'_calc_relation')=0) set @sql='alter ' else set @sql='create '
set @sql = @sql+' procedure '+@tbname+N'_calc_relation('+char(13)+
'@cmpid int,'+char(13)+
'@ordid bigint'+char(13)+
') as '+char(13)+
'begin'+char(13) +
'declare @t table(bizid int , ordid bigint) '+char(13)+
'insert into @t(bizid,ordid) values('+convert(varchar(10),@bizid1)+',@ordid) '+char(13)+
'insert into @t(bizid,ordid) '+char(13)+
'select ord_fbizid , ord_fordid '+char(13)+
'from '+@dbname+@tbname+char(13)+
'where ord_id=@ordid and ord_ftype=''biz'' and ord_ford2tag=0 and ord_fordid>0 '+char(13) + 
'declare @ic int '+char(13) + 
'select @ic = count(*) from @t '+char(13) + 
'if (@ic<=1) return   '+char(13) + 
'declare @ic2 int '+char(13) + 
'while (@ic=@ic)'+char(13) + 
'begin'+char(13)  

if (dbo.checkprocedure(@tbname+'_g_relation')=0) set @sql2='alter ' else set @sql2='create '
set @sql2 = @sql2+' procedure '+@tbname+N'_g_relation('+char(13)+
'@ordid bigint, '+char(13)+
'@useruid int '+char(13)+
') as '+char(13)+
'begin'+char(13) +
'set nocount on '+char(13) +
'select ord_id,ord_bizid,ord_bcid,ord_branch,ord_cmpid,ord_createcuid,ord_createdepart,ord_createdpid,ord_creator,ord_date,ord_key,ord_no,ord_status,ord_title,ord_type,ord_updated,ord_dpid,ord_cuid,ord_depart,ord_empname , @useruid ordp_cuid '+char(13)+
'from '+@dbname+@tbname+' where ord_id=@ordid '+char(13) 


 declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bizid2  from @t2  where  bizid2<>@bizid1 
open status11
FETCH NEXT FROM status11 INTO @bizid2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @biz2 = @bizid2 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid2,0,@tbname2 output,@fkey output ,@dbname2 output 
set @dbdottb = @dbname2 + @tbname2 
set @hastablei = 1 
exec mps_sys_checktable @dbdottb , @hastablei output 
if (@hastablei=0) 
begin
set @sql=@sql+ 
'insert into @t(bizid,ordid) '+char(13) + 
'select ord_bizid , ord_id '+char(13) + 
'from '+@dbname2+@tbname2+' , @t a '+char(13) + 
'where ord_cmpid=@cmpid and ord_fbizid=a.bizid and ord_fordid=a.ordid and ord_ftype=''biz'' and '+char(13) + 
'(not exists (select 1 from @t b where b.bizid=ord_bizid and b.ordid=ord_id ))'+char(13) +
'insert into @t(bizid,ordid) '+char(13) + 
'select ord_fbizid , ord_fordid '+char(13) + 
'from '+@dbname2+@tbname2+' , @t a '+char(13) + 
'where ord_cmpid=@cmpid and ord_bizid=a.bizid and ord_id=a.ordid and ord_ftype=''biz'' and ord_fbizid>0 and ord_fordid>0 and '+char(13) + 
'(not exists (select 1 from @t b where b.bizid=ord_bizid and b.ordid=ord_id ))'+char(13)   

set @sql2 = @sql2+'union all'+char(13)+
'select ord_id,ord_bizid,ord_bcid,ord_branch,ord_cmpid,ord_createcuid,ord_createdepart,ord_createdpid,ord_creator,ord_date,ord_key,ord_no,ord_status,ord_title,ord_type,ord_updated,ord_dpid,ord_cuid,ord_depart,ord_empname,isnull(ordp_cuid,0) ordp_cuid '+char(13)+
'from '+@dbname2+@tbname2+' inner join '+@dbname+@tbname+'_relation on ordr_rordid=ord_id '+char(13)+
'and ordr_ordid=@ordid and ordr_rcrmid='+convert(varchar(10),@biz2)+' LEFT OUTER JOIN '+char(13)+
''+@dbname2+@tbname2+'_users on ordp_ordid=ord_id and ordp_type=''bizview'' and ordp_cuid=@useruid '
end 
 FETCH NEXT FROM status11 INTO @bizid2 
END
  CLOSE status11
DEALLOCATE status11 
set @sql=@sql+ 
'select @ic2=count(1) from @t '+char(13)+
'if (@ic2=@ic) break  -- 如何平衡，则退出。'+char(13)+
'set @ic=@ic2 '+char(13)+
'end  '+char(13) 

set @sql=@sql+ 
'declare @mybizid int '+char(13) +
'declare @myordid bigint'+char(13)+ 
'declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for '+char(13) +
'select bizid,ordid  from @t  '+char(13) +
'open status1'+char(13) +
'FETCH NEXT FROM status1 INTO @mybizid,@myordid'+char(13)+
'WHILE (@@FETCH_STATUS = 0)'+char(13) +
'BEGIN'+char(13)
 
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bizid2  from @t2   
open status11
FETCH NEXT FROM status11 INTO @bizid2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @biz2 = @bizid2 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid2,0,@tbname2 output,@fkey output ,@dbname2 output 
set @dbdottb = @dbname2 + @tbname2 
set @hastablei = 1 
exec mps_sys_checktable @dbdottb , @hastablei output 
if (@hastablei = 0) 
begin
set @sql=@sql+ 
'if (@mybizid='+convert(varchar(10),@bizid2)+') '+char(13) +
 'begin'+char(13) +
 'insert into '+@dbname2+@tbname2+'_relation(ordr_ordid,ordr_rcrmid,ordr_rordid) '+char(13) +
'select @myordid , bizid , ordid from @t where bizid<>@mybizid and ordid<>@myordid and '+char(13) +
'(not exists (select 1 from '+@dbname2+@tbname2+'_relation where ordr_ordid=@myordid and ordr_rcrmid=bizid and ordr_rordid=ordid)) '+char(13) +
'end '+char(13) 
end 
 FETCH NEXT FROM status11 INTO @bizid2 
END
  CLOSE status11
DEALLOCATE status11 

set @sql=@sql+ 
 'FETCH NEXT FROM status1 INTO @mybizid,@myordid '+char(13) +
'END'+char(13)+ 
'CLOSE status1'+char(13) +
'DEALLOCATE status1 '+char(13) 

 FETCH NEXT FROM status1 INTO @bizid1 
 set @sql=@sql+ 
'end' 

set @sql2=@sql2+ 
'order by ord_date '+char(13)+
'set nocount off '+char(13) +
'end  '+char(13) 

exec myprint @sql 
exec myprint @sql2 
execute(@sql)  
execute(@sql2) 
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_g_relation'',
''@ordid bigint,@useruid int ''' 
execute(@sql) 
END
CLOSE status1
DEALLOCATE status1 

end