IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_myworkinfo_project]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_myworkinfo_project]
GO
 
CREATE procedure [dbo].[mps_g_myworkinfo_project](
@cmpid int ,
@useruid int,
@objid bigint  ,
@key nvarchar(50) ,
@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   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) 

set @i = @objid 
if (@i=0) set @i=999999999
if (@ct=0) 
select top 10 ord_id identid ,1 msgread, ord_id objid , ord_type objtype ,'' objaction , ord_date objdate ,
ord_no+'/'+ord_title objtitle ,  'biz' objctgtype, ord_bizid objid1,ord_id objid2
from mps_order with(nolock) , mps_order_users  with(nolock) 
where ord_cmpid=@cmpid and ordp_cuid=@useruid  and ord_identid<@i   and  ordp_bizid=ord_bizid 
and ordp_cuid=@useruid and ordp_ordid=ord_id and 
( ord_branch like '%'+@key+'%' or ord_createdepart like '%'+@key+'%' or ord_depart like '%'+@key+'%' 
or ord_creator like '%'+@key+'%'  or ord_empname  like '%'+@key+'%' 
or ord_no  like '%'+@key+'%'  or  
ord_title  like '%'+@key+'%'  or 
ord_status  like '%'+@key+'%' or 
ord_type  like '%'+@key+'%' )
order by ord_date desc  
else 
select top 10 ord_id identid ,1 msgread, ord_id objid , ord_type objtype ,'' objaction , ord_date objdate ,
ord_no+'/'+ord_title objtitle ,  'biz' objctgtype, ord_bizid objid1,ord_id objid2
from mps_order with(nolock) , mps_order_users with(nolock)  ,@t 
where ord_cmpid=@cmpid and ordp_cuid=@useruid  and ord_identid<@i   and  ordp_bizid=ord_bizid and ord_bizid=crmid  
and ordp_cuid=@useruid and ordp_ordid=ord_id and 
( ord_branch like '%'+@key+'%' or ord_createdepart like '%'+@key+'%' or ord_depart like '%'+@key+'%' 
or ord_creator like '%'+@key+'%'  or ord_empname  like '%'+@key+'%' 
or ord_no  like '%'+@key+'%'  or  
ord_title  like '%'+@key+'%'  or 
ord_status  like '%'+@key+'%' or 
ord_type  like '%'+@key+'%' )
order by ord_date desc  
set nocount off
end