<?php

namespace app\command;

use app\model\bluev\BluevOceanAccount;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use think\facade\Db;
use yzh52521\EasyHttp\Http;

class OceanStat extends Command
{


    protected static string $defaultName = 'ocean:stat';
    protected static string $defaultDescription = '方舟账户消耗采集';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('day', InputArgument::OPTIONAL, 'Name description');
    }


    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $nextPage = 1;$done = 0;
            $day = $input->getArgument("day");
            $day = $day?:date("Y-m-d",strtotime("-1 day"));
            while (is_numeric($nextPage)) {
                $url = "https://agent.oceanengine.com/agent/settlement/adv/cost?costAggType=DAY_AGG&endDate={$day}&page={$nextPage}&size=10&startDate={$day}";
                $resp = Http::withHeaders([
                    'cookie' => '_ga_L06HKX6C3X=GS2.1.s1750734931$o1$g1$t1750734994$j60$l0$h0; Hm_lvt_df640d0b13edcfb2bad5f8d5e951c90e=1761717274,1762762609,1763134384; _ga=GA1.2.1350795548.1745830796; _ga_9VSTZJB1H2=GS2.2.s1763190382$o13$g0$t1763190382$j60$l0$h0; _ga_313DD262YW=GS2.1.s1763190382$o13$g0$t1763190385$j57$l0$h0; sso_auth_status=1fbba44876944012c9625e6b6d1b3f5a; sso_auth_status_ss=1fbba44876944012c9625e6b6d1b3f5a; __security_mc_61_s_sdk_crypt_sdk=86ad1c37-4965-a529; __security_mc_61_s_sdk_cert_key=9294d5dc-47bc-ba90; __security_mc_61_s_sdk_sign_data_key_web_protect=f62b5070-42a1-9716; bd_ticket_guard_client_web_domain=2; is_staff_user=false; has_biz_token=false; passport_csrf_token=7fdfe278cf7bccac93138bd4b97ddae0; passport_csrf_token_default=7fdfe278cf7bccac93138bd4b97ddae0; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCTW92WEJpeEFnMUhIdjdXdmwxczgxKzcySzZ4U3Y2TFJDakpTMFFUazJFdjJqMHlLVldkVVVEeGNBUkNqSWxhVzk4TnBoM1V0Mk1IbDg3QUtBM2tTQWc9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D; toutiao_sso_user=e96d800af768826f82338998195e493f; toutiao_sso_user_ss=e96d800af768826f82338998195e493f; __security_mc_61_s_sdk_sign_data_key_sso=7531c1b2-4d11-a0ba; sid_guard=7b4db9ff0484173cc41bfb641a5b032a%7C1782196143%7C5184002%7CSat%2C+22-Aug-2026+06%3A29%3A05+GMT; uid_tt=e1be2697fa95a52984a5d96eaa5ae4d4; uid_tt_ss=e1be2697fa95a52984a5d96eaa5ae4d4; sid_tt=7b4db9ff0484173cc41bfb641a5b032a; sessionid=7b4db9ff0484173cc41bfb641a5b032a; sessionid_ss=7b4db9ff0484173cc41bfb641a5b032a; session_tlb_tag=sttt%7C6%7Ce025_wSEFzzEG_tkGlsDKv_________G6gDZXIm9GeT9gTbmz2RoB-sifsP4huhmcLUXH3JRHq0%3D; sid_ucp_v1=1.0.0-KGEzMmVlNmFkYTNkNTI1ODIwYjMwNjdlN2VmYjViNTk5OGQyNDAzODEKHAjOqoDpmYyDAhCvz-jRBhjYjzkgDDgBQOsHSAQaAmhsIiA3YjRkYjlmZjA0ODQxNzNjYzQxYmZiNjQxYTViMDMyYQ; ssid_ucp_v1=1.0.0-KGEzMmVlNmFkYTNkNTI1ODIwYjMwNjdlN2VmYjViNTk5OGQyNDAzODEKHAjOqoDpmYyDAhCvz-jRBhjYjzkgDDgBQOsHSAQaAmhsIiA3YjRkYjlmZjA0ODQxNzNjYzQxYmZiNjQxYTViMDMyYQ; d_ticket=4d153a6a0c6285f7365053aef5ae1704edc6e; n_mh=-EiDw5WGYpykDSWMNMiX-xbTcqH_mmqPh8dnfswDW2A; sso_uid_tt=d191440a23332cd2ab01795a3ddea682; sso_uid_tt_ss=d191440a23332cd2ab01795a3ddea682; ttwid=1%7CqmgRehFzGMzoyRENPmA3cbk6oagTIHrZaG54lkG71ww%7C1782456453%7C9083b2486cbb1c3703bfeec4ace5cd09fd8be53e1f4ebdb46722597515ec2a19; passport_mfa_token=Cjdx32aYwlL8qaKwp7z1iWtYLgc%2FJK57drakwOUbtJ43mZNAp5ujGUimrd7B%2FD%2FSQRtZIlJ7685oGkoKPAAAAAAAAAAAAABQlqoZrIl9jyF%2FQHS4PCrhTnWG0cmKP4ElUQ6gtwZv%2B83xWQ2QZfovKk0FztxEkkqATxCemZUOGPax0WwgAiIBA5Omieg%3D; passport_auth_status=8cb6945c85280567d52c958864366c67%2C6875220d48bf55d3d10c195adfe5c8db; passport_auth_status_ss=8cb6945c85280567d52c958864366c67%2C6875220d48bf55d3d10c195adfe5c8db; odin_tt=4a749d90406577490b3b737823b2bf2bb8b646c5376b77231d30d5b690120497649820dabfd88841c19aeee3fa199ee10fa67573a6372e554cb806b13d51631a; sid_ucp_sso_v1=1.0.0-KDcxNjg1NjE5ZjY3ODM2MjUzNTc4Yjg0ZDJiNjMyM2NkYmZiYjQ1YTMKIQiUv5Cvv8yyBxCUwfjRBhimDCAMMOO8_akGOAJA8QdIBhoCbGYiIGU5NmQ4MDBhZjc2ODgyNmY4MjMzODk5ODE5NWU0OTNm; ssid_ucp_sso_v1=1.0.0-KDcxNjg1NjE5ZjY3ODM2MjUzNTc4Yjg0ZDJiNjMyM2NkYmZiYjQ1YTMKIQiUv5Cvv8yyBxCUwfjRBhimDCAMMOO8_akGOAJA8QdIBhoCbGYiIGU5NmQ4MDBhZjc2ODgyNmY4MjMzODk5ODE5NWU0OTNm'
                ])->get($url)->array();
                if (!isset($resp['code']) || $resp['code'] <> 0)
                {
                    echo getDateFull()."===获取数据失败\n";
                    return self::SUCCESS;
                }
                $total = $resp['data']['total'];
                if ($done >= $total){
                    if ($day == "2025-01-01") { // 终止
                        $nextPage = null; // 重新开始
                        $output->writeln('全部结束了!~==>'.getDateFull());
                        return self::SUCCESS;
                    }
                    $output->writeln('休息2秒后进入下一天!~==>'.$day);
                    sleep(2);
                    $day = date("Y-m-d",strtotime("-1 day",strtotime($day." 00:00:00")));
                    $nextPage = 1; // 重新开始
                    $done = 0;
                    $output->writeln('开始下一天!~==>'.$day);
                } else {
                    $model = (new BluevOceanAccount);
                    foreach ($resp['data']['data'] as $key => $val)
                    {
                        ++$done;
                        $respData = $model->where(['day' => $day,'advertiser_id' => $val['advId']])->findOrEmpty();
                        if ($respData->isEmpty()) {
                            $respData->insertGetId([
                                "day"               => $day,
                                "advertiser_id"     => $val['advId'],
                                "advertiser_name"   => $val['advName'],
                                "company_id"        => $val['companyId'],
                                "company_name"      => $val['companyName'],
                                "registerTime"      => $val['registerTime'],
                                "platform"          => $val['platform'], // 0 AD 4 千川 8 // 本地推
                                "noGrantCost"       => $val['noGrantCostLong'] // 除以 10000
                            ]);
                            $output->writeln("{$val['advId']}==写入成功===>".$day);
                        } else {
                            $respData->inc("noGrantCost",$val['noGrantCostLong'])->save();
                            $output->writeln("{$val['advId']}==更新成功===>".$day);
                        }
                    }
                    $nextPage = $nextPage+1;
                    $output->writeln("{$day}===当前页码{$nextPage}==>".$day);
                }
            }
            return self::SUCCESS;
        } catch (\Throwable $throwable) {
            echo getDateFull()."==报错啦===".$throwable->getFile()."\n";
            echo getDateFull()."==报错啦===".$throwable->getLine()."\n";
            echo getDateFull()."==报错啦===".$throwable->getMessage()."\n";
            return self::SUCCESS;
        }
    }

}