<?php

namespace app\command;

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

class MiniTask extends Command
{


    protected static string $defaultName = 'min:task';

    protected static string $defaultDescription = '小程序星川任务';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $nextPage = 1;$done = 0;
            $output->writeln("===小程序星川任务开始==={$nextPage}==>".getDateFull());
            while (is_numeric($nextPage)) {
                [$state,$resp] = (new XcJob)->token()->getTaskList($nextPage);
                if (!$state) {
                    $nextPage = null;
                    $output->writeln("===获取全部任务失败==>".getDateFull());
                    return self::SUCCESS;
                }
                if (!$resp['has_more']) {
                    $nextPage = null;
                    $output->writeln("===没有更多啦==>".getDateFull());
                    return self::SUCCESS;
                }
                $model = (new BlueTask);
                foreach ($resp['list'] as $key => $val)
                {
                    $data = (new XcJob)->token()->getTaskDetail($val['demand_info']['id']); // 获取任务详情
                    if ($data['base_resp']['status_code'] <> 0)
                    {
                        $nextPage = null;
                        $output->writeln("===获取任务详情失败==>".getDateFull());
                        return self::SUCCESS;
                    }
                    $task  = [
                        "expiration_time_end"   => date("Y-m-d H:i:s",$data['task_detail_info']['task_base_info']['expiration_time_end']),
                        "title"                 => $data['task_detail_info']['task_base_info']['demand_name']??'',
                        "product_cover_url"     => $data['task_detail_info']['object_field_info']['object_product_info']['product_cover_url']??'',
                        "product_name"          => $data['task_detail_info']['object_field_info']['object_product_info']['product_name']??'',
                        "shop_name"             => $data['task_detail_info']['object_field_info']['object_product_info']['shop_name']??'',
                        "product_id"            => $data['task_detail_info']['object_field_info']['object_product_info']['external_product_id']??'',
                        "product_link"          => $data['task_detail_info']['object_field_info']['object_product_info']['product_link']??'',
                        "product_price"         => $data['task_detail_info']['component_info']['ecom_info']['ecoms'][0]['price']?$data['task_detail_info']['component_info']['ecom_info']['ecoms'][0]['price']*100:'0',
                        "auto"                  => 2,
                        "job_id"                => $val['demand_info']['id'],
                        "category"              => "douyin",
                        "category_detail"       => $data['task_detail_info']['task_base_info']['second_class_category_name']??'',
                        "product_selling_point" => $data['task_detail_info']['object_field_info']['object_product_info']['product_selling_point_detail']??'',
                        "target_audience"       => $data['task_detail_info']['object_field_info']['object_product_info']['target_audience_desc']??'',
                        "creative_material_types"       => $data['task_detail_info']['star_qc_unite_info']['creative_material_types']?json_encode($data['task_detail_info']['star_qc_unite_info']['creative_material_types']):[],
                        "content"               => $data['task_detail_info']['task_base_info']['other']?nl2br($data['task_detail_info']['task_base_info']['other']):'',
                        "total_budget"          => $data['task_detail_info']['project_detail_info']['base_project_info']['total_budget']??0, // 0不上限
                    ];
                    $taskData = $model->where("product_id",$task['product_id'])->findOrEmpty();
                    if ($taskData->isEmpty()) { // 新增
                        $taskData->insertGetId($task);
                        $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;
        }
    }

}