Merge pull request #101 from xpack-ai/feature/1.3-liujian
The transaction list now includes "balance" and "description" fields.
This commit is contained in:
5 files changed
+80
-61
No files matched your search
@@ -89,29 +89,5 @@ def get_user_order_list_by_user_id(
|
||||
for s in sl:
|
||||
status_list.append(int(s.strip()))
|
||||
total, orders = user_wallet_history_service.success_order_list_by_user(user_id, offset, page_size, order_type_list, status_list)
|
||||
if not orders:
|
||||
return ResponseUtils.success_page(data=[], total=total, page_num=page, page_size=page_size)
|
||||
result = []
|
||||
for order in orders:
|
||||
confirm_at = ""
|
||||
if order.status == 1:
|
||||
confirm_at = order.updated_at
|
||||
for order in orders:
|
||||
info = {
|
||||
"id": order.id,
|
||||
"user_id": order.user_id,
|
||||
"order_id": order.transaction_id,
|
||||
"payment_type": order.payment_method,
|
||||
"payment_state": order.status,
|
||||
"amount": order.amount,
|
||||
"create_at": order.created_at,
|
||||
"confirm_at": confirm_at,
|
||||
}
|
||||
if order.type == TransactionType.API_CALL:
|
||||
info["order_type"] = "purchase"
|
||||
elif order.type == TransactionType.DEPOSIT:
|
||||
info["order_type"] = "recharge"
|
||||
else:
|
||||
info["order_type"] = order.type
|
||||
result.append(info)
|
||||
return ResponseUtils.success_page(data=result, total=total, page_num=page, page_size=page_size)
|
||||
|
||||
return ResponseUtils.success_page(data=orders, total=total, page_num=page, page_size=page_size)
|
||||
@@ -11,7 +11,7 @@ from services.admin_service.services.user_task_service import UserTaskService
|
||||
from services.admin_service.services.auth_service import AuthService
|
||||
from services.admin_service.services.resource_group_service import ResourceGroupService
|
||||
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
|
||||
from services.common.models.user_wallet_history import TransactionType
|
||||
|
||||
from typing import Optional,List
|
||||
|
||||
router = APIRouter()
|
||||
@@ -33,6 +33,7 @@ def get_resource_group_service(db: Session = Depends(get_db)) -> ResourceGroupSe
|
||||
def get_order_service(db: Session = Depends(get_db)) -> UserWalletHistoryService:
|
||||
return UserWalletHistoryService(db)
|
||||
|
||||
|
||||
@router.get("/info", response_model=dict)
|
||||
def get_user(request: Request, user_wallet: UserWalletService = Depends(get_user_wallet), user_task: UserTaskService = Depends(get_user_task_service), resource_group_service: ResourceGroupService = Depends(get_resource_group_service)):
|
||||
"""Get current user information and wallet balance."""
|
||||
@@ -125,29 +126,5 @@ def get_order_list(
|
||||
for s in sl:
|
||||
status_list.append(int(s.strip()))
|
||||
total, orders = user_wallet_history_service.success_order_list_by_user(user.id, offset, page_size, order_type_list, status_list)
|
||||
if not orders:
|
||||
return ResponseUtils.success_page(data=[], total=total, page_num=page, page_size=page_size)
|
||||
result = []
|
||||
for order in orders:
|
||||
confirm_at = ""
|
||||
if order.status == 1:
|
||||
confirm_at = order.updated_at
|
||||
for order in orders:
|
||||
info = {
|
||||
"id": order.id,
|
||||
"user_id": order.user_id,
|
||||
"order_id": order.transaction_id,
|
||||
"payment_type": order.payment_method,
|
||||
"payment_state": order.status,
|
||||
"amount": order.amount,
|
||||
"create_at": order.created_at,
|
||||
"confirm_at": confirm_at,
|
||||
}
|
||||
if order.type == TransactionType.API_CALL:
|
||||
info["order_type"] = "purchase"
|
||||
elif order.type == TransactionType.DEPOSIT:
|
||||
info["order_type"] = "recharge"
|
||||
else:
|
||||
info["order_type"] = order.type
|
||||
result.append(info)
|
||||
return ResponseUtils.success_page(data=result, total=total, page_num=page, page_size=page_size)
|
||||
|
||||
return ResponseUtils.success_page(data=orders, total=total, page_num=page, page_size=page_size)
|
||||
@@ -18,8 +18,20 @@ class UserWalletHistoryRepository:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_by_id(self, transaction_id: str) -> Optional[UserWalletHistory]:
|
||||
"""
|
||||
Get a user wallet history record by ID.
|
||||
|
||||
def add_deposit(self, user_id: str, amount: float, payment_method: str, transaction_id: str = "", status: int = 0) -> UserWalletHistory:
|
||||
Args:
|
||||
transaction_id: Transaction ID
|
||||
|
||||
Returns:
|
||||
Optional[UserWalletHistory]: Found record or None
|
||||
"""
|
||||
return self.db.query(UserWalletHistory).filter(UserWalletHistory.id == transaction_id).first()
|
||||
|
||||
def add_deposit(self, user_id: str, amount: float, payment_method: str, transaction_id: str = "", status: int = 0, balance_after: float = 0.00) -> UserWalletHistory:
|
||||
"""
|
||||
Create a new deposit record.
|
||||
|
||||
@@ -40,7 +52,7 @@ class UserWalletHistoryRepository:
|
||||
user_id=user_id,
|
||||
payment_method=PaymentMethod(payment_method),
|
||||
amount=amount,
|
||||
balance_after=0.00,
|
||||
balance_after=balance_after,
|
||||
type=TransactionType.DEPOSIT,
|
||||
transaction_id=transaction_id,
|
||||
status=status,
|
||||
@@ -81,7 +93,7 @@ class UserWalletHistoryRepository:
|
||||
self.db.refresh(history)
|
||||
return history
|
||||
|
||||
def set_balance(self,user_id: str,amount:float, payment_method: str, transaction_id: str = "", status: int = 0) -> UserWalletHistory:
|
||||
def set_balance(self,user_id: str,amount:float, payment_method: str, transaction_id: str = "", status: int = 0, balance_after: float = 0.00) -> UserWalletHistory:
|
||||
"""
|
||||
Create a new balance record.
|
||||
|
||||
@@ -102,7 +114,7 @@ class UserWalletHistoryRepository:
|
||||
user_id=user_id,
|
||||
payment_method=PaymentMethod(payment_method),
|
||||
amount=amount,
|
||||
balance_after=0.00,
|
||||
balance_after=balance_after,
|
||||
type=TransactionType.RESET,
|
||||
status=status, # 0=new, 1=completed, 2=refunded
|
||||
created_at=now,
|
||||
|
||||
@@ -403,11 +403,21 @@ class PaymentService:
|
||||
try:
|
||||
if typ == "incr":
|
||||
self.user_wallet_history_repository.add_deposit(
|
||||
user_id=user_id, amount=amount, payment_method=payment_method, transaction_id=transaction_id,status=1
|
||||
user_id=user_id,
|
||||
amount=amount,
|
||||
payment_method=payment_method,
|
||||
transaction_id=transaction_id,
|
||||
status=1,
|
||||
balance_after=new_balance,
|
||||
)
|
||||
elif typ == "set":
|
||||
self.user_wallet_history_repository.set_balance(
|
||||
user_id=user_id, amount=amount, payment_method=payment_method, transaction_id=transaction_id,status=1
|
||||
user_id=user_id,
|
||||
amount=amount,
|
||||
payment_method=payment_method,
|
||||
transaction_id=transaction_id,
|
||||
status=1,
|
||||
balance_after=new_balance,
|
||||
)
|
||||
self._delete_wallet_cache(user_id)
|
||||
logger.info(f"Successfully updated balance for user_id {user_id}: {current_wallet.balance} -> {new_balance}")
|
||||
|
||||
@@ -3,11 +3,16 @@ from services.common.models.user_wallet_history import UserWalletHistory
|
||||
from services.admin_service.repositories.user_wallet_history_repository import UserWalletHistoryRepository
|
||||
from typing import Optional, Dict, Any, List, Tuple
|
||||
from datetime import datetime
|
||||
from services.common.models.user_wallet_history import TransactionType
|
||||
from services.admin_service.repositories.mcp_service_repository import McpServiceRepository
|
||||
import json
|
||||
|
||||
|
||||
|
||||
class UserWalletHistoryService:
|
||||
def __init__(self, db: Session):
|
||||
self.user_wallet_history_repository = UserWalletHistoryRepository(db)
|
||||
self.mcp_service_repository = McpServiceRepository(db)
|
||||
|
||||
def add_deposit(self, user_id: str, amount: float, payment_method: str) -> UserWalletHistory:
|
||||
return self.user_wallet_history_repository.add_deposit(user_id, amount, payment_method)
|
||||
@@ -18,8 +23,47 @@ class UserWalletHistoryService:
|
||||
def success_deposit_order_list(self, offset: int, limit: int) -> Tuple[int, List[UserWalletHistory]]:
|
||||
return self.user_wallet_history_repository.success_deposit_order_list(offset, limit)
|
||||
|
||||
def success_order_list_by_user(self, user_id: str, offset: int, limit: int, order_type: Optional[List[str]] = None, status: Optional[List[int]] = None) -> Tuple[int, List[UserWalletHistory]]:
|
||||
return self.user_wallet_history_repository.success_order_list_by_user(user_id, offset, limit, order_type, status)
|
||||
def success_order_list_by_user(self, user_id: str, offset: int, limit: int, order_type: Optional[List[str]] = None, status: Optional[List[int]] = None) -> Tuple[int, List[dict]]:
|
||||
total, orders = self.user_wallet_history_repository.success_order_list_by_user(user_id, offset, limit, order_type, status)
|
||||
if total == 0:
|
||||
return total, []
|
||||
result = []
|
||||
for order in orders:
|
||||
confirm_at = ""
|
||||
if order.status == 1:
|
||||
confirm_at = order.updated_at
|
||||
info = {
|
||||
"id": order.id,
|
||||
"user_id": order.user_id,
|
||||
"order_id": order.transaction_id,
|
||||
"payment_type": order.payment_method,
|
||||
"payment_state": order.status,
|
||||
"amount": order.amount,
|
||||
"create_at": order.created_at,
|
||||
"confirm_at": confirm_at,
|
||||
}
|
||||
if order.type == TransactionType.API_CALL:
|
||||
info["order_type"] = "purchase"
|
||||
try:
|
||||
callback_data = json.loads(order.callback_data)
|
||||
service = self.mcp_service_repository.get_by_id(callback_data["service_id"])
|
||||
if service:
|
||||
info["description"] = service.name
|
||||
except:
|
||||
info["description"] = "-"
|
||||
info["balance_after"] = order.balance_after
|
||||
elif order.type == TransactionType.DEPOSIT:
|
||||
info["order_type"] = "recharge"
|
||||
if order.status == 1:
|
||||
info["balance_after"] = order.balance_after
|
||||
else:
|
||||
info["balance_after"] = "-"
|
||||
info["description"] = order.payment_method
|
||||
else:
|
||||
info["order_type"] = order.type
|
||||
result.append(info)
|
||||
return total, result
|
||||
return total, orders
|
||||
|
||||
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
|
||||
return self.user_wallet_history_repository.order_list(payment_method, status, start, end)
|
||||
|
||||
Reference in new issue
Block a user