<?php

namespace app\command;

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

class StarAuthor extends Command
{

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

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $nextPage = 1;$done = 0;$subPage = 1;
            while (is_numeric($nextPage))
            {
                $output->writeln("开始位置：{$nextPage}==".getDateFull());
                $resp = (new StarTask)->page($nextPage,10)->select();
                if ($resp->isEmpty()) {
                    $output->writeln("没有任务啦==".getDateFull());
                    $nextPage = null;
                    return true;
                }
                $taskUser = (new StarTaskUser);
                foreach ($resp as $key=>$val) {
                    $output->writeln("任务ID：{$val['job_id']}==页码：{$subPage}==开始执行==".getDateFull());
                    while (is_numeric($subPage))
                    {
                        [$state,$taskResp] = (new XcJob)->token()->getTaskAuthor($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 = $taskUser->where(['job_id' => $val['job_id'],'author_id' => $sv['author_base_info']['author_id']])->findOrEmpty();
                            if ($userData->isEmpty()) {
                                $userData->insertGetId([
                                    "job_id"            => $val['job_id'],
                                    'nick_name'         => removeEmoji2($sv['author_base_info']['nick_name']),
                                    'show_nick_name'    => emojiToUnicode($sv['author_base_info']['nick_name']),
                                    "avatar_uri"        => $sv['author_base_info']['avatar_uri'],
                                    "author_id"         => $sv['author_base_info']['author_id'],
                                    "sample_address_str"    => $sv['author_base_info']['sample_address_str']??'',
                                    "follower"    => $sv['author_base_info']['follower'],
                                    "gender"    => $sv['author_base_info']['gender'],
                                    "sec_uid"    => $sv['author_base_info']['sec_uid'],
                                    "wechat"    => $sv['author_base_info']['wechat']??'',
                                    "author_level"    => $sv['author_base_info']['ecom_author_level']??'L0',
                                    "author_order_status"    => $sv['author_order_info']['author_order_status'],
                                    "invitation_status"    => $sv['author_invitaion_info']['invitation_status'],
                                    "metric_data_num"    => $sv['author_select_metric_data']['offline_metric_data']['6001']??0,
                                    "metric_data_rate"    => $sv['author_select_metric_data']['offline_metric_data']['6002']??0,
                                    "release_item_count"    => $sv['author_order_info']['release_item_count'],
                                    "visible_item_count"    => $sv['author_order_info']['visible_item_count'],
                                ]);
                                echo "任务ID：{$val['job_id']}，星图ID：{$sv['author_base_info']['author_id']}，昵称：".removeEmoji2($sv['author_base_info']['nick_name'])."===".emojiToUnicode($sv['author_base_info']['nick_name'])."===入库成功!~==>".getDateFull()."\n";
                            } else {
                                $userData->save([
                                    'nick_name'         => removeEmoji2($sv['author_base_info']['nick_name']),
                                    'show_nick_name'    => emojiToUnicode($sv['author_base_info']['nick_name']),
                                    "avatar_uri"        => $sv['author_base_info']['avatar_uri'],
                                    "follower"    => $sv['author_base_info']['follower'],
                                    "gender"    => $sv['author_base_info']['gender'],
                                    "wechat"    => $sv['author_base_info']['wechat']??'',
                                    "author_level"    => $sv['author_base_info']['ecom_author_level']??'L0',
                                    "author_order_status"    => $sv['author_order_info']['author_order_status'],
                                    "invitation_status"    => $sv['author_invitaion_info']['invitation_status'],
                                    "metric_data_num"    => $sv['author_select_metric_data']['offline_metric_data']['6001']??0,
                                    "metric_data_rate"    => $sv['author_select_metric_data']['offline_metric_data']['6002']??0,
                                    "release_item_count"    => $sv['author_order_info']['release_item_count'],
                                    "visible_item_count"    => $sv['author_order_info']['visible_item_count'],
                                ]);
                                echo "任务ID：{$val['job_id']}，星图ID：{$sv['author_base_info']['author_id']}===更新一下!~==>".getDateFull()."\n";
                            }
                        }
                        $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;
    }

}