From 9fb8a83fa5d60d24bc5e2f39b709e71e38013893 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 24 Aug 2022 21:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E5=B0=81=E8=A3=85=E7=AE=80?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProgramConstructController.php | 26 +---- .../ProgramVideoConstructController.php | 27 +---- app/Util/ProgramVideoUtil.php | 108 ++++++++++++++++++ app/Util/TimeUtil.php | 19 --- 4 files changed, 112 insertions(+), 68 deletions(-) create mode 100644 app/Util/ProgramVideoUtil.php delete mode 100644 app/Util/TimeUtil.php diff --git a/app/Http/Controllers/ProgramConstructController.php b/app/Http/Controllers/ProgramConstructController.php index a5559ab..8e582ab 100644 --- a/app/Http/Controllers/ProgramConstructController.php +++ b/app/Http/Controllers/ProgramConstructController.php @@ -6,6 +6,7 @@ use App\Models\Programs; use App\Models\ProgramVideos; use App\Models\VideoComments; use App\Models\VideoParts; +use App\Util\ProgramVideoUtil; use App\Util\TimeUtil; use Carbon\Carbon; use Illuminate\Database\QueryException; @@ -97,30 +98,6 @@ class ProgramConstructController extends BaseController $video_pivot->start_part = $match["part"]; $video_pivot->start_time = $time; $video_pivot->stop_part = $match["part"]; - if (sizeof($video_parts) > 0) { - /** - * 计算开始时间 - * @var VideoParts $cur_part - */ - $cur_part = $video_parts->where("part_num", "=", $match["part"])->first(); - if ($cur_part) { - // 根据标题名称,判断开始时间 - $title = $cur_part->title; - $date_str = substr($title, 0, 13); - $base_time = Carbon::createFromFormat("Ymd_Hi", $date_str); - $start_time = Carbon::createFromFormat("H:i:s", $time); - $video_pivot->created_at = TimeUtil::calculate_program_time( - $base_time, - $start_time->secondsSinceMidnight() - ); - if ($cur_part->duration->diffInMinutes($start_time) < 20) { - $video_pivot->stop_part += 1; - if ($video_pivot->stop_part > $video_parts->pluck("part_num")->max()) { - $video_pivot->stop_part = $video_parts->pluck("part_num")->max(); - } - } - } - } $program->save(); $program->video_pivots()->save($video_pivot); } @@ -131,6 +108,7 @@ class ProgramConstructController extends BaseController "content" => $e->getMessage() ]); } + ProgramVideoUtil::fix_created_at_by_part_info($bvid); return redirect(route("program.construct.list")); } diff --git a/app/Http/Controllers/ProgramVideoConstructController.php b/app/Http/Controllers/ProgramVideoConstructController.php index 57f4684..b81ca40 100644 --- a/app/Http/Controllers/ProgramVideoConstructController.php +++ b/app/Http/Controllers/ProgramVideoConstructController.php @@ -4,8 +4,7 @@ namespace App\Http\Controllers; use App\Models\Programs; use App\Models\ProgramVideos; -use App\Util\TimeUtil; -use Illuminate\Database\Eloquent\Builder; +use App\Util\ProgramVideoUtil; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; use Illuminate\Support\Carbon; @@ -125,29 +124,7 @@ class ProgramVideoConstructController extends BaseController } $each_time = $request->post("each_time"); $each_time_sec = Carbon::createFromFormat("H:i:s", $each_time)->secondsSinceMidnight(); - /** - * @var $program_videos ProgramVideos[] - */ - $program_videos = ProgramVideos::query() - ->where("video_bvid", "=", $program_video->video_bvid) - ->where("id", "!=", $program_video->id) - ->where(function (Builder $query) use ($program_video) { - $query->where("created_at", ">", $program_video->created_at)->orWhere("created_at", "=", null); - }) - ->get(); - $created_at = $program_video->created_at; - $base_time = $created_at->subSeconds($program_video->start_sec)->subSeconds(($program_video->start_part - 1) * $each_time_sec); - foreach ($program_videos as $video) { - if ($video->id === $program_video->id) { - continue; - } - $video->created_at = TimeUtil::calculate_program_time( - $base_time, - $video->start_sec, - ($video->start_part - 1) * $each_time_sec - ); - $video->update(); - } + ProgramVideoUtil::fix_created_at_by_manual($program_video, $each_time_sec); return redirect(route("program.construct.list")); } } diff --git a/app/Util/ProgramVideoUtil.php b/app/Util/ProgramVideoUtil.php new file mode 100644 index 0000000..54940c6 --- /dev/null +++ b/app/Util/ProgramVideoUtil.php @@ -0,0 +1,108 @@ +created_at->copy() + ->subSeconds($base_video->start_sec)->subSeconds(($base_video->start_part - 1) * $each_time_sec); + $query = ProgramVideos::query() + ->where("video_bvid", "=", $base_video->video_bvid) + ->where("id", "!=", $base_video->id); + if (!$force) { + $query->where(function (Builder $query) use ($base_video) { + $query->where("created_at", ">", $base_video->created_at)->orWhere("created_at", "=", null); + }); + } + $program_videos = $query->get(); + DB::beginTransaction(); + try { + /** + * @var ProgramVideos $video + */ + foreach ($program_videos as $video) { + if ($video->id === $base_video->id) { + continue; + } + $video->created_at = static::calculate_program_time( + $base_time, + $video->start_sec, + ($video->start_part - 1) * $each_time_sec + ); + $video->save(); + } + DB::commit(); + return true; + } catch (\Exception $e) { + DB::rollBack(); + return false; + } + } + + public static function fix_created_at_by_part_info(string $bvid, bool $force = false): bool + { + $video_parts = VideoParts::query()->where("bvid", "=", $bvid)->get(); + if (sizeof($video_parts) === 0) { + return false; + } + $query = ProgramVideos::query()->where("video_bvid", "=", $bvid); + if (!$force) { + $query->where("created_at", "=", null); + } + $program_videos = $query->get(); + DB::beginTransaction(); + try { + /** + * @var ProgramVideos $program_video + */ + foreach ($program_videos as $program_video) { + /** + * 计算开始时间 + * @var VideoParts $cur_part + */ + $cur_part = $video_parts->where("part_num", "=", $program_video->start_part)->first(); + if ($cur_part) { + // 根据标题名称,判断开始时间 + $title = $cur_part->title; + $date_str = substr($title, 0, 13); + $base_time = Carbon::createFromFormat("Ymd_Hi", $date_str); + $start_time = Carbon::createFromFormat("H:i:s", $program_video->start_time); + $program_video->created_at = static::calculate_program_time( + $base_time, + $start_time->secondsSinceMidnight() + ); + if ($cur_part->duration->diffInMinutes($start_time) < 20) { + $program_video->stop_part += 1; + if ($program_video->stop_part > $video_parts->pluck("part_num")->max()) { + $program_video->stop_part = $video_parts->pluck("part_num")->max(); + } + } + } + } + DB::commit(); + return true; + } catch (\Exception $e) { + DB::rollBack(); + return false; + } + } + + protected static function calculate_program_time(Carbon $base_time, int $seconds, int $bias = 0): Carbon + { + $time = $base_time->copy()->addSeconds($seconds)->addSeconds($bias); + if ($time->second > 30) { + $time->addMinute(); + } + $time->seconds(0); + return $time; + } +} diff --git a/app/Util/TimeUtil.php b/app/Util/TimeUtil.php deleted file mode 100644 index c05133d..0000000 --- a/app/Util/TimeUtil.php +++ /dev/null @@ -1,19 +0,0 @@ -copy()->addSeconds($seconds)->addSeconds($bias); - if ($time->second > 30) { - $time->addMinute(); - } - $time->seconds(0); - return $time; - } -}