<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/3/18
 * Time: 下午10:11
 */

/**
 * 前台品牌分类
 *
 *
 *
 ***/


defined('InShopNC') or exit('Access Invalid!');

class goodsControl extends mobileControl
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 单个商品信息页
     */
    public function indexOp()
    {
        $goods_id = intval($_GET['goods_id']);
        if (empty($_GET ['goods_id']) || $goods_id < 0) {
            return self::outerr(errcode::ErrParamter, "goods_id = {$goods_id} must be > 0.");
        }
        // 商品详细信息
        $model_goods = Model('goods');
        $goods_detail = $model_goods->getGoodsDetail($goods_id);
        $goods_info = $goods_detail['goods_info'];
        if (empty($goods_info)) {
            return self::outerr(errcode::ErrGoodsNotExist);
        }
        $rs = $model_goods->getGoodsList(array('goods_commonid' => $goods_info['goods_commonid']));
        $count = 0;
        foreach ($rs as $v) {
            $count += $v['goods_salenum'];
        }
        $goods_info['goods_salenum'] = $count;
        $model_store = Model('store');
        $store_info = $model_store->getStoreOnlineInfoByID($goods_info['store_id']);
        //  添加 end

        Tpl::output('spec_list', $goods_detail['spec_list']);
        Tpl::output('spec_image', $goods_detail['spec_image']);
        Tpl::output('goods_image', $goods_detail['goods_image']);
        Tpl::output('mansong_info', $goods_detail['mansong_info']);
        Tpl::output('gift_array', $goods_detail['gift_array']);

        // 生成缓存的键值
        $hash_key = $goods_info['goods_id'];
        $_cache = rcache($hash_key, 'product');
        if (empty($_cache)) { // 查询SNS中该商品的信息
            $snsgoodsinfo = Model('sns_goods')->getSNSGoodsInfo(array('snsgoods_goodsid' => $goods_info['goods_id']), 'snsgoods_likenum,snsgoods_sharenum');
            $data = array();
            $data['likenum'] = $snsgoodsinfo['snsgoods_likenum'];
            $data['sharenum'] = $snsgoodsinfo['snsgoods_sharenum'];
            // 缓存商品信息
            wcache($hash_key, $data, 'product');
        }
        $goods_info = array_merge($goods_info, $_cache);

        $inform_switch = true;
        // 检测商品是否下架,检查是否为店主本人
        if ($goods_info['goods_state'] != 1 || $goods_info['goods_verify'] != 1 || $goods_info['store_id'] == $_SESSION['store_id']) {
            $inform_switch = false;
        }
        Tpl::output('inform_switch', $inform_switch);

        // 如果使用运费模板
        if ($goods_info['transport_id'] > 0) {
            // 取得三种运送方式默认运费
            $model_transport = Model('transport');
            $transport = $model_transport->getExtendList(array('transport_id' => $goods_info['transport_id'], 'is_default' => 1));
            if (!empty($transport) && is_array($transport)) {
                foreach ($transport as $v) {
                    $goods_info[$v['type'] . "_price"] = $v['sprice'];
                }
            }
        }
        Tpl::output('goods', $goods_info);

        //v3-b11 抢购商品是否开始
        $IsHaveBuy = 0;
        if (!empty($_SESSION['member_id'])) {
            $buyer_id = $_SESSION['member_id'];
            $promotion_type = $goods_info["promotion_type"];
            if ($promotion_type == 'groupbuy') {
                //检测是否限购数量
                $upper_limit = $goods_info["upper_limit"];
                if ($upper_limit > 0) {
                    //查询些会员的订单中，是否已买过了
                    $model_order = Model('order');
                    //取商品列表
                    $order_goods_list = $model_order->getOrderGoodsList(array('goods_id' => $goods_id, 'buyer_id' => $buyer_id, 'goods_type' => 2));
                    if ($order_goods_list) {
                        //取得上次购买的活动编号(防一个商品参加多次团购活动的问题)
                        $promotions_id = $order_goods_list[0]["promotions_id"];
                        //用此编号取数据，检测是否这次活动的订单商品。
                        $model_groupbuy = Model('groupbuy');
                        $groupbuy_info = $model_groupbuy->getGroupbuyInfo(array('groupbuy_id' => $promotions_id));
                        if ($groupbuy_info) {
                            $IsHaveBuy = 1;
                        } else {
                            $IsHaveBuy = 0;
                        }
                    }
                }
            }
        }
        Tpl::output('IsHaveBuy', $IsHaveBuy);
        //end
        Tpl::output('store_id', $goods_info ['store_id']);

        // 输出一级地区
        $area_list = Model('area')->getTopLevelAreas();

        if (strtoupper(CHARSET) == 'GBK') {
            $area_list = Language::getGBK($area_list);
        }
        Tpl::output('area_list', $area_list);

        //优先得到推荐商品
        $goods_commend_list = $model_goods->getGoodsOnlineList(array('store_id' => $goods_info['store_id'], 'goods_commend' => 1), 'goods_id,goods_name,goods_jingle,goods_image,store_id,goods_price', 0, 'rand()', 5, 'goods_commonid');
        Tpl::output('goods_commend', $goods_commend_list);


        // 当前位置导航
        $nav_link_list = Model('goods_class')->getGoodsClassNav($goods_info['gc_id'], 0);
        $nav_link_list[] = array('title' => $goods_info['goods_name']);
        Tpl::output('nav_link_list', $nav_link_list);

        //评价信息
        $goods_evaluate_info = Model('evaluate_goods')->getEvaluateGoodsInfoByGoodsID($goods_id);
        Tpl::output('goods_evaluate_info', $goods_evaluate_info);

        $seo_param = array();
        $seo_param['name'] = $goods_info['goods_name'];
        $seo_param['key'] = $goods_info['goods_keywords'];
        $seo_param['description'] = $goods_info['goods_description'];
        Model('seo')->type('product')->param($seo_param)->show();
        Tpl::showpage('goods');
    }

    /**
     * 商品列表接口
     *
     * 请求参数：
     * key:
     * order: 1-升序 2-降序
     * page: 每页数量
     * curpage: 当前页码
     * gc_id: 分类编码
     * keyword：搜索关键字
     * brand_id: 品牌
     * efficacy_id：功效查询
     *
     */
    public function goods_listOp()
    {
        $page = trim($_GET['page']);
        $curpage = trim($_GET['curpage']);
        $this->initpage($page, $curpage);

        $model_goods = Model('goods');
        $condition = array();
        if (!empty(trim($_GET['gc_id']))) {
            $condition['gc_id'] = intval($_GET['gc_id']);
        }
        // 关键字
        if (!empty(trim($_GET['keyword']))) {
            $condition['goods_name|goods_jingle'] = array('like', '%' . urldecode($_GET['keyword']) . '%');
        }
        // 品牌查询
        if (!empty(trim($_GET['brand_id']))) {
            $condition['brand_id'] = intval($_GET['brand_id']);
        }
        // 功效查询
        if (!empty(trim($_GET['efficacy_id']))) {
            $condition['efficacy_id'] = intval($_GET['efficacy_id']);
        }

        //所需字段
        $fieldstr = "goods_id,goods_commonid,store_id,brand_id,gc_id,goods_name,goods_price,goods_marketprice,goods_image,goods_salenum,evaluation_good_star,evaluation_count,goods_storage,goods_storage_alarm";

        // 添加3个状态字段
        $fieldstr .= ',is_virtual,is_presell,is_fcode,have_gift';

        //排序方式
        $order = $this->_goods_list_order($_GET['key'], $_GET['order']);

        //优先从全文索引库里查找
//        list($indexer_ids, $indexer_count) = $model_search->indexerSearch($_GET, $this->page);
//        if (is_array($indexer_ids)) {
//            //商品主键搜索
//            $goods_list = $model_goods->getGoodsOnlineList(array('goods_id' => array('in', $indexer_ids)), $fieldstr, 0, $order, $this->page, null, false);
//
//            //如果有商品下架等情况，则删除下架商品的搜索索引信息
//            if (count($goods_list) != count($indexer_ids)) {
//                $model_search->delInvalidGoods($goods_list, $indexer_ids);
//            }
//            pagecmd('setEachNum', $this->page);
//            pagecmd('setTotalNum', $indexer_count);
//        } else {
//          $goods_list = $model_goods->getGoodsListByColorDistinct($condition, $fieldstr, $order, $this->page);
//        }

        $goods_list = $model_goods->getGoodsListByColorDistinct($condition, $fieldstr, $order, $this->page_size);
        $page_count = $model_goods->gettotalpage();
        $model_goods->cls();

        //处理商品列表(抢购、限时折扣、商品图片)
        $goods_list = $this->_goods_list_extend($goods_list);

        // 增加品牌两个图片字段
        $extend = array();
        if (!empty(trim($_GET['brand_id']))) {
            $brand = Model()->table("brand")->find(intval($_GET['brand_id']));
            if (!empty($brand)) {
                $extend['brand_img_logo'] = image_helper::format_brand_img($brand['brand_img_logo']);
                $extend['brand_img_bg'] = image_helper::format_brand_img($brand['brand_img_bg']);
                $extend['brand_title'] = $brand['brand_name'];
            }
        }

        joutput_data(array('goods_list' => $goods_list, 'mobile_page' => mobile_page($page_count), 'extend' => $extend));
    }

    /**
     * 商品列表排序方式
     */
    private function _goods_list_order($key, $order)
    {
        $result = 'is_own_shop desc,goods_commonid desc';
        if (!empty($key)) {

            $sequence = 'desc';
            if ($order == 1) {
                $sequence = 'asc';
            }

            switch ($key) {
                //销量
                case '1' :
                    $result = 'goods_salenum' . ' ' . $sequence;
                    break;
                //浏览量
                case '2' :
                    $result = 'goods_click' . ' ' . $sequence;
                    break;
                //价格
                case '3' :
                    $result = 'goods_price' . ' ' . $sequence;
                    break;
            }
        }
        return $result;
    }

    /**
     * 处理商品列表(抢购、限时折扣、商品图片)
     */
    private function _goods_list_extend($goods_list)
    {
        $commonid_array = array();
        $goodsid_array = array();
        foreach ($goods_list as $key => $value) {
            $commonid_array[] = $value['goods_commonid'];
            $goodsid_array[] = $value['goods_id'];

            // image 切换为goods_common 表中数据
            $condition['goods_commonid'] = $value['goods_commonid'];
            $goods_image = Model()->table('goods_common')->where($condition)->field('goods_image')->select();
            $goods_list[$key]['goods_image'] = $goods_image[0]['goods_image'];
        }

        //促销
        $groupbuy_list = Model('groupbuy')->getGroupbuyListByGoodsCommonIDString(implode(',', $commonid_array));
        $xianshi_list = Model('p_xianshi_goods')->getXianshiGoodsListByGoodsString(implode(',', $goodsid_array));
        foreach ($goods_list as $key => &$value) {
            //抢购
            if (isset($groupbuy_list[$value['goods_commonid']])) {
                $goods_list[$key]['goods_price'] = $groupbuy_list[$value['goods_commonid']]['groupbuy_price'];
                $goods_list[$key]['group_flag'] = true;
            } else {
                $goods_list[$key]['group_flag'] = false;
            }

            //限时折扣
            if (isset($xianshi_list[$value['goods_id']]) && !$goods_list[$key]['group_flag']) {
                $goods_list[$key]['goods_price'] = $xianshi_list[$value['goods_id']]['xianshi_price'];
                $goods_list[$key]['xianshi_flag'] = true;
            } else {
                $goods_list[$key]['xianshi_flag'] = false;
            }

            //商品图片url
            $goods_list[$key]['goods_image_url'] = cthumb($value['goods_image'], 360, $value['store_id']);

            unset($goods_list[$key]['store_id']);
            //unset($goods_list[$key]['goods_commonid']);
            unset($goods_list[$key]['nc_distinct']);
            unset($goods_list[$key]['gc_id']);
            unset($goods_list[$key]['goods_marketprice']);
            unset($goods_list[$key]['goods_image']);
            unset($goods_list[$key]['goods_salenum']);
            unset($goods_list[$key]['evaluation_good_star']);
            unset($goods_list[$key]['evaluation_count']);
            unset($goods_list[$key]['is_virtual']);
            unset($goods_list[$key]['is_presell']);
            unset($goods_list[$key]['is_fcode']);
            unset($goods_list[$key]['have_gift']);
            unset($goods_list[$key]['group_flag']);
            unset($goods_list[$key]['xianshi_flag']);

            // 品牌名
            $brand = Model()->table("brand")->find($value['brand_id']);
            if (!empty($brand)) {
                $value['brand_name'] = $brand['brand_name'];
            }

            // 分类
            $gc_info = Model()->table('goods_class')->find($value['gc_id']);
            if (!empty($gc_info)) {
                $value['gc_name'] = $gc_info['gc_name'];
            }

            // 功效列表
            $goods_common = Model('goods');
            $goods_common_info = $goods_common->getGoodeCommonInfoByID($value['goods_commonid']);
            if (!empty($goods_common_info)) {
                $value['goods_attr'] = $this->_parse_for_efficacy(unserialize($goods_common_info['goods_attr']));
            }
        }

        return $goods_list;
    }

    /**
     * 商品详细页扩展
     *
     * 输入参数：
     * goods_id：商品id
     *
     */
    public function goods_commonOp()
    {
        $goods_id = intval($_GET ['goods_id']);
        if (empty($_GET ['goods_id']) || $goods_id < 0) {
            return joutput_error(errcode::ErrParamter, "goods_id = {$goods_id} must be > 0.");
        }
        // 商品详细信息
        $model_goods = Model('goods')->cls();
        $goods_detail = $model_goods->getGoodsSku($goods_id);
        if (empty($goods_detail)) {
            return joutput_error(errcode::ErrGoodsNotExist);
        }
        //推荐商品
        $model_store = Model('store')->cls();
        $store_info = $model_store->getStoreInfoByID($goods_detail['goods_info']['store_id']);
        $goods_detail['store_info']['store_id'] = $store_info['store_id'];
        $goods_detail['store_info']['store_name'] = $store_info['store_name'];
        $goods_detail['store_info']['member_id'] = $store_info['member_id'];
        //显示QQ及旺旺 好商城V3
        $goods_detail['store_info']['store_qq'] = $store_info['store_qq'];
        $goods_detail['store_info']['store_ww'] = $store_info['store_ww'];
        $goods_detail['store_info']['store_phone'] = $store_info['store_phone'];
        $goods_detail['store_info']['member_name'] = $store_info['member_name'];
        $goods_detail['store_info']['avatar'] = getMemberAvatarForID($store_info['member_id']);

        //商品详细信息处理
        $goods_detail = $this->_goods_common_extend($goods_detail);

        joutput_data($goods_detail);
    }

    /**
     * 商品详细信息处理
     */
    private function _goods_common_extend($goods_detail)
    {
        //商品链接
        $goods_detail['goods_info']['goods_url'] = urlShop('goods', 'index', array('goods_id' => $goods_detail['request_goods_id']));

        //整理数据
        unset($goods_detail['goods_info']['goods_commonid']);
        unset($goods_detail['goods_info']['gc_id']);
        unset($goods_detail['goods_info']['gc_name']);
        unset($goods_detail['goods_info']['store_name']);
        unset($goods_detail['goods_info']['brand_id']);
        unset($goods_detail['goods_info']['brand_name']);
        unset($goods_detail['goods_info']['type_id']);
        unset($goods_detail['goods_info']['goods_image']);
        //unset($goods_detail['goods_info']['goods_body']);
        unset($goods_detail['goods_info']['mobile_body']);
        unset($goods_detail['goods_info']['goods_state']);
        unset($goods_detail['goods_info']['goods_stateremark']);
        unset($goods_detail['goods_info']['goods_verify']);
        unset($goods_detail['goods_info']['goods_verifyremark']);
        unset($goods_detail['goods_info']['goods_lock']);
        unset($goods_detail['goods_info']['goods_addtime']);
        unset($goods_detail['goods_info']['goods_edittime']);
        unset($goods_detail['goods_info']['goods_selltime']);
        unset($goods_detail['goods_info']['goods_show']);
        unset($goods_detail['goods_info']['goods_commend']);
        unset($goods_detail['goods_info']['explain']);
        unset($goods_detail['goods_info']['cart']);
        unset($goods_detail['goods_info']['buynow_text']);
        unset($goods_detail['goods_info']['spec_value']);
        unset($goods_detail['goods_info']['spec_name']);
        unset($goods_detail['goods_info']['goods_spec']);
        unset($goods_detail['groupbuy_info']);
        unset($goods_detail['xianshi_info']);

        return $goods_detail;
    }

    /**
     * 商品详细页
     *
     * 输入参数：
     * goods_id: 商品id
     * from：目前说明是来自app还是h5
     */
    public function goods_detailOp()
    {
        $goods_id = intval($_GET ['goods_id']);
        $from = $_GET['from'];

        // 商品详细信息
        $model_goods = Model('goods');
        $goods_detail = $model_goods->getGoodsDetail($goods_id);
        if (empty($goods_detail)) {
            return joutput_error(errcode::ErrGoodsNotExist);
        }

        // 推荐商品
        $model_store = Model('store');
        $hot_sales = $model_store->getHotSalesList($goods_detail['goods_info']['store_id'], 6);
        $goods_commend_list = array();
        foreach ($hot_sales as $value) {
            $goods_commend = array();
            $goods_commend['goods_id'] = $value['goods_id'];
            $goods_commend['goods_name'] = $value['goods_name'];
            $goods_commend['goods_price'] = $value['goods_price'];
            $goods_commend['goods_image_url'] = cthumb($value['goods_image'], 240);
            $goods_commend_list[] = $goods_commend;
        }
        $goods_detail['goods_commend_list'] = $goods_commend_list;
        $store_info = $model_store->getStoreInfoByID($goods_detail['goods_info']['store_id']);
        $goods_detail['store_info']['store_id'] = $store_info['store_id'];
        $goods_detail['store_info']['store_name'] = $store_info['store_name'];
        $goods_detail['store_info']['member_id'] = $store_info['member_id'];
        //显示QQ及旺旺 好商城V3
        $goods_detail['store_info']['store_qq'] = $store_info['store_qq'];
        $goods_detail['store_info']['store_ww'] = $store_info['store_ww'];
        $goods_detail['store_info']['store_phone'] = $store_info['store_phone'];
        $goods_detail['store_info']['member_name'] = $store_info['member_name'];
        $goods_detail['store_info']['avatar'] = getMemberAvatarForID($store_info['member_id']);

        //商品详细信息处理
        $goods_detail = $this->_goods_detail_extend($goods_detail);

        if ($from === 'app') {
            $goods_detail['goods_info']['spec'] = $this->_parse_spec($goods_detail['goods_info']['spec_name'], $goods_detail['goods_info']['spec_value'], $goods_detail['spec_image'], $goods_detail['spec_list']);
            $goods_detail['goods_info']['attributes'] = $this->_parse_attributes($goods_detail['goods_info']['goods_attr']);
            $goods_detail['goods_info']['goods_spec_obj'] = $this->_parse_goods_spec($goods_detail['goods_info']['goods_spec']);
            $goods_detail['UPLOAD_SITE_URL'] = $GLOBALS['setting_config']['upload_site_url'];
        }
        //v3-b11 抢购商品是否开始
        $goods_info = $goods_detail['goods_info'];
        //print_r($goods_info);
        $IsHaveBuy = 0;
        if (!empty($_COOKIE['username'])) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfo(array('member_name' => $_COOKIE['username']));
            $buyer_id = $member_info['member_id'];

            $promotion_type = $goods_info["promotion_type"];

            if ($promotion_type == 'groupbuy') {
                //检测是否限购数量
                $upper_limit = $goods_info["upper_limit"];
                if ($upper_limit > 0) {
                    //查询些会员的订单中，是否已买过了
                    $model_order = Model('order');
                    //取商品列表
                    $order_goods_list = $model_order->getOrderGoodsList(array('goods_id' => $goods_id, 'buyer_id' => $buyer_id, 'goods_type' => 2));
                    if ($order_goods_list) {
                        //取得上次购买的活动编号(防一个商品参加多次团购活动的问题)
                        $promotions_id = $order_goods_list[0]["promotions_id"];
                        //用此编号取数据，检测是否这次活动的订单商品。
                        $model_groupbuy = Model('groupbuy');
                        $groupbuy_info = $model_groupbuy->getGroupbuyInfo(array('groupbuy_id' => $promotions_id));
                        if ($groupbuy_info) {
                            $IsHaveBuy = 1;
                        } else {
                            $IsHaveBuy = 0;
                        }
                    }
                }
            }
        }
        $goods_detail['IsHaveBuy'] = $IsHaveBuy;
        //v3-b11 end
        joutput_data($goods_detail);
    }

    /**
     * 商品详细信息处理
     */
    private function _goods_detail_extend($goods_detail)
    {
        //整理商品规格
        unset($goods_detail['spec_list']);
        $goods_detail['spec_list'] = $goods_detail['spec_list_mobile'];
        unset($goods_detail['spec_list_mobile']);

        //整理商品图片
        unset($goods_detail['goods_image']);
        $goods_detail['goods_image'] = implode(',', $goods_detail['goods_image_mobile']);
        unset($goods_detail['goods_image_mobile']);

        //商品链接
        $goods_detail['goods_info']['goods_url'] = urlShop('goods', 'index', array('goods_id' => $goods_detail['goods_info']['goods_id']));

        //整理数据
        unset($goods_detail['goods_info']['goods_commonid']);
        unset($goods_detail['goods_info']['gc_id']);
        unset($goods_detail['goods_info']['gc_name']);
        unset($goods_detail['goods_info']['store_name']);
        unset($goods_detail['goods_info']['brand_id']);
        unset($goods_detail['goods_info']['brand_name']);
        unset($goods_detail['goods_info']['type_id']);
        unset($goods_detail['goods_info']['goods_image']);
        unset($goods_detail['goods_info']['goods_body']);
        unset($goods_detail['goods_info']['mobile_body']);
        unset($goods_detail['goods_info']['goods_state']);
        unset($goods_detail['goods_info']['goods_stateremark']);
        unset($goods_detail['goods_info']['goods_verify']);
        unset($goods_detail['goods_info']['goods_verifyremark']);
        unset($goods_detail['goods_info']['goods_lock']);
        unset($goods_detail['goods_info']['goods_addtime']);
        unset($goods_detail['goods_info']['goods_edittime']);
        unset($goods_detail['goods_info']['goods_selltime']);
        unset($goods_detail['goods_info']['goods_show']);
        unset($goods_detail['goods_info']['goods_commend']);
        unset($goods_detail['goods_info']['explain']);
        unset($goods_detail['goods_info']['cart']);
        unset($goods_detail['goods_info']['buynow_text']);
        unset($goods_detail['groupbuy_info']);
        //unset($goods_detail['xianshi_info']);

        return $goods_detail;
    }

    /**
     * 解析spec字段
     *
     * @param $spec_name
     * @param $spec_value
     * @param array $spec_image
     * @param array $spec_list
     * @return array
     */
    private function _parse_spec($spec_name, $spec_value, $spec_image = [], $spec_list = [])
    {
        $spec = [];
        foreach ($spec_name as $key => $value) {
            $spec_item = [];
            $spec_item['spec_id'] = $key;
            $spec_item['spec_name'] = $value;
            if (!empty($spec_value[$key])) {
                $spec_item['value_list'] = [];
                foreach ($spec_value[$key] as $v_key => $v) {
                    $value_item = [];
                    $value_item['value_id'] = $v_key;
                    $value_item['value_name'] = $v;
                    $value_item['value_image'] = empty($spec_image[$v_key]) ? '' : $spec_image[$v_key];
                    $value_item['value'] = empty($spec_list[$v_key]) ? '' : $spec_list[$v_key];
                    array_push($spec_item['value_list'], $value_item);
                }
            }
            array_push($spec, $spec_item);
        }
        return $spec;
    }

    /**
     * 解析商品属性字段
     *
     * @param $goods_attr
     * @return array
     */
    private function _parse_attributes($goods_attr)
    {
        $attributes = [];
        foreach ($goods_attr as $key => $value) {
            $attribute_item = [];
            $attribute_item['goods_attr_id'] = $key;
            $attribute_item['goods_attr_value'] = [];
            if (!empty($value)) {
                foreach ($value as $v_key => $v) {
                    $attribute_attr = [];
                    if ($v_key === 'name') {
                        $attribute_item['goods_attr_name'] = $v;
                    } else {
                        $attribute_attr['value_id'] = $v_key;
                        $attribute_attr['value_name'] = $v;
                        array_push($attribute_item['goods_attr_value'], $attribute_attr);
                    }
                }
            }
            array_push($attributes, $attribute_item);
        }
        return $attributes;
    }

    /**
     * 解析功效
     *
     * @param $goods_attr
     * @return mixed
     */
    private function _parse_for_efficacy($goods_attr)
    {
        foreach ($goods_attr as $key => $val) {
            $attr_item['goods_attr_id'] = intval($key);
            $attr_item['goods_attr_name'] = $val['name'];
            if ($attr_item['goods_attr_name'] === '功效') {
                $goods_attr_value = array();
                foreach ($val as $id => $name) {
                    if ($id !== 'name') {
                        $val_item['value_id'] = intval($id);
                        $val_item['value_name'] = $name;
                        array_push($goods_attr_value, $val_item);
                    }
                }
                $attr_item['goods_attr_value'] = $goods_attr_value;
                break;
            }
        }
        return $attr_item;
    }

    /**
     * 商品规格序列化解析
     *
     * @param $goods_spec
     * @return array
     */
    private function _parse_goods_spec($goods_spec)
    {
        $goods_spec_obj = [];
        foreach ($goods_spec as $key => $value) {
            $spec_item = [];
            $spec_item['spec_value'] = $key;
            $spec_item['spec_name'] = $value;
            array_push($goods_spec_obj, $spec_item);
        }
        return $goods_spec_obj;
    }

    public function detailOp()
    {
        $commonid = 0;
        $goods_id = intval($_GET['goods_id']);
        if (empty($_GET['goods_id']) || $goods_id <= 0) {
            if (!empty($_GET['goods_commonid'])) {
                $commonid = intval($_GET['goods_commonid']);
            }
        } else {
            $info = Model('goods')->getGoodsInfoByID($goods_id, 'goods_commonid');
            $commonid = intval($info['goods_commonid']);
        }

        if ($commonid > 0) {
            $items = Model()->table('goods_common')->field('goods_body')->where(array('goods_commonid' => $commonid))->select();
            if (!empty($items) && count($items) > 0) {
                Tpl::output('goods_body', $items[0]['goods_body']);
            }
        }
        Tpl::showpage('goods_detail');
    }
}