Brands.php 9.95 KB
<?php

namespace Product;

use Api\Yohobuy;
use WebPlugin\HelperSearch;
use LibModels\Web\Product\BrandData;
use WebPlugin\Helpers;
use LibModels\Web\Product\SearchData;
use Configs\WebCacheConfig;
use WebPlugin\Cache;
use Index\HomeModel;

/**
 * 品牌首页模板数据模型
 *
 * @author Administrator
 */
class BrandsModel
{
    /**
     * 搜索品牌数据
     * @param $condition array(
     *      'brand' => int (品牌id,必传)
     *      'folder_id' => string (搜索品牌系列参数)
     * )
     * @param $options array(
     *      'brandName' => string (品牌域名)
            'uid' => int (用户uid)
            'brandId' => int (品牌id)
            'node' => string (左侧广告node)
            'brandBanner' => string (品牌banner)
            'brandNameEn' => string (品牌英文名)
            'brandNameCn' => string (品牌中文名)
            'reviewNum'  => int (底部浏览记录显示个数)
            'controller' => string (说明当前控制器名)
            'action' => string (说明当前方法名)
     * )   排序条件
     * @return array()  (处理后的品牌首页数据)
     */
    public static function getBrandSearchData($condition, $options)
    {
        //获取$condition和$option 筛选条件和排序条件
        $searchCondition = SearchModel::searchCondition($condition, $options);

        // 并行调用品牌相关接口并封装数据
        $result = self::getBrandData($searchCondition, $options);


        //获取水牌node
        $result['node'] = $options['node'];

        return $result;
    }

