MongodbSearchFilter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. class MongodbSearchFilter {
  5. const SYMBOL_MAP = [
  6. '>' => [
  7. 'label' => '大于',
  8. 'sql' => '$gt',
  9. ],
  10. '>=' => [
  11. 'label' => '大于等于',
  12. 'sql' => '$gte',
  13. ],
  14. '<' => [
  15. 'label' => '小于',
  16. 'sql' => '$lt',
  17. ],
  18. '<=' => [
  19. 'label' => '小于等于',
  20. 'sql' => '$lte',
  21. ],
  22. '<>' => [
  23. 'label' => '不等于',
  24. 'sql' => '$ne',
  25. ],
  26. ];
  27. /**
  28. * 筛选条件
  29. * @param array $tableParams
  30. * @return array
  31. */
  32. public static function filterCondition(array $tableParams = []){
  33. $allGet = Yii::$app->request->get();
  34. $condition = [];
  35. foreach ($tableParams as $getParam => $tableField) {
  36. if (isset($allGet[$getParam]) && $allGet[$getParam]) {
  37. $getValue = trim($allGet[$getParam], ", \t\n\r\0\x0B");
  38. if (strpos($getValue, '|') > 0) {
  39. $childValueArr = explode('|', $getValue);
  40. $condition['$or'] = [];
  41. foreach ($childValueArr as $k => $value) {
  42. $result = self::getConditionAndParams($value, $tableField);
  43. $condition['$or'][] = $result;
  44. }
  45. } else {
  46. $result = self::getConditionAndParams($getValue, $tableField);
  47. $condition += $result;
  48. }
  49. }
  50. }
  51. return [
  52. 'condition' => $condition,
  53. 'request' => $allGet,
  54. ];
  55. }
  56. /**
  57. * 处理筛选条件
  58. * @param $getValue
  59. * @param $tableField
  60. * @return array
  61. */
  62. public static function getConditionAndParams($getValue, $tableField) {
  63. $condition = [];
  64. $isDate = false;
  65. $filterModel = '';
  66. if (strpos($getValue, ',') > 0) {
  67. $getValueArr = explode(',', $getValue);
  68. $getSymbol = strtoupper($getValueArr[0]);
  69. if ($getSymbol == 'IN') {
  70. $bindValueArr = $getValueArr;
  71. unset($bindValueArr[0]);
  72. $bindValue = implode("','", $bindValueArr);
  73. $bindValue = "'$bindValue'";
  74. } else {
  75. $bindValue = $getValueArr[1];
  76. $filterModel = end($getValueArr);
  77. reset($getValueArr);
  78. if($filterModel == 'date'){
  79. $bindValue = strtotime($getValueArr[1]);
  80. $isDate = true;
  81. }
  82. elseif($filterModel == 'area'){
  83. $bindValue = array_slice($getValueArr, 1, 3);
  84. }
  85. }
  86. } else {
  87. $getSymbol = '=';
  88. $bindValue = $getValue;
  89. }
  90. if ($getSymbol == 'LIKE') {
  91. $condition = [
  92. $tableField => ['$regex' => $bindValue],
  93. ];
  94. } elseif ($getSymbol == 'NOTLIKE') {
  95. $condition = [
  96. $tableField => ['$regex' => '^((?!'.$bindValue.').)*$'],
  97. ];
  98. } else {
  99. if ($isDate && $getSymbol == '=') {
  100. $condition = [
  101. $tableField => [
  102. self::SYMBOL_MAP['>=']['sql'] => floatval($bindValue),
  103. self::SYMBOL_MAP['<=']['sql'] => floatval($bindValue + 86399)
  104. ],
  105. ];
  106. }
  107. elseif($filterModel == 'area'){
  108. if($bindValue[0]){
  109. $condition = [
  110. $tableField['FIELD'][0] => $bindValue[0],
  111. ];
  112. if($bindValue[1]){
  113. $condition += [
  114. $tableField['FIELD'][1] => $bindValue[1],
  115. ];
  116. if($bindValue[2]){
  117. $condition += [
  118. $tableField['FIELD'][2] => $bindValue[2],
  119. ];
  120. }
  121. }
  122. }
  123. }
  124. elseif($getSymbol == '=') {
  125. $condition = [
  126. $tableField => $bindValue,
  127. ];
  128. }
  129. else {
  130. $condition = [
  131. $tableField => [self::SYMBOL_MAP[$getSymbol]['sql'] => floatval($bindValue)],
  132. ];
  133. }
  134. }
  135. return $condition;
  136. }
  137. }