IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_flowwork]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_flowwork]
GO
CREATE procedure [dbo].[mps_save_flowwork] 
@wrkid int ,
@wrk_code varchar(20),
@wrk_title nvarchar(100),
@wrk_shape varchar(30),
@wrk_limited decimal(18,4),
@wrk_limitedcaption nvarchar(20),
@wrk_icons varchar(200),
@wrk_type int,
@wrk_keyparams varchar(300) ,
@wrk_executor_num int ,
@wrk_executor_group int ,
@wrk_exec_start_msgid int,
@wrk_exec_start_msg nvarchar(50),
@wrk_exec_end_msgid int,
@wrk_exec_end_msg nvarchar(50),
@wrk_exec_back_msgid int,
@wrk_exec_back_msg nvarchar(50),
@wrk_audit_start_msgid int,
@wrk_audit_start_msg nvarchar(50),
@wrk_audit_end_msgid int,
@wrk_audit_end_msg nvarchar(50),
@wrk_audit_back_msgid int,
@wrk_audit_back_msg nvarchar(50),
@wrk_coop_start_msgid int,
@wrk_coop_start_msg nvarchar(50),
@wrk_coop_end_msgid int,
@wrk_coop_end_msg nvarchar(50),
@wrk_coop_back_msgid int,
@wrk_coop_back_msg nvarchar(50),
@wrk_view_start_msgid int,
@wrk_view_start_msg nvarchar(50),
@wrk_view_end_msgid int,
@wrk_view_end_msg nvarchar(50),
@wrk_view_back_msgid int,
@wrk_view_back_msg nvarchar(50),
@wrk_cst1_source nvarchar(50),
@wrk_cst1_start_msgid int,
@wrk_cst1_start_msg nvarchar(50),
@wrk_cst1_end_msgid int,
@wrk_cst1_end_msg nvarchar(50),
@wrk_cst1_back_msgid int,
@wrk_cst1_back_msg nvarchar(50),
@wrk_cst2_source nvarchar(50),
@wrk_cst2_start_msgid int,
@wrk_cst2_start_msg nvarchar(50),
@wrk_cst2_end_msgid int,
@wrk_cst2_end_msg nvarchar(50),
@wrk_cst2_back_msgid int,
@wrk_cst2_back_msg nvarchar(50),
@wrk_emp1_source nvarchar(50),
@wrk_emp1_start_msgid int,
@wrk_emp1_start_msg nvarchar(50),
@wrk_emp1_end_msgid int,
@wrk_emp1_end_msg nvarchar(50),
@wrk_emp1_back_msgid int,
@wrk_emp1_back_msg nvarchar(50),
@wrk_emp2_source nvarchar(50),
@wrk_emp2_start_msgid int,
@wrk_emp2_start_msg nvarchar(50),
@wrk_emp2_end_msgid int,
@wrk_emp2_end_msg nvarchar(50) ,
@wrk_emp2_back_msgid int,
@wrk_emp2_back_msg nvarchar(50) ,
@wrk_emp3_source nvarchar(50),
@wrk_emp3_start_msgid int,
@wrk_emp3_start_msg nvarchar(50),
@wrk_emp3_end_msgid int,
@wrk_emp3_end_msg nvarchar(50) ,
@wrk_emp3_back_msgid int,
@wrk_emp3_back_msg nvarchar(50) ,
@wrk_start decimal(18,4),
@wrk_start_caption nvarchar(20) ,
@wrk_prevwrkids varchar(1000),
@wrk_status nvarchar(20),
@wrk_status_f nvarchar(20),
@wrk_status_ff nvarchar(20),
@wrk_status_r nvarchar(20),
@wrk_key nvarchar(20),
@wrk_key_f nvarchar(20),
@wrk_key_ff nvarchar(20),
@wrk_key_r nvarchar(20) ,
@wrk_log_start_msgid int,
@wrk_log_start_msg nvarchar(50),
@wrk_log_end_msgid int,
@wrk_log_end_msg nvarchar(50),
@wrk_log_back_msgid int,
@wrk_log_back_msg nvarchar(50),
@wrk_cst1_tag int,
@wrk_cst1_datasource int,
@wrk_cst2_tag int,
@wrk_cst2_datasource int,
@wrk_emp1_datasource int,
@wrk_emp2_datasource int,
@wrk_emp3_datasource int ,
@wrk_other_datasourceint,
@wrk_other_sourcenvarchar(50),
@wrk_other_start_msgidint,
@wrk_other_start_msgnvarchar(50),
@wrk_other_end_msgidint,
@wrk_other_end_msgnvarchar(50),
@wrk_other_back_msgidint,
@wrk_other_back_msgnvarchar(50) ,
@wrk_backstyle int 
as
begin
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
update mps_flowwork set 
wrk_code = case when @wrk_code='_none_' then wrk_code else @wrk_code end,
wrk_title = case when @wrk_title='_none_' then wrk_title else @wrk_title end,
wrk_shape = case when @wrk_shape='_none_' then wrk_shape else @wrk_shape end,
wrk_limited = case when @wrk_limited=-999999999 then wrk_limited else @wrk_limited end,
wrk_limitedcaption = case when @wrk_limitedcaption='_none_' then wrk_limitedcaption else @wrk_limitedcaption end,
wrk_icons = case when @wrk_icons='_none_' then wrk_icons else @wrk_icons end,
wrk_type = case when @wrk_type=-999999999 then wrk_type else @wrk_type end,
wrk_backstyle = case when @wrk_backstyle=-999999999 then wrk_backstyle else @wrk_backstyle end,
wrk_executor_num = case when @wrk_executor_num=-999999999 then wrk_executor_num else @wrk_executor_num end,
wrk_executor_group = case when @wrk_executor_group=-999999999 then wrk_executor_group else @wrk_executor_group end,
wrk_keyparams = case when @wrk_keyparams='_none_' then wrk_keyparams else @wrk_keyparams end ,
wrk_exec_start_msgid =case when @wrk_exec_start_msgid  = -999999999then wrk_exec_start_msgid  else @wrk_exec_start_msgidend ,
wrk_exec_start_msg =case when @wrk_exec_start_msg = '_none_'then wrk_exec_start_msg   else @wrk_exec_start_msg end ,
wrk_exec_end_msgid=case when @wrk_exec_end_msgid = -999999999then wrk_exec_end_msgidelse @wrk_exec_end_msgidend ,
wrk_exec_end_msg =case when @wrk_exec_end_msg = '_none_'then wrk_exec_end_msg   else @wrk_exec_end_msg   end ,
wrk_exec_back_msgid=case when @wrk_exec_back_msgid = -999999999then wrk_exec_back_msgidelse @wrk_exec_back_msgidend ,
wrk_exec_back_msg =case when @wrk_exec_back_msg = '_none_'then wrk_exec_back_msg   else @wrk_exec_back_msg   end ,
wrk_audit_start_msgid  =case when @wrk_audit_start_msgid = -999999999then wrk_audit_start_msgid else @wrk_audit_start_msgidend , 
wrk_audit_start_msg    =case when @wrk_audit_start_msg  = '_none_'then wrk_audit_start_msg   else @wrk_audit_start_msg   end ,
wrk_audit_end_msgid  =case when @wrk_audit_end_msgid   = -999999999then wrk_audit_end_msgid   else @wrk_audit_end_msgid   end ,
wrk_audit_end_msg =case when @wrk_audit_end_msg = '_none_'then wrk_audit_end_msg else @wrk_audit_end_msg   end ,
wrk_audit_back_msgid  =case when @wrk_audit_back_msgid   = -999999999then wrk_audit_back_msgid   else @wrk_audit_back_msgid   end ,
wrk_audit_back_msg =case when @wrk_audit_back_msg = '_none_'then wrk_audit_back_msg else @wrk_audit_back_msg   end ,
wrk_coop_start_msgid =case when @wrk_coop_start_msgid  = -999999999then wrk_coop_start_msgid  else @wrk_coop_start_msgidend ,
wrk_coop_start_msg =case when @wrk_coop_start_msg = '_none_'then wrk_coop_start_msg   else @wrk_coop_start_msg end ,
wrk_coop_end_msgid =case when @wrk_coop_end_msgid    = -999999999then wrk_coop_end_msgid   else @wrk_coop_end_msgid   end ,
wrk_coop_end_msg =case when @wrk_coop_end_msg   = '_none_'then wrk_coop_end_msg   else @wrk_coop_end_msg   end ,

