修复穿搭图片生成前端显示问题

- 在生成成功时创建OutfitImage记录到数据库
- 修复get_records_by_model_id函数加载关联数据
- 解决前端状态显示'未知'和图片不显示的问题
- 添加必要的导入和错误处理
This commit is contained in:
imeepos 2025-07-30 22:32:06 +08:00
parent cf672e474b
commit 49ce97af90
2 changed files with 33 additions and 7 deletions

View File

@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use rusqlite::{params, Row};
use std::sync::Arc;
use chrono::{DateTime, Utc};
use tracing::warn;
use crate::data::models::outfit_image::{
OutfitImageRecord, OutfitImageStatus, ProductImage, OutfitImage, OutfitImageStats
@ -434,12 +435,20 @@ impl OutfitImageRepository {
let mut records = Vec::new();
for record in record_iter {
let record = record.map_err(|e| anyhow!("解析记录失败: {}", e))?;
let mut record = record.map_err(|e| anyhow!("解析记录失败: {}", e))?;
// 🚨 关联查询也需要使用连接池,但为了避免嵌套连接获取,
// 我们在这里只返回主记录,关联数据由调用方按需加载
// record.product_images = self.get_product_images_by_record_id(&record.id)?;
// record.outfit_images = self.get_outfit_images_by_record_id(&record.id)?;
// 加载关联的商品图片和穿搭图片
record.product_images = self.get_product_images_by_record_id(&record.id)
.unwrap_or_else(|e| {
warn!("获取商品图片失败: {}", e);
Vec::new()
});
record.outfit_images = self.get_outfit_images_by_record_id(&record.id)
.unwrap_or_else(|e| {
warn!("获取穿搭图片失败: {}", e);
Vec::new()
});
records.push(record);
}

View File

@ -6,7 +6,7 @@ use chrono::Utc;
use crate::app_state::AppState;
use crate::data::models::outfit_image::{
OutfitImageRecord, OutfitImageGenerationRequest, OutfitImageGenerationResponse,
OutfitImageStats, ProductImage
OutfitImageStats, ProductImage, OutfitImage
};
use crate::data::models::outfit_photo_generation::{
WorkflowProgress
@ -518,9 +518,26 @@ pub async fn execute_outfit_image_generation(
.map(|url| convert_s3_to_cdn_url(url))
.collect();
// 更新记录状态为完成
// 更新记录状态为完成并创建OutfitImage记录
if let Ok(Some(mut record)) = outfit_repo.get_record_by_id(&record_id) {
record.complete(cdn_urls.clone());
// 创建OutfitImage记录
for (index, image_url) in cdn_urls.iter().enumerate() {
let outfit_image = OutfitImage::new(
record_id.clone(),
image_url.clone(),
index as u32,
);
// 保存OutfitImage到数据库
if let Err(e) = outfit_repo.create_outfit_image(&outfit_image) {
warn!("创建OutfitImage记录失败: {}", e);
} else {
record.outfit_images.push(outfit_image);
}
}
let _ = outfit_repo.update_record(&record);
info!("📝 更新穿搭图片生成记录状态为完成,保存 {} 张图片", cdn_urls.len());
}