ReplaceHtml.php 3.1 KB
<?php
/**
 * @remark :
 * @name   :ReplaceHtml.php
 * @author :lyh
 * @method :post
 * @time   :2024/5/10 17:20
 */

namespace App\Console\Commands\ReplaceHtml;

use App\Models\Template\BTemplate;
use App\Models\Template\TemplateReplaceHtmlLog;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\Template\TemplateReplaceHtml;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '替换html代码生成子任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return bool
     */
    public function handle()
    {
        while (true){
            $replaceHtmlModel = new TemplateReplaceHtml();
            $replaceHtmlList = $replaceHtmlModel->list(['status'=>$replaceHtmlModel::STATUS]);
            if(!empty($replaceHtmlList)){
                foreach ($replaceHtmlList as $k => $v){
                    ProjectServer::useProject($v['project_id']);
                    echo '开始,任务id:'.$v['id'].PHP_EOL;
                    $this->createReplaceHtmlLog($v);
                    //修改当前主任务状态为待执行
                    $replaceHtmlModel->edit(['status'=>$replaceHtmlModel::STATUS_START],['id'=>$v['id']]);
                    echo '结束'.PHP_EOL;
                    DB::disconnect('custom_mysql');
                }
            }
            sleep(5);
            return true;
        }
    }

    /**
     * @remark :生成子任务
     * @name   :createReplaceHtmlLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 17:26
     */
    public function createReplaceHtmlLog($info){
        //获取当前页面所有数据
        $saveData = [];
        $bTemplateModel = new BTemplate();
        $condition = ['source'=>$info['source'],'template_id'=>$info['template_id'],'is_custom'=>$info['is_custom'],'is_list'=>$info['is_list']];
        $bTemplateList = $bTemplateModel->list($condition,'id',['id']);
        foreach ($bTemplateList as $v){
            $saveData[] = [
                'replace_id'=>$info['id'],
                'project_id'=>$info['project_id'],
                'status'=>0,
                'old_html'=>$info['old_html'],
                'html'=>$info['html'],
                'source'=>$info['source'],
                'source_id'=>$v['id'],
                'is_custom'=>$info['is_custom'],
                'is_list'=>$info['is_list'],
                'is_rollback'=>$info['is_rollback'],
                'template_id'=>$info['template_id'],
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ];
        }
        if(!empty($saveData)){
            $templateHtmlLogModel = new TemplateReplaceHtmlLog();
            $templateHtmlLogModel->insert($saveData);
        }
        return true;
    }


}