<?php

namespace App\Services\Anime;

use App\Consts\ErrorConst;
use App\Facade\Site;
use App\Libs\Utils;
use App\Models\MpGeneratePicTask;
use App\Services\AIGeneration\AIImageGenerationService;
use App\Services\AIGeneration\AIVideoGenerationService;
use App\Services\DeepSeek\DeepSeekService;
use Dflydev\DotAccessData\Util;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use OSS\Core\OssException;
use OSS\OssClient;

class AnimeService
{
    protected $aiImageGenerationService;
    protected $aiVideoGenerationService;
    protected $DeepSeekService;
    private $url;
    private $api_key;
    private $headers;

    public function __construct(
        AIImageGenerationService $aiImageGenerationService,
        AIVideoGenerationService $aiVideoGenerationService,
        DeepSeekService $DeepSeekService
    ) {
        $this->aiImageGenerationService = $aiImageGenerationService;
        $this->aiVideoGenerationService = $aiVideoGenerationService;
        $this->DeepSeekService = $DeepSeekService;
        $this->url = 'https://api.deepseek.com/chat/completions';
        $this->api_key = env('DEEPSEEK_API_KEY');
        $this->headers = [
            'Authorization' => 'Bearer '.$this->api_key,
            'Content-Type'  => 'application/json; charset=UTF-8'
        ];
    }

    public function createAnime($data) {
        $uid = Site::getUid();
        $input_art_style = getProp($data, 'art_style');
        // 替换美术风格
        $mappedArtStyle = $this->DeepSeekService->getArtStylePromptByInput($input_art_style);

        $model = getProp($data, 'model');
        if ($model && !DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->value('id')) {
            Utils::throwError('20003:该模型不存在!');
        }
        $anime_data = [
            'user_id'       => $uid,
            'anime_name'    => '新剧本策划',
            'is_multi'      => getProp($data, 'is_multi', 1),
            'model'         => $model,
            'created_at'    => date('Y-m-d H:i:s'),
            'updated_at'    => date('Y-m-d H:i:s'),
        ];

        if ($mappedArtStyle) $anime_data['art_style'] = $mappedArtStyle;

        return DB::table('mp_animes')->insertGetId($anime_data);
    }

    public function chatList($data) {
        $anime_name = getProp($data, 'anime_name');

        $query = DB::table('mp_animes')->where('is_deleted', 0);
        if ($anime_name) {
            $query->where('anime_name', 'like', "%$anime_name%");
        }

        return $query->orderBy('id', 'desc')->paginate();
    }

    public function chatHistory($data) {
        $anime_id = getProp($data, 'anime_id');
        $sequence = getProp($data, 'sequence', 0);

        if (!$anime_id) Utils::throwError('20003:请选择对话!');

        $query = DB::table('mp_anime_records as ar')->leftJoin('mp_anime_episodes as ae', 'ar.episode_id', '=', 'ae.id')->where('ar.anime_id', $anime_id)
        ->where('ar.sequence', $sequence)->select('ar.role', 'ar.content', 'ar.created_at', 'ar.episode_id', 'ae.title as episode_title', 'ae.created_at as episode_created_at');
        
        return $query->orderBy('ar.id')->get()->map(function ($value) {
            $value = (array)$value;
            $value['created_at'] = transDate($value['created_at']);
            $value['episode_created_at'] = transDate($value['episode_created_at']);
            return $value;
        })->toArray();
    }

    public function batchSetRoleImg($data) {
        $episode_id = getProp($data, 'episode_id');
        if (!$episode_id) Utils::throwError('1002:请选择分集');
        $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
        $roles = json_decode(getProp($data, 'roles'), true);
        $art_style = getProp($episode, 'art_style');
        
        foreach ($roles as &$role) {
            $role_name = getProp($role, 'role');
            if ($role_name == '旁白') continue;
            $description = getProp($role, 'description');
            if ($art_style) $description = "美术风格：\n$art_style\n主体描述：$description";
            // 参考图地址
            $ref_img_url = getProp($role, 'url');
            if ($ref_img_url) continue;     // 已有图片则跳过
            
            // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
            try {
                $params = [
                    'prompt' => $description,
                    'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
                ];
                
                $task = $this->aiImageGenerationService->createImageGenerationTask($params);
                $task_id = $task->id;
                
                if (!$task_id) {
                    Utils::throwError("20003:角色({$role_name})生成图片失败");
                }
                
                $role['task_id'] = $task_id;
                $role['task_status'] = 'processing';
            } catch (\Exception $e) {
                logDB('anime', 'error', "角色({$role_name})生成图片失败", ['error' => $e->getMessage()]);
                Utils::throwError("20003:" . $e->getMessage());
            }
        }
        
        // 保存角色信息
        $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update([
            'roles'             => json_encode($roles, 256),
            'generate_status'   => '执行中',
            'generate_at'       => date('Y-m-d H:i:s'),
            'updated_at'        => date('Y-m-d H:i:s')
        ]);
        if (!$boolen) {
            Utils::throwError('20003:保存角色信息失败');
        }

        return $boolen;
    }


    public function batchSetSceneImg($data) {
        $episode_id = getProp($data, 'episode_id');
        if (!$episode_id) Utils::throwError('1002:请选择分集');
        $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
        $scenes = json_decode(getProp($episode, 'scenes'), true);
        $art_style = getProp($episode, 'art_style');
        $target_scene = getProp($data, 'target_scene');
        
        foreach ($scenes as &$scene) {
            $scene_name = getProp($scene, 'scene');
            if ($scene_name != $target_scene) continue;
            $description = getProp($scene, 'description');
            if ($art_style) $description = "美术风格：\n$art_style\n\n场景描述：$description";
            // 参考图地址
            $ref_img_url = getProp($scene, 'url');
            if ($ref_img_url) continue;     // 已有图片则跳过
            
            // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
            try {
                $params = [
                    'prompt' => $description,
                    'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
                ];
                
                $task = $this->aiImageGenerationService->createImageGenerationTask($params);
                $task_id = $task->id;
                
                if (!$task_id) {
                    Utils::throwError("20003:场景({$scene_name})生成图片失败");
                }
                
                $scene['task_id'] = $task_id;
                $scene['task_status'] = 'processing';
            } catch (\Exception $e) {
                logDB('anime', 'error', "场景({$scene_name})生成图片失败", ['error' => $e->getMessage()]);
                Utils::throwError("20003:" . $e->getMessage());
            }
        }
        
        // 保存场景信息
        $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update([
            'scenes'             => json_encode($scenes, 256),
            'generate_status'   => '执行中',
            'generate_at'       => date('Y-m-d H:i:s'),
            'updated_at'        => date('Y-m-d H:i:s')
        ]);
        if (!$boolen) {
            Utils::throwError('20003:保存角色信息失败');
        }

        return $boolen;
    }

    public function editAnime($data) {
        $script_arr =getProp($data, 'script', []);
        if (!$script_arr) Utils::throwError('20003:剧本内容不能为空');
        $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
        $anime_id = getProp($data, 'anime_id');
        try {   
            DB::beginTransaction();
            $table_data = [
                'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
                'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
                'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
                'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
                'roles' => isset($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '',
                'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
                'scenes' => isset($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '',
                'status' => 3,
                'start_episode_sequence' => $start_episode_sequence,
                'end_episode_sequence' => $start_episode_sequence + count($script_arr['episodes']) - 1,
                'episode_num' => count($script_arr['episodes']),
                'updated_at' => date('Y-m-d H:i:s')
            ];

            // 保存剧本内容
            if (!empty($anime_id)) {
                $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
                if (!$boolen) {
                    dLog('anime')->info('对话保存失败', $table_data);
                    Utils::throwError('20003:对话保存失败');
                }
            }else {
                $table['created_at'] = $table_data['updated_at'];
                $anime_id = DB::table('mp_animes')->insertGetId($table_data);
                if (!$anime_id) {
                    dLog('anime')->info('对话保存失败', $table_data);
                    Utils::throwError('20003:对话保存失败');
                }
            }
            
            
            // 保存分集内容
            // 删除历史分集内容
            $boolen1 = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->delete();
            
            $episodes  = [];
            $sequence = 1;
            foreach ($script_arr['episodes'] as $episode) {
                $segment_number = 1;
                foreach($episode['segments'] as $segment) {
                    $episodes[] = [
                        'anime_id' => $anime_id,
                        'episode_number' => $episode['episode_number'],
                        'title' => $episode['title'],
                        // 'content' => $episode['content'],
                        'content' => '',
                        'segment_number' => $segment_number,
                        'segment_content' => $segment['segment_content'],
                        'sequence' => $sequence,
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s')
                    ];
                    $sequence++;
                    $segment_number++;
                }
                
            }

            $boolen2 = DB::table('mp_anime_episodes')->insert($episodes);
            if (!$boolen2) {
                dLog('deepseek')->info('分集内容保存失败', $episodes);
                logDB('deepseek', 'error', '分集内容保存失败', ['episodes_count' => count($episodes)]);
                Utils::throwError('20003:分集内容保存失败');
            }

        } catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }
        DB::commit();
        return true;
    }

    public function animeDetail($data) {
        $anime_id = getProp($data, 'anime_id');
        $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)
        ->selectRaw('id as anime_id, anime_name, intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, remark, generate_status, created_at')->first();
        if (!$anime) Utils::throwError('20003:该剧本不存在!');
        $anime = (array)$anime;
        $anime['roles'] = json_decode($anime['roles']);
        $anime['scenes'] = json_decode($anime['scenes']);

        // 获取分集信息
        $episodes = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('is_default', 1)
        ->select('id as episode_id', 'episode_number', 'title', DB::raw("(select es.img_url from mp_episode_segments as es where es.episode_id = mp_anime_episodes.id order by es.segment_number limit 1) as first_frame_url"))
        ->orderBy('episode_number')->get()->map(function ($value) {
            return (array)$value;
        })->toArray();

        $anime['episodes'] = $episodes;

        return $anime;
    }

    public function episodeInfo($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');
        if (!$anime_id || !$episode_id) {
            Utils::throwError('1002:请选择剧集');
        }

        $episode = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('id', $episode_id)->select('id as episode_id', 'anime_id', 'episode_number', 'title', 'intro', 'art_style', 'roles', 'scenes')->first();
        if (!$episode) Utils::throwError('20003:该剧集不存在!');
        $episode = (array)$episode;
        $episode['roles'] = json_decode($episode['roles'], true);
        $episode['scenes'] = json_decode($episode['scenes'], true);
        
        // 获取分镜信息
        $segments = DB::table('mp_episode_segments')->where('anime_id', $anime_id)->where('episode_id', getProp($episode, 'episode_id'))->orderBy('segment_number')
        ->select('*')->get()->map(function ($value) {
            return (array)$value;
        })->toArray();
        
        $acts = [];
        foreach($segments as $segment) {
            if (isset($acts[getProp($segment, 'act_number')])) {
                $acts[getProp($segment, 'act_number')]['segments'][] = [
                    'segment_id' => getProp($segment, 'segment_id'),
                    'segment_number' => getProp($segment, 'segment_number'),
                    'segment_content' => getProp($segment, 'segment_content'),
                    'description' => getProp($segment, 'description'),
                    'composition' => getProp($segment, 'composition'),
                    'camera_movement' => getProp($segment, 'camera_movement'),
                    'voice_actor' => getProp($segment, 'voice_actor'),
                    'dialogue' => getProp($segment, 'dialogue'),
                    'frame_type' => getProp($segment, 'frame_type'),
                    'scene' => getProp($segment, 'scene'),
                    'characters' => getProp($segment, 'characters'),
                    'tail_frame' => getProp($segment, 'tail_frame'),
                    'emotion' => getProp($segment, 'emotion'),
                    'emotion_type' => getProp($segment, 'emotion_type'),
                    'gender' => getProp($segment, 'gender'),
                    'speed_ratio' => getProp($segment, 'speed_ratio'),
                    'loudness_ratio' => getProp($segment, 'loudness_ratio'),
                    'emotion_scale' => getProp($segment, 'emotion_scale'),
                    'pitch' => getProp($segment, 'pitch'),
                    'voice_name' => getProp($segment, 'voice_name'),
                    'voice_type' => getProp($segment, 'voice_type'),
                    'voice_audio_url' => getProp($segment, 'voice_audio_url'),
                    'img_url' => getProp($segment, 'img_url'),
                    'video_url' => getProp($segment, 'video_url'),
                    'last_frame_url' => getProp($segment, 'last_frame_url'),
                ];
            }else {
                $acts[getProp($segment, 'act_number')]= [
                    'act_number' => getProp($segment, 'act_number'),
                    'act_title' => getProp($segment, 'act_title'),
                    'segments' => [
                        [
                            'segment_id' => getProp($segment, 'segment_id'),
                            'segment_number' => getProp($segment, 'segment_number'),
                            'segment_content' => getProp($segment, 'segment_content'),
                            'description' => getProp($segment, 'description'),
                            'composition' => getProp($segment, 'composition'),
                            'camera_movement' => getProp($segment, 'camera_movement'),
                            'voice_actor' => getProp($segment, 'voice_actor'),
                            'dialogue' => getProp($segment, 'dialogue'),
                            'frame_type' => getProp($segment, 'frame_type'),
                            'scene' => getProp($segment, 'scene'),
                            'characters' => getProp($segment, 'characters'),
                            'tail_frame' => getProp($segment, 'tail_frame'),
                            'emotion' => getProp($segment, 'emotion'),
                            'emotion_type' => getProp($segment, 'emotion_type'),
                            'gender' => getProp($segment, 'gender'),
                            'speed_ratio' => getProp($segment, 'speed_ratio'),
                            'loudness_ratio' => getProp($segment, 'loudness_ratio'),
                            'emotion_scale' => getProp($segment, 'emotion_scale'),
                            'pitch' => getProp($segment, 'pitch'),
                            'voice_name' => getProp($segment, 'voice_name'),
                            'voice_type' => getProp($segment, 'voice_type'),
                            'voice_audio_url' => getProp($segment, 'voice_audio_url'),
                            'img_url' => getProp($segment, 'img_url'),
                            'video_url' => getProp($segment, 'video_url'),
                            'last_frame_url' => getProp($segment, 'last_frame_url'),
                        ]
                    ]
                ];
            }
        }
        $episode['acts'] = array_values($acts);

        return $episode;
    }

