IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_156_brief]') AND type = 'P')
DROP PROCEDURE [dbo].[order_156_brief]
GO

-- 生成时间:2018-05-02 17:21:58 by ebs-core  
CREATE  procedure order_156_brief(
    @cmpid int , 
@useruid int , 
@ordid bigint ) 
as  
begin  
  set nocount on 
  declare @ordfinished int 
  declare @bizid int 
  select @bizid = ord_bizid, @ordfinished = ord_finished from order_156 where ord_id = @ordid 
  -- 返回当前业务定义t1 
  select a.*, b.* from mps_biztype a, order_156  b where ord_id = @ordid and biz_id = @bizid 
  -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t2 
  if (@ordfinished >= 0  and  @ordfinished <= 1) 
  begin 
    -- 如果有我的工作，就出现我的工作，如果没有，则列出工作名称清单 
    -- 由于任签与汇签，都将出现在job中。所以，如果有我需要处理的工作，一定存在 
    if (exists(select 1 from mps_job where jb_cmpid = @cmpid and jb_ordid = @ordid and jb_crmid=@bizid and jb_cuid = @useruid and 
    jb_wsid = 0 and jb_isauto = 1  and jb_back = 1 and jb_confirm=0 )) 
    begin 
      -- t2 我可以做的工作清单 
      select a.*, b.* from mps_job a, mps_flowwork b 
      where  jb_cmpid = @cmpid and jb_crmid = @bizid and 
        jb_ordid = @ordid and jb_isauto = 1  and jb_back = 1  and jb_wsid = 0 and 
        jb_wsstatus = ''  and jb_cuid = @useruid and wrk_id = jb_wrkid  and jb_confirm=0 
      select 0 none, 1 none2 where 0 = 1 --t3 工作清单列表 
      
      -- 获取业务单中的监督
      select distinct bp_type as jp_type , 0 jp_canedit   
      from mps_biztype, mps_biztype_users 
      where biz_id = @bizid  and bp_bizid = biz_id and biz_params like '%auditedit%' 
      and bp_cuid=@useruid and bp_type='bizaudit' 
      union 
      select distinct ordp_type as jp_type , 1 jp_canedit   
      from order_156_users
      where ordp_ordid=@ordid and ordp_cuid=@useruid and (ordp_type='bizdelete' or ordp_type='bizabolish') 
      
      select 0 none, 1 none2 where 0 = 1      --t5 我可以回撤的
    end else 
    begin 
    if (not exists (select 1 from mps_job where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid)) 
    begin
    select 0 none, 1 none2 where 0 = 1  --t2 我可以做的工作清单 
      select 'fail' jobtitles   --t3 工作清单列表  异常
      
      select 0 none, 1 none2 where 0 = 1      --t4  
       
      select 0 none, 1 none2 where 0 = 1      --t5 我可以回撤的
    end else
    begin
      select 0 none, 1 none2 where 0 = 1  --t2 我可以做的工作清单 
      -- 没有我可以执行的，则返回未处理的工作名称 
      declare @t table(title1 nvarchar(300)) 
      insert into @t(title1) 
      select distinct jb_title + '(' + case when jb_confirm=1 then jb_executor+':未完成流程转交' else jb_executor end + ')' from mps_job 
      where jb_cmpid = @cmpid and jb_ordid = @ordid and 
      jb_wsid = 0 and jb_isauto = 1 and  jb_back = 1 and jb_crmid=@bizid 
      declare @titles nvarchar(4000) 
      set @titles = null 
      select @titles = ISNULL(@titles + ',', '') + title1 from @t 
      set @titles = ISNULL(@titles, '') 
      select @titles jobtitles  -- t3 工作清单列表 
      -- 获取与我有关的所有的监督、协作的工作清单 
      -- t4 监督与协作 
      select distinct jp_type, 
      case when charindex('coeditable', wrk_keyparams)>0 and jp_type = 'wrkcoops' then 0 
      when charindex('auditedit', wrk_keyparams)>0 and jp_type = 'wrkaudit' then 0 
      when charindex('bizaudit', wrk_keyparams)>0 and jp_type = 'bizaudit' then 0 
      when charindex('bizdelete', wrk_keyparams)>0 and jp_type = 'bizdelete' then 1 
      when charindex('bizabolish', wrk_keyparams)>0 and jp_type = 'bizabolish' then 1
      when jp_type = 'wrklog' then 0 
        else 1 
        end jp_canedit 
      from mps_job, mps_job_users, mps_flowwork 
      where jb_cmpid = @cmpid and jb_ordid = @ordid and jp_cuid=@useruid and 
      jb_wsid = 0 and jb_isauto = 1 and jb_confirm=0 and jb_back = 1 and jp_jbid = jb_id and jb_crmid=@bizid 
      and (jp_type = 'wrkaudit' or jp_type = 'wrkcoops' or jp_type='wrklog' or jp_type = 'bizaudit' or jp_type='bizabolish' or jp_type='bizdelete') and 
      jb_wrkid = wrk_id 
      
      union
      
      select distinct bp_type as jp_type , 0 jp_canedit   
      from mps_biztype, mps_biztype_users 
      where biz_id = @bizid  and bp_bizid = biz_id and biz_params like '%auditedit%' 
      and bp_cuid=@useruid and bp_type='bizaudit' 
      
      union
      
      select distinct bp_type as jp_type , 0 jp_canedit   
      from  mps_biztype_users 
      where bp_bizid = @bizid and bp_cuid=@useruid and (bp_type='bizdelete' or bp_type='bizabolish' )
      
      union 
      select distinct ordp_type as jp_type , 1 jp_canedit   
      from order_156_users 
      where ordp_ordid=@ordid and ordp_cuid=@useruid and (ordp_type='bizdelete' or ordp_type='bizabolish') 
      
      union
      
      select distinct 'wrktask' as jp_type , 1 jp_canedit   
      from  mps_task 
      where tsk_cuid=@useruid and tsk_objtype='biz' and tsk_ctgid=156 and tsk_objid=@ordid  
      
      --t5 我可以回撤的
      select top 1 c.jb_id , c.jb_wrkid , c.jb_wrktype,c.jb_title 
      from mps_job a , mps_flowwork b , mps_job c 
      where a.jb_cmpid=@cmpid and a.jb_ordid=@ordid and a.jb_wsid=0 and  
      a.jb_fjbid=c.jb_id and c.jb_cuid=@useruid and c.jb_wsid>0 and c.jb_ordid=@ordid and 
      c.jb_wrkid=wrk_id and wrk_keyparams like '%reback%'
    end 
    end 
  end else 
  if (@ordfinished>0 and @ordfinished<10) 
  begin 
      select 0 none, 1 none2 where 0 = 1  --t2 我可以做的工作清单 
      select 0 none, 1 none2 where 0 = 1  --t3 工作清单列表  
      
      select distinct bp_type as jp_type , 0 jp_canedit   
      from mps_biztype, mps_biztype_users 
      where biz_id = @bizid  and bp_bizid = biz_id and biz_params like '%auditedit%' 
      and bp_cuid=@useruid and bp_type='bizaudit' 
       union
      select distinct ordp_type as jp_type , 1 jp_canedit   
      from  order_156_users 
      where ordp_cuid=@useruid and ordp_ordid=@ordid and  (ordp_type='bizlog' or ordp_type='wrklog' )
       
      select 0 none, 1 none2 where 0 = 1      --t5 我可以回撤的
  end  else
  begin
  select 0 none, 1 none2 where 0 = 1  --t2 我可以做的工作清单 
      select 0 none, 1 none2 where 0 = 1  --t3 工作清单列表  
      select 0 none, 1 none2 where 0 = 1
      select 0 none, 1 none2 where 0 = 1      --t5 我可以回撤的
  end 
  -- t6 未确认的工作 
  select COUNT(1)  unconfirm 
  from mps_job 
    where jb_cmpid = @cmpid and jb_ordid = @ordid  and jb_cuid = @useruid 
    and jb_back = 1 and jb_isauto = 1 and jb_confirm = 1 and jb_wsid = 0 and jb_crmid=@bizid 
  set nocount off  
end