ServerConfigLogic.php 4.3 KB
<?php

namespace App\Http\Logic\Aside\Devops;



use App\Http\Logic\Aside\BaseLogic;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Devops\DevopsTask;
use App\Models\Project\Project;
use App\Models\Devops\ServerConfig;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;

/**
 * Class ServerConfigLogic
 * @package App\Http\Logic\Aside
 * @author zbj
 * @date 2023/4/23
 */
class ServerConfigLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new ServerConfig();
    }

    /**
     * @remark :获取域名列表
     * @name   :serviceConfigList
     * @author :lyh
     * @method :post
     * @time   :2023/8/2 14:51
     */
    public function serviceConfigList($map,$page,$row,$order = 'id',$filed = ['*']){
        $lists = $this->model->lists($map,$page,$row,$order,$filed);
        return $this->success($lists);
    }

    /**
     * @remark :获取当前数据详情
     * @name   :getServiceConfig
     * @author :lyh
     * @method :post
     * @time   :2023/8/2 17:53
     */
    public function getServiceConfig(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或者被删除');
        }
        return $this->success($info);
    }

    /**
     * @remark :保存
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/8/2 17:55
     */
    public function serviceConfigSave()
    {
        DB::beginTransaction();
        try {
            //保存配置
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $this->model->edit($this->param,['id'=>$this->param['id']]);
            }else{
                $this->model->add($this->param);
            }
            //初始化数据库
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            $this->fail('保存失败');
        }
        return $this->success();
    }


    /**
     * @remark :初始化数据库
     * @name   :initializationMysql
     * @author :lyh
     * @method :post
     * @time   :2023/8/4 15:08
     */
    public function initializationMysql($param){
        if ($param['type'] == ServerConfig::TYPE_MYSQL) {
            //切换数据库配置
            $project = ProjectServer::useProject($param['project_id']);
            //创建数据库
            ProjectServer::createDatabase($project);
            //创建表
            ProjectServer::initTable($project);
        }
        return true;
    }

    /**
     * @remark :删除记录
     * @name   :delServiceConfig
     * @author :lyh
     * @method :post
     * @time   :2023/8/2 15:43
     */
    public function delServiceConfig(){
        $info = $this->model->read(['id'=>$this->requestAll['id']]);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        $rs = $this->model->del(['id'=>$this->requestAll['id']]);
        if($rs === false){
            $this->fail('删除失败');
        }
        //TODO::上线放开
//        if($info['type'] == $this->model::TYPE_MYSQL){
//            $sql = 'DROP DATABASE '.$info['title'];
//            DB::connection('custom_mysql')->statement($sql);
//        }
        return $this->success();
    }

    /**
     * 更新指定项目 数据库
     * @param $param
     * @return array
     * @author zbj
     * @date 2023/4/24
     */
    public function updateTable($param){
        $project = ProjectServer::useProject($param['project_id']);
        if(!$project){
            $this->fail('项目不存在或数据库未配置');
        }
        DB::connection('custom_mysql')->statement($param['sql']);
        return $this->success();
    }

    /**
     * 更新本地 数据库
     * @param $param
     * @return array
     * @author zbj
     * @date 2023/4/24
     */
    public function updateLocalTable($param){
        DB::statement($param['sql']);
        return $this->success();
    }

    /**
     * 更新所有项目 数据库
     * @param $param
     * @return array
     * @author zbj
     * @date 2023/4/24
     */
    public function updateAllTable($param){
        DevopsTask::addTask($param['sql']);
        return $this->success();
    }
}