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

namespace App\Console\Commands\ReplaceHtml;

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

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

    /**
     * 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_START]);
            foreach ($replaceHtmlList as $value){
                echo '开始主任务id:'.$value['id'].PHP_EOL;
                $replaceHtmlLogModel = new TemplateReplaceHtmlLog();
                $replaceHtmlLogList = $replaceHtmlLogModel->list(['replace_id'=>$value['id'],'status'=>$replaceHtmlLogModel::STATUS]);
                ProjectServer::useProject($value['project_id']);
                foreach ($replaceHtmlLogList as $v){
                    echo date('Y-m-d H:i:s') . '子任务id :'.$v['id'] . PHP_EOL;
                    $this->replaceHtml($v);
                    $replaceHtmlLogModel->edit(['status'=>$replaceHtmlLogModel::STATUS_END],['id'=>$v['id']]);
                }
                DB::disconnect('custom_mysql');
                //修改当前主任务状态为待执行
                $replaceHtmlModel->edit(['status'=>$replaceHtmlModel::STATUS_END],['id'=>$value['id']]);
                echo '结束'.PHP_EOL;
            }
        }
        sleep(20);
        return true;
    }

    /**
     * @remark :替换html
     * @name   :replaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 17:56
     */
    public function replaceHtml($info){
        $source_id = $info['source'] == 1 ? 0 : $info['source_id'];
        $bTemplateModel = new BTemplate();
        $condition = ['source'=>$info['source'],'source_id'=>$source_id,
            'template_id'=>$info['template_id'],'is_custom'=>$info['is_custom'],'is_list'=>$info['is_list']];
        $old_html = $info['old_html'];
        $html = $info['html'];
        if($info['template_id'] == 0){
            $bTemplateModel->formatQuery($condition)->update(['html' => DB::raw("REPLACE(html, '$old_html', '$html')")]);
        }else{
            $bTemplateModel->where($condition)
                ->update([
                    'main_html' => DB::raw("REPLACE(main_html, '" . addslashes($old_html) . "', '" . addslashes($html) . "')"),
                    'updated_at' => now(),
                ]);
        }
        return true;
    }

}