IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_saleprds_details]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_saleprds_details]
GO
--查询商品
CREATE procedure [dbo].[mps_g_saleprds_details](
@cmpid int,
@mycuid int , 
@sptype nvarchar(30),
@category1 nvarchar(50),
@category2 nvarchar(50),
@category3 nvarchar(50),
@searchkey nvarchar(50),
@maxid int 
) as
begin
set nocount on
declare @key1 nvarchar(50) = '' 
declare @key2 nvarchar(50) = '' 
declare @key3 nvarchar(50) = '' 
declare @i int 

set @searchkey = REPLACE(@searchkey,',',' ') 
set @searchkey = REPLACE(@searchkey,'.',' ') 
set @searchkey = REPLACE(@searchkey,'，',' ') 
set @searchkey = REPLACE(@searchkey,'　',' ') 
set @searchkey = REPLACE(@searchkey,'  ',' ') 
set @searchkey = REPLACE(@searchkey,'　',' ') 
set @searchkey = LTRIM(rtrim(@searchkey)) 
set @i = CHARINDEX(' ',@searchkey) 
if (@i>0) 
begin
set @key1 = SUBSTRING(@searchkey,1,@i-1) 
set @searchkey = SUBSTRING(@searchkey,@i+1,100) 
set @i = CHARINDEX(' ',@searchkey) 
if (@i>0) 
begin
set @key2 = SUBSTRING(@searchkey,1,@i-1) 
set @key3 = SUBSTRING(@searchkey,@i+1,100) 
end else 
begin
set @key2 = @searchkey
end 
end else 
begin
set @key1 = @searchkey 
end 

-- 将所有满足条件的ID 找出来 
declare @prd table(orderi int IDENTITY(1,1) , spid bigint) 
if (@key1='') 
begin
insert into @prd(spid) 
select sp_id from [mps_saleprds] with(nolock) 
where sp_cmpid=@cmpid and (@mycuid=0 or sp_cuid=@mycuid)   and sp_type=@sptype and 
(sp_category1=@category1 or @category1='') and 
 (sp_category2=@category2 or @category2='') and 
 (sp_category3=@category3 or @category3='')  
order by sp_sale desc , sp_category2 , sp_caption 
end else 
if (@key2='') 
begin
insert into @prd(spid) 
select sp_id from [mps_saleprds] with(nolock) 
where sp_cmpid=@cmpid and (@mycuid=0 or sp_cuid=@mycuid)   and sp_type=@sptype and 
(sp_category1=@category1 or @category1='') and 
 (sp_category2=@category2 or @category2='') and 
 (sp_category3=@category3 or @category3='') and 
(
sp_category2 like '%'+@key1+'%' or 
sp_category3 like '%'+@key1+'%' or 
sp_caption like '%'+@key1+'%' or 
sp_model like '%'+@key1+'%' or 
sp_spec like '%'+@key1+'%' or 
sp_pack like '%'+@key1+'%' or 
sp_additional like '%'+@key1+'%' or 
sp_special like '%'+@key1+'%'  
)
order by sp_sale desc , sp_category2 , sp_caption 
end else 
    if (@key3='') 
    begin
insert into @prd(spid) 
select sp_id from [mps_saleprds] with(nolock) 
where sp_cmpid=@cmpid and (@mycuid=0 or sp_cuid=@mycuid)   and sp_type=@sptype and 
(sp_category1=@category1 or @category1='') and 
 (sp_category2=@category2 or @category2='') and 
 (sp_category3=@category3 or @category3='') and 
(
sp_category2 like '%'+@key1+'%' or 
sp_category3 like '%'+@key1+'%' or 
sp_caption like '%'+@key1+'%' or 
sp_model like '%'+@key1+'%' or 
sp_spec like '%'+@key1+'%' or 
sp_pack like '%'+@key1+'%' or 
sp_additional like '%'+@key1+'%' or 
sp_special like '%'+@key1+'%'  
) and 
(
sp_category2 like '%'+@key2+'%' or 
sp_category3 like '%'+@key2+'%' or 
sp_caption like '%'+@key2+'%' or 
sp_model like '%'+@key2+'%' or 
sp_spec like '%'+@key2+'%' or 
sp_pack like '%'+@key2+'%' or 
sp_additional like '%'+@key2+'%' or 
sp_special like '%'+@key2+'%'  
)
order by sp_sale desc , sp_category2 , sp_caption 
    end else
    begin
insert into @prd(spid) 
select sp_id from [mps_saleprds] with(nolock) 
where sp_cmpid=@cmpid and (@mycuid=0 or sp_cuid=@mycuid)   and sp_type=@sptype and 
 (sp_category1=@category1 or @category1='') and 
 (sp_category2=@category2 or @category2='') and 
 (sp_category3=@category3 or @category3='') and 
(
sp_category2 like '%'+@key1+'%' or 
sp_category3 like '%'+@key1+'%' or 
sp_caption like '%'+@key1+'%' or 
sp_model like '%'+@key1+'%' or 
sp_spec like '%'+@key1+'%' or 
sp_pack like '%'+@key1+'%' or 
sp_additional like '%'+@key1+'%' or 
sp_special like '%'+@key1+'%'  
) and 
(
sp_category2 like '%'+@key2+'%' or 
sp_category3 like '%'+@key2+'%' or 
sp_caption like '%'+@key2+'%' or 
sp_model like '%'+@key2+'%' or 
sp_spec like '%'+@key2+'%' or 
sp_pack like '%'+@key2+'%' or 
sp_additional like '%'+@key2+'%' or 
sp_special like '%'+@key2+'%'  
) and 
(
sp_category2 like '%'+@key3+'%' or 
sp_category3 like '%'+@key3+'%' or 
sp_caption like '%'+@key3+'%' or 
sp_model like '%'+@key3+'%' or 
sp_spec like '%'+@key3+'%' or 
sp_pack like '%'+@key3+'%' or 
sp_additional like '%'+@key3+'%' or 
sp_special like '%'+@key3+'%'  
)
order by sp_sale desc , sp_category2 , sp_caption 
    end  
    set @maxid = ISNULL(@maxid,0) 
    declare @amount int
    select @amount= COUNT(*) from @prd
    select top 20 
        orderi,
        @amount as amount,
sp_id,
sp_cmpid,
sp_bcid,
sp_dpid,
sp_cuid,
sp_type,
sp_prdid,
sp_category1,
sp_category2,
sp_category3,
sp_code,
sp_caption,
sp_model,
sp_spec,
sp_unit,
sp_pack,
sp_ext,
sp_amount,
sp_price,
sp_score,
sp_key,
sp_click,
sp_sale,
sp_limited,
sp_price0,
sp_additional,
sp_special,
sp_status,
sp_use,
sp_order,
sp_begin,
sp_end,
sp_tag,
sp_pcid,
sp_icon 
 from mps_saleprds a with(nolock) , @prd 
    where sp_id=spid and orderi>@maxid 
    order by orderi 
set nocount off 
end