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

create procedure [dbo].[sys_sql_dealdata](
@cmpid  int,
@parseid int,
@thisbiztype varchar(10),
@thisbizid int,
@procType varchar(20),
@errmsg varchar(2000) output
) as

begin
select @errmsg=''
/*
 -- 1 展开数据源
 create table flatDSS(  
 dsIDint identity,
 parseid int,
 predsname varchar(20),--前级数据源名称
 pretbname varchar(20),    --前级表名
 dsnamevarchar(20),--本级
 shortname varchar(20),
 fullname varchar(100),
 keyval varchar(200),
 biztype varchar(20),--本级业务类型
 bizid int default 0,--本级业务ID
 tagidint default 0,--明细表Id
 canbizchange int default 0,--业务类型、业务ID是否可变:只有 '上级' ，才能改变
 preLnkFld varchar(20),
 tbNamevarchar(20),
tbDataBase varchar(30),
 keyfld varchar(20),
 prekeyfld varchar(20),
 dschain varchar(100),
 isDetailTable int default 0 ,--1 是明细表
 isDTPost int default 0,--1 是否明细表后继数据源
 orderNum int default 0,
 row int default 0,
 col int default 0,
 idParamVar varchar(50),    -- 关键值参数名称
 isLoaded int default 0,              --值是否加载 0=未加载 1=已加载
 sqlCond varchar(200),
 sqlgrp varchar(100),
 isUpper int ,
 isroot int,
 forupdate int,
 forInsert int,
 fordelete int,
 getOrder int 
 )
 
 --最终数据源与物理表的映射关系
 create table finalDS2Table(
 dsname varchar(20),
 tbname varchar(20)
 )
 
 create table flatFld(
  fldIDint identity,
 parseid int,
 dsname varchar(20),--前级数据源名称
 biztype varchar(20),
 bizid int,
 tagid int, 
 tbname varchar(20),    --前级表名
 fldname varchar(20),
 fldexp varchar(40),
 tbfld varchar(20),
 fldvar varchar(50),
 fldvarnv varchar(50),
 datatype varchar(20),
 fldsize int,
 fldprec int,
 tbfldid int,--字段ID
 isValGeted int default 0,
 fldDefinition varchar(50),  -- 字段定义
 fldDefNV varchar(50),  -- 字段新值定义 
 definedname   varchar(50),--声明名称 
 row int,
 col int
)  

 create table flatCursor(
  cursorIDint identity,
 cursorname varchar(20)--游标名称
)
 
 create table flatDSVars(
  varidint identity,
 dsname varchar(20),
 tbname varchar(20), 
 flds varchar(1000) ,--相关字段列表（用于SELECT）
fldvars varchar(4000) ,--每个字段对应的变量列表（用于Into cursor）
fldvals varchar(4000) ,--字段变量=字段,列表（用于取值）
fldDefs varchar(4000) ,
nullvals varchar(4000)
)

create table flatVarDef(
  varidint identity,
varname varchar(50),
 datatype varchar(20),
 fldsize int,
 fldprec int,
 issys int
)

create table flatRepl(
  repidint identity,
    parseid              int                  null,
   repname              varchar(50)           null,
     repExp                  varchar(500)         null,
     dsname               varchar(20)          null,
     fldname              varchar(20)          null,
     row                  int                  null,
     col                  int                  null, 
)

*/
 --数据源处理
 
delete flatDSS
delete flatfld
delete flatCursor
delete flatDSVars
delete flatVarDef 
delete flatRepl

Declare idCu cursor static For
select parseid 
from sys_sql_inf
where parseid=@parseid or glparseid=@parseid
order by parseid


open idCu
Fetch next from idCu into @parseid
While (@@Fetch_status=0)
Begin
 insert into flatRepl (parseid,repname,repExp,dsname,fldname,row,col)
 select parseid,repname,repExp,dsname,fldname,row,col
 from sys_sql_replace where parseid=@parseid
  
 --1.2 把数据源取到临时表
 insert into flatDSS(preDSName,dsName,preLnkFld,biztype,bizid,tagid,shortname,fullname,parseid,dschain,row,col,
 isDetailTable,idParamVar,isupper,isroot,forupdate,forinsert,fordelete,getorder,pretbname)
 select predsname,dsname,prelnkfld,biztype,bizid,ord2tag,shortname,fullname,parseid,dschain,row,col,
     isDetailTable,idParamVar,isupper,isroot,forupdate,forinsert,fordelete,getorder,pretbname 
 from sys_sql_ds
 where parseid=@parseid and shortname <> 'global'
 order by dschain
 update flatDSS
 set biztype=@thisbiztype,bizid=@thisbizid,idParamVar='@objid__' 
 where parseid=@parseid and shortname='this'
 update flatDSS
 set biztype='EMP',bizid=0, idParamVar='@useruid'
 where parseid=@parseid and shortname='user'
 update flatDSS
 set biztype='dpt',bizid=0
 where parseid=@parseid and shortname='depart'
 update flatDSS
 set biztype='brch',bizid=0
 where parseid=@parseid and shortname='branch'
   
