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

-- 生成时间:2018-05-02 17:23:20 by ebs-core  
CREATE  procedure order_26_full(
@cmpid int,  
    @ordid bigint, 
    @useruid int, 
    @jbid bigint ) 
as  
begin  
set nocount on 
-- 首先取模板  t1 t2 
declare @tmpid int 
declare @ordfinished int 
declare @ordbizid int 
declare @ord_ftype varchar(20),@ord_fbizid int,@ord_ford2tag int, @ord_ford2id bigint,@ord_fordid bigint 
declare @ord_rtype varchar(20),@ord_rbizid int,@ord_rord2tag int, @ord_rord2id bigint,@ord_rordid bigint 
declare @fordid bigint  
  


select  @fordid = ord_fordid, @ordbizid = ord_bizid, @tmpid = ord_templateid, @ordfinished = ord_finished  ,
  @ord_ftype=ord_ftype,@ord_fbizid=ord_fbizid,@ord_ford2tag=ord_ford2tag, @ord_ford2id=ord_ford2id,@ord_fordid =ord_fordid ,
  @ord_rtype=ord_rtype,@ord_rbizid=ord_rbizid,@ord_rord2tag=ord_rord2tag, @ord_rord2id=ord_rord2id,@ord_rordid =ord_rordid 
from order_26 where ord_id = @ordid and ord_cmpid = @cmpid 
declare @jbtmpid int   --保存工作步骤的模板 
declare @jbwsid int 
declare @jbwrkid int 
if (@ordfinished <= 1)  --业务还没有完 
begin 
select @jbwrkid=jb_wrkid,@jbtmpid = jb_templateid, @jbwsid = jb_wsid  from  mps_job where jb_id = @jbid and jb_cmpid = @cmpid 
end 
set @jbtmpid = isnull(@jbtmpid, 0) 
set @jbwsid = isnull(@jbwsid, -1) 
set @jbwrkid = isnull(@jbwrkid, 0) 
if (@jbtmpid>0) --步骤中有模板 
begin 
if (@tmpid = 0)  --没有子模板，则直接取步骤中的模板 
begin 
select * from mps_template where tmp_id = @jbtmpid  
select 0 none where 0 = 1  --没有子模板 
end else 
begin 
-- 先取主模板，再取子模板 
select  * from mps_template where tmp_id = @jbtmpid 
select  * from mps_template where tmp_id = @tmpid 
--select  0  as ord2id_page  
--select  * from mps_template where tmp_id = @jbtmpid 
end  
end else 
    begin 
      if (@tmpid = 0)  --没有子模板，则获取主模板 
      begin 
        select top 1 * from mps_template where tmp_cmpid = @cmpid and tmp_type = 'biz' and tmp_objid = @ordbizid and tmp_caption='缺省模板' order by tmp_id 
        select 0 none where 0 = 1  --没有子模板 
      end else 
      begin 
        select top 1 * from mps_template where tmp_cmpid = @cmpid and tmp_type = 'biz' and tmp_objid = @ordbizid  and tmp_caption='缺省模板' order by tmp_id 
        select  * from mps_template where tmp_id = @tmpid 
      end 
    end 
    -- 返回工作记录模板 t3
    if (@jbwrkid>0) 
    begin
if (exists (select * from mps_template where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid)) 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid 
end else 
begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
end
    end else
    begin
select * from mps_template where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
    end
    -- 返回当前业务定义t4 
    select * from mps_biztype where biz_id = @ordbizid 
    -- 返回业务单中的特殊权限  t5 
    select * from order_26_users where(ordp_type = 'bizaudit' or ordp_type = 'bizdelete' or ordp_type = 'bizabolish') and ordp_ordid = @ordid and ordp_cuid=@useruid 
    -- 返回工作中的特殊权限  t6 
    if (@jbid>0 and @ordfinished <= 1) 
    select * from mps_job_users where jp_jbid = @jbid and jp_cuid=@useruid 
    else select 0 none where 0 = 1
    --返回业务单数据 t7 
    select * from order_26 where ord_id = @ordid 
    -- 获取业务表主根据扩展数据  t8 
    select  * from order_26_0 where ord3_ordid = @ordid 
    -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t9  
    if (@jbwsid = 0 and @ordfinished <= 1) 
    select a.*, b.* from mps_job a, mps_flowwork b where jb_id = @jbid and wrk_id = jb_wrkid 
    else select 0 none where 0 = 1 
    --返回所有附件  t10 
    select *, da_categoryid as datagroup from mps_attach where da_cmpid = @cmpid and 
    (da_type = 'biz26' )
    and da_objid = @ordid   order by da_categoryid 
    -- 返回需要签字的工作记录。  t11 
    if (@ordfinished <= 1) 
    select a.* from mps_job a  where jb_crmid = @ordbizid and jb_ordid = @ordid and  jb_wsid>0 and jb_sign = 0  and    jb_back = 1 
    else select a.* from mps_job a   where jb_crmid = @ordbizid and jb_ordid = @ordid and jb_wsid>0 and jb_sign = 0  and jb_back = 1  

--明细表数据
 
----以下是数据源 

--- 以下获取用户自定义SQL afterloaded 


--- 以上获取用户自定义SQL afterloaded 
set nocount off
end