IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[QSY_huopinchenlie]') AND type = 'P')
DROP PROCEDURE [dbo].[QSY_huopinchenlie]
GO
-- =============================================
-- Author:00384
-- Create date: 2018-5-21
-- Description:管理部货品陈列检查
-- =============================================
CREATE PROCEDURE [dbo].[QSY_huopinchenlie] 
-- Add the parameters for the stored procedure here
@ym nvarchar(6),
@lx nvarchar(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here

--declare @ym nvarchar(6)
--declare @lx nvarchar(10)

declare @startdate nvarchar(10)
declare @enddate nvarchar(10)

--set @ym='201805'
--set @lx='11-20号'

if (@lx='1-10号') 
begin
set @startdate=LEFT(@ym,4)+'-'+RIGHT(@ym,2)+'-01'
set @enddate=LEFT(@ym,4)+'-'+RIGHT(@ym,2)+'-10'
end

if (@lx='11-20号') 
begin
set @startdate=LEFT(@ym,4)+'-'+RIGHT(@ym,2)+'-11'
set @enddate=LEFT(@ym,4)+'-'+RIGHT(@ym,2)+'-20'
end

if (@lx='21-月底') 
begin
set @startdate=LEFT(@ym,4)+'-'+RIGHT(@ym,2)+'-21'
set @enddate=CONVERT(varchar(100), DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,@startdate)+1, 0)) , 23)
end


select @lx as dt,f.code,f.name,f.sf,f.cs,f.pq,f.fz,f.zj,f.jl,f.zg,f.dz,f.dpfzr,xy.ord_no,xy.ord_creator,CONVERT(varchar(100),xy.ord_commit, 23) rq,CONVERT(varchar(100),xy.ord_commit, 8) sj,xy.ord_bz,f.xz,
xy.ord_czggs, 
    (case when xy.ord_photos='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos,'|',1) end) 拍照11,
    (case when (case when xy.ord_photos='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos,'|',1) end)=(case when xy.ord_photos='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos,'|',2) end) then '' else (case when xy.ord_photos='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos,'|',2) end) end) 拍照12,
    (case when xy.ord_photos='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos,'|',3) end) 拍照13,
    (case when xy.ord_photos='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos,'|',4) end) 拍照14,
    (case when xy.ord_photos='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos,'|',5) end) 拍照15,
    '',xy.ord_ffggs,
    (case when xy.ord_photos1='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',1) end) 拍照21,
    (case when (case when xy.ord_photos1='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',1) end)=(case when xy.ord_photos1='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',2) end) then '' else (case when xy.ord_photos1='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',2) end) end) 拍照22,
    (case when xy.ord_photos1='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',3) end) 拍照23,
    (case when xy.ord_photos1='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',4) end) 拍照24,
    (case when xy.ord_photos1='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos1,'|',5) end) 拍照25,
    '',xy.ord_spggs,
    (case when xy.ord_photos2='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',1) end) 拍照31,
    (case when (case when xy.ord_photos2='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',1) end)=(case when xy.ord_photos2='' then '' else ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',2) end) then '' else (case when xy.ord_photos2='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',2) end) end) 拍照32,
    (case when xy.ord_photos2='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',3) end) 拍照33,
    (case when xy.ord_photos2='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',4) end) 拍照34,
    (case when xy.ord_photos2='' then '' else 'http://ebs.shesyea.com.cn'+ebs2java.dbo.chaifenlie(xy.ord_photos2,'|',5) end) 拍照35  
    from (select xz,code,name,sf,cs,pq,fz,zj,jl,zg,dz,dpfzr from Qsy_OldShopInfo where billmonth=@ym and closedate is null and xz in ('直营','加盟')) f  
    left join mps_depart m on m.dp_code=f.code 
    left join order_240 xy on m.dp_id=xy.ord_createdpid and xy.ord_status='已完成' 
and CONVERT(varchar(100),xy.ord_commit, 23)>=@startdate 
and CONVERT(varchar(100),xy.ord_commit, 23)<=@enddate
and (xy.ord_photos<>'' or xy.ord_photos1<>'' or xy.ord_photos2<>'')  
    order by xy.ord_commit desc,f.pq,f.xz desc,f.fz,f.zj,f.jl,f.zg   
    
END