ServerConfigLogic.php 5.4 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 App\Utils\EncryptUtils;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;

/**
 * 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);
        if(!empty($lists) && !empty($lists)){
            $projectModel = new Project();
            foreach ($lists['list'] as $k => $v){
                if($v['type'] == $this->model::TYPE_SERVER){
                    $v['count'] = $projectModel->where(['serve_id'=>$v['id'],'delete_status'=>0])->count();
                }
                $lists['list'][$k] = $v;
            }
        }
        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 {
            $encrypt = new EncryptUtils();
            $this->param['user'] = $encrypt->lock_url($this->param['user']);
            $this->param['password'] = $encrypt->lock_url($this->param['password']);
            $this->param['port'] = $encrypt->lock_url($this->param['port']);
            //保存配置
            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   :mysqlConfig
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 11:57
     */
    public function mysqlConfig($data){
        // 构建数据库配置数组
        $dbConfig = [
            'driver' => 'mysql', // 数据库类型
            'host' => $data['host'],     // 数据库主机
            'database' => $data['host'], // 数据库名
            'username' => $data['username'], // 数据库用户名
            'password' => $data['password'], // 数据库密码
        ];
        // 临时设置数据库配置
        config(['database.connections.temp' => $dbConfig]);
        try {
            // 使用数据库配置信息尝试建立数据库连接
            DB::connection()->getPdo();
            // 如果连接成功,表示账号、密码和主机信息正确
            echo "Database connection successful!";
        } catch (\Exception $e) {
            // 连接失败,输出错误信息
            echo "Database connection failed: " . $e->getMessage();
        }
    }

    /**
     * @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('删除失败');
        }
        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']);
        //执行命令行
        Artisan::call('php artisan devops_task');
        return $this->success();
    }
}