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