Plusstar.php 5.66 KB
<?php

namespace Guang;

use Configs\CacheConfig;
use LibModels\Wap\Guang\PlusstarData;
use Plugin\Helpers;
use Plugin\Cache;

/**
 * 明星原创品牌相关的模板数据模型
 * 
 * @name PlusstarModel
 * @package models/Guang
 * @copyright yoho.inc
 * @version 1.0 (2015-10-23 15:30:00)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class PlusstarModel
{

    /**
     * 获取国际优选的品牌列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @return array
     */
    public static function getFirstBrands($gender)
    {
        $channel = '1';
        switch (strval($gender)) {
            case '1,3': // 男
                $channel = '1';
                break;
            case '2,3': // 女
                $channel = '2';
                break;
            default:     // 默认
                $gender = '1,3';
                $channel = '1';
                break;
        }

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

        $result = array();
        
        $brandList = PlusstarData::firstBrandList($gender, $channel);
        // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
        if (empty($brandList['recom']['data']['list'][0]['data']) || empty($brandList['all']['data']['list'][0]['data'])) {
            if (USE_CACHE) {
                $result = Cache::get($key, 'slave');
            }
            return $result;
        }
        
        $result['ps']['star'] = self::formatData($brandList['recom']['data']['list'][0]['data'], $gender, true);
        $result['ps']['plus'] = self::formatData($brandList['all']['data']['list'][0]['data'], $gender, true);

        // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
        if (USE_CACHE) {
            Cache::set($key, $result);
        }

        return $result;
    }

    /**
     * 获取明星原创的品牌列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @return array
     */
    public static function getBrands($gender)
    {
        $channel = '1';
        switch (strval($gender)) {
            case '1,3': // 男
                $channel = '1';
                break;
            case '2,3': // 女
                $channel = '2';
                break;
            default:     // 默认
                $gender = '1,3';
                $channel = '1';
                break;
        }

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

        $result = array();
        $brandList = PlusstarData::brandList($gender, $channel);
        if (!empty($brandList['star']['data']['list'][0]['data'])) {
            $result['ps']['star'] = self::formatData($brandList['star']['data']['list'][0]['data'], $gender, false);
            //$result['ps']['pName'] = isset($brandList['star']['data']['brand_type_name']) ? $brandList['star']['data']['brand_type_name'] : '';
        }
        if (!empty($brandList['original']['data']['list'][0]['data'])) {
            $result['ps']['plus'] = self::formatData($brandList['original']['data']['list'][0]['data'], $gender, true);
            //$result['ps']['sName'] = isset($brandList['original']['data']['brand_type_name']) ? $brandList['original']['data']['brand_type_name'] : '';
        }

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

        return $result;
    }

    /**
     * 格式化成模板数据格式
     * 
     * @param array $data 源数据
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param bool $showDesciption 控制是否显示摘要内容
     * @return array
     */
    private static function formatData($data, $gender, $showSummary = true)
    {
        $result = array();

        $build = array();
        $imgs = array();
        foreach ($data as $value) {
            $build = array();
            // 情况1: 多张图
            if (isset($value['data'][1])) {
                $imgs = array();
                foreach ($value['data'] as $ps) {
                    $build['url'] = Helpers::url('/plustar/brandinfo', array('id' => $value['id'], 'gender' => $gender), 'guang');
                    $build['img'] = Helpers::getImageUrl($ps['src'], 640, 310);
                    $build['deps'] = $showSummary ? $value['brand_title'] : false;
                    $imgs[] = $build;
                }
                $result[] = array('imgs' => $imgs);
            }
            // 情况2: 单张图
            elseif (isset($value['data'][0])) {
                $build['url'] = Helpers::url('/plustar/brandinfo', array('id' => $value['id'], 'gender' => $gender), 'guang');
                $build['img'] = Helpers::getImageUrl($value['data'][0]['src'], 640, 310);
                $build['deps'] = $showSummary ? $value['brand_title'] : false;
                $result[] = $build;
            }
        }

        return $result;
    }

}