<?php
//use Shopnc\Tpl;

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

require_once(BASE_ROOT_PATH . '/helper/category_helper.php');
require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once(BASE_ROOT_PATH . '/helper/history_helper.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');
require_once(BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
require_once(BASE_ROOT_PATH . '/helper/search/util.php');
require_once(BASE_ROOT_PATH . '/helper/search_param.php');
require_once(BASE_ROOT_PATH . '/mobile/control/special.php');
require_once(BASE_ROOT_PATH . '/helper/ugc_helper.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
require_once(BASE_ROOT_PATH . '/helper/text_filter.php');
require_once(BASE_ROOT_PATH . '/helper/bonus_helper.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');


define('MOBILE_SERVER',true);

class searchControl extends mobileHomeControl
{
    const def_history_divider = '/mobile/defimg/history_divider.png';
    const def_hot_divider = '/mobile/defimg/hot_divider.png';
    const def_guess_divider = '/mobile/defimg/guess_like.png';

    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $param = new search_param($_GET);
        $params = $param->format($this->page_no(),$this->page_size());

        if($params == false) {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(0)));
        } else {
            $result = search\search_client::instance()->get_result($params);
        }

        if(empty($result))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(0)));
        }

        $cids = $result['cids'];
        $model_goods = Model('goods');
        $items = $model_goods->getGoodsListByColorDistinct(array('goods_commonid' => array('in',$cids)),goods_helper::fieldstr,'','');
        $page_count = intval($result['page_count']);

        if(empty($items))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page($page_count)));
        }
        else
        {
            $tmp =[];
            foreach ($items as $item) {
                $commonid = intval($item['goods_commonid']);
                $tmp[$commonid] = $item;
            }

            $goods_list = [];
            foreach ($cids as $cid)
            {
                if(array_key_exists($cid,$tmp)) {
                    $goods_list[] = $tmp[$cid];
                }
            }

            $blocks = [];
            if($this->page_no() == 1)
            {
                if($param->brand_id() != 0)
                {
                    $block = special_formater::format_brand_sale($param->brand_id(),false);
                    if(!empty($block))
                    {
                        $blocks[] = $block;
                        if($_SESSION['is_lasted']) {
                            $blocks[] = special_formater::def_divider();
                        }
                    }
                }
                else
                {
                    $block = special_formater::format_brand($result['brands']);
                    if(!empty($block))
                    {
                        $blocks[] = $block;
                        if($_SESSION['is_lasted']) {
                            $blocks[] = special_formater::def_divider();
                        }
                    }
                }

                $block = special_formater::format_category($result['hots']);
                if(!empty($block))
                {
                    $blocks[] = $block;
                    if($_SESSION['is_lasted']) {
                        $blocks[] = special_formater::def_divider();
                    }
                }
            }

            foreach ($goods_list as $goods) {
                $goods_id = intval($goods['goods_id']);
                $goods_ids[] = $goods_id;
            }

            $helper = new goods_helper();
            $ret = $helper->summary($goods_list,$related_goods);

            $block = special_formater::format_goods($goods_ids,"",$ret['sort_summary']);
            $blocks[] = $block;

            $spids = $result['spids'];
            $ugc_blocks = $this->format_special($spids,$ugcs);
            if(!empty($ugc_blocks))
            {
                foreach ($ugc_blocks as $block) {
                    $blocks[] = $block;
                }
            }

            return self::outsuccess(array('special_list' => $blocks,
                'ugcs' => $ugcs,
                'summary'  => $ret['summary'],
                'groupbuy' => $ret['groupbuy'],
                'limitime' => $ret['limitime'],
                'bundling' => $ret['bundling'],
                'mobile_page' => mobile_page($page_count)));
        }
    }

    private function format_special($spids,&$ugcs)
    {
        $index = $this->page_no();
        if(empty($spids) || $index > count($spids)) {
            $ugcs = [];
            return [];
        }
        else {
            rsort($spids,SORT_NUMERIC);
            $spid = $spids[$index - 1];
            return $this->ugc_block($spid,$ugcs);
        }
    }

    private function ugc_block($spid, &$ugcs)
    {
        try
        {
            $ugcs = [];
            $mod_special = Model('mb_special');
            $items = $mod_special->getMbSpecialList(['special_id' => $spid]);
            if(empty($items)) return [];

            $special = new ugc\special($items[0]);
            $sender = $special->memberid();

            $mod_member = Model('member');
            $members = $mod_member->getMemberList(['member_id' => $sender]);
            if(empty($members)) return [];

            $minfo = new member_info($members[0]);

            $divider = special_formater::def_divider();
            $result[] = $divider;
            $block = special_formater::format_ugc($special);
            $result[] = $block;
            $divider = special_formater::def_divider();
            $result[] = $divider;

            $ugc = $this->format_ugc($minfo,$special);
            $ugcs[] = $ugc;

            return $result;
        }
        catch (Exception $ex)
        {
            return [];
        }
    }

    private function format_ugc(member_info $minfo, ugc\special $special)
    {
        $ret['special_id'] = $special->special_id();
        $ret['member_avatar'] = $minfo->avatar();
        $ret['member_nickname'] = $minfo->nickname();
        $ret['has_vote'] = $special->has_vote();
        $ret['appreciate_num'] = $special->appreciates();
        $ret['comment_num'] = $special->comments();
        $ret['clicks'] = $special->clicks();
        $ret['pubtime'] = $special->editime();
        $ret['desc'] = $special->description();
        $supporter = new ugc\special_support($special->special_id(),0);
        $ret['supported'] = $supporter->supported();
        $ret['support_num'] = $special->likes();
        $cid = $special->category_id();
        $ret['category'] = ugc_helper::category_title($cid);

        $ret['can_del'] = false;
        if(session_helper::logined())
        {
            if($special->memberid() == $_SESSION['member_id']) {
                $ret['can_del'] = true;
            }
        }

        return $ret;
    }

    public function search_goodsOp()
    {
        $param = new search_param($_GET);
        $params = $param->format($this->page_no(),$this->page_size());

        if($params == false) {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(0)));
        } else {
            $result = search\search_client::instance()->get_result($params);
        }

        if(empty($result))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(0)));
        }

        $cids = $result['cids'];
        $model_goods = Model('goods');
        $items = $model_goods->getGoodsListByColorDistinct(array('goods_commonid' => array('in',$cids)),goods_helper::fieldstr,'','');
        $page_count = intval($result['page_count']);

        if(empty($items))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page($page_count)));
        }
        else
        {
            $tmp =[];
            foreach ($items as $item) {
                $commonid = intval($item['goods_commonid']);
                $tmp[$commonid] = $item;
            }

            $goods_list = [];
            foreach ($cids as $cid)
            {
                if(array_key_exists($cid,$tmp)) {
                    $goods_list[] = $tmp[$cid];
                }
            }

            foreach ($goods_list as $goods) {
                $goods_id = intval($goods['goods_id']);
                $goods_ids[] = $goods_id;
            }

            $helper = new goods_helper();
            $ret = $helper->summary($goods_list,$related_goods);

            $blocks = special_formater::format_ugc_goods($goods_ids,"",$ret['sort_summary']);

            return self::outsuccess(['special_list' => $blocks,
                'summary'  => $ret['summary'],
                'groupbuy' => $ret['groupbuy'],
                'limitime' => $ret['limitime'],
                'bundling' => $ret['bundling'],
                'mobile_page' => mobile_page($page_count)]);
        }
    }

    public function suggest_wordsOp()
    {
        $keyword = $_GET['keyword'];
        if(empty($keyword)) {
            return self::outerr(errcode::ErrParamter);
        }
        else
        {
            $result = search\search_client::instance()->get_words($keyword);
            if($result == false) {
                $result = null;
                $pages = 0;
            } else {
                $result = $this->separate_page($result,$pages);
            }
            return self::outsuccess(['words' => $result,'mobile_page' => mobile_page($pages)]);
        }
    }

    public function historyOp()
    {
        $helper = new history_helper();
        $history = $helper->histories();
        $hot_search = @explode(',',C('hot_search'));

        $blocks = [];
        if(!empty($history))
        {
            if($_SESSION['is_lasted']) {
                $blocks[] = self::history_divider();
            }
            $blocks[] = special_formater::format_keyword($history,'历史搜索');
        }
        if(!empty($hot_search))
        {
            if($_SESSION['is_lasted']) {
                $blocks[] = self::hot_divider();
            }
            $blocks[] = special_formater::format_keyword($hot_search,'热门搜索');
        }

        $guslike = $this->guesslike();
        if($guslike == false)
        {
            return self::outsuccess(['special_list' => $blocks,'mobile_page' => mobile_page(1)]);
        }
        else
        {
            $gus_blocks = $guslike['special_list'];
            if(!empty($gus_blocks))
            {
                if($_SESSION['is_lasted']) {
                    $blocks[] = self::guess_divider();
                }
                $blocks[] = $gus_blocks[0];
            }

            $guslike['special_list'] = $blocks;
            $guslike['mobile_page'] = mobile_page(1);

            return self::outsuccess($guslike);
        }
    }

    private static function history_divider()
    {
        static $block = [];
        if(empty($block))
        {
            $block['item_type'] = 'divider';
            $block['item_title'] = '';
            $block['bg_type'] = 'image';
            $image = RESOURCE_SITE_URL . self::def_history_divider;
            $block['bg_data'] = $image;
            $block['has_margin'] = false;
            $block['scale'] = special_formater::scale($image);
        }
        return $block;
    }
    private static function hot_divider()
    {
        static $block = [];
        if(empty($block))
        {
            $block['item_type'] = 'divider';
            $block['item_title'] = '';
            $block['bg_type'] = 'image';
            $image = RESOURCE_SITE_URL . self::def_hot_divider;
            $block['bg_data'] = $image;
            $block['has_margin'] = false;
            $block['scale'] = special_formater::scale($image);
        }
        return $block;
    }
    private static function guess_divider()
    {
        static $block = [];
        if(empty($block))
        {
            $block['item_type'] = 'divider';
            $block['item_title'] = '';
            $block['bg_type'] = 'image';
            $image = RESOURCE_SITE_URL . self::def_guess_divider;
            $block['bg_data'] = $image;
            $block['has_margin'] = false;
            $block['scale'] = special_formater::scale($image);
        }
        return $block;
    }

    public function guesslike()
    {
        $goods_list = activity_helper::recomoned_goodsids();

        if(empty($goods_list)) {
            return false;
        }
        else
        {
            $gids = [];
            $count = count($goods_list);
            $len = mt_rand(1,$count);

            for ($i = 0; $i < $len;)
            {
                $pos = intval(mt_rand(0,$count - 1));
                $gid = $goods_list[$pos];
                if(in_array($gid,$gids)) {
                    continue;
                } else {
                    $gids[] = $gid;
                }
                if($len == count($gids)) {
                    break;
                }
            }

            $helper = new goods_helper();
            $ret = $helper->online_summary($gids,$related_goods);
            $block = special_formater::format_goods($gids,"猜你喜欢",$ret['sort_summary']);

            return array('special_list' => array($block),
                'summary'  => $ret['summary'],
                'groupbuy' => $ret['groupbuy'],
                'limitime' => $ret['limitime'],
                'bundling' => $ret['bundling']);
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * @return array
     */
    private function condition()
    {
        $condition = array();
        //分类查询
        if (!empty(trim($_GET['gc_id']))) {
            $condition['gc_id'] = intval($_GET['gc_id']);
        }
        // 关键字
        if (!empty(trim($_GET['keyword']))) {
            $word = trim(urldecode($_GET['keyword']));
            $helper = new history_helper();
            $helper->add_word($word);
            $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']);
        }
        //重新组合后的分类查询
        if (!empty(trim($_GET['hot_id']))) {
            $id = intval($_GET['hot_id']);
            $cids = category_helper::instance()->cids($id);
            $condition['gc_id'] = array('in', $cids);
        }
        return $condition;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function goods_listOp()
    {
        $model_goods = Model('goods');
        $condition = $this->condition();

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

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

        $goods_ids = array();
        foreach($goods_list as $val) {
            array_push($goods_ids,$val['goods_id']);
        }

        $helper = new goods_helper();
        $goods_list = $helper->get_infos($goods_ids);

        self::outsuccess(array('goods_list' => $goods_list, 'mobile_page' => mobile_page($page_count)));
    }

    /**
     * 商品列表排序方式
     */
    private function get_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;
    }


    /**
     * 商品详细页扩展
     *
     * 输入参数：
     * 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['goods_info']['goods_attr']);

        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 goods_bodyOp()
    {
        return joutput_error(errcode::ErrProtocolDisabled);

        $goods_id = intval($_GET ['goods_id']);
        $out_type = $_GET ['type'];
        $model_goods = Model('goods');

        if (empty($_GET ['goods_commonid'])) {
            $goods_info = $model_goods->getGoodsInfoByID($goods_id, 'goods_commonid');
        } else {
            $goods_info['goods_commonid'] = intval($_GET ['goods_commonid']);
        }
        $goods_common_info = $model_goods->getGoodeCommonInfoByID($goods_info['goods_commonid']);

        if ($out_type === 'json') {
            $goods_common_info['goods_attr'] = $this->_parse_attributes(unserialize($goods_common_info['goods_attr']));
            $goods_common_info['spec_name'] = unserialize($goods_common_info['spec_name']);
            $goods_common_info['spec_value'] = unserialize($goods_common_info['spec_value']);
            $goods_common_info['spec'] = $this->_parse_spec($goods_common_info['spec_name'], $goods_common_info['spec_value']);
            $goods_common_info['goods_body'] = ''; //base64_encode($goods_common_info['goods_body']);
            $goods_common_info['mobile_body'] = '';//base64_encode($goods_common_info['mobile_body']);
            joutput_data($goods_common_info);
        } else {
            Tpl::output('goods_common_info', $goods_common_info);
            Tpl::showpage('goods_body');
        }
    }

    /**
     * 获取detail（未启用）
     */
    public function detailOp()
    {
        $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 {
            $items = Model()->table('goods')->field('goods_commonid')->where(array('goods_id' => $goods_id))->limit(1)->select();
            if (!empty($items) && count($items) > 0) {
                $commonid = intval($items[0]['goods_commonid']);
            }
        }

        if (isset($commonid) && $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');
    }
}