12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Http\Admin;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Request;
- use Txj\Elastic\Facades\ES;
- class ConnectDBController extends AdminBaseController
- {
- public string $table = 'dep_config_connect_db';
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- $this->isLoginJson();
- }
- public function showList()
- {
- $request = Request::instance();
- $category_type = $request->post('category_type');
- $listObj = DB::table($this->table)->orderBy('created_at');
- if ($category_type) {
- $listObj->where('category_type', $category_type);
- }
- $list = $listObj->get();
- $list = array_map('get_object_vars', $list->toArray());
- return responseMessage(1002, '', $list);
- }
- /**
- * @return JsonResponse
- */
- public function save()
- {
- $request = Request::instance();
- $id = $request->post('id');
- $category_type = $request->post('category_type'); //1 关系数据库 2 非关系数据库
- $type = $request->post('type'); // // sqlite mysql pgsql sqlsrv tidb file redis elasticsearch mongodb
- $title = $request->post('title');
- $config_json = $request->post('config_json');
- $requestData = [
- 'category_type' => $category_type,
- 'type' => $type,
- 'title' => $title,
- 'config_json' => $config_json
- ];
- if ($id) {
- // 判断是否已经存在
- $isExist = ES::table($this->table)
- ->where('title', $title)
- ->where('type', $type)
- ->mustNot(function ($query) use ($id){
- $query->where('id', $id);
- })
- ->count();
- if ($isExist) {
- return responseMessage(2001, '该标题已经存在,不允许重复添加!');
- }
- $isSuccess = ES::table($this->table)->updateEntityById($id, $requestData);
- } else {
- // 判断是否已经存在
- $isExist = ES::table($this->table)->where('title', $title)->where('type', $type)->count();
- if ($isExist) {
- return responseMessage(2002, '该标题已经存在,不允许重复添加!');
- }
- $isSuccess = ES::table($this->table)->insertGetId($requestData);
- }
- if ($isSuccess) {
- return responseMessage(1001, '添加成功!');
- } else {
- return responseMessage(2003, '添加失败,请重试!');
- }
- }
- }
|