<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/9/29
 * Time: 下午1:48
 */

class commonid_helper
{
    static private $stInstance = null;

    private $mGid2CidContainer;
    private $mCid2GidsContainer;
    private $mMainImage;

    private function __construct()
    {
    }

    static public function instance()
    {
        if(self::$stInstance == null) {
            self::$stInstance = new commonid_helper();
        }
        if(StatesHelper::fetch_state('common_info')) {
            Log::record(__METHOD__ . " reinit.",Log::DEBUG);
            self::$stInstance->init();
        }
        return self::$stInstance;
    }
    private function init()
    {
        $this->mGid2CidContainer = [];
        $this->mCid2GidsContainer = [];
        $this->mMainImage = [];
    }

    public function main_image($goods_id)
    {
        $goods_id = intval($goods_id);
        if($goods_id <= 0) return false;
        $commonid = $this->common_id($goods_id);
        if($commonid == false) {
            return false;
        }
        else
        {
            if(array_key_exists($commonid,$this->mMainImage)) {
                return $this->mMainImage[$commonid];
            }
            else
            {
                $mod_goods = Model('goods');
                $items = $mod_goods->getGoodsCommonList(['goods_commonid' => $commonid],'store_id,goods_image');
                if(empty($items)) {
                    return false;
                }
                else
                {
                    $goods = $items[0];
                    $storeid = intval($goods['store_id']);
                    $image = $goods['goods_image'];
                    if($storeid > 0 && !empty($image)) {
                        $main_url = $this->img_url($image,$storeid);
                    }
                    else {
                        $main_url = '';
                    }
                    $this->mMainImage[$commonid] = $main_url;

                    return $main_url;
                }
            }
        }
    }

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

    public function common_id($goods_id)
    {
        $goods_id = intval($goods_id);
        if($goods_id <= 0) return false;

        if(isset($this->mGid2CidContainer[$goods_id])) {
            return $this->mGid2CidContainer[$goods_id];
        }
        else
        {
            $mod = Model('goods');
            $goods_info = $mod->getGoodsInfoByID($goods_id);
            if(empty($goods_info)) {
                Log::record("fetch goods_commonid err goods_id={$goods_id}",Log::ERR);
                return false;
            }
            else
            {
                $common_id = intval($goods_info['goods_commonid']);
                $this->mGid2CidContainer[$goods_id] = $common_id;

                $items = $mod->getGoodsOnlineList(array('goods_commonid' => $common_id));

                $this->mCid2GidsContainer[$common_id] = [];
                foreach ($items as $item) {
                    $goods_id = intval($item['goods_id']);
                    $this->mCid2GidsContainer[$common_id][] = $goods_id;
                }

                return $common_id;
            }
        }
    }

    public function goods_ids($common_id)
    {
        if(intval($common_id) <= 0) return false;

        if(isset($this->mCid2GidsContainer[$common_id]))
        {
            if(empty($this->mCid2GidsContainer[$common_id])) {
                return false;
            } else {
                return $this->mCid2GidsContainer[$common_id];
            }
        }
        else
        {
            $mod = Model('goods');
            $items = $mod->where(array('goods_commonid' => $common_id))->field('goods_id')->limit(false)->select();
            if(empty($items)) {
                $this->mCid2GidsContainer[$common_id] = [];
                return false;
            }
            else
            {
                $this->mCid2GidsContainer[$common_id] = [];
                foreach ($items as $item) {
                    $goods_id = intval($item['goods_id']);
                    $this->mCid2GidsContainer[$common_id][] = $goods_id;
                }

                return $this->mCid2GidsContainer[$common_id];
            }
        }
    }
    public function one_goods($common_id)
    {
        $gids = $this->goods_ids($common_id);
        if($gids == false) {
            return false;
        } else {
            return $gids[0];
        }
    }
}