lyhDemo.php 11.8 KB
<?php
/**
 * @remark :
 * @name   :lyhDemo.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/24 9:38
 */

namespace App\Console\Commands\LyhTest;

use App\Models\Ai\AiBlog;
use App\Models\Product\Category;
use App\Models\Project\AggregateKeywordAffix;
use App\Models\Project\AiBlogTask;
use App\Models\Project\KeywordPrefix;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Models\Project\ProjectWhiteHatAffix;
use App\Models\Template\BTemplateMain;
use App\Models\Template\TemplateTypeMain;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新路由';

    public function handle(){
        return $this->aggregate_keyword_affix();
    }

    /**
     * @remark :获取数据
     * @name   :aggregate_keyword_affix
     * @author :lyh
     * @method :post
     * @time   :2025/6/26 17:58
     */
    public function aggregate_keyword_affix(){
        $projectModel = new Project();
        $lists = $projectModel->list(['delete_status' => 0,'project_type'=>0,'extend_type'=>0,'type'=>['in',[1,2,4,6]]], 'id', ['id']);
        $keywordAffixModel = new AggregateKeywordAffix();
        foreach ($lists as $val){
            $info = $keywordAffixModel->read(['project_id'=>$val['id']]);
            $prefix = "How To
Why
Top 5
Top 3
Best Way To
Methods To
Top-Rated
How To find
why choose
China Top
Best Way To Choose
Methods To choose
10 tips
Top 10
How To Select
How To Find The Best
How To Evaluate
How To Compare
How To Identify
How To Source
How To Pick The Right
How To Decide Between
Why Opt For
Why Invest In
Why Consider
Why Trust
Why Professionals Prefer
Top Picks For
Best Practices For
Best Ways To
Best Options For
Best Brands For
Best Methods To
Best Strategies For
Effective Ways To
Proven Methods To
Simple Ways To
Key Methods For
Smart Ways To
Practical Methods To
Step-by-Step Guide To
10 Essential Tips For
7 Key Tips To
Ultimate Guide To
Expert Tips For
Must-Know Tips For
Quick Tips To
Insider Tips For
Leading Chinese
Best Chinese
China’s Best-Selling
Why China’s
How Chinese Manufacturers
Exploring
Uncovering
Discovering
Pinpointing
Decoding
Reasons to Choose
Advantages Explained
Where the Value Lies
Not to Be Missed
Must-Choose Reasons
Excellence in
Cutting-Edge
Premier
Champions of
The Ultimate Guide to
Practical Strategies for
Making Smart Choices
Key Steps to Choosing
Finding Your Perfect Match
Avoiding Pitfalls
Tips & Tricks for
In-Depth Analysis of Methods
How-To Guide
Step-by-Step Guide
The Secret to
Must-See List
Curated Selection of
Trending
Popular
Don't Miss These
Comprehensive Comparison of
The Clear Choice of
Today's Choice of
Expert Choice of";
            $suffix = "Manufacturer
Supplier
Application
Products
Service
Factory
Is The Best
Stands Out
Dominates
in 2025
Industry Leaders
Industry Giant
Market Leader
For the Current Year
Ahead of the Curve
Trusted by Pros
Pioneers in the Field
Now Trending
Sets the Industry Standard
Leads the Global Market
Delivers Unmatched Quality
Guarantees Peak Performance
Exceeds Industry Benchmarks
Winning in 2025
Where Innovation Meets 2025
Your Trusted OEM Partner
Supplies the World’s Top Brands
Manufacturers You Can Rely On
Factory-Direct Excellence
Custom Solutions,
Global Reach
Where Service Meets Innovation
Your End-to-End Solution
More Than a Supplier - A Partner
Service Backed by Expertise
From Concept to Delivery
Outperforms the Competition";
//            $prefix = "How To find"."\n"."Why Choose"."\n"."China Top"."\n"."Best Way To Choose"."\n"."Methods To Choose";
//            $suffix = "Manufacturer"."\n"."Supplier"."\n"."Products"."\n"."Factory";
            if($info === false){
                echo '执行的项目id:'.$val['id'].PHP_EOL;
                $data = [
                    'project_id'=>$val['id'],
                    'prefix'=>$prefix,
                    'suffix'=>$suffix,
                ];
                $keywordAffixModel->addReturnId($data);
            }

        }
        return true;
    }

    public function _actionTemplateMain(){
        $data = [];
        $projectModel = new Project();
        $lists = $projectModel->list(['delete_status' => 0,'project_type'=>0,'extend_type'=>0,'type'=>['in',[2,3,4,6]]], 'id', ['id']);
        foreach ($lists as $val){
            echo date('Y-m-d H:i:s') . '开始--项目的id:'. $val['id'] . PHP_EOL;
            ProjectServer::useProject($val['id']);
            $bTemplateMainModel = new BTemplateMain();
            $info = $bTemplateMainModel->read(['type'=>2,'is_list'=>1],['id']);
            $categoryModel = new Category();
            $count = $categoryModel->counts(['id'=>['>',0]]);
            if(($info === false) && ($count > 0)){
                $mainModel = new TemplateTypeMain();
                $mainInfo = $mainModel->read(['type'=>2,'is_list'=>1]);
                $main_html = $mainInfo['main_html'];
                $main_css = "<style id='globalsojs-styles'></style>";
                //写入一条初始数据
                $bTemplateMainModel->addReturnId(['type'=>2,'is_list'=>1,'is_custom'=>0,'project_id'=>$val['id'],'main_html'=>$main_html,'main_css'=>$main_css]);
                $data[] = $val['id'];
            }
            DB::disconnect('custom_mysql');
            echo date('Y-m-d H:i:s') . '结束--项目的id:'. $val['id'] . PHP_EOL;
        }
        dd($data);
    }

    public function _action_ai_blog(){
        $projectModel = new Project();
        $lists = $projectModel->list(['delete_status' => 0,'extend_type'=>0,'type'=>['in',[2,3,4,6]]], 'id', ['id']);
        foreach ($lists as $val) {
            $aiSettingInfo = $this->getSetting($val['id']);
            if($aiSettingInfo === false){
                echo '当前项目未注册。'.$val['id'].PHP_EOL;
                continue;
            }
            $aiBlogTaskModel = new AiBlogTask();
            $blog_lists = $aiBlogTaskModel->list(['type'=>2,'status'=>2,'project_id'=>$val['id']]);
            if(empty($blog_lists)){
                echo '未获取到ao_blog'.PHP_EOL;
                continue;
            }
            echo date('Y-m-d H:i:s') . '开始--项目的id:'. $val['id'] . PHP_EOL;
            ProjectServer::useProject($val['id']);
            $aiBlogService = new AiBlogService($val['id']);
            foreach ($blog_lists as $item) {
                $aiBlogModel = new AiBlog();
                $aiBlogInfo = $aiBlogModel->read(['task_id' => $item['task_id']], ['id']);
                if ($aiBlogInfo === false) {
                    continue;
                }
                try {
                    $aiBlogService->task_id = $item['task_id'];
                    //拉取文章数据
                    $result = $aiBlogService->getDetail();
                    if (isset($result) && ($result['status'] == 200)) {
                        $aiBlogModel->edit(['seo_description' => $result['data']['description']], ['task_id' => $item['task_id']]);
                    }
                } catch (\Exception $e) {
                    echo '跳过。' . PHP_EOL;
                    continue;
                }
            }
            DB::disconnect('custom_mysql');
            echo date('Y-m-d H:i:s') . '结束--项目的id:'. $val['id'] . PHP_EOL;
        }
        return true;
    }

    public function _action(){
        $aiBlogTaskModel = new AiBlogTask();
        $lists = $aiBlogTaskModel->list(['type'=>2,'status'=>2,'project_id'=>467]);
        foreach ($lists as $item){
            echo date('Y-m-d H:i:s') . '开始--项目的id:'. $item['project_id'] . PHP_EOL;
            ProjectServer::useProject($item['project_id']);
            $aiBlogModel = new AiBlog();
            $aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id','route']);
            if($aiBlogInfo === false){
                continue;
            }
            try {
                $aiBlogService = new AiBlogService($item['project_id']);
                $aiBlogService->task_id = $item['task_id'];
                //拉取文章数据
                $result = $aiBlogService->getDetail();
                if(isset($result) && ($result['status'] == 200)){
                    $aiBlogModel->edit(['seo_description'=>$result['data']['description']],['task_id'=>$item['task_id']]);
                }
            }catch (\Exception $e){
                echo '跳过。'.PHP_EOL;
            }
            DB::disconnect('custom_mysql');
            echo date('Y-m-d H:i:s') . '结束--项目的id:'. $item['project_id'] . PHP_EOL;
        }
    }

    public function getSetting($project_id)
    {
        $projectAiSettingModel = new ProjectAiSetting();
        $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
        return $aiSettingInfo;
    }


    public function _actions() {
        echo '开始执行...' . PHP_EOL;
        ProjectServer::useProject(565);
        $table = 'gl_product_category';
        $connection = DB::connection('custom_mysql');
        $dbName = $connection->getDatabaseName();
        $brandsStr = 'Trimos,insize,Fowler,Mahr,Starrett,Tesa,Mitutoyo,Wixey,Shars,Fisso,Spi,Grizzly,Accumaster,Accuremote,Baker,Sharpe,Compac,Earth,Etalon,Federal,Fell,Fisso,Fowler,Gem,Grizzly,Husky,iGaging,Insize,Kanetec,Magnetics,Mighty,Mitutoyo,Noga,Pittsburgh,Procheck,Saker,Scherr Tumico,Shars,Shinwa,SPI,Starrett,Tesa,Technidea,Trimos,Verdict,Wyler,Wixey';
        $brands = array_unique(array_map('trim', explode(',', $brandsStr)));
        // 获取文本字段
        $columns = $connection->table('information_schema.columns')
            ->where('table_schema', $dbName)
            ->where('table_name', $table)
            ->whereIn('data_type', ['varchar', 'text', 'mediumtext', 'longtext'])
            ->pluck('column_name')
            ->toArray();
        if (empty($columns)) {
            echo "未找到文本字段,退出。" . PHP_EOL;
            return;
        }
        foreach ($columns as $field) {
            $sqlField = "`$field`";
            // 构造第一轮REPLACE表达式(原样品牌)
            $replaceExprRaw = $sqlField;
            foreach ($brands as $brand) {
                $safeBrand = addslashes($brand);
                $replaceExprRaw = "REPLACE(REPLACE($replaceExprRaw, '$safeBrand-', ''), '$safeBrand', '')";
            }
            // 构造第二轮REPLACE表达式(小写品牌)
            $lowerBrands = array_map('strtolower', $brands);
            $replaceExprLower = $sqlField;
            foreach ($lowerBrands as $brand) {
                $safeBrand = addslashes($brand);
                $replaceExprLower = "REPLACE(REPLACE($replaceExprLower, '$safeBrand-', ''), '$safeBrand', '')";
            }
            // 先执行原样品牌替换
            $sqlRaw = "UPDATE `$table` SET $sqlField = $replaceExprRaw WHERE $sqlField IS NOT NULL AND $sqlField != ''";
            echo "执行字段(品牌原样替换):$field" . PHP_EOL;
            try {
                $connection->update($sqlRaw);
            } catch (\Exception $e) {
                echo "字段 $field 原样替换出错:" . $e->getMessage() . PHP_EOL;
            }
            // 再执行小写品牌替换
            $sqlLower = "UPDATE `$table` SET $sqlField = $replaceExprLower WHERE $sqlField IS NOT NULL AND $sqlField != ''";
            echo "执行字段(品牌小写替换):$field" . PHP_EOL;
            try {
                $connection->update($sqlLower);
            } catch (\Exception $e) {
                echo "字段 $field 小写替换出错:" . $e->getMessage() . PHP_EOL;
            }
        }

        echo '执行结束' . PHP_EOL;

        DB::disconnect('custom_mysql');
    }
}