wrk_coop_back_msgid =case when @wrk_coop_back_msgid    = -999999999then wrk_coop_back_msgid   else @wrk_coop_back_msgid   end ,
wrk_coop_back_msg =case when @wrk_coop_back_msg   = '_none_'then wrk_coop_back_msg   else @wrk_coop_back_msg   end ,
wrk_view_start_msgid =case when @wrk_view_start_msgid    = -999999999then wrk_view_start_msgid  else @wrk_view_start_msgidend ,
wrk_view_start_msg =case when @wrk_view_start_msg   = '_none_'then wrk_view_start_msg   else @wrk_view_start_msg   end ,
wrk_view_end_msgid =case when @wrk_view_end_msgid    = -999999999then wrk_view_end_msgid   else @wrk_view_end_msgid   end ,
wrk_view_end_msg =case when @wrk_view_end_msg   = '_none_'then wrk_view_end_msg   else @wrk_view_end_msg   end ,

wrk_view_back_msgid =case when @wrk_view_back_msgid    = -999999999then wrk_view_back_msgid   else @wrk_view_back_msgid   end ,
wrk_view_back_msg =case when @wrk_view_back_msg   = '_none_'then wrk_view_back_msg   else @wrk_view_back_msg   end ,
wrk_log_start_msgid =case when @wrk_log_start_msgid    = -999999999then wrk_log_start_msgid  else @wrk_log_start_msgidend ,
wrk_log_start_msg =case when @wrk_log_start_msg   = '_none_'then wrk_log_start_msg   else @wrk_log_start_msg   end ,
wrk_log_end_msgid =case when @wrk_log_end_msgid    = -999999999then wrk_log_end_msgid   else @wrk_log_end_msgid   end ,
wrk_log_end_msg =case when @wrk_log_end_msg   = '_none_'then wrk_log_end_msg   else @wrk_log_end_msg   end ,

