<?php

namespace App\Console\Commands\Video\WechatCheck;

use App\Service\Miniprogram\Wechat\AccessTokenService;
use App\Service\Util\Support\Http\HttpRequestService;
use App\Service\Util\Support\Http\WechatURL;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SyncMediaInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'WechatCheck:SyncMediaInfo {--video_ids= : videos.id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取短剧分集详细信息';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $video_ids = $this->option('video_ids');
        $videoIds = [];
        if($video_ids) {
            $videoIds = explode(',', $video_ids);
        }

        DB::table('video_wechat_check')
            ->where('status', '<>', 0)
            ->where('is_enabled', 1)
            ->where('drama_id', '<>', 0)
            ->when($videoIds, function ($query, $videoIds) {
                return $query->whereIn('video_id', $videoIds);
            })->select('video_id', 'id', 'drama_id', 'appid')
            ->orderBy('id')
            ->chunk(10, function ($items) {
                foreach ($items as $item) {
                    $this->syncInfo($item);
                }
            });
    }

    public function syncInfo($item) {
        $appid = $item->appid ?: config('wechat.duanju.masterAppid');
        $accessToken = Redis::get(AccessTokenService::getAccessTokenRedisKey($appid));
        $offset = 0;
        while (true) {
            $parsedContent = HttpRequestService::simplePost(WechatURL::vod_listmedia . $accessToken, [
                'drama_id' => $item->drama_id,
                'limit' => 100,
                'offset' => $offset
            ]);
            $offset += 100;
            if(false === $parsedContent || (0 != $parsedContent['errcode'] ?? 0)) {
                myLog('SyncMediaInfo')->error('拉取短剧分集信息失败', [
                    'appid' => $appid,
                    'video_id' => $item->video_id, 'drama_id' => $item->drama_id,
                ]);
                break;
            }

            $media_info_list = $parsedContent['media_info_list'];
            $now = date('Y-m-d H:i:s');
            foreach ($media_info_list as $media_info) {
                $audit_detail = $media_info['audit_detail'];
                $media_id = $media_info['media_id'];
                DB::table('video_series_wechat_check')
                    ->where(['video_id' => $item->video_id, 'is_enabled' => 1, 'media_id' => $media_id])
                    ->update([
                        'check_status' => $audit_detail['status'],
                        'check_at' => date('Y-m-d H:i:s', $audit_detail['audit_time']),
                        'check_reason' => $audit_detail['check_reason'],
                        'evidence_material_id_list' => \json_encode($audit_detail['evidence_material_id_list']),
                        'updated_at' => $now,
                    ]);
            }

        }

    }
}