From 88325a0ba9cb4d92346dd38f014911e11dd329fa Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 29 Dec 2025 14:19:26 +0800 Subject: [PATCH 1/5] 1. Users can view transaction data on the client side. 2. The MCP service list supports tag filtering. --- .../admin_service/controllers/mcp_manager.py | 16 ++++- services/admin_service/controllers/order.py | 63 ++++++++++++++++++ services/admin_service/controllers/user.py | 66 ++++++++++++++++++- services/admin_service/controllers/web.py | 10 ++- .../repositories/mcp_service_repository.py | 15 ++++- .../user_wallet_history_repository.py | 24 ++++++- .../services/mcp_manager_service.py | 29 +++++++- .../services/user_wallet_history_service.py | 3 + services/common/config.py | 1 + 9 files changed, 218 insertions(+), 9 deletions(-) diff --git a/services/admin_service/controllers/mcp_manager.py b/services/admin_service/controllers/mcp_manager.py index fff8d1b..b413122 100644 --- a/services/admin_service/controllers/mcp_manager.py +++ b/services/admin_service/controllers/mcp_manager.py @@ -270,6 +270,20 @@ def get_mcp_service_simple_list( logger.error(f"Failed to get service simple list: {str(e)}") return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) +@router.get("/service/tags", summary="Get MCP service tags") +def get_mcp_service_tags( + request: Request, + mcp_manager_service: McpManagerService = Depends(get_mcp_manager), +): + if not UserUtils.is_admin(request): + return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION) + try: + tags = mcp_manager_service.get_tags_list(enabled_only=False) + return ResponseUtils.success(data=tags) + except Exception as e: + logger.error(f"Failed to get service tags: {str(e)}") + return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) + @router.get("/service/resource_group/list", summary="Get MCP service's resource group list") def get_mcp_service_resource_group_list( request: Request, @@ -360,4 +374,4 @@ def unbind_group( return ResponseUtils.success() except Exception as e: logger.error(f"Failed to unbind resource groups: {str(e)}") - return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) \ No newline at end of file + return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR) diff --git a/services/admin_service/controllers/order.py b/services/admin_service/controllers/order.py index 34d8821..fe900b9 100644 --- a/services/admin_service/controllers/order.py +++ b/services/admin_service/controllers/order.py @@ -1,4 +1,7 @@ +from os import stat +from typing import Optional, List from fastapi import APIRouter, Depends, Query +from services.common.models.user_wallet_history import TransactionType from sqlalchemy import table from sqlalchemy.orm import Session from services.common.database import get_db @@ -52,3 +55,63 @@ def get_user_order_list( ) return ResponseUtils.success_page(data=result, total=total, page_num=page, page_size=page_size) + +@router.get("/list/by_user", summary="Get user order list by user id") +def get_user_order_list_by_user_id( + user_id: str = Query(..., description="User ID"), + page: int = Query(1, description="Current page number"), + page_size: int = Query(15, description="Number of items per page"), + order_type: Optional[str] = Query(None, description="Order type"), + status: Optional[str] = Query(None, description="Order status"), + user_service: UserService = Depends(get_user_service), + user_wallet_history_service: UserWalletHistoryService = Depends(get_user_wallet_history_service), +): + """Get paginated list of user order history by user id.""" + user = user_service.get_by_id(user_id) + if not user: + return ResponseUtils.error(message="not found user", code=404) + offset = (page - 1) * page_size + order_type_list = [] + if order_type: + # 将字符串转成列表,中间使用逗号分隔 + ots = order_type.split(",") + for ot in ots: + ot = ot.strip() + if ot == "purchase": + order_type_list.append("api_call") + elif ot == "recharge": + order_type_list.append("deposit") + else: + order_type_list.append(ot) + status_list: Optional[List[int]] = [] + if status: + sl = status.split(",") + 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) \ No newline at end of file diff --git a/services/admin_service/controllers/user.py b/services/admin_service/controllers/user.py index 12a1004..73e102b 100644 --- a/services/admin_service/controllers/user.py +++ b/services/admin_service/controllers/user.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Depends, Request,Body +from fastapi import APIRouter, Depends, Request,Body,Query from sqlalchemy.orm import Session from services.common.database import get_db from services.admin_service.constants.user_task import user_task_ids @@ -10,6 +10,9 @@ from services.admin_service.services.user_wallet_service import UserWalletServic 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() @@ -27,6 +30,9 @@ def get_user_task_service(db: Session = Depends(get_db)) -> UserTaskService: def get_resource_group_service(db: Session = Depends(get_db)) -> ResourceGroupService: return ResourceGroupService(db) +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.""" @@ -87,3 +93,61 @@ def update_password( return ResponseUtils.error(message="not found user", code=404) +@router.get("/order/list", summary="Get order list") +def get_order_list( + request: Request, + page: int = Query(1, description="Current page number"), + page_size: int = Query(15, description="Number of items per page"), + order_type: Optional[str] = Query(None, description="Order type"), + status: Optional[str] = Query(None, description="Order status"), + user_wallet_history_service: UserWalletHistoryService = Depends(get_order_service), +): + """Get order list.""" + user = UserUtils.get_request_user(request) + if not user: + return ResponseUtils.error(message="not found user", code=404) + offset = (page - 1) * page_size + order_type_list = [] + if order_type: + # 将字符串转成列表,中间使用逗号分隔 + ots = order_type.split(",") + for ot in ots: + ot = ot.strip() + if ot == "purchase": + order_type_list.append("api_call") + elif ot == "recharge": + order_type_list.append("deposit") + else: + order_type_list.append(ot) + status_list: Optional[List[int]] = [] + if status: + sl = status.split(",") + 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) \ No newline at end of file diff --git a/services/admin_service/controllers/web.py b/services/admin_service/controllers/web.py index a5f8b47..36e48e4 100644 --- a/services/admin_service/controllers/web.py +++ b/services/admin_service/controllers/web.py @@ -29,6 +29,7 @@ def get_public_mcp_services( keyword: Optional[str] = Query(None, description="Search keyword"), page: Optional[int] = Query(1, description="Page number (starts from 1)"), page_size: Optional[int] = Query(10, description="Page size (default 10)"), + tag: Optional[str] = Query(None, description="Filter by tag"), mcp_manager_service: McpManagerService = Depends(get_mcp_manager), ): """Get paginated list of public MCP services with keyword search.""" @@ -43,12 +44,19 @@ def get_public_mcp_services( # Get service list - let any exception bubble up to middleware service_list, total = mcp_manager_service.get_public_services_paginated( - keyword=search_keyword, page=validated_page, page_size=validated_page_size + keyword=search_keyword, page=validated_page, page_size=validated_page_size, tag=tag ) logger.info(f"Successfully retrieved {len(service_list)} services") return ResponseUtils.success_page(data=service_list, page_num=validated_page, page_size=validated_page_size, total=total) +@router.get("/mcp_tags", summary="Get public MCP tags list") +def get_public_mcp_tags( + mcp_manager_service: McpManagerService = Depends(get_mcp_manager), +): + tags = mcp_manager_service.get_tags_list(enabled_only=True) + return ResponseUtils.success(data=tags) + @router.get("/mcp_service_info", summary="Get public MCP service information") def get_public_mcp_service_info( diff --git a/services/admin_service/repositories/mcp_service_repository.py b/services/admin_service/repositories/mcp_service_repository.py index 98c33eb..3a8e713 100644 --- a/services/admin_service/repositories/mcp_service_repository.py +++ b/services/admin_service/repositories/mcp_service_repository.py @@ -1,4 +1,5 @@ from datetime import datetime, timezone +from sqlalchemy import func from sqlalchemy.orm import Session from services.common.models.mcp_service import McpService from typing import Optional, Tuple, List @@ -103,7 +104,7 @@ class McpServiceRepository: self.db.refresh(mcp_service) return mcp_service - def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10) -> Tuple[List[McpService], int]: + def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[McpService], int]: """Get public service list with pagination, supports keyword search""" offset = (page - 1) * page_size @@ -112,9 +113,21 @@ class McpServiceRepository: if keyword: keyword = f"%{keyword}%" query = query.filter((McpService.name.like(keyword)) | (McpService.short_description.like(keyword))) + if tag: + normalized_tag = tag.strip() + if normalized_tag: + normalized_tags = func.replace(func.replace(McpService.tags, ", ", ","), " ,", ",") + query = query.filter(func.find_in_set(normalized_tag, normalized_tags) > 0) + total = query.count() services = query.order_by(McpService.created_at.desc()).offset(offset).limit(page_size).all() return services, total + + def get_tags_strings(self, enabled_only: bool = False) -> List[str]: + query = self.db.query(McpService.tags).filter(McpService.tags.isnot(None)).filter(McpService.tags != "") + if enabled_only: + query = query.filter(McpService.enabled == 1) + return [row[0] for row in query.all() if row and row[0]] diff --git a/services/admin_service/repositories/user_wallet_history_repository.py b/services/admin_service/repositories/user_wallet_history_repository.py index d47cc95..0da9b47 100644 --- a/services/admin_service/repositories/user_wallet_history_repository.py +++ b/services/admin_service/repositories/user_wallet_history_repository.py @@ -242,8 +242,8 @@ class UserWalletHistoryRepository: .all() ) return total, history - - def get_by_id(self, history_id: str) -> Optional[UserWalletHistory]: + + 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]]: """ Get user wallet history record by ID. @@ -253,7 +253,25 @@ class UserWalletHistoryRepository: Returns: Optional[UserWalletHistory]: UserWalletHistory object or None """ - return self.db.query(UserWalletHistory).filter(UserWalletHistory.id == history_id).first() + + query = self.db.query(UserWalletHistory).filter( + UserWalletHistory.user_id == user_id, + ) + if order_type and len(order_type) > 0: + query = query.filter(UserWalletHistory.type.in_(order_type)) + if status and len(status) > 0: + query = query.filter(UserWalletHistory.status.in_(status)) + total = query.count() + if total < offset: + return total, [] + history = ( + query + .order_by(UserWalletHistory.created_at.desc()) + .offset(offset) + .limit(limit) + .all() + ) + return total, history def add_consume_record(self, wallet_history: UserWalletHistory) -> Optional[UserWalletHistory]: """ diff --git a/services/admin_service/services/mcp_manager_service.py b/services/admin_service/services/mcp_manager_service.py index 67655ff..e40f654 100644 --- a/services/admin_service/services/mcp_manager_service.py +++ b/services/admin_service/services/mcp_manager_service.py @@ -329,6 +329,31 @@ class McpManagerService: """Get service list with pagination""" return self.mcp_service_repository.get_all_paginated(page=page, page_size=page_size) + def get_tags_list(self, enabled_only: bool = False) -> List[str]: + tags_strings = self.mcp_service_repository.get_tags_strings(enabled_only=enabled_only) + tag_counts: dict[str, int] = {} + canonical: dict[str, str] = {} + + for tags_str in tags_strings: + normalized_in_service: set[str] = set() + for tag in parse_tags_to_array(tags_str): + normalized = tag.casefold() + if not normalized: + continue + if normalized in normalized_in_service: + continue + normalized_in_service.add(normalized) + canonical.setdefault(normalized, tag) + + for normalized in normalized_in_service: + tag_counts[normalized] = tag_counts.get(normalized, 0) + 1 + + sorted_normalized = sorted( + tag_counts.keys(), + key=lambda normalized: (-tag_counts[normalized], canonical[normalized].casefold()), + ) + return [canonical[normalized] for normalized in sorted_normalized] + def create_service_from_openapi(self, openapi_data: OpenApiForAI) -> str: # Convert OpenApiForAI info to McpService object and McpToolApi object list, return service ID or exception. try: @@ -488,9 +513,9 @@ class McpManagerService: logger.error(f"Failed to update service from OpenAPI: {str(e)}") raise ValueError(f"Failed to update service from OpenAPI.") - def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10) -> Tuple[List[dict], int]: + def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[dict], int]: """Get public services list with pagination, returns formatted data with API info""" - services, total = self.mcp_service_repository.get_public_services_paginated(keyword, page, page_size) + services, total = self.mcp_service_repository.get_public_services_paginated(keyword, page, page_size, tag) service_list = [] for service in services: diff --git a/services/admin_service/services/user_wallet_history_service.py b/services/admin_service/services/user_wallet_history_service.py index caf9a31..85f2556 100644 --- a/services/admin_service/services/user_wallet_history_service.py +++ b/services/admin_service/services/user_wallet_history_service.py @@ -18,6 +18,9 @@ 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 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) diff --git a/services/common/config.py b/services/common/config.py index ff8b7ae..6dc19e6 100644 --- a/services/common/config.py +++ b/services/common/config.py @@ -58,6 +58,7 @@ class Config: "/api/auth/account/sign", "/api/auth/email/send_captcha", "/api/auth/google/sign", + "/api/web/mcp_tags", "/api/web/mcp_services", "/api/web/mcp_service_info", "/api/payment/callback_stripe", From 77ca12045da40e6e420856b80c56ba4a0228c07f Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 29 Dec 2025 15:43:10 +0800 Subject: [PATCH 2/5] The system settings support setting the tag_bar_display variable. --- scripts/resource/sql/version-1.3.0.sql | 8 ++++++++ services/admin_service/constants/sys_config_key.py | 1 + services/admin_service/controllers/init_config.py | 1 + services/admin_service/controllers/sys_config.py | 6 ++++++ 4 files changed, 16 insertions(+) create mode 100644 scripts/resource/sql/version-1.3.0.sql diff --git a/scripts/resource/sql/version-1.3.0.sql b/scripts/resource/sql/version-1.3.0.sql new file mode 100644 index 0000000..01477b5 --- /dev/null +++ b/scripts/resource/sql/version-1.3.0.sql @@ -0,0 +1,8 @@ +INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`) +VALUES ('xpack-tag-bar-display','tag_bar_display', 'true', 'Display tag bar on the top of the page', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) + ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP; + + +INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`) +VALUES ('xpack-version','version', '1.3.0', 'The user interface supports viewing the transaction list.', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) + ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP; diff --git a/services/admin_service/constants/sys_config_key.py b/services/admin_service/constants/sys_config_key.py index 4e0205e..2dd145d 100644 --- a/services/admin_service/constants/sys_config_key.py +++ b/services/admin_service/constants/sys_config_key.py @@ -7,6 +7,7 @@ KEY_SUBHEADLINE = "subheadline" KEY_LANGUAGE = "language" KEY_THEME = "theme" KEY_ABOUT_PAGE = "about_page" +KEY_TAG_BAR_DISPLAY = "tag_bar_display" KEY_ADMIN_USERNAME = "admin_username" KEY_ADMIN_PASSWORD = "admin_password" KEY_LOGIN_GOOGLE_CLIENT = "login_google_client_id" diff --git a/services/admin_service/controllers/init_config.py b/services/admin_service/controllers/init_config.py index e138335..54b276c 100644 --- a/services/admin_service/controllers/init_config.py +++ b/services/admin_service/controllers/init_config.py @@ -67,6 +67,7 @@ def get_config( "facebook_image_url": values.get(KEY_FACEBOOK_IMAGE_URL, ""), "social_account_facebook_url": values.get(KEY_SOCIAL_ACCOUNT_FACEBOOK_URL, ""), "social_account_x_url": values.get(KEY_SOCIAL_ACCOUNT_X_URL, ""), + "tag_bar_display": values.get(KEY_TAG_BAR_DISPLAY, "false").lower() in ("true", "t", "yes", "y", "1"), }, "faq": faq, "embeded_html": embeded_html, diff --git a/services/admin_service/controllers/sys_config.py b/services/admin_service/controllers/sys_config.py index d2ac13d..6c7a1a3 100644 --- a/services/admin_service/controllers/sys_config.py +++ b/services/admin_service/controllers/sys_config.py @@ -144,6 +144,7 @@ def get_sysconfig( "social_account_facebook_url": small_values.get(sys_config_key.KEY_SOCIAL_ACCOUNT_FACEBOOK_URL, ""), "social_account_x_url": small_values.get(sys_config_key.KEY_SOCIAL_ACCOUNT_X_URL, ""), "cname_a_ip": cname_a_ip, + "tag_bar_display": small_values.get(sys_config_key.KEY_TAG_BAR_DISPLAY, "").lower() in ("true", "t", "yes", "y", "1"), }, "account": { "username": admin_username, @@ -219,6 +220,9 @@ def set_sysconfig( facebook_image_url = platform.get("facebook_image_url") social_account_facebook_url = platform.get("social_account_facebook_url") social_account_x_url = platform.get("social_account_x_url") + tag_bar_display = platform.get("tag_bar_display") + + account = body.get("account", {}) admin_username = account.get("username") @@ -270,6 +274,7 @@ def set_sysconfig( (sys_config_key.KEY_FACEBOOK_IMAGE_URL, facebook_image_url, "Facebook image URL"), (sys_config_key.KEY_SOCIAL_ACCOUNT_FACEBOOK_URL, social_account_facebook_url, "Facebook URL"), (sys_config_key.KEY_SOCIAL_ACCOUNT_X_URL, social_account_x_url, "X URL"), + (sys_config_key.KEY_TAG_BAR_DISPLAY, tag_bar_display, "Tag bar display"), (sys_config_key.KEY_LOGIN_GOOGLE_CLIENT, login_google_client, "Google login client ID"), (sys_config_key.KEY_LOGIN_GOOGLE_SECRET, login_google_secret, "Google login client secret"), (sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "Google login enabled"), @@ -318,6 +323,7 @@ def set_sysconfig( "facebook_image_url": facebook_image_url, "social_account_facebook_url": social_account_facebook_url, "social_account_x_url": social_account_x_url, + "tag_bar_display": tag_bar_display, } # Use background reporting to avoid blocking the main configuration update From 915a2bcf9e0b76a3578d6a05ace6c4ade131b3f6 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Tue, 30 Dec 2025 15:08:19 +0800 Subject: [PATCH 3/5] The transaction list now includes "balance" and "description" fields. --- services/admin_service/controllers/order.py | 28 +---------- services/admin_service/controllers/user.py | 31 ++---------- .../user_wallet_history_repository.py | 20 ++++++-- .../admin_service/services/payment_service.py | 14 +++++- .../services/user_wallet_history_service.py | 48 ++++++++++++++++++- 5 files changed, 80 insertions(+), 61 deletions(-) diff --git a/services/admin_service/controllers/order.py b/services/admin_service/controllers/order.py index fe900b9..05257e7 100644 --- a/services/admin_service/controllers/order.py +++ b/services/admin_service/controllers/order.py @@ -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) \ No newline at end of file + + return ResponseUtils.success_page(data=orders, total=total, page_num=page, page_size=page_size) \ No newline at end of file diff --git a/services/admin_service/controllers/user.py b/services/admin_service/controllers/user.py index 73e102b..daa3104 100644 --- a/services/admin_service/controllers/user.py +++ b/services/admin_service/controllers/user.py @@ -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) \ No newline at end of file + + return ResponseUtils.success_page(data=orders, total=total, page_num=page, page_size=page_size) \ No newline at end of file diff --git a/services/admin_service/repositories/user_wallet_history_repository.py b/services/admin_service/repositories/user_wallet_history_repository.py index 0da9b47..a161d70 100644 --- a/services/admin_service/repositories/user_wallet_history_repository.py +++ b/services/admin_service/repositories/user_wallet_history_repository.py @@ -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, diff --git a/services/admin_service/services/payment_service.py b/services/admin_service/services/payment_service.py index 566abcb..7026f4a 100644 --- a/services/admin_service/services/payment_service.py +++ b/services/admin_service/services/payment_service.py @@ -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}") diff --git a/services/admin_service/services/user_wallet_history_service.py b/services/admin_service/services/user_wallet_history_service.py index 85f2556..87052a0 100644 --- a/services/admin_service/services/user_wallet_history_service.py +++ b/services/admin_service/services/user_wallet_history_service.py @@ -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) From fa23805b5c26761aa3bdb256e7868688771aa2ed Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Tue, 30 Dec 2025 18:41:18 +0800 Subject: [PATCH 4/5] Change the balance_after field to balance --- .../admin_service/services/user_wallet_history_service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/admin_service/services/user_wallet_history_service.py b/services/admin_service/services/user_wallet_history_service.py index 87052a0..7d6df60 100644 --- a/services/admin_service/services/user_wallet_history_service.py +++ b/services/admin_service/services/user_wallet_history_service.py @@ -51,13 +51,13 @@ class UserWalletHistoryService: info["description"] = service.name except: info["description"] = "-" - info["balance_after"] = order.balance_after + info["balance"] = 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["balance"] = "-" info["description"] = order.payment_method else: info["order_type"] = order.type From 50637087ae421fba4836fa98946e4844f5b337e8 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Tue, 30 Dec 2025 18:43:35 +0800 Subject: [PATCH 5/5] Change the balance_after field to balance --- services/admin_service/services/user_wallet_history_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/admin_service/services/user_wallet_history_service.py b/services/admin_service/services/user_wallet_history_service.py index 7d6df60..905b671 100644 --- a/services/admin_service/services/user_wallet_history_service.py +++ b/services/admin_service/services/user_wallet_history_service.py @@ -55,7 +55,7 @@ class UserWalletHistoryService: elif order.type == TransactionType.DEPOSIT: info["order_type"] = "recharge" if order.status == 1: - info["balance_after"] = order.balance_after + info["balance"] = order.balance_after else: info["balance"] = "-" info["description"] = order.payment_method