    /**
     * 品牌介绍页
     * @param $customOptions  array(
     *      'brandName' => string (品牌域名)
            'uid' => int (用户uid)
            'brandId' => int (品牌id)
            'brandAbout' => string (品牌简介)
            'brandBanner' => string (品牌banner)
            'brandNameEn' => string (品牌英文名)
            'brandNameCn' => string (品牌中文名)
     * )
     * @return array
     */
    public static function getBrandIntro($customOptions = array())
    {
        $urlList = array();
        if (USE_CACHE) {
            $key = WebCacheConfig::KEY_WEB_PRODUCT_INDEX_BRANDINTRO;
            if (!empty($customOptions['brandId'])) {
                $key .= $customOptions['brandId'];
            }
            if (!empty($customOptions['brandName'])) {
                $key .= $customOptions['brandName'];
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        //组合搜索品牌url
        $urlList['brand'] = SearchData::getBrandUrl($customOptions);
        //批量调接口
        $data = Yohobuy::getMulti($urlList, array(), true);
        $result = array();
        if (isset($data['brand']) && isset($customOptions['channel'])) {
            $banner = HelperSearch::getBannerFormat($data['brand'], $customOptions['brandBanner']);
            $channel = $customOptions['channel'];
            $brandNav = $customOptions['brandNameEn'] . ' ' . $customOptions['brandNameCn'];
            //根据频道判断home页地址
            switch ($channel) {
                case 'boys':
                    $home = Helpers::url('', '', 'default');
                    break;
                case 'girls':
                    $home = Helpers::url('/woman', '', 'new');
                    break;
                case 'lifestyle':
                    $home = Helpers::url('/lifestyle', '', 'new');
                    break;
                case 'kids':
                    $home = Helpers::url('/kids', '', 'new');
                    break;
                default:
                    $home = Helpers::url('', '', 'new');
                    break;
            }
            $result = array(
                'brandBanner' => $banner,
                'brandAbout' => $customOptions['brandAbout'],
                'pathNav' => array(
                    array(
                        'href' => $home,
                        'name' => strtoupper($channel) . '首页',
                        'title' => 'YOHO!有货'
                    ),
                    array(
                        'href' => Helpers::url('/brands'),
                        'name' => '品牌一览',
                        'title' => '品牌一览'
                    ),
                    array(
                        'name' => $brandNav
                    )
                )
            );
        }



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

        return $result;
    }


    /**
     * 根据品牌域名处理相关品牌参数
     * @param $domain (品牌域名)
     * @return array|bool
     */
    public static function getBrandByDomain($domain)
    {
        //调用接口获得数据
        $brandInfo = BrandData::getBrandLogoByDomain($domain);
        $result = array();
        //组装品牌页顶部banner条需要的数据
        if (!empty($brandInfo['data']) && $brandInfo['code'] == 200) {
            $result['brandId'] = isset($brandInfo['data']['id']) ? $brandInfo['data']['id'] : '';
            $result['node'] = isset($brandInfo['data']['static_content_code']) ? $brandInfo['data']['static_content_code'] : false;
            $result['brandBanner'] = isset($brandInfo['data']['brand_banner']) ? $brandInfo['data']['brand_banner'] : '';
            $result['brandNameEn'] = isset($brandInfo['data']['brand_name_en']) ? $brandInfo['data']['brand_name_en'] : '';
            $result['brandNameCn'] = isset($brandInfo['data']['brand_name_cn']) ? $brandInfo['data']['brand_name_cn'] : '';
            $result['brandAbout'] = isset($brandInfo['data']['brand_intro']) ? $brandInfo['data']['brand_intro'] : '';
            $result['shopTemplateType'] = isset($brandInfo['data']['shop_template_type']) ?
                intval($brandInfo['data']['shop_template_type']) : '';
            $result['type'] = isset($brandInfo['data']['type']) ? $brandInfo['data']['type'] * 1 : 0;
            $result['shopId'] = isset($brandInfo['data']['shop_id']) ? $brandInfo['data']['shop_id'] : '';
        }
        else {
            return false;
        }
        return $result;
    }


    /**
     * 获取品牌系列数据
     * @param $brandId  (品牌id)
     * @param int $status   (状态码)
     * @return array
     */
    public static function getAdNav($brandId, $status = 1)
    {
        $result = array();
        
        //调用接口获得品牌系列数据
        $series = BrandData::getSeries($brandId, $status);
        $advNav = BrandData::getFolderByBrand($brandId, $status);
        
        $index = 0;
        if (isset($series['data']) && $series['code'] === 200) {
            foreach ($series['data'] as $value) {
                $result['list'][$index]['href'] = '?series=' . $value['id'];
                $result['list'][$index]['src'] = $value['series_banner'];
                ++ $index;
            }
            //title设置为经典系列
            $result['picTitle'] = '经典系列';
        }

        if (isset($advNav['data']) && $advNav['code'] === 200) {
            foreach ($advNav['data'] as $value) {
                $result['list'][$index]['href'] = '?folder_id=' . $value['id'];
                $result['list'][$index]['src'] = $value['brand_sort_ico'];
                ++ $index;
            }
            //title设置为经典系列
            $result['picTitle'] = '经典系列';
        }

        return $result;
    }

    /**
     * 根据条件获取搜索品牌数据
     * @param $searchCondition array()    (经过过滤的、用户输入的一些数据)
     * @param $customOptions array(
     *       'brandName' => string (品牌域名)
     *       'uid' => int (用户uid)
     *       'brandId' => int (品牌id)
     *       'node' => string (左侧广告node)
     *       'brandBanner' => string (品牌banner)
     *       'brandNameEn' => string (品牌英文名)
     *       'brandNameCn' => string (品牌中文名)
     *       'reviewNum'  => int (底部浏览记录显示个数)
     *       'controller' => string (说明当前控制器名)
     *       'action' => string (说明当前方法名)
     * )
     * @return array()  (品牌首页数据,不包括系列数据、广告数据、banner数据)
     */
    public static function getBrandData($searchCondition = array(), $customOptions = array())
    {
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);

        // 组合搜索分类url
        $sortCondition = array();
        if (isset($searchCondition['condition']['misort']) && !empty($searchCondition['condition']['misort'])) {
            $sortCondition['needSmallSort'] = 1;
        }
        $sortCondition= array(
            'attribute_not' => $searchCondition['sortCondition']['attribute_not'],
            'brand' => $searchCondition['sortCondition']['brand']
        );
        $urlList['sort'] = SearchData::getClassesUrl($sortCondition);

        //组合搜索品牌url
        $urlList['brand'] = SearchData::getBrandUrl($customOptions);

        //批量调接口
        $data = Yohobuy::getMulti($urlList);

        //组织模板数据格式
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);

        return $result;
    }

}