IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_up_workstatus]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_up_workstatus]
GO
CREATE procedure [dbo].[mps_up_workstatus] 
@wsid int,
@swrkid int,
@twrkid int,
@nodetitle nvarchar(50),
@condition nvarchar(4000),
@ws_pickvalues nvarchar(1000),
@ws_status nvarchar(20),
@ws_status_f nvarchar(20),
@ws_status_ff nvarchar(20),
@ws_status_r nvarchar(20),
@ws_key nvarchar(20),
@ws_key_f nvarchar(20),
@ws_key_ff nvarchar(20),
@ws_key_r nvarchar(20) ,
@ws_prevwrkids varchar(1000),
@ws_fjbstatus nvarchar(20) ,
@ws_accsource int,
@ws_acctype varchar(10),
@ws_accdate varchar(50),
@ws_accobjtype varchar(10),
@ws_accobj varchar(50),
@ws_accfid varchar(50),
@ws_accmethod int ,
@ws_accvalue varchar(50),
@ws_accsuspend varchar(50),
@ws_accsql nvarchar(1000) ,
@ws_tosubflow int ,
@ws_whsourceint,
@ws_whprdtagint,
@ws_whprdvarchar(50),
@ws_whunitvarchar(50),
@ws_whbatchvarchar(50),
@ws_whbarn1varchar(50),
@ws_whbarn2varchar(50),
@ws_whmethodint,
@ws_whvaluevarchar(50),
@ws_whprevvarchar(50),
@ws_whpricevarchar(50),
@ws_whlocationvarchar(50),
@ws_whnotevarchar(50),
@ws_whexpirevarchar(50),
@ws_whsqlnvarchar(1000) ,
@ws_exec_end_msgidint,
@ws_exec_end_msgnvarchar(50),
@ws_audit_end_msgidint,
@ws_audit_end_msgnvarchar(50),
@ws_coop_end_msgidint,
@ws_coop_end_msgnvarchar(50),
@ws_view_end_msgidint,
@ws_view_end_msgnvarchar(50),
@ws_log_end_msgidint,
@ws_log_end_msgnvarchar(50),
@ws_cst1_tagint,
@ws_cst1_datasourceint,
@ws_cst1_sourcenvarchar(50),
@ws_cst1_end_msgidint,
@ws_cst1_end_msgnvarchar(50),
@ws_cst2_tagint,
@ws_cst2_datasourceint,
@ws_cst2_sourcenvarchar(50),
@ws_cst2_end_msgidint,
@ws_cst2_end_msgnvarchar(50),
@ws_emp1_datasourceint,
@ws_emp1_sourcenvarchar(50),
@ws_emp1_end_msgidint,
@ws_emp1_end_msgnvarchar(50),
@ws_emp2_datasourceint,
@ws_emp2_sourcenvarchar(50),
@ws_emp2_end_msgidint,
@ws_emp2_end_msgnvarchar(50),
@ws_emp3_datasourceint,
@ws_emp3_sourcenvarchar(50),
@ws_emp3_end_msgidint,
@ws_emp3_end_msgnvarchar(50),
@ws_other_datasourceint,
@ws_other_sourcenvarchar(50),
@ws_other_end_msgidint,
@ws_other_end_msgnvarchar(50) 
 as
begin
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @wsid1 int 
if (@swrkid>0 and @twrkid>0) 
begin
select top 1 @wsid1=ws_id from mps_flowworkstatus where ws_id=@wsid and ws_swrkid=@swrkid and ws_twrkid=@twrkid 
end else 
begin
select top 1 @wsid1=ws_id , @swrkid= ws_swrkid  from mps_flowworkstatus where ws_id=@wsid  
end
set @wsid1 = isnull(@wsid1,0) 
if (@wsid1<=0) select top 1 @wsid1=ws_id  , @swrkid= ws_swrkid  from mps_flowworkstatus where  ws_swrkid=@swrkid and ws_twrkid=@twrkid 
    set @wsid1 = isnull(@wsid1,0) 
