<?php

namespace App\Console\Commands;

use App\Distributor\Repositories\SmmPost;
use App\Distributor\Repositories\SmmPostLog;
use App\Distributor\Repositories\SmmUserAccount;
use App\Libraries\CommonHelper;
use App\Services\SmmService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\DomCrawler\Crawler;


/**
 * 定时任务:发送社媒帖子
 * php artisan timer:ssmPost
 */
class TimerSsmPost extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'timer:ssmPost';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送社媒帖子';


    public function handle()
    {
        // 发送社媒帖子
        $waitPost = SmmPost::getWaitPost();
        foreach ($waitPost as $post) {
            //插入post_logs表
            $accountIds = explode(',', $post->account_ids);
            $accounts = SmmUserAccount::getAccountsByIds($accountIds);
            foreach ($accounts as $account) {
                //生成post_logs表数据
                $data = [
                    'post_id' => $post->id,
                    'account_id' => $account->id,
                    'account_name' => $account->name,
                    'status' => 0,
                    'remark' => '',
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                    'dist_id' => $account->dist_id,
                    'media_name' => $account->getParent->name,
                    'response_ids'=> '',
                    'send_time' => $post->send_time,
                ];
                SmmPostLog::createLog($data);
            }
            $post->status = 1;
            $post->save();
        }

        //dd('middle');

        //发送社媒帖子开始
        $sendLog = SmmPostLog::getSendLog();
        foreach ($sendLog as $log) {
            //获取帖子内容
            $post = SmmPost::getPostById($log->post_id);
            $message = $post->message;
            $imageVideoUrl = $post->image_video_url;
            $mediaName = $log->media_name;
            $postType = $post->post_type;
            $accountInfo = SmmUserAccount::getAccountById($log->account_id);
            $accessToken = $accountInfo->access_token;
            //发送帖子
            $configData = ['accountInfo' => $accountInfo->toArray()];
            $ssmService = new SmmService($mediaName,$configData);
            if ($postType == 0) {
                //图片帖子
                $imageVideoUrl = explode(',', $imageVideoUrl);
                foreach ($imageVideoUrl as $key => $url) {
                    $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
                }
                $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
            } else {
                $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
                $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);

//                $response = $ssmService->postVideo([
//                    'title' => 'Test Video',
//                    'description' => 'This is a test video111',
//                    'categoryId' => '22', // People & Blogs
//                    'privacyStatus' => 'private'
//                ], '/mnt/hgfs/wwwroot/mtb_dcatadmin_plus/1.mp4', $accessToken);

            }

            //更新post_logs表
            if ($response['status'] == true) {
                $log->status = 1;
                $log->response_ids = json_encode($response['data']['responseIds']);
                $log->updated_at = Carbon::now();
                $log->send_time = Carbon::now();
                $log->save();
            } else {
                $log->status = 2;
                $log->remark = $response['data'];
                $log->updated_at = Carbon::now();
                $log->send_time = Carbon::now();
                $log->save();
            }
        }
        dd('发送社媒帖子完成');
    }

}