首页 > 代码库 > 【瑞星系统】修改会员积分处理

【瑞星系统】修改会员积分处理

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER procedure GetVIPMark(@SLDAT datetime, @PNO integer, @SNO integer, @VIPNO varchar(20), @PLUInfo text , @CurVIPMark varchar(50) output , @TotalVIPMark varchar(150) output, @Return bit output)
as
begin
declare @UID bigint
declare @CharTable table (pluno varchar(50), qty varchar(50), mark numeric(12,4))
declare @TMP varchar(50)
declare @B sql_variant, @C sql_variant
declare @LEN int, --字符串长度
@B_POS int, --开始位置
@E_POS int, --结束位置
@C_POS int, --当前位置
@F_POS int,
@G_POS int,
@summark numeric(12,4), -------wzx20040708,一笔交易的总金额
@TMP_LEN int,@i int
declare @PLU varchar(50), @MARK varchar(50), @QTY varchar(50)
select @Return = 0
select @CurVIPMark=‘0‘ -----wzx2004-08-31
select @TotalVIPMark=‘0‘ -----wzx2004-08-31

--将传入的文本信息写入临时表,记录之间通过‘;‘进行分割,商品编码,数量,销售金额通过‘,‘进行分割。
select @LEN = datalength(@PLUInfo), @B_POS = 1, @E_POS = 0
while @B_POS <= @LEN
begin
set @G_POS = 0
set @C_POS = patindex(‘%;%‘, substring(@PLUInfo, @B_POS, @LEN))
set @E_POS = case @C_POS when 0 then @LEN + 1 else @E_POS + @C_POS end
set @TMP_LEN = len(substring(@PLUInfo, @B_POS , @E_POS - @B_POS ))
set @F_POS = patindex(‘%,%‘, substring(substring(@PLUInfo, @B_POS , @E_POS - @B_POS ), 1, @TMP_LEN))
set @G_POS = case @F_POS when 0 then @TMP_LEN + 1 else @G_POS + @F_POS end
select @PLU = substring(substring(@PLUInfo, @B_POS , @E_POS - @B_POS ), 1 , @G_POS - 1 )
select @Mark = substring(substring(@PLUInfo, @B_POS , @E_POS - @B_POS ), @G_POS + 1, @TMP_LEN)
select @TMP = convert(varchar(50), @Mark)
select @i = patindex(‘%,%‘, @TMP)
select @QTY = substring(@TMP, 1, @i - 1)
select @Mark = substring(@TMP, @i + 1, @TMP_LEN)
if (isnull(@Mark , ‘‘) = ‘‘) select @Mark = ‘0‘
if (isnull(@QTY , ‘‘) = ‘‘) select @QTY = ‘0‘
insert into @CharTable( pluno , qty , mark)
values(@PLU, convert(varchar(50), (convert(numeric(12,2), @QTY) / 100)), convert(varchar(50), (convert(numeric(12,2), @Mark) / 100)))
set @B_POS = @E_POS + 1
end

------wjk,货号不存在就不返回积分
if not exists(select 1 from @chartable a join basplumain b on a.pluno=b.pluno)
return

--增加对于销售金额的判断。如果销售金额小于10元,则不进行处理。
select @summark=sum(mark) from @chartable ---------wzx20040708
--更新当前消费表中的积分信息,WJK,20050204(记重商品重算数量)
update a set mark = case when c.VIPRAT < 0 then 0
when c.VIPRAT = 0 and convert(numeric(12,2), @summark) >= 1 then a.mark -----wzx20040708
when c.VIPRAT > 0 and convert(numeric(12,2), @summark) >= 1 then case when c.PROP = 1 then case when c.SLPRC = 0 then c.viprat * a.Mark
else C.VIPRAT * Convert(numeric(12,3),(a.MARK / c.SLPRC))
end
else C.viprat * a.qty
end ---------wzx20040708
end
from @CharTable a join basplumain b on a.pluno = b.pluno join baspluprc c on b.pluid = c.pluid

select @CurVIPMark = FLOOR(convert(varchar(50), convert(numeric(16,2), isnull(sum(mark), 0)))) --向下取整
from @CharTable

select @TotalVIPMark = convert(varchar(50), convert(numeric(30,2), isnull(sum(addvalue), 0)))
from basvipadup
where ltrim(rtrim(VIPNO)) = ltrim(rtrim(@VIPNO))
group by VIPNO

if (ltrim(rtrim(isnull(@TotalVIPMark, ‘‘)))= ‘‘)
select @TotalVIPMark = 0

select @TotalVIPMark = floor(convert(numeric(30,2), @TotalVIPMark))
-- select @TotalVIPMark = convert(numeric(16,2), @CurVIPMark) + convert(numeric(30,2), @TotalVIPMark)
--wjk 修改于20041020,将前台VIP数据直接写入临时表,晚上关帐时,关联POS流水进行VIP积分处理。
begin tran
select @UID = dbo.GetUID(@SLDAT, @PNO, @SNO)
if @@error > 0
begin
select @CurVIPMark = 0
select @TotalVIPMark = 0
rollback tran
return
end

if not exists(select 1 from POSVIPMARK(nolock) where UID = @UID)
begin
insert into POSVIPMARK(UID, PLUNO, QTY, VIPMARK)
select UID = @UID, PLUNO, QTY, isnull(MARK,0)
from @CharTable
if @@error > 0
begin
select @CurVIPMark = 0
select @TotalVIPMark = 0
rollback tran
return
end
end
else
begin
select @CurVIPMark = 0
select @TotalVIPMark = 0
rollback tran
return
end

commit tran

select @Return = 1
select 1
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO