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

-- 生成时间:2018-11-22 18:21:45 by ebs-core  
CREATE  procedure order_251_full(
@cmpid int,  
    @ordid bigint, 
    @useruid int, 
    @userdpid int,
    @branchid int ,
    @userotid int,
    @usertype int,
    @useradmin int,
    @userctid int,
    @jbid bigint ) 
as  
begin  
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @operation nvarchar(100)
declare @errorinfo nvarchar(4000)
-- 首先取模板  t1 t2 
declare @tmpid int 
declare @ordfinished int 
declare @ordbizid int 
declare @authority int = 0 
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  
  
declare @myallow__time int = 1 
declare @myallow__times varchar(10)=''
declare @myallow__date1 varchar(19)=''
declare @myallow__date2 varchar(19)=''
declare @myallow__week int = 0 
declare @myallow__day int = 0 
set @myallow__times = CONVERT(varchar(5),getdate(),114)
set @myallow__date1 = CONVERT(varchar(10),getdate(),120)
set @myallow__date2 = @myallow__date1 +' 23:59:59'
set @myallow__date1 = @myallow__date1 +' 00:00:00'
set @myallow__week = datepart(dw,getdate()) - 1 
set @myallow__day = day(getdate())
if (@myallow__week=0) set @myallow__week=7  

set @myallow__time=0 


select  @fordid = ord_fordid, @ordbizid = ord_bizid, @tmpid = ord_templateid, @ordfinished = ord_finished  ,@authority=ord_authority_factor,
  @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_251 with(nolock) where ord_id = @ordid and ord_cmpid = @cmpid 
declare @jbtmpid int   --保存工作步骤的模板 
declare @jbwsid int 
declare @jbwrkid int
declare @jbwrkids varchar(100)  
if (@ordfinished <= 1)  --业务还没有完 
begin 
select @jbwrkid=jb_wrkid,@jbtmpid = jb_templateid, @jbwsid = jb_wsid  from  mps_job with(nolock)  where jb_id = @jbid and jb_cmpid = @cmpid 
end 
set @jbtmpid = isnull(@jbtmpid, 0) 
set @jbwsid = isnull(@jbwsid, -1) 
set @jbwrkid = isnull(@jbwrkid, 0) 
set @jbwrkids = convert(varchar(10),@jbwrkid) 

-- 下面的代码是计算主模板及子模板

declare @main__1 int 
declare @template table(tmpid int , ismain int) 
select  @main__1=655 from order_251 with(nolock) where ord_id=@ordid 

if (@main__1>0 and (not exists (select 1 from @template where ismain=0))) insert into @template(tmpid,ismain) values(@main__1,0) 
-- 如果没有主模板，则要缺省找一个
if (not exists (select 1 from @template where ismain=0)) 
begin
insert into @template(tmpid,ismain) select tmp_id ,0 from mps_template with(nolock) where tmp_cmpid=@cmpid and tmp_objid=251 and tmp_cmpid=1 and (tmp_caption='缺省模板' or tmp_caption='') and tmp_main=0 
end 

if (@myallow__time=1)
begin
-- 返回主模板 t1
if (@errorinfo='') set @errorinfo='当前时间不允许访问'
select tmp_id,tmp_cmpid,tmp_type ,tmp_objid,tmp_objid2,tmp_caption,tmp_flowxml,tmp_scope,tmp_language,tmp_wrks,tmp_main,
'<div class=f24 style="text-align:center;padding-top:30px;">'+@errorinfo+'</div>' tmp_form,'' tmp_js,'' tmp_css,
'<div class=f24 style="text-align:center;padding-top:30px;">'+@errorinfo+'</div>' tmp_formmobile,'' tmp_jsmobile,'' tmp_cssmobile,'' tmp_sql,'' tmp_jssql 
from mps_template with(nolock) , @template where tmp_id=tmpid and ismain=0
-- 返回子模板 t2
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain>0 and tmp_id=-1 
end else
begin
-- 返回主模板 t1
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain=0
-- 返回子模板 t2
select * from mps_template with(nolock) , @template where tmp_id=tmpid and ismain>0
end 

    -- 返回工作记录模板 t3
    if (@jbwrkid>0) 
    begin
