213 lines
6.5 KiB
Python
213 lines
6.5 KiB
Python
from flask import request, jsonify, current_app
|
|
from app import db
|
|
from app.models import Package, Product
|
|
from . import api_bp
|
|
from .decorators import require_login, require_admin
|
|
import traceback
|
|
|
|
@api_bp.route('/packages', methods=['GET'])
|
|
@require_login
|
|
def get_packages():
|
|
"""获取套餐列表"""
|
|
try:
|
|
product_id = request.args.get('product_id')
|
|
if not product_id:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '缺少产品ID参数'
|
|
}), 400
|
|
|
|
# 验证产品是否存在
|
|
product = Product.query.filter_by(product_id=product_id).first()
|
|
if not product:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '产品不存在'
|
|
}), 404
|
|
|
|
# 获取套餐列表
|
|
packages = Package.query.filter_by(product_id=product_id).order_by(Package.sort_order).all()
|
|
package_list = [pkg.to_dict() for pkg in packages]
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'data': {
|
|
'packages': package_list
|
|
}
|
|
})
|
|
|
|
except Exception as e:
|
|
current_app.logger.error(f"获取套餐列表失败: {str(e)}")
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '服务器内部错误'
|
|
}), 500
|
|
|
|
@api_bp.route('/packages', methods=['POST'])
|
|
@require_login
|
|
def create_package():
|
|
"""创建套餐"""
|
|
try:
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '请求数据为空'
|
|
}), 400
|
|
|
|
# 验证必填字段
|
|
required_fields = ['product_id', 'name', 'price', 'duration']
|
|
for field in required_fields:
|
|
if field not in data or not data[field]:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': f'缺少必要参数: {field}'
|
|
}), 400
|
|
|
|
# 验证产品是否存在
|
|
product = Product.query.filter_by(product_id=data['product_id']).first()
|
|
if not product:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '产品不存在'
|
|
}), 404
|
|
|
|
# 创建套餐
|
|
package = Package(
|
|
product_id=data['product_id'],
|
|
name=data['name'],
|
|
description=data.get('description', ''),
|
|
price=float(data['price']),
|
|
duration=int(data['duration']),
|
|
max_devices=int(data.get('max_devices', 1)),
|
|
stock=int(data.get('stock', -1)),
|
|
sort_order=int(data.get('sort_order', 0)),
|
|
status=int(data.get('status', 1))
|
|
)
|
|
|
|
db.session.add(package)
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '套餐创建成功',
|
|
'data': package.to_dict()
|
|
})
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
current_app.logger.error(f"创建套餐失败: {str(e)}")
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '服务器内部错误'
|
|
}), 500
|
|
|
|
@api_bp.route('/packages/<package_id>', methods=['GET'])
|
|
@require_login
|
|
def get_package(package_id):
|
|
"""获取套餐详情"""
|
|
try:
|
|
package = Package.query.filter_by(package_id=package_id).first()
|
|
if not package:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '套餐不存在'
|
|
}), 404
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'data': package.to_dict()
|
|
})
|
|
|
|
except Exception as e:
|
|
current_app.logger.error(f"获取套餐详情失败: {str(e)}")
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '服务器内部错误'
|
|
}), 500
|
|
|
|
@api_bp.route('/packages/<package_id>', methods=['PUT'])
|
|
@require_login
|
|
def update_package(package_id):
|
|
"""更新套餐"""
|
|
try:
|
|
package = Package.query.filter_by(package_id=package_id).first()
|
|
if not package:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '套餐不存在'
|
|
}), 404
|
|
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '请求数据为空'
|
|
}), 400
|
|
|
|
# 更新字段
|
|
if 'name' in data:
|
|
package.name = data['name']
|
|
if 'description' in data:
|
|
package.description = data['description']
|
|
if 'price' in data:
|
|
package.price = float(data['price'])
|
|
if 'duration' in data:
|
|
package.duration = int(data['duration'])
|
|
if 'max_devices' in data:
|
|
package.max_devices = int(data['max_devices'])
|
|
if 'stock' in data:
|
|
package.stock = int(data['stock'])
|
|
if 'sort_order' in data:
|
|
package.sort_order = int(data['sort_order'])
|
|
if 'status' in data:
|
|
package.status = int(data['status'])
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '套餐更新成功',
|
|
'data': package.to_dict()
|
|
})
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
current_app.logger.error(f"更新套餐失败: {str(e)}")
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '服务器内部错误'
|
|
}), 500
|
|
|
|
@api_bp.route('/packages/<package_id>', methods=['DELETE'])
|
|
@require_login
|
|
def delete_package(package_id):
|
|
"""删除套餐"""
|
|
try:
|
|
package = Package.query.filter_by(package_id=package_id).first()
|
|
if not package:
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '套餐不存在'
|
|
}), 404
|
|
|
|
db.session.delete(package)
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '套餐删除成功'
|
|
})
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
current_app.logger.error(f"删除套餐失败: {str(e)}")
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
|
return jsonify({
|
|
'success': False,
|
|
'message': '服务器内部错误'
|
|
}), 500 |