ListsSearchTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\lists;
  20. trait ListsSearchTrait
  21. {
  22. protected array $params;
  23. protected $searchWhere = [];
  24. /**
  25. * @notes 搜索条件生成
  26. * @param $search
  27. * @return array
  28. * @author 令狐冲
  29. * @date 2021/7/7 19:36
  30. */
  31. private function createWhere($search)
  32. {
  33. if (empty($search)) {
  34. return [];
  35. }
  36. $where = [];
  37. foreach ($search as $whereType => $whereFields) {
  38. switch ($whereType) {
  39. case '=':
  40. case '<>':
  41. case '>':
  42. case '>=':
  43. case '<':
  44. case '<=':
  45. case 'in':
  46. foreach ($whereFields as $whereField) {
  47. $paramsName = substr_symbol_behind($whereField);
  48. if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
  49. continue;
  50. }
  51. $where[] = [$whereField, $whereType, $this->params[$paramsName]];
  52. }
  53. break;
  54. case '%like%':
  55. foreach ($whereFields as $whereField) {
  56. $paramsName = substr_symbol_behind($whereField);
  57. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  58. continue;
  59. }
  60. $where[] = [$whereField, 'like', '%' . $this->params[$paramsName] . '%'];
  61. }
  62. break;
  63. case '%like':
  64. foreach ($whereFields as $whereField) {
  65. $paramsName = substr_symbol_behind($whereField);
  66. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  67. continue;
  68. }
  69. $where[] = [$whereField, 'like', '%' . $this->params[$paramsName]];
  70. }
  71. break;
  72. case 'like%':
  73. foreach ($whereFields as $whereField) {
  74. $paramsName = substr_symbol_behind($whereField);
  75. if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
  76. continue;
  77. }
  78. $where[] = [$whereField, 'like', $this->params[$paramsName]];
  79. }
  80. break;
  81. case 'between_time':
  82. if (!is_numeric($this->startTime) || !is_numeric($this->endTime)) {
  83. break;
  84. }
  85. $where[] = [$whereFields, 'between', [$this->startTime, $this->endTime]];
  86. break;
  87. case 'between':
  88. if (empty($this->start) || empty($this->end)) {
  89. break;
  90. }
  91. $where[] = [$whereFields, 'between', [$this->start, $this->end]];
  92. break;
  93. }
  94. }
  95. return $where;
  96. }
  97. }