|
|
@@ -8998,6 +8998,110 @@ class AnimeService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 定时任务:检查产品图片生成任务状态并更新产品记录
|
|
|
+ * 扫描 mp_products 中 pic_task_id 非空且 pic_task_status 为生成中的记录,
|
|
|
+ * 通过 pic_task_id 查询 mp_generate_pic_tasks 获取生成结果,按结果更新状态
|
|
|
+ *
|
|
|
+ * @return array ['total' => 总数, 'success' => 成功数, 'failed' => 失败数, 'pending' => 待处理数]
|
|
|
+ */
|
|
|
+ public function checkProductPicTasks() {
|
|
|
+ // 查询所有生成中的产品图片任务
|
|
|
+ $products = DB::table('mp_products')
|
|
|
+ ->whereNotNull('pic_task_id')
|
|
|
+ ->where('pic_task_status', '生成中')
|
|
|
+ ->where('is_deleted', 0)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $stats = [
|
|
|
+ 'total' => $products->count(),
|
|
|
+ 'success' => 0,
|
|
|
+ 'failed' => 0,
|
|
|
+ 'pending' => 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($products as $product) {
|
|
|
+ try {
|
|
|
+ $task_id = $product->pic_task_id;
|
|
|
+
|
|
|
+ // 通过 pic_task_id 查询图片生成任务结果
|
|
|
+ $task = DB::table('mp_generate_pic_tasks')
|
|
|
+ ->where('id', $task_id)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($task) {
|
|
|
+ if ($task->status === 'success' && $task->result_url) {
|
|
|
+ // 解析图片URL
|
|
|
+ $result_urls = $task->result_url;
|
|
|
+ if (is_string($result_urls)) {
|
|
|
+ $result_urls = json_decode($result_urls, true);
|
|
|
+ }
|
|
|
+ $img_url = is_array($result_urls) ? ($result_urls[0] ?? $task->result_url) : $task->result_url;
|
|
|
+
|
|
|
+ // 更新资产表状态
|
|
|
+ DB::table('mp_products')
|
|
|
+ ->where('id', $product->id)
|
|
|
+ ->update([
|
|
|
+ 'pic_task_status' => '生成成功',
|
|
|
+ 'url' => $img_url,
|
|
|
+ 'updated_at' => now()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $stats['success']++;
|
|
|
+
|
|
|
+ dLog('anime')->info('资产图片生成成功', [
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'task_id' => $task_id,
|
|
|
+ 'img_url' => $img_url
|
|
|
+ ]);
|
|
|
+ } elseif ($task->status === 'failed') {
|
|
|
+ // 更新资产表状态
|
|
|
+ DB::table('mp_products')
|
|
|
+ ->where('id', $product->id)
|
|
|
+ ->update([
|
|
|
+ 'pic_task_status' => '生成失败',
|
|
|
+ 'updated_at' => now()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $stats['failed']++;
|
|
|
+
|
|
|
+ dLog('anime')->warning('资产图片生成失败', [
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'task_id' => $task_id,
|
|
|
+ 'error' => $task->error_message ?? '未知错误'
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ // 任务还在处理中
|
|
|
+ $stats['pending']++;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 任务不存在,标记为失败
|
|
|
+ DB::table('mp_products')
|
|
|
+ ->where('id', $product->id)
|
|
|
+ ->update([
|
|
|
+ 'pic_task_status' => '生成失败',
|
|
|
+ 'updated_at' => now()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $stats['failed']++;
|
|
|
+
|
|
|
+ dLog('anime')->error('图片生成任务不存在', [
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'task_id' => $task_id
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dLog('anime')->error('检查产品图片生成任务异常', [
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'task_id' => $product->pic_task_id ?? null,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $stats;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取剧本关联的资产列表
|
|
|
* @param array $data 请求参数
|
|
|
* @return array
|