<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/25
 * Time: 上午11:06
 */
namespace ugc;

use bonus;
use session_helper;
use trans_wapper;

class submit_rule
{
    private $bonus;
    private $type_sn;
    private $per_amount;

    public function __construct($content)
    {
        $this->bonus = boolval($content['bonus']);
        $this->type_sn = $content['type_sn'];
        $this->per_amount = $content['per_amount'];
    }
    private function support_bonus() {
        return $this->bonus;
    }
    public function type_sn() {
        return empty($this->type_sn) ? false : $this->type_sn;
    }
    public function per_amount() {
        return $this->per_amount;
    }
    public function make_bonus($num,&$amount,&$bonus_rate)
    {
        if($this->support_bonus() == false) return false;
        if($this->has_bonus($amount,$bonus_rate) == true) {
            return $this->type_sn();
        }

        try
        {
            $trans = new trans_wapper(null,'submit_rule make_bonus');
            $type = bonus\type::create_by_sn($this->type_sn());
            $bonus_rate = $type->bonus_rate();
            $bonus_value = intval(floatval($this->per_amount()) * $num * 100 + 0.5);

            if($type->isEnd() || $type->remain_over()) {
                $trans->commit();
                return false;
            }

            $left = intval($type->remain_amount() * 100 + 0.5);
            $bonus_value = $bonus_value > $left ? $left : $bonus_value;
            $bonus_value = $bonus_value / 100;

            $params['session_id']  = session_helper::session_id();
            $params['grab_time']   = time();
            $params['bonus_rate'] = $type->bonus_rate();
            $params['type_id'] = $type->getType_id();
            $params['type_sn'] = $this->type_sn();
            $params['bonus_value'] = $bonus_value;
            $params['remain_amount'] = $bonus_value;
            $params['bonus_sn'] = bonus\make_bonus_sn();
            if(session_helper::isLogin()) {
                $params['user_id'] = session_helper::memberid();
                $params['user_mobile'] = session_helper::cur_mobile();
                $params['user_name'] = session_helper::nickname();
                $params['bonus_status'] = 2;
            } else {
                $params['bonus_status'] = 1;
            }

            $mod_bonus = Model('user_bonus');
            $ret = $mod_bonus->add($params);
            if($ret != false)
            {
                if(session_helper::isLogin()) {
                    Model('bonus_type')->edit(['type_id' => $type->getType_id()],
                        array('grabed_num' => array('exp', 'grabed_num+1'),
                            'binded_num'   => array('exp', 'binded_num+1'),
                            'total_num'    => array('exp', 'total_num+1'),
                            'remain_amount'=> array('exp', "remain_amount-{$bonus_value}")));
                }
                else {
                    Model('bonus_type')->edit(['type_id' => $type->getType_id()],
                        array('grabed_num' => array('exp', 'grabed_num+1'),
                              'total_num'  => array('exp', 'total_num+1')));

                }
                dcache($type->getType_sn(),'bonus_type');
            }
            $trans->commit();

            $amount = $bonus_value;
            if($ret == false) {
                return false;
            } else {
                return $this->type_sn();
            }
        }
        catch (\Exception $ex) {
            $trans->rollback();
            return false;
        }
    }
    public function avaliable()
    {
        if($this->type_sn() == false) {
            return false;
        }

        $type = bonus\type::create_by_sn($this->type_sn());
        if($type->isEnd() || $type->remain_over()) {
            return false;
        } else {
            return true;
        }
    }

    private function has_bonus(&$bonus_value,&$bonus_rate)
    {
        $mod_bonus = Model('user_bonus');
        $sessionid = session_helper::session_id();
        if(session_helper::isLogin()) {
            $member_id = session_helper::memberid();
            $cond['session_id|user_id'] = array('_multi'=>true,$sessionid,$member_id);
        }
        else {
            $cond['session_id']  = $sessionid;
        }
        $cond['type_sn'] = $this->type_sn();

        $bonusex = $mod_bonus->get($cond);
        if(empty($bonusex)) {
            return false;
        } else {
            $bonus_value = doubleval($bonusex[0]['bonus_value']);
            $bonus_rate  = intval($bonusex[0]['bonus_rate']);
            return true;
        }
    }
}

class special
{
    private $mItems;
    public function __construct($items) {
        $this->mItems = $items;
    }
    public function special_id() {
        return intval($this->mItems['special_id']);
    }
    public function memberid() {
        return intval($this->mItems['member_id']);
    }
    public function published() {
        return intval($this->mItems['state']) == 0;
    }
    public function editing() {
        return intval($this->mItems['state']) == 1;
    }
    public function preview() {
        return (intval($this->mItems['state']) == 1);
    }
    public function description() {
        return $this->mItems['description'];
    }
    public function has_vote() {
        return ($this->vote_param() != false);
    }
    public function has_question() {
        return ($this->question_param() != false);
    }
    public function vote_param()
    {
        $params = $this->params();
        if(empty($params)) return false;

        $param = $params['vote'];
        if(empty($param)) return false;

        return $param;
    }
    public function question_param()
    {
        $params = $this->params();
        if(empty($params)) return false;

        $param = $params['questions'];
        if(empty($param)) return false;

        return $param;
    }
    public function vote_single()
    {
        $vote_param = $this->vote_param();
        if(empty($vote_param)) return true;

        if(!isset($vote_param['vote_single'])) {
            return true;
        } else {
            return $vote_param['vote_single'];
        }
    }
    private function params()
    {
        $params = $this->mItems['params'];
        if(empty($params)) return false;

        $params = unserialize($params);
        return $params;
    }
    public function deadline()
    {
        $vote_param = $this->vote_param();
        if($vote_param == false) {
            return 0;
        } else {
            $deadline = intval($vote_param['deadline']);
            return $deadline;
        }
    }
    public function has_over()
    {
        $dead_line = $this->deadline();
        if($dead_line == 0) {
            return false;
        } else {
            return $dead_line <= time();
        }
    }
    public function vote_result()
    {
        $result = $this->mItems['vote_result'];
        if(empty($result)) {
            return false;
        }
        else {
            return unserialize($result);
        }
    }
    public function reader_type() {
        return intval($this->mItems['reader_type']);
    }
    public function submit_rule()
    {
        $result = $this->mItems['submit_rule'];
        if(empty($result)) {
            return false;
        }
        else {
            return new submit_rule(unserialize($result));
        }
    }
    public function category_id() {
        return intval($this->mItems['category_id']);
    }
    public function likes() {
        return intval($this->mItems['likes']);
    }
    public function comments() {
        return intval($this->mItems['comments']);
    }
    public function clicks() {
        return intval($this->mItems['clicks']);
    }
    public function has_appreciate() {
        return intval($this->mItems['appreciate']) == 1;
    }
    public function appreciates() {
        return intval($this->mItems['appreciates']);
    }
    public function appreciate_amonut() {
        return intval($this->mItems['appreciate_amonut']);
    }
    public function editime() {
        return intval($this->mItems['editime']);
    }
    public function pubtime() {
        return intval($this->mItems['pubtime']);
    }
    public function bless() {
        return '答题红包';
    }
    public function from_user() {
        return ($this->mItems['from_user'] == 1);
    }
    public function share_img() {
        return $this->mItems['share_image'];
    }
    public function share_title() {
        return $this->mItems['share_title'];
    }
}