    public function segmentInfo($data) {
        $segment_id = getProp($data, 'segment_id');
        if (!$segment_id) {
            Utils::throwError('1002:请选择分镜');
        }
        
        // 获取分镜信息
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        
        $result = [
            'segment_id' => getProp($segment, 'segment_id'),
            'segment_number' => getProp($segment, 'segment_number'),
            'segment_content' => getProp($segment, 'segment_content'),
            'description' => getProp($segment, 'description'),
            'composition' => getProp($segment, 'composition'),
            'camera_movement' => getProp($segment, 'camera_movement'),
            'voice_actor' => getProp($segment, 'voice_actor'),
            'dialogue' => getProp($segment, 'dialogue'),
            'frame_type' => getProp($segment, 'frame_type'),
            'scene' => getProp($segment, 'scene'),
            'characters' => getProp($segment, 'characters'),
            'tail_frame' => getProp($segment, 'tail_frame'),
            'emotion' => getProp($segment, 'emotion'),
            'emotion_type' => getProp($segment, 'emotion_type'),
            'gender' => getProp($segment, 'gender'),
            'speed_ratio' => getProp($segment, 'speed_ratio'),
            'loudness_ratio' => getProp($segment, 'loudness_ratio'),
            'emotion_scale' => getProp($segment, 'emotion_scale'),
            'pitch' => getProp($segment, 'pitch'),
            'voice_name' => getProp($segment, 'voice_name'),
            'voice_type' => getProp($segment, 'voice_type'),
            'voice_audio_url' => getProp($segment, 'voice_audio_url'),
            'img_url' => getProp($segment, 'img_url'),
            'video_url' => getProp($segment, 'video_url'),
            'last_frame_url' => getProp($segment, 'last_frame_url'),
        ];

        return $result;
    }

    public function copyEpisodeVersion($data) {
        $episode_id = getProp($data, 'episode_id');
        $uid = Site::getUid();

        $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
        if (!$episode) Utils::throwError('20003:该剧集不存在');

        $episode = (array)$episode;
        $anime_id = $episode['anime_id'];
        $episode_number = $episode['episode_number'];
        $count = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->where('version_count', '>', 1)->count('id');
        unset($episode['id']);
        $now = date('Y-m-d H:i:s');
        $episode['created_at'] = $now;
        $episode['updated_at'] = $now;
        $episode['is_default'] = 1;
        $episode['title'] = $count > 0 ? $episode['title']."（副本{$count}）" : $episode['title']."（副本）";
        $episode['version_count'] = $count + 2;
        $episode['is_generated'] = 0;

        try {
            DB::beginTransaction();
            DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->update(['is_default' => 0, 'updated_at'=>$now]);

            // 新增副本
            $new_episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
            if (!$new_episode_id) {
                Utils::throwError('20003:创建副本失败!');
            }

            // 获取分镜数据并准备插入
            $segments_for_insert = DB::table('mp_episode_segments')->where('episode_id', $episode_id)->orderBy('segment_number')->get()->map(function($item) use ($new_episode_id) {
                $item = (array)$item;
                unset($item['id']);
                $item['episode_id'] = $new_episode_id;
                $item['segment_id'] = date('YmdHis').mt_rand(1000,9999).str_pad($item['segment_number'], 3, "0", STR_PAD_LEFT);
                $item['created_at'] = $item['updated_at'] = date('Y-m-d H:i:s');
                return $item;
            })->toArray();

            // 写入分镜数据
            $boolen2 = DB::table('mp_episode_segments')->insert($segments_for_insert);
            if (!$boolen2) {
                Utils::throwError('20003:写入分镜数据失败!');
            }

            // 写入对话历史
            $records = [
                [
                    'uid' => $uid,
                    'anime_id' => $anime_id,
                    'role' => 'user',
                    'content' => '创建副本',
                    'sequence' => $episode_number,
                    'episode_id' => $new_episode_id,
                    'created_at' => $now,
                    'updated_at' => $now
                ],
                [
                    'uid' => $uid,
                    'anime_id' => $anime_id,
                    'role' => 'assistant',
                    'content' => '好的，已为您创建副本',
                    'sequence' => $episode_number,
                    'episode_id' => $new_episode_id,
                    'created_at' => $now,
                    'updated_at' => $now
                ]
            ];
            $boolen3 = DB::table('mp_anime_records')->insert($records);
            if (!$boolen3) {
                Utils::throwError('20003:对话记录保存失败');
            }
            
        }catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        DB::commit();

        // 构建与 episodeInfo 方法一致的返回数据结构
        $result = [
            'episode_id' => $new_episode_id,
            'anime_id' => $anime_id,
            'episode_number' => $episode_number,
            'title' => $episode['title'],
            'intro' => $episode['intro'],
            'art_style' => $episode['art_style'],
            'roles' => json_decode($episode['roles'], true),
            'scenes' => json_decode($episode['scenes'], true),
            'is_generated' => (int)$episode['is_generated']
        ];

        // 复用已有的分镜数据，按 act 分组（避免重复查询数据库）
        $acts = [];
        foreach($segments_for_insert as $segment) {
            $act_number = getProp($segment, 'act_number');
            $segment_info = [
                'segment_id' => getProp($segment, 'segment_id'),
                'segment_number' => getProp($segment, 'segment_number'),
                'segment_content' => getProp($segment, 'segment_content'),
                'description' => getProp($segment, 'description'),
                'composition' => getProp($segment, 'composition'),
                'camera_movement' => getProp($segment, 'camera_movement'),
                'voice_actor' => getProp($segment, 'voice_actor'),
                'dialogue' => getProp($segment, 'dialogue'),
                'frame_type' => getProp($segment, 'frame_type'),
                'scene' => getProp($segment, 'scene'),
                'characters' => getProp($segment, 'characters'),
                'tail_frame' => getProp($segment, 'tail_frame'),
                'emotion' => getProp($segment, 'emotion'),
                'emotion_type' => getProp($segment, 'emotion_type'),
                'gender' => getProp($segment, 'gender'),
                'speed_ratio' => getProp($segment, 'speed_ratio'),
                'loudness_ratio' => getProp($segment, 'loudness_ratio'),
                'emotion_scale' => getProp($segment, 'emotion_scale'),
                'pitch' => getProp($segment, 'pitch'),
                'voice_name' => getProp($segment, 'voice_name'),
                'voice_type' => getProp($segment, 'voice_type'),
                'voice_audio_url' => getProp($segment, 'voice_audio_url'),
                'img_url' => getProp($segment, 'img_url'),
                'video_url' => getProp($segment, 'video_url'),
                'last_frame_url' => getProp($segment, 'last_frame_url'),
            ];

            if (isset($acts[$act_number])) {
                $acts[$act_number]['segments'][] = $segment_info;
            } else {
                $acts[$act_number] = [
                    'act_number' => $act_number,
                    'act_title' => getProp($segment, 'act_title'),
                    'segments' => [$segment_info]
                ];
            }
        }
        $result['acts'] = array_values($acts);

        return $result;
    }

    public function episodeVersions($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');
        if (!$anime_id || !$episode_id) {
            Utils::throwError('1002:请选择剧集');
        }

        $episodes = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('id', $episode_id)->select('anime_id', 'episode_number', 'sequence', 'title', 'intro', 'art_style', 'roles', 'scenes')->get()->map(function($value) {
            return (array)$value;
        })->toArray();
        if (!$episodes) Utils::throwError('20003:该剧集不存在!');
        foreach($episodes as &$episode) {
            $episode['version'] = 'V'.$episode['version_count'];
            $episode['roles'] = json_decode($episode['roles'], true);
            $episode['scenes'] = json_decode($episode['scenes'], true);
        }

        return $episodes;
    }

    public function bindEpisodeVersion($data) {
        $episode_id = getProp($data, 'episode_id');

        $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
        if (!$episode) Utils::throwError('20003:该剧集不存在');
        $episode = (array)$episode;

        if ((int)$episode['is_default'] === 1) return true;

        try {
            DB::beginTransaction();
            // 移除is_default标志
            $boolen = DB::table('mp_anime_episodes')->where('anime_id', $episode['anime_id'])->where('episode_number', $episode['episode_number'])->update(['is_default' => 0, 'updated_at'=>date('Y-m-d H:i:s')]);
            if (!$boolen) {
                Utils::throwError('20003:更新失败!');
            }

            // 绑定副本
            $boolen2 = DB::table('mp_anime_episodes')->where('id', $episode_id)->update(['is_default' => 1, 'updated_at'=>date('Y-m-d H:i:s')]);
            if (!$boolen2) {
                Utils::throwError('20003:绑定失败!');
            }
        }catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        DB::commit();

        return true;
    }

    public function chatChangeImg($data) {
        $segment_id = getProp($data, 'segment_id');
        $prompt = getProp($data, 'prompt');
        if (!$prompt || !$segment_id) {
            Utils::throwError('1002:请输入提示词,并且选择分镜');
        }
        $segment = DB::table("mp_episode_segments")->where('segment_id', $segment_id)->first();
        if (!$segment) Utils::throwError('20003:该分镜不存在!');
        $ref_img_url = getProp($segment, 'img_url');
        if (empty($ref_img_url)) {
            $prompt = getProp($segment, 'segment_content');
        }

        $ratio = getProp($data, 'ratio');
        if (!in_array($ratio, ['1:1', '4:3', '3:2', '16:9', '21:9'])) {
            Utils::throwError('1002:画面比例选择不正确');
        }

        // 根据画面比例给宽高赋值
        switch ($ratio) {
            case '1:1':
                $width = 2048;
                $height = 2048;
                break;
            case '4:3':
                $width = 2304;
                $height = 1728;
                break;
            case '3:2':
                $width = 2496;
                $height = 1664;
                break;
            case '16:9':
                $width = 2560;
                $height = 1440;
                break;
            case '21:9':
                $width = 3024;
                $height = 1296;
                break;
            default:
                $width = 2048;
                $height = 2048;
        }

        try {
            $params = [
                'alias_segment_id' => $segment_id,
                'prompt' => $prompt,
                'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : [],
                'width' => $width,
                'height' => $height
            ];
            
            $task = $this->aiImageGenerationService->createImageGenerationTask($params);
            $task_id = $task->id;
            
            if (!$task_id) {
                Utils::throwError("20003:生成图片失败");
            }
            
            // 更新任务ID
            $boolen = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update(['pic_task_id' => $task_id, 'pic_task_status' => '生成中', 'updated_at' => date('Y-m-d H:i:s')]);
            if (!$boolen) Utils::throwError('20003:更新任务ID失败');
        } catch (\Exception $e) {
            Utils::throwError("20003:" . $e->getMessage());
        }

        // 创建任务之后先暂停10s等待异步任务完成
        sleep(10);
        $img_url = $this->loopGetPicTaskResult($task_id);

        if (!$img_url) Utils::throwError('20003:图片生成中,请稍后刷新');
        return $img_url;
    }

