RankDataLogic.php 43.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
<?php

namespace App\Http\Logic\Bside\RankData;


use App\Helper\Arr;
use App\Helper\GoogleSpeedApi;
use App\Helper\QuanqiusouApi;
use App\Helper\SemrushApi;
use App\Http\Logic\Aside\Project\DomainInfoLogic;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryRelateDomain;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\MinorLanguages;
use App\Models\Project\Project;
use App\Models\RankData\ExternalLinks;
use App\Models\RankData\ExternalLinks as ExternalLinksModel;
use App\Models\RankData\IndexedPages;
use App\Models\RankData\IndexedPages as IndexedPagesModel;
use App\Models\RankData\RankData;
use App\Models\RankData\RankDataBmseo;
use App\Models\RankData\RankDataLog;
use App\Models\RankData\RankWeek;
use App\Models\RankData\RankWeek as RankWeekModel;
use App\Models\RankData\RecommDomain;
use App\Models\RankData\RecommDomain as RecommDomainModel;
use App\Models\RankData\Speed;
use App\Models\RankData\Speed as GoogleSpeedModel;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class RankDataLogic extends BaseLogic
{

    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        if(isset($this->user['project_id']) && ($this->user['project_id'] == 5172)){
            $this->user['project_id'] = 3298;
        }
    }

    /**
     * 统计数据
     * @author zbj
     * @date 2023/5/11
     */
    public function index()
    {
        $project_id = $this->user['project_id'];
        //查数据
        $project = (new ProjectLogic())->getProjectInfo($project_id);
        if(request('api_no')){
            $api_no = request('api_no');
        }else{
            $api_no = $project['deploy_optimize']['api_no'] ?? 0;
        }
        $domain_info = (new DomainInfoLogic)->getDomainInfo($project_id);
        $rank = RankData::where('project_id', $project_id)->where('api_no', $api_no)->first();
        if(empty($rank) && ($project['deploy_optimize']['api_no'] != 0)){
            $data['langs_status'] = 1;
        }
        $rank_week = RankWeek::where('project_id', $project_id)->where('api_no', $api_no)->first();
        $recomm_domain = RecommDomain::where('project_id', $project_id)->where('api_no', $api_no)->first();
        $external_links = ExternalLinks::where('project_id', $project_id)->where('api_no', $api_no)->first();
        $indexed_pages = IndexedPages::where('project_id', $project_id)->where('api_no', $api_no)->first();
        $speed = Speed::where('project_id', $project_id)->first();
        //暂停优化的项目(排名数据显示横杠 - ,关键词排名第一页 、 关键词排名前十页 、 Google收录页面数 、 可查询外链数)
        if(!empty($project['level'])){
            if (in_array('2', $project['level'])) {// || in_array('3', $project['level'])
                $rank['first_page_num'] = $rank['first_ten_pages_num'] = $rank['indexed_pages_num'] = $external_links['total'] = '-';
            }
        }
        //排名数据
        $data = [
            'first_num' => $rank['first_num'] ?? 0,
            'first_page_num' => $rank['first_page_num'] ?? 0,
            'first_three_pages_num' => $rank['first_three_pages_num'] ?? 0,
            'first_five_pages_num' => $rank['first_five_pages_num'] ?? 0,
            'first_ten_pages_num' => $rank['first_ten_pages_num'] ?? 0,
            'indexed_pages_num' => $rank['indexed_pages_num'] ?? 0,
            'external_links_num' => $external_links['total'] ?? 0,
        ];
        $g_top_plan = $project['deploy_optimize']['g_top_plan'];
        if(!empty($g_top_plan)){
            $g_top_plan['is_compliance'] = $g_top_plan['is_compliance'] ?? 0;
            if(!isset($g_top_plan['service_day']) || empty($g_top_plan['service_day'])){
                $g_top_plan['service_day'] = 0;
            }
            $g_top_plan['day'] = $g_top_plan['service_day'] - $g_top_plan['is_compliance'];
        }
        $plan = Project::planMap();
        $seo_plan = Project::seoMap();
        //项目信息
        $data['project'] = [
            'company' => $project['company'],
            'domain' =>  $domain_info['domain'] ?? '',
            'domain_info' => $domain_info['domain_info'] ?? '',
            'cert_info' => $domain_info['cert_info'] ?? '',
            'plan' => $plan[$project['deploy_build']['plan']] ?? 0,
            'seo_plan' => $seo_plan[$project['deploy_build']['seo_plan']] ?? 0,
            'keyword_num' => $project['deploy_build']['keyword_num'],
            'compliance_day' => $project['finish_remain_day'] ?? 0,
            'remain_day' => $project['remain_day'],
            'seo_remain_day' => $project['seo_remain_day'],
            'g_top_plan' => $g_top_plan ?? [],
            'is_remain_today'=>$project_id['is_remain_today'] ?? 0,
            'bm_is_remain_today'=>$project_id['bm_is_remain_today'] ?? 0
        ];
        //小语种列表
        $quanqiusou_api = new QuanqiusouApi();
        $lang_data = $quanqiusou_api->getLangRankData($api_no);
        $lang_data = Arr::setValueToKey($lang_data, 'language');
        $data['langs'] = [];
        $languageModel = new MinorLanguages();
        $languageList = $languageModel->list(['project_id'=>$project['id'], 'is_delete' => 0]);
        if(!empty($languageList) && is_array($languageList)){
            foreach($languageList as $lang){
                if($lang['lang'] =='ja'){
                    $lang['lang'] ='jp';
                }
                if($lang['lang'] == 'ko'){
                    $lang['lang'] ='kr';
                }
                if($lang['lang'] =='ms'){
                    $lang['lang']='my';
                }
                if($lang['lang'] == 'vi'){
                    $lang['lang'] ='vn';
                }
                $remain_day = $lang_data[$lang['lang']]['dabiao_day'] ?? 0;
                $data['langs'][$lang['language'] ?? ''] = [
                    'lang'=>$lang['lang'],
                    'lang_text' => $lang['language'],
                    'keyword_num' => $lang['keywords'] ?? 0,
                    'reach_day' => $lang_data[$lang['lang']]['dabiao_day'] ?? 0,
                    'home_cnt' => $lang_data[$lang['lang']]['home_cnt'] ?? 0,
                    'remain_day' => ($lang['type']??0) == 1 ? $data['project']['remain_day'] : $lang['service_day'] - $remain_day,
                    'type' => $lang['type'] ?? 1,
                    'dabiao_day'=>$remain_day,
                    'service_day' => $lang['service_day'] ?? 0,
                ];
            }
        }
        //测速
        $data['speed'] = $speed['data'] ?? [];

        //seo基础设置
        $data['seo'] = [
            'H1,H2,H3标签',
            'TDK设置(Title, Description, Keywords)',
            'Google 收录提交',
            'Sitemap.xml Google站长地图',
            'Robots.txt文件优化',
            'Google站长工具优化设置'
        ];
        if($project['type'] == 3){
            $data['seo'] = [
                'H1,H2,H3标签',
                'TDK设置(Title, Description, Keywords)',
                'Sitemap.xml Google站长地图',
                'Robots.txt文件优化',
            ];
        }
        //外链引荐域名
        $recomm_domain = $recomm_domain ? $recomm_domain->toArray() : [];
        $recomm_domain['data'] = Collection::make($recomm_domain['data'] ?? [])->sortBy('backlinks_num')->all();
        $data['external_links_domain_chat'] = [
            'labels' => array_map(function ($item) {
                return mb_substr($item, 0, 2) . '***' . mb_substr($item, 5);
            }, Arr::pluck($recomm_domain['data'] ?? [], 'domain')),
            'data' => Arr::pluck($recomm_domain['data'] ?? [], 'backlinks_num'),
            'list_date' => $recomm_domain['updated_at'] ?? '',
        ];
        //SEO数据周期分析图 外链数
        $data['external_links_chat'] = [
            'labels' => array_keys($external_links['data'] ?? []),
            'data' => array_values($external_links['data'] ?? []),
        ];
        //SEO数据周期分析图 收录数
        $data['indexed_pages_chat'] = [
            'labels' => array_keys($indexed_pages['data'] ?? []),
            'data' => array_values($indexed_pages['data'] ?? []),
        ];
        //收录数加上当日数据
        if ((Arr::last($data['indexed_pages_chat']['labels'])) != date('Y-m-d')) {
            $data['indexed_pages_chat']['labels'][] = date('Y-m-d');
            $data['indexed_pages_chat']['data'][] = $data['indexed_pages_num'];
        }
        //关键词排名分析图
        $data['rank_chat'] = [
            'data' => $rank_week['data'] ?? [],
            'labels' => $rank_week['date'] ?? [],
        ];

        //多个api_no项目 切换api_no查看数据
        if($project_id == 2104){
            $data['other_api_no'] = $api_no ==  10690 ? 11201 : 10690;
            $data['other_api_no_source'] = $data['other_api_no'] ==  10690 ? 'Yandex' : 'Google';
            $data['current_api_no'] = $api_no;
            $data['current_api_source'] = $api_no ==  10690 ? 'Yandex' : 'Google';
        }
        $data['api_no'] = $api_no;
//        $api_no = RankDataBmseo::where('project_id', $project_id)->value('api_no');
//        if($api_no){
//            $data['bmseo_api_no'] = $api_no;
//        }

        return $data;
    }

    /**
     * 关键词排名
     * @author zbj
     * @date 2023/5/12
     */
    public function keywords_rank_list($export = false)
    {
        $page = intval($this->request['page'] ?: 1);
        $lang = $this->request['lang'] ?: '';
        $project_id = $this->user['project_id'];
        # fixme 白帽演示项目或者演示项目排名信息数据
        if ($project_id == 3989) {
            $project_id = 1;
        }
        $project = (new ProjectLogic())->getProjectInfo($project_id);
        if(request('api_no')){
            $api_no = request('api_no');
        }else{
            $api_no = $project['deploy_optimize']['api_no'] ?? 0;
        }

        if(!$api_no || Str::endsWith($api_no, '_bmseo')){
            $bm_api_no = RankDataBmseo::where('project_id', $project_id)->value('api_no');
            if($bm_api_no){
                $api_no = $bm_api_no;
            }
        }

        $domain = (!empty($project['deploy_optimize']['domain']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) :  '');
        $domain_arr = parse_url($domain);
        $domain = $domain_arr['host'] ?? $domain_arr['path'];
        //复制站点域名
        $ext_projects = $this->getExtendProjects();
        $flg_ext = $this->getExtFlag($ext_projects, $domain, $api_no);
        $ext_domain = explode(',', str_replace('www.', '', $this->getExtendProjects($api_no)['ext'] ?? ''));//关联域名
        $main_domain = str_replace('www.', '', $this->getExtendProjects($api_no)['url'] ?? '');
        //关联域名
        $relate_domain = str_replace('www.', '', InquiryRelateDomain::getRelateDomain($domain, 'globalso_domain'));
        //AI站点域名
        $ai_projects = $this->getAiProjects()['data'] ?? [];
        $flg_ai = $this->getAiFlag($ai_projects, $domain);
        $ai_domain = str_replace('www.', '', $this->getAiProjects($domain)['domain'] ?? '');
        if (Str::endsWith($api_no, '_bmseo')) {
            $list = RankDataBmseo::where('project_id', $project_id)->where('api_no', $api_no)->value('data') ?: [];
        } else {
            $list = RankData::where('project_id', $project_id)->where('api_no', $api_no)->where('lang', $lang)->value('data') ?: [];
        }

        $list30 = []; //排名前三十的
        $list30_0 = []; //排名前三十且近三天没有排名的
        $list100 = []; //排名前100的
        $list0 = [];//排名为0的
        foreach ($list as $key => $v) {
            $last = Arr::last($v);
            $data = [];
            //处理日期
            foreach ($v as $date => $position) {
                $data[date('m-d', strtotime($date . '+ 1 day'))] = $position['position'];
            }
            //域名类型
            $domain_text = '主域名:' . str_replace('www.', '', $domain);
            if (!empty($last['r'])) {
                $domain_text = 'AI域名:' . $last['r'];
                if (in_array($flg_ext, [1, 2]) || $flg_ai == 1) {
                    if ($last['r'] == $ai_domain) {
                        $domain_text = '星链域名:' . $last['r'];
                    } else if (in_array($last['r'], $ext_domain)) {
                        $domain_text = '主域名2:' . $last['r'];
                    } else if ($last['r'] == $relate_domain) {
                        $domain_text = '主域名2:' . $last['r'];
                    } else {
                        $domain_text = 'AI域名:' . $last['r'];
                    }
                }
            }
            //当前站是复制站,显示主站的域名
            if($flg_ext == 2 && empty($last['r'])){
                $domain = $main_domain;
                $domain_text = '主域名:' . $main_domain;
            }

            $domain_arr = explode(':', $domain_text);
            $v = [
                'keyword' => $key,
                'domain_type' => $domain_arr[0],
                'domain' => $this->user['project_id'] == 3989 ? 'google.com' : $domain_arr[1],      # fixme 白帽演示项目或者演示项目排名信息数据
                'domain_text' => $this->user['project_id'] == 3989 ? 'google.com' : $domain_text,   # fixme 白帽演示项目或者演示项目排名信息数据
                'g' => $last['g'],  //1核心关键词
                'position' => $data,
            ];
            //图片排名
            if(isset($last['p_img'])){
                $v['img_position'] = $last['p_img'];
            }
            //视频排名
            if(isset($last['p_vid'])){
                $v['video_position'] = $last['p_vid'];
            }

            if ($last['position'] == 0) {
                $list0[] = $v;
            } elseif ($last['position'] <= 30) {
                if (($v['position'][date('m-d', strtotime('-1day'))] ?? '') == 0 ||
                    ($v['position'][date('m-d', strtotime('-2day'))] ?? '') == 0) {
                    $list30_0[] = $v;
                } else {
                    $list30[] = $v;
                }
            } else {
                $list100[] = $v;
            }
        }
        //排序 排名前30的 按关键词长短排序 最近三天无排名的排后; 后30名的按排名排序
        $list30 = collect($list30)->sortBy(function ($item) {
            return strlen($item['keyword']);
        })->values()->all();
        $list30_0 = collect($list30_0)->sortBy(function ($item) {
            return strlen($item['keyword']);
        })->values()->all();
        $list100 = collect($list100)->sortBy(function ($item) {
            return Arr::last($item['position']);
        })->values()->all();
        $list = collect($list30)->merge($list30_0)->merge($list100)->merge($list0)->filter(function ($item) {
            //搜索
            if ($this->request['search']) {
                return strpos(strtolower($item['keyword']), strtolower($this->request['search'])) !== false;
            }
            //前几名
            if ($this->request['first']) {
                $position = Arr::last($item['position']);
                return $position > 0 && $position <= $this->request['first'];
            }
            //核心词
            if ($this->request['g']) {
                return $item['g'] == $this->request['g'];
            }
            return true;
        })->values();

        if($export){
            return $list->toArray();
        }
        $data = [
            "list" => $list->forPage($page, 100)->toArray(),
            "total" => $list->count(),
            "page" => $page,
            "total_page" => ceil($list->count() / 100),
            "size" => 100
        ];
        return $this->success($data);
    }


    /**
     * 获取复制站点项目
     * @author zbj
     * @date 2023/5/12
     */
    public function getExtendProjects($api_no = null)
    {
        $key = 'extend_projects_list';
        $data = Cache::get($key);
        if (!$data) {
            $api_url = 'http://api.quanqiusou.cn/google-rank/api/extend_projects.php';
            try {
                $data = HttpUtils::get($api_url, []);
                if ($data) {
                    $data = Arr::s2a($data);
                    Cache::put($key, $data, 4 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('复制站点项目获取失败', [], $e);
                return false;
            }
        }
        if ($api_no !== null) {
            $data = collect($data)->where('apino', $api_no)->first();
            return $data ?: [];
        }
        return $data;
    }

    /**
     * 获取白帽seo站点项目
     * @author zbj
     * @date 2023/5/12
     */
    public function getBmSeoProjects($domain = '')
    {
        $key = 'weblist_bm';
        $data = Cache::get($key);
        if (!$data) {
            $api_url = 'http://api.quanqiusou.cn/api/index/weblist_bm';
            try {
                $data = HttpUtils::get($api_url, ['key'=> '289c1fc81c89d79c04ed4fd72822948e']);
                if ($data) {
                    $data = Arr::s2a($data);
                    Cache::put($key, $data, 4 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('白帽seo站点项目获取失败', [], $e);
                return false;
            }
        }
        if ($domain) {
            return array_search($domain, $data);
        }
        return $data;
    }

    /**
     * 获取AI站点项目
     *
     * @author zbj
     * @date 2023/5/12
     */
    public function getAiProjects($domain = null)
    {
        $file_path = public_path('ai_domains.txt');
        $data = file_get_contents($file_path);
        $data = json_decode($data, true);
        if ($domain !== null) {
            $domain = parse_url($domain);
            $data = collect($data)->where('bind_domain', $domain['host'] ?? $domain['path'])->first();
            return $data ?: [];
        }
        return $data;
    }

    /**
     * 获取复制项目标识
     * @author zbj
     * @date 2023/5/15
     */
    protected function getExtFlag($ext_projects, $domain, $api_no)
    {
        //复制站点标识
        $flg_ext = 0;
        if ($ext_projects) {
            $ext_urls = array_column($ext_projects, 'ext');
            $api_nos = array_column($ext_projects, 'apino');
            if (in_array($api_no, $api_nos)) {
                $flg_ext = 1;
            }
            if (in_array($domain, $ext_urls)) {
                $flg_ext = 2;
            }
        }
        return $flg_ext;
    }

    /**
     * 获取AI项目标识
     * @author zbj
     * @date 2023/5/15
     */
    protected function getAiFlag($ai_projects, $domain)
    {
        $flg_ai = 0;
        foreach ($ai_projects as $ai_project) {
            if ($ai_project['bind_domain'] == $domain) {
                $flg_ai = 1;
            }
        }
        return $flg_ai;
    }

    /**
     * 同步排名信息
     * @throws \Exception
     * @author zbj
     * @date 2023/9/20
     */
    public function syncRankData($api_no, $site_res, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('project_id');
        //特殊处理
        if($api_no == 11201){
            $project_ids[] = 2104;
        }
        foreach ($project_ids as $project_id) {
            Log::channel('rank_data')->info('开始查项目:' . $project_id);
            $project = Project::find($project_id);
            if (!$project) {
                throw new \Exception($api_no . '关联的项目不存在');
            }
            $api = new QuanqiusouApi();
            $model = RankData::where('project_id', $project_id)->where('lang', '')->first();
            if (!$model || $model->updated_date != date('Y-m-d') || $force) {
                Log::channel('rank_data')->info('开始接口数据:' . $project_id);
                $res = $api->getGoogleRank($project_id, $api_no, '', 7, $force);
                if (!$res) {
                    throw new \Exception("接口数据获取失败,api_no:{$api_no}");
                }
                //收录数
                $indexed_pages_num = $site_res[$api_no] ?? 0;
                Log::channel('rank_data')->info('开始保存:' . $project_id);
                $this->save_rank($project_id, $api_no, $res, $indexed_pages_num);
            }
            //有小语种的
            $lang_list = $api->getLangList();
            if (!empty($lang_list[$api_no])) {
                foreach ($lang_list[$api_no] as $lang){
                    if($lang =='ja'){
                        $lang ='jp';
                    }
                    if($lang == 'ko'){
                        $lang ='kr';
                    }
                    if($lang =='ms'){
                        $lang='my';
                    }
                    if($lang == 'vi'){
                        $lang ='vn';
                    }
                    $model = RankData::where('project_id', $project_id)->where('lang', $lang)->first();
                    if (!$model || $model->updated_date != date('Y-m-d') || $force) {
                        $res = $api->getGoogleRank($project_id, $api_no, $lang, 7, $force);
                        if (!$res) {
                            throw new \Exception("接口数据获取失败,api_no:{$api_no},lang");
                        }
                        $data = [];
                        //不同的小语种取出来
                        foreach ($res as $keyword => $v) {
                            $data[Arr::last($v)['lang']][$keyword] = $v;
                        }
                        foreach ($data as $lang => $rank) {
                            $this->save_rank($project_id, $api_no, $rank, 0, $lang);
                        }
                    }
                }
            }
        }
    }


    /**
     * @param $project_id
     * @param $indexed_pages_num
     * @param $data
     * @param string $lang
     * @author zbj
     * @date 2023/5/8
     */
    public function save_rank($project_id, $api_no, $data, $indexed_pages_num = null, string $lang = ''){
        $without_project_ids = [];  //不用处理排名的项目
        $without_extension_project_ids = [658]; //是否达标只统计主词的
        $extension_project_ids = [354]; //扩展词也到达标的
        $compliance_project_ids = [2163,257,823,1750,497,1006,2663,255]; //直接达标处理的
        $ceaseProjectId = [354, 378, 649, 1226, 1283, 1703, 1893, 2066, 2250,2193,2399,1685, 3931,2273,3647,1934];//暂停的项目
        $uptimeProjectId = [1434,1812,276,2414,2974];//按上线时间统计的项目
        //一个项目多个api_no
        $multiple_api_no_project_ids = [
            2104 => [
                11201  => 50,  //api_no => 关键词达标数
                10690  => 100,
            ]
        ];
        $first_num = $first_page_num = $first_three_pages_num = $first_five_pages_num = $first_ten_pages_num = 0;
        $first_page_without_extension_num = 0; //不算扩展词在首页的数量
        $first_page_extension_num = 0; //扩展词在首页的数量
        $g_top_first_page_extension_num = 0;
        foreach ($data as &$ranks){
            ksort($ranks);
            $last = Arr::last($ranks);
            //第一名
            if($last['position'] == 1){
                $first_num ++;
            }
            //排名第一页
            if($last['position'] > 0 && $last['position'] <= 10){
                $first_page_num ++;
                $last['g'] == 1 && $first_page_without_extension_num++;
                $last['g'] == 2 && $first_page_extension_num++;
                $last['g'] == 3 && $g_top_first_page_extension_num++;
            }
            //排名前三页
            if($last['position'] > 0 && $last['position'] <= 30){
                $first_three_pages_num ++;
            }
            //排名前五页
            if($last['position'] > 0 && $last['position'] <= 50){
                $first_five_pages_num ++;
            }
            //排名前十页
            if($last['position'] > 0 && $last['position'] <= 100){
                $first_ten_pages_num ++;
            }
        }
        $where = [
            'project_id' => $project_id,
            'api_no' => $api_no,
            'lang' => $lang
        ];
        $model = RankData::where($where)->first();
        if(!$model){
            $model = new RankData();
        }
        //g-top方案达标天数
        $this->g_top_plan($project_id,$g_top_first_page_extension_num);
        //保证关键词数
        $keyword_num = DeployBuild::where('project_id', $project_id)->value('keyword_num');
        //多api_no项目的保证关键词数
        if(in_array($project_id, array_keys($multiple_api_no_project_ids))){
            $keyword_num = $multiple_api_no_project_ids[$project_id][$api_no]??0;
        }
        $type = Project::where('id', $project_id)->value('type');
        $model_is_compliance = $model->is_compliance;
        $model->is_compliance = 0;
        //是否达标
        $is_compliance = $first_page_num >= $keyword_num;
        if(in_array($project_id, $without_extension_project_ids)){
            $is_compliance = $first_page_without_extension_num >= $keyword_num;
        }
        if(in_array($project_id, $extension_project_ids)){
            $is_compliance = $first_page_extension_num >= $keyword_num;
        }
        if(in_array($project_id, $compliance_project_ids)){
            $is_compliance = 1; //直接达标处理
        }
        if ($keyword_num && $type == Project::TYPE_TWO && $is_compliance) {
            Log::channel('rank_data')->info('项目' . $project_id . ':关键词达标'. $keyword_num .' - ' . $first_page_num . ' - ' . $first_page_without_extension_num);
            //项目表更新
            if (($model->updated_date != date('Y-m-d') || empty($model_is_compliance)) && !$lang) {
                $compliance_day = Project::where(['id' => $project_id])->value('finish_remain_day') ?: 0;
                if(!in_array($project_id,$ceaseProjectId)){//不是暂停的项目
                    if($compliance_day == 0){//达标天数为0并当天达标  记录当前达标时间
                        DeployOptimize::where(['project_id'=>$project_id])->update(['first_compliance_time'=>date('Y-m-d')]);
                    }
                    if(in_array($project_id,$uptimeProjectId)){//直接按上线时间统计的项目
                        Project::where('id', $project_id)->update(['is_remain_today' => 1, 'finish_remain_day' => $compliance_day + 1]);
                    }else{
                        //多api_no项目 要api_no都分别达标才算
                        if(in_array($project_id, array_keys($multiple_api_no_project_ids))){
                            $api_nos = array_keys($multiple_api_no_project_ids[$project_id]);
                            //今天其他api_no是否都达标了
                            $count = RankData::where('project_id', $project_id)->whereIn('api_no', $api_nos)->where('api_no', '<>', $api_no)->where('updated_date', date('Y-m-d'))
                                ->where('is_compliance', 1)->count();
                            if($count == count($api_nos) - 1){
                                Project::where('id', $project_id)->update(['is_remain_today' => 1, 'finish_remain_day' => $compliance_day + 1]);
                            }
                        }else{
                            Project::where('id', $project_id)->update(['is_remain_today' => 1, 'finish_remain_day' => $compliance_day + 1]);
                        }
                        Log::channel('rank_data')->info('项目' . $project_id . '达标天数+1:' . ($compliance_day + 1));
                    }
                }else{
                    Project::where('id', $project_id)->update(['is_remain_today' => 1, 'finish_remain_day' => $compliance_day]);
                    Log::channel('rank_data')->info('项目' . $project_id . '暂停项目达标天数不加:'. ($compliance_day));
                }
            }
            $model->is_compliance = 1;
        } else {
            Log::channel('rank_data')->info('项目' . $project_id . ':关键词未达标'. $keyword_num .' - ' . $first_page_num);
        }
        $model->compliance_day = Project::where(['id' => $project_id])->value('finish_remain_day') ?: 0;
        $model->project_id = $project_id;
        $model->api_no = $api_no;
        $model->first_num = $first_num;
        $model->first_page_num = $first_page_num;
        $model->first_three_pages_num = $first_three_pages_num;
        $model->first_five_pages_num = $first_five_pages_num;
        $model->first_ten_pages_num = $first_ten_pages_num;
        if($indexed_pages_num !== null){
            $model->indexed_pages_num = $indexed_pages_num;
        }
        $model->lang = $lang;
        $model->data = $data;
        $model->updated_date = date('Y-m-d');
        $model->save();

        return $model->is_compliance;
    }

    public function save_rank_bmseo($project_id, $api_no, $data){
        $first_num = $first_page_num = $first_three_pages_num = $first_five_pages_num = $first_ten_pages_num = 0;
        $first_page_without_extension_num = 0; //不算扩展词在首页的数量
        $first_page_extension_num = 0; //扩展词在首页的数量
        foreach ($data as &$ranks){
            ksort($ranks);
            $last = Arr::last($ranks);
            //第一名
            if($last['position'] == 1){
                $first_num ++;
            }
            //排名第一页
            if($last['position'] > 0 && $last['position'] <= 10){
                $first_page_num ++;
                $last['g'] == 1 && $first_page_without_extension_num++;
                $last['g'] == 2 && $first_page_extension_num++;
            }
            //排名前三页
            if($last['position'] > 0 && $last['position'] <= 30){
                $first_three_pages_num ++;
            }
            //排名前五页
            if($last['position'] > 0 && $last['position'] <= 50){
                $first_five_pages_num ++;
            }
            //排名前十页
            if($last['position'] > 0 && $last['position'] <= 100){
                $first_ten_pages_num ++;
            }
        }

        //保证关键词数
        $keyword_num = DeployBuild::where('project_id', $project_id)->value('keyword_num');
        if($keyword_num){
            $is_compliance = $first_page_num >= $keyword_num;
        }else{
            $is_compliance = 0;
        }

        if ($keyword_num && $is_compliance) {
            Log::channel('rank_data')->info('项目' . $project_id . '白帽版:关键词达标'. $keyword_num .' - ' . $first_page_num);

            $compliance_log = RankDataLog::where('api_no', $api_no)->where('date', date('Y-m-d'))->where('is_compliance', 1)->first();
            if (!$compliance_log) {
                $compliance_day = Project::where(['id' => $project_id])->value('finish_remain_day') ?: 0;
                Project::where('id', $project_id)->update(['bm_is_remain_today' => 1, 'bm_finish_remain_day' => $compliance_day + 1]);
                Log::channel('rank_data')->info('项目' . $project_id . '白帽版:达标天数+1:' . ($compliance_day + 1));
            }
        }else {
            Log::channel('rank_data')->info('项目' . $project_id . '白帽版:关键词未达标'. $keyword_num .' - ' . $first_page_num);
        }

        $where = [
            'project_id' => $project_id,
            'api_no' => $api_no,
        ];
        $model = RankDataBmseo::where($where)->first();
        if(!$model){
            $model = new RankDataBmseo();
        }
        $model->project_id = $project_id;
        $model->api_no = $api_no;
        $model->first_num = $first_num;
        $model->first_page_num = $first_page_num;
        $model->first_three_pages_num = $first_three_pages_num;
        $model->first_five_pages_num = $first_five_pages_num;
        $model->first_ten_pages_num = $first_ten_pages_num;
        $model->data = $data;
        $model->updated_date = date('Y-m-d');
        $model->save();

        return $is_compliance;
    }

    /**
     * @remark :g_top
     * @name   :g_top_plan
     * @author :lyh
     * @method :post
     * @time   :2024/5/15 14:25
     */
    public function g_top_plan($project_id,$first_page_num){
        $optimizeModel = new DeployOptimize();
        $info = $optimizeModel->read(['project_id'=>$project_id]);
        if(!empty($info) && !empty($info['g_top_plan'])){
            $gTopData = $info['g_top_plan'];
            $keyword_num = $gTopData['keyword_num'] ?? 0;
            $is_compliance = ($first_page_num >= $keyword_num);
            if($is_compliance && $keyword_num){
                if(!isset($gTopData['is_compliance'])){
                    $gTopData['is_compliance'] = 0;
                }
                $gTopData['is_compliance'] = (int)$gTopData['is_compliance'] + 1;
            }
            $optimizeModel->edit(['g_top_plan'=>json_encode($gTopData)],['id'=>$info['id']]);
        }
    }


    /**
     * 同步外链
     * @throws \Exception|GuzzleException
     * @author zbj
     * @date 2023/9/20
     */
    public function syncExternalLinks($api_no, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('domain', 'project_id');
        //特殊处理
        if($api_no == 11201){
            $project_ids[2104] = 'www.xabcbiology.ru';
        }
        foreach ($project_ids as $project_id => $domain) {
            if (!$domain) {
                Log::channel('rank_data')->error('syncExternalLinks:未配置正式域名', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }

            $model = ExternalLinksModel::where('project_id', $project_id)->where('api_no', $api_no)->first();
            if ($model && $model->updated_date >= getThisWeekStarDate() && !$force) {
                //continue;
            }
            if (!$model) {
                $model = new ExternalLinksModel();
            }

            //5.0
            try {
//                $data = HttpUtils::get("https://www.quanqiusou.cn/semrush-api/data_json/{$api_no}.json", []);
                $client = new \GuzzleHttp\Client();
                $data = $client->request('GET', "https://www.quanqiusou.cn/semrush-api/data_json/{$api_no}.json", [
                    'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
                ])->getBody()->getContents();
                $data = Arr::s2a($data);
            }catch (\Exception $e){
                $data = [];
            }
            if(!$data){
                //外链数据
                $domain =  (new DomainInfo())->getDomain($domain);
                $semrushApi = new SemrushApi();
                $res = $semrushApi->backlinks_overview($domain);
                if (!$res) {
                    Log::channel('rank_data')->error('syncExternalLinks:外链数据为空', ['project_id' => $project_id, 'api_no' => $api_no]);
                    continue;
                }

                $data = $this->_data($project_id, $res['total']);
            }
            $model->project_id = $project_id;
            $model->api_no = $api_no;
            $model->total = $data['total'];
            $model->data = $data['data'];
            $model->updated_date = date('Y-m-d');
            $model->save();
        }
    }

    /**
     * 构造外链chat数据
     * @param $project_id
     * @param $total
     * @return array|mixed
     * @author zbj
     * @date 2023/5/10
     */
    public function _data($project_id, $total)
    {
        // //外链数
        $data['total'] = intval($total);
        $model = ExternalLinksModel::where('project_id', $project_id)->first();
        if ($model) {
            //特殊处理的外链数
//            $data['total'] = ($total > $model->total) ? intval($total) : $model->total; //特殊处理的

            //chat数据
            $chat_data = Arr::s2a($model['data']);
            if (empty($chat_data[date('Y-m-d')])) {
                array_shift($chat_data);
            }
        } else {
            //chat数据
            for ($i = 1; $i < 12; $i++) {
                $date = date("Y-m-d", strtotime(-7 * $i . 'days'));
                $chat_data[$date] = 0;  //伪造ceil($total - ($total * rand(5, 10) / 100));
            }
        }
        $chat_data[date('Y-m-d')] = $data['total'];
        $data['data'] = $chat_data;
        return $data;
    }

    /**
     * 同步引荐域名数据
     * @throws \Exception|GuzzleException
     * @author zbj
     * @date 2023/9/20
     */
    public function syncRecommDomain($api_no, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('domain', 'project_id');
        //特殊处理
        if($api_no == 11201){
            $project_ids[2104] = 'www.xabcbiology.ru';
        }
        foreach ($project_ids as $project_id => $domain) {
            if (!$domain) {
                Log::channel('rank_data')->error('syncRecommDomain:未配置正式域名', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }
            $model = RecommDomainModel::where('project_id', $project_id)->where('api_no', $api_no)->first();
            if ($model && $model->updated_date >= getThisWeekStarDate() && !$force) {
                continue;
            }
            if (!$model) {
                $model = new RecommDomainModel();
            }

            //5.0
            try {
//                $data = HttpUtils::get("https://www.quanqiusou.cn/semrush-api/data_json/{$api_no}.json", []);
                $client = new \GuzzleHttp\Client();
                $data = $client->request('GET', "https://www.quanqiusou.cn/semrush-api/data_json/{$api_no}.json", [
                    'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
                ])->getBody()->getContents();
                $data = Arr::s2a($data)['list'];
            }catch (\Exception $e){
                $data = [];
            }

            if(!$data){
                //外链引荐域名
                $domain =  (new DomainInfo())->getDomain($domain);
                $semrushApi = new SemrushApi();
                $data = $semrushApi->backlinks_refdomains($domain);
            }

            if (!$data) {
                Log::channel('rank_data')->error('syncRecommDomain:引荐域名数据为空', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }
            $model->project_id = $project_id;
            $model->api_no = $api_no;
            $model->data = $data;
            $model->updated_date = date('Y-m-d');
            $model->save();
        }
    }

    /**
     * 收录数
     * @author zbj
     * @date 2024/1/9
     */
    public function syncIndexedPages($api_no, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('domain', 'project_id');
        //特殊处理
        if($api_no == 11201){
            $project_ids[2104] = 'www.xabcbiology.ru';
        }
        foreach ($project_ids as $project_id => $domain) {
            $model = IndexedPagesModel::where('project_id', $project_id)->where('api_no', $api_no)->first();
            if($model && $model->updated_date >= getThisWeekStarDate() && !$force){
                continue;
            }

            if(!$model){
                $model = new IndexedPagesModel();
            }
            $is_ext = 0;
            $copy_domain = '';
            $api = new QuanqiusouApi();
            //复制站  域名在这个http://api.quanqiusou.cn/google-rank/ext_sitenum_list.php里面 就用这个接口的数据
            $domain =  (new DomainInfo())->getDomain($domain);
            $host = parse_url($domain, PHP_URL_HOST);
            $host=str_replace('www.', '', $host);
            $copy_site_list = $api->getSiteNumList();
            if(in_array($host, $copy_site_list)){
                $copy_domain = $host;
                $is_ext = 1;
            }
            $res = $api->getSiteResPer($api_no, $is_ext, $copy_domain);
            if(!$res){
                Log::channel('rank_data')->error('syncIndexedPages:收录数数据为空', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }
            $model->project_id = $project_id;
            $model->api_no = $api_no;
            $model->data = $res['data'];
            $model->updated_date = date('Y-m-d');
            $model->save();
        }
    }

    /**
     * 测速
     * @param $api_no
     * @param false $force
     * @author zbj
     * @date 2024/1/9
     */
    public function syncSpeed($api_no, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('domain', 'project_id');
        foreach ($project_ids as $project_id => $domain) {
            $model = GoogleSpeedModel::where('project_id', $project_id)->first();
            if ($model && $model->updated_date >= getThisWeekStarDate() && !$force) {
                continue;
            }
            $domain =  (new DomainInfo())->getDomain($domain);
            $googleSpeedApi = new GoogleSpeedApi();
            $res = $googleSpeedApi->run($domain);
            if (!$res) {
                Log::channel('rank_data')->error('syncSpeed:测速数据为空', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }
            if (!$model) {
                $model = new GoogleSpeedModel;
            }
            $model->project_id = $project_id;
            $model->data = $res;
            $model->updated_date = date('Y-m-d');
            $model->save();
        }

    }

    /**
     * 每周排名数据
     * @param $api_no
     * @param false $force
     * @author zbj
     * @date 2024/1/9
     */
    public function syncRankWeek($api_no, $force=false){
        $project_ids = DeployOptimize::where('api_no', $api_no)->pluck('domain', 'project_id');
        //特殊处理
        if($api_no == 11201){
            $project_ids[2104] = 'www.xabcbiology.ru';
        }
        foreach ($project_ids as $project_id => $domain) {
            $rank_week = RankWeekModel::where('project_id', $project_id)->where('api_no', $api_no)->first();
            if ($rank_week && $rank_week->updated_date >= getThisWeekStarDate() && !$force) {
                //本周数据已更新
                continue;
            }
            $api = new QuanqiusouApi();
            $res = $api->getGoogleRankWeek();
            if (!$res) {
                Log::channel('rank_data')->error('syncRankWeek:每周排名数据为空', ['project_id' => $project_id, 'api_no' => $api_no]);
                continue;
            }

            if (!$rank_week) {
                $rank_week = new RankWeekModel();
            }
            $rank_week->project_id = $project_id;
            $rank_week->api_no = $api_no;
            $rank_week->data = $res['data'][$api_no] ?? [];
            $rank_week->date = $res['date'];
            $rank_week->updated_date = date('Y-m-d');
            $rank_week->save();
        }

    }

}