fix code
This commit is contained in:
@@ -13,6 +13,7 @@ from backend.common.const import (
|
||||
POINTS_ACTION_REFUND_FREEZE,
|
||||
POINTS_ACTION_REFUND_UNFREEZE,
|
||||
POINTS_ACTION_REFUND_DEDUCT,
|
||||
POINTS_ACTION_COUPON,
|
||||
)
|
||||
|
||||
|
||||
@@ -108,10 +109,19 @@ class PointsService:
|
||||
|
||||
current_balance = points_account.balance
|
||||
|
||||
# 原子性增加积分并设置30天有效期
|
||||
result = await points_dao.add_points_atomic(db, user_id, amount, extend_expiration=True)
|
||||
|
||||
result = await points_dao.add_points_atomic(db, user_id, amount)
|
||||
if not result:
|
||||
return False
|
||||
|
||||
coupon_lot = PointsLot(
|
||||
user_id=user_id,
|
||||
order_id=None,
|
||||
points_total=amount,
|
||||
points_remaining=amount,
|
||||
amount_cents_total=0,
|
||||
)
|
||||
await points_lot_dao.add(db, coupon_lot)
|
||||
|
||||
# 记录积分变动日志,包含coupon id和区分action为coupon
|
||||
log_details = {
|
||||
@@ -122,10 +132,10 @@ class PointsService:
|
||||
new_balance = current_balance + amount
|
||||
await points_log_dao.add_log(db, {
|
||||
"user_id": user_id,
|
||||
"action": POINTS_ACTION_COUPON, # 使用充值action类型
|
||||
"action": POINTS_ACTION_COUPON,
|
||||
"amount": amount,
|
||||
"balance_after": new_balance,
|
||||
"related_id": coupon_id, # 关联coupon id
|
||||
"related_id": coupon_id,
|
||||
"details": log_details
|
||||
})
|
||||
|
||||
@@ -180,18 +190,29 @@ class PointsService:
|
||||
return False
|
||||
current_balance = points_account.balance
|
||||
|
||||
# 批次扣减(FIFO)
|
||||
# 批次扣减
|
||||
remaining = amount
|
||||
lot_items = await points_lot_dao.list_available(db, user_id)
|
||||
alloc_details = []
|
||||
for lot in lot_items:
|
||||
if remaining <= 0:
|
||||
break
|
||||
take = min(lot.points_remaining, remaining)
|
||||
if action == POINTS_ACTION_REFUND_DEDUCT and related_id is not None:
|
||||
target_lot = await points_lot_dao.get_by_order(db, related_id)
|
||||
if not target_lot:
|
||||
return False
|
||||
take = min(target_lot.points_remaining, remaining)
|
||||
if take > 0:
|
||||
await points_lot_dao.deduct_from_lot(db, lot.id, take)
|
||||
alloc_details.append({"lot_id": lot.id, "order_id": lot.order_id, "points": take})
|
||||
await points_lot_dao.deduct_from_lot(db, target_lot.id, take)
|
||||
alloc_details.append({"lot_id": target_lot.id, "order_id": target_lot.order_id, "points": take})
|
||||
remaining -= take
|
||||
else:
|
||||
|
||||
lot_items = await points_lot_dao.list_available(db, user_id)
|
||||
for lot in lot_items:
|
||||
if remaining <= 0:
|
||||
break
|
||||
take = min(lot.points_remaining, remaining)
|
||||
if take > 0:
|
||||
await points_lot_dao.deduct_from_lot(db, lot.id, take)
|
||||
alloc_details.append({"lot_id": lot.id, "order_id": lot.order_id, "points": take})
|
||||
remaining -= take
|
||||
result = await points_dao.deduct_balance_atomic(db, user_id, amount)
|
||||
if not result:
|
||||
return False
|
||||
|
||||
@@ -20,12 +20,12 @@ from backend.utils.timezone import timezone
|
||||
async def wx_user_index_history() -> None:
|
||||
"""异步实现 wx_user_index_history 任务"""
|
||||
# 计算前一天的时间范围(统一为 UTC,避免时区误差)
|
||||
today_local = timezone.now().date()
|
||||
today_local = datetime.now().date()
|
||||
yesterday = today_local - timedelta(days=1)
|
||||
yesterday_start_naive = datetime(yesterday.year, yesterday.month, yesterday.day)
|
||||
yesterday_end_naive = datetime(today_local.year, today_local.month, today_local.day)
|
||||
yesterday_start = timezone.to_utc(yesterday_start_naive)
|
||||
yesterday_end = timezone.to_utc(yesterday_end_naive)
|
||||
yesterday_start = yesterday_start_naive
|
||||
yesterday_end = yesterday_end_naive
|
||||
|
||||
async with async_db_session() as db:
|
||||
# 优化:通过 audit_log 表查询有相关记录的用户,避免遍历所有用户
|
||||
|
||||
@@ -10,11 +10,14 @@ from backend.app.admin.service.wxpay_service import WxPayService
|
||||
from random import sample
|
||||
from string import ascii_letters, digits
|
||||
from wechatpayv3 import WeChatPay, WeChatPayType
|
||||
from backend.app.admin.tasks import wx_user_index_history
|
||||
|
||||
|
||||
app = register_app()
|
||||
|
||||
@app.get("/")
|
||||
async def read_root():
|
||||
await wx_user_index_history()
|
||||
return {"Hello": f"World"}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user