IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_fieldsinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_fieldsinfo]
GO
CREATE procedure [dbo].[mps_save_fieldsinfo] 
@cmpid int ,
@tbtype varchar(10) ,
@objid int ,
@objid2 int ,
@fldname varchar(50) ,
@fldcaption nvarchar(100) ,
@fldtype varchar(20) ,
@fldsize int ,
@flddefault nvarchar(100) 
as
begin
set @fldname = REPLACE(@fldname,'?','') 
/*
ss=["\t","\r","\n"," ","-","+","=","*","(",")","&","^","%","$","#","@","!","~","`","{","}","[","]","|","\\"
,"/",";",":","'",'"',".",">","?",",","<","，","。","（","）","、","；","："];
*/

if (@fldtype='string') set @fldtype='nvarchar' 

set @fldname = REPLACE(@fldname,'-','') 
set @fldname = REPLACE(@fldname,'+','') 
set @fldname = REPLACE(@fldname,'=','') 
set @fldname = REPLACE(@fldname,'*','') 
set @fldname = REPLACE(@fldname,'(','') 
set @fldname = REPLACE(@fldname,')','') 
set @fldname = REPLACE(@fldname,'&','') 
set @fldname = REPLACE(@fldname,'%','') 
set @fldname = REPLACE(@fldname,'$','') 
set @fldname = REPLACE(@fldname,'@','') 
set @fldname = REPLACE(@fldname,' ','') 
set @fldname = REPLACE(@fldname,'.','') 
set @fldname = REPLACE(@fldname,'|','') 
declare @nomaster int 
set @nomaster = 1  
declare @nodetail int 
set @nodetail = 1  

-- 要转化一下cst prd equ 的TAG值 因为传入的@objid=ctid 
if (@tbtype='cst') 
begin
select top 1 @objid=ct_tag from mps_csttype where ct_id=@objid 
end else 
if (@tbtype='prd') 
begin
select top 1 @objid=pc_tag from mps_prdctg where pc_id=@objid 
end else 
if (@tbtype='equ') 
begin
select top 1 @objid=ec_tag from mps_equctg where ec_id=@objid 
end   else 
if (@tbtype='etc') 
begin
select top 1 @objid=etc_tag from mps_ext_resource where etc_id=@objid 
end
declare @dbname varchar(50)
declare @tbname varchar(50) 
declare @tbname0 varchar(50) 
declare @tbn varchar(50) 
declare @tbid int 
declare @skeyfld varchar(50) 


if (@tbtype='biz') 
begin
exec sys_sql_getTableNamePk @cmpid,'biz',@objid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='ord_id' 
set @tbname = @dbname+@tbname 

set @tbn=@tbname 
set @tbname0=@tbname 
if (@objid2=-1) set @tbname=@tbname+'_0'
if (@objid2=0) set @tbname=@tbname 
if (@objid2>0) set @tbname=@tbname+'_'+ltrim(rtrim(str(@objid2)))
end else 
if (@tbtype='cst') 
begin
exec sys_sql_getTableNamePk @cmpid,'cst',@objid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='cc_id' 
set @tbname = @dbname+@tbname 

set @tbname0=@tbname
set @tbn=@tbname
if (@objid2=-1) set @tbname=@tbname+'_0'
if (@objid2=0) set @tbname=@tbname
if (@objid2>0) set @tbname=@tbname+'_'+ltrim(rtrim(str(@objid2)))
end else 
if (@tbtype='doc') 
begin
set @dbname = '' 
set @tbname='mps_document' 
set @tbname0=@tbname
set @tbn =@tbname 
end  else 
if (@tbtype='prd') 
begin
exec sys_sql_getTableNamePk @cmpid,'prd',@objid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='prd_id' 
set @tbname = @dbname+@tbname 

set @tbname0=@tbname
set @tbn =@tbname
if (@objid2=-1) set @tbname=@tbname+'_0'
if (@objid2=0) set @tbname=@tbname
if (@objid2>0) set @tbname=@tbname+'_'+ltrim(rtrim(str(@objid2)))
end  else 
if (@tbtype='equ') 
begin
exec sys_sql_getTableNamePk @cmpid,'equ',@objid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='equ_id' 
set @tbname = @dbname+@tbname 

set @tbname0=@tbname
set @tbn =@tbname
if (@objid2=-1) set @tbname=@tbname+'_0'
if (@objid2=0) set @tbname=@tbname
if (@objid2>0) set @tbname=@tbname+'_'+ltrim(rtrim(str(@objid2)))

end  else 
if (@tbtype='ext') 
begin
exec sys_sql_getTableNamePk @cmpid,'ext',@objid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='ext_id' 
set @tbname = @dbname+@tbname 

set @tbname0=@tbname
set @tbn =@tbname
if (@objid2=-1) set @tbname=@tbname+'_0'
if (@objid2=0) set @tbname=@tbname
if (@objid2>0) set @tbname=@tbname+'_'+ltrim(rtrim(str(@objid2)))
 
end else 
if (@tbtype='prg') 
begin
set @dbname=''
select top 1  @dbname=cmp_dbprivate from ebs_kernel.dbo.pub_company where cmp_id=@cmpid 
set @dbname=isnull(@dbname,'') 

set @tbid=@objid 
set @tbid = isnull(@tbid,0) 

set @tbname='user_'+ltrim(rtrim(str(@tbid))) 
set @tbname0=@tbname
set @tbn ='user_'+ltrim(rtrim(str(@tbid))) 
if (@objid2=-1) set @tbname='user_'+ltrim(rtrim(str(@tbid)))+'_0'
if (@objid2=0) set @tbname='user_'+ltrim(rtrim(str(@tbid))) 
if (@objid2>0) set @tbname='user_'+ltrim(rtrim(str(@tbid)))+'_'+ltrim(rtrim(str(@objid2)))
end else 
begin
set @tbid = 0 
set @dbname='' 
set @tbname='' 
set @tbname0=@tbname
end
 
if (@tbname='') return 


-- 如果主表字段没有，则新建
if (not exists (select * from mps_fieldinfo 
where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=0)) 
begin
set @nomaster = 0  

insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_system,fldi_hidden,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,fldi_ord2tag,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_system,fldi_hidden ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag<=-1 
order by fldi_id 

insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_system,fldi_hidden,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,0,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_system,fldi_hidden ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=1 
order by fldi_id 

  -- 生成0 user ... 等等关联表
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index )
select @cmpid,@tbtype,@objid,fldi_ord2tag,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default  ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag<=-1 
order by fldi_id 

-- 生成主表
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,0,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=1 
order by fldi_id 
end 

-- 如果没有明细表，则先建明细表
if ((@objid2>0) and  (not exists (select * from mps_fieldinfo 
where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2))) 
begin
set @nodetail = 0  
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_system,fldi_hidden,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,@objid2,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname end,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_system,fldi_hidden ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=0 
order by fldi_id 

insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,@objid2,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname end,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=0 
order by fldi_id 
end 

-- 如果是系统字段，则不处理
declare @ok int 
set @ok = 0 
if (@objid2=0)
begin
if (exists (select * from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=1 and fldi_fieldname=@fldname))
begin
set @ok=1
end 
end else
begin
if (exists (select * from ebs_kernel.dbo.pub_fieldinfo_standard 
where fldi_tbtype=@tbtype and fldi_ord2tag=0 and fldi_fieldname=@fldname))
begin
set @ok=1
end 
end 


if (@ok=0) 
begin

declare @fldid int 
 
-- 如果没有明细表，则生成明细表
set @fldid=0 
select top 1 @fldid = fldi_id from mps_fieldinfo_operation where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldname  
set @fldid = isnull(@fldid,0) 
if (@fldid=0)
begin
   if (not exists (select * from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldname))
   begin
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,
fldi_fieldname_old,fldi_datatype_old,fldi_size_old,fldi_decimal_old,fldi_default_old) 
select @cmpid , @tbtype , @objid , @objid2 ,@dbname, @tbname ,
@fldname , @fldtype,
case when @fldtype='decimal' then 18 else @fldsize end , 
case when @fldtype='decimal' then @fldsize else 0 end ,
@flddefault ,'','',0,0,'' 
end else 
begin
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,
fldi_fieldname_old,fldi_datatype_old,fldi_size_old,fldi_decimal_old,fldi_default_old,fldi_ident,fldi_key,fldi_index) 
select @cmpid , @tbtype , @objid , @objid2 ,@dbname,@tbname  ,
@fldname , @fldtype,
case when @fldtype='decimal' then 18 else @fldsize end , 
case when @fldtype='decimal' then @fldsize else 0 end ,
@flddefault ,
fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index 
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldname and 
(fldi_datatype<>@fldtype or fldi_size<>@fldsize or fldi_default<>@flddefault) 
end 
end else 
begin
update mps_fieldinfo_operation set 
fldi_size = case when @fldtype='decimal' then 18 else @fldsize end ,
fldi_decimal = case when @fldtype='decimal' then @fldsize else 0 end , 
fldi_default = @flddefault ,
fldi_datatype = @fldtype 
where fldi_id=@fldid 
end 
set @fldid=0 
select top 1 @fldid = fldi_id from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldname  
set @fldid = isnull(@fldid,0) 
if (@fldid=0)
begin
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname, fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default) 
select @cmpid , @tbtype , @objid , @objid2 ,@dbname,@tbname,
@fldname , @fldcaption,@fldtype,
case when @fldtype='decimal' then 18 else @fldsize end , 
case when @fldtype='decimal' then @fldsize else 0 end ,
@flddefault 
end else 
begin
update mps_fieldinfo set 
fldi_fieldcaption = @fldcaption ,
fldi_size = case when @fldtype='decimal' then 18 else @fldsize end ,
fldi_decimal = case when @fldtype='decimal' then @fldsize else 0 end , 
fldi_default = @flddefault , 
fldi_changed = 0 
where fldi_id=@fldid 
end 
end 
 
 
 -- 生成0 user ... 等等关联表
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index )
select @cmpid,@tbtype,@objid,fldi_ord2tag,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default  ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard a 
where fldi_tbtype=@tbtype and fldi_ord2tag<=-1 and
  (not exists (select 1 from mps_fieldinfo b where 
b.fldi_cmpid=@cmpid and b.fldi_tbtype=a.fldi_tbtype and  a.fldi_ord2tag=b.fldi_ord2tag and 
b.fldi_tbid=@objid and b.fldi_fieldname=a.fldi_fieldname 
 )) 
order by fldi_id 


insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_system,fldi_hidden,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,fldi_ord2tag,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_system,fldi_hidden ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard  a  
where fldi_tbtype=@tbtype and fldi_ord2tag<=-1 and
  (not exists (select 1 from mps_fieldinfo b where 
b.fldi_cmpid=@cmpid and b.fldi_tbtype=a.fldi_tbtype and  a.fldi_ord2tag=b.fldi_ord2tag and 
b.fldi_tbid=@objid and b.fldi_fieldname=a.fldi_fieldname 
 )) 
order by fldi_id 

insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,0,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard  a  
where fldi_tbtype=@tbtype and fldi_ord2tag=1 and
  (not exists (select 1 from mps_fieldinfo b where 
b.fldi_cmpid=@cmpid and b.fldi_tbtype=a.fldi_tbtype and  a.fldi_ord2tag=1 and b.fldi_ord2tag=0 and 
b.fldi_tbid=@objid and b.fldi_fieldname=a.fldi_fieldname 
 )) 
order by fldi_id 

insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag, fldi_database,fldi_tbname,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default,fldi_system,fldi_hidden,fldi_ident,fldi_key,fldi_index)
select @cmpid,@tbtype,@objid,0,@dbname,case when fldi_tbname>'' then @tbn+'_'+fldi_tbname else @tbname0 end,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default ,fldi_system,fldi_hidden ,fldi_ident,fldi_key,fldi_index
from ebs_kernel.dbo.pub_fieldinfo_standard  a  
where fldi_tbtype=@tbtype and fldi_ord2tag=1 and
  (not exists (select 1 from mps_fieldinfo b where 
b.fldi_cmpid=@cmpid and b.fldi_tbtype=a.fldi_tbtype and  a.fldi_ord2tag=1 and b.fldi_ord2tag=0 and 
b.fldi_tbid=@objid and b.fldi_fieldname=a.fldi_fieldname 
 )) 
order by fldi_id 
end