Hotrank.php 10.5 KB
<?php

namespace Product;

use WebPlugin\Images;
use WebPlugin\Helpers;
use LibModels\Web\Product\HotrankData;
use LibModels\Web\Product\SearchData;
use LibModels\Web\Home\IndexData;
use WebPlugin\DataProcess\Channel as ChannelProcess;

/*
 * Hotrank页数据模型
 */

class HotrankModel
{

    /**
     * 获取频道资源
     *                       
     * @return array
     */
    public static function getChannelResource()
    {
        $channel = isset($_COOKIE['_Channel']) ? $_COOKIE['_Channel'] : 'boys';
        switch ($channel) {
            case 'boys' :
                $resource = array(
                    'channel' => 'boys',
                    'gender' => '1,3',
                    'road' => 1,
                    'code' => '80d772d8dff25300a2a2e4c97165330c');
                return $resource;
            case 'girls' :
                $resource = array(
                    'channel' => 'girls',
                    'gender' => '2,3',
                    'road' => 2,
                    'code' => '8df64e505e94edb9881fd1e7efb702e9');
                return $resource;
            case 'lifestyle' :
                $resource = array(
                    'channel' => 'lifestyle',
                    'gender' => '',
                    'road' => 4,
                    'code' => 'd131aba83a84a6977eee3a7403a713de');
                return $resource;
            case 'kids' :
                $resource = array(
                    'channel' => 'kids',
                    'gender' => '',
                    'road' => 3,
                    'code' => 'bd6a06a08f8ca9b0db762f78e0bc5b68');
                return $resource;
        }
    }

    /**
     * 获取热销排行所有资源
     * @param $data 频道信息 
     * @param $sid 一周热卖分类id
     * @param $page 页码                    
     * @return array
     */
    public static function getHotrankResource($data, $sid, $page)
    {
        //焦点图 热门品牌
        $focus = self::getFocusResource($data['channel'], $data['code']);
        $data['slide'] = $focus[0]['slide'];
        //焦点图只有一张是不显示切换图片按钮
        //$iconfont = count($data['slide']['list']) == 1 ? false : true;
        $data['hotBrands'] = $focus[1]['hotBrands'];
        //人气单品 一周热卖
        $sort = ($sid == 1) ? '' : $sid;
        $config = array(
            'sort' => $sort,
            'gender' => $data['gender'],
            'road' => $data['road']);
        $publiclist = self::getSearchData($config, $page, 0);
        $data['popular'] = $publiclist['popular'];
        $data['hotWeek'] = $publiclist['hotWeek'];
        //分类标签
        $nav = self::getHotrankTag('web', $data, $page, true);
        $data['hotWeek']['nav'] = $nav;
        return $data;
    }

    /**
     * 一周热卖加载更多
     * @param $data 频道信息 
     * @param $sid 一周热卖分类id
     * @param $page 页码  
     */
    public static function getListData($data, $sid, $page)
    {
        $sort = '';
        //是否是默认标签
        if ($sid > 1) {
            //通过ID获取标签信息
            $info = HotrankData::getTagById($sid, false);
            if (!empty($info['data'])) {
                $sort = $info['data']['category_id'];
            }
        }
        $config = array(
            'sort' => $sort,
            'gender' => $data['gender'],
            'road' => $data['road']);
        $list = self::getSearchData($config, $page, 1);
        if (!empty($list)) {
            $lister = array_values($list['hotWeek']['list']);
            if ($sid == 1 && $page == 1) {
                $lister = array_slice($lister, 10, 50); //排除十个“人气单品”的商品
            }
            else {
                $lister = array_slice($lister, 0, 50);
            }
            $data = array(
                'code' => 200,
                'data' => $lister,);
            return $data;
        }
    }

    /**
     * 获取焦点图,热门品牌资源
     * @param string $channel            
     * @param string $content_code            
     * @return array
     */
    public static function getFocusResource($channel, $content_code)
    {
        $resource = IndexData::getResourceData($content_code);
        // 格式化数据
        $data = ChannelProcess::getFormat($channel, $resource['data']);
        return $data;
    }

    /**
     * 获取人气单品,一周热卖商品资源
     * @param $config 过滤参数
     * @param $page 当前页数
     * @param $ajax 是否是ajax加载  默认0不是
     */
    public static function getSearchData($config, $page, $ajax)
    {
        //配置查询商品参数
        $param = array(
            'order' => 's_n_desc',
            'viewNum' => 60,
            'page' => $page,
            'stocknumber' => 1,
            'status' => 1,
            'sort' => $config['sort'],
            'gender' => $config['gender'],
            'attribute_not' => 2);
        if ($ajax == 0 || ($ajax == 1 && empty($param['sort']))) {
            switch ($config['road']) {
                case 3 :
                    $param['msort'] = 365;
                    break;
                case 4 :
                    $param['msort'] = 10;
                    break;
                default :
                    $param['not_maxSortId'] = '10,365';
            }
        }
        //根据给定查询数据搜索数据列表
        $result = SearchData::searchElasticByCondition($param);
        //人气单品,一周热卖数据处理
        $data = self::getProductList($result, $page, $ajax);
        return $data;
    }

