ListsSearchTrait.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\lists;
  15. trait ListsSearchTrait
  16. {
  17. protected array $params;
  18. protected $searchWhere = [];
  19. /**
  20. * @notes 搜索条件生成
  21. * @param $search
  22. * @return array
  23. * @author 令狐冲
  24. * @date 2021/7/7 19:36
  25. */
  26. private function createWhere($search)
  27. {
  28. if (empty($search)) {
  29. return [];
  30. }
  31. $where = [];
  32. foreach ($search as $whereType => $whereFields) {
  33. switch ($whereType) {
  34. case '=':
  35. case '<>':
  36. case '>':
  37. case '>=':
  38. case '<':
  39. case '<=':
  40. case 'in':
  41. foreach ($whereFields as $whereField) {
  42. $paramsName = substr_symbol_behind($whereField);
  43. if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
  44. continue;
  45. }
  46. $where[] = [$whereField, $whereType, $this->params[$paramsName]];
  47. }
  48. break;
  49. case '%like%':
  50. foreach ($whereFields as $whereField) {
  51. $paramsName = substr_symbol_behind($whereField);
  52. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  53. continue;
  54. }
  55. $where[] = [$whereField, 'like', '%' . $this->params[$paramsName] . '%'];
  56. }
  57. break;
  58. case '%like':
  59. foreach ($whereFields as $whereField) {
  60. $paramsName = substr_symbol_behind($whereField);
  61. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  62. continue;
  63. }
  64. $where[] = [$whereField, 'like', '%' . $this->params[$paramsName]];
  65. }
  66. break;
  67. case 'like%':
  68. foreach ($whereFields as $whereField) {
  69. $paramsName = substr_symbol_behind($whereField);
  70. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  71. continue;
  72. }
  73. $where[] = [$whereField, 'like', $this->params[$paramsName] . '%'];
  74. }
  75. break;
  76. case 'between_time':
  77. if (!is_numeric($this->startTime) || !is_numeric($this->endTime)) {
  78. break;
  79. }
  80. $where[] = [$whereFields, 'between', [$this->startTime, $this->endTime]];
  81. break;
  82. case 'between':
  83. if (empty($this->start) || empty($this->end)) {
  84. break;
  85. }
  86. $where[] = [$whereFields, 'between', [$this->start, $this->end]];
  87. break;
  88. case 'find_in_set': // find_in_set查询
  89. foreach ($whereFields as $whereField) {
  90. $paramsName = substr_symbol_behind($whereField);
  91. if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
  92. continue;
  93. }
  94. $where[] = [$whereField, 'find in set', $this->params[$paramsName]];
  95. }
  96. break;
  97. }
  98. }
  99. return $where;
  100. }
  101. }