    public function changeEpisodeRoles($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');
        $target_roles = getProp($data, 'roles');

        // 参数验证
        if (!$anime_id) Utils::throwError('20003:请提供动漫ID');
        if (!$episode_id) Utils::throwError('20003:请提供剧集ID');
        if (!$target_roles) Utils::throwError('20003:请提供角色信息');

        // 验证剧集是否存在
        $episode = DB::table('mp_anime_episodes')
            ->where('anime_id', $anime_id)
            ->where('id', $episode_id)
            ->first();
        
        if (!$episode) Utils::throwError('20003:该剧集不存在');
        $roles = json_decode(getProp($episode, 'roles'), true);

        try {
            foreach($roles as &$role) {
                if (getProp($role, 'role') === getProp($target_roles, 'role')) {
                    $role = $target_roles;

                    // 更新角色列表
                    $result = DB::table('mp_anime_episodes')
                        ->where('anime_id', $anime_id)
                        ->where('id', $episode_id)
                        ->update([
                            'roles' => json_encode($roles, 256),
                            'updated_at' => date('Y-m-d H:i:s')
                        ]);

                    if ($result === false) {
                        Utils::throwError('20003:更新角色列表失败');
                    }
                    break;
                }
            }
        } catch (\Exception $e) {
            dLog('anime')->error('更新剧集角色失败', [
                'anime_id' => $anime_id,
                'episode_id' => $episode_id,
                'error' => $e->getMessage()
            ]);
            Utils::throwError('20003:更新角色列表失败　' . $e->getMessage());
        }

        return true;
    }

    public function changeEpisodeScenes($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');
        $target_scenes = getProp($data, 'scenes');

        // 参数验证
        if (!$anime_id) Utils::throwError('20003:请提供动漫ID');
        if (!$episode_id) Utils::throwError('20003:请提供剧集ID');
        if (!$target_scenes) Utils::throwError('20003:请提供场景信息');

        // 验证剧集是否存在
        $episode = DB::table('mp_anime_episodes')
            ->where('anime_id', $anime_id)
            ->where('id', $episode_id)
            ->first();
        
        if (!$episode) Utils::throwError('20003:该剧集不存在');
        $scenes = json_decode(getProp($episode, 'scenes'), true);

        try {
            foreach($scenes as &$scene) {
                if (getProp($scene, 'scene') === getProp($target_scenes, 'scene')) {
                    $scene = $target_scenes;

                    // 更新场景列表
                    $result = DB::table('mp_anime_episodes')
                        ->where('anime_id', $anime_id)
                        ->where('id', $episode_id)
                        ->update([
                            'scenes' => json_encode($scenes, 256),
                            'updated_at' => date('Y-m-d H:i:s')
                        ]);

                    if ($result === false) {
                        Utils::throwError('20003:更新场景列表失败');
                    }
                    break;
                }
            }
        } catch (\Exception $e) {
            dLog('anime')->error('更新剧集场景失败', [
                'anime_id' => $anime_id,
                'episode_id' => $episode_id,
                'error' => $e->getMessage()
            ]);
            Utils::throwError('20003:更新场景列表失败　' . $e->getMessage());
        }

        return true;
    }

    public function editSegment($data) {
        $segment_id = getProp($data, 'segment_id');
        $segment_content = getProp($data, 'segment_content');

        // 参数验证
        if (!$segment_id) Utils::throwError('20003:请提供分镜ID');
        if (!$segment_content) Utils::throwError('20003:请提供分镜内容');

        // 验证分镜是否存在
        $segment = DB::table('mp_episode_segments')
            ->where('segment_id', $segment_id)
            ->first();
        
        if (!$segment) Utils::throwError('20003:该分镜不存在');

        try {
            // 准备更新数据
            $update_data = [
                'segment_content' => $segment_content,
                'updated_at' => date('Y-m-d H:i:s')
            ];

            // 使用现有方法分析segment_content，提取各个字段
            $this->handleSegmentContent($update_data);

            // 更新分镜信息
            $result = DB::table('mp_episode_segments')
                ->where('segment_id', $segment_id)
                ->update($update_data);

            if ($result === false) {
                Utils::throwError('20003:更新分镜剧本失败');
            }

            return true;
        } catch (\Exception $e) {
            dLog('anime')->error('更新分镜剧本失败', [
                'segment_id' => $segment_id,
                'error' => $e->getMessage()
            ]);
            Utils::throwError('20003:更新分镜剧本失败　' . $e->getMessage());
        }
    }

    public function batchSetSegmentPics($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');
        $ratio = getProp($data, 'ratio');
        if (!in_array($ratio, ['1:1', '4:3', '3:2', '16:9', '21:9'])) {
            Utils::throwError('1002:画面比例选择不正确,只支持1:1、4:3、3:2、16:9、21:9');
        }
        
        if (!$anime_id || !$episode_id) {
            Utils::throwError('1002:请选择剧集');
        }

        // 获取剧集信息
        $episode = DB::table('mp_anime_episodes')
            ->where('anime_id', $anime_id)
            ->where('id', $episode_id)
            ->where('is_default', 1)
            ->select('id as episode_id', 'anime_id', 'episode_number')
            ->first();
            
        if (!$episode) {
            Utils::throwError('20003:该剧集不存在');
        }

        // 根据画面比例给宽高赋值
        switch ($ratio) {
            case '1:1':
                $width = 2048;
                $height = 2048;
                break;
            case '4:3':
                $width = 2304;
                $height = 1728;
                break;
            case '3:2':
                $width = 2496;
                $height = 1664;
                break;
            case '16:9':
                $width = 2560;
                $height = 1440;
                break;
            case '21:9':
                $width = 3024;
                $height = 1296;
                break;
            default:
                $width = 2048;
                $height = 2048;
        }

        // 获取动漫的角色信息（包含参考图）
        $anime = DB::table('mp_animes')
            ->where('id', $anime_id)
            ->select('roles', 'scenes')
            ->first();
            
        if (!$anime || !$anime->roles) {
            Utils::throwError('20003:未找到角色信息');
        }

        $roles = json_decode($anime->roles, true);
        if (!$roles) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $role_map = [];
        foreach ($roles as $role) {
            $role_name = getProp($role, 'role');
            $role_url = getProp($role, 'url');
            if (!empty($role_name) && !empty($role_url)) {
                // // 处理角色名称，去除装扮后缀（如：许芸-校服装 -> 许芸）- 修复中文乱码问题(弃用,容易产生重复图片)
                // $base_name = preg_replace('/[-－].*$/u', '', $role_name);
                // $role_map[$base_name] = $role_url;
                // 同时保存完整名称的映射
                $role_map[$role_name] = $role_url;
            }
        }

        $scenes = json_decode($anime->scenes, true);
        if (!$scenes) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $scene_map = [];
        foreach ($scenes as $scene) {
            $scene_name = getProp($scene, 'scene');
            $scene_url = getProp($scene, 'url');
            if (!empty($scene_name) && !empty($scene_url)) {
                $scene_map[$scene_name] = $scene_url;
            }
        }

        // 获取所有分镜信息
        $segments = DB::table('mp_episode_segments')
            ->where('anime_id', $anime_id)
            ->where('episode_id', $episode_id)
            ->whereNotIn('pic_task_status', ['已完成', '生成中'])
            ->orderBy('segment_number')
            ->get()
            ->toArray();

        if (empty($segments)) {
            Utils::throwError('20003:未找到分镜信息');
        }

        $updated_segments = [];
        $failed_segments = [];

        // 批量为每个分镜生成图片任务
        foreach ($segments as $segment) {
            $segment_id = $segment->segment_id;
            $segment_content = $segment->segment_content;
            
            if (empty($segment_content)) {
                $failed_segments[] = [
                    'segment_id' => $segment_id,
                    'error' => '分镜内容为空'
                ];
                continue;
            }

            try {
                // 解析分镜内容，提取画面描述作为主要提示词
                $prompt = $segment_content;
                $ref_img_urls = [];
                // 分镜场景
                $scene = getProp($segment, 'scene');
                $matched_scene = '';
                if (isset($scene_map[$scene])) {
                    $img_index = count($ref_img_urls) + 1;
                    $prompt = "场景：$scene(图$img_index)\n$prompt";
                    $ref_img_urls[] = $scene_map[$scene];
                    $matched_scene = $scene;
                }

                // 分镜角色
                $characters = getProp($segment, 'characters');
                $characters = str_replace(['、', ',', ';'], ',', $characters);
                $roles = explode(',', $characters);
                
                // 从分镜内容中提取涉及的角色
                $segment_characters = $this->extractCharactersFromSegment($segment_content, array_keys($role_map), $roles);
                
                // 获取匹配角色的参考图
                foreach ($segment_characters as $character) {
                    if (isset($role_map[$character])) {
                        // 将$prompt中出现的角色替换成角色(图1、2、3,序号是$ref_img_urls的个数加1)
                        $img_index = count($ref_img_urls) + 1;
                        $prompt = str_replace($character, $character . '(图' . $img_index . ')', $prompt);
                        $ref_img_urls[] = $role_map[$character];
                    }
                }
                
                // 如果没有找到匹配的角色参考图，不使用参考图
                if (empty($ref_img_urls)) {
                    $ref_img_urls = [];
                }

                // 准备生成任务参数
                $params = [
                    'alias_segment_id' => $segment_id,
                    'prompt' => $prompt,
                    'ref_img_urls' => $ref_img_urls,
                    'width' => $width,
                    'height' => $height,
                ];
                
                // $task_id = mt_rand(100000, 999999);
                // 调用AI图片生成服务
                $task = $this->aiImageGenerationService->createImageGenerationTask($params);
                $task_id = $task->id;
                
                if (!$task_id) {
                    $failed_segments[] = [
                        'segment_id' => $segment_id,
                        'error' => '创建生成任务失败'
                    ];
                    continue;
                }

                // 如果有对话内容，创建视频配音合成任务
                if (getProp($segment, 'dialogue')) {
                    $now = date('Y-m-d H:i:s');
                    $generate_json = [
                        'text' => getProp($segment, 'dialogue'),
                        'role' => getProp($segment, 'voice_actor'),
                        'voice_type' => getProp($segment, 'voice_type'),
                        'voice_name' => getProp($segment, 'voice_name'),
                        'emotion' => getProp($segment, 'emotion'),
                        'emotion_type' => getProp($segment, 'emotion_type'),
                        'gender' => getProp($segment, 'gender'),
                        'speed_ratio' => getProp($segment, 'speed_ratio', 0),
                        'loudness_ratio' => getProp($segment, 'loudness_ratio', 0),
                        'emotion_scale' => getProp($segment, 'emotion_scale', 0),
                        'pitch' => getProp($segment, 'pitch', 0),
                    ];
                    
                    // 插入视频配音合成任务
                    $dubTaskId = DB::table('mp_dub_video_tasks')->insertGetId([
                        'alias_segment_id' => $segment_id,
                        'generate_status' => '执行中',
                        'audio_url' => '',
                        'generate_json' => json_encode($generate_json, 256),
                        'created_at' => $now,
                        'updated_at' => $now,
                    ]);
                    
                    if (!$dubTaskId) {
                        Utils::throwError('20003:创建配音任务失败!');
                    }
                    
                    // 请求远程服务器执行生成
                    $client = new Client(['timeout' => 300, 'verify' => false]);
                    $result = $client->get("http://122.9.129.83:5000/api/anime/genAudio?taskId={$dubTaskId}");
                    $response = $result->getBody()->getContents();
                    $response_arr = json_decode($response, true);
                    if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
                        $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
                        dLog('anime')->error('火山生成音频失败', ['error' => $error_msg, 'task_id' => $dubTaskId]);
                        Utils::throwError('20003:火山生成音频失败');
                    }
                }
                
                // 更新分镜的任务信息
                $update_result = DB::table('mp_episode_segments')
                    ->where('segment_id', $segment_id)
                    ->update([
                        'pic_task_id' => $task_id,
                        'pic_task_status' => '生成中',
                        'audio_url' => '',
                        'updated_at' => date('Y-m-d H:i:s')
                    ]);

                // 如果是第一集的首帧图片,则存入待更新数组
                if ($update_result && (int)getProp($segment, 'episode_number') === 1 && (int)getProp($segment, 'segment_number') === 1) {
                    Redis::sadd('anime_first_frame_urls', $segment_id);
                }
                
                if ($update_result) {
                    $updated_segments[] = [
                        'segment_id' => $segment_id,
                        'task_id' => $task_id,
                        'status' => '生成中',
                        'matched_scenes' => $matched_scene,
                        'matched_roles' => $segment_characters,
                        'ref_urls_count' => count($ref_img_urls)
                    ];
                } else {
                    $failed_segments[] = [
                        'segment_id' => $segment_id,
                        'error' => '更新分镜任务状态失败'
                    ];
                }
                
            } catch (\Exception $e) {
                $failed_segments[] = [
                    'segment_id' => $segment_id,
                    'error' => $e->getMessage()
                ];
            }
        }

