初始化提交
This commit is contained in:
89
NBATransfer-backend/services/user_service.py
Normal file
89
NBATransfer-backend/services/user_service.py
Normal file
@@ -0,0 +1,89 @@
|
||||
from flask import request
|
||||
from models import db, User, Transaction, ApiCall
|
||||
from sqlalchemy import desc
|
||||
|
||||
class UserService:
|
||||
@staticmethod
|
||||
def get_profile(user_id):
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return {'error': '用户不存在'}, 404
|
||||
return user.to_dict(), 200
|
||||
|
||||
@staticmethod
|
||||
def update_profile(user_id, data):
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return {'error': '用户不存在'}, 404
|
||||
|
||||
if 'username' in data:
|
||||
user.username = data['username'].strip()
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
return {
|
||||
'message': '资料更新成功',
|
||||
'user': user.to_dict()
|
||||
}, 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return {'error': '资料更新失败'}, 500
|
||||
|
||||
@staticmethod
|
||||
def get_balance(user_id):
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return {'error': '用户不存在'}, 404
|
||||
|
||||
return {
|
||||
'balance': user.balance,
|
||||
'user_id': user.id
|
||||
}, 200
|
||||
|
||||
@staticmethod
|
||||
def get_transactions(user_id, page, per_page):
|
||||
pagination = Transaction.query.filter_by(user_id=user_id)\
|
||||
.order_by(desc(Transaction.created_at))\
|
||||
.paginate(page=page, per_page=per_page, error_out=False)
|
||||
|
||||
return {
|
||||
'transactions': [t.to_dict() for t in pagination.items],
|
||||
'total': pagination.total,
|
||||
'page': page,
|
||||
'per_page': per_page,
|
||||
'pages': pagination.pages
|
||||
}, 200
|
||||
|
||||
@staticmethod
|
||||
def get_api_calls(user_id, page, per_page):
|
||||
pagination = ApiCall.query.filter_by(user_id=user_id)\
|
||||
.order_by(desc(ApiCall.created_at))\
|
||||
.paginate(page=page, per_page=per_page, error_out=False)
|
||||
|
||||
return {
|
||||
'api_calls': [call.to_dict() for call in pagination.items],
|
||||
'total': pagination.total,
|
||||
'page': page,
|
||||
'per_page': per_page,
|
||||
'pages': pagination.pages
|
||||
}, 200
|
||||
|
||||
@staticmethod
|
||||
def get_stats(user_id):
|
||||
total_calls = ApiCall.query.filter_by(user_id=user_id).count()
|
||||
success_calls = ApiCall.query.filter_by(user_id=user_id, status='success').count()
|
||||
failed_calls = ApiCall.query.filter_by(user_id=user_id, status='failed').count()
|
||||
|
||||
total_cost = db.session.query(db.func.sum(Transaction.amount))\
|
||||
.filter_by(user_id=user_id, type='consume').scalar() or 0
|
||||
|
||||
total_recharge = db.session.query(db.func.sum(Transaction.amount))\
|
||||
.filter_by(user_id=user_id, type='recharge').scalar() or 0
|
||||
|
||||
return {
|
||||
'total_calls': total_calls,
|
||||
'success_calls': success_calls,
|
||||
'failed_calls': failed_calls,
|
||||
'total_cost': abs(total_cost),
|
||||
'total_recharge': total_recharge
|
||||
}, 200
|
||||
Reference in New Issue
Block a user