Search.php 14.1 KB
<?php

namespace Product;

use Configs\CacheConfig;
use LibModels\Wap\Product\SearchData;
use LibModels\Wap\Category\BrandData;
use LibModels\Wap\Category\ClassData;
use Plugin\DataProcess\ListProcess;
use Plugin\Cache;
use Plugin\Helpers;

/**
 * 搜索相关的模板数据模型
 *
 * @name Search
 * @package Product
 * @copyright yoho.inc
 * @version 1.0 (2015-11-1 14:35:52)
 */
class SearchModel
{

    /**
     * 获取搜索的数据
     *
     * @param array $condition 查询条件
     * @return array
     */
    public static function getSearchData($condition, $showTag = true, $tagNew = true, $tagSale = true)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_SEARCH_SEARCH;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口查询数据 (使用新的 Elastic Engine)
        $listData = SearchData::searchElasticByCondition($condition);
        // 调用接口查询数据 (使用老的 Sphinx Engine)
        // $listData = SearchData::searchByCondition($condition);
        // 处理返回的数据
        if (isset($listData['code']) && $listData['code'] === 200) {
            $result = ListProcess::getListData($listData['data'], false, $showTag, $tagNew, $tagSale);
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            } // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 300); // 缓存5分钟
            }
        }

        return $result;
    }

    /**
     * 获取筛选的数据
     *
     * @param array $condition 查询条件
     * @return array
     */
    public static function getFilterData($condition)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_SEARCH_FILTER;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 设置选定的gender
        $gender = '1,2,3';
        if (isset($condition['gender']) && !empty($condition['gender'])) {
            $gender = $condition['gender'];
        }

        // 区别各种列表页面的筛选数据
        if (isset($condition['shop_id'])) {
            $listData = SearchData::searchByCondition($condition);
            $exclude = 'null';
        } elseif (isset($condition['brand'])) {
            $listData = BrandData::filterBrandData($condition);
            $exclude = 'brand';
        } else if (isset($condition['sort'])) {
            $listData = ClassData::filterClassData($condition);
            $exclude = 'group_sort';
        } else if (isset($condition['filter_poolId'])) {
            $listData = SearchData::getFilter($condition);
            $exclude = null;
        } else if (isset($condition['students'])) {
            $listData = ClassData::filterStudentsData($condition);
            $exclude = 'students';
        } else {
            $listData = SearchData::searchByCondition($condition);
            $exclude = null;
        }

        if (isset($listData['data']['filter'])) {
            $result['filter'] = ListProcess::getFilterData($listData['data']['filter'], $gender, $exclude);
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            } // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 300); // 缓存5分钟
            }
        }

        return $result;
    }

    /**
     * @param array $params 直接传筛选参数
     * @return array
     */
    public static function filter($params = array())
    {
        $data = array();

        if (empty($params)) {
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_DEFAULT,
                'shop_id' => FILTER_DEFAULT,
                'sort' => FILTER_DEFAULT,
                'msort' => FILTER_DEFAULT,
                'misort' => FILTER_DEFAULT,
                'color' => FILTER_DEFAULT,
                'size' => FILTER_DEFAULT,
                'style' => FILTER_DEFAULT,
                'price' => FILTER_DEFAULT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
                'channel' => FILTER_DEFAULT,
                'ageLevel' => FILTER_DEFAULT,
                'p_d' => FILTER_DEFAULT,
                'students' => FILTER_DEFAULT,
                'filter_poolId' => FILTER_DEFAULT
            ), false);

            // 转义分类
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
            // 转义分类
            if (isset($condition['msort'])) {
                $condition['msort'] = rawurldecode($condition['msort']);
            }
            // 转义分类
            if (isset($condition['misort'])) {
                $condition['misort'] = rawurldecode($condition['misort']);
            }
            // 转义颜色
            if (isset($condition['color'])) {
                $condition['color'] = rawurldecode($condition['color']);
            }
            //
            if (isset($condition['ageLevel'])) {
                $condition['age_level'] = rawurldecode($condition['ageLevel']);
                unset($condition['ageLevel']);
            }
            // 为了兼容现在运营在用的p_d
            if (isset($condition['p_d'])) {
                $condition['p_d'] = rawurldecode($condition['p_d']);
            } // 转换折扣
            elseif (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
            // 转义性别
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }
            // 转义价格
            if (isset($condition['price'])) {
                $condition['price'] = rawurldecode($condition['price']);
            }
            // 转义风格
            if (isset($condition['style'])) {
                $condition['style'] = rawurldecode($condition['style']);
            }

        } else {
            $condition = $params;
        }

        // 区别各种列表页面的筛选数据
        $data = self::getFilterData($condition);

        return $data;
    }

    /**
     * @param array $params
     * @return array
     */
    public static function search($params = array())
    {
        do {

            /* 如果参数为空是 Ajax 调用 */
            if (empty($params)) {
                /* Ajax 调用过滤请求参数 */
                $condition = filter_input_array(INPUT_GET, array(
                    'shop_id' => FILTER_DEFAULT,
                    'query' => FILTER_DEFAULT,
                    'specialsale_id' => FILTER_DEFAULT,
                    'promotion' => FILTER_DEFAULT,
                    'brand' => FILTER_DEFAULT,
                    'sort' => FILTER_DEFAULT,
                    'msort' => FILTER_DEFAULT,
                    'misort' => FILTER_DEFAULT,
                    'color' => FILTER_DEFAULT,
                    'size' => FILTER_DEFAULT,
                    'style' => FILTER_DEFAULT,
                    'price' => FILTER_DEFAULT,
                    'discount' => FILTER_DEFAULT,
                    'gender' => FILTER_DEFAULT,
                    'channel' => FILTER_DEFAULT,
                    'p_d' => FILTER_DEFAULT,
                    'outlets' => FILTER_DEFAULT,
                    'age_level' => FILTER_DEFAULT,
                    'page' => FILTER_VALIDATE_INT,
                    'filter_poolId' => FILTER_DEFAULT,
                    'showTag' => FILTER_DEFAULT,
                    'tagNew' => FILTER_DEFAULT,
                    'students' => FILTER_DEFAULT,
                    'tagSale' => FILTER_DEFAULT), false);


                if (!empty($condition['shop_id'])) {
                    $condition['shop'] = $condition['shop_id'];
                }
                // 转义分类
                if (isset($condition['sort'])) {
                    $condition['sort'] = rawurldecode($condition['sort']);
                }
                // 转义分类
                if (isset($condition['msort'])) {
                    $condition['msort'] = rawurldecode($condition['msort']);
                }
                // 转义分类
                if (isset($condition['misort'])) {
                    $condition['misort'] = rawurldecode($condition['misort']);
                }
                // 转义活动ID
                if (isset($condition['specialsale_id'])) {
                    $condition['specialsale_id'] = rawurldecode($condition['specialsale_id']);
                }
                if (isset($condition['promotion'])) {
                    $condition['promotion'] = rawurldecode($condition['promotion']);
                }
                // 转义颜色
                if (isset($condition['color'])) {
                    $condition['color'] = rawurldecode($condition['color']);
                }
                // 转义价格
                if (isset($condition['price'])) {
                    $condition['price'] = rawurldecode($condition['price']);
                }
                // 转义风格
                if (isset($condition['style'])) {
                    $condition['style'] = rawurldecode($condition['style']);
                }
                // 为了兼容现在运营在用的p_d
                if (isset($condition['p_d'])) {
                    $condition['p_d'] = rawurldecode($condition['p_d']);
                } // 转换折扣
                elseif (isset($condition['discount'])) {
                    $condition['p_d'] = rawurldecode($condition['discount']);
                    unset($condition['discount']);
                }
                // 转义性别
                if (isset($condition['gender'])) {
                    $condition['gender'] = rawurldecode($condition['gender']);
                }
                // 转义性别
                if (isset($condition['age_level'])) {
                    $condition['age_level'] = rawurldecode($condition['age_level']);
                }
                if (isset($condition['query'])) {
                    $condition['query'] = rawurldecode($condition['query']);
                }

                // 转义奥莱
                if (isset($condition['outlets'])) {
                    $condition['outlets'] = rawurldecode($condition['outlets']);
                }

                // 用户自定义商品类目
                if (isset($condition['filter_poolId'])) {
                    $condition['filter_poolId'] = rawurldecode($condition['filter_poolId']);
                }

                // 转换排序方式
//            $type = $this->get('type', '');
//            $order = $this->get('order', 0);
                $order = empty($_GET['order']) ? 0 : $_GET['order'];
                $condition['order'] = Helpers::transOrder($order, $_GET['type']);

                /* 如果参数不为空是直接调用 */
            } else {
                $condition = $params;
            }

            //标签显示控制
//            $showTag = $this->get('showTag', 0);
            $showTag = empty($condition['showTag']);
//            $showTag = $showTag === 0;
            //显示标签new
//            $tagNew = $this->get('tagNew', 0);
            $tagNew = empty($condition['tagNew']);
//            $tagNew = $tagNew === 0;
            //显示标签sale
            $tagSale = empty($condition['tagSale']);
//            $tagSale = $tagSale === 0;

            // 过滤掉值为空的条件
            $condition = array_filter($condition);

//            /* 模糊搜索关键词 */
//            if (isset($condition['query'])) {
//                $data = Product\SearchModel::getSearchData($condition);
//                break;
//            }
//
//            /* 精确搜索品牌 */
//            if (isset($condition['brand'])) {
//                $title = '';
//                $data = Product\ListModel::getBrandData($condition, $title);
//                break;
//            }
//
//            /* 精确搜索品类 */
//            $data = Product\ListModel::getClassData($condition);

            // 产品吴婷要求这么做,目的是保持性别中的所有性别和当前频道性别一致
            if (isset($condition['gender']) && isset($condition['channel']) && $condition['gender'] === '1,2,3') {
                switch ($condition['channel']) {
                    case 1:
                        $condition['gender'] = '1,3';
                        break;
                    case 2:
                        $condition['gender'] = '2,3';
                        break;
                    default:
                        break;
                }
            }

            $data = self::getSearchData($condition, $showTag, $tagNew, $tagSale);
        } while (false);

        //奥莱商品过滤新品标签
        if (isset($condition['outlets']) && $condition['outlets'] === '1') {
            foreach ($data['new'] as $outletskey => $outletsval) {
                unset($data['new'][$outletskey]['tags']);
            }
        }

        // 是否显示学生价
        if (isset($condition['students'])) {
            $data['students'] = true;
        }

        return $data;
    }

}