IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[Qsy_cc_desk_vipRETAIL]') AND type = 'P')
DROP PROCEDURE [dbo].[Qsy_cc_desk_vipRETAIL]
GO

-- =============================================
-- Author:<00384>
-- Create date: <2018-06-28>
-- Description:<呼叫中心-VIP消费查询>
-- =============================================
CREATE PROCEDURE [dbo].[Qsy_cc_desk_vipRETAIL] 
-- Add the parameters for the stored procedure here
@pagesize int,
@pagecount int ,
@pageobjids varchar(max),
@VIPID nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


declare @s1 varchar(8000),
@s2 varchar(100),
@IDs varchar(max)='',
@i int,
@amount int,
@sql nvarchar(max)

declare @pobjs table(nid int identity(1, 1), ordid bigint) 
select @amount = @pagecount*@pagesize

set @s1 = @pageobjids

while (@s1>'')
begin
set @i = charindex(',', @s1)
if (@i>0)
begin
set @s2 = substring(@s1,1,@i-1)
set @s1 = substring(@s1,@i+1,len(@s1)-@i)
end else
begin
set @s2 = @s1
set @s1 = ''
end
if(@IDs>'') 
set @IDs = @IDs +','+@s2
else 
set @IDs = @s2
insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end

if (exists (select top 1 * from @pobjs))
begin
set @sql='
select * from openquery(posformal,''select m.id,m.docno,m.billdate ,to_char(m.STATUSTIME,''''YYYYMMDD'''') createdate,c.NAME shopname,m.SALESREPS_NAME 
,p.NAME kh,p.VALUE cpname,a.VALUE1 color,a.VALUE2 msize
,mi.PRICELIST ,mi.PRICEACTUAL ,mi.DISCOUNT ,mi.QTY ,mi.TOT_AMT_LIST 
,mi.TOT_AMT_ACTUAL ,m.TOT_AMT_ACC 
from M_RETAIL m
join M_RETAILITEM mi on mi.M_RETAIL_ID=m.id 
join C_STORE c on c.id=m.C_STORE_ID
join M_PRODUCT p on p.id=mi.M_PRODUCT_ID
join M_ATTRIBUTESETINSTANCE a on a.id=mi.M_ATTRIBUTESETINSTANCE_ID
where m.C_VIP_ID='''''+@VIPID+''''' and m.STATUS=2 and m.ISACTIVE=''''Y'''' and m.IS_RCOPY=1
and m.id in ('+@IDs+') 
order by m.billdate desc,docno desc
'')    

'

exec (@sql)
return
end

---以下是第一次加载数据
set @sql='
select * from openquery(posformal,''select count(m.id) cnt
from M_RETAIL m
join M_RETAILITEM mi on mi.M_RETAIL_ID=m.id 
join C_STORE c on c.id=m.C_STORE_ID
join M_PRODUCT p on p.id=mi.M_PRODUCT_ID
join M_ATTRIBUTESETINSTANCE a on a.id=mi.M_ATTRIBUTESETINSTANCE_ID
where m.C_VIP_ID='''''+@VIPID+''''' and m.STATUS=2 and m.ISACTIVE=''''Y'''' and m.IS_RCOPY=1
'')    

select * from openquery(posformal,''select m.id
from M_RETAIL m
join M_RETAILITEM mi on mi.M_RETAIL_ID=m.id 
join C_STORE c on c.id=m.C_STORE_ID
join M_PRODUCT p on p.id=mi.M_PRODUCT_ID
join M_ATTRIBUTESETINSTANCE a on a.id=mi.M_ATTRIBUTESETINSTANCE_ID
where m.C_VIP_ID='''''+@VIPID+''''' and m.STATUS=2 and m.ISACTIVE=''''Y'''' and m.IS_RCOPY=1
and rownum<='+convert(varchar(20),@amount)+'
order by m.billdate desc,docno desc
'')    


select * from openquery(posformal,''select m.id,m.docno,m.billdate ,to_char(m.STATUSTIME,''''YYYYMMDD'''') createdate,c.NAME shopname,m.SALESREPS_NAME 
,p.NAME kh,p.VALUE cpname,a.VALUE1 color,a.VALUE2 msize
,mi.PRICELIST ,mi.PRICEACTUAL ,mi.DISCOUNT ,mi.QTY ,mi.TOT_AMT_LIST 
,mi.TOT_AMT_ACTUAL ,m.TOT_AMT_ACC 
from M_RETAIL m
join M_RETAILITEM mi on mi.M_RETAIL_ID=m.id 
join C_STORE c on c.id=m.C_STORE_ID
join M_PRODUCT p on p.id=mi.M_PRODUCT_ID
join M_ATTRIBUTESETINSTANCE a on a.id=mi.M_ATTRIBUTESETINSTANCE_ID
where m.C_VIP_ID='''''+@VIPID+''''' and m.STATUS=2 and m.ISACTIVE=''''Y'''' and m.IS_RCOPY=1
and rownum<='+convert(varchar(20),@pagesize)+'
order by m.billdate desc,docno desc
'')    

'
/*@sql='
select * from openquery(posformal,''select m.id,m.docno,m.billdate 单据日期,to_char(m.STATUSTIME,''''YYYYMMDD'''') 建单日期,c.NAME shopname,m.SALESREPS_NAME 
,p.NAME kh,p.VALUE cpname,a.VALUE1 color,a.VALUE2 msize
,mi.PRICELIST 标准价,mi.PRICEACTUAL 成交价,mi.DISCOUNT 折扣,mi.QTY 数量,mi.TOT_AMT_LIST 标准金额
,mi.TOT_AMT_ACTUAL 成交金额,m.TOT_AMT_ACC 总结算金额
from M_RETAIL m
join M_RETAILITEM mi on mi.M_RETAIL_ID=m.id 
join C_STORE c on c.id=m.C_STORE_ID
join M_PRODUCT p on p.id=mi.M_PRODUCT_ID
join M_ATTRIBUTESETINSTANCE a on a.id=mi.M_ATTRIBUTESETINSTANCE_ID
where m.C_VIP_ID='''''+@VIPID+''''' and m.STATUS=2 and m.ISACTIVE=''''Y'''' and m.IS_RCOPY=1
'')    order by 单据日期 desc,docno desc*/

exec (@sql)


END