ReplaceHtmlLogic.php 7.5 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\CustomModule\CustomModule;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
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 :获取模版id
     * @name   :getTemplateId
     * @author :lyh
     * @method :post
     * @time   :2024/5/9 18:03
     */
    public function getTemplateId($typeInfo){
        //获取当前template_id
        $bSettingModel = new Setting();
        $templateInfo = $bSettingModel->read(['project_id'=>$this->param['project_id']]);
        if($templateInfo === false){
            $this->fail('请先选择模版');
        }
        $template_id = $templateInfo['template_id'];
        if($typeInfo['is_custom'] == 1){//扩展模块
            $customModuleModel = new CustomModule();
            $moduleInfo = $customModuleModel->read(['id'=>$typeInfo['type']],['list_customized','detail_customized']);
            if($moduleInfo === false){
                $this->fail('当前扩展模块不存在或已被删除');
            }
            if($typeInfo['is_list'] == 1 && $moduleInfo['list_customized'] == 1){
                $template_id = 0;
            }
            if($typeInfo['is_list'] == 0 && $moduleInfo['detail_customized'] == 1){
                $template_id = 0;
            }
        }
        return $this->success($template_id);
    }

    /**
     * @remark :替换可视化的html代码(按类型)
     * @name   :replaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/7 15:52
     */
    public function replaceTemplateMainHtml(){
        $data = $this->sourceTypeInfo();
        $typeInfo = $data[$this->param['name']];
        if(!isset($typeInfo)){
            $this->fail('当前类型不存在,请联系管理员');
        }
        $template_id = $this->getTemplateId($typeInfo);
        //查询当前类型所有装修的记录
        ProjectServer::useProject($this->param['project_id']);
        $bTemplateModel = new BTemplate();
        $condition = ['source'=>$typeInfo['source'],'is_custom'=>$typeInfo['is_custom'],
            'is_list'=>$typeInfo['is_list'], 'template_id'=>$template_id,'main_html'=>['like','%'.$this->param['old_html'].'%']];
        $list = $bTemplateModel->list($condition);
        DB::disconnect('custom_mysql');
        //TODO::生成一条任务记录
        $total_num = count($list);
        $replaceId = $this->saveReplaceHtml($this->param,$typeInfo,$template_id,$total_num);
        foreach ($list as $value){
            //生成子任务
            $this->saveReplaceHtmlLog($replaceId,$value);
        }
        return $this->success();
    }

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

    /**
     * @remark :保存每条替换记录
     * @name   :saveReplaceHtmlLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 9:37
     */
    public function saveReplaceHtmlLog($param,$replace_id,$data){
        $logData = [
            'replace_id'=>$replace_id,
            'source'=>$data['source'],
            'source_id'=>$data['source_id'],
            'is_list'=>$data['is_list'],
            'is_custom'=>$data['is_custom'],
            'project_id'=>$data['project_id'],
            'uid'=>$this->user['manager_id'],
            'status'=>$this->model::STATUS,
            'old_html'=>$param['old_html'],
            'html'=>$param['html'],
            'template_id'=>$data['template_id']
        ];
        $replaceHtmlModel = new TemplateReplaceHtmlLog();
        $save_id = $replaceHtmlModel->addReturnId($logData);
        return $this->success($save_id);
    }

    /**
     * @remark :还原所有记录
     * @name   :reductionHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 10:35
     */
    public function reductionHtml(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在');
        }
        $replaceId = $this->saveResultReplaceHtml($info);
        ProjectServer::useProject($this->param['project_id']);
        $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'=>$value['id']]);
                }else{
                    $html = str_replace($info['html'],$info['old_html'],$value['html']);
                    $this->model->edit(['html'=>$html],['id'=>$value['id']]);
                }
            }
            $this->saveReplaceHtmlLog($replaceId,$value['id']);
        }
        DB::disconnect('custom_mysql');
        return $this->success();
    }

    /**
     * @remark :保存还原的html
     * @name   :saveResultReplaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 10:01
     */
    public function saveResultReplaceHtml($info){
        $logData = [
            'type'=>$info['type'],
            'is_custom'=>$info['is_custom'],
            'is_list'=>$info['is_list'],
            'template_id'=>$info['template_id'],
            'old_html'=>$info['html'],
            'html'=>$info['old_html'],
            'project_id'=>$info['project_id'],
        ];
        return $this->model->addReturnId($logData);
    }

    /**
     * @remark :替换类型
     * @name   :sourceTypeInfo
     * @author :lyh
     * @method :post
     * @time   :2024/5/9 17:15
     */
    public function sourceTypeInfo(){
        ProjectServer::useProject($this->param['project_id']);
        $data = $this->model->sourceType();
        $customModule = new CustomModule();
        $moduleList = $customModule->list(['project_id'=>$this->param['project_id']],'id',['id','name']);
        foreach ($moduleList as $value){
            $data[$value['name'].'详情'] = ['source'=>$value['id'],'is_list'=>0,'is_custom'=>1];
            $data[$value['name'].'列表'] = ['source'=>$value['id'],'is_list'=>1,'is_custom'=>1];
        }
        DB::disconnect('custom_mysql');
        return $this->success($data);
    }
}