IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_category_rptinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_category_rptinfo]
GO
CREATE PROCEDURE [dbo].[mps_g_category_rptinfo](
@cmpid int,
@useruid int,
@useradmin int ,
@category nvarchar(200),
@mobile int = 1
)
as
begin
set nocount on 
if(@category!='' and @category!='_none_')
begin
declare @sub_category varchar(200) = ''
declare @par_category varchar(200) = @category
declare @index int = CHARINDEX('/',@category);
if(@index>0)
begin
set @sub_category=SUBSTRING(@category,@index+1,LEN(@category))
set @par_category = SUBSTRING(@category,1,@index-1)
end
declare @tmp_type varchar(64) = case when @sub_category >'' then 'rpt_sc' else 'rpt_c' end;
declare @tmp_id int = 0 
declare @reports table(rptid int)
declare @reports2 table(rpt_category nvarchar(400),rpt_categorysub nvarchar(400),
rpt_id int,
rpt_accesscount int,rpt_url varchar(1000),rpt_title nvarchar(200),
rpt_icon varchar(300),rpt_num int
)

print @category
print @sub_category
print @tmp_type


--获取首页模板
select @tmp_id = tmp_id from mps_template where tmp_type=@tmp_type and tmp_caption = @category and tmp_objid2=@useradmin
if @tmp_id IS NULL or @tmp_id =0 
select @tmp_id = tmp_id from mps_template where tmp_type=@tmp_type and tmp_caption = @category and tmp_objid2=0

print @par_category

--获取有查看权限的报表
insert into @reports(rptid)
select rpt_id 
from mps_report,mps_report_users
where rpt_category=@par_category 
and rpt_categorysub=@sub_category 
and rptp_rptid=rpt_id 
and rptp_cuid=@useruid 
and ((@mobile=0 and rpt_hidemenu_mobile=1) or ((@mobile=1 and rpt_hidemenu=1)))

if(@par_category>'' and @sub_category='')
begin
insert into @reports2(rpt_category,rpt_categorysub,rpt_id,
rpt_accesscount,rpt_url,rpt_title,rpt_icon,rpt_num)
select rpt_category,rpt_categorysub,-999,0,'',rpt_categorysub,'iconfont icon-XT-124',COUNT(rpt_id)
from mps_report,mps_report_users
where rpt_category=@par_category 
and rpt_categorysub != '' 
and rptp_rptid=rpt_id 
and rptp_cuid=@useruid 
and ((@mobile=0 and rpt_hidemenu_mobile=1) or ((@mobile=1 and rpt_hidemenu=1)))
group by rpt_category,rpt_categorysub 
end

end else--如果没有指定类型则返回该用户所有可以查看的报表
begin 
insert into @reports(rptid)
select rpt_id 
from mps_report,mps_report_users
where rptp_rptid=rpt_id and rptp_cuid=@useruid and ((@mobile=0 and rpt_hidemenu_mobile=1) or ((@mobile=1 and rpt_hidemenu=1)))
end
-----------返回数据-------------
select * from mps_template where tmp_id = @tmp_id
insert into @reports2(rpt_id,rpt_accesscount,rpt_url,rpt_title,rpt_category,rpt_categorysub,rpt_icon)
select rpt_id,rpt_accesscount,rpt_url,rpt_title,rpt_category,rpt_categorysub,rpt_icon
from @reports,mps_report where rptid=rpt_id

select *,rpt_category+'/'+rpt_categorysub as rpt_sub from @reports2 

end