if (@wsid1<=0) 
begin
insert into mps_flowworkstatus(ws_swrkid,ws_twrkid,ws_caption,ws_condition,ws_prevwrkids) 
values(@swrkid,@twrkid,@nodetitle,@condition,@ws_prevwrkids) 
select top 1 @wsid1=ws_id from mps_flowworkstatus where ws_swrkid=@swrkid and ws_twrkid=@twrkid and ws_caption=@nodetitle   order by ws_id desc 
end else 
begin
update mps_flowworkstatus  set 
ws_swrkid=case when @swrkid=-999999999 then  ws_swrkid else @swrkid end , 
ws_twrkid=case when @twrkid=-999999999 then    ws_twrkid else @twrkid end, 
ws_caption=case when @nodetitle='_none_' then ws_caption else  @nodetitle end , 
ws_condition=case when @condition='_none_' then ws_condition else @condition end ,
ws_pickvalues=case when @ws_pickvalues='_none_' then ws_pickvalues else @ws_pickvalues end ,
ws_status=case when @ws_status='_none_' then ws_status else @ws_status end ,
ws_status_f=case when @ws_status_f='_none_' then ws_status_f else @ws_status_f end ,
ws_status_ff=case when @ws_status_ff='_none_' then ws_status_ff else @ws_status_ff end ,
ws_status_r=case when @ws_status_r='_none_' then ws_status_r else @ws_status_r end ,
ws_key=case when @ws_key='_none_' then ws_key else @ws_key end ,
ws_key_f=case when @ws_key_f='_none_' then ws_key_f else @ws_key_f end ,
ws_key_ff=case when @ws_key_ff='_none_' then ws_key_ff else @ws_key_ff end ,
ws_key_r=case when @ws_key_r='_none_' then ws_key_r else @ws_key_r end  ,
ws_prevwrkids=case when @ws_prevwrkids='_none_' then ws_prevwrkids else @ws_prevwrkids end ,
ws_fjbstatus=case when @ws_fjbstatus='_none_' then ws_fjbstatus else @ws_fjbstatus end  ,
ws_accsource =case when @ws_accsource =-999999999 then  ws_accsource else @ws_accsource end , 
ws_acctype  =case when @ws_acctype='_none_' then ws_acctype else @ws_acctype end , 
ws_accdate  =case when @ws_accdate='_none_' then ws_accdate else @ws_accdate end , 
ws_accobjtype  =case when @ws_accobjtype='_none_' then ws_accobjtype else @ws_accobjtype end , 
ws_accobj  =case when @ws_accobj='_none_' then ws_accobj else @ws_accobj end , 
ws_accfid  =case when @ws_accfid='_none_' then ws_accfid else @ws_accfid end , 
ws_accmethod  =case when @ws_accmethod =-999999999 then  ws_accmethod else @ws_accmethod end , 
ws_accvalue  =case when @ws_accvalue='_none_' then ws_accvalue else @ws_accvalue end , 
ws_accsuspend =case when @ws_accsuspend='_none_' then ws_accsuspend else @ws_accsuspend end , 
ws_accsql  =case when @ws_accsql='_none_' then ws_accsql else @ws_accsql end  ,
ws_tosubflow=case when @ws_tosubflow=-999999999 then    ws_tosubflow else @ws_tosubflow end ,

ws_whprdtag =case when @ws_whprdtag =-999999999 then  ws_whprdtag else @ws_whprdtag end , 
ws_whsource =case when @ws_whsource =-999999999 then  ws_whsource else @ws_whsource end , 
ws_whsql  =case when @ws_whsql='_none_' then ws_whsql else @ws_whsql end  ,

ws_whprd=case when @ws_whprd='_none_' then ws_whprd else @ws_whprd end , 
ws_whunit=case when @ws_whunit='_none_' then ws_whunit else @ws_whunit end , 
ws_whbatch=case when @ws_whbatch='_none_' then ws_whbatch else @ws_whbatch end , 
ws_whbarn1=case when @ws_whbarn1='_none_' then ws_whbarn1 else @ws_whbarn1 end , 
ws_whbarn2=case when @ws_whbarn2='_none_' then ws_whbarn2 else @ws_whbarn2 end , 
ws_whmethod=case when @ws_whmethod=-999999999 then ws_whmethod else @ws_whmethod end , 
ws_whvalue=case when @ws_whvalue='_none_' then ws_whvalue else @ws_whvalue end , 
ws_whprev=case when @ws_whprev='_none_' then ws_whprev else @ws_whprev end , 
ws_whprice=case when @ws_whprice='_none_' then ws_whprice else @ws_whprice end , 
ws_whlocation=case when @ws_whlocation='_none_' then ws_whlocation else @ws_whlocation end , 
ws_whexpire=case when @ws_whexpire='_none_' then ws_whexpire else @ws_whexpire end , 
ws_whnote=case when @ws_whnote='_none_' then ws_whnote else @ws_whnote end  ,

