<?php
namespace common\helpers;
use Yii;

class MongodbSearchFilter {

    const SYMBOL_MAP = [
        '>'    => [
            'label' => '大于',
            'sql'   => '$gt',
        ],
        '>='   => [
            'label' => '大于等于',
            'sql'   => '$gte',
        ],
        '<'    => [
            'label' => '小于',
            'sql'   => '$lt',
        ],
        '<='   => [
            'label' => '小于等于',
            'sql'   => '$lte',
        ],
        '<>'   => [
            'label' => '不等于',
            'sql'   => '$ne',
        ],
    ];

    /**
     * 筛选条件
     * @param array $tableParams
     * @return array
     */
    public static function filterCondition(array $tableParams = []){
        $allGet = Yii::$app->request->get();
        $condition = [];
        foreach ($tableParams as $getParam => $tableField) {
            if (isset($allGet[$getParam]) && $allGet[$getParam]) {
                $getValue = trim($allGet[$getParam], ", \t\n\r\0\x0B");
                if (strpos($getValue, '|') > 0) {
                    $childValueArr = explode('|', $getValue);
                    $condition['$or'] = [];
                    foreach ($childValueArr as $k => $value) {
                        $result = self::getConditionAndParams($value, $tableField);
                        $condition['$or'][] = $result;
                    }
                } else {
                    $result = self::getConditionAndParams($getValue, $tableField);
                    $condition += $result;
                }
            }
        }
        return [
            'condition' => $condition,
            'request' => $allGet,
        ];
    }

    /**
     * 处理筛选条件
     * @param $getValue
     * @param $tableField
     * @return array
     */
    public static function getConditionAndParams($getValue, $tableField) {
        $condition = [];
        $isDate = false;
        $filterModel = '';
        if (strpos($getValue, ',') > 0) {
            $getValueArr = explode(',', $getValue);
            $getSymbol = strtoupper($getValueArr[0]);
            if ($getSymbol == 'IN') {
                $bindValueArr = $getValueArr;
                unset($bindValueArr[0]);
                $bindValue = implode("','", $bindValueArr);
                $bindValue = "'$bindValue'";
            } else {
                $bindValue = $getValueArr[1];
                $filterModel = end($getValueArr);
                reset($getValueArr);
                if($filterModel == 'date'){
                    $bindValue = strtotime($getValueArr[1]);
                    $isDate = true;
                }
                elseif($filterModel == 'area'){
                    $bindValue = array_slice($getValueArr, 1, 3);
                }
            }
        } else {
            $getSymbol = '=';
            $bindValue = $getValue;
        }
        if ($getSymbol == 'LIKE') {
            $condition = [
                $tableField => ['$regex' => $bindValue],
            ];
        } elseif ($getSymbol == 'NOTLIKE') {
            $condition = [
                $tableField => ['$regex' => '^((?!'.$bindValue.').)*$'],
            ];
        } else {
            if ($isDate && $getSymbol == '=') {
                $condition = [
                    $tableField => [
                        self::SYMBOL_MAP['>=']['sql'] => floatval($bindValue),
                        self::SYMBOL_MAP['<=']['sql'] => floatval($bindValue + 86399)
                    ],
                ];
            }
            elseif($filterModel == 'area'){
                if($bindValue[0]){
                    $condition = [
                        $tableField['FIELD'][0] => $bindValue[0],
                    ];
                    if($bindValue[1]){
                        $condition += [
                            $tableField['FIELD'][1] => $bindValue[1],
                        ];
                        if($bindValue[2]){
                            $condition += [
                                $tableField['FIELD'][2] => $bindValue[2],
                            ];
                        }
                    }
                }
            }
            elseif($getSymbol == '=') {
                $condition = [
                    $tableField => $bindValue,
                ];
            }
            else {
                $condition = [
                    $tableField => [self::SYMBOL_MAP[$getSymbol]['sql'] => floatval($bindValue)],
                ];
            }
        }
        return $condition;
    }

}