功能: - 文章内容库管理 - 待处理产品列表管理 - 自动处理流程 - 智能搜索和数据提取 - ParamHub API集成 - 定时任务调度 部署端口: 16043
216 lines
6.3 KiB
Python
216 lines
6.3 KiB
Python
"""
|
|
产品处理 API
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from models.database import db
|
|
from services.process_service import process_service
|
|
|
|
bp = Blueprint('products', __name__, url_prefix='/api/products')
|
|
|
|
@bp.route('/pending', methods=['GET'])
|
|
def list_pending():
|
|
"""获取待处理产品列表"""
|
|
limit = request.args.get('limit', 20, type=int)
|
|
order_by = request.args.get('order_by', 'priority')
|
|
|
|
products = db.get_pending_products(limit=limit, order_by=order_by)
|
|
count = db.get_pending_count()
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'products': products,
|
|
'count': count
|
|
})
|
|
|
|
@bp.route('/pending', methods=['POST'])
|
|
def add_pending():
|
|
"""添加待处理产品"""
|
|
data = request.get_json()
|
|
|
|
if isinstance(data, dict):
|
|
products = [data]
|
|
elif isinstance(data, list):
|
|
products = data
|
|
else:
|
|
return jsonify({'error': '无效的数据格式'}), 400
|
|
|
|
added_count = 0
|
|
for item in products:
|
|
if 'product_name' not in item:
|
|
continue
|
|
|
|
result = db.add_pending_product(
|
|
product_name=item['product_name'],
|
|
category=item.get('category'),
|
|
subcategory=item.get('subcategory'),
|
|
priority=item.get('priority', 0),
|
|
source=item.get('source', 'manual')
|
|
)
|
|
if result:
|
|
added_count += 1
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'added_count': added_count,
|
|
'message': f'成功添加 {added_count} 个产品到待处理列表'
|
|
})
|
|
|
|
@bp.route('/pending/<product_name>', methods=['DELETE'])
|
|
def remove_pending(product_name):
|
|
"""从待处理列表移除产品"""
|
|
success = db.remove_pending_product(product_name)
|
|
|
|
if success:
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '产品已从待处理列表移除'
|
|
})
|
|
else:
|
|
return jsonify({'error': '产品不存在'}), 404
|
|
|
|
@bp.route('/processing', methods=['GET'])
|
|
def list_processing():
|
|
"""获取正在处理的产品列表"""
|
|
products = db.get_processing_products()
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'products': products,
|
|
'count': len(products)
|
|
})
|
|
|
|
@bp.route('/history', methods=['GET'])
|
|
def list_history():
|
|
"""获取处理历史"""
|
|
limit = request.args.get('limit', 100, type=int)
|
|
history = db.get_process_history(limit=limit)
|
|
|
|
# 解析JSON字段
|
|
for item in history:
|
|
if item.get('details'):
|
|
item['details'] = __import__('json').loads(item['details'])
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'history': history,
|
|
'count': len(history)
|
|
})
|
|
|
|
@bp.route('/history/<product_name>', methods=['GET'])
|
|
def get_product_history(product_name):
|
|
"""获取指定产品的处理历史"""
|
|
history = db.get_history_by_product(product_name)
|
|
|
|
for item in history:
|
|
if item.get('details'):
|
|
item['details'] = __import__('json').loads(item['details'])
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'product_name': product_name,
|
|
'history': history,
|
|
'count': len(history)
|
|
})
|
|
|
|
@bp.route('/process', methods=['POST'])
|
|
def process_single():
|
|
"""处理单个产品"""
|
|
data = request.get_json()
|
|
|
|
if 'product_name' not in data:
|
|
return jsonify({'error': '请提供产品名称'}), 400
|
|
|
|
product_info = {
|
|
'product_name': data['product_name'],
|
|
'category': data.get('category'),
|
|
'subcategory': data.get('subcategory')
|
|
}
|
|
|
|
# 检查是否正在处理
|
|
processing = db.get_processing_products()
|
|
if any(p['product_name'] == product_info['product_name'] for p in processing):
|
|
return jsonify({'error': '该产品正在处理中'}), 400
|
|
|
|
# 添加到处理中列表
|
|
db.start_processing(
|
|
product_name=product_info['product_name'],
|
|
category=product_info['category'],
|
|
subcategory=product_info['subcategory']
|
|
)
|
|
|
|
try:
|
|
# 执行处理
|
|
result = process_service.process_product(product_info)
|
|
|
|
# 从待处理列表移除
|
|
db.remove_pending_product(product_info['product_name'])
|
|
|
|
# 如果发现新产品,已在process_service中添加到待处理列表
|
|
|
|
return jsonify({
|
|
'success': result['success'],
|
|
'message': result['message'],
|
|
'review_id': result.get('review_id'),
|
|
'new_products': result.get('new_products', [])
|
|
})
|
|
finally:
|
|
# 完成处理,从处理中列表移除
|
|
db.finish_processing(product_info['product_name'])
|
|
|
|
@bp.route('/process/batch', methods=['POST'])
|
|
def process_batch():
|
|
"""批量处理产品"""
|
|
data = request.get_json()
|
|
limit = data.get('limit', 5)
|
|
|
|
# 获取待处理产品
|
|
products = db.get_pending_products(limit=limit)
|
|
|
|
if not products:
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '没有待处理的产品',
|
|
'processed': 0
|
|
})
|
|
|
|
results = []
|
|
for product in products:
|
|
# 检查是否正在处理
|
|
processing = db.get_processing_products()
|
|
if any(p['product_name'] == product['product_name'] for p in processing):
|
|
results.append({
|
|
'product_name': product['product_name'],
|
|
'success': False,
|
|
'message': '正在处理中'
|
|
})
|
|
continue
|
|
|
|
# 添加到处理中列表
|
|
db.start_processing(
|
|
product_name=product['product_name'],
|
|
category=product.get('category'),
|
|
subcategory=product.get('subcategory')
|
|
)
|
|
|
|
try:
|
|
# 执行处理
|
|
result = process_service.process_product(product)
|
|
|
|
# 从待处理列表移除
|
|
db.remove_pending_product(product['product_name'])
|
|
|
|
results.append({
|
|
'product_name': product['product_name'],
|
|
'success': result['success'],
|
|
'message': result['message'],
|
|
'review_id': result.get('review_id')
|
|
})
|
|
finally:
|
|
# 完成处理
|
|
db.finish_processing(product['product_name'])
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'processed': len(results),
|
|
'results': results
|
|
}) |