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

-- 生成时间:2017-11-22 17:45:28 by ebs-core  
CREATE  procedure order_186_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_186 where ord_id = @ordid 
  -- 返回当前业务定义t1 
  select a.*, b.* from mps_biztype a, order_186  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 0 none, 1 none2 where 0 = 1      --t4 监督与协作 
    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 + '(' + jb_executor + ')' from mps_job 
      where jb_cmpid = @cmpid and jb_ordid = @ordid and 
      jb_wsid = 0 and jb_isauto = 1 and jb_confirm=0 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 
        else 1 
        end jp_canedit 
      from mps_job, mps_job_users, mps_flowwork 
      where jb_cmpid = @cmpid and jb_ordid = @ordid 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') and 
      jb_wrkid = wrk_id 
    end 
  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  --t4 监督与协作 
  end 
  -- t5 未确认的工作 
  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