ws_exec_end_msgid =case when @ws_exec_end_msgid=-999999999  then ws_exec_end_msgidelse   @ws_exec_end_msgid   end ,
ws_exec_end_msg    =case when @ws_exec_end_msg    ='_none_'   then ws_exec_end_msg  else   @ws_exec_end_msg     end , 
ws_audit_end_msgid  =case when @ws_audit_end_msgid=-999999999 then ws_audit_end_msgid else @ws_audit_end_msgid end ,
ws_audit_end_msg    =case when @ws_audit_end_msg  ='_none_'   then ws_audit_end_msg  else   @ws_audit_end_msg end ,  
ws_coop_end_msgid  =case when @ws_coop_end_msgid  =-999999999 then ws_coop_end_msgid  else @ws_coop_end_msgid   end ,   
ws_coop_end_msg    =case when @ws_coop_end_msg    ='_none_'   then ws_coop_end_msg  else   @ws_coop_end_msg     end ,  
ws_view_end_msgid  =case when @ws_view_end_msgid  =-999999999 then ws_view_end_msgidelse   @ws_view_end_msgid   end ,
ws_view_end_msg    =case when @ws_view_end_msg    ='_none_'   then ws_view_end_msg  else   @ws_view_end_msg     end ,  
ws_log_end_msgid    =case when @ws_log_end_msgid  =-999999999 then ws_log_end_msgid  else   @ws_log_end_msgid end ,   
ws_log_end_msg      =case when @ws_log_end_msg    ='_none_'   then ws_log_end_msg    else   @ws_log_end_msg   end ,   
ws_cst1_tag        =case when @ws_cst1_tag        =-999999999 then ws_cst1_tag      else   @ws_cst1_tag         end ,
ws_cst1_datasource   =case when @ws_cst1_datasource  =-999999999 then ws_cst1_datasource else   @ws_cst1_datasource  end ,  
ws_cst1_source      =case when @ws_cst1_source    ='_none_'   then ws_cst1_source    else   @ws_cst1_source   end ,   
ws_cst1_end_msgid    =case when @ws_cst1_end_msgid   =-999999999 then ws_cst1_end_msgid  else   @ws_cst1_end_msgid   end ,  
ws_cst1_end_msg    =case when @ws_cst1_end_msg    ='_none_'   then ws_cst1_end_msg  else   @ws_cst1_end_msg     end ,
ws_cst2_tag        =case when @ws_cst2_tag        =-999999999 then ws_cst2_tag      else   @ws_cst2_tag         end ,  
ws_cst2_datasource   =case when @ws_cst2_datasource  =-999999999 then ws_cst2_datasource else   @ws_cst2_datasource  end ,  
ws_cst2_source      =case when @ws_cst2_source    ='_none_'   then ws_cst2_source    else   @ws_cst2_source   end ,   
ws_cst2_end_msgid    =case when @ws_cst2_end_msgid   =-999999999 then ws_cst2_end_msgid  else   @ws_cst2_end_msgid   end ,
ws_cst2_end_msg    =case when @ws_cst2_end_msg    ='_none_'   then ws_cst2_end_msg  else   @ws_cst2_end_msg     end ,  
ws_emp1_datasource   =case when @ws_emp1_datasource  =-999999999 then ws_emp1_datasource else   @ws_emp1_datasource  end ,  
ws_emp1_source      =case when @ws_emp1_source    ='_none_'   then ws_emp1_source    else   @ws_emp1_source   end ,   
ws_emp1_end_msgid    =case when @ws_emp1_end_msgid   =-999999999 then ws_emp1_end_msgid  else   @ws_emp1_end_msgid   end ,
ws_emp1_end_msg    =case when @ws_emp1_end_msg    ='_none_'   then ws_emp1_end_msg  else   @ws_emp1_end_msg     end ,  
ws_emp2_datasource   =case when @ws_emp2_datasource  =-999999999 then ws_emp2_datasource else   @ws_emp2_datasource  end ,  
ws_emp2_source      =case when @ws_emp2_source    ='_none_'   then ws_emp2_source    else   @ws_emp2_source   end ,   
ws_emp2_end_msgid    =case when @ws_emp2_end_msgid   =-999999999 then ws_emp2_end_msgid  else   @ws_emp2_end_msgid   end ,
ws_emp2_end_msg    =case when @ws_emp2_end_msg    ='_none_'   then ws_emp2_end_msg  else   @ws_emp2_end_msg     end ,  
ws_emp3_datasource   =case when @ws_emp3_datasource  =-999999999 then ws_emp3_datasource else   @ws_emp3_datasource  end ,  
ws_emp3_source      =case when @ws_emp3_source    ='_none_'   then ws_emp3_source    else   @ws_emp3_source   end ,   
ws_emp3_end_msgid  =case when @ws_emp3_end_msgid  =-999999999 then ws_emp3_end_msgidelse   @ws_emp3_end_msgid   end ,
ws_emp3_end_msg    =case when @ws_emp3_end_msg    ='_none_'   then ws_emp3_end_msg  else   @ws_emp3_end_msg     end ,  
ws_other_datasource  =case when @ws_other_datasource =-999999999 then ws_other_datasource else  @ws_other_datasource end ,  
ws_other_source    =case when @ws_other_source    ='_none_'   then ws_other_source   else  @ws_other_source     end ,  
ws_other_end_msgid   =case when @ws_other_end_msgid  =-999999999 then ws_other_end_msgid  else  @ws_other_end_msgid  end ,
ws_other_end_msg    =case when @ws_other_end_msg  ='_none_'   then ws_other_end_msg   else  @ws_other_end_msg end  


where ws_id=@wsid1 
end 
declare @bizid int 
select top 1 @bizid = wrk_bizid from mps_flowwork where wrk_id=@swrkid 
declare @cmpid int 
select @cmpid=biz_cmpid from mps_biztype where biz_id=@bizid 
exec sys_gen_joblink_updatedata_all @cmpid ,@bizid  

select @wsid1 ws_id  
set nocount off
end