123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace Collection\Lib;
- use Dever;
- class Ranking
- {
- # 积分回调,增加积分榜单
- public function score($log)
- {
- if ($log['type_id'] > 0) {
- $this->up($log['uid'], $log['type_id'], 4, $log['num']);
- }
- }
- # 获取最新的总榜
- public function getInfo($info_id, $type = 1)
- {
- $where['info_id'] = $info_id;
- $where['type'] = $type;
- $info = Dever::db('collection/ranking')->getOne($where);
- return $info;
- }
- # 获取最新的一期
- public function getPeriodsNew($info_id, $type = 1)
- {
- $info = $this->getInfo($info_id, $type);
- if ($info) {
- $where['info_id'] = $info_id;
- $where['ranking_id'] = $info['id'];
- $info = Dever::db('collection/ranking_periods')->getNew($where);
- return $info;
- }
- return false;
- }
- # 获取当前期的数据
- public function getData($periods_id)
- {
- $where['periods_id'] = $periods_id;
- $data = Dever::db('collection/ranking_data')->getAll($where);
- return $data;
- }
- # 加入门票榜单
- public function up($uid, $info_id, $type = 1, $num = 1)
- {
- $periods = $this->getPeriodsNew($info_id, $type);
- if ($periods) {
- $update['uid'] = $uid;
- $update['periods_id'] = $periods['id'];
- $data = Dever::db('collection/ranking_data')->one($update);
- $update['info_id'] = $info_id;
- $update['ranking_id'] = $periods['ranking_id'];
- if ($data) {
- $update['where_id'] = $data['id'];
- if ($num < 0) {
- $update['num'] = $data['num'] - $num;
- } else {
- $update['num'] = $data['num'] + $num;
- }
-
- Dever::db('collection/ranking_data')->update($update);
- } else {
- $update['num'] = $num;
- Dever::db('collection/ranking_data')->insert($update);
- }
- }
- }
- # 加入到cron里 检测所有榜单是否有新的期数,如果没有,自动添加新的一期 collection/lib/ranking.cron
- public function cron_api()
- {
- $ranking = Dever::db('collection/ranking')->getAll();
- if ($ranking) {
- $cur = time();
- foreach ($ranking as $k => $v) {
- $where['ranking_id'] = $v['id'];
- $info = Dever::db('collection/ranking_periods')->getNew($where);
- if (!$info) {
- # 如果没有,就建立新的期数
- $this->createPeriods(1, $cur, $v);
- } else {
- # 如果有,检查结束时间到没到期,如果到了,就把这一期置为已结束,重新建立新的期数
- if ($info['end'] > 0 && $cur > $info['end']) {
- Dever::db('collection/ranking_periods')->update(array('where_id' => $info['id'], 'status' => 2));
- $this->createPeriods($info['periods'] + 1, $cur, $v);
- }
- }
- }
- }
- }
- private function createPeriods($periods, $cur, $info)
- {
- # 查看总榜的结束时间是否结束
- if ($info['end'] && $cur > $info['end']) {
- Dever::db('collection/ranking')->update(array('where_id' => $info['id'], 'status' => 2));
- return;
- }
- $data['info_id'] = $info['info_id'];
- $data['ranking_id'] = $info['id'];
- $data['name'] = '第' . $periods . '期';
- $data['periods'] = $periods;
- $data['users'] = 0;
- $data['status'] = 1;
- $data['start'] = $cur;
- $day = 86400;
- if ($info['periods'] == 1) {
- # 天
- $time = $day;
- } elseif ($info['periods'] == 2) {
- # 周
- $time = $day * 7;
- } elseif ($info['periods'] == 3) {
- # 月
- $time = $day * 30;
- } elseif ($info['periods'] == 4) {
- # 年
- $time = $day * 365;
- } elseif ($info['periods'] == 5) {
- # 永久
- $time = 0;
- }
- if ($time > 0) {
- $data['end'] = $data['start'] + $time;
- } else {
- $data['end'] = 0;
- }
-
- Dever::db('collection/ranking_periods')->insert($data);
- }
- }
|