wrk_log_back_msgid =case when @wrk_log_back_msgid    = -999999999then wrk_log_back_msgid   else @wrk_log_back_msgid   end ,
wrk_log_back_msg =case when @wrk_log_back_msg   = '_none_'then wrk_log_back_msg   else @wrk_log_back_msg   end ,
wrk_cst1_source =case when @wrk_cst1_source   = '_none_'then wrk_cst1_source else @wrk_cst1_source end ,
wrk_cst1_start_msgid =case when @wrk_cst1_start_msgid    = -999999999then wrk_cst1_start_msgid  else @wrk_cst1_start_msgid  end ,
wrk_cst1_start_msg =case when @wrk_cst1_start_msg = '_none_'then wrk_cst1_start_msg   else @wrk_cst1_start_msg   end ,
wrk_cst1_end_msgid =case when @wrk_cst1_end_msgid    = -999999999then wrk_cst1_end_msgid   else @wrk_cst1_end_msgid   end ,
wrk_cst1_end_msg =case when @wrk_cst1_end_msg   = '_none_'then wrk_cst1_end_msg   else @wrk_cst1_end_msg end ,

wrk_cst1_back_msgid =case when @wrk_cst1_back_msgid    = -999999999then wrk_cst1_back_msgid   else @wrk_cst1_back_msgid   end ,
wrk_cst1_back_msg =case when @wrk_cst1_back_msg   = '_none_'then wrk_cst1_back_msg   else @wrk_cst1_back_msg end ,
wrk_cst2_source =case when @wrk_cst2_source   = '_none_'then wrk_cst2_source else @wrk_cst2_source   end ,
wrk_cst2_start_msgid =case when @wrk_cst2_start_msgid    = -999999999then wrk_cst2_start_msgid  else @wrk_cst2_start_msgid  end ,
wrk_cst2_start_msg =case when @wrk_cst2_start_msg   = '_none_'then wrk_cst2_start_msg   else @wrk_cst2_start_msg   end ,
wrk_cst2_end_msgid =case when @wrk_cst2_end_msgid    = -999999999then wrk_cst2_end_msgid   else @wrk_cst2_end_msgid end ,
wrk_cst2_end_msg =case when @wrk_cst2_end_msg   = '_none_'then wrk_cst2_end_msg   else @wrk_cst2_end_msg   end ,

