<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/15
 * Time: 下午10:21
 */

namespace activity;
use Log;
use StatesHelper;


class limit_item
{
    private $mParam;
    public function __construct($param)
    {
        $this->mParam = null;
        if(isset($param) && is_array($param))
        {
            $this->mParam = $param;
        }
    }

    public function format()
    {
        $info['xianshi_id']      = intval($this->mParam['xianshi_id']);
        $info['xianshi_name']    = $this->mParam['xianshi_name'];
        $info['xianshi_title']   = $this->mParam['xianshi_title'];
        $info['xianshi_explain'] = $this->mParam['xianshi_explain'];
        $info['start_time']  = intval($this->mParam['start_time']);
        $info['end_time']    = intval($this->mParam['end_time']);
        $info['lower_limit'] = intval($this->mParam['lower_limit']);
        $info['state']       = intval($this->mParam['state']);
        $info['xianshi_type']       = intval($this->mParam['xianshi_type']);

        return $info;
    }
}

class limitime
{
    const XIANSHI_STATE_NORMAL = 1;
    const XIANSHI_STATE_CLOSE = 2;
    const XIANSHI_STATE_CANCEL = 3;

    const cache_name = 'all_limitime';
    const STORE_ID = 6;

    static private $stInstance = null;
    private $limit_list;
    private $mGoodsidLimitGoods;
    private $mLimitGoods;

    private function __construct()
    {
    }
    static public function instance()
    {
        if(self::$stInstance == null) {
            self::$stInstance = new limitime();
        }
        if(StatesHelper::fetch_state('limit')) {
            Log::record("limit reinit data.",Log::DEBUG);
            self::$stInstance->init();
        }

        return self::$stInstance;
    }

    private function init()
    {
        $data = $this->read_cache();

        $this->limit_list = $data['limit_list'];
        $this->mGoodsidLimitGoods = $data['goodsidLimitGoods'];
        $this->mLimitGoods = $data['limitGoods'];
    }

    private function read_cache()
    {
        $prefix = 'mb_activity';
        $items = rcache(self::cache_name, $prefix);

        if (!empty($items)) {
            return unserialize($items['data']);
        }

        $limit_list = [];
        $goodsidLimitGoods = [];
        $limitGoods = [];

        $mod = Model('p_xianshi');
        $condition = array();
        $condition['store_id'] = self::STORE_ID;
        $condition['state'] = self::XIANSHI_STATE_NORMAL;

        $list = $mod->getXianshiList($condition,null,'xianshi_type asc');
        foreach ($list as $val)
        {
            $xianshi_id = intval($val['xianshi_id']);
            $limit_list[$xianshi_id] = $val;
            $limitGoods[$xianshi_id] = [];


            $gids = [];
            $goods_list = Model('p_xianshi_goods')->getXianshiGoodsList(array('xianshi_id' => $xianshi_id));
            foreach ($goods_list as $xianshi_goods) {
                $goods_id = intval($xianshi_goods['goods_id']);
                self::add_limitgoods($goodsidLimitGoods,$goods_id,$xianshi_goods);
                $gids[] = $goods_id;
            }
            $limitGoods[$xianshi_id] = $gids;
            $limit_list[$xianshi_id]['goods_list'] = $gids;
        }

        $data = array('limit_list' => $limit_list,'goodsidLimitGoods' => $goodsidLimitGoods, 'limitGoods' => $limitGoods);
        wcache(self::cache_name, array('data' => serialize($data)), $prefix);

        return $data;
    }

    private static function add_limitgoods(&$goodsidLimitGoods,$goods_id,$limit_goods)
    {
        if(isset($goodsidLimitGoods[$goods_id])) {
            $goodsidLimitGoods[$goods_id][] = $limit_goods;
        } else {
            $goodsidLimitGoods[$goods_id] = [];
            $goodsidLimitGoods[$goods_id][] = $limit_goods;
        }
    }

    public function isTakepart($goods_id,&$act_id)
    {
        if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
        {
            $limits = $this->mGoodsidLimitGoods[$goods_id];

            foreach ($limits as $limit)
            {
                $start  = $limit['start_time'];
                $end    = $limit['end_time'];

                $cur = time();
                if($cur >= intval($start) && $cur < intval($end)) {
                    $act_id = intval($limit['xianshi_id']);
                    return true;
                }
            }
            return false;
        }
        else {
            return false;
        }
    }

    public function get_info($act_id)
    {
        if(isset($this->limit_list[$act_id])) {
            $limit = $this->limit_list[$act_id];
            $item = new limit_item($limit);
            return $item->format();
        }
        else {
            return false;
        }
    }

    public function promotion_price($limit_id,$goods_id,&$price)
    {
        if(isset($this->mGoodsidLimitGoods[intval($goods_id)]))
        {
            $limit_goods = $this->mGoodsidLimitGoods[$goods_id];
            foreach ($limit_goods as $goods)
            {
                $id = intval($goods['xianshi_id']);
                if($id == $limit_id) {
                    $price = doubleval($goods['xianshi_price']);
                    return true;
                }
            }
            return false;
        }
        else {
            return false;
        }
    }

    public function goods($limit_id)
    {
        if(isset($this->limit_list[$limit_id])) {
            return $this->mLimitGoods[$limit_id];
        } else {
            return array();
        }
    }

    public function acting()
    {
        $limits = [];
        $curtm = time();
        foreach ($this->limit_list as $limit)
        {
            $start = intval($limit['start_time']);
            $end = intval($limit['end_time']);
            if($curtm >= $start && $curtm < $end) {
                $limits[] = $limit;
            }
        }

        $result = [];
        foreach ($limits as $val) {
            $item = new limit_item($val);
            $result[] = $item->format();
        }

        return $result;
    }

    public function unstart()
    {
        $limits = [];
        $curtm = time();
        foreach ($this->limit_list as $limit)
        {
            $start = intval($limit['start_time']);
            if($curtm < $start) {
                $limits[] = $limit;
            }
        }

        $result = [];
        foreach ($limits as $val) {
            $item = new limit_item($val);
            $result[] = $item->format();
        }

        return $result;
    }
}