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

namespace activity;

use algorithm;
use Log;
use StatesHelper;

class bundling
{
    const STATE_OPEN = 1;        // 开启
    const STATE_CLOSE = 0;       // 关闭
    const STORE_ID = 6;
    const cache_name = 'all_bundling';

    static private $stInstance = null;
    private $bundling_list;
    private $bundling_goods;
    private $goods_ids;

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

        return self::$stInstance;
    }

    private function init()
    {
        $data = $this->read_cache();
        $this->bundling_list  = $data['list'];
        $this->bundling_goods = $data['goods'];
        $this->goods_ids = $data['ids'];
    }

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

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

        $bundling_list = [];
        $bundling_goods = [];
        $goods_ids = [];

        $mod = Model('p_bundling');
        $list = $mod->getBundlingList(array('bl_state' => self::STATE_OPEN));
        foreach ($list as $val)
        {
            $bl_id = intval($val['bl_id']);
            $bundling_list[$bl_id] = $val;
            $bundling_goods[$bl_id] = [];
            $bgoods = $mod->getBundlingGoodsList(array('bl_id' => $bl_id, 'bl_appoint' => 1), 'goods_id,bl_goods_price');
            foreach ($bgoods as $goods) {
                $goods_id =intval($goods['goods_id']);
                array_push($bundling_goods[$bl_id],$goods);
                $goods_ids[$goods_id][] = $bl_id;
            }
        }

        $data = array('list' => $bundling_list,'goods' => $bundling_goods, 'ids' => $goods_ids);
        wcache(self::cache_name, array('data' => serialize($data)), $prefix);

        return $data;
    }


    public function have_bundling($goods_id)
    {
        $goods_id = intval($goods_id);
        if(isset($this->goods_ids[$goods_id])) {
            return $this->goods_ids[$goods_id];
        }

        return false;
    }

    private function format($act_id,&$goods_ids)
    {
        $val = $this->bundling_list[$act_id];

        $ret['bl_id'] = intval($val['bl_id']);
        $ret['bl_name'] = $val['bl_name'];
        $ret['bl_price'] = doubleval($val['bl_discount_price']);

        $goods_ids = [];

        $bl_goods = $this->bundling_goods[$act_id];
        foreach ($bl_goods as $goods)
        {
            $goods_id = intval($goods['goods_id']);
            $item['goods_id'] = $goods_id;
            $item['bl_goods_price'] = doubleval($goods['bl_goods_price']);

            if(algorithm::binary_search($goods_ids,$goods_id) == false) {
                $pos = algorithm::lower_bonud($goods_ids,$goods_id);
                algorithm::array_insert($goods_ids,$pos,$goods_id);
            }

            $ret['goods'][] = $item;
        }

        return $ret;
    }

    public function get_info($act_id,&$goods_ids)
    {
        $bl_id = intval($act_id);
        if(isset($this->bundling_list[$bl_id]) && isset($this->bundling_goods[$bl_id])) {
            return $this->format($bl_id,$goods_ids);
        }
        else {
            return false;
        }
    }

    public function bundling_goods($bl_id)
    {
        $bl_id = intval($bl_id);
        if(isset($this->bundling_list[$bl_id]) && isset($this->bundling_goods[$bl_id]))
        {
            $goods_ids = array();
            $bl_goods = $this->bundling_goods[$bl_id];
            foreach ($bl_goods as $goods) {
                $goods_ids[] = intval($goods['goods_id']);
            }
            return $goods_ids;
        }
        else {
            return false;
        }
    }
}