IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_myworkinfo_job]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_myworkinfo_job]
GO
CREATE procedure [dbo].[mps_g_myworkinfo_job](
@cmpid int ,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@usersecurity int ,
@crmids varchar(4000) ,
@category nvarchar(200) ,
@key nvarchar(50) ,
@mobile int=default,
@allcount int=default 
)
as 
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
if (@mobile is null or @mobile<0) set @mobile = 1 
if (@allcount is null or @allcount<=0) set @allcount = 100 

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 

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

-- 如果 @allcount <= 20  or @key中有内容 ， 则不聚合

if (@allcount<=20 or @key>'') 
begin
if (@ct=0) 
select 'job' objtag , jb_ordtype objtype  , jb_title objtitle1 , jb_title objtitle2, 
1 objcount ,
jb_id identid ,jb_id objid , 'biz' objctgtype, jb_crmid objid1, jb_ordid objid2 ,
case when @mobile=1 
 then CONVERT(varchar(16),jb_date,120)+' / '+jb_ordcreator++' / '+jb_ordno+case when jb_ordtitle='' then '' else ' / '+jb_ordtitle end +' / '+jb_title 
 else CONVERT(varchar(5),jb_date,10)+' '+CONVERT(varchar(5),jb_date,8)+'/'+jb_ordcreator+'/'+jb_title 
end objtitle  
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_ordtype like '%'+@key+'%' or jb_ordno like '%'+@key+'%' or  jb_ordtitle like '%'+@key+'%'
or  jb_ordcreator like '%'+@key+'%' or  jb_backinfo  like '%'+@key+'%' 
or  jb_ordstatus  like '%'+@key+'%' or   jb_title   like '%'+@key+'%')  and jb_ordno not like '%临%'
order by jb_ordtype , jb_date desc 
else 
select 'job' objtag , jb_ordtype objtype  , jb_title objtitle1 , jb_title objtitle2, 
1 objcount ,
jb_id identid ,jb_id objid , 'biz' objctgtype, jb_crmid objid1, jb_ordid objid2 ,
case when @mobile=1 
 then CONVERT(varchar(16),jb_date,120)+' / '+jb_ordcreator++' / '+jb_ordno+case when jb_ordtitle='' then '' else ' / '+jb_ordtitle end +' / '+jb_title 
 else CONVERT(varchar(5),jb_date,10)+' '+CONVERT(varchar(5),jb_date,8)+'/'+jb_ordcreator+'/'+jb_title 
end objtitle  
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_showinundo=0 and jb_crmid=crmid and 
(jb_ordtype like '%'+@key+'%' or jb_ordno like '%'+@key+'%' or  jb_ordtitle like '%'+@key+'%'
or  jb_ordcreator like '%'+@key+'%' or  jb_backinfo  like '%'+@key+'%' 
or  jb_ordstatus  like '%'+@key+'%' or   jb_title   like '%'+@key+'%')  and jb_ordno not like '%临%'
order by jb_ordtype , jb_date desc 
end else
begin
if (@ct=0) 
begin
select 'job' objtag , jb_ordtype objtype  , min(jb_title) objtitle1 , MAX(jb_title) objtitle2, 
COUNT(jb_id) objcount ,
min(jb_id) identid ,min(jb_id) objid , 'biz' objctgtype, min(jb_crmid) objid1,min(jb_ordid) objid2 ,
case when @mobile=1 
 then  MIN(CONVERT(varchar(16),jb_date,120)+' / '+jb_ordcreator++' / '+jb_ordno+case when jb_ordtitle='' then '' else ' / '+jb_ordtitle end +' / '+jb_title) 
 else  MIN(CONVERT(varchar(5),jb_date,10)+' '+CONVERT(varchar(5),jb_date,8)+'/'+jb_ordcreator+'/'+jb_title)
end objtitle 
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_ordtype like '%'+@key+'%' or jb_ordno like '%'+@key+'%' or  jb_ordtitle like '%'+@key+'%'
or  jb_ordcreator like '%'+@key+'%' or  jb_backinfo  like '%'+@key+'%' 
or  jb_ordstatus  like '%'+@key+'%' or   jb_title   like '%'+@key+'%')  and jb_ordno not like '%临%'
group by jb_ordtype  
order by jb_ordtype  
end else
begin
select 'job' objtag , jb_ordtype objtype  , min(jb_title) objtitle1 , MAX(jb_title) objtitle2, 
COUNT(jb_id) objcount ,
min(jb_id) identid ,min(jb_id) objid , 'biz' objctgtype, min(jb_crmid) objid1,min(jb_ordid) objid2 ,
case when @mobile=1 
 then  MIN(CONVERT(varchar(16),jb_date,120)+' / '+jb_ordcreator++' / '+jb_ordno+case when jb_ordtitle='' then '' else ' / '+jb_ordtitle end +' / '+jb_title) 
 else  MIN(CONVERT(varchar(5),jb_date,10)+' '+CONVERT(varchar(5),jb_date,8)+'/'+jb_ordcreator+'/'+jb_title)
end objtitle 
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_showinundo=0 and  jb_crmid=crmid and 
(jb_ordtype like '%'+@key+'%' or jb_ordno like '%'+@key+'%' or  jb_ordtitle like '%'+@key+'%'
or  jb_ordcreator like '%'+@key+'%' or  jb_backinfo  like '%'+@key+'%' 
or  jb_ordstatus  like '%'+@key+'%' or   jb_title   like '%'+@key+'%')  and jb_ordno not like '%临%'
group by jb_ordtype  
order by jb_ordtype  
end
end 

set nocount off
end