ConnectDBController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Admin;
  3. use Illuminate\Http\JsonResponse;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Request;
  6. use Txj\Elastic\Facades\ES;
  7. class ConnectDBController extends AdminBaseController
  8. {
  9. public string $table = 'dep_config_connect_db';
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->setWebsite(__NAMESPACE__);
  14. $this->isLoginJson();
  15. }
  16. public function showList()
  17. {
  18. $request = Request::instance();
  19. $category_type = $request->post('category_type');
  20. $listObj = DB::table($this->table)->orderBy('created_at');
  21. if ($category_type) {
  22. $listObj->where('category_type', $category_type);
  23. }
  24. $list = $listObj->get();
  25. $list = array_map('get_object_vars', $list->toArray());
  26. return responseMessage(1002, '', $list);
  27. }
  28. /**
  29. * @return JsonResponse
  30. */
  31. public function save()
  32. {
  33. $request = Request::instance();
  34. $id = $request->post('id');
  35. $category_type = $request->post('category_type'); //1 关系数据库 2 非关系数据库
  36. $type = $request->post('type'); // // sqlite mysql pgsql sqlsrv tidb file redis elasticsearch mongodb
  37. $title = $request->post('title');
  38. $config_json = $request->post('config_json');
  39. $requestData = [
  40. 'category_type' => $category_type,
  41. 'type' => $type,
  42. 'title' => $title,
  43. 'config_json' => $config_json
  44. ];
  45. if ($id) {
  46. // 判断是否已经存在
  47. $isExist = ES::table($this->table)
  48. ->where('title', $title)
  49. ->where('type', $type)
  50. ->mustNot(function ($query) use ($id){
  51. $query->where('id', $id);
  52. })
  53. ->count();
  54. if ($isExist) {
  55. return responseMessage(2001, '该标题已经存在,不允许重复添加!');
  56. }
  57. $isSuccess = ES::table($this->table)->updateEntityById($id, $requestData);
  58. } else {
  59. // 判断是否已经存在
  60. $isExist = ES::table($this->table)->where('title', $title)->where('type', $type)->count();
  61. if ($isExist) {
  62. return responseMessage(2002, '该标题已经存在,不允许重复添加!');
  63. }
  64. $isSuccess = ES::table($this->table)->insertGetId($requestData);
  65. }
  66. if ($isSuccess) {
  67. return responseMessage(1001, '添加成功!');
  68. } else {
  69. return responseMessage(2003, '添加失败,请重试!');
  70. }
  71. }
  72. }