QueryDb.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Interface\Database\Query;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Request;
  7. use App\Services\Interface\Database\CommonDb;
  8. class QueryDb extends CommonDb
  9. {
  10. public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
  11. {
  12. parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
  13. }
  14. public function exec()
  15. {
  16. $search_type = $this->interfaceInfo['search_type'];
  17. if (1 === $search_type) { // 单条信息查询
  18. $result = $this->queryOne();
  19. } elseif (2 === $search_type) { // 分页查询
  20. $result = $this->queryByPaginator();
  21. } elseif (3 === $search_type) { // 列表查询
  22. $result = $this->queryAll();
  23. } elseif (4 === $search_type) { // 树型列表
  24. $result = $this->queryAll();
  25. if ($result) {
  26. $result = generateToTree($result);
  27. } else {
  28. $result = [];
  29. }
  30. } elseif (5 === $search_type) { // 条件总数
  31. $result = $this->queryCount();
  32. } elseif (6 === $search_type) { // sum
  33. $result = $this->querySum();
  34. } elseif (7 === $search_type) { // avg
  35. $result = $this->queryAvg();
  36. } elseif (8 === $search_type) { // max
  37. $result = $this->queryMax();
  38. } elseif (9 === $search_type) { // min
  39. $result = $this->queryMin();
  40. } else {
  41. abort(508, '接口配置[search_type]错误');
  42. }
  43. return $result;
  44. }
  45. /**
  46. * 单条信息查询.
  47. */
  48. public function queryOne(): array
  49. {
  50. if (empty($this->conditionSettingsInfo)) {
  51. abort(508, '参数错误,查询条件不能为空!');
  52. }
  53. list($find, $toArrColumns) = $this->queryEntity();
  54. if ($this->conditionSettingsInfo) { // es查询
  55. foreach ($this->conditionSettingsInfo as $row) {
  56. if (1 == $row['type']) { // 普通查询
  57. $this->execSearchCon($find, $row);
  58. } else { // 混合查询
  59. $this->checkExpression('search_type', $this->requestData, $row['condition'], $find);
  60. }
  61. }
  62. }
  63. // 排序规则
  64. $this->sortFields($find);
  65. // DB::connection($this->getDbConnection())->enableQueryLog();#开启执行日志
  66. $result = $find->first();
  67. // Log::info($this->interfaceInfo['en_alias'] . " sql === ", DB::getQueryLog());
  68. // 需要转换为数组的字段
  69. if ($toArrColumns) {
  70. foreach ($toArrColumns as $column) {
  71. if ($result->{$column}) {
  72. $result->{$column} = json_decode($result->{$column});
  73. } else {
  74. $result->{$column} = [];
  75. }
  76. }
  77. }
  78. return $result ? get_object_vars($result) : [];
  79. }
  80. public function queryByPaginator(): array
  81. {
  82. $currentPage = Request::post('page', "1");
  83. $size = Request::post('size', "20");
  84. $size = (int)$size;
  85. DB::connection($this->getDbConnection())->enableQueryLog();#开启执行日志
  86. list($find, $toArrColumns) = $this->queryEntity();
  87. $total = $find->count();
  88. $list = $find->offset(($currentPage - 1) * $size)->limit($size)->get();
  89. Log::info($this->interfaceInfo['en_alias'] . " sql === ", DB::getQueryLog());
  90. // 需要转换为数组的字段
  91. if ($toArrColumns) {
  92. foreach ($list as $key => $item) {
  93. foreach ($toArrColumns as $column) {
  94. if ($item->{$column}) {
  95. $item->{$column} = json_decode($item->{$column});
  96. } else {
  97. $item->{$column} = [];
  98. }
  99. }
  100. }
  101. }
  102. return ['list' => $list, 'total' => $total];
  103. }
  104. public function queryAll(): array
  105. {
  106. list($find, $toArrColumns) = $this->queryEntity();
  107. $list = $find->get();
  108. // 需要转换为数组的字段
  109. if ($toArrColumns) {
  110. foreach ($list as $key => $item) {
  111. foreach ($toArrColumns as $column) {
  112. if ($item->{$column}) {
  113. $item->{$column} = json_decode($item->{$column});
  114. } else {
  115. $item->{$column} = [];
  116. }
  117. }
  118. }
  119. }
  120. return array_map('get_object_vars', $list->toArray());
  121. }
  122. public function queryCount(): int
  123. {
  124. list($find) = $this->queryEntity();
  125. return $find->count();
  126. }
  127. public function querySum(): int
  128. {
  129. $search_column_id = $this->interfaceSettingInfo['search_column_id'];
  130. $field = $this->columnIdToCodes[$search_column_id];
  131. list($find) = $this->queryEntity();
  132. return $find->sum($field);
  133. }
  134. public function queryAvg(): int
  135. {
  136. $search_column_id = $this->interfaceSettingInfo['search_column_id'];
  137. $field = $this->columnIdToCodes[$search_column_id];
  138. list($find) = $this->queryEntity();
  139. return $find->avg($field);
  140. }
  141. public function queryMax(): int
  142. {
  143. $search_column_id = $this->interfaceSettingInfo['search_column_id'];
  144. $field = $this->columnIdToCodes[$search_column_id];
  145. list($find) = $this->queryEntity();
  146. return $find->max($field);
  147. }
  148. public function queryMin(): int
  149. {
  150. $search_column_id = $this->interfaceSettingInfo['search_column_id'];
  151. $field = $this->columnIdToCodes[$search_column_id];
  152. list($find) = $this->queryEntity();
  153. return $find->min($field);
  154. }
  155. /**
  156. * 通过id 获取信息.
  157. */
  158. public function queryEntity()
  159. {
  160. $infoObj = DB::connection($this->getDbConnection())->table($this->table, 'A');
  161. // 获取需要显示的字段
  162. if ($selectColumns = $this->getSelectFields()) {
  163. $infoObj->select($selectColumns);
  164. }
  165. // 查询条件
  166. if ($this->conditionSettingsInfo) {
  167. foreach ($this->conditionSettingsInfo as $row) {
  168. if (1 == $row['type']) { // 普通查询
  169. $this->execSearchCon($infoObj, $row);
  170. } else { // 混合查询
  171. $this->checkExpression('search_type', $this->requestData, $row['condition'], $infoObj);
  172. }
  173. }
  174. }
  175. // 去除伪删除数据
  176. if (isset($this->requestData['is_delete'])) {
  177. $infoObj->where('is_delete', 0);
  178. }
  179. // 排序
  180. $this->sortFields($infoObj);
  181. $toArrColumns = []; // 需要json转为数组的字段
  182. return [$infoObj, $toArrColumns];
  183. }
  184. /**
  185. * 获取部分字段.
  186. *
  187. * @param $find
  188. */
  189. private function selectFields($find): void
  190. {
  191. $column_only_one = $this->interfaceResultInfo['column_only_one'] ?? false; // 是否直接返回单个字段
  192. $needFiledIds = $this->interfaceResultInfo['columns'];
  193. if ($column_only_one) {
  194. $find->select($this->columnIdToCodes[$needFiledIds]);
  195. } else {
  196. if ($needFiledIds) { // 将id,转为字段
  197. $showColumnsArr = [];
  198. foreach ($needFiledIds as $column_id) {
  199. if (isset($this->columnIdToCodes[$column_id])) {
  200. $showColumnsArr[] = $this->columnIdToCodes[$column_id];
  201. }
  202. }
  203. $find->select($showColumnsArr);
  204. }
  205. }
  206. }
  207. /**
  208. * 排序规则.
  209. *
  210. * @param $find
  211. */
  212. public function sortFields($find): void
  213. {
  214. $sortSettings = $this->interfaceInfo['sort_settings'] ? json_decode($this->interfaceInfo['sort_settings'], true) : []; // 排序规则
  215. if ($sortSettings) {
  216. foreach ($sortSettings as $row) {
  217. $column = $this->columnIdToCodes[$row['column_id']];
  218. $value = $this->requestData[$row['paramDirection']];
  219. $value = strtolower($value);
  220. if (\in_array($value, ['asc', 'desc'], true)) {
  221. $find->orderBy($column, $value);
  222. }
  223. }
  224. } else {
  225. $find->orderBy('id', 'desc');
  226. }
  227. }
  228. }