<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/19
 * Time: 上午10:13
 */
require_once (BASE_ROOT_PATH . '/helper/user_session/fcode.php');

class goods_summary
{
    private $goods_id;
    private $goods_commonid;
    private $goods_info;
    private $bundles;
    const new_goods_speriod = 10 * 3600 * 24;


    public function __construct($param)
    {
        if(is_array($param)) {
            $this->goods_info = $param;
            $this->goods_id = intval($this->goods_info['goods_id']);
            $this->goods_commonid = intval($this->goods_info['goods_commonid']);
        }
        else
        {
            $this->goods_id = intval($param);
            $mod = Model('goods');
            $this->goods_info = $mod->getGoodsOnlineInfoByID($this->goods_id);
            if(empty($this->goods_info)) {
                throw new Exception("该商品不存在或者已经下架~");
            }
            $this->goods_commonid = intval($this->goods_info['goods_commonid']);
        }
        $this->bundles = activity_helper::have_bundling($this->goods_id);
        if($this->bundles == false) {
            $this->bundles = [];
        }
    }

    public function goods_id() {
        return $this->goods_id;
    }
    public function goods_commonid() {
        return $this->goods_commonid;
    }

    static private function img_url($value,$store_id)
    {
        return cthumb($value, 1280, $store_id);
    }

    static function get_spec($arSpec)
    {
        $specs = unserialize($arSpec);
        if(isset($specs) && is_array($specs))
        {
            $spec_val = '';
            foreach ($specs as $key => $val) {
                $spec_val .= " ";
                $spec_val .= $val;
            }
            return trim($spec_val);
        } else {
            return "";
        }
    }
    private function is_new()
    {
        $add_time  = intval($this->goods_info['goods_addtime']);
        $start = time() - self::new_goods_speriod;
        return ($add_time > $start);
    }

