feat(finance): manage token plans and simplify recharge checkout
This commit is contained in:
@@ -29,6 +29,30 @@ def summary():
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/plans")
|
||||
def token_plans():
|
||||
db = _session()
|
||||
try:
|
||||
return ApiResponse(data=finance_service.list_token_plans(db))
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.patch("/plans/{plan_id}")
|
||||
def update_token_plan(plan_id: str, body: dict = Body(...)):
|
||||
db = _session()
|
||||
try:
|
||||
finance_service.update_token_plan(
|
||||
db, plan_id, amount=int(body.get("amount") or 0), price=float(body.get("price") or 0),
|
||||
name=str(body.get("name") or ""), badge=str(body.get("badge") or ""), desc=str(body.get("desc") or ""),
|
||||
)
|
||||
return ApiResponse(message="充值套餐已更新")
|
||||
except (ValueError, FinanceServiceError) as exc:
|
||||
_raise(exc if isinstance(exc, FinanceServiceError) else FinanceServiceError("积分和价格格式不正确"))
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/orders")
|
||||
def orders(
|
||||
page: int = Query(1, ge=1),
|
||||
|
||||
@@ -28,6 +28,23 @@ def _money(cents):
|
||||
|
||||
|
||||
class FinanceService:
|
||||
@staticmethod
|
||||
def list_token_plans(db):
|
||||
rows = db.execute(text('SELECT id,name,amount,price,badge,"desc",virtual_product_id FROM token_plans ORDER BY price ASC')).fetchall()
|
||||
return [dict(row._mapping) for row in rows]
|
||||
|
||||
@staticmethod
|
||||
def update_token_plan(db, plan_id: str, *, amount: int, price: float, name: str, badge: str, desc: str):
|
||||
if amount <= 0 or price < 0 or not name.strip():
|
||||
raise FinanceServiceError("套餐名称、正数积分和非负价格不正确")
|
||||
result = db.execute(text('''UPDATE token_plans SET name=:name,amount=:amount,price=:price,badge=:badge,"desc"=:desc WHERE id=:id'''), {
|
||||
"id": plan_id, "name": name.strip()[:80], "amount": amount, "price": price,
|
||||
"badge": badge.strip()[:80], "desc": desc.strip()[:200],
|
||||
})
|
||||
if result.rowcount == 0:
|
||||
raise FinanceServiceError("充值套餐不存在", 404)
|
||||
db.commit()
|
||||
|
||||
@staticmethod
|
||||
def _tables_ready(db) -> bool:
|
||||
names = {
|
||||
|
||||
Reference in New Issue
Block a user