UpdateKeyword.php 5.0 KB
<?php
/**
 * @remark :
 * @name   :UpdateKeyword.php
 * @author :lyh
 * @method :post
 * @time   :2024/7/3 9:23
 */

namespace App\Console\Commands\Update;

use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordPage;
use App\Models\Product\Product;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdateKeyword extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_keyword_content';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量更新关键词内容';


    public function handle(){
        while (true){
            $keywordPageModel = new KeywordPage();
            $lists = $keywordPageModel->list(['status'=>0]);
            if(empty($lists)){
                sleep(100);
                continue;
            }
            $domainModel = new DomainInfo();
            foreach ($lists as $v){
                ProjectServer::useProject($v['project_id']);
                $this->saveKeywordContent($v);
                DB::disconnect('custom_mysql');
                //获取当前项目的域名
                $domainInfo = $domainModel->read(['project_id'=>$v['project_id']]);
                if($domainInfo !== false){
                    $this->curlDelRoute($domainInfo['domain'],$v['project_id']);
                }
                $keywordPageModel->edit(['status'=>1],['id'=>$v['id']]);
            }
            sleep(10);
        }

    }

    /**
     * @remark :更新关键词内容
     * @name   :saveKeywordContent
     * @author :lyh
     * @method :post
     * @time   :2024/7/3 10:25
     */
    public function saveKeywordContent($info){
        $keywordModel = new Keyword();
        $updateObject = json_decode($info['update_object'],true);
        $text = json_decode($info['text'],true);
        if(empty($text)){
            return false;
        }
        $number = count($text);
        if($updateObject['type'] == 0){//更新所有关键字
            //获取所有关键字的id
            $idArr = $keywordModel->selectField(['id'=>['>',0]],'id');
            if(empty($idArr)){
                return true;
            }
            if($info['update_method'] != 1){
                $idArr = shuffle($idArr);
            }
            $result = $this->splitArrayIntoParts($idArr,$number);
            foreach ($result as $key => $val){
                $keywordModel->edit(['keyword_content'=>$text[$key]],['id'=>['in',$val]]);
            }
        }else{
            //按传递的关键字更新
            if(!empty($updateObject['keyword'])){
                $updateObject['keyword'] = (array)$updateObject['keyword'];
                foreach ($updateObject['keyword'] as $key => $item){
                    if($info['update_method'] != 1){
                        $randomNumber = rand(0, $number - 1);
                    }else{
                        $randomNumber = $text[$key] ?? rand(0, $number - 1);
                    }
                    $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>$item]);
                }
            }
            //按给定的数量更新
            if(!empty($updateObject['number']) && ($updateObject['number'] != 0)){
                $keywordIdArr = $keywordModel->where("status",1)->inRandomOrder()->take($updateObject['number'])->pluck('id')->toArray();
                foreach ($keywordIdArr as $key => $item){
                    if($info['update_method'] != 1){
                        $randomNumber = rand(0, $number - 1);
                    }else{
                        $randomNumber = $text[$key] ?? rand(0, $number - 1);
                    }
                    $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>$item]);
                }
            }
        }
        return true;
    }

    /**
     * @remark :删除路由通知C端
     * @name   :curlDelRoute
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 14:43
     */
    public function curlDelRoute($domain,$project_id){
        if (strpos($domain, 'https://') === false) {
            $domain = 'https://' . $domain . '/';
        }
        $url = $domain.'api/update_page/?project_id='.$project_id.'&route=4';
        shell_exec('curl -k "'.$url.'"');
        return true;
    }


    public function splitArrayIntoParts(array $data, int $parts): array
    {
        $count = count($data);
        // 每份的最小长度(向下取整)
        $minSize = intdiv($count, $parts);
        // 余数(说明有一些数组项要平均分配给前面的几份)
        $remainder = $count % $parts;
        $result = [];
        $start = 0;
        for ($i = 0; $i < $parts; $i++) {
            $size = $minSize + ($i < $remainder ? 1 : 0);
            $result[] = array_slice($data, $start, $size);
            $start += $size;
        }
        return $result;
    }
}