    public function format(&$act_type, &$act_id,user_session\favorite $favorate,user_session\anotice $anotice,$main_page)
    {
        $summary = [];

        $summary['goods_id'] = intval($this->goods_info['goods_id']);
        $summary['goods_commonid'] = intval($this->goods_info['goods_commonid']);

        $goods_mobile_name = $this->goods_info['goods_mobile_name'];
        $goods_name = $this->goods_info['goods_name'];
        $summary['goods_mobile_name'] = empty($goods_mobile_name) ? $goods_name : $goods_mobile_name;

        $summary['goods_spec'] = self::get_spec($this->goods_info['goods_spec']);
        $summary['goods_jingle'] = $this->goods_info['goods_jingle'];
        $summary['store_id'] = intval($this->goods_info['store_id']);

        $summary['brand_id'] = intval($this->goods_info['brand_id']);
        $summary['brand_name'] = brand_helper::instance()->name($summary['brand_id']);
        $summary['brand_country'] = brand_helper::instance()->country($summary['brand_id']);
        $summary['brand_country_logo'] = brand_helper::instance()->country_logo($summary['brand_id']);
        $summary['brand_author_desc'] = brand_helper::instance()->author_desc($summary['brand_id']);

        $summary['gc_id'] = intval($this->goods_info['gc_id']);
        $summary['goods_price'] = $this->goods_info['goods_price'];
        $summary['goods_marketprice'] = $this->goods_info['goods_marketprice'];
        $summary['goods_promotion_price'] = $this->goods_info['goods_promotion_price'];

        if($main_page)
        {
            $image = commonid_helper::instance()->main_image($summary['goods_id']);
            if(empty($image)) {
                $summary['goods_image_url'] = self::img_url($this->goods_info['goods_image'],$this->goods_info['store_id']);
            } else {
                $summary['goods_image_url'] = $image;
            }
        }
        else {
            $summary['goods_image_url'] = self::img_url($this->goods_info['goods_image'],$this->goods_info['store_id']);
        }

        $summary['goods_state'] = intval($this->goods_info['goods_state']);
        $summary['goods_salenum'] = intval($this->goods_info['goods_salenum']) + 19;
        $summary['goods_storage'] = intval($this->goods_info['goods_storage']);
        $summary['goods_collect'] = intval($this->goods_info['goods_collect']);
        $summary['is_new'] = $this->is_new();

        $have_gift = intval($this->goods_info['have_gift']) == 1 ? true : false;
        $summary['have_gift'] = $have_gift;
        if($have_gift)
        {
            if (activity_helper::have_gift($this->goods_id)) {
                $summary['have_gift'] = true;
                $summary['gifts'] = activity_helper::goods_gifts($this->goods_id);
            }
            else {
                $summary['have_gift'] = false;
                $summary['gifts'] = array();
            }
        }
        else {
            $summary['gifts'] = array();
        }

        $have_combo = activity_helper::have_combo($this->goods_id);
        $summary['have_combo'] = $have_combo;
        if($have_combo) {
            $summary['combos'] = activity_helper::combo_goods($this->goods_id);
        } else {
            $summary['combos'] = array();
        }
        $have_recommend = activity_helper::have_recommend($this->goods_id);
        $summary['have_recommend'] = $have_recommend;

        if($this->bundles == false || empty($this->bundles)) {
            $summary['have_bundle'] = false;
            $summary['bundles'] = array();
        } else {
            $summary['have_bundle'] = true;
            $summary['bundles'] = $this->bundles;
        }

        $act_id = activity_helper::take_parted($this->goods_id,$act_type);
        if($act_id == false) {
            $summary['act_id'] = 0;
            $summary['act_type'] = $act_type;
        }
        else {
            $summary['act_id'] = $act_id;
            $summary['act_type'] = $act_type;
        }

        global $config;
        if($act_id == false)
        {
            $summary['bonus_price']  = predeposit_helper::login_bonus_price($this->goods_info['goods_price'],$rates);
            $gap = predeposit_helper::discount_gap($summary['bonus_price'],$this->goods_info['goods_price']);
            $summary['discount_gap'] = $gap;
            $summary['gap_show'] = true;
            if(intval($gap * 100 + 0.5) == 0) {
                $summary['gap_desc'] = $config['bonus_gap']['no_gap'];
            } else {
                $summary['gap_desc'] = sprintf($config['bonus_gap']['have_gap'],$gap);
            }
        }
        else {
            $summary['bonus_price'] = $this->goods_info['goods_price'];
            $summary['discount_gap'] = 0;
            $summary['gap_show'] = false;
            $summary['gap_desc'] = "";
        }
        $summary['earn_bonus'] = $config['bonus_gap']['earn_bonus'];
        $summary['favored']  = $favorate->favored_goods($this->goods_id);
        $summary['anoticed'] = $anotice->noticed($this->goods_id);

        $op_info = activity_helper::take_part_opgoods($this->goods_id);
        if($op_info != false) {
            $summary['is_opgoods'] = true;
            $spid = $op_info['special_id'];
            $summary['opgoods_open'] = "xmmz://p.lrlz.com/block/special?title=N元任选&special_id={$spid}&isSpecial=true";
            $summary['opgoods_desc'] = $op_info['desc'];
            $summary['opgoods_shortdesc'] = $op_info['short_desc'];
        } else {
            $summary['is_opgoods'] = false;
            $summary['opgoods_open'] = "";
            $summary['opgoods_desc'] = "";
            $summary['opgoods_shortdesc'] = "";
        }

        $is_fcode = intval($this->goods_info['is_fcode']) == 1 ? true : false;
        $summary['is_fcode'] = $is_fcode;

        if($is_fcode) {
            $fcode = new user_session\fcode();
            $summary['has_fcode'] = $fcode->goods_has_code($this->goods_id,$lock_num) == false ? false : true;
        } else {
            $summary['has_fcode'] = false;
        }
        if($summary['has_fcode']) {
            $summary['fcode_desc'] = "你有F码，可以购买";
        }
        else
        {
            global $config;
            $spid = $config['autosend_fcodes']['fcodes_spid'];

            if(isset($lock_num) && $lock_num > 0) {
                $summary['fcode_desc'] = "F码处于锁定状态，启用后可以购买";
                $summary['fcode_open'] = "xmmz://p.lrlz.com/block/special?title=F码专区&special_id={$spid}&isSpecial=true";
            } else {
                $summary['fcode_desc'] = "你没有该商品的F码";
                $summary['fcode_open'] = "xmmz://p.lrlz.com/block/special?title=F码专区&special_id={$spid}&isSpecial=true";
            }
        }
        $summary['goods_url'] = BASE_SITE_URL . "/mshop/goods_detail?goods_id={$summary['goods_id']}";

        return $summary;
    }

    public function related_goods()
    {
        $combos = array();
        $gifts  = array();
        $bundles = array();

        if(activity_helper::have_combo($this->goods_id)) {
            $combos = activity_helper::combo_goods($this->goods_id);
        }
        if(activity_helper::have_gift($this->goods_id)) {
            $gifts = activity_helper::goods_giftids($this->goods_id);
        }

        foreach ($this->bundles as $bl_id) {
            $gids = activity_helper::bundling_goods($bl_id);
            $bundles = array_merge($bundles,$gids);
        }

        $ret = array_merge($combos,$gifts,$bundles);
        return array_unique($ret);
    }
}