<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/4/17
 * Time: 下午7:03
 */

//  `bonus_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
//  `type_id` tinyint(3) unsigned NOT NULL DEFAULT '0',
//  `bonus_sn` bigint(20) unsigned NOT NULL DEFAULT '0',
//  `bonus_value` decimal(10,2) NOT NULL DEFAULT '0.00',
//  `user_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
//  `user_mobile` varchar(20) NOT NULL DEFAULT '',
//  `user_name` varchar(45) NOT NULL,
//  `user_comment` varchar(45) NOT NULL DEFAULT '',
//  `get_time` int(11) unsigned NOT NULL DEFAULT '0',
//  `status` int(2) NOT NULL DEFAULT '0' COMMENT '0 没有被抢，1，被抢，2 被领',
//  `grab_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '抢红包时间点',
//  `session_id` varchar(45) NOT NULL DEFAULT '',

namespace bonus;

use \Exception;
use \errcode;

class user_bonus
{
    const OriginStatus = 0;
    const GrabStatus = 1;
    const BindStatus = 2;
    const TopupStatus = 3;

    private $mParamer;

    private function __construct($param) {
        $this->mParamer = $param;
    }
    public function isBinded() {
        return (intval($this->mParamer['bonus_status']) >= 2);
    }
    public function grab_time()
    {
        return intval($this->mParamer['grab_time']);
    }
    public function bonus_sn() {
        return $this->mParamer['bonus_sn'];
    }
    public function bonus_id() {
        return intval($this->mParamer['bonus_id']);
    }
    public function type_id() {
        return intval($this->mParamer['type_id']);
    }
    public function bonus_value() {
        $val = intval(doubleval($this->mParamer['bonus_value']) * 100 + 0.5);
        return doubleval($val) / 100;
    }
    public function bonus_status()
    {
        if(!isset($this->mParamer['bonus_status']) || empty($this->mParamer['bonus_status'])) {
            return 0;
        } else {
            return intval($this->mParamer['bonus_status']);
        }
    }
    public function can_shake() {
        $status = intval($this->mParamer['bonus_status']);
        return ($status == self::GrabStatus || $status == self::BindStatus);
    }
    public function expired() {
        $usable_time = intval($this->mParamer['usable_time']);
        return ($usable_time <= time());
    }

    public function remain_amount() {
        $val = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
        return doubleval($val) / 100;
    }
    public function pay_amount() {
        $val = intval(doubleval($this->mParamer['pay_amount']) * 100 + 0.5);
        return doubleval($val) / 100;
    }
    public function send_amount() {
        $val = intval(doubleval($this->mParamer['send_amount']) * 100 + 0.5);
        return doubleval($val) / 100;
    }

    public function spend_over() {
        $remain = intval(doubleval($this->mParamer['remain_amount']) * 100 + 0.5);
        return ($remain == 0);
    }

    public function type_sn() {
        return $this->mParamer['type_sn'];
    }
    public function get_param() {
        return $this->mParamer;
    }
    public function user_mobile() {
        return $this->mParamer['user_mobile'];
    }
    public function user_name()
    {
        $user_name = $this->mParamer['user_name'];
        if(empty($user_name)) {
            $user_name = substr_replace($this->mParamer['user_mobile'], '****', 3, 4);
        }
        return $user_name;
    }
    public function user_id() {
        return intval($this->mParamer['user_id']);
    }
    public function user_comment()
    {
        $user_comment = $this->mParamer['user_comment'];
        if(mb_strlen($user_comment) < 20) {
            return $user_comment;
        } else {
            return mb_substr($user_comment,0,15) . '...';
        }
    }
    public function get_time()
    {
        return intval($this->mParamer['get_time']);
    }
    public function get_time_format() {
        $get_time = $this->mParamer['get_time'];
        return strftime("%m-%d %H:%M",$get_time);
    }
    public function usable_time() {
        return intval($this->mParamer['usable_time']);
    }
    public function bonus_rate() {
        return intval($this->mParamer['bonus_rate']);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static public function create_by_param($param) {
        return new user_bonus($param);
    }
    static public function create_by_sn($bonus_sn)
    {
        $mod_bonus = Model('user_bonus');
        $items = $mod_bonus->get(array('bonus_sn' => $bonus_sn));

        if(empty($items)) {
            throw new Exception("错误的红包SN号.",errcode::ErrBonusSN);
        } else {
            return new user_bonus($items[0]);
        }
    }
}