<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/17
 * Time: 下午3:49
 */

namespace activity;

use Log;
use StatesHelper;


class full_item
{
    private $mStart;
    private $mEnd;
    private $mName;
    private $mRules;

    public function __construct($start,$end,$name)
    {
        $this->mStart = $start;
        $this->mEnd = $end;
        $this->mName = $name;

        $this->mRules = [];
    }

    public function add_rule($title)
    {
        $this->mRules[] = $title;
    }
    public function is_runing()
    {
        $cur = time();
        return ($cur >= $this->mStart && $cur < $this->mEnd);
    }
    public function rules() {
        return $this->mRules;
    }
}

class full_sent
{
    const MANSONG_STATE_NORMAL = 1;
    const MANSONG_STATE_CLOSE = 2;
    const MANSONG_STATE_CANCEL = 3;
    const STORE_ID = 6;

    static private $stInstance = null;
    private $mItem;
    private $mFreePrice;

    private function __construct()
    {
    }

    static public function instance()
    {
        if(self::$stInstance == null) {
            self::$stInstance = new full_sent();
        }

        if(StatesHelper::fetch_state('full_sent')) {
            Log::record("full_sent reinit data.",Log::DEBUG);
            self::$stInstance->init();
        }

        return self::$stInstance;
    }

    public function rules()
    {
        if(is_null($this->mItem) == false && $this->mItem->is_runing()) {
            return $this->mItem->rules();
        }
        else {
            return array();
        }
    }

    public function free_price()
    {
        if($this->mFreePrice > 0) {
            $val = $this->mFreePrice / 100;
            return "专柜价满{$val}元（除抢购商品外）包邮";
        } else {
            return false;
        }
    }

    private function init()
    {
        $store_list = Model('store')->getStoreOnlineList(array('store_id' => 6),null,'','store_free_price');
        if(empty($store_list)) {
            $this->mFreePrice = 0;
        } else {
            $this->mFreePrice = intval($store_list[0]['store_free_price'] * 100 + 0.5);
        }

        $mod = Model('p_mansong');
        $value = $mod->getMansongInfoByStoreID(self::STORE_ID);
        if(empty($value)) {
            $this->mItem = null;
        }
        else
        {
            $start = intval($value['start_time']);
            $end   = intval($value['end_time']);
            $name  = $value['mansong_name'];

            $this->mItem = new full_item($start,$end,$name);

            $rules = $value['rules'];
            foreach ($rules as $rule)
            {
                $val = $this->filter($rule);
                if($val != false) {
                    $this->mItem->add_rule($val);
                }
            }
        }
    }

    private function filter($rule)
    {
        $discount = doubleval($rule['discount']) * 100 + 0.5;
        $discount = intval($discount);

        if($discount > 0) {
            $discount = $discount / 100;
            $desc = "减{$discount}";
        } else {
            $desc = "";
        }

        $goods_id = intval($rule['goods_id']);
        if($goods_id > 0) {
            $goods_name = $rule['mansong_goods_name'];
            $name = "赠{$goods_name}";
        } else {
            $name = '';
        }

        if(empty($desc) && empty($name)) {
            return false;
        }

        $price = $rule['price'];
        if(empty($desc)) {
            $title = "满{$price}{$name}";
        }
        elseif(empty($name)) {
            $title = "满{$price}{$desc}";
        }
        else {
            $title = "满{$price}{$desc}，{$name}";
        }

        return $title;
    }
}