IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_flowchart]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_flowchart]
GO
 
CREATE procedure [dbo].[mps_gen_flowchart] 
@crmid int ,
@flwid int ,
@newnodes nvarchar(4000),
@newlines nvarchar(4000),
@nodes nvarchar(4000),
@lines nvarchar(4000)  
as
begin
    set nocount on
    declare @ic int 
    set @ic = @flwid 
declare @s nvarchar(4000) 
declare @i int  
declare @s1 nvarchar(500) 
declare @s2 nvarchar(500) 
declare @flag varchar(10) 
declare @wrkid varchar(10) 
declare @swrkid varchar(10) 
declare @twrkid varchar(10) 
declare @toid varchar(10) 
declare @order varchar(10) 
declare @text1 nvarchar(100) 
declare @newwrkid int 
declare @cmpid int 
select @cmpid=biz_cmpid from mps_biztype with(nolock) where biz_id=@crmid 

declare @node table(flag varchar(10), wrkid varchar(10),orderi int , text1 nvarchar(100), newwrkid int) 
declare @line table(flag varchar(10), wrkid varchar(10),orderi int , text1 nvarchar(100), swrkid varchar(10),twrkid varchar(10),toid varchar(10),newwrkid int , isnew int) 

set @s = @newlines 
if (@s='_none_') set @s=''
while (@s>'') 
begin
  set @ic =  @ic + 1 
  set @i = charindex('|',@s) 
  if (@i>0) 
  begin
   set @s1 = substring(@s,1,@i-1) 
   set @s = substring(@s , @i+1 , len(@s) -  @i) 
  end else 
  begin
  set @s1 = @s 
  set @s = '' 
  end 
  if (@s1>'') 
  begin
   -- flag,wrkid,order,swrkid,twrkid,toid,text 
   set @i  = charindex(',',@s1) 
   set @flag = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @wrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @order = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @swrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @twrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @toid = substring(@s1,1,@i-1) 
   set @text1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   -- 生成记录
   insert into mps_flowworkstatus(ws_swrkid,ws_twrkid,ws_caption) 
   select @ic , @ic+1 ,  @text1  
   
   select top 1 @newwrkid = ws_id from mps_flowworkstatus where ws_swrkid=@ic and ws_twrkid=@ic+1 and ws_caption=@text1 order by ws_id desc 
   set @newwrkid = isnull(@newwrkid,0) 
   if (@newwrkid>0) 
   begin
   insert into @line(flag,wrkid,orderi,text1,swrkid,twrkid,toid,newwrkid,isnew)
 values(@flag , @wrkid , convert(int,@order),@text1,@swrkid,@twrkid,@toid,@newwrkid,0) 
 end  
  end 
end 


-- 重新计算以前的line 
declare @oldline table(wrkid int , swrkid varchar(10),twrkid varchar(10),toid varchar(10),text1 nvarchar(100)) 
set @s = @lines 
if (@s='_none_') set @s=''
while (@s>'') 
begin
  set @i = charindex('|',@s) 
  if (@i>0) 
  begin
   set @s1 = substring(@s,1,@i-1) 
   set @s = substring(@s , @i+1 , len(@s) -  @i) 
  end else 
  begin
  set @s1 = @s 
  set @s = '' 
  end 
  if (@s1>'') 
  begin
   -- wrkid,swrkid,twrkid,toid,text1
   set @i  = charindex(',',@s1) 
   set @wrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   set @i  = charindex(',',@s1) 
   set @swrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   set @i  = charindex(',',@s1) 
   set @twrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   set @i  = charindex(',',@s1) 
   set @toid = substring(@s1,1,@i-1) 
   set @text1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   insert into @oldline(wrkid,swrkid,twrkid,toid,text1)
 values(@wrkid , @swrkid,@twrkid,@toid,@text1) 
 insert into @line(flag,wrkid,orderi,text1,swrkid,twrkid,toid,newwrkid,isnew)
 values('l',@wrkid ,0, @text1,@swrkid,@twrkid,@toid,@wrkid,1) 
  end 
end 

-- 
set @s = @newnodes 
if (@s='_none_') set @s=''
while (@s>'') 
begin
  set @i = charindex('|',@s) 
  if (@i>0) 
  begin
   set @s1 = substring(@s,1,@i-1) 
   set @s = substring(@s , @i+1 , len(@s) -  @i) 
  end else 
  begin
  set @s1 = @s 
  set @s = '' 
  end 
  if (@s1>'') 
  begin
   -- flag,wrkid,order,text 
   set @i  = charindex(',',@s1) 
   set @flag = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @wrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @order = substring(@s1,1,@i-1) 
   set @text1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
   -- 生成记录
   insert into mps_flowwork(wrk_bizid,wrk_flwid,wrk_order,wrk_title,wrk_shape) 
   select @crmid , @flwid , convert(int,@order) , @text1 , case when @flag='e' then 'end' when @wrkid='begin' then 'begin' else '' end 
   select top 1 @newwrkid = wrk_id from mps_flowwork where wrk_bizid=@crmid and wrk_flwid=@flwid and wrk_order=convert(int,@order) and wrk_title=@text1 order by wrk_id desc 
   set @newwrkid = isnull(@newwrkid,0) 
   if (@newwrkid>0) 
   begin
   insert into @node(flag,wrkid,orderi,text1,newwrkid)
   values(@flag , @wrkid , convert(int,@order),@text1,@newwrkid) 
   update @line set swrkid=ltrim(rtrim(str(@newwrkid))) where swrkid=@wrkid 
   update @line set twrkid=ltrim(rtrim(str(@newwrkid))) where twrkid=@wrkid 
   update @line set toid=ltrim(rtrim(str(@newwrkid))) where toid=@wrkid 
 end  
 -- 对 @newwrkid 进行一些初始化
 update mps_flowwork with(rowlock) set wrk_keyparams = ',nodup,' where wrk_id=@newwrkid and wrk_shape not like 'begin%' 
 update mps_flowwork with(rowlock) set wrk_keyparams = ',genordno,' where wrk_id=@newwrkid and wrk_shape  like 'begin%' 
 insert into mps_permission(mp_cmpid,mp_type,mp_ctgid,mp_group,mp_ppid,mp_objid1,mp_objid2,mp_caption)
 values(@cmpid,'wrkexecutor',@newwrkid,0,401,0,0,'创建人') 
  end 
