Ranking.php 3.5 KB

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