最近写了一个关于销售返现的存储过程,与大家分享一下

一、销售返现的存储过程

我也是很长时间没有写存储过程了,最近也是项目场景需要,在网上查了一些相关资料,也写了一下,有不足的地方希望大家指出来,感谢!

SQL语句:

DROP PROCEDURE IF EXISTS PRO_PAY_BONUS;

DELIMITER $$

CREATE PROCEDURE PRO_PAY_BONUS(IN lowestArea int,in otherArea1 int,in otherArea2 int)

BEGIN

#select "三分区销售额最低区域:"+lowestArea;

# 1.获取三分区返现最新配置数据。

select @lowerAreaRebate:= lower_area_rebate_ratio,

@lotteryThreshold:= lottery_mini_threshold,

@lotteryTimes:= others_area_lottery_times

from mall_tg_three_area_rebate where retbate_status=1 order by effect_date desc limit 1;

#select @lowerAreaRebate;

#select @lotteryThreshold;

#select @lotteryTimes;

# 2.处理三分区销售额最低区域的用户列表

begin

declare user_id bigint(20);#用户ID

declare price decimal(10,2);#用户金额

declare tmp_price decimal(10,2);#临时用户金额

declare cnt int default 0;#定义判断标识符

# 创建结束标志变量

declare done int default false;

# 创建游标 获取user_id和price的集合

declare lowSalesAreaUser cursor for select b.user_id,sum(a.price) as price from

mall_gd_order_goods a left join mall_gd_order b on a.order_id = b.id

where a.special_area = lowestArea and b.user_id is not null group by b.user_id;

# 指定游标循环结束时的返回值

declare continue HANDLER for not found set done = true;

# 打开游标

open lowSalesAreaUser;

# 开始循环游标里的数据

read_loop:LOOP

# 根据游标当前指向的一条数据 插入到上面申明的局部变量中

fetch lowSalesAreaUser into user_id,price;

# 判断游标的循环是否结束

IF done THEN

LEAVE read_loop;

END IF;

################################## 发放奖励:销售区域最低的用户返现 #############################

set tmp_price = price*@lowerAreaRebate;

select user_id,tmp_price;

if tmp_price > 0 then

###############返现用户插入用户流水表:mall_user_capital_flow

insert into mall_user_capital_flow(flow_type,expenses_receipts_type,amount,user_id,create_time)

values(1,0,tmp_price,user_id,now());

###############返现用户账户表:mall_user_account

SET SQL_SAFE_UPDATES = 0;

select count(1) into cnt from mall_user_account ua where ua.user_id = user_id;

if(cnt > 0) then

update mall_user_account set available_amount=available_amount + tmp_price,

can_reflect_amount = can_reflect_amount + tmp_price where user_id = user_id;

else

insert into mall_user_account(user_id,available_amount,can_reflect_amount,create_time)

values(user_id,tmp_price,tmp_price,now());

end if;

end if;

# 有loop 就一定要有end loop

END LOOP;

# 关闭游标

close lowSalesAreaUser;

end;

# 3.处理其他两个区域的用户列表

begin

declare user_id bigint(20);#用户ID

declare price decimal(10,2);#用户金额

declare times int default 0;#临时用户金额

declare cnt int default 0;#定义判断标识符

# 创建结束标志变量

declare done int default false;

# 创建游标 获取user_id和price的集合

declare lowSalesAreaUser cursor for select b.user_id,sum(a.price) as price from

mall_gd_order_goods a left join mall_gd_order b on a.order_id = b.id

where (a.special_area = otherArea1 or a.special_area = otherArea2) and b.user_id is not null

group by b.user_id;

# 指定游标循环结束时的返回值

declare continue HANDLER for not found set done = true;

# 打开游标

open lowSalesAreaUser;

# 开始循环游标里的数据

read_loop:LOOP

# 根据游标当前指向的一条数据 插入到上面申明的局部变量中

fetch lowSalesAreaUser into user_id,price;

# 判断游标的循环是否结束

IF done THEN

LEAVE read_loop;

END IF;

################################## 赠送抽奖次数:(其他2个区的用户送通证次数,见业务规则:抽奖) #############################

if price > @lotteryTimes then set times = price/@lotteryTimes;

elseif price>= @lotteryThreshold then set times = 1;

else set times = 0;

end if;

select user_id,times;

################ 赠送抽奖次数,其他2个区的用户送通证次数向mall_gd_prize_sources 插入数据

if times > 0 then

insert into mall_gd_prize_sources(user_id,prize_type,times,create_time) values(user_id,0,times,now());

select count(1) into cnt from mall_gd_prize_sum gp where gp.user_id = user_id;

if(cnt > 0) then

update mall_gd_prize_sum set prize_accumulation=prize_accumulation +times,surplus_times=surplus_times + times

where user_id = user_id;

else

insert into mall_gd_prize_sum(user_id,prize_accumulation,surplus_times,today_date,create_time)

values(user_id,times,times,DATE_FORMAT(now(),'%Y-%m-%d'),now());

end if;

end if;

# 有loop 就一定要有end loop

END LOOP;

# 关闭游标

close lowSalesAreaUser;

end;

END$$

DELIMITER ;

 

call PRO_PAY_BONUS(1,2,3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些实现这些功能的Python代码。下面是一个简单的示例: ``` class Supermarket: def __init__(self, balance): self.balance = balance self.customers = {} def add_customer(self, name): self.customers[name] = {"balance": 0, "is_member": False} def add_balance(self, customer_name, amount): if customer_name not in self.customers: self.add_customer(customer_name) self.customers[customer_name]["balance"] += amount def add_membership(self, customer_name): if customer_name not in self.customers: self.add_customer(customer_name) self.customers[customer_name]["is_member"] = True def purchase(self, customer_name, amount, payment_type): if customer_name not in self.customers: self.add_customer(customer_name) if self.balance >= amount: self.balance -= amount self.customers[customer_name]["balance"] -= amount print("Purchase successful!") else: print("Not enough balance in the supermarket!") if self.customers[customer_name]["is_member"]: if payment_type == "cash": self.customers[customer_name]["balance"] += amount * 0.05 print("5% cashback added to customer's balance!") elif payment_type == "credit_card": print("Credit card payment successful!") else: print("Invalid payment type!") else: print("Customer is not a member, no benefits available.") # Example usage: supermarket = Supermarket(1000) print(f"Supermarket balance before purchase: {supermarket.balance}") supermarket.purchase("John", 50, "cash") print(f"Supermarket balance after purchase: {supermarket.balance}") print(f"John's customer balance after purchase: {supermarket.customers['John']['balance']}") supermarket.add_membership("John") supermarket.purchase("John", 50, "credit_card") print(f"John's customer balance after purchase with credit card: {supermarket.customers['John']['balance']}") ``` 这段代码定义了一个 `Supermarket` 类,它具有购买、添加客户、添加余额、添加会员等功能。上面的示例演示了如何使用这些功能来实现超市购物。 在这个示例中,我们首先创建一个超市对象,并将其余额初始化为 1000。然后我们尝试以 John 的名义购买价值 50 的物品,使用现金付款。因为我们的超市余额足够支付,所以这个购买操作成功了,并且 John 的客户余额减去了 50。然后我们将 John 添加为会员,并用信用卡再次购买了 50 的物品。由于 John 是会员,并且使用的是信用卡支付,所以他获得了 5% 的返现。最后,我们打印出了 John 的客户余额,以确保一切正常工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值