ReplaceHtmlLogic.php 11.2 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\Http\Logic\Aside\Project\ProjectLogic;
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){//扩展模块
            return $this->getCustomTemplateId($typeInfo,$template_id);
        }
        //获取当前项目详情
        $projectInfo = (new ProjectLogic())->getProjectInfo($this->param['project_id']);
        //查看当前页面是否为定制
        if($projectInfo['is_customized'] == BTemplate::IS_VISUALIZATION) {//定制项目
            $type = $this->getCustomizedType($typeInfo['source'], $typeInfo['is_list']);//获取定制界面类型
            //查看当前页面是否定制,是否开启可视化
            $page_array = (array)$projectInfo['is_visualization']->page_array;//获取所有定制界面
            if (in_array($type, $page_array)) {//当前页面是定制界面
                $template_id = 0;
            }
        }
        return $this->success($template_id);
    }

    /**
     * @remark :获取扩展模块的templateId
     * @name   :getCustomTemplateId
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 14:53
     */
    public function getCustomTemplateId($typeInfo,$template_id){
        $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 :定制页面头部类型---根据source获取type类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:20
     */
    public function getCustomizedType($source,$is_list){
        $type = BTemplate::TYPE_HOME;
        if($source == BTemplate::SOURCE_PRODUCT){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_PRODUCT_LIST;
            }else{
                $type = BTemplate::TYPE_PRODUCT_DETAIL;
            }
        }
        if($source == BTemplate::SOURCE_BLOG){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_BLOG_LIST;
            }else{
                $type = BTemplate::TYPE_BLOG_DETAIL;
            }
        }
        if($source == BTemplate::SOURCE_NEWS){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_NEWS_LIST;
            }else{
                $type = BTemplate::TYPE_NEWS_DETAIL;
            }
        }
        return $type;
    }

    /**
     * @remark :替换可视化的html代码(按类型)
     * @name   :replaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/7 15:52
     */
    public function replaceTemplateMainHtml(){
        $data = $this->sourceTypeInfo($this->param['project_id']);
        $typeInfo = $data[$this->param['name']];
        ProjectServer::useProject($this->param['project_id']);
        $bTemplateModel = new BTemplate();
        if($typeInfo['source'] == 0){//所有页面
            $bSettingModel = new Setting();
            $templateInfo = $bSettingModel->read(['project_id'=>$this->param['project_id']]);
            if($templateInfo === false){
                $this->fail('请先选择模版');
            }
            $template_id = $templateInfo['template_id'];
            $condition = ['template_id'=>$template_id,'main_html'=>['like','%'.$this->param['old_html'].'%']];
            $total_num = $bTemplateModel->formatQuery($condition)->count();
        }else{
            $template_id = $this->getTemplateId($typeInfo);
            $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'].'%']];
            $total_num = $bTemplateModel->formatQuery($condition)->count();
        }
        DB::disconnect('custom_mysql');
        $replaceId = $this->saveReplaceHtml($this->param,$typeInfo,$template_id,$total_num ?? 0);
        return $this->success(['id'=>$replaceId]);
    }

    /**
     * @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,
            'operator_id'=>$this->manager['id']
        ];
        return $this->model->addReturnId($logData);
    }

    /**
     * @remark :生成还原记录
     * @name   :reductionHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 10:35
     */
    public function reductionHtml(){
        $info = $this->model->read(['id'=>$this->param['id'],'status'=>$this->model::STATUS_END]);
        if($info === false){
            $this->fail('当前数据不存在');
        }
        if($info['source'] == 0){//当前数据是替换的所有页面
            $data = $this->sourceTypeInfo($info['project_id']);
            $typeInfo = $data[$this->param['name']];
            if($typeInfo['source'] != 0){//回滚页面
                $info['source'] = $typeInfo['source'];
                $info['is_custom'] = $typeInfo['is_custom'];
                $info['is_list'] = $typeInfo['is_list'];
                $replaceId = $this->saveResultReplaceHtml($info);
                return $this->success(['id'=>$replaceId]);
            }
        }
        $replaceId = $this->saveResultReplaceHtml($info);
        return $this->success(['id'=>$replaceId]);
    }

    /**
     * @remark :保存还原的html
     * @name   :saveResultReplaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 10:01
     */
    public function saveResultReplaceHtml($info,$status = 0,$num = 0){
        $logData = [
            'source'=>$info['source'],
            'is_custom'=>$info['is_custom'],
            'is_list'=>$info['is_list'],
            'template_id'=>$info['template_id'],
            'status'=>$status != 0 ? $status : $this->model::STATUS,
            'old_html'=>$info['html'],
            'html'=>$info['old_html'],
            'project_id'=>$info['project_id'],
            'is_rollback'=>1,
            'rollback_id'=>$info['id'],
            'total_num'=>($num != 0) ? $num : $info['total_num'],
            'operator_id'=>$this->manager['id']
        ];
        return $this->model->addReturnId($logData);
    }

    /**
     * @remark :替换类型
     * @name   :sourceTypeInfo
     * @author :lyh
     * @method :post
     * @time   :2024/5/9 17:15
     */
    public function sourceTypeInfo($project_id){
        $data = $this->model->sourceType();
        ProjectServer::useProject($project_id);
        $customModule = new CustomModule();
        $moduleList = $customModule->list(['project_id'=>$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);
    }

    /**
     * @remark :根据类型获取名称
     * @name   :getSourceName
     * @author :lyh
     * @method :post
     * @time   :2024/5/10 16:55
     */
    public function getSourceName($source,$is_list,$is_custom,$project_id){
        $arr = ['source'=>$source,'is_list'=>$is_list,'is_custom'=>$is_custom];
        $data = $this->sourceTypeInfo($project_id);
        foreach ($data as $k => $v){
            if($v == $arr){
                return $this->success($k);
            }
        }
        return $this->success();
    }

    /**
     * @remark :根据id还原
     * @name   :rollbackIdHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/11 11:31
     */
    public function rollbackIdHtml(){
        if(is_array($this->param['id']) && isset($this->param['id'])){
            $save_id = 0;
            foreach ($this->param['id'] as $k => $id){
                $replaceHtmlLogModel = new TemplateReplaceHtmlLog();
                $logInfo = $replaceHtmlLogModel->read(['id'=>$id]);
                //查询当前主任务
                $replaceHtmlModel = new TemplateReplaceHtml();
                $info = $replaceHtmlModel->read(['id'=>$logInfo['replace_id']]);
                if($k == 0){
                    //生成一条已完成的主记录
                    $save_id = $this->saveResultReplaceHtml($info,TemplateReplaceHtml::STATUS_END,count($this->param['id']));
                }
                //生成一条子记录
                $this->saveResultSonInfo($save_id,$logInfo);
            }
        }
        return $this->success();
    }

    /**
     * @remark :生成一条还原子记录
     * @name   :saveResultSonInfo
     * @author :lyh
     * @method :post
     * @time   :2024/5/13 15:14
     */
    public function saveResultSonInfo($save_id,$logInfo){
        $saveData = [
            'replace_id'=>$save_id,
            'project_id'=>$logInfo['project_id'],
            'status'=>TemplateReplaceHtmlLog::STATUS,
            'old_html'=>$logInfo['html'],
            'html'=>$logInfo['old_html'],
            'source'=>$logInfo['source'],
            'source_id'=>$logInfo['source_id'],
            'is_custom'=>$logInfo['is_custom'],
            'is_list'=>$logInfo['is_list'],
            'is_rollback'=>1,
            'template_id'=>$logInfo['template_id'],
            'source_name'=>$logInfo['source_name']
        ];
        $replaceHtmlLogModel = new TemplateReplaceHtmlLog();
        return $replaceHtmlLogModel->addReturnId($saveData);
    }
}