<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/5/11
 * Time: 上午11:56
 */

namespace fcode;

require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');

use StatesHelper;
use Log;
use special_manager;

class send_manager
{
    public static $stInstance;
    private $mNormalGoods;

    private function __construct()
    {
    }

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

        return self::$stInstance;
    }

    private function init()
    {
        $this->mNormalGoods = [];

        global $config;
        $fcodes_id = $config['autosend_fcodes']['fcodes_spid'];
        $blocks = special_manager::instance()->special($fcodes_id,$goods_ids);

        $cur_amount = 0;
        $cur_block = false;
        foreach ($blocks as $block)
        {
            $item_type  = $block['item_type'];
            if($item_type == 'home1')
            {
                $items = $block['items'];
                if(empty($items)) continue;

                $item = $items[0];
                $amount = intval($item['reserved']);
                if($amount > 0) {
                    $cur_amount = $amount;
                    $cur_block = $block;
                }
            }
            elseif ($item_type == 'home_goods')
            {
                $items = $block['items'];
                if(empty($items)) continue;

                foreach ($items as $item)
                {
                    if($item['type'] == "goods")
                    {
                        $goods_id = intval($item['data']);
                        $batch_code = $item['reserved'];
                        if($cur_amount > 0 && $goods_id > 0 && empty($batch_code) == false) {
                            $this->add($cur_amount,$goods_id,$batch_code,$cur_block);
                        }
                    }
                }
            }
        }

        krsort($this->mNormalGoods);
    }

    private function add($amount,$goods_id,$batch_code,$cur_block)
    {
        if(array_key_exists($amount,$this->mNormalGoods) == false) {
            $this->mNormalGoods[$amount] = [];
        }

        $this->mNormalGoods[$amount][] = ['goods_id' => $goods_id,'batch_code' => $batch_code,'block' => $cur_block];
    }

    public function fetch($amount,$pay_sn,$mobile,$session_id)
    {
        $normal = $this->fetch_normal($amount,$pay_sn,$mobile,$session_id);

        $result = [];
        if(!empty($normal)) {
            $result['fcode'][]  = $normal['fcode'];
            $result['banner'][] = $normal['banner'];
        }

        return empty($result) ? false : $result;
    }

    private function fetch_normal($amount,$pay_sn,$mobile,$session_id)
    {
        $amount = intval($amount);
        if($amount <= 0) return false;

        $fcode_goods = [];
        foreach ($this->mNormalGoods as $key => $items)
        {
            if(empty($items)) continue;
            if($key <= $amount)
            {
                foreach ($items as $item) {
                    $fcode_goods[] = $item;
                }
            }
        }
        if(empty($fcode_goods)) {
            return false;
        }

        $result = $this->validate_one($fcode_goods,$pay_sn,$mobile,$session_id);
        return $result;
    }

    private function validate_one($fcode_goods,$pay_sn,$mobile,$session_id)
    {
        $gid_codes = [];
        $gids = [];
        foreach ($fcode_goods as $item) {
            $gid = $item['goods_id'];
            $gids[] = $gid;
            $gid_codes[$gid] = $item;
        }

        $mod_goods  = Model('goods');
        $goods_list = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids),'goods_storage' => array('gt',0),'is_fcode' => 1));
        if(empty($goods_list)) return false;

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

        foreach ($gids as $gid)
        {
            if(array_key_exists($gid,$all_goods))
            {
                $goods = $all_goods[$gid];

                $batch_code = $gid_codes[$gid]['batch_code'];
                $block = $gid_codes[$gid]['block'];

                $commonid = $goods['goods_commonid'];
                $oper = new operator($commonid,$batch_code,$mobile,$session_id);
                if($oper->grabed() == false)
                {
                    $fcode = $oper->lock($pay_sn);
                    if($fcode != false) {
                        return ['fcode' => $fcode,'banner' => $block];
                    }
                }
            }
        }

        return false;
    }
}