stanley-king 8 лет назад
Родитель
Сommit
7dc320f89c

+ 22 - 1
data/model/bonus_toplist.model.php

@@ -4,4 +4,25 @@
  * User: stanley-king
  * Date: 16/7/22
  * Time: 下午3:41
- */
+ */
+
+defined('InShopNC') or exit('Access Invalid!');
+
+class bonus_toplistModel extends Model
+{
+    public function __construct() {
+        parent::__construct('bonus_toplist');
+    }
+    public function getToplist($cond,$field = '*',$order = '') {
+        return $this->field($field)->where($cond)->order($order)->limit(false)->select();
+    }
+
+    public function getByListSn($list_sn) {
+        return $this->where(array('list_sn' => $list_sn))->find();
+    }
+
+    public function edit($cond,$data)
+    {
+        return $this->where($cond)->update($data);
+    }
+}

+ 3 - 0
helper/bonus_helper.php

@@ -8,7 +8,9 @@
 
 require_once (BASE_ROOT_PATH . '/helper/field_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/toplist_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/text_filter.php');
+
 require_once (BASE_ROOT_PATH . '/helper/bonus/util.php');
 require_once (BASE_ROOT_PATH . '/helper/bonus/type.php');
 require_once (BASE_ROOT_PATH . '/helper/bonus/user_bonus.php');
@@ -148,6 +150,7 @@ class bonus_helper
                     throw new Exception();
                 } else {
                     array_push($bonusex,$val);
+                    toplist_helper::add_money($_SESSION['member_id'],$bonus->bonus_value());
                 }
                 Db::commit();
             } catch (Exception $ex) {

+ 31 - 0
helper/date_helper.php

@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/7/24
+ * Time: 下午6:25
+ */
+class date_helper
+{
+    static public function diff_days(DateTime $a,DateTime $b)
+    {
+        if($a->getTimestamp() < $b->getTimestamp()) {
+            return false;
+        }
+        else {
+            $interval = $a->diff($b);
+            return $interval->days;
+        }
+    }
+
+    static public function sub_days($time,$days)
+    {
+        $date = new DateTime();
+        $date->setTimestamp($time);
+        $date->setTime(0,0,0);
+        $ret = $date->sub(new DateInterval("P{$days}D"));
+        return $ret->getTimestamp();
+    }
+}
+

+ 240 - 0
helper/model/bonus_toplist.php

@@ -0,0 +1,240 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/7/22
+ * Time: 下午4:31
+ */
+
+class bonus_toplist
+{
+    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_toplist');
+        $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 = func_get_arg(0);
+            $this->list_date = func_get_arg(1);
+            $this->list_sn = toplist_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'] = serialize($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'] = serialize($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 supports_count()
+    {
+        return count($this->supports);
+    }
+    public function support($other_id)
+    {
+        if(algorithm_helper::bsearch($other_id,$this->supports) == -1) {
+            array_push($this->supports,$other_id);
+            sort($this->supports);
+            $this->dirty = true;
+
+            return true;
+        } else {
+            return false;
+        }
+    }
+    public function unsupport($other_id)
+    {
+        $pos = algorithm_helper::bsearch($other_id,$this->supports);
+        if($pos == -1) {
+            return false;
+        } else {
+            array_splice($this->supports,$pos,1);
+            $this->dirty = true;
+            return true;
+        }
+    }
+    private function init($param)
+    {
+        if(!empty($param)) {
+            $this->list_sn = $param['list_sn'];
+            $this->member_id = $param['member_id'];
+            $this->list_date = $param['list_date'];
+            $this->list_value = $param['list_value'];
+            $this->list_rank = $param['list_rank'];
+            $this->top_id = $param['top_id'];
+            $this->top_money = $param['top_money'];
+            $this->supports = empty($param['supports']) ? array() : unserialize(($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 = array();
+            $this->friends = array();
+
+            $this->update = false;
+            $this->dirty = true; //生成对象之后,为了上层需要,需要存储下来
+        }
+    }
+
+    ///////////////////////////////////////////////////////////////////////
+    static public function create_by_sn($list_sn)
+    {
+        try
+        {
+            return new bonus_toplist($list_sn);
+        } catch (Exception $ex) {
+            return false;
+        }
+    }
+
+    static public function create_by_date($member_id,$date)
+    {
+        try
+        {
+            return new bonus_toplist($member_id,$date);
+        } catch (Exception $ex) {
+            return false;
+        }
+    }
+
+    static public function create_by_store($row_info)
+    {
+        return new bonus_toplist($row_info);
+    }
+}

+ 6 - 1
helper/model/member_info.php

@@ -22,6 +22,9 @@ class member_info
             $this->member_id = $member_id;
             $mod_member = Model('member');
             $this->member_info = $mod_member->getMemberInfoByID($this->member_id);
+            if(empty($this->member_info)) {
+                throw new Exception("该用户不存在~", errcode::ErrMemberNotExist);
+            }
         }
     }
 
@@ -84,7 +87,6 @@ class member_info
             return $url;
         }
     }
-
     public function filter()
     {
         $info['member_id'] = $this->member_id();
@@ -94,4 +96,7 @@ class member_info
 
         return $info;
     }
