<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/7/22
 * Time: 下午4:33
 */

require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
require_once(BASE_ROOT_PATH . '/helper/date_helper.php');

class ranklist_helper
{
    const start_date = '2016-08-01';

    static private function date_stamp()
    {
        $num = func_num_args();
        if($num == 0) {
            $day = new DateTime();
            $day->setTimestamp(time());
            $day->setTime(0,0,0);

            return $day->getTimestamp();
        }
        else if($num == 1) {
            $time = func_get_arg(0);
            $day = new DateTime();
            $day->setTimestamp($time);
            $day->setTime(0,0,0);

            return $day->getTimestamp();
        }
        else if($num == 3) {
            $year = func_get_arg(0);
            $mon  = func_num_arg(1);
            $mday = func_get_arg(2);

            $day = new DateTime();
            $day->setDate($year,$mon,$mday);
            $day->setTime(0,0,0);

            return $day->getTimestamp();
        }
        else {
            return false;
        }
    }

    static public function make_sn($member_id,$time)
    {
        $date_time = self::date_stamp($time);
        $sn = md5("{$member_id}-{$date_time}");

        return $sn;
    }

    static public function add_money($member_id,$bonus_val)
    {
        $top_list = ranklist::create_by_date($member_id,self::date_stamp());
        if($top_list == false) {
            return false;
        } else {
            $top_list->add_money($bonus_val);
            return true;
        }
    }

    static private function interval_days($end_time)
    {
        $now_date = new DateTime('now');

        $dt = new DateTime();
        $dt->setTimestamp($end_time);
        $dt->setTime(0,0,0);

        return $now_date->diff($dt)->days;
    }

    static private function def_start_time()
    {
        $param = date_parse(self::start_date);
        $dt = new DateTime();
        $dt->setDate($param['year'],$param['month'],$param['day']);
        $dt->setTime(0,0,0);

        return $dt->getTimestamp();
    }

    public static function list_count($member_id)
    {
        try
        {
            $minfo = new member_info($member_id);
            $reg_time = $minfo->reg_time();
            if($reg_time <= 0) {
                return self::interval_days(self::def_start_time());
            }
            elseif($reg_time <= self::def_start_time()) { // 注册时间,比该功能推出时间早
                return self::interval_days(self::def_start_time());
            }
            else {
                return self::interval_days($reg_time);
            }
        }
        catch (Exception $ex) {
            return false;
        }
    }

    public static function gen_ranklist($list_sn,&$userid,&$err)
    {
        $rank = ranklist::create_by_sn($list_sn);
        if($rank == false) {
            $err = array(errcode::ErrParamter,'该排行榜不存在或者已经过期~');
            return false;
        }
        $friends = $rank->get_friends();
        $userid = $rank->member_id();
        $mod_rank = Model('bonus_ranklist');
        $cond = array('list_date' => $rank->list_date(),'member_id' => array('in',$friends));
        $items = $mod_rank->getRanklist($cond,'*','list_value desc');
        if(!empty($items)) {
            $top = ranklist::create_by_store($items[0]);
            $rank->set_top($top->member_id(),$top->money());
        }

        return $items;
    }

    public static function calc_top(ranklist &$top)
    {
        $member_id = $top->member_id();
        $list_date = $top->list_date();

        if(empty($top->get_friends()))
        {
            $relation = new \relation\mem_relation($_SESSION['member_id']);
            $friends = array_merge($relation->follower(),$relation->subscriber());
            array_push($friends,intval($member_id));

            $cur_friends = array();
            foreach ($friends as $val) {
                if(!empty($val) && intval($val) > 0) {
                    array_push($cur_friends,intval($val));
                }
            }

            $top->set_friends($cur_friends);
        } else {
            $friends = $top->get_friends();
        }

        $mod_top = Model('bonus_ranklist');
        $items = $mod_top->getRanklist(array('member_id' => array('in',$friends),'list_date' => $list_date),'*','list_value desc');

        if(empty($items)) {
            $top->set_top($member_id,$top->money());
            $top->set_rank(0);
        }
        else
        {
            $first = true;
            $index = 0;
            $finded = false;
            foreach ($items as $val)
            {
                $obj = ranklist::create_by_store($val);
                if($first) {
                    $top->set_top($obj->member_id(),$obj->money());
                    $first = false;
                }
                if($obj->member_id() == $top->member_id()) {
                    $top->set_rank($index);
                    $finded = true;
                }

                ++$index;
            }

            if($finded == false) {
                $top->set_rank($index);
            }
        }
    }

    public static function ranklists($member_id, $times)
    {
        $ret_list = array();
        foreach ($times as $val) {
            $ret_list[$val] = null;
        }

        $mod_top = Model('bonus_ranklist');
        $items = $mod_top->getRanklist(array('member_id' => $member_id,'list_date' => array('in',$times)),'*','list_date desc');
        foreach ($items as $item)
        {
            $top = ranklist::create_by_store($item);
            if($top->calculated() == false) {
                self::calc_top($top);
            }
            $ret_list[$top->list_date()] = $top;
        }

        foreach ($ret_list as $key => $val)
        {
            if(is_null($val)) {
                $top = ranklist::create_by_date($member_id,$key);
                if($top->calculated() == false) {
                    self::calc_top($top);
                }
                $ret_list[$top->list_date()] = $top;
            }
        }

        return $ret_list;
    }
}