<?php

namespace Modules\Video\Services;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Modules\Common\Errors\Errors;
use Modules\Common\Exceptions\CommonBusinessException;
use Modules\Common\Support\Http\HttpRequest;
use Modules\Common\Support\Http\WechatURL;
use Modules\Manage\Services\WechatMiniprogramService;


class WechatCheckSyncService
{
    /**
     * 拉取上传
     * @param $item 剧集信息
     * @param $accessToken 小程序accessToken
     * @return  string task_id
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function pullupload($item, $accessToken) {
        return HttpRequest::simplePost(WechatURL::vod_pullupload. $accessToken, [
            'media_name' => $item->media_name,
            'media_url' => $item->video_url
        ]);
    }

    /**
     * 获取微信短剧剧集的播放链接信息
     * @param $seriesId
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function getMedialinkInfo($seriesId) {
        $syncInfo = DB::table('video_series_wechat_check')
            ->where([
                'series_id'=> $seriesId,
                'sync_status' => 3,
                'is_enabled' => 1,
            ])->first();

        if(!$syncInfo || !$syncInfo->media_id) {
            CommonBusinessException::throwError(Errors::SYNC_WECHAT_NOT_OK);
        }
        $mediaId = $syncInfo->media_id;
        $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);

        return HttpRequest::simplePost(WechatURL::vod_getmedialink. $accessToken, [
            'media_id' => $mediaId,
            't' => time() + 7200,
        ]);
    }

    /**
     * 短剧播放链接保存的rediskey
     * @param $seriesId
     * @return string
     */
    public static function getWechatMediaLinkRedisKey($seriesId) {
        return 'wechat.medialink.'.$seriesId;
    }

}
