<?php

namespace app\command;

use app\extra\tools\XcJob;
use app\model\star\StarTask;
use app\model\star\StarTaskVideo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StarVideo extends Command
{

    protected static string $defaultName = 'star:video';
    protected static string $defaultDescription = '星川任务';

    protected array $status = [
        3 => "已完成",
        31 => "平台审核中",
        32 => "审核未通过",
        33 => "审核未通过",
        70 => "服务商审核中",
        71 => "审核未通过",
        72 => "预热中",
        73 => "审核超时",
        74 => "客户审核中",
        75 => "审核未通过",
        76 => "审核超时",
        80 => "持续计费中",
        81 => "结果公示中",
        82 => "计费暂停",
    ];

    protected function execute(InputInterface $input, OutputInterface $output): int
    {

        try {
            $nextPage = 1;$done = 0;$subPage = 1;
            while (is_numeric($nextPage)) {
                $resp = (new StarTask)->page($nextPage,10)->select();
                if ($resp->isEmpty()) {
                    $output->writeln("没有任务啦==".getDateFull());
                    $nextPage = null;
                    return true;
                }
                $taskVideo = (new StarTaskVideo);
                foreach ($resp as $key=>$val) {
                    $output->writeln("任务ID：{$val['job_id']}==页码：{$subPage}==开始执行==" . getDateFull());
                    while (is_numeric($subPage))
                    {
                        [$state,$taskResp] = (new XcJob)->token()->getAuthorVideo($val['job_id'],$subPage);
                        if (!$state) {
                            $output->writeln("任务ID：{$val['job_id']}==没有视频啦==".getDateFull());
                            $subPage = 1;
                            break;
                        }
                        if (!$taskResp['has_more']) {
                            $output->writeln("进入下一个任务==".getDateFull());
                            $subPage = 1;
                            break;
                        }
                        foreach ($taskResp['list'] as $sk=>$sv) {
                            $userData = $taskVideo->where(['job_id' => $val['job_id'],'item_id' => $sv['base_info']['item_id']])->findOrEmpty();
                            [$vState,$videDetail] = (new XcJob)->token()->getVideoDetail($sv['base_info']['video_id']);
                            if ($userData->isEmpty()) {
                                $userData->insertGetId([
                                    "job_id"        => $val['job_id'],
                                    "author_id"     => $sv['author_info']['author_id'],
                                    "play_url"      => $vState ? $videDetail['play_url'] : '',
                                    "cover"         => $vState ? $videDetail['cover'] : '',
                                    "title"         => removeEmoji2($sv['base_info']['title']),
                                    "duration"      => $sv['base_info']['duration'],
                                    "item_id"       => $sv['base_info']['item_id'],
                                    "lego_mid"      => $sv['ad_info']['lego_mid'],
                                    "video_id"      => $sv['base_info']['video_id'],
                                    "item_status"   => $this->status[$sv['extra_info']['item_status']],
                                    "time_agg_data_10006"   => $sv['item_real_time_agg_data']['10006']??0,
                                    "time_agg_data_10010"   => $sv['item_real_time_agg_data']['10010']??0,
                                    "time_agg_data_10014"   => $sv['item_real_time_agg_data']['10014']??0,
                                    "time_agg_data_10035"   => $sv['item_real_time_agg_data']['10035']??0,
                                    "time_agg_data_10036"   => $sv['item_real_time_agg_data']['10036']??0,
                                    "time_agg_data_10057"   => $sv['item_real_time_agg_data']['10057']??0,
                                    "time_agg_data_10056"   => $sv['item_real_time_agg_data']['10056']??0,
                                    "time_agg_data_10058"   => $sv['item_real_time_agg_data']['10058']??0,
                                    "time_agg_data_10059"   => $sv['item_real_time_agg_data']['10059']??0,
                                    "time_agg_data_10060"   => $sv['item_real_time_agg_data']['10060']??0,
                                    "time_agg_data_10061"   => $sv['item_real_time_agg_data']['10061']??0,
                                    "indicator_agg_data_20021"   => $sv['bill_indicator_agg_data']['20021']??0,
                                    "indicator_agg_data_20022"   => $sv['bill_indicator_agg_data']['20022']??0,
                                    "indicator_agg_data_20020"   => $sv['bill_indicator_agg_data']['20020']??0,
                                    "indicator_agg_data_20019"   => $sv['bill_indicator_agg_data']['20019']??0,
                                    "indicator_agg_data_20016"   => $sv['bill_indicator_agg_data']['20016']??0,
                                    "indicator_agg_data_20015"   => $sv['bill_indicator_agg_data']['20015']??0,
                                    "indicator_agg_data_20011"   => $sv['bill_indicator_agg_data']['20011']??0,
                                ]);
                                $output->writeln("{$sv['base_info']['video_id']}==【{$sv['base_info']['title']}】==新增一条视频素材==".getDateFull());
                            } else {
                                $userData->save([
                                    "play_url"      => $vState ? $videDetail['play_url'] : '',
                                    "cover"         => $vState ? $videDetail['cover'] : '',
                                    "title"         => removeEmoji2($sv['base_info']['title']),
                                    "duration"      => $sv['base_info']['duration'],
                                    "item_id"       => $sv['base_info']['item_id'],
                                    "lego_mid"      => $sv['ad_info']['lego_mid'],
                                    "video_id"      => $sv['base_info']['video_id'],
                                    "item_status"   => $this->status[$sv['extra_info']['item_status']],
                                    "time_agg_data_10006"   => $sv['item_real_time_agg_data']['10006']??0,
                                    "time_agg_data_10010"   => $sv['item_real_time_agg_data']['10010']??0,
                                    "time_agg_data_10014"   => $sv['item_real_time_agg_data']['10014']??0,
                                    "time_agg_data_10035"   => $sv['item_real_time_agg_data']['10035']??0,
                                    "time_agg_data_10036"   => $sv['item_real_time_agg_data']['10036']??0,
                                    "time_agg_data_10057"   => $sv['item_real_time_agg_data']['10057']??0,
                                    "time_agg_data_10056"   => $sv['item_real_time_agg_data']['10056']??0,
                                    "time_agg_data_10058"   => $sv['item_real_time_agg_data']['10058']??0,
                                    "time_agg_data_10059"   => $sv['item_real_time_agg_data']['10059']??0,
                                    "time_agg_data_10060"   => $sv['item_real_time_agg_data']['10060']??0,
                                    "time_agg_data_10061"   => $sv['item_real_time_agg_data']['10061']??0,
                                    "indicator_agg_data_20021"   => $sv['bill_indicator_agg_data']['20021']??0,
                                    "indicator_agg_data_20022"   => $sv['bill_indicator_agg_data']['20022']??0,
                                    "indicator_agg_data_20020"   => $sv['bill_indicator_agg_data']['20020']??0,
                                    "indicator_agg_data_20019"   => $sv['bill_indicator_agg_data']['20019']??0,
                                    "indicator_agg_data_20016"   => $sv['bill_indicator_agg_data']['20016']??0,
                                    "indicator_agg_data_20015"   => $sv['bill_indicator_agg_data']['20015']??0,
                                    "indicator_agg_data_20011"   => $sv['bill_indicator_agg_data']['20011']??0,
                                ]);
                                $output->writeln("{$sv['base_info']['video_id']}===更新数据==".getDateFull());
                            }
                        }
                        $subPage = $subPage+1;
                        $output->writeln("进入下一页视频=={$nextPage}-{$subPage}==".getDateFull());
                        $output->writeln("===========================================================================");
                        sleep(2);
                    }
                }
                $nextPage = $nextPage+1;
                $subPage = 1;
                $output->writeln("进入下一页=={$nextPage}==".getDateFull());
            }
        } catch (\Throwable $throwable) {
            echo getDateFull()."==报错啦===".$throwable->getFile()."\n";
            echo getDateFull()."==报错啦===".$throwable->getLine()."\n";
            echo getDateFull()."==报错啦===".$throwable->getMessage()."\n";
        }
        return true;
    }

}