完善图片挂载逻辑:筛选generate状态图片,挂载成功后更新次数和状态为published
This commit is contained in:
@@ -128,7 +128,7 @@ class ArticleImageMatcher:
|
||||
|
||||
def get_available_images_with_tags(self) -> List[Dict]:
|
||||
"""
|
||||
从ai_image_tags表获取可用的图片及其标签
|
||||
从ai_image_tags表获取可用的图片及其标签(状态为generate且挂载次数<5)
|
||||
|
||||
Returns:
|
||||
包含图片ID、标签等信息的列表
|
||||
@@ -137,24 +137,26 @@ class ArticleImageMatcher:
|
||||
connection = self.db_manager.get_connection()
|
||||
try:
|
||||
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
|
||||
# 查询附加文章数量小于5的图片
|
||||
# 查询状态为generate且附加文章数量小于5的图片
|
||||
sql = """
|
||||
SELECT
|
||||
id,
|
||||
image_id,
|
||||
image_name,
|
||||
image_url,
|
||||
image_thumb_url,
|
||||
tag_id,
|
||||
tag_name,
|
||||
keywords_id,
|
||||
keywords_name,
|
||||
department_id,
|
||||
department_name,
|
||||
image_attached_article_count
|
||||
FROM ai_image_tags
|
||||
WHERE image_attached_article_count < 5
|
||||
ORDER BY image_attached_article_count ASC, id DESC
|
||||
it.id,
|
||||
it.image_id,
|
||||
it.image_name,
|
||||
it.image_url,
|
||||
it.image_thumb_url,
|
||||
it.tag_id,
|
||||
it.tag_name,
|
||||
it.keywords_id,
|
||||
it.keywords_name,
|
||||
it.department_id,
|
||||
it.department_name,
|
||||
it.image_attached_article_count
|
||||
FROM ai_image_tags it
|
||||
INNER JOIN ai_images i ON it.image_id = i.id
|
||||
WHERE it.image_attached_article_count < 5
|
||||
AND i.status = 'generate'
|
||||
ORDER BY it.image_attached_article_count ASC, it.id DESC
|
||||
"""
|
||||
cursor.execute(sql)
|
||||
results = cursor.fetchall()
|
||||
@@ -352,6 +354,14 @@ class ArticleImageMatcher:
|
||||
"""
|
||||
cursor.execute(update_sql, (image_data['id'],))
|
||||
|
||||
# 更新图片状态为published
|
||||
update_image_status_sql = """
|
||||
UPDATE ai_images
|
||||
SET status = 'published'
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(update_image_status_sql, (image_data['image_id'],))
|
||||
|
||||
connection.commit()
|
||||
logger.info(f"成功插入文章图片关联 - 文章ID: {article_id}, 图片ID: {image_data['image_id']}, 分数: {match_score}")
|
||||
return True
|
||||
@@ -652,6 +662,22 @@ class ArticleImageMatcher:
|
||||
article_image_id = cursor.lastrowid
|
||||
logger.info(f"文章图片关联信息已插入ai_article_images表,id: {article_image_id}")
|
||||
|
||||
# 更新图片附加文章计数
|
||||
update_count_sql = """
|
||||
UPDATE ai_image_tags
|
||||
SET image_attached_article_count = image_attached_article_count + 1
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(update_count_sql, (tag_image_id,))
|
||||
|
||||
# 更新图片状态为published
|
||||
update_image_status_sql = """
|
||||
UPDATE ai_images
|
||||
SET status = 'published'
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(update_image_status_sql, (image_id,))
|
||||
|
||||
connection.commit()
|
||||
return article_image_id
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user