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

class ranklist
{
    private $list_sn;
    private $member_id;
    private $list_date;
    private $list_value;
    private $supports;
    private $top_id;
    private $top_money;
    private $list_rank;
    private $friends;

    private $update;
    private $dirty;
    private $mod_toplist;

    private function __construct()
    {
        $cl = __CLASS__;

        $this->dirty = false;
        $this->mod_toplist = Model('bonus_ranklist');
        $num = func_num_args();
        if($num == 1)
        {
            $param = func_get_arg(0);
            if(empty($param)) {
                throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
            }

            if(is_array($param)) {
                $this->init($param);
            }
            else
            {
                $param = $this->mod_toplist->getByListSn($param);
                if(empty($param)) {
                    throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
                } else {
                    $this->init($param);
                }
            }
        }
        elseif($num == 2) {
            $this->member_id = intval(func_get_arg(0));
            $this->list_date = intval(func_get_arg(1));
            $this->list_sn = ranklist_helper::make_sn($this->member_id,$this->list_date);

            $param = $this->mod_toplist->getByListSn($this->list_sn);
            $this->init($param);
        }
        else {
            throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
        }
    }

    public function __destruct()
    {
        if($this->dirty)
        {
            if($this->update) {
                $data = array();
                $data['list_value'] = $this->list_value;
                $data['supports'] = $this->supports;
                $data['friends'] = serialize($this->friends);
                $data['top_id'] = $this->top_id;
                $data['top_money'] = $this->top_money;
                $data['list_rank'] = $this->list_rank;

                $this->mod_toplist->edit(array('list_sn' => $this->list_sn),$data);
            } else {
                $data = array();
                $data['list_value'] = $this->list_value;
                $data['list_sn'] = $this->list_sn;
                $data['member_id'] = $this->member_id;
                $data['list_date'] = $this->list_date;
                $data['supports'] = $this->supports;
                $data['friends'] = serialize($this->friends);
                $data['top_id'] = $this->top_id;
                $data['top_money'] = $this->top_money;
                $data['list_rank'] = $this->list_rank;

                $this->mod_toplist->insert($data);
            }
        }
    }

    public function add_money($money)
    {
        $penny = intval($money * 100 + 0.5);
        if($penny > 0) {
            $this->list_value += $money;
            $this->dirty = true;
        }
    }
    public function set_top($top_id,$top_money)
    {
        if($this->top_id != $top_id) {
            $this->dirty = true;
            $this->top_id = $top_id;
        }
        if(intval($this->top_money * 100 + 0.5) != intval($top_money * 100 + 0.5)) {
            $this->dirty = true;
            $this->top_money = $top_money;
        }
    }
    public function get_rank() {
        return $this->list_rank;
    }
    public function set_rank($rank)
    {
        if($this->list_rank != $rank) {
            $this->list_rank = $rank;
            $this->dirty = true;
        }
    }
    public function top_id() {
        return $this->top_id;
    }
    public function top_money() {
        return $this->top_money;
    }
    public function calculated() {
        return $this->top_id > 0;
    }
    public function money()
    {
        return $this->list_value;
    }
    public function list_date() {
        return$this->list_date;
    }
    public function list_sn() {
        return $this->list_sn;
    }
    public function member_id() {
        return $this->member_id;
    }
    public function get_friends() {
        return $this->friends;
    }
    public function set_friends($friends)
    {
        if(empty($this->friends) && !empty($friends)) {
            $this->friends = $friends;
            $this->dirty = true;
        }
    }
    public function supports()
    {
        return $this->supports;
    }

    public function support()
    {
        $this->supports += 1;
        $this->dirty = true;
    }

    public function unsupport()
    {
        if($this->supports >= 1) {
            $this->supports -= 1;
            $this->dirty = true;
        }
    }

    private function init($param)
    {
        if(!empty($param)) {
            $this->list_sn = $param['list_sn'];
            $this->member_id = intval($param['member_id']);
            $this->list_date = intval($param['list_date']);
            $this->list_value = $param['list_value'];
            $this->list_rank = intval($param['list_rank']);
            $this->top_id = intval($param['top_id']);
            $this->top_money = $param['top_money'];
            $this->supports = intval($param['supports']);
            $this->friends = empty($param['friends']) ? array() : unserialize(($param['friends']));

            $this->update = true;
        } else {
            $this->list_value = 0.00;
            $this->list_rank = 0;
            $this->top_id = 0;
            $this->top_money = 0.00;
            $this->supports = 0;
            $this->friends = array();

            $this->update = false;
            $this->dirty = true; //生成对象之后,为了上层需要,需要存储下来
        }
    }

    ///////////////////////////////////////////////////////////////////////
    static public function create_by_sn($list_sn)
    {
        try
        {
            return new ranklist($list_sn);
        } catch (Exception $ex) {
            return false;
        }
    }

    static public function create_by_date($member_id,$date)
    {
        try
        {
            return new ranklist($member_id,$date);
        } catch (Exception $ex) {
            return false;
        }
    }

    static public function create_by_store($row_info)
    {
        return new ranklist($row_info);
    }
}