wrk_cst2_back_msgid =case when @wrk_cst2_back_msgid    = -999999999then wrk_cst2_back_msgid   else @wrk_cst2_back_msgid end ,
wrk_cst2_back_msg =case when @wrk_cst2_back_msg   = '_none_'then wrk_cst2_back_msg   else @wrk_cst2_back_msg   end ,
wrk_emp1_source =case when @wrk_emp1_source   = '_none_'then wrk_emp1_source else @wrk_emp1_source   end ,
wrk_emp1_start_msgid =case when @wrk_emp1_start_msgid  = -999999999then wrk_emp1_start_msgid  else @wrk_emp1_start_msgid  end ,
wrk_emp1_start_msg =case when @wrk_emp1_start_msg   = '_none_'then wrk_emp1_start_msg   else @wrk_emp1_start_msg end ,
wrk_emp1_end_msgid =case when @wrk_emp1_end_msgid    = -999999999then wrk_emp1_end_msgid   else @wrk_emp1_end_msgid   end ,
wrk_emp1_end_msg =case when @wrk_emp1_end_msg   = '_none_'then wrk_emp1_end_msg   else @wrk_emp1_end_msg   end ,

wrk_emp1_back_msgid =case when @wrk_emp1_back_msgid    = -999999999then wrk_emp1_back_msgid   else @wrk_emp1_back_msgid   end ,
wrk_emp1_back_msg =case when @wrk_emp1_back_msg   = '_none_'then wrk_emp1_back_msg   else @wrk_emp1_back_msg   end ,
wrk_emp2_source =case when @wrk_emp2_source   = '_none_'then wrk_emp2_source else @wrk_emp2_source   end ,
wrk_emp2_start_msgid =case when @wrk_emp2_start_msgid    = -999999999then wrk_emp2_start_msgid  else @wrk_emp2_start_msgidend ,
wrk_emp2_start_msg =case when @wrk_emp2_start_msg   = '_none_'then wrk_emp2_start_msg   else @wrk_emp2_start_msg   end ,
wrk_emp2_end_msgid =case when @wrk_emp2_end_msgid    = -999999999then wrk_emp2_end_msgid   else @wrk_emp2_end_msgid   end ,
wrk_emp2_end_msg =case when @wrk_emp2_end_msg   = '_none_'then wrk_emp2_end_msg   else @wrk_emp2_end_msg   end ,

wrk_emp2_back_msgid =case when @wrk_emp2_back_msgid    = -999999999then wrk_emp2_back_msgid   else @wrk_emp2_back_msgid   end ,
wrk_emp2_back_msg =case when @wrk_emp2_back_msg   = '_none_'then wrk_emp2_back_msg   else @wrk_emp2_back_msg   end ,
wrk_emp3_source =case when @wrk_emp3_source   = '_none_'then wrk_emp3_source else @wrk_emp3_source   end ,
wrk_emp3_start_msgid =case when @wrk_emp3_start_msgid    = -999999999then wrk_emp3_start_msgid  else @wrk_emp3_start_msgidend ,
wrk_emp3_start_msg =case when @wrk_emp3_start_msg   = '_none_'then wrk_emp3_start_msg   else @wrk_emp3_start_msg   end ,
wrk_emp3_end_msgid =case when @wrk_emp3_end_msgid    = -999999999then wrk_emp3_end_msgid   else @wrk_emp3_end_msgid   end ,
wrk_emp3_end_msg =case when @wrk_emp3_end_msg   = '_none_'then wrk_emp3_end_msg   else @wrk_emp3_end_msg   end ,

wrk_emp3_back_msgid =case when @wrk_emp3_back_msgid    = -999999999then wrk_emp3_back_msgid   else @wrk_emp3_back_msgid   end ,
wrk_emp3_back_msg =case when @wrk_emp3_back_msg   = '_none_'then wrk_emp3_back_msg   else @wrk_emp3_back_msg   end ,

