Search.php 3.95 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;

/**
 * 搜索相关的模板数据模型
 * 
 * @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'])) {
            $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 {
            $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;
    }

}