Ranking.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Ranking
  5. {
  6. # 积分回调,增加积分榜单
  7. public function score($log)
  8. {
  9. $this->up($log['uid'], $log['type_id'], 4, $log['num']);
  10. }
  11. # 获取最新的总榜
  12. public function getInfo($info_id, $type = 1)
  13. {
  14. $where['info_id'] = $info_id;
  15. $where['type'] = $type;
  16. $info = Dever::db('collection/ranking')->getOne($where);
  17. return $info;
  18. }
  19. # 获取最新的一期
  20. public function getPeriodsNew($info_id, $type = 1)
  21. {
  22. $info = $this->getInfo($info_id, $type);
  23. if ($info) {
  24. $where['info_id'] = $info_id;
  25. $where['ranking_id'] = $info['id'];
  26. $info = Dever::db('collection/ranking_periods')->getNew($where);
  27. return $info;
  28. }
  29. return false;
  30. }
  31. # 获取当前期的数据
  32. public function getData($periods_id)
  33. {
  34. $where['periods_id'] = $periods_id;
  35. $data = Dever::db('collection/ranking_data')->getAll($where);
  36. return $data;
  37. }
  38. # 加入门票榜单
  39. public function up($uid, $info_id, $type = 1, $num = 1)
  40. {
  41. $periods = $this->getPeriodsNew($info_id, $type);
  42. if ($periods) {
  43. $update['uid'] = $uid;
  44. $update['periods_id'] = $periods['id'];
  45. $data = Dever::db('collection/ranking_data')->one($update);
  46. $update['info_id'] = $info_id;
  47. $update['ranking_id'] = $periods['ranking_id'];
  48. if ($data) {
  49. $update['where_id'] = $data['id'];
  50. if ($num < 0) {
  51. $update['num'] = $data['num'] - $num;
  52. } else {
  53. $update['num'] = $data['num'] + $num;
  54. }
  55. Dever::db('collection/ranking_data')->update($update);
  56. } else {
  57. $update['num'] = $num;
  58. Dever::db('collection/ranking_data')->insert($update);
  59. }
  60. }
  61. }
  62. # 加入到cron里 检测所有榜单是否有新的期数,如果没有,自动添加新的一期
  63. public function cron_api()
  64. {
  65. $ranking = Dever::db('collection/ranking')->getAll();
  66. if ($ranking) {
  67. $cur = time();
  68. foreach ($ranking as $k => $v) {
  69. $where['ranking_id'] = $v['id'];
  70. $info = Dever::db('collection/ranking_periods')->getNew($where);
  71. if (!$info) {
  72. # 如果没有,就建立新的期数
  73. $this->createPeriods(1, $cur, $v);
  74. } else {
  75. # 如果有,检查结束时间到没到期,如果到了,就把这一期置为已结束,重新建立新的期数
  76. if ($info['end'] > 0 && $cur > $info['end']) {
  77. Dever::db('collection/ranking_periods')->update(array('where_id' => $info['id'], 'status' => 2));
  78. $this->createPeriods($info['periods'] + 1, $cur, $v);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. private function createPeriods($periods, $cur, $info)
  85. {
  86. # 查看总榜的结束时间是否结束
  87. if ($info['end'] && $cur > $info['end']) {
  88. Dever::db('collection/ranking')->update(array('where_id' => $info['id'], 'status' => 2));
  89. return;
  90. }
  91. $data['info_id'] = $info['info_id'];
  92. $data['ranking_id'] = $info['id'];
  93. $data['name'] = '第' . $periods . '期';
  94. $data['periods'] = $periods;
  95. $data['users'] = 0;
  96. $data['status'] = 1;
  97. $data['start'] = $cur;
  98. $day = 86400;
  99. if ($info['periods'] == 1) {
  100. # 天
  101. $time = $day;
  102. } elseif ($info['periods'] == 2) {
  103. # 周
  104. $time = $day * 7;
  105. } elseif ($info['periods'] == 3) {
  106. # 月
  107. $time = $day * 30;
  108. } elseif ($info['periods'] == 4) {
  109. # 年
  110. $time = $day * 365;
  111. } elseif ($info['periods'] == 5) {
  112. # 永久
  113. $time = 0;
  114. }
  115. if ($time > 0) {
  116. $data['end'] = $data['start'] + $time;
  117. } else {
  118. $data['end'] = 0;
  119. }
  120. Dever::db('collection/ranking_periods')->insert($data);
  121. }
  122. }