wrk_start =case when @wrk_start    = -999999999then wrk_start   else @wrk_start   end ,
wrk_start_caption =case when @wrk_start_caption   = '_none_'then wrk_start_caption   else @wrk_start_caption   end    ,
wrk_prevwrkids =case when @wrk_prevwrkids   = '_none_'then wrk_prevwrkids   else @wrk_prevwrkids   end    ,
wrk_status=case when @wrk_status='_none_' then wrk_status else @wrk_status end ,
wrk_status_f=case when @wrk_status_f='_none_' then wrk_status_f else @wrk_status_f end ,
wrk_status_ff=case when @wrk_status_ff='_none_' then wrk_status_ff else @wrk_status_ff end ,
wrk_status_r=case when @wrk_status_r='_none_' then wrk_status_r else @wrk_status_r end ,
wrk_key=case when @wrk_key='_none_' then wrk_key else @wrk_key end ,
wrk_key_f=case when @wrk_key_f='_none_' then wrk_key_f else @wrk_key_f end ,
wrk_key_ff=case when @wrk_key_ff='_none_' then wrk_key_ff else @wrk_key_ff end ,
wrk_key_r=case when @wrk_key_r='_none_' then wrk_key_r else @wrk_key_r end   ,
wrk_cst1_tag =case when @wrk_cst1_tag    = -999999999then wrk_cst1_tag   else @wrk_cst1_tag   end ,
wrk_cst2_tag =case when @wrk_cst2_tag    = -999999999then wrk_cst2_tag   else @wrk_cst2_tag   end ,
wrk_cst1_datasource =case when @wrk_cst1_datasource    = -999999999then wrk_cst1_datasource   else @wrk_cst1_datasource   end ,
wrk_cst2_datasource =case when @wrk_cst2_datasource    = -999999999then wrk_cst2_datasource   else @wrk_cst2_datasource   end ,
wrk_emp1_datasource =case when @wrk_emp1_datasource    = -999999999then wrk_emp1_datasource   else @wrk_emp1_datasource   end ,
wrk_emp2_datasource =case when @wrk_emp2_datasource    = -999999999then wrk_emp2_datasource   else @wrk_emp2_datasource   end ,
wrk_emp3_datasource =case when @wrk_emp3_datasource    = -999999999then wrk_emp3_datasource   else @wrk_emp3_datasource   end  ,
wrk_changed = 0 ,
wrk_other_datasource =case when @wrk_other_datasource    = -999999999then wrk_other_datasource   else @wrk_other_datasource   end  ,
wrk_other_back_msgid =case when @wrk_other_back_msgid    = -999999999then wrk_other_back_msgid   else @wrk_other_back_msgid   end ,
wrk_other_back_msg =case when @wrk_other_back_msg   = '_none_'then wrk_other_back_msg   else @wrk_other_back_msg   end ,
wrk_other_source =case when @wrk_other_source   = '_none_'then wrk_other_source else @wrk_other_source   end ,
wrk_other_start_msgid =case when @wrk_other_start_msgid    = -999999999then wrk_other_start_msgid  else @wrk_other_start_msgidend ,
wrk_other_start_msg =case when @wrk_other_start_msg   = '_none_'then wrk_other_start_msg   else @wrk_other_start_msg   end ,
wrk_other_end_msgid =case when @wrk_other_end_msgid    = -999999999then wrk_other_end_msgid   else @wrk_other_end_msgid   end ,
wrk_other_end_msg =case when @wrk_other_end_msg   = '_none_'then wrk_other_end_msg   else @wrk_other_end_msg   end  

   where wrk_id = @wrkid 
   
   declare @cmpid int 
   select top 1 @cmpid=biz_cmpid from mps_biztype with(nolock) , mps_flowwork with(nolock) where biz_id=wrk_bizid and wrk_id=@wrkid 
   update mps_flowwork with(rowlock) set wrk_keyparams = ',nodup,' where wrk_id=@wrkid and wrk_shape not like 'begin%' and wrk_keyparams='' 
   update mps_flowwork with(rowlock) set wrk_keyparams = ',genordno,' where wrk_id=@wrkid and wrk_shape  like 'begin%' and wrk_keyparams=''  
   if (not exists (select 1 from mps_permission with(nolock) where mp_type='wrkexecutor' and mp_ctgid=@wrkid and mp_cmpid=@cmpid ))
   begin
    insert into mps_permission(mp_cmpid,mp_type,mp_ctgid,mp_group,mp_ppid,mp_objid1,mp_objid2,mp_caption)
 values(@cmpid,'wrkexecutor',@wrkid,0,401,0,0,'创建人') 
end  
 
end