<?php

namespace app\command;

use app\extra\tools\XcJob;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StarTask extends Command
{


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

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $nextPage = 1;$done = 0;
            while (is_numeric($nextPage)) {
                [$state,$resp] = (new XcJob)->token()->getTaskList($nextPage);
                if (!$state) {
                    $nextPage = null;
                    return self::SUCCESS;
                }
                if (!$resp['has_more']) {
                    $nextPage = null;
                    return self::SUCCESS;
                }
                $model = (new \app\model\star\StarTask);
                foreach ($resp['list'] as $key => $val)
                {
                    $task = $model->where("job_id",$val['demand_info']['id'])->findOrEmpty();
                    if ($task->isEmpty()) { // 新增
                        $task->insertGetId([
                            "job_id"        => $val['demand_info']['id'],
                            "project_id"    => $val['project_info']['project_id'],
                            "project_name"    => $val['project_info']['project_name'],
                            "shop_name"    => $val['project_info']['shop_name'],
                            "product_name"    => $val['project_info']['product_name'],
                            "cover"    => $val['demand_info']['qc_ecom_product_info']['cover']??'',
                            "detail_url"    => $val['demand_info']['qc_ecom_product_info']['detail_url']??'',
                            "product_id"    => $val['demand_info']['qc_ecom_product_info']['product_id']??'',
                            "expiration_time"    => $val['challenge_info']['expiration_time'],
                            "expiration_time_end"    => $val['challenge_info']['expiration_time_end'],
                        ]);
                        $output->writeln("{$val['demand_info']['id']}===入库成功!~==>".getDateFull());
                    } else {
                        // 更新
                        $output->writeln("{$val['demand_info']['id']}===已存在，不入库!~==>".getDateFull());
                    }
                }
                $nextPage = $nextPage+1;
                $output->writeln("===当前页码{$nextPage}==>".getDateFull());
            }
            return self::SUCCESS;
        } catch (\Throwable $throwable) {
            echo getDateFull()."==报错啦===".$throwable->getFile()."\n";
            echo getDateFull()."==报错啦===".$throwable->getLine()."\n";
            echo getDateFull()."==报错啦===".$throwable->getMessage()."\n";
            return self::SUCCESS;
        }
    }

}