IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_myworkinfo_]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_myworkinfo_]
GO
CREATE procedure [dbo].[mps_g_myworkinfo_](
@cmpid int ,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@usersecurity int ,
@crmids varchar(4000) ,
@category nvarchar(200) ,
@showgw int ,  -- 需要处理公文
@showdoc int    -- 需要处理文档

)
as 
begin

set nocount on
declare @c1 nvarchar(100) 
declare @c2 nvarchar(100) 
declare @i bigint 
set @i = CHARINDEX('/',@category) 
if (@i>0) 
begin
set @c1 = SUBSTRING(@category ,  1 , @i-1) 
set @c2 = SUBSTRING(@category ,  @i+1 , LEN(@category) - @i) 
end else 
begin
set @c1 = @category 
set @c2 = '' 
end 

declare @t table(crmid int,ok int) 

declare  @curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(4000)
set @srcs = @crmids 
select @curInd=1,@strlen=LEN(@srcs) 
while (@curInd<=@strlen) 
begin 
select @i=charindex(',',@srcs,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd) 
set @curInd=@i+1 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@tmps>'') insert into @t(crmid) values(convert(bigint,@tmps)) 
end

declare @ct int
select @ct = COUNT(1) from @t  
set @ct = ISNULL(@ct,0) 

if (@ct=0 and (@c1>'' or @c2>'')) 
begin
if (@c2='') 
insert into @t(crmid,ok) 
select biz_id,0
from mps_biztype  with(nolock)
where biz_cmpid=@cmpid and biz_category=@c1 
else 
insert into @t(crmid,ok) 
select biz_id ,0 
from mps_biztype  with(nolock)
        where biz_cmpid=@cmpid and biz_category=@c1 and biz_subcategory=@c2 
        
  update @t set ok=1  
    from mps_biztype_users with(nolock) 
    where bp_bizid=crmid and bp_cuid=@useruid and 
(bp_type='bizaudit' or bp_type='bizabolish' or bp_type='bizdelete' or bp_type='biznew' or bp_type='bizview' )

delete @t where ok = 0 
end 

--- 公文是这样处理的，只需要显示最近未读的最多3条公文  主要用于移动端 
--  同时显示 最近5条，主要用于PC上右上角
if (@showgw=0) 
begin
-- t1 3条未读的公告 
select top 3 a.* , dc_type , dc_caption
from mps_document a with(nolock), mps_docctg with(nolock)
where a.doc_cmpid=@cmpid and a.doc_security>=@usersecurity and 
doc_dcid=dc_id and dc_frombiz=0 and doc_distroy>=getdate() and 
(not exists (select 1 from mps_document_view with(nolock) where docv_docid=doc_id and docv_cuid=@useruid))
order by doc_create desc 

-- t2 5条最新的公告 
select top 5 a.* , dc_type , dc_caption
from mps_document a with(nolock), mps_docctg with(nolock)
where a.doc_cmpid=@cmpid and a.doc_security>=@usersecurity and 
doc_dcid=dc_id and dc_frombiz=0 and doc_distroy>=getdate() 
order by doc_create desc 
end else 
begin
select 1 none where 0=1
select 1 none where 0=1
end 


-- t3 t4
if (@showdoc=0)
begin
---- 获取8份 推荐文章  t3
select top 8 a.* , dc_type , dc_caption
from mps_document a with(nolock), mps_docctg with(nolock)
where a.doc_cmpid=@cmpid and a.doc_security>=@usersecurity and 
doc_dcid=dc_id and dc_frombiz=1 and doc_distroy>=getdate() and doc_deliver=1 
order by doc_create desc 

---- 获取8份 最新上传  t4 
select top 8 a.* , dc_type , dc_caption
from mps_document a with(nolock) , mps_docctg with(nolock)
where a.doc_cmpid=@cmpid and a.doc_security>=@usersecurity and 
doc_dcid=dc_id and dc_frombiz=1 and doc_distroy>=getdate() and doc_deliver<>1 
order by doc_create desc 
end else
begin
select 1 none where 0=1
select 1 none where 0=1
end 


select @ct = COUNT(1) from @t  
set @ct = ISNULL(@ct,0) 



declare @today datetime 
set @today=convert(datetime,convert(varchar(10),GETDATE(),120),120)

-- 获取我的待办事项  t4 
declare @undo int = 0
if (@ct=0) 
select @undo=count(1) from mps_job with(nolock) where jb_cmpid=@cmpid and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 and jb_enabled=0 and jb_back=1 and jb_showinundo=0  and jb_ordno not like '%临%' 
else select @undo=count(1) from mps_job with(nolock),@t where jb_cmpid=@cmpid and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 and jb_enabled=0 and jb_back=1 and jb_crmid=crmid and jb_showinundo=0  and jb_ordno not like '%临%' 

-- 我的工作任务
declare @task int=0 
select @task=count(1) from mps_task with(nolock) where tsk_cmpid=@cmpid and tsk_cuid=@useruid 

--与我有关的项目   
declare  @prj int =0 
if (@ct=0) 
select @prj=count(1) from mps_order with(nolock) ,mps_order_users with(nolock)  where ord_cmpid=@cmpid and ordp_cuid=@useruid and ordp_ordid=ord_id and ordp_bizid=ord_bizid   and ord_no not like '%临%'
else select @prj=count(1) from mps_order with(nolock) ,mps_order_users with(nolock) ,@t where ord_cmpid=@cmpid and ordp_cuid=@useruid and ordp_ordid=ord_id and ordp_bizid=ord_bizid and ord_bizid=crmid  and ord_no not like '%临%'


-- 与我有关的预警 mps_alarm_message 
declare @alarm int =0 
select @alarm=count(1) from mps_alarm_message  with(nolock) where almmsg_cmpid=@cmpid and almmsg_type='alarm' and  almmsg_cuid=@useruid 


-- 与我有关的风险 mps_alarm_message 
declare @risk int =0 
select @risk=count(1) from mps_alarm_message with(nolock)  where almmsg_cmpid=@cmpid and almmsg_type='risk' and  almmsg_cuid=@useruid 

-- 与我有关的rbge mps_alarm_message 
declare @report int =0 
select @report=count(1) from mps_alarm_message  with(nolock) where almmsg_cmpid=@cmpid and almmsg_type='report' and  almmsg_cuid=@useruid and almmsg_read=1  


-- 与我有关的消息 mps_alarm_message 
declare @msg int =0 
select @msg=count(1) from mps_alarm_message with(nolock)  where almmsg_cmpid=@cmpid and almmsg_type='message' and  almmsg_cuid=@useruid   and almmsg_read=1 



-- t5 
-- 返回最近几条待办事项
if (@undo<=5) 
begin
select top(@undo) @undo maxcount ,* from mps_job  with(nolock) 
where  jb_cmpid=@cmpid and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 and jb_enabled=0 and jb_back=1 and jb_showinundo=0  and jb_ordno not like '%临%' 
end else
begin
select top 3 @undo maxcount , * from mps_job  with(nolock) 
where  jb_cmpid=@cmpid and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 and jb_enabled=0 and jb_back=1 and jb_showinundo=0  and jb_ordno not like '%临%' 
end 

-- t6 返回数量
select @undo info_undo , @task info_task,@prj info_prj , @alarm info_alarm , @risk info_risk , @report info_report , @msg info_msg 


set nocount off
end