<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/3/30
 * Time: 下午3:31
 */
require_once (BASE_ROOT_PATH . '/helper/field_helper.php');
require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
require_once (BASE_ROOT_PATH . '/helper/activity_helper.php');
require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/favorite.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/anotice.php');

class goods_helper
{
    const fieldstr = 'goods_id,goods_commonid,goods_state,store_id,brand_id,gc_id,goods_name,goods_price,goods_marketprice,goods_promotion_price,goods_jingle,
                  goods_image,goods_salenum,evaluation_good_star,evaluation_count,goods_storage,goods_storage_alarm,is_virtual,is_presell,
                  is_fcode,have_gift,goods_mobile_name,goods_spec,goods_collect,
                  goods_addtime,goods_edittime';

    private $mUseMainPage;
    public function __construct($main_page = true) {
        $this->mUseMainPage = $main_page;
    }

    public function get_distinct($goods_commonids)
    {
        $goods_list = Model('goods')->cls()->getGoodsListByColorDistinct(array('goods_commonid' => array('in', $goods_commonids)),self::fieldstr);
        $goods_list = $this->goods_list_extend($goods_list);
        return $goods_list;
    }

    public function get_infos($goods_ids)
    {
        $goods_list = Model('goods')->cls()->getGoodsOnlineList(array('goods_id' => array('in', $goods_ids)),self::fieldstr);
        $goods_list = $this->goods_list_extend($goods_list);
        return $goods_list;
    }

    //show_gid => 显示这个sku的详情
    public function get_spu($common_id,$show_gid,&$err)
    {
        $err = errcode::Success;

        $common_id = intval($common_id);
        $mod = Model('goods');
        $common_info = $mod->getGoodeCommonInfoByID($common_id);
        if(empty($common_info)) {
            $err = array('code' => errcode::ErrParamter,'msg' =>'商品不存在或者已经删除');
            return false;
        }

        $goods_list = $mod->getGoodsOnlineList(array('goods_commonid' => $common_id),self::fieldstr);
        if(empty($goods_list)) {
            $err = array('code' => errcode::ErrParamter,'msg' =>'抱歉，商品已经下架了，不能查看详情～');
            return false;
        }

        foreach ($goods_list as $goods_info) {
            $mod->click_goods($goods_info['goods_id']);
        }

        $ret = array();
        $gcommon = new goods_common($common_info,$goods_list,$show_gid);
        $ret['common_info'] = $gcommon->format();

        $ret_summary = $this->summary($goods_list,$related_goods);
        $summary = $ret_summary['summary'];

        if(!empty($related_goods)) {
            $ret_related = $this->online_summary($related_goods,$x);
            $summary = array_merge($summary,$ret_related['summary']);
        }

        $ret['summary']  = $summary;
        if(empty($ret['common_info']['images']))
        {
            $images = [];
            foreach ($summary as $item) {
                $images[] = $item['goods_image_url'];
            }
            $ret['common_info']['images'] = $images;
        }
        $ret['groupbuy'] = $ret_summary['groupbuy'];
        $ret['limitime'] = $ret_summary['limitime'];
        $ret['bundling'] = $ret_summary['bundling'];

        return $ret;
    }

    public function common_summary($common_ids, &$goods_ids, &$related_goods)
    {
        $mod = Model('goods');
        $goods_list = $mod->getGoodsListByColorDistinct(array('goods_commonid' => array('in', $common_ids)),self::fieldstr);

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

        return $this->summary($goods_list,$related_goods);
    }

    public function online_summary(&$goods_ids, &$related_goods)
    {
        $goods_list = Model('goods')->cls()->getGoodsOnlineList(array('goods_id' => array('in', $goods_ids)),self::fieldstr);
        $goods_ids = array();
        foreach ($goods_list as $goods) {
            $goods_id = intval($goods['goods_id']);
            $goods_ids[] = $goods_id;
        }
        $goods_ids = array_unique($goods_ids);
        $ret = $this->summary($goods_list,$related_goods);

        return $ret;
    }

    public function cart_summary($goods_ids,&$related_goods)
    {
        $goods_list = Model('goods')->cls()->getGoodsList(array('goods_id' => array('in', $goods_ids)),self::fieldstr);
        return $this->summary($goods_list,$related_goods);
    }

    public function summary($goods_list, &$related_goods)
    {
        $related_goods = [];
        $summary_list = [];
        $sort_summarys = [];

        $favorate = new user_session\favorite();
        $anotice  = new user_session\anotice();
        foreach ($goods_list as $goods)
        {
            $summary = new goods_summary($goods);
            $info = $summary->format($act_type,$act_id,$favorate,$anotice,$this->mUseMainPage);
            $goods_id = intval($goods['goods_id']);
            $sort_summarys[$goods_id] = $info;

            $summary_list[] = $info;
            $related_goods = array_merge($related_goods,$summary->related_goods());
        }
        $related_goods = array_unique($related_goods);

        $gbuys = [];
        $ltimes = [];
        $bundling = [];

        foreach ($summary_list as $summary)
        {
            $act_id   = intval($summary['act_id']);
            $act_type = intval($summary['act_type']);

            if($act_type == activity_helper::ACTIVITY_GROUPBUY)
            {
                if(isset($gbuys[$act_id]) == false) {
                    $info = activity\groupbuy::instance()->get_info($act_id);
                    $gbuys[$act_id] = $info;
                }
            }
            elseif($act_type == activity_helper::ACTIVITY_LIMITTIME)
            {
                if(isset($ltimes[$act_id]) == false) {
                    $info = activity\limitime::instance()->get_info($act_id);
                    $ltimes[$act_id] = $info;
                }
            }
            else {

            }

            $have_bundle = $summary['have_bundle'];
            if($have_bundle)
            {
                foreach ($summary['bundles'] as $bl_id) {
                    $info = activity\bundling::instance()->get_info($bl_id,$goods_ids);
                    $bundling[$bl_id] = $info;
                }
            }
        }

        $ret = array();
        $ret['summary'] = $summary_list;
        $ret['sort_summary'] = $sort_summarys;

        $ret['groupbuy'] = array();
        foreach ($gbuys as $key => $val) {
            array_push($ret['groupbuy'],$val);
        }

        $ret['limitime'] = array();
        foreach ($ltimes as $key => $val) {
            array_push($ret['limitime'],$val);
        }

        $ret['bundling'] = array();
        foreach ($bundling as $key => $val) {
            array_push($ret['bundling'],$val);
        }

        return $ret;
    }

    /**
     * 处理商品列表(抢购、限时折扣、商品图片)
     */
    private function goods_list_extend($goods_list)
    {
        if(empty($goods_list)) return $goods_list;

        foreach ($goods_list as $key => &$value)
        {
            $value['group_flag'] = false;
            $value['xianshi_flag'] = false;
            //商品图片url
            $value['goods_image_url'] = cthumb($value['goods_image'], 360, $value['store_id']);
            $value['goods_state'] = 1;
            $value['goods_storage_state'] = intval($value['goods_storage']) > 0 ? 1 : 0;
            $value['goods_name'] = $value['goods_mobile_name'];

            $fields = 'store_id,nc_distinct,gc_id,goods_image,goods_salenum,evaluation_good_star,evaluation_count,is_virtual,is_presell,' .
                'is_fcode,have_gift,goods_storage,goods_storage_alarm,goods_attr,group_flag,xianshi_flag';
            field_helper::unset_column($value,$fields);
        }
        return $goods_list;
    }
}