AddDb.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Interface\Database\Add;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Str;
  7. use Txj\Elastic\Facades\ES;
  8. use App\Services\Interface\Database\CommonDb;
  9. class AddDb extends CommonDb
  10. {
  11. public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
  12. {
  13. parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
  14. // $requestData 过滤多余的字段,防止添加的时候造成字段污染
  15. $columnArr = array_column($this->assembleColumns, 'code');
  16. if ($requestData) {
  17. foreach ($requestData as $key => $value) {
  18. if (!in_array($key, $columnArr)) {
  19. unset($requestData[$key]);
  20. }
  21. }
  22. $this->requestData = $requestData;
  23. }
  24. }
  25. public function exec()
  26. {
  27. $add_type = $this->interfaceSettingInfo['add_type'] ?? 0;
  28. if (2 === $add_type) { // 批量添加
  29. $result = $this->addMulti();
  30. } elseif (3 === $add_type) { // 添加树型数据
  31. $result = $this->addOneTree();
  32. } elseif (1 === $add_type) { // 添加单条数据
  33. $result = $this->addOne();
  34. } else {
  35. $result = $this->addOne();
  36. }
  37. return $result;
  38. }
  39. /**
  40. * 添加单条数据.
  41. */
  42. public function addOne(): int
  43. {
  44. $find = DB::connection($this->getDbConnection())->table($this->table);
  45. // 添加数据 最多允许的数量
  46. $this->addTotalLimit($find);
  47. if (!isset($this->requestData['mid'])) {
  48. $this->requestData['mid'] = Str::random(12);
  49. }
  50. if (!isset($this->requestData['created_at'])) {
  51. $this->requestData['created_at'] = time();
  52. }
  53. if (!isset($this->requestData['updated_at'])) {
  54. $this->requestData['updated_at'] = time();
  55. }
  56. return $find->insertGetId($this->requestData);
  57. }
  58. /**
  59. * 添加树型数据.
  60. *
  61. * @return int
  62. */
  63. public function addOneTree()
  64. {
  65. $find = DB::connection($this->getDbConnection())->table($this->table);
  66. // 添加数据 最多允许的数量
  67. $this->addTotalLimit($find);
  68. if (!isset($this->requestData['mid'])) {
  69. $this->requestData['mid'] = Str::random(12);
  70. }
  71. if (!isset($this->requestData['created_at'])) {
  72. $this->requestData['created_at'] = time();
  73. }
  74. if (!isset($this->requestData['updated_at'])) {
  75. $this->requestData['updated_at'] = time();
  76. }
  77. // 设置树型数据参数
  78. $requestData = $this->getTreeParams($this->requestData);
  79. return $find->insertGetId($requestData);
  80. }
  81. /**
  82. * 批量添加数据.
  83. */
  84. public function addMulti() // // todo 代码不对
  85. {
  86. $find = DB::connection($this->getDbConnection())->table($this->table);
  87. return $find->insert();
  88. }
  89. /**
  90. * 填充树型数据的父级分类数据.
  91. */
  92. private function getTreeParams(?array $requestData): array
  93. {
  94. if (isset($requestData['parent_id']) && $requestData['parent_id']) {
  95. // 查询父级信息
  96. $parentInfo = ES::table($this->table)->find($requestData['parent_id']);
  97. if (empty($parentInfo)) {
  98. abort(508, '父级数据不存在,请重试!');
  99. }
  100. $requestData['level'] = $parentInfo['level'] + 1;
  101. } else {
  102. $requestData['parent_id'] = 0;
  103. $requestData['level'] = 1;
  104. }
  105. return $requestData;
  106. }
  107. /**
  108. * 添加数据 最多允许的数量.
  109. */
  110. private function addTotalLimit($find): void
  111. {
  112. // 判断是否超出总数限制
  113. $totalSetting = $this->interfaceInfo['total_setting'] ?? [];
  114. if ($totalSetting) {
  115. $totalSetting = json_decode($totalSetting, true);
  116. if (2 === $totalSetting['type']) {
  117. $num = $totalSetting['num'] ?? 1;
  118. $total = $find->count();
  119. if ($total > $num) {
  120. abort(508, '对不起,最多只允许添加' . $num . '条信息!');
  121. }
  122. }
  123. }
  124. }
  125. }