Class.php 4.72 KB
<?php

namespace Category;

use LibModels\Wap\Category\ClassData;
use Configs\CacheConfig;
use Plugin\Helpers;
use Plugin\Cache;

/**
 * 品牌相关的模板数据模型
 * 
 * @name BrandModel
 * @package Models/Category
 * @copyright yoho.inc
 * @version 1.0 (2015-10-21 13:52:44)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class ClassModel
{

    /**
     * 根据频道获取品类数据
     * 
     * @return array
     */
    public static function getClassData()
    {
        $result = array();

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

        do {
            $data = ClassData::getClassesData();
            if (empty($data['data'])) {
                break;
            }

            $genderArr = array('boy' => '1,3', 'girl' => '2,3', 'kids' => '1,2,3', 'lifestyle' => '1,2,3');
            $oneClass = array();
            $item = array();
            foreach ($data['data'] as $k => $v) {
                $oneClass = array('name' => ucfirst(rtrim($k, 's')), 'ca' => array());

                foreach ($v as $val) {
                    $item = array();
                    $item['name'] = $val['category_name'];
                    $item['id'] = $val['relation_parameter']['sort'];

                    // 首先添加一级分类
                    $item['sub'][] = array(
                        'name' => '全部' . $item['name'],
                        'id' => $item['id'],
                        'url' => Helpers::url('/', array(
                            'sort' => $item['id'],
                            'sort_name' => $item['name'],
                            'gender' => $genderArr[$k]), 'list')
                    );

                    $subitem = array();
                    foreach ($val['sub'] as $value) {
                        $subitem = array();
                        $subitem['name'] = $value['category_name'];
                        $subitem['id'] = $value['relation_parameter']['sort'];
                        $subitem['url'] = Helpers::url('/', array(
                                'sort' => $value['relation_parameter']['sort'],
                                'sort_name' => $value['category_name'],
                                'gender' => $genderArr[$k]
                                ), 'list');
                        $item['sub'][] = $subitem;
                    }

                    $oneClass['ca'][] = $item;
                }

                $result[] = $oneClass;
            }
        } while (false);

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

        return $result;
    }

    /**
     * 根据频道获取所有品类名称
     * 
     * @return array(
     *     "first" => '一级品类',
     *     "second" => '二级品类',
     * )
     */
    public static function getClassNames()
    {
        $result = array('first' => array(), 'second' => array());

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

        $classes = ClassData::getClassesData();
        if (!empty($classes['data'])) {
            $id = 0;
            foreach ($classes['data'] as $data) {
                foreach ($data as $item) {
                    $id = $item['category_id'];
                    $result['first'][$id] = $item['category_name'];
                    foreach ($item['sub'] as $sub) {
                        $id = $sub['category_id'];
                        $result['second'][$id] = $sub['category_name'];
                    }
                }
            }
        }
        $classes = array();

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

        return $result;
    }

}