        // 更新剧集生成状态
        if (!empty($updated_segments)) {
            DB::table('mp_anime_episodes')
                ->where('id', $episode_id)
                ->update([
                    'is_generated' => 1,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
        }

        return [
            'success_count' => count($updated_segments),
            'failed_count' => count($failed_segments),
            'success_segments' => $updated_segments,
            'failed_segments' => $failed_segments,
            'total_segments' => count($segments)
        ];
    }

    public function reGenerateSegment($data) {
        $segment_id = getProp($data, 'segment_id');
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        $anime_id = getProp($segment, 'anime_id');
        $episode_id = getProp($segment, 'episode_id');
        $episode_number = getProp($segment, 'episode_number');
        $segment_content = getProp($data, 'segment_content');
        $ratio = getProp($data, 'ratio');
        if (!in_array($ratio, ['1:1', '4:3', '3:2', '16:9', '21:9'])) {
            Utils::throwError('1002:画面比例选择不正确');
        }
        
        if (!$anime_id || !$episode_id) {
            Utils::throwError('1002:请选择分镜');
        }

        // 使用文生文模型重新解析segment_content
        if (!empty($segment_content)) {
            $segment_content = $this->parseSegmentContentWithAI($segment_content);
        }

        // 根据画面比例给宽高赋值
        switch ($ratio) {
            case '1:1':
                $width = 2048;
                $height = 2048;
                break;
            case '4:3':
                $width = 2304;
                $height = 1728;
                break;
            case '3:2':
                $width = 2496;
                $height = 1664;
                break;
            case '16:9':
                $width = 2560;
                $height = 1440;
                break;
            case '21:9':
                $width = 3024;
                $height = 1296;
                break;
            default:
                $width = 2048;
                $height = 2048;
        }

        // 获取动漫的角色信息（包含参考图）
        $anime = DB::table('mp_animes')
            ->where('id', $anime_id)
            ->select('roles', 'scenes')
            ->first();
            
        if (!$anime || !$anime->roles) {
            Utils::throwError('20003:未找到角色信息');
        }

        $roles = json_decode($anime->roles, true);
        if (!$roles) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $role_map = [];
        foreach ($roles as $role) {
            $role_name = getProp($role, 'role');
            $role_url = getProp($role, 'url');
            if (!empty($role_name) && !empty($role_url)) {
                $role_map[$role_name] = $role_url;
            }
        }

        $scenes = json_decode($anime->scenes, true);
        if (!$scenes) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $scene_map = [];
        foreach ($scenes as $scene) {
            $scene_name = getProp($scene, 'scene');
            $scene_url = getProp($scene, 'url');
            if (!empty($scene_name) && !empty($scene_url)) {
                $scene_map[$scene_name] = $scene_url;
            }
        }

        $segment_id = $segment->segment_id;
        $original_segment_content = $segment->segment_content;
        
        // 如果传入了新的segment_content，使用新的内容，否则使用原有内容
        $final_segment_content = !empty($segment_content) ? $segment_content : $original_segment_content;
        
        if (empty($final_segment_content)) {
            Utils::throwError('20003:分镜内容不存在,请填写内容后重试');
        }

        try {
            // 分镜剧本数组
            $update_data = [
                'segment_content' => $final_segment_content,
                'pic_task_status' => '生成中',
                'updated_at' => date('Y-m-d H:i:s')
            ];

            // 解析分镜内容，提取画面描述作为主要提示词
            $prompt = $final_segment_content;
            $ref_img_urls = [];
            // 分镜场景
            $scene = getProp($data, 'scene');
            if ($scene) {
                $matched_scene = '';
                if (isset($scene_map[$scene])) {
                    $img_index = count($ref_img_urls) + 1;
                    $prompt = "场景：$scene(图$img_index)\n$prompt";
                    $ref_img_urls[] = $scene_map[$scene];
                    $matched_scene = $scene;
                }
                $update_data['scene'] = $scene;
            }

            // 分镜角色
            $characters = getProp($data, 'characters');
            if ($characters) {
                $update_data['characters'] = $characters;
                $characters = str_replace(['、', ',', ';'], ',', $characters);
                $characters = explode(',', $characters);
                
                // 从分镜内容中提取涉及的角色
                $segment_characters = $this->extractCharactersFromSegment($final_segment_content, array_keys($role_map), $characters);
                
                // 获取匹配角色的参考图
                foreach ($segment_characters as $character) {
                    if (isset($role_map[$character])) {
                        // 将$prompt中出现的角色替换成角色(图1、2、3,序号是$ref_img_urls的个数加1)
                        $img_index = count($ref_img_urls) + 1;
                        $prompt = str_replace($character, $character . '(图' . $img_index . ')', $prompt);
                        $ref_img_urls[] = $role_map[$character];
                    }
                }
            }
            
            
            // 如果没有找到匹配的角色参考图，不使用参考图
            if (empty($ref_img_urls)) {
                $ref_img_urls = [];
            }

            // 准备生成任务参数
            $params = [
                'alias_segment_id' => $segment_id,
                'prompt' => $prompt,
                'ref_img_urls' => $ref_img_urls,
                'width' => $width,
                'height' => $height,
            ];
            
            // 调用AI图片生成服务
            $task = $this->aiImageGenerationService->createImageGenerationTask($params);
            $task_id = $task->id;
            
            if (!$task_id) {
                Utils::throwError('20003:创建生成任务失败!');
            }
            $update_data['pic_task_id'] = $task_id;
            

            // 更新分镜剧本信息
            $this->handleSegmentContent($update_data);

            // 如果有对话内容，创建视频配音合成任务(有更改分镜内容时创建)
            if ($segment_content && getProp($update_data, 'dialogue')) {
                $now = date('Y-m-d H:i:s');
                $generate_json = [
                    'text' => getProp($update_data, 'dialogue'),
                    'role' => getProp($update_data, 'voice_actor'),
                    'voice_type' => getProp($update_data, 'voice_type'),
                    'voice_name' => getProp($update_data, 'voice_name'),
                    'emotion' => getProp($update_data, 'emotion'),
                    'emotion_type' => getProp($update_data, 'emotion_type'),
                    'gender' => getProp($update_data, 'gender'),
                    'speed_ratio' => getProp($update_data, 'speed_ratio', 0),
                    'loudness_ratio' => getProp($update_data, 'loudness_ratio', 0),
                    'emotion_scale' => getProp($update_data, 'emotion_scale', 0),
                    'pitch' => getProp($update_data, 'pitch', 0),
                ];
                
                // 插入视频配音合成任务
                $dubTaskId = DB::table('mp_dub_video_tasks')->insertGetId([
                    'alias_segment_id' => $segment_id,
                    'generate_status' => '执行中',
                    'audio_url' => '',
                    'generate_json' => json_encode($generate_json, 256),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
                
                if (!$dubTaskId) {
                    Utils::throwError('20003:创建配音任务失败!');
                }
                
                // 请求远程服务器执行生成
                $client = new Client(['timeout' => 300, 'verify' => false]);
                $result = $client->get("http://122.9.129.83:5000/api/anime/genAudio?taskId={$dubTaskId}");
                $response = $result->getBody()->getContents();
                $response_arr = json_decode($response, true);
                if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
                    $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
                    dLog('anime')->error('火山生成音频失败', ['error' => $error_msg, 'task_id' => $dubTaskId]);
                    logDB('anime', 'error', '火山生成音频失败', ['error' => $error_msg, 'task_id' => $dubTaskId]);
                    Utils::throwError('20003:火山生成音频失败');
                }
                $update_data['audio_url'] = '';
            }

            $boolen = DB::table('mp_episode_segments')
                ->where('segment_id', $segment_id)
                ->update($update_data);

            // 如果是第一集的首帧图片,则存入待更新数组
            if ($boolen && (int)getProp($segment, 'episode_number') === 1 && (int)getProp($segment, 'segment_number') === 1) {
                Redis::sadd('anime_first_frame_urls', $segment_id);
            }
            
        } catch (\Exception $e) {
            $failed_segments[] = [
                'segment_id' => $segment_id,
                'error' => $e->getMessage()
            ];
        }

        // 创建任务之后先暂停10s等待异步任务完成
        sleep(10);
        $img_url = $this->loopGetPicTaskResult($task_id);
        if (!$img_url) Utils::throwError('20003:图片生成中,请稍后刷新');
        return $img_url;
    }

    public function addSegment($data) {
        $prev_segment_id = getProp($data, 'prev_segment_id');
        $segment = DB::table('mp_episode_segments')->where('segment_id', $prev_segment_id)->first();
        $anime_id = getProp($segment, 'anime_id');
        $episode_id = getProp($segment, 'episode_id');
        $episode_number = getProp($segment, 'episode_number');
        $segment_number = getProp($segment, 'segment_number');
        $segment_content = getProp($data, 'segment_content');
        $img_url = getProp($data, 'img_url');
        $video_url = getProp($data, 'video_url');
        $ratio = getProp($data, 'ratio');
        if (!in_array($ratio, ['1:1', '4:3', '3:2', '16:9', '21:9'])) {
            Utils::throwError('1002:画面比例选择不正确');
        }
        
        if (!$anime_id || !$episode_id) {
            Utils::throwError('1002:请选择分镜');
        }

        // 使用文生文模型重新解析segment_content
        if (!empty($segment_content)) {
            $segment_content = $this->parseSegmentContentWithAI($segment_content);
        }

        // 根据画面比例给宽高赋值
        switch ($ratio) {
            case '1:1':
                $width = 2048;
                $height = 2048;
                break;
            case '4:3':
                $width = 2304;
                $height = 1728;
                break;
            case '3:2':
                $width = 2496;
                $height = 1664;
                break;
            case '16:9':
                $width = 2560;
                $height = 1440;
                break;
            case '21:9':
                $width = 3024;
                $height = 1296;
                break;
            default:
                $width = 2048;
                $height = 2048;
        }

        // 获取动漫的角色信息（包含参考图）
        $anime = DB::table('mp_animes')
            ->where('id', $anime_id)
            ->select('roles', 'scenes')
            ->first();
            
        if (!$anime || !$anime->roles) {
            Utils::throwError('20003:未找到角色信息');
        }

        $roles = json_decode($anime->roles, true);
        if (!$roles) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $role_map = [];
        foreach ($roles as $role) {
            $role_name = getProp($role, 'role');
            $role_url = getProp($role, 'url');
            if (!empty($role_name) && !empty($role_url)) {
                $role_map[$role_name] = $role_url;
            }
        }

        $scenes = json_decode($anime->scenes, true);
        if (!$scenes) {
            Utils::throwError('20003:角色信息格式错误');
        }
        // 构建角色名称到参考图的映射
        $scene_map = [];
        foreach ($scenes as $scene) {
            $scene_name = getProp($scene, 'scene');
            $scene_url = getProp($scene, 'url');
            if (!empty($scene_name) && !empty($scene_url)) {
                $scene_map[$scene_name] = $scene_url;
            }
        }

        $segment_id = $segment->segment_id;
        $original_segment_content = $segment->segment_content;
        
        // 如果传入了新的segment_content，使用新的内容，否则使用原有内容
        $final_segment_content = !empty($segment_content) ? $segment_content : $original_segment_content;
        
        if (empty($final_segment_content)) {
            Utils::throwError('20003:分镜内容不存在,请填写内容后重试');
        }

        try {
            DB::beginTransaction();

            $new_segment_number = $segment_number + 1;
            $segment_id = date('YmdHis') . mt_rand(1000, 9999) . str_pad($new_segment_number, 3, "0", STR_PAD_LEFT);
            // 分镜剧本数组
            $insert_data = [
                'anime_id'      => $anime_id,
                'episode_id'    => $episode_id,
                'episode_number' => $episode_number,
                'act_number'    => getProp($segment, 'act_number'),
                'act_title'     => getProp($segment, 'act_title'),
                'segment_id'    => $segment_id,
                'segment_number' => $new_segment_number,
                'segment_content' => $final_segment_content,
                'img_url'       => $img_url,
                'video_url'     => $video_url,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ];

            // 更新分镜剧本信息
            $this->handleSegmentContent($insert_data);

            // 如果有对话内容，创建视频配音合成任务
            if (getProp($insert_data, 'dialogue')) {
                $now = date('Y-m-d H:i:s');
                $generate_json = [
                    'text' => getProp($insert_data, 'dialogue'),
                    'role' => getProp($insert_data, 'voice_actor'),
                    'voice_type' => getProp($insert_data, 'voice_type'),
                    'voice_name' => getProp($insert_data, 'voice_name'),
                    'emotion' => getProp($insert_data, 'emotion'),
                    'emotion_type' => getProp($insert_data, 'emotion_type'),
                    'gender' => getProp($insert_data, 'gender'),
                    'speed_ratio' => getProp($insert_data, 'speed_ratio', 0),
                    'loudness_ratio' => getProp($insert_data, 'loudness_ratio', 0),
                    'emotion_scale' => getProp($insert_data, 'emotion_scale', 0),
                    'pitch' => getProp($insert_data, 'pitch', 0),
                ];
                
                // 插入视频配音合成任务
                $dubTaskId = DB::table('mp_dub_video_tasks')->insertGetId([
                    'alias_segment_id' => $segment_id,
                    'generate_status' => '执行中',
                    'audio_url' => '',
                    'generate_json' => json_encode($generate_json, 256),
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
                
                if (!$dubTaskId) {
                    Utils::throwError('20003:创建配音任务失败!');
                }
                
                // 请求远程服务器执行生成
                $client = new Client(['timeout' => 300, 'verify' => false]);
                $result = $client->get("http://122.9.129.83:5000/api/anime/genAudio?taskId={$dubTaskId}");
                $response = $result->getBody()->getContents();
                $response_arr = json_decode($response, true);
                if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
                    $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
                    dLog('anime')->error('火山生成音频失败', ['error' => $error_msg, 'task_id' => $dubTaskId]);
                    Utils::throwError('20003:火山生成音频失败');
                }
            }

            // 如果没有上传图片则使用当前描述进行生成
            if (!$img_url) {
                // 解析分镜内容，提取画面描述作为主要提示词
                $prompt = $final_segment_content;
                $ref_img_urls = [];
                // 分镜场景
                $scene = getProp($insert_data, 'scene');
                if ($scene) {
                    $matched_scene = '';
                    if (isset($scene_map[$scene])) {
                        $img_index = count($ref_img_urls) + 1;
                        $prompt = "场景：$scene(图$img_index)\n$prompt";
                        $ref_img_urls[] = $scene_map[$scene];
                        $matched_scene = $scene;
                    }
                    $update_data['scene'] = $scene;
                }

                // 分镜角色
                $characters = getProp($insert_data, 'characters');
                if ($characters) {
                    $update_data['characters'] = $characters;
                    $characters = str_replace(['、', ',', ';'], ',', $characters);
                    $characters = explode(',', $characters);
                    
                    // 从分镜内容中提取涉及的角色
                    $segment_characters = $this->extractCharactersFromSegment($final_segment_content, array_keys($role_map), $characters);
                    
                    // 获取匹配角色的参考图
                    foreach ($segment_characters as $character) {
                        if (isset($role_map[$character])) {
                            // 将$prompt中出现的角色替换成角色(图1、2、3,序号是$ref_img_urls的个数加1)
                            $img_index = count($ref_img_urls) + 1;
                            $prompt = str_replace($character, $character . '(图' . $img_index . ')', $prompt);
                            $ref_img_urls[] = $role_map[$character];
                        }
                    }
                }
                
                
                // 如果没有找到匹配的角色参考图，不使用参考图
                if (empty($ref_img_urls)) {
                    $ref_img_urls = [];
                }

                // 准备生成任务参数
                $params = [
                    'alias_segment_id' => $segment_id,
                    'prompt' => $prompt,
                    'ref_img_urls' => $ref_img_urls,
                    'width' => $width,
                    'height' => $height,
                ];
                
                // 调用AI图片生成服务
                $task = $this->aiImageGenerationService->createImageGenerationTask($params);
                $task_id = $task->id;
                // $task_id = 'whatever';

                if (!$task_id) {
                    Utils::throwError('20003:创建生成任务失败!');
                }
                $insert_data['pic_task_status'] = '生成中';
                $insert_data['pic_task_id'] = $task_id;
            }
            
            // 先更新序号
            DB::table('mp_episode_segments')->where('anime_id', $anime_id)->where('episode_id', $episode_id)->where('segment_number', '>', $segment_number)->increment('segment_number');

            $boolen = DB::table('mp_episode_segments')->insert($insert_data);
            if (!$boolen) {
                Utils::throwError('20003:新增分镜失败!');
            }

            // 如果是第一集的首帧图片,则存入待更新数组
            if ($boolen && (int)$episode_number === 1 && (int)$new_segment_number === 1) {
                Redis::sadd('anime_first_frame_urls', $segment_id);
            }
            
        } catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        DB::commit();

        // 创建任务之后先暂停10s等待异步任务完成
        sleep(10);
        if (isset($task_id)) $img_url = $this->loopGetPicTaskResult($task_id);
        if (!$img_url) Utils::throwError('20003:图片生成中,请稍后刷新');
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        $segment = $segment ? (array)$segment : [];
        return $segment;
    }

    public function copySegment($data) {
        $segment_id = getProp($data, 'segment_id');
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        if (!$segment) Utils::throwError('20003:请选择分镜');
        $anime_id = getProp($segment, 'anime_id');
        $episode_id = getProp($segment, 'episode_id');
        $episode_number = getProp($segment, 'episode_number');
        $segment_number = getProp($segment, 'segment_number');

        $new_segment_id = 0;
        try {
            DB::beginTransaction();

            $segment = (array)$segment;
            unset($segment['id']);
            $segment['segment_number'] += 1;
            $segment['segment_id'] = date('YmdHis') . mt_rand(1000, 9999) . str_pad($segment['segment_number'], 3, "0", STR_PAD_LEFT);
            $segment['pic_task_id'] = $segment['pic_task_status'] = $segment['video_task_id'] = $segment['video_task_status'] = '';
            $segment['created_at'] = $segment['updated_at'] = date('Y-m-d H:i:s');

            // 更新其他分镜序号
            DB::table('mp_episode_segments')->where('anime_id', $anime_id)->where('episode_id', $episode_id)->where('segment_number', '>', $segment_number)->increment('segment_number');

            // 复制分镜
            $new_segment_id = DB::table('mp_episode_segments')->insertGetId($segment);
            if (!$new_segment_id) {
                Utils::throwError('20003:复制分镜失败');
            }

            
        }catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        DB::commit();
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        $segment = $segment ? (array)$segment : [];
        return $segment;
    }

    public function moveSegment($data) {
        $segment_id = getProp($data, 'segment_id');
        $target_segment_id = getProp($data, 'target_segment_id');
        
        // 获取源分镜信息
        $source_segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        if (!$source_segment) Utils::throwError('20003:请选择分镜');
        
        // 获取目标分镜信息
        $target_segment = DB::table('mp_episode_segments')->where('segment_id', $target_segment_id)->first();
        if (!$target_segment) Utils::throwError('20003:目标分镜不存在');
        
        $anime_id = getProp($source_segment, 'anime_id');
        $episode_id = getProp($source_segment, 'episode_id');
        $episode_number = getProp($source_segment, 'episode_number');
        $source_segment_number = getProp($source_segment, 'segment_number');
        $target_segment_number = getProp($target_segment, 'segment_number');
        
        // 验证源分镜和目标分镜必须属于同一个动漫的同一集
        $target_anime_id = getProp($target_segment, 'anime_id');
        $target_episode_number = getProp($target_segment, 'episode_number');
        
        if ($anime_id != $target_anime_id || $episode_number != $target_episode_number) {
            Utils::throwError('20003:只能在同一动漫的同一集内移动分镜');
        }
        
        // 如果源分镜和目标分镜相同，无需移动
        if ($source_segment_number == $target_segment_number) {
            return true;
        }

        try {
            DB::beginTransaction();

            if ($source_segment_number < $target_segment_number) {
                // 向后移动：源分镜移动到目标分镜之后
                // 1. 将源分镜和目标分镜之间的所有分镜序号减1
                DB::table('mp_episode_segments')
                    ->where('anime_id', $anime_id)
                    ->where('episode_id', $episode_id)
                    ->where('segment_number', '>', $source_segment_number)
                    ->where('segment_number', '<=', $target_segment_number)
                    ->decrement('segment_number');
                
                // 2. 将源分镜移动到目标分镜之后
                $new_segment_number = $target_segment_number;
                
            } else {
                // 向前移动：源分镜移动到目标分镜之后
                // 1. 将目标分镜之后到源分镜之前的所有分镜序号加1
                DB::table('mp_episode_segments')
                    ->where('anime_id', $anime_id)
                    ->where('episode_id', $episode_id)
                    ->where('segment_number', '>', $target_segment_number)
                    ->where('segment_number', '<', $source_segment_number)
                    ->increment('segment_number');
                
                // 2. 将源分镜移动到目标分镜之后
                $new_segment_number = $target_segment_number + 1;
            }
            
            // 3. 更新源分镜的序号
            $update_result = DB::table('mp_episode_segments')
                ->where('segment_id', $segment_id)
                ->update([
                    'segment_number' => $new_segment_number,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
                
            if (!$update_result) {
                Utils::throwError('20003:更新分镜序号失败');
            }

            DB::commit();
            return true;
            
        } catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:' . $e->getMessage());
        }
    }

    public function delSegment($data) {
        $segment_id = getProp($data, 'segment_id');
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        if (!$segment) Utils::throwError('20003:请选择分镜');
        $anime_id = getProp($segment, 'anime_id');
        $episode_id = getProp($segment, 'episode_id');
        $episode_number = getProp($segment, 'episode_number');
        $segment_number = getProp($segment, 'segment_number');

        try {
            DB::beginTransaction();

            // 删除分镜
            $boolen = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->delete();
            if (!$boolen) {
                Utils::throwError('20003:删除分镜失败');
            }

            // 更新其他分镜序号
            DB::table('mp_episode_segments')->where('anime_id', $anime_id)->where('episode_id', $episode_id)->where('segment_number', '>', $segment_number)->decrement('segment_number');
        }catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        DB::commit();
        return true;
    }

    public function segmentHistory($data) {
        $segment_id = getProp($data, 'segment_id');

        // 获取历史图片
        $pics = [];
        $pic_history = DB::table('mp_generate_pic_tasks')->where('alias_segment_id', $segment_id)->where('status', 'success')->orderBy('created_at', 'desc')->get();
        foreach($pic_history as $task) {
            $result_url = getProp($task, 'result_url');
            if (is_json($result_url)) {
                $pics = array_merge($pics, json_decode($result_url, true));
            }else {
                $pics[] = $result_url;
            }
        }
        
        // 获取历史视频
        $videos = [];
        $video_history = DB::table('mp_generate_video_tasks')->where('alias_segment_id', $segment_id)->where('status', 'success')->orderBy('created_at', 'desc')->get();
        foreach($video_history as $task) {
            $result_url = getProp($task, 'result_url');
            if (is_json($result_url)) {
                $videos = array_merge($pics, json_decode($result_url, true));
            }else {
                $videos[] = $result_url;
            }
        }

        return compact('pics', 'videos');
    }

    public function applyAudioData($data) {
        $segment_id = getProp($data, 'segment_id');
        $global_data = getProp($data, 'global_data');
        $segment_data = getProp($data, 'segment_data');

        if (!$segment_id) Utils::throwError('20003:请选择分镜');

        $global_data = is_array($global_data) ? $global_data : [];
        $segment_data = is_array($segment_data) ? $segment_data : [];
        
        // dialogue只能在segment_data中，如果在global_data中则移到segment_data
        $dialogue_item = null;
        foreach ($global_data as $key => $item) {
            if (trim((string)getProp($item, 'name')) === 'dialogue') {
                $dialogue_item = $item;
                unset($global_data[$key]);
                break;
            }
        }
        if ($dialogue_item) {
            $segment_data[] = $dialogue_item;
        }
        
        $allow_fields = ['dialogue', 'voice_type', 'speed_ratio', 'loudness_ratio', 'emotion_scale'];
        $global_update_data = [];
        $segment_update_data = [];
        $global_voice_type = '';
        $target_segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        if (!$target_segment) Utils::throwError('20003:分镜不存在');

        $episode_id = getProp($target_segment, 'episode_id');
        $target_voice_actor = trim((string)getProp($target_segment, 'voice_actor'));
        $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
        if (!$episode) Utils::throwError('20003:分集不存在');

        foreach ($global_data as $item) {
            $field_name = trim((string)getProp($item, 'name'));
            if (!$field_name || !in_array($field_name, $allow_fields)) {
                continue;
            }

            $global_update_data[$field_name] = getProp($item, 'value');
            if ($field_name === 'voice_type') {
                $global_voice_type = trim((string)getProp($item, 'value'));
            }
        }

        foreach ($segment_data as $item) {
            $field_name = trim((string)getProp($item, 'name'));
            if (!$field_name || !in_array($field_name, $allow_fields)) {
                continue;
            }

            $segment_update_data[$field_name] = getProp($item, 'value');
        }

        if ($global_voice_type) {
            $timbre_info = DB::table('mp_timbres')
                ->where('timbre_type', $global_voice_type)
                ->select('audio_url', 'timbre_name')
                ->first();
            if ($timbre_info) {
                $global_update_data['voice_audio_url'] = getProp($timbre_info, 'audio_url');
                $global_update_data['voice_name'] = str_replace('（多情感）', '', getProp($timbre_info, 'timbre_name'));
            }
        }

        try {
            DB::beginTransaction();

            $now = date('Y-m-d H:i:s');

            if (!empty($global_update_data)) {
                if (!$target_voice_actor) Utils::throwError('20003:角色异常');

                $global_segment_update_data = $global_update_data;
                $global_segment_update_data['updated_at'] = $now;
                DB::table('mp_episode_segments')
                    ->where('episode_id', $episode_id)
                    ->where('voice_actor', $target_voice_actor)
                    ->update($global_segment_update_data);

                if ($global_voice_type) {
                    $episode_roles = json_decode((string)getProp($episode, 'roles'), true);
                    $episode_roles = is_array($episode_roles) ? $episode_roles : [];
                    $roles_updated = false;

                    foreach ($episode_roles as &$role) {
                        if (trim((string)getProp($role, 'voice_name')) !== $target_voice_actor) {
                            continue;
                        }

                        $role['voice_type'] = $global_voice_type;
                        $role['voice_audio_url'] = getProp($global_update_data, 'voice_audio_url');
                        $role['voice_name'] = getProp($global_update_data, 'voice_name');
                        $roles_updated = true;
                    }
                    unset($role);

                    if ($roles_updated) {
                        DB::table('mp_anime_episodes')->where('id', $episode_id)->update([
                            'roles' => json_encode($episode_roles, 256),
                            'updated_at' => $now,
                        ]);
                    }
                }
            }

            if (!empty($segment_update_data)) {
                $segment_update_data['updated_at'] = $now;
                DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update($segment_update_data);
            }

            // 收集需要创建音频合成任务的segment_id
            $segment_ids_to_create_task = [];
            $segments_data = [];

            // 如果有全局更新，获取该角色的所有分镜数据
            if (!empty($global_update_data)) {
                $affected_segments = DB::table('mp_episode_segments')
                    ->where('episode_id', $episode_id)
                    ->where('voice_actor', $target_voice_actor)
                    ->select('segment_id', 'video_url', 'voice_actor', 'dialogue', 'voice_type', 'voice_name', 'emotion', 'emotion_type', 'gender', 'speed_ratio', 'loudness_ratio', 'emotion_scale', 'pitch')
                    ->get();
                
                foreach ($affected_segments as $seg) {
                    $seg = (array)$seg;
                    $sid = getProp($seg, 'segment_id');
                    $segment_ids_to_create_task[] = $sid;
                    $segments_data[$sid] = $seg;
                }
            }

            // 如果有单个分镜更新，添加该分镜数据
            if (!empty($segment_update_data)) {
                $segment_ids_to_create_task[] = $segment_id;
                
                // 如果该分镜数据还未获取，则获取
                if (!isset($segments_data[$segment_id])) {
                    $segment = DB::table('mp_episode_segments')
                        ->where('segment_id', $segment_id)
                        ->select('segment_id', 'video_url', 'voice_actor', 'dialogue', 'voice_type', 'voice_name', 'emotion', 'emotion_type', 'gender', 'speed_ratio', 'loudness_ratio', 'emotion_scale', 'pitch')
                        ->first();
                    
                    if ($segment) {
                        $segments_data[$segment_id] = (array)$segment;
                    }
                }
            }

            // 去重
            $segment_ids_to_create_task = array_unique($segment_ids_to_create_task);

            DB::commit();

        } catch (\Exception $e) {
            DB::rollBack();
            Utils::throwError('20003:'.$e->getMessage());
        }

        return true;
    }

    public function episodePicsInfo($data) {
        $anime_id = getProp($data, 'anime_id');
        $episode_id = getProp($data, 'episode_id');

        // 参数验证
        if (!$anime_id && !$episode_id) {
            Utils::throwError('20003:请提供动漫ID或分集ID');
        }

        // 根据参数获取数据源
        if ($episode_id) {
            // 从剧集表获取
            $source = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
            if (!$source) Utils::throwError('20003:该剧集不存在');
            $table_name = 'mp_anime_episodes';
            $id_field = 'id';
            $id_value = $episode_id;
        } else {
            // 从动漫表获取
            $source = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
            if (!$source) Utils::throwError('20003:该动漫不存在');
            $table_name = 'mp_animes';
            $id_field = 'id';
            $id_value = $anime_id;
        }

        $source = (array)$source;
        $roles = json_decode(getProp($source, 'roles', '[]'), true) ?: [];
        $scenes = json_decode(getProp($source, 'scenes', '[]'), true) ?: [];

        // 返回生成器函数，用于 SSE 流式输出
        return function() use ($roles, $scenes, $table_name, $id_field, $id_value) {
            $startTime = time();
            $maxDuration = 600; // 10分钟超时
            $checkInterval = 3; // 每3秒检查一次

            // Redis 缓存键
            $cacheKey = "anime:pics_info:{$table_name}:{$id_value}";
            
            // 生成当前数据的哈希值用于比较
            $generateHash = function($roles, $scenes) {
                return md5(json_encode(['roles' => $roles, 'scenes' => $scenes], JSON_UNESCAPED_UNICODE));
            };

            // 发送初始数据
            $currentHash = $generateHash($roles, $scenes);
            Redis::setex($cacheKey, 600, $currentHash); // 缓存10分钟

            echo "data: " . json_encode([
                'type' => 'init',
                'data' => [
                    'roles' => $roles,
                    'scenes' => $scenes,
                    'start_time' => $startTime
                ]
            ], JSON_UNESCAPED_UNICODE) . "\n\n";
            ob_flush();
            flush();

            // 持续轮询任务状态
            while (time() - $startTime < $maxDuration) {
                $hasProcessing = false;
                $updated = false;

                // 检查角色任务状态
                foreach ($roles as $index => &$role) {
                    // 如果已经有 URL，跳过检查
                    $existing_url = getProp($role, 'url');
                    if (!empty($existing_url)) {
                        continue;
                    }

                    $task_id = getProp($role, 'task_id');
                    $task_status = getProp($role, 'task_status');

                    // 只处理有任务ID且状态为processing的角色
                    if ($task_id && $task_status === 'processing') {
                        $hasProcessing = true;
                        
                        // 查询任务状态
                        $task = DB::table('mp_generate_pic_tasks')
                            ->where('id', $task_id)
                            ->select('id', 'status', 'result_url', 'error_message')
                            ->first();

                        if ($task) {
                            $task = (array)$task;
                            $status = getProp($task, 'status');

                            // 如果任务完成或失败
                            if (in_array($status, ['success', 'failed'])) {
                                $role['task_status'] = $status;
                                
                                if ($status === 'success') {
                                    $result_url = getProp($task, 'result_url');
                                    if ($result_url) {
                                        $result_urls = json_decode($result_url, true);
                                        $role['url'] = is_array($result_urls) ? $result_urls[0] : $result_url;
                                    }
                                }
                                
                                $updated = true;
                            }
                        }
                    }
                }

                // 检查场景任务状态
                foreach ($scenes as $index => &$scene) {
                    // 如果已经有 URL，跳过检查
                    $existing_url = getProp($scene, 'url');
                    if (!empty($existing_url)) {
                        continue;
                    }

                    $task_id = getProp($scene, 'task_id');
                    $task_status = getProp($scene, 'task_status');

                    // 只处理有任务ID且状态为processing的场景
                    if ($task_id && $task_status === 'processing') {
                        $hasProcessing = true;
                        
                        // 查询任务状态
                        $task = DB::table('mp_generate_pic_tasks')
                            ->where('id', $task_id)
                            ->select('id', 'status', 'result_url', 'error_message')
                            ->first();

                        if ($task) {
                            $task = (array)$task;
                            $status = getProp($task, 'status');

                            // 如果任务完成或失败
                            if (in_array($status, ['success', 'failed'])) {
                                $scene['task_status'] = $status;
                                
                                if ($status === 'success') {
                                    $result_url = getProp($task, 'result_url');
                                    if ($result_url) {
                                        $result_urls = json_decode($result_url, true);
                                        $scene['url'] = is_array($result_urls) ? $result_urls[0] : $result_url;
                                    }
                                }
                                
                                $updated = true;
                            }
                        }
                    }
                }

                // 如果有更新，检查数据是否真的变化了（通过哈希对比）
                if ($updated) {
                    $newHash = $generateHash($roles, $scenes);
                    $cachedHash = Redis::get($cacheKey);

                    // 只有当数据真正变化时才更新数据库和发送事件
                    if ($newHash !== $cachedHash) {
                        try {
                            // 更新数据库
                            DB::table($table_name)->where($id_field, $id_value)->update([
                                'roles' => json_encode($roles, JSON_UNESCAPED_UNICODE),
                                'scenes' => json_encode($scenes, JSON_UNESCAPED_UNICODE),
                                'updated_at' => date('Y-m-d H:i:s')
                            ]);

                            // 更新缓存
                            Redis::setex($cacheKey, 600, $newHash);

                            // 发送更新事件
                            echo "data: " . json_encode([
                                'type' => 'update',
                                'data' => [
                                    'roles' => $roles,
                                    'scenes' => $scenes,
                                    'elapsed_time' => time() - $startTime
                                ]
                            ], JSON_UNESCAPED_UNICODE) . "\n\n";
                            ob_flush();
                            flush();
                        } catch (\Exception $e) {
                            dLog('anime')->error('更新角色/场景信息失败: ' . $e->getMessage());
                        }
                    }
                }

                // 如果所有任务都已完成，发送完成事件并退出
                if (!$hasProcessing) {
                    // 清理缓存
                    Redis::del($cacheKey);

                    echo "data: " . json_encode([
                        'type' => 'completed',
                        'data' => [
                            'roles' => $roles,
                            'scenes' => $scenes,
                            'total_time' => time() - $startTime
                        ]
                    ], JSON_UNESCAPED_UNICODE) . "\n\n";
                    ob_flush();
                    flush();
                    break;
                }

                // 等待下次检查
                sleep($checkInterval);
            }

            // 超时处理
            if (time() - $startTime >= $maxDuration) {
                // 清理缓存
                Redis::del($cacheKey);

                echo "data: " . json_encode([
                    'type' => 'timeout',
                    'message' => '轮询超时，请刷新页面查看最新状态',
                    'data' => [
                        'roles' => $roles,
                        'scenes' => $scenes
                    ]
                ], JSON_UNESCAPED_UNICODE) . "\n\n";
                ob_flush();
                flush();
            }
        };
    }

    // 轮训获取图片任务结果
    private function loopGetPicTaskResult($task_id) {
        $task = MpGeneratePicTask::where('id', $task_id)->first();
        if (!$task) {
            return '';
        }

        // 检查任务模型类型
        $model = getProp($task, 'model');
        $isVolcModel = in_array($model, \App\Consts\BaseConst::VOLC_PIC_MODELS);

        // 火山API是同步返回结果的，直接检查任务状态
        if ($isVolcModel) {
            // 火山API任务，直接返回结果（已在创建时处理完成）
            if ($task->status === 'success' && $task->result_url) {
                $result_urls = is_array($task->result_url) ? $task->result_url : json_decode($task->result_url, true);
                return is_array($result_urls) ? $result_urls[0] : $task->result_url;
            }
            return '';
        }

        // 即梦AI需要轮询查询状态
        $start_count = 0;
        $img_url = '';
        while ($start_count <= 20) {    // 循环20次请求,每3s一次
            sleep(3);

            $task = MpGeneratePicTask::where('id', $task_id)->first();
            $statusInfo = $this->aiImageGenerationService->queryTaskStatus($task);

            if ($statusInfo['status'] === 'success') {
                $task->updateStatus('success', [
                    'result_url'    => $statusInfo['result_url'],
                    'result_json'   => $statusInfo['result_json'] ?? []
                ]);

                // 同步调整分镜图片状态和结果
                if (getProp($task, 'alias_segment_id')) {
                    DB::table('mp_episode_segments')->where('segment_id', getProp($task, 'alias_segment_id'))->update([
                        'img_url'   => $statusInfo['result_url'][0],
                        'pic_task_status'   => '已完成',
                    ]);
                }
                $img_url = $statusInfo['result_url'][0];
                break;
            } elseif ($statusInfo['status'] === 'failed') {
                $task->updateStatus('failed', [
                    'error_message' => $statusInfo['error_message'],
                    'result_json'   => $statusInfo['result_json'] ?? []
                ]);

                if (getProp($task, 'alias_segment_id')) {
                    DB::table('mp_episode_segments')->where('segment_id', getProp($task, 'alias_segment_id'))->update([
                        'pic_task_status'   => '失败',
                    ]);
                }
                break;
            }
            $start_count++;
        }

        return $img_url;
    }

    /**
     * 从分镜内容中提取涉及的角色
     */
    private function extractCharactersFromSegment($segment_content, $available_characters, $roles) {
        $found_characters = [];
        
        foreach ($available_characters as $character) {
            // 检查角色名称是否在分镜内容中出现
            if (strpos($segment_content, $character) !== false) {
                $found_characters[] = $character;
            }
        }

        if (!$found_characters) {
            foreach ($roles as $character) {
                // 检查角色名称是否在分镜内容中出现
                if (strpos($segment_content, $character) !== false) {
                    $found_characters[] = $character;
                }
            }
        }
        
        return array_unique($found_characters);
    }

    // 从分镜剧本中提取关键字段
    private function handleSegmentContent(&$data) {
        $segmentContent = getProp($data, 'segment_content');
        // 解析分镜详细信息 - 与 Helpers.php 中 handleEpisodeContent 的 segmentData 结构保持一致
        $segmentData = [
            'description'     => '',
            'composition'     => '',
            'camera_movement' => '',
            'voice_actor'     => '',
            'dialogue'        => '',
            'frame_type'      => '',
            'scene'           => '',        // 场景
            'characters'      => '',        // 出镜角色
            'tail_frame'      => '',        // 尾帧描述
            'emotion'         => '中性',    // 情感
            'gender'          => '0',       // 性别（0未知，1男，2女）
            'speed_ratio'     => 0,         // 语速
            'loudness_ratio'  => 0,         // 音量
            'emotion_scale'   => 4,         // 情感强度
            'pitch'           => 0,         // 音调
        ];
        
        // 用于存储需要从 segment_content 中移除的匹配项
        $replaceEmptyArr = [];
        
        // 提取各个字段 - 兼容中文冒号和英文冒号，支持多种表达方式
        if (preg_match('/(?:画面描述|镜头描述|场景描述)[：:]\s*([^\n]+)/u', $segmentContent, $descMatch)) {
            $segmentData['description'] = trim($descMatch[1]);
            $data['description'] = trim($descMatch[1]);
        }
        if (preg_match('/(?:构图设计|构图|镜头构图)[：:]\s*([^\n]+)/u', $segmentContent, $compMatch)) {
            $segmentData['composition'] = trim($compMatch[1]);
            $data['composition'] = trim($compMatch[1]);
        }
        if (preg_match('/(?:运镜调度|运镜|镜头运动|摄影机运动)[：:]\s*([^\n]+)/u', $segmentContent, $cameraMatch)) {
            $segmentData['camera_movement'] = trim($cameraMatch[1]);
            $data['camera_movement'] = trim($cameraMatch[1]);
        }
        if (preg_match('/(?:配音角色|配音|角色|声优)[：:]\s*([^\n]+)/u', $segmentContent, $voiceMatch)) {
            $segmentData['voice_actor'] = trim($voiceMatch[1]);
            $data['voice_actor'] = trim($voiceMatch[1]);
        }
        if (preg_match('/(?:台词内容|台词|对白|对话)[：:]\s*([^\n]+)/u', $segmentContent, $dialogueMatch)) {
            $segmentData['dialogue'] = trim($dialogueMatch[1]);
            $data['dialogue'] = trim($dialogueMatch[1]);
        }
        if (preg_match('/(?:画面类型|镜头类型|类型)[：:]\s*([^\n]+)/u', $segmentContent, $frameMatch)) {
            $segmentData['frame_type'] = trim($frameMatch[1]);
            $data['frame_type'] = trim($frameMatch[1]);
        }
        // 场景字段
        if (preg_match('/(?:场景|拍摄场景|背景场景|环境)[：:]\s*([^\n]+)/u', $segmentContent, $sceneMatch)) {
            $segmentData['scene'] = trim($sceneMatch[1]);
            $data['scene'] = trim($sceneMatch[1]);
        }
        // 出镜角色字段
        if (preg_match('/(?:出镜角色|角色出镜|登场角色|人物)[：:]\s*([^\n]+)/u', $segmentContent, $charactersMatch)) {
            $segmentData['characters'] = trim($charactersMatch[1]);
            $data['characters'] = trim($charactersMatch[1]);
        }
        // 尾帧描述字段
        if (preg_match('/(?:尾帧描述|尾帧|结束帧|最后一帧|结尾画面|结束画面)[：:]\s*([^\n]+)/u', $segmentContent, $tailFrameMatch)) {
            $segmentData['tail_frame'] = trim($tailFrameMatch[1]);
            $data['tail_frame'] = trim($tailFrameMatch[1]);
        }
        
        // 情感字段
        if (preg_match('/(?:情感|情绪|感情)[：:]\s*([^\n]+)/u', $segmentContent, $emotionMatch)) {
            $replaceEmptyArr[] = trim($emotionMatch[0]);
            $segmentData['emotion'] = trim($emotionMatch[1]);
            $data['emotion'] = trim($emotionMatch[1]);
        }
        
        // 性别字段
        if (preg_match('/(?:性别)[：:]\s*([^\n]+)/u', $segmentContent, $genderMatch)) {
            $replaceEmptyArr[] = trim($genderMatch[0]);
            $genderStr = trim($genderMatch[1]);
            if (strpos($genderStr, '男') !== false || $genderStr === '1') {
                $segmentData['gender'] = '1';
                $data['gender'] = '1';
            } elseif (strpos($genderStr, '女') !== false || $genderStr === '2') {
                $segmentData['gender'] = '2';
                $data['gender'] = '2';
            } else {
                $segmentData['gender'] = '0';
                $data['gender'] = '0';
            }
        }
        
        // 语速字段
        if (preg_match('/(?:语速|说话速度)[：:]\s*([-+]?[0-9]*\.?[0-9]+)/u', $segmentContent, $speedMatch)) {
            $replaceEmptyArr[] = trim($speedMatch[0]);
            $segmentData['speed_ratio'] = (float)trim($speedMatch[1]);
            $data['speed_ratio'] = (float)trim($speedMatch[1]);
        }
        
        // 音量字段
        if (preg_match('/(?:音量|声音大小)[：:]\s*([-+]?[0-9]*\.?[0-9]+)/u', $segmentContent, $loudnessMatch)) {
            $replaceEmptyArr[] = trim($loudnessMatch[0]);
            $segmentData['loudness_ratio'] = (float)trim($loudnessMatch[1]);
            $data['loudness_ratio'] = (float)trim($loudnessMatch[1]);
        }
        
        // 情感强度字段
        if (preg_match('/(?:情感强度|情绪强度)[：:]\s*([0-9]+)/u', $segmentContent, $scaleMatch)) {
            $replaceEmptyArr[] = trim($scaleMatch[0]);
            $segmentData['emotion_scale'] = (int)trim($scaleMatch[1]);
            $data['emotion_scale'] = (int)trim($scaleMatch[1]);
        }
        
        // 音调字段
        if (preg_match('/(?:音调|音高)[：:]\s*([-+]?[0-9]+)/u', $segmentContent, $pitchMatch)) {
            $replaceEmptyArr[] = trim($pitchMatch[0]);
            $segmentData['pitch'] = (int)trim($pitchMatch[1]);
            $data['pitch'] = (int)trim($pitchMatch[1]);
        }
        
        // 从 segment_content 中移除已提取的配音参数字段
        if (!empty($replaceEmptyArr)) {
            $data['segment_content'] = str_replace($replaceEmptyArr, '', $segmentContent);
        }
        
        // 如果有配音角色，查询音色信息并验证情感
        $voice_actor = $segmentData['voice_actor'];
        if (!empty($voice_actor) && $voice_actor !== '旁白') {
            // 从当前分镜所属的剧集或动漫中查找角色音色信息
            $anime_id = getProp($data, 'anime_id');
            $episode_id = getProp($data, 'episode_id');
            
            // 优先从剧集的角色列表中查找
            $roles = [];
            if ($episode_id) {
                $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->first();
                if ($episode && getProp($episode, 'roles')) {
                    $roles = json_decode(getProp($episode, 'roles'), true) ?: [];
                }
            }
            
            // 如果剧集中没有，从动漫的角色列表中查找
            if (empty($roles) && $anime_id) {
                $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
                if ($anime && getProp($anime, 'roles')) {
                    $roles = json_decode(getProp($anime, 'roles'), true) ?: [];
                }
            }
            
            // 查找匹配的角色音色信息
            $timbre_info = null;
            foreach ($roles as $role) {
                if (getProp($role, 'role') === $voice_actor) {
                    $timbre_info = $role;
                    break;
                }
            }
            
            // 如果找到音色信息，添加到数据中
            if ($timbre_info) {
                $voice_type = getProp($timbre_info, 'voice_type');
                $voice_name = getProp($timbre_info, 'voice_name');
                $voice_audio_url = getProp($timbre_info, 'voice_audio_url');
                
                if ($voice_type) {
                    $data['voice_type'] = $voice_type;
                    $segmentData['voice_type'] = $voice_type;
                }
                if ($voice_name) {
                    $data['voice_name'] = $voice_name;
                    $segmentData['voice_name'] = $voice_name;
                }
                if ($voice_audio_url) {
                    $data['voice_audio_url'] = $voice_audio_url;
                    $segmentData['voice_audio_url'] = $voice_audio_url;
                }
                
                // 验证情感是否在音色支持的情感列表中
                if ($voice_type) {
                    $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', $voice_type)->value('emotion');
                    if ($timbre_emotion) {
                        $timbre_emotion = explode('，', $timbre_emotion);
                    } else {
                        $timbre_emotion = [];
                    }

                    $emotion = getProp($segmentData, 'emotion', '中性');
                    if (!in_array($emotion, $timbre_emotion)) {
                        $emotion = '中性';
                    }
                    $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
                    $emotion_list = array_flip($emotion_list);
                    $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
                    $data['emotion_type'] = $emotion_type;
                    $segmentData['emotion_type'] = $emotion_type;
                }
            }
        }

        return $segmentData;
    }

    public function createSegmentVideoTask($data) {
        $segment_id = getProp($data, 'segment_id');
        $tail_frame = getProp($data, 'tail_frame');
        
        if (!$segment_id) {
            Utils::throwError('1002:分镜ID不能为空');
        }
        
        // 获取分镜信息
        $segment = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->first();
        if (!$segment) {
            Utils::throwError('20003:分镜不存在');
        }
        
        $segment = (array)$segment;
        
        // 获取分镜内容
        $segmentContent = getProp($segment, 'segment_content', '');
        
        // 如果没有传入尾帧描述，使用分镜表中的尾帧描述
        if (!$tail_frame) {
            $tail_frame = getProp($segment, 'tail_frame', '');
        }
        
        // 构建完整的提示词
        $fullPrompt = $segmentContent;
        if ($tail_frame) {
            $fullPrompt .= "\n尾帧描述：$tail_frame";
        }
        
        // 智能选择视频时长
        // $videoDuration = $this->calculateOptimalVideoDuration($segmentContent, $tail_frame);
        $videoDuration = -1;
        
        // 默认使用 Seedance 1.0 Pro 模型
        $model = getProp($data, 'model', 'doubao-seedance-1-0-pro-250528');
        
        // 构建视频生成参数
        $videoParams = [
            'model' => $model,
            'alias_segment_id' => $segment_id,
            'prompt' => $fullPrompt,
            'video_duration' => $videoDuration,
            'video_resolution' => '720P',
            'seed' => -1,
            'ratio' => '16:9',
            'generate_audio' => false,
            'draft' => false,
            'watermark' => false,
            'camera_fixed' => false,
            'callback_url' => 'http://mpaudio.yqsd.cn/api/video/seedanceCallback'
        ];
        
        // 如果分镜有图片，作为首帧
        $hasImage = !empty(getProp($segment, 'img_url'));
        $hasVideo = !empty(getProp($segment, 'video_url'));
        
        if ($hasImage) {
            $videoParams['first_frame_url'] = getProp($segment, 'img_url');
        }
        
        // 构建content数组
        $videoParams['content'] = [
            [
                'type' => 'text',
                'text' => $videoParams['prompt'],
            ]
        ];
        
        // 如果有首帧图片，添加到content中
        if ($hasImage) {
            if (in_array($model, ['doubao-seedance-2-0-260128', 'doubao-seedance-2-0-fast-260128'])) {
                $videoParams['content'][] = [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => $videoParams['first_frame_url'],
                    ],
                    'role' => 'reference_image',
                ];
            }else {
                $videoParams['content'][] = [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => $videoParams['first_frame_url'],
                    ],
                    'role' => 'first_frame',
                ];
            }
        }
        
        // 获取配音音频URL
        $audioUrl = getProp($segment, 'audio_url');
        $audioDuration = getProp($segment, 'audio_duration');
        
        // 音频传入的前提：必须有至少一张图片或一个视频
        $canUseAudio = ($hasImage || $hasVideo) && !empty($audioUrl);

        // dd($videoParams);
        
        // 根据模型选择不同的视频生成方法
        if (strpos($model, 'jimeng') !== false) {
            // 即梦模型参数调整
            $videoParams['video_duration'] = $audioDuration > 5 ? 10 : 5; // 即梦只支持5s或10s
            unset($videoParams['content']); // 即梦不使用content格式
            $task = $this->aiVideoGenerationService->createJimengTask($videoParams);
        } elseif (strpos($model, 'kling') !== false) {
            // Keling模型参数调整
            $videoParams['video_duration'] = max(3, min(15, ceil($audioDuration))); // Keling支持3-15秒
            $videoParams['aspect_ratio'] = $videoParams['ratio']; // Keling使用aspect_ratio
            unset($videoParams['ratio']);
            unset($videoParams['content']); // Keling不使用content格式
            $task = $this->aiVideoGenerationService->createKelingOmniTask($videoParams);
        } else {
            // 检查是否为 Seedance 2.0 模型，如果是则需要处理配音音频
            if ($canUseAudio && in_array($model, ['doubao-seedance-2-0-260128', 'doubao-seedance-2-0-fast-260128'])) {
                // 添加配音音频到content中
                $videoParams['content'][] = [
                    'type' => 'audio_url',
                    'audio_url' => [
                        'url' => $audioUrl,
                    ],
                    'role' => 'reference_audio',
                ];
                
                // 优化提示词，增加音频相关描述
                $audioPrompt = "全程使用音频1作为视频配音。" . $fullPrompt;
                $videoParams['prompt'] = $audioPrompt;
                $videoParams['content'][0]['text'] = $audioPrompt;
            } else if ($audioDuration) {    // 有音频时长则需将视频时长设置为比音频时长更长的整数
                $videoParams['video_duration'] = max(2, min(12, ceil($audioDuration)));   // seedance支持2-12s
            }
            // Seedance模型（默认）
            $task = $this->aiVideoGenerationService->createSeedanceTask($videoParams);
        }
        
        // 更新分镜表的视频任务信息
        DB::table('mp_episode_segments')->where('segment_id', $segment_id)->update([
            'video_task_id' => $task->id,
            'video_task_status' => '生成中',
            'updated_at' => date('Y-m-d H:i:s')
        ]);
        
        return [
            'task_id' => $task->id,
            'status' => $task->status,
            'segment_id' => $segment_id,
            'video_duration' => $videoDuration
        ];
    }

    /**
     * 根据提示词内容智能计算最优视频时长
     * 
     * @param string $segmentContent 分镜内容
     * @param string $tailFrame 尾帧描述
     * @return int 视频时长（2-12秒）
     */
    public function calculateOptimalVideoDuration($segmentContent, $tailFrame = '') {
        // 合并所有文本内容
        $fullText = trim($segmentContent . ' ' . $tailFrame);
        
        // 如果没有内容，返回默认时长
        if (empty($fullText)) {
            return 5;
        }
        
        // 计算文本长度（中文字符按2个字符计算）
        $textLength = mb_strlen($fullText, 'UTF-8');
        $chineseCharCount = preg_match_all('/[\x{4e00}-\x{9fff}]/u', $fullText);
        $adjustedLength = $textLength + $chineseCharCount; // 中文字符权重更高
        
        // 分析内容复杂度
        $complexityScore = $this->analyzeContentComplexity($fullText);
        
        // 基础时长计算（根据文本长度）
        $baseDuration = 3; // 默认2秒
        
        // if ($adjustedLength <= 20) {
        //     $baseDuration = 3;
        // } elseif ($adjustedLength <= 40) {
        //     $baseDuration = 4;
        // } elseif ($adjustedLength <= 60) {
        //     $baseDuration = 5;
        // } elseif ($adjustedLength <= 80) {
        //     $baseDuration = 6;
        // } elseif ($adjustedLength <= 100) {
        //     $baseDuration = 7;
        // } elseif ($adjustedLength <= 120) {
        //     $baseDuration = 8;
        // } else {
        //     $baseDuration = 9;
        // }
        
        // 根据内容复杂度调整时长
        $finalDuration = $baseDuration + $complexityScore;
        
        // 确保时长在2-12秒范围内
        return max(2, min(12, $finalDuration));
    }

    /**
     * 分析内容复杂度，返回时长调整值
     * 
     * @param string $text 文本内容
     * @return int 调整值（-2到+3）
     */
    private function analyzeContentComplexity($text) {
        $score = 0;
        
        // 动作关键词（需要更长时间展现）
        $actionKeywords = [
            '跑步', '奔跑', '飞行', '跳跃', '战斗', '打斗', '追逐', '逃跑', '舞蹈', '表演',
            '变化', '变身', '爆炸', '崩塌', '建造', '制作', '烹饪', '绘画', '写字',
            '移动', '旋转', '翻滚', '攀爬', '游泳', '潜水', '滑行', '飞翔'
        ];
        
        // 静态关键词（可以用较短时间）
        $staticKeywords = [
            '站立', '坐着', '躺着', '静止', '凝视', '思考', '沉思', '观察', '等待',
            '睡觉', '休息', '静坐', '冥想', '阅读', '听音乐'
        ];
        
        // 复杂场景关键词（需要更长时间）
        $complexSceneKeywords = [
            '人群', '聚会', '会议', '战争', '庆典', '仪式', '表演', '比赛', '竞技',
            '多人', '群体', '团队', '合作', '互动', '对话', '争论', '讨论'
        ];
        
        // 情感关键词（需要适中时间表现）
        $emotionKeywords = [
            '哭泣', '笑容', '愤怒', '惊讶', '恐惧', '悲伤', '喜悦', '兴奋', '紧张',
            '感动', '震惊', '困惑', '失望', '希望', '绝望', '温柔', '严肃'
        ];
        
        // 检查动作关键词
        foreach ($actionKeywords as $keyword) {
            if (strpos($text, $keyword) !== false) {
                $score += 1;
                break; // 避免重复加分
            }
        }
        
        // 检查静态关键词
        foreach ($staticKeywords as $keyword) {
            if (strpos($text, $keyword) !== false) {
                $score -= 1;
                break;
            }
        }
        
        // 检查复杂场景关键词
        foreach ($complexSceneKeywords as $keyword) {
            if (strpos($text, $keyword) !== false) {
                $score += 1;
                break;
            }
        }
        
        // 检查情感关键词
        foreach ($emotionKeywords as $keyword) {
            if (strpos($text, $keyword) !== false) {
                $score += 1;
                break;
            }
        }
        
        // 检查时间相关词汇
        if (preg_match('/缓慢|慢慢|渐渐|逐渐|慢动作/', $text)) {
            $score += 1;
        }
        
        if (preg_match('/快速|迅速|急速|瞬间|立刻|马上/', $text)) {
            $score -= 1;
        }
        
        // 检查转场词汇
        if (preg_match('/然后|接着|随后|紧接着|同时|与此同时/', $text)) {
            $score += 1;
        }
        
        // 检查环境描述（复杂环境需要更多时间）
        if (preg_match('/风景|景色|环境|背景|氛围|光线|阴影|色彩|细节/', $text)) {
            $score += 1;
        }
        
        // 检查对话内容（对话需要更多时间）
        if (preg_match('/说|讲|告诉|回答|询问|问|对话|交谈/', $text)) {
            $score += 1;
        }
        
        // 限制调整范围
        return max(-1, min(4, $score));
    }

    /**
     * 创建完整视频合成任务
     * 
     * @param array $data
     * @return array
     */
    public function createCompleteVideoTask($data)
    {
        $animeId = getProp($data, 'anime_id');
        $episodeId = getProp($data, 'episode_id');

        // 验证参数
        if (!$animeId || !$episodeId) {
            Utils::throwError('20003:anime_id 和 episode_id 不能为空');
        }

        // 检查是否已存在处理中的任务
        $existingTask = DB::table('mp_complete_video_tasks')
            ->where('anime_id', $animeId)
            ->where('episode_id', $episodeId)
            ->whereIn('generate_status', ['执行中'])
            ->first();

        if ($existingTask) {
            Utils::throwError('20003:该剧集已有正在处理的视频合成任务');
        }

        // 获取所有分镜的配音视频，按 segment_number 排序
        $segments = DB::table('mp_episode_segments')
            ->where('anime_id', $animeId)
            ->where('episode_id', $episodeId)
            ->orderBy('segment_number')
            ->select('segment_id', 'segment_number', 'video_url', 'audio_url')
            ->get();

        if ($segments->isEmpty()) {
            Utils::throwError('20003:未找到该剧集的分镜数据');
        }

        // 检查是否所有分镜都有配音
        $missingVideos = $segments->filter(function($segment) {
            return empty($segment->audio_url);
        });

        if ($missingVideos->isNotEmpty()) {
            Utils::throwError('20003:存在未完成配音的分镜，请确保所有分镜都已完成配音');
        }

        // 构建 generate_json
        $generateJson = [];
        $sequence = 1;
        foreach ($segments as $segment) {
            $generateJson[] = [
                'sequence' => $sequence++,
                'video_url' => $segment->video_url,
                'audio_url' => $segment->audio_url,
            ];
        }

        // 创建任务
        $now = date('Y-m-d H:i:s');
        $taskId = DB::table('mp_complete_video_tasks')->insertGetId([
            'anime_id' => $animeId,
            'episode_id' => $episodeId,
            'generate_json' => json_encode($generateJson, 256),
            'generate_status' => '执行中',
            'complete_video_url' => '',
            'created_at' => $now,
            'updated_at' => $now
        ]);

        dLog('anime')->info('创建完整视频合成任务', [
            'task_id' => $taskId,
            'anime_id' => $animeId,
            'episode_id' => $episodeId,
            'segment_count' => count($generateJson)
        ]);

        return [
            'task_id' => $taskId,
            'anime_id' => $animeId,
            'episode_id' => $episodeId,
            'segment_count' => count($generateJson),
            'generate_status' => '执行中'
        ];
    }

    public function generateImg($data) {
        $prompt = getProp($data, 'prompt');
        $model = getProp($data, 'model', 'doubao-seedream-5-0-lite-260128');

        // 参数验证
        if (!$prompt) Utils::throwError('20003:请提供提示词');

        try {
            // 创建图片生成任务
            $params = [
                'prompt' => $prompt,
                'model' => $model,
                'ref_img_urls' => [],
                'width' => 2048,
                'height' => 2048
            ];
            
            $task = $this->aiImageGenerationService->createImageGenerationTask($params);
            $task_id = $task->id;
            
            if (!$task_id) {
                Utils::throwError('20003:创建图片生成任务失败');
            }
            
            // 检查任务模型类型
            $model = getProp($task, 'model');
            $isVolcModel = in_array($model, \App\Consts\BaseConst::VOLC_PIC_MODELS);
            
            // 火山API是同步返回结果的，直接检查任务状态
            if ($isVolcModel) {
                // 等待队列处理（火山API通过队列异步处理）
                sleep(5);
                
                // 刷新任务状态
                $task = MpGeneratePicTask::where('id', $task_id)->first();
                
                if ($task->status === 'success' && $task->result_url) {
                    $result_urls = is_array($task->result_url) ? $task->result_url : json_decode($task->result_url, true);
                    $img_url = is_array($result_urls) ? $result_urls[0] : $task->result_url;
                    return $img_url;
                } elseif ($task->status === 'failed') {
                    Utils::throwError('20003:图片生成失败，' . ($task->error_message ?? '未知错误'));
                }
                
                // 如果还在处理中，提示用户稍后查看
                Utils::throwError('20003:图片生成中，请稍后刷新查看结果');
            }
            
            // 即梦AI需要轮询获取结果（3分钟超时，每3秒查询一次）
            $start_time = time();
            $timeout = 180; // 3分钟
            $img_url = '';
            
            while (time() - $start_time < $timeout) {
                sleep(3);
                
                $task = MpGeneratePicTask::where('id', $task_id)->first();
                $statusInfo = $this->aiImageGenerationService->queryTaskStatus($task);
                
                if ($statusInfo['status'] === 'success') {
                    $task->updateStatus('success', [
                        'result_url' => $statusInfo['result_url'],
                        'result_json' => $statusInfo['result_json'] ?? []
                    ]);
                    
                    $img_url = $statusInfo['result_url'][0];
                    break;
                } elseif ($statusInfo['status'] === 'failed') {
                    $task->updateStatus('failed', [
                        'error_message' => $statusInfo['error_message'],
                        'result_json' => $statusInfo['result_json'] ?? []
                    ]);
                    dLog('anime')->error('图片生成失败，', [
                        'error' => $statusInfo['error_message'] ?? '未知错误'
                    ]);
                    Utils::throwError('20003:图片生成失败，' . ($statusInfo['error_message'] ?? '未知错误'));
                }
            }
            
            if (empty($img_url)) {
                Utils::throwError('20003:图片生成超时，请稍后重试');
            }
            
            return $img_url;
            
        } catch (\Exception $e) {
            dLog('anime')->error('更新角色或场景失败', [
                'error' => $e->getMessage()
            ]);
            logDB('anime', 'error', '更新角色或场景失败', ['error' => $e->getMessage()]);
            
            Utils::throwError('20003:' . $e->getMessage());
        }
    }

    /**
     * 使用文生文模型解析分镜内容
     */
    private function parseSegmentContentWithAI($segment_content) {
        try {
            // 使用DeepSeek服务的公开方法解析分镜内容
            return $this->DeepSeekService->parseSegmentContent($segment_content);
        } catch (\Exception $e) {
            // 如果AI解析失败，记录日志并返回原内容
            dLog('anime')->error('AI解析分镜内容失败: ' . $e->getMessage());
            return $segment_content;
        }
    }
}
