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) 
)
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  
where biz_cmpid=@cmpid and biz_category=@c1 
else 
insert into @t(crmid,ok) 
select biz_id ,0 
from mps_biztype  
        where biz_cmpid=@cmpid and biz_category=@c1 and biz_subcategory=@c2 
        
  update @t set ok=1  
    from mps_biztype_users 
    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 
-- 获取5  条公司通告  t1

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

select top 5 a.* , dc_type , dc_caption
from mps_document a , mps_docctg 
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 

---- 获取8份 推荐文章  t2
select top 8 a.* , dc_type , dc_caption
from mps_document a , mps_docctg 
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份 最新上传  t3 
select top 8 a.* , dc_type , dc_caption
from mps_document a , mps_docctg 
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 

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


declare @msg table(id int , msg_caption nvarchar(50),msg_type varchar(20), msg_alli int ,msg_all nvarchar(100), msg_todayi int , msg_today nvarchar(100),
msg_delayi int , msg_delay nvarchar(100), msg_lastmi int , msg_lastm nvarchar(100)) 

-- 获取我的待办事项  t4 
declare @alli int 
if (@ct=0) 
select @alli=count(*) from mps_job 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 @alli=count(*) from mps_job,@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 @todayi int 
if (@ct=0) 
select @todayi=count(*) from mps_job 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_date>=@today and jb_showinundo=0 -- and jb_ordno not like '%临%'
else select @todayi=count(*) from mps_job,@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_date>=@today and jb_crmid=crmid  and jb_showinundo=0 -- and jb_ordno not like '%临%'

insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 1,'待办事项', 'job' , @alli , '有'+convert(varchar(10),@alli)+'项',@todayi,'有'+convert(varchar(10),@todayi)+'项新发生的', 0, '已经逾期的0项',0,'一个月前有0项'
-- 

-- t4 
-- 我的工作任务
set @alli =0 
select @alli=count(*) from mps_task where tsk_cmpid=@cmpid and tsk_cuid=@useruid 
set @todayi =0  
select @todayi=count(*) from mps_task where tsk_cmpid=@cmpid and tsk_cuid=@useruid and tsk_date>=@today 

insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 2,'任务事项', 'task' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '有'+convert(varchar(10),@todayi)+'项新发生的',0, '　',0,'　'


-- t5 与我有关的项目   
set @alli =0 
if (@ct=0) 
select @alli=count(*) from mps_order,mps_order_users 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 @alli=count(*) from mps_order,mps_order_users,@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 '%临%'
set @todayi =0  
if (@ct=0) 
select @todayi=count(*) from mps_order,mps_order_users where ord_cmpid=@cmpid and ordp_cuid=@useruid and ordp_ordid=ord_id and ordp_bizid=ord_bizid  and ord_date>=GETDATE()-30  ----and ord_no not like '%临%'
else 
select @todayi=count(*) from mps_order,mps_order_users,@t where ord_cmpid=@cmpid and ordp_cuid=@useruid and ordp_ordid=ord_id and ordp_bizid=ord_bizid  and ord_date>=GETDATE()-30 and ord_bizid=crmid  ----and ord_no not like '%临%'

if (@alli>0)
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 3,'项目管理', 'project' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '近期有'+convert(varchar(10),@todayi)+'项',0, '　',0,'　'
else 
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 3,'　', 'project' , @alli ,'　', @todayi, '　',0, '　',0,'　'


-- t6 与我有关的预警 mps_alarm_message 
set @alli =0 
select @alli=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='alarm' and  almmsg_cuid=@useruid 
set @todayi =0  
select @todayi=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='alarm' and  almmsg_cuid=@useruid and almmsg_read=1 
if (@alli>0)
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 4,'预警事件', 'alarm' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '有'+convert(varchar(10),@todayi)+'项未处理',0, '　',0,'　'
else 
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 4,'　', 'alarm' , @alli ,'　', @todayi, '　',0, '　',0,'　'

-- t7 与我有关的风险 mps_alarm_message 
set @alli =0 
select @alli=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='risk' and  almmsg_cuid=@useruid 
set @todayi =0  
select @todayi=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='risk' and  almmsg_cuid=@useruid and almmsg_read=1 
if (@alli>0)
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 5,'风险事件', 'risk' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '有'+convert(varchar(10),@todayi)+'项未处理',0, '　',0,'　'
else 
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 5,'　', 'risk' , @alli ,'　', @todayi, '　',0, '　',0,'　'


-- t8 与我有关的rbge mps_alarm_message 
set @alli =0 
select @alli=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='report' and  almmsg_cuid=@useruid 
set @todayi =0  
select @todayi=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='report' and  almmsg_cuid=@useruid and almmsg_read=1 
if (@alli>0)
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 6,'数据报', 'report' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '有'+convert(varchar(10),@todayi)+'项未读',0, '　',0,'　'
else 
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 6,'　', 'report' , @alli ,'　', @todayi, '　',0, '　',0,'　'


-- t8 与我有关的消息 mps_alarm_message 
set @alli =0 
select @alli=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='message' and  almmsg_cuid=@useruid 
set @todayi =0  
select @todayi=count(*) from mps_alarm_message where almmsg_cmpid=@cmpid and almmsg_type='message' and  almmsg_cuid=@useruid and almmsg_read=1 
if (@alli>0)
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 7,'消息提示', 'message' , @alli ,'有'+convert(varchar(10),@alli)+'项', @todayi, '有'+convert(varchar(10),@todayi)+'项未读',0, '　',0,'　'
else 
insert into @msg(id ,msg_caption, msg_type , msg_alli, msg_all,msg_todayi ,msg_today, msg_delayi , msg_delay, msg_lastmi,msg_lastm) 
select 7,'　', 'message' , @alli ,'　', @todayi, '　',0, '　',0,'　'


select top 5 * , id msg_order from @msg order by  case when msg_alli>0 then 0 else 1 end , id 

-- 返回最近没有填报完毕的业务单  --t5 
select 0 none1 , 1  none2 where 0=1 

/*
select top 8 jb_ordtype,jb_id,jb_ordid,jb_crmid,jb_orddate,jb_ordno,jb_ordtitle 
from mps_job 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_ordno like '%临%'
order by jb_orddate  */ 

set nocount off
end