--1.3 展开数据源
 exec Sys_sql_expandDs  @parseid,@cmpid,@errmsg output
  --错误检查
  if (len(@errmsg)>0)
 return 
 
 update flatDSS
set canbizchange = 1
 where parseid=@parseid and shortname='上级'
 
 
 -- 1.4 展开字段信息 
exec Sys_sql_expandFld @cmpid,@parseid, @errmsg output
  --错误检查
 if (len(@errmsg)>0)
 return 

update flatDSS
set idParamVar='@useruid'
where shortname='user'

if (@procType='java')
begin
update flatDSS
set idParamVar='@dpid'
where shortname='depart'

update flatDSS
set idParamVar='@bcid'
where shortname='branck'

update flatDSS
set idParamVar='@cmpid'
where shortname='company'
end


Fetch next From idCu Into @parseid
End
Close idCu
Deallocate idCu

--2017-8-20 生成数据源辅助信息：是否明细表
declare @isdetail int,@prebiztype varchar(20),@prebizid int,@pretagid int,@predsname varchar(20),@pretbname varchar(20)
,@biztype varchar(20),@bizid int,@ord2tag int,@tagid int,@shortname varchar(20),@dsname varchar(20),@fullname varchar(100)
,@tbname varchar(20)
Declare dsInf cursor static For
select predsname,pretbname,biztype,bizid,tagid,shortname,fullname,tbname
  from flatDSS
Open dsInf
Fetch next from dsInf Into @predsname,@pretbname,@biztype,@bizid,@tagid,@shortname,@fullname,@tbname
while (@@fetch_status=0)
begin
set @isdetail = 0
if (@predsname is not null or len(@predsname)>2)
begin
select @prebiztype=biztype,@prebizid=bizid,@pretagid=tagid
  from flatdsS
  where tbname=@pretbname
if (@prebiztype=@biztype and @prebizid=@bizid and @pretagid=0 and @tagid>0)
  set @isdetail=1  
end
update flatDSS
  set isdetailTable=@isdetail
  where tbname=@tbname
  
Fetch next from dsInf Into @predsname,@pretbname,@biztype,@bizid,@tagid,@shortname,@fullname,@tbname
end
Close dsInf
Deallocate dsInf

--生成取数顺序
-- 初始值
declare @getorder int
set @getorder=1

update flatDSS
  set getorder=100
--1 没有前级的数据源
declare ds1 cursor static for
select dsname from flatDSS where predsname is null or LEN(predsname)=0

open ds1
fetch next from ds1 into @dsname
while (@@fetch_status=0)
begin
update flatdsS
set getorder=@getorder
where dsname=@dsname
select @getorder=@getorder+1
fetch next from ds1 into @dsname
end
close ds1
deallocate ds1

--2 明细、上级、根以上数据源
declare @dealt int
set @dealt = 1
while (@dealt=1)
begin
set @dealt=0

declare ds2 cursor static for
select dsname from flatDSS 
where (isDetailTable=0 and isupper=0 and isroot=0) and getorder=100
    and predsname in (select dsname from flatDSS where getorder<100)

open ds2
fetch next from ds2 into @dsname
while (@@fetch_status=0)
begin
update flatdsS
set getorder=@getorder
where dsname=@dsname
select @getorder=@getorder+1,@dealt=1
fetch next from ds2 into @dsname
end
close ds2
deallocate ds2
end

--3 明细表、上级、根
set @getorder=51  --明细表、上级、根及以后的加载序号，从50开始

declare ds3 cursor static for
select dsname from flatDSS 
     where (isupper=1 or isroot=1 or isdetailtable=1) and getorder=100
    and predsname in (select dsname from flatDSS where getorder<100)
open ds3
fetch next from ds3 into @dsname
while (@@fetch_status=0)
begin
update flatdsS
set getorder=@getorder
where dsname=@dsname
select @getorder=@getorder+1
fetch next from ds3 into @dsname
end
close ds3
deallocate ds3

--4 其它
set @dealt = 1
while (@dealt=1)
begin
set @dealt=0

declare ds4 cursor static for
select dsname from flatDSS 
where getorder=100 and predsname in (select dsname from flatDSS where getorder<100)

open ds4
fetch next from ds4 into @dsname
while (@@fetch_status=0)
begin
update flatdsS
set getorder=@getorder
where dsname=@dsname
select @getorder=@getorder+1,@dealt=1
fetch next from ds4 into @dsname
end
close ds4
deallocate ds4
end

--5目标表为insert,delete的表不取数
update flatDSS
set getorder=0
where (forinsert=1 or fordelete=1) and forupdate=0


-- 生成数据源字段，变量，定义
Declare @flds varchar(1000),@fldvars varchar(4000),@fldvals varchar(4000)
Declare ds5 cursor static For
select dsname,tbname
from flatDSS
open ds5
Fetch next From ds5 Into @dsname,@tbname
While (@@fetch_status=0)
Begin
 exec sys_sql_get_flds @parseid,@tbname,1,@flds output,@fldvars output,@fldvals output
 
Fetch next From ds5 Into @dsname,@tbname
End
close ds5
Deallocate ds5

end