    /**
     * 获取标签资源
     * @param $client_type 客户端类型
     * @param $channel 频道类型
     * @param $is_concurrent 是否异步,true异步,false同步,默认true
     */
    public static function getHotrankTag($client_type, $data, $page, $is_concurrent)
    {
        $list = HotrankData::getHotrankTag($client_type, $data['road'], $is_concurrent);
        $nav = array();
        if (!empty($list['data']['list'])) {
            $i = 0;
            foreach ($list['data']['list'] as $lt) {
                $sort = $lt['category_id'];
                $config = array(
                    'sort' => $sort,
                    'gender' => $data['gender'],
                    'road' => $data['road']);
                $list = self::getSearchData($config, $page, 1);
                //判断商品分类下是否有商品,没有则页面不展示
                if (isset($list['hotWeek']['list']) && !empty($list['hotWeek']['list'])) {
                    $nav[$i]['sid'] = $lt['id'];
                    $nav[$i]['textCn'] = $lt['tag_name'];
                    $i = $i + 1;
                }
            }
        }
        return $nav;
    }

    /**
     * 人气单品  一周热卖  数据处理
     * @param $result 过滤参数
     * @param $page 当前页数
     * @param $ajax 是否是ajax加载  默认0不是
     */
    public static function getProductList($result, $page, $ajax = 0)
    {
        $product = array();
        if (isset($result) && isset($result['data']) && isset($result['data']['product_list'])) {
            $data = array(
                'popular' => array(
                    'name' => '人气单品',
                    'list' => array(),
                ),
                'hotWeek' => array(
                    'name' => '一周热卖',
                    'list' => array(),
                )
            );
            $url = Helpers::url('/product/pro', '', 'item');
            foreach ($result['data']['product_list'] as $key => $val) {
                //过滤掉商品列表为空和下架商品
                if (empty($val['goods_list']) || $val['status'] == 0) {
                    continue;
                }
                //配置商品图片超链接路径
                $defaultGoodsId = 0;
                foreach ($val['goods_list'] as $v) {
                    if ($v['status'] == 0) {
                        continue;
                    }
                    if ($v['is_default'] == 'Y') {
                        $defaultGoodsId = empty($v['goods_id']) ? $val['goods_list'][0]['goods_id'] : $v['goods_id'];
                    }
                }
                $product_id = empty($val['product_id']) ? '' : $val['product_id'];
                if ($key <= 9 && $page == 1 && $ajax == 0) {
                    //人气单品
                    $product['rank'] = $key + 1;
                    $product['title'] = $val['product_name'];
                    $product['href'] = $url . '_' . $product_id . '_' . $defaultGoodsId . '.html';
                    $product['price'] = (int) $val['market_price'] == (int) $val['sales_price'] ? false : $val['market_price'];
                    $product['sPrice'] = empty($val['sales_price']) ? false : $val['sales_price'];
                    if ($key <= 5) {
                        //6张大图
                        $product['img'] = empty($val['default_images']) ? Images::getImageUrl($val['goods_list'][0]['images_url'], 280, 373, 2) : Images::getImageUrl($val['default_images'], 378, 504, 2);
                        $data['popular']['list'][] = $product;
                    }
                    else {
                        //4张小图
                        $product['img'] = empty($val['default_images']) ? Images::getImageUrl($val['goods_list'][0]['images_url'], 280, 373, 2) : Images::getImageUrl($val['default_images'], 280, 373, 2);
                        $data['popular']['list'][] = $product;
                    }
                }
                else {
                    //一周热卖
                    $product['name'] = $val['product_name'];
                    $product['url'] = $url . '_' . $product_id . '_' . $defaultGoodsId . '.html';
                    $product['marketPrice'] = (int) $val['market_price'] == (int) $val['sales_price'] ? false : $val['market_price'];
                    $product['salePrice'] = empty($val['sales_price']) ? false : $val['sales_price'];
                    $product['thumb'] = empty($val['default_images']) ? Images::getImageUrl($val['goods_list'][0]['images_url'], 280, 373, 2) : Images::getImageUrl($val['default_images'], 280, 373, 2);
                    $data['hotWeek']['list'][] = $product;
                }
            }
        }
        return $data;
    }

}