if (exists (select * from mps_template  with(nolock) where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid)) 
begin
select * from mps_template  with(nolock) where tmp_cmpid=@cmpid and tmp_type='wrklog' and tmp_objid=@jbwrkid 
end else 
begin
select * from mps_template  with(nolock)  where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
end
    end else
    begin
select * from mps_template  with(nolock)  where tmp_cmpid=@cmpid and tmp_type='bizlog' and tmp_objid=@ordbizid 
    end
    -- 返回当前业务定义t4 
    select * from mps_biztype  with(nolock) where biz_id = @ordbizid  and @myallow__time=0
    -- 返回业务单中的特殊权限  t5  
    select case orda_type when 1 then 'bizview' when 2 then 'bizaudit' when 3 then 'bizabolish' when 4 then 'bizdelete' when 5 then 'bizlog' else '' end ordp_type , orda_cuid , orda_cuid ordp_cuid 
    from order_251_authority_control  with(nolock) where orda_ordafid=@authority and orda_cuid=@useruid and orda_type>1  and @myallow__time=0
    -- 返回工作中的特殊权限  t6 
    if (@jbid>0 and @ordfinished <= 1) 
    select * from mps_job_users  with(nolock) where jp_jbid = @jbid and jp_cuid=@useruid  and @myallow__time=0
    else select 0 none where 0 = 1
    --返回业务单数据 t7 
    select * from order_251 with(nolock)  where ord_id = @ordid and @myallow__time=0
    -- 获取业务表主根据扩展数据  t8 
    select  * from order_251_0 with(nolock)  where ord3_ordid = @ordid and @myallow__time=0
    -- 返回当前的工作状态   如果工作已做完，是不需要返回的。  t9  
    if (@jbwsid = 0 and @ordfinished <= 1) 
    select a.*, b.* from mps_job a with(nolock) , mps_flowwork b  with(nolock) where jb_id = @jbid and wrk_id = jb_wrkid  and @myallow__time=0
    else select 0 none where 0 = 1 
    --返回所有附件  t10 
    select *, da_categoryid as datagroup from mps_attach  with(nolock) where da_cmpid = @cmpid and 
    (da_type = 'biz251' )
    and da_objid = @ordid  and @myallow__time=0  order by da_categoryid 
    -- 返回需要签字的工作记录。  t11 
    if (@ordfinished <= 1) 
    select a.* from mps_job a  with(nolock)  where jb_crmid = @ordbizid and jb_ordid = @ordid and  jb_wsid>0 and jb_sign = 0  and    jb_back = 1  and @myallow__time=0
    else select a.* from mps_job a  with(nolock)   where jb_crmid = @ordbizid and jb_ordid = @ordid and jb_wsid>0 and jb_sign = 0  and jb_back = 1   and @myallow__time=0

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

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


--- 以上获取用户自定义SQL afterloaded 
-- 获取最近5条评论
--创建内存表，存储所有fm_ffmid = 0的评论
declare @forumpobjs table(nid int identity(1, 1), fmid bigint) 
insert into @forumpobjs(fmid)
select top 5 fm_id from mps_forum a with(nolock) 
where a.fm_cmpid = @cmpid and a.fm_ffmid=0 and a.fm_type = 'biz' and a.fm_ctgid = 251 and a.fm_objid=@ordid 
order by a.fm_id desc

--查询出回复
declare @strforum varchar(max) = ''
select @strforum = stuff((
select ' fm_id = '+convert(varchar(100),fmid)+ ' or '
from @forumpobjs for xml path('')),1,1,'')
set @strforum = isnull(@strforum,'')

--查询出主回复的回复
declare @strforum1 varchar(max) = ''
select @strforum1 = stuff((
select ' fm_ffmid = '+convert(varchar(100),fmid)+' or fm_fffmid = ' +convert(varchar(100),fmid)+ ' or '
from @forumpobjs for xml path('')),1,1,'')

set @strforum1 = isnull(@strforum1,'')

if @strforum1 > ''
begin
set @strforum1 = @strforum +substring(@strforum1,1,len(@strforum1)-3)
set @strforum1 = ' select 1 as fm_bizforum,fm_id,fm_cuid,fm_cuname,fm_text,fm_ffmid,fm_fffmid,fm_createdtime,fm_attach from mps_forum a  with(nolock) where ' + @strforum1
 + ' order by fm_id '
exec(@strforum1)
end

set nocount off
end