end

 
-- 重新计算以前的 node
declare @oldnode table(flag varchar(10),wrkid int , orderi int,text1 nvarchar(100)) 
set @s = @nodes 
if (@s='_none_') set @s=''
while (@s>'') 
begin
  set @i = charindex('|',@s) 
  if (@i>0) 
  begin
   set @s1 = substring(@s,1,@i-1) 
   set @s = substring(@s , @i+1 , len(@s) -  @i) 
  end else 
  begin
  set @s1 = @s 
  set @s = '' 
  end 
  if (@s1>'') 
  begin
   -- flag,wrkid,order,text 
   set @s2=@s1 
   set @i  = charindex(',',@s1) 
   set @flag = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @wrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   if (@i<=0) set @s1='t,'+@s2 else set @s1 = @s2 
   -- 重新来
   set @i  = charindex(',',@s1) 
   set @flag = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   set @wrkid = substring(@s1,1,@i-1) 
   set @s1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   set @i  = charindex(',',@s1) 
   if (@i>0) 
   begin
  set @order = substring(@s1,1,@i-1) 
  set @text1 = substring(@s1 ,  @i + 1 , len(@s1) - @i) 
   
  insert into @oldnode(flag,wrkid,orderi,text1)
values(@flag , @wrkid , convert(int,@order),@text1) 
 end  
  end 
end 

--纠正，有wrkid但没有数据的
declare @badwrkid int 
set @badwrkid = 0
declare @badwrkid2 int 
set @badwrkid2 = 0
while (@badwrkid=@badwrkid)
begin
set @badwrkid = 0
select top 1 @badwrkid=wrkid 
from @oldnode 
where not exists (select 1 from mps_flowwork where wrkid=wrk_id)
if (@badwrkid=0) break 
set identity_insert mps_flowwork on
insert into mps_flowwork(wrk_id,wrk_bizid,wrk_flwid,wrk_order,wrk_title,wrk_shape) 
  select distinct @badwrkid , @crmid , @flwid , orderi , text1 , case when flag='e' then 'end'  else '' end 
  from @oldnode where wrkid=@badwrkid 
  set identity_insert mps_flowwork off
  
end
 
-- 纠正以前的LINE 
--  @oldline table(wrkid int , swrkid varchar(10),twrkid varchar(10),toid varchar(10),text1 nvarchar(100)) 
declare @oldline2 table(wrkid int , swrkid varchar(10),twrkid varchar(10),toid varchar(10),text1 nvarchar(100)) 
insert into @oldline2(wrkid , swrkid ,twrkid ,toid ,text1)
select wrkid , swrkid ,twrkid ,toid ,text1 from @oldline
where SUBSTRING(swrkid,1,1)<='9' and SUBSTRING(twrkid,1,1)<='9'
set @badwrkid = 0
set @badwrkid2 = 0
while (@badwrkid=@badwrkid)
begin
set @badwrkid = 0
set @badwrkid2 = 0
select top 1 @badwrkid=swrkid , @badwrkid2=twrkid from @oldline2 
where not exists (select 1 from  mps_flowworkstatus where swrkid=ws_swrkid and twrkid=ws_twrkid) 
if (@badwrkid>0 and @badwrkid2>0) 
begin
set @newwrkid = 0
--set identity_insert mps_flowworkstatus on
insert into mps_flowworkstatus(ws_swrkid,ws_twrkid,ws_caption) 
  select distinct swrkid ,twrkid ,  text1   
  from @oldline2 where swrkid=@badwrkid and twrkid=@badwrkid2 and (not exists (select 1 from  mps_flowworkstatus where swrkid=ws_swrkid and twrkid=ws_twrkid))
  --set identity_insert mps_flowworkstatus off
end else break 
end 

 
-- 修改以前的node 
update mps_flowwork set wrk_order = orderi , wrk_title=text1  
from @oldnode where wrk_id=wrkid 

-- 删除不存在的node
declare @t table(nid int) 
insert into @t(nid) select wrkid from @oldnode 
insert into @t(nid) select newwrkid from @node 
delete mps_flowwork  where wrk_flwid=@flwid and wrk_bizid=@crmid and (not exists (select * from @t where nid=wrk_id)) 

-- 修改
update mps_flowworkstatus set  ws_caption=text1 , ws_swrkid=convert(int,swrkid)  ,ws_twrkid=convert(int,twrkid) 
from @line where ws_id=newwrkid 

-- 删除不存在的

delete mps_flowworkstatus  from mps_flowwork 
where wrk_flwid=@flwid and wrk_bizid=@crmid and (wrk_id=ws_swrkid or wrk_id=ws_twrkid) and 
(not exists (select * from @line where newwrkid=ws_id )) 

select * from @node order by orderi  
select * from @line where isnew=0 order by orderi  
    -- 
update mps_flowworkstatus set ws_tshape=b.wrk_shape 
from mps_flowwork a , mps_flowwork b 
where a.wrk_bizid=@crmid and a.wrk_flwid=@flwid and ws_swrkid=a.wrk_id and 
ws_twrkid=b.wrk_id 
    
     
    set nocount off
end