IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_ding_maininfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_ding_maininfo]
GO
create PROCEDURE mps_ding_maininfo
 @cmpid int , 
 @useruid int  
AS  
BEGIN  
   
 SET NOCOUNT ON;  
 
 /*t1:查询业务单(手机模板未设置隐藏，且当前用户具有bizview查看权限)，显示业务大类的图标和名字*/  
 declare @bizs table(bizid int) 
 insert into @bizs(bizid)
 select distinct biz_id 
 from mps_biztype with(nolock),mps_biztype_users  with(nolock) 
 where 
biz_cmpid=@cmpid and biz_hidemenu_mobile = 1  and 
biz_id = bp_bizid and bp_cuid = @useruid  and 
(bp_type = 'bizview' or bp_type='biznew' or bp_type='bizshow' or bp_type='bizdelete')  
 
 
 select biz_category   category , max(biz_icon0) icon0 , count(biz_id) counti , 1 msg_type  
 from mps_biztype with(nolock),@bizs
 where biz_id=bizid 
 group by biz_category 
 
  
 /*t2:查询报表(手机模板未设置隐藏，且当前用户具有rptview查看权限)，显示报表大类的图标和名字*/  
 select rpt_category category,max(rpt_icon0)  icon0, count(rpt_id) counti , 2 msg_type  
 from mps_report with(nolock),mps_report_users   with(nolock)
 where 
rpt_cmpid=@cmpid and rpt_widget=1 and  rpt_hidemenu_mobile = 1 and 
rpt_id = rptp_rptid and rptp_cuid = @useruid  and 
rptp_type = 'rptview' 
 group by rpt_category 
  
 /*t3:查询自定义应用(业务单和报表)*/  
 -- 先清理
 /*delete mps_ding_myapp 
 where not exists (
select 1 from @bizs where bizid=dm_bizid  
) and dm_type=1 and dm_cuid = @useruid
 delete mps_ding_myapp 
 where not exists (
select 1 from mps_report_users with(nolock) where dm_cuid=@useruid and dm_type=2 and dm_bizid=rptp_rptid and (rptp_type = 'rptview') 
)  and dm_cuid = @useruid */


 select dm_id,dm_bizid,dm_type,dm_category,dm_title,dm_subcategory,
  case when dm_type = 1 then (select biz_icon from mps_biztype where biz_id = dm_bizid)
  else (case when dm_type = 2 then (select rpt_icon from mps_report where rpt_id = dm_bizid) 
  else (case when dm_type = 3 then (select pt_icon from mps_programtype where pt_id=dm_bizid) 
else (case when dm_type = 4 then (select dc_icon from mps_docctg where dc_id = dm_bizid) else '' end) end) end)
   end as dm_icon
 from  mps_ding_myapp with(nolock) where (dm_cuid=@useruid or dm_cuid = 0)
 order by dm_id
 
 --- 公文是这样处理的，只需要显示最近未读的最多3条公文  主要用于移动端 
 
 declare @doc table(docid bigint) 
 insert into @doc(docid) 
 select top 3 doc_id  
 from mps_document a with(nolock), mps_docctg with(nolock) , mps_document_users with(nolock) 
 where a.doc_cmpid=@cmpid and  
  doc_dcid=dc_id and dc_frombiz=0 and doc_distroy>=getdate() and  doc_id=docp_docid and docp_cuid=@useruid and docp_type='docview' 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 
 declare @ic int 
 select @ic=COUNT(*) from @doc 
 set @ic = 3-@ic 
 if (@ic>0) 
 begin
insert into @doc(docid) 
select top(@ic) doc_id  
 from mps_document a with(nolock), mps_docctg with(nolock) , mps_document_users with(nolock) 
 where a.doc_cmpid=@cmpid and  
  doc_dcid=dc_id and dc_frombiz=0 and doc_distroy>=getdate() and  doc_id=docp_docid and docp_cuid=@useruid and docp_type='docview'  and 
  (not exists (select 1 from @doc where docid=doc_id))
order by  doc_create desc 
 end 
 
 -- t4 3条公告 
 select  a.* , dc_type , dc_caption
from mps_document a with(nolock), mps_docctg with(nolock),@doc 
where doc_id=docid and doc_dcid=dc_id 
order by doc_create desc 
 
 declare @today datetime 
set @today=convert(datetime,convert(varchar(10),GETDATE(),120),120)

-- 获取我的待办事项  t4 
declare @undo int = 0
select @undo=count(1) from mps_job with(nolock),@bizs 
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=bizid 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 
select @prj=count(1) from mps_order with(nolock) ,mps_order_users with(nolock) ,@bizs 
where ord_cmpid=@cmpid and ordp_cuid=@useruid and ordp_ordid=ord_id and ordp_bizid=ord_bizid and ord_bizid=bizid  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 返回数量
select @undo info_undo , @task info_task,@prj info_prj , @alarm info_alarm , @risk info_risk , @report info_report , @msg info_msg 

---------- t6自定义应用
declare @mnroleid int = 0
declare @mnflag varchar(200)
    --查询出mn_flag
select @mnflag = 'prg'+CONVERT(varchar(50),(select top 1 pt_id 
from mps_programtype where pt_filename = 'mobileindex'))
--查询出mn_roleid
select @mnroleid = mn_roleid
from mps_employee,mps_menu 
where cu_id = @useruid and cu_function = mn_roleid
and mn_flag = @mnflag and mn_cmpid = 1 
if(@mnroleid is null or @mnroleid <0) set @mnroleid = 0
--查询出结果
select mn_id,mn_title ,mn_flag,mn_sub,mn_icon ,mn_order,mn_link,
(select mn_title from mps_menu where mn_id = a.mn_sub) as mn_caption
from mps_menu  a
where mn_flag = @mnflag
and mn_cmpid = 1 and mn_sub>0
and  mn_roleid = @mnroleid
order by mn_order,mn_sub


 
 set nocount off;  
END