ReplaceHtmlLogic.php 4.3 KB
<?php
/**
 * @remark :
 * @name   :ReplaceHtmlLogic.php
 * @author :lyh
 * @method :post
 * @time   :2024/5/8 10:03
 */

namespace App\Http\Logic\Aside\Template;

use App\Http\Logic\aside\BaseLogic;
use App\Models\Template\BTemplate;
use App\Models\Template\TemplateReplaceHtml;
use App\Models\Template\TemplateReplaceHtmlLog;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;

class ReplaceHtmlLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new TemplateReplaceHtml();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :替换可视化的html代码(按类型)
     * @name   :replaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/7 15:52
     */
    public function replaceHtml(){
        ProjectServer::useProject($this->param['project_id']);
        //TODO::生成一条任务记录
        $replaceId = $this->saveReplaceHtml($this->param);
        //查询当前类型所有装修的记录
        $condition = ['source'=>$this->param['type'],'is_custom'=>$this->param['is_custom'],'is_list'=>$this->param['is_list'],
            'template_id'=>$this->param['template_id']];
        $list = $this->model->list($condition);
        foreach ($list as $v){
            if($v['type'] == 0){
                $main_html = str_replace($this->param['old_html'],$this->param['html'],$v['main_html']);
                $this->model->edit(['main_html'=>$main_html],['id'=>$v['id']]);
            }else{
                $html = str_replace($this->param['old_html'],$this->param['html'],$v['html']);
                $this->model->edit(['html'=>$html],['id'=>$v['id']]);
            }
            $this->saveReplaceHtmlLog($replaceId,$v['id']);
        }
        DB::disconnect('custom_mysql');
        return $this->success();
    }

    /**
     * @remark :生成一条记录
     * @name   :saveTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 9:23
     */
    public function saveReplaceHtml($data,$template_id){
        $logData = [
            'type'=>$data['type'],
            'is_custom'=>$data['is_custom'],
            'is_list'=>$data['is_list'],
            'template_id'=>$template_id,
            'old_html'=>$data['old_html'],
            'html'=>$data['html'],
        ];
        return $this->model->addReturnId($logData);
    }

    /**
     * @remark :保存每条替换记录
     * @name   :saveReplaceHtmlLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 9:37
     */
    public function saveReplaceHtmlLog($replace_id,$replace_template_id){
        $logData = [
            'replace_id'=>$replace_id,
            'replace_template_id'=>$replace_template_id,
            'uid'=>$this->user['manager_id'],
        ];
        $replaceHtmlModel = new TemplateReplaceHtmlLog();
        return $replaceHtmlModel->add($logData);
    }

    /**
     * @remark :还原所有记录
     * @name   :reductionHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 10:35
     */
    public function reductionHtml(){
        ProjectServer::useProject($this->param['project_id']);
        //获取当前数据详情
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在');
        }
        $replaceLogModel = new TemplateReplaceHtmlLog();
        $logList = $replaceLogModel->list(['replace_id'=>$this->param['id']]);
        $replaceArr = [];
        foreach ($logList as $v){
            $replaceArr[] = $v['replace_template_id'];
        }
        if(!empty($replaceArr)){
            //查询可视化数据
            $bTemplateModel = new BTemplate();
            $templateList = $bTemplateModel->list(['id'=>['in',$replaceArr]]);
            foreach ($templateList as $value){
                if($v['type'] == 0){
                    $main_html = str_replace($info['html'],$info['old_html'],$value['main_html']);
                    $this->model->edit(['main_html'=>$main_html],['id'=>$v['id']]);
                }else{
                    $html = str_replace($info['html'],$info['old_html'],$value['html']);
                    $this->model->edit(['html'=>$html],['id'=>$v['id']]);
                }
            }
        }
        DB::disconnect('custom_mysql');
        return $this->success();
    }
}