+    public function reg_time() {
+        return intval($this->member_info['member_time']);
+    }
 }

+ 1 - 0
helper/model_helper.php

@@ -8,3 +8,4 @@
 
 require_once (BASE_ROOT_PATH . "/helper/model/member_info.php");
 require_once (BASE_ROOT_PATH . "/helper/model/order.php");
+require_once (BASE_ROOT_PATH . "/helper/model/bonus_toplist.php");

+ 200 - 0
helper/toplist_helper.php

@@ -0,0 +1,200 @@
+<?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 toplist_helper
+{
+    const start_date = '2016-07-20';
+
+    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 = bonus_toplist::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,&$err)
+    {
+        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) {
+            $err = array('code' => $ex->getCode(),'msg' => $ex->getMessage());
+            return false;
+        }
+    }
+
+    public static function calc_top(bonus_toplist &$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_toplist');
+        $items = $mod_top->getToplist(array('member_id' => array('in',$friends),'list_date' => $list_date),'*','order by 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 = bonus_toplist::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 top_list($member_id,$times)
+    {
+        $ret_list = array();
+        foreach ($times as $val) {
+            $ret_list[$val] = null;
+        }
+
+        $mod_top = Model('bonus_toplist');
+        $items = $mod_top->getToplist(array('member_id' => $member_id,'list_date' => array('in',$times)),'*','order by list_date desc');
+        foreach ($items as $item)
+        {
+            $top = bonus_toplist::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 = bonus_toplist::create_by_date($member_id,$key);
+                if($top->calculated() == false) {
+                    self::calc_top($top);
+                }
+                $ret_list[$top->list_date()] = $top;
+            }
+        }
+
+        return $ret_list;
+    }
+}

+ 6 - 2
mobile/control/control.php

@@ -16,8 +16,8 @@ class mobileControl
     protected $client_type_array = array('android', 'wap', 'wechat', 'ios', 'ajax', 'web','jsonp');
 
     //列表默认分页数
-    protected $page_size = 20;
-    protected $cur_page = 1;
+    protected $page_size;
+    protected $cur_page;
 
     //任务开始时间
     private static $startime = 0;
@@ -30,9 +30,13 @@ class mobileControl
         //分页数处理
         if (is_numeric($_GET['page']) && intval(trim($_GET['page'])) > 0) {
             $this->page_size = intval(trim($_GET['page']));
+        } else {
+            $this->page_size = 20;
         }
         if (is_numeric($_GET['curpage']) && intval(trim($_GET['curpage'])) > 0) {
             $this->cur_page = intval(trim($_GET['curpage']));
+        } else {
+            $this->cur_page = 1;
         }
         initpage($this->page_size, $this->cur_page);
 

+ 21 - 0
mobile/control/member_bonus.php

@@ -10,6 +10,7 @@ require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/text_filter.php');
 require_once (BASE_ROOT_PATH . '/helper/shaker_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/toplist_helper.php');
 
 
 class member_bonusControl extends mbMemberControl
@@ -283,4 +284,24 @@ class member_bonusControl extends mbMemberControl
             return self::outsuccess(array("bonuses" => $bonuses,"info" => array('count' => $count, 'money' => $total_amount)));
         }
     }
+
+    public function top_listOp()
+    {
+        $count = toplist_helper::list_count($_SESSION['member_id'],$err);
+        if($count == false) {
+            return self::outerr($err['code'],$err['msg']);
+        }
+        $start_index = ($this->page_no() - 1) * $this->page_size();
+        $end = $start_index + $this->page_size();
+        $end = $end > $count ? $count : $end;
+
+        $times = array();
+        $start_tm = time();
+        for ($index = $start_index; $index < $end; ++$index) {
+            $date_time = date_helper::sub_days($start_tm,$index);
+            array_push($times,$date_time);
+        }
+
+        $page_obj = toplist_helper::top_list($_SESSION['member_id'],$times);
+    }
 }

+ 58 - 0
test/bouns_toplist_helperTest.php

@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/7/22
+ * Time: 下午4:34
+ */
+
+define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_ROOT_PATH . '/helper/bouns_toplist_helper.php');
+
+class bouns_toplist_helperTest extends PHPUnit_Framework_TestCase
+{
+    public static function setUpBeforeClass()
+    {
+        Base::run_util();
+    }
+
+    public function testMakesn()
+    {
+        $cur_date = '2016-07-21';
+        $start_date = '2015-06-21';
+
+        $x = date_parse($cur_date);
+        $y = date_parse($start_date);
+
+        $start = new DateTime();
+        $start->setDate($x['year'],$x['month'],$x['day']);
+        $start->setTime(0,0,0);
+
+        $end = new DateTime();
+        $end->setDate($y['year'],$y['month'],$y['day']);
+        $end->setTime(0,0,0);
+
+        $inter = $start->diff($end);
+        $inter->days;
+    }
+
+    public function testListCount()
+    {
+        $count = toplist_helper::list_count(36490,$err);
+    }
+
+
+    public function testAddMondey()
+    {
+        toplist_helper::add_money(36490,1.25);
+    }
+
+
+    public static function tearDownAfterClass()
+    {
+
+    }
+}