IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_bizcomefrom]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_bizcomefrom]
GO
 
CREATE procedure [dbo].[mps_save_bizcomefrom] 
@bsid int,
@bs_popwin int,
@bs_sql nvarchar(4000),
@bs_ord2tag int,
@bs_newviaord2 int,
@scrmmc nvarchar(100),
@tag2mc nvarchar(100) ,
@bs_displayfield nvarchar(4000),
@bs_orderfields varchar(200),
@bs_wintitle nvarchar(100),
@bs_width varchar(20),
@bs_js nvarchar(4000) ,
@bs_prevsql nvarchar(max) ,
@bs_tables nvarchar(1000) ,
@bs_where nvarchar(4000) 
as
begin
declare @bsord2tag int 
set @bsord2tag = @bs_ord2tag 
set @bsord2tag = ISNULL(@bsord2tag,-999999999) 
if (@bs_newviaord2=0) 
begin
set @bsord2tag = 0 
end 
update mps_biztype_comefrom 
set bs_popwin = case when @bs_popwin=-999999999 then bs_popwin else @bs_popwin end ,
bs_ord2tag = case when @bsord2tag=-999999999 then bs_ord2tag else @bsord2tag end ,
bs_newviaord2 = case when @bs_newviaord2=-999999999 then bs_newviaord2 else @bs_newviaord2 end ,
bs_sql = case when @bs_sql='_none_' then bs_sql else @bs_sql end ,
bs_scaption = case when @scrmmc='_none_' then bs_scaption else @scrmmc end ,
bs_displayfield = case when @bs_displayfield='_none_' then bs_displayfield else @bs_displayfield end ,
bs_orderfields = case when @bs_orderfields='_none_' then bs_orderfields else @bs_orderfields end ,
bs_wintitle = case when @bs_wintitle='_none_' then bs_wintitle else @bs_wintitle end ,
bs_width = case when @bs_width='_none_' then bs_width else @bs_width end ,
bs_js = case when @bs_js='_none_' then bs_js else @bs_js end ,
bs_prevsql = case when @bs_prevsql='_none_' then bs_prevsql else @bs_prevsql end ,
bs_tables = case when @bs_tables='_none_' then bs_tables else @bs_tables end ,
bs_where = case when @bs_where='_none_' then bs_where else @bs_where end ,
bs_scaption_detail = case when @tag2mc='_none_' then bs_scaption_detail else @tag2mc end  
   where bs_id=ABS(@bsid) 
   
   declare @bizid int 
   select top 1 @bizid = bs_bizid from mps_biztype_comefrom where bs_id=ABS(@bsid)  
   declare @cmpid int 
   select top 1 @cmpid = biz_cmpid from mps_biztype where biz_id=@bizid 
   -- fix data 
   -- 看看有没有  bs_newviaord2=0 bs_ord2tag>0 的情况
   declare @t table(bsid int , ord2tag int ) 
   insert into @t(bsid) 
   select bs_id from mps_biztype_comefrom 
   where bs_bizid=@bizid and bs_newviaord2=0 and bs_ord2tag>0 
   update mps_biztype_comefrom set bs_ord2tag=0 from @t where bs_id=bsid 
   update mps_biztype_comefrom_fieldmap set bsf_use=1 from @t where bsf_bsid=bsid 
   
   -- 看看有没, bs_newviaord2>0 bs_ord2tag=0  的情况 
   delete @t 
   insert into @t(bsid,ord2tag) 
   select bs_id,0 from mps_biztype_comefrom 
   where bs_bizid=@bizid and bs_newviaord2>0 and bs_ord2tag=0 and bs_sord2tag>0 
   
   declare @tt table(ord2 int)
   insert into @tt(ord2) 
   select distinct biz2_tag 
   from mps_biztype2 
   where biz2_bizid=@bizid  and biz2_tag>0 
   
   delete @tt 
   from mps_biztype_comefrom 
   where bs_bizid=@bizid and bs_ord2tag=ord2  
   
   
   update @t set ord2tag = ord2 from @tt 
   
   
   update mps_biztype_comefrom set bs_ord2tag=ord2tag from @t where bs_id=bsid 
   update mps_biztype_comefrom_fieldmap set bsf_use=1 from @t where bsf_bsid=bsid 
   
   declare @ord2t int 
   select top 1 @ord2t = biz2_tag from mps_biztype2 where biz2_bizid=@bizid and biz2_tag>0 
   set @ord2t = isnull(@ord2t,0) 
   update mps_biztype_comefrom set bs_ord2tag=@ord2t where bs_bizid=@bizid and ((bs_ord2tag is null ) or (bs_ord2tag=0)) and bs_sord2tag>0 and  bs_newviaord2>0 
   
   if (@bsid>0)  update  mps_biztype_comefrom_fieldmap set bsf_use=1 where  bsf_bsid=@bsid   
   exec mps_calc_bizrelation @cmpid , @bizid 
end