新增异常产品管理功能
- 新增 abnormal_products 数据库表 - 内容库和互联网均无搜索结果时存入异常库 - 新增异常产品 API: - GET /api/products/abnormal - 获取异常产品列表 - GET /api/products/abnormal/<product_name> - 获取异常产品详情 - POST /api/products/abnormal/<product_name>/resolve - 解决异常产品 - DELETE /api/products/abnormal/<product_name> - 删除异常产品记录 - POST /api/products/abnormal/<product_name>/retry - 重试处理 - 更新 README 文档
This commit is contained in:
@@ -204,9 +204,30 @@ class Database:
|
||||
)
|
||||
''')
|
||||
|
||||
# 异常产品表
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS abnormal_products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
product_name TEXT NOT NULL UNIQUE,
|
||||
category TEXT,
|
||||
subcategory TEXT,
|
||||
abnormal_type TEXT DEFAULT 'no_search_results',
|
||||
abnormal_reason TEXT,
|
||||
search_results TEXT,
|
||||
retry_count INTEGER DEFAULT 0,
|
||||
status TEXT DEFAULT 'pending',
|
||||
resolution TEXT,
|
||||
resolved_at DATETIME,
|
||||
resolved_by TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
last_retry_at DATETIME
|
||||
)
|
||||
''')
|
||||
|
||||
# 创建索引
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_process_steps_session ON process_steps(process_id)')
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_process_sessions_status ON process_sessions(status)')
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_abnormal_products_status ON abnormal_products(status)')
|
||||
|
||||
conn.commit()
|
||||
|
||||
@@ -830,5 +851,99 @@ class Database:
|
||||
intervention_data=intervention_data
|
||||
)
|
||||
|
||||
# ========== 异常产品操作 ==========
|
||||
def add_abnormal_product(self, product_name, category=None, subcategory=None,
|
||||
abnormal_type='no_search_results', abnormal_reason=None,
|
||||
search_results=None):
|
||||
"""添加异常产品"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute('''
|
||||
INSERT INTO abnormal_products
|
||||
(product_name, category, subcategory, abnormal_type, abnormal_reason, search_results)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (product_name, category, subcategory, abnormal_type, abnormal_reason,
|
||||
json.dumps(search_results, ensure_ascii=False) if search_results else None))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
except sqlite3.IntegrityError:
|
||||
# 产品已存在,更新重试次数
|
||||
cursor.execute('''
|
||||
UPDATE abnormal_products
|
||||
SET retry_count = retry_count + 1,
|
||||
last_retry_at = CURRENT_TIMESTAMP,
|
||||
abnormal_reason = ?,
|
||||
search_results = ?
|
||||
WHERE product_name = ?
|
||||
''', (abnormal_reason,
|
||||
json.dumps(search_results, ensure_ascii=False) if search_results else None,
|
||||
product_name))
|
||||
conn.commit()
|
||||
return None
|
||||
|
||||
def get_abnormal_products(self, limit=100, status='pending'):
|
||||
"""获取异常产品列表"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
if status == 'all':
|
||||
cursor.execute('''
|
||||
SELECT * FROM abnormal_products
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
''', (limit,))
|
||||
else:
|
||||
cursor.execute('''
|
||||
SELECT * FROM abnormal_products
|
||||
WHERE status = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
''', (status, limit))
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
def get_abnormal_count(self, status='pending'):
|
||||
"""获取异常产品数量"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
if status == 'all':
|
||||
cursor.execute('SELECT COUNT(*) FROM abnormal_products')
|
||||
else:
|
||||
cursor.execute('SELECT COUNT(*) FROM abnormal_products WHERE status = ?', (status,))
|
||||
return cursor.fetchone()[0]
|
||||
|
||||
def resolve_abnormal_product(self, product_name, resolution, resolved_by='manual'):
|
||||
"""标记异常产品为已解决"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
UPDATE abnormal_products
|
||||
SET status = 'resolved',
|
||||
resolution = ?,
|
||||
resolved_by = ?,
|
||||
resolved_at = CURRENT_TIMESTAMP
|
||||
WHERE product_name = ?
|
||||
''', (resolution, resolved_by, product_name))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def delete_abnormal_product(self, product_name):
|
||||
"""删除异常产品记录"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM abnormal_products WHERE product_name = ?', (product_name,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def get_abnormal_product(self, product_name):
|
||||
"""获取异常产品详情"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT * FROM abnormal_products WHERE product_name = ?', (product_name,))
|
||||
row = cursor.fetchone()
|
||||
result = dict(row) if row else None
|
||||
if result and result.get('search_results'):
|
||||
result['search_results'] = json.loads(result['search_results'])
|
||||
return result
|
||||
|
||||
# 全局数据库实例
|
||||
db = Database()
|
||||
Reference in New Issue
Block a user