BoxService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\BoxModel;
  4. use utils\RedisCache;
  5. /**
  6. * 盒子期数服务 by wes
  7. * Class BoxService
  8. * @package app\common\service
  9. */
  10. class BoxService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new BoxModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if (!self::$instance) {
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 获取最新一期福袋信息
  31. * @return BoxModel|array|mixed|\think\Model|null
  32. * @throws \think\Exception
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getNewInfo()
  38. {
  39. $info = BoxModel::where(['status' => 1, 'open_status' => 1])->order('id desc')->find();
  40. $info = $info ? $info->toArray() : [];
  41. $id = isset($info['id']) ? $info['id'] : 0;
  42. if (empty($info) || $id <= 0) {
  43. sr_throw('暂未开启');
  44. }
  45. $cur_time = time();
  46. $time = sr_getcurtime($cur_time, 'Y-m-d ');
  47. $times = explode('|', $info['time_set']);
  48. $appoint_status = 1;
  49. // 1 还未开始 2 进行中 3已结束
  50. $dis_time = 0;
  51. // 当前时间大于 最后一场结束时间
  52. if ($appoint_status != 3) {
  53. $end_timesarr = explode('-', $times[count($times) - 1]);
  54. $end_randtime = strtotime($time . $end_timesarr[1]);
  55. if ($cur_time > $end_randtime) {
  56. $appoint_status = 3;
  57. }
  58. }
  59. // 是否拿到最近的一场未开始距离时间
  60. if ($appoint_status != 3) {
  61. foreach ($times as $key => $val) {
  62. $time_cur = explode('-', $val);
  63. $time_randbegin = strtotime($time . $time_cur[0]);
  64. $time_randend = strtotime($time . $time_cur[1]);
  65. if ($cur_time > $time_randbegin && $cur_time < $time_randend) {
  66. // 正在这一场进行中
  67. $appoint_status = 2;
  68. }
  69. }
  70. if ($appoint_status != 2) {
  71. $has_get = false;
  72. foreach ($times as $key => $val) {
  73. $time_cur = explode('-', $val);
  74. $time_randbegin = strtotime($time . $time_cur[0]);
  75. if ($cur_time < $time_randbegin && !$has_get) {
  76. // 正在这一场进行中
  77. $has_get = true;
  78. $dis_time = $time_randbegin - $cur_time;
  79. }
  80. }
  81. }
  82. }
  83. $isOpen = (int)SystemConfigService::make()->getConfigByName('fudai_is_apply', 1, 'fudai');
  84. $info['is_open'] = $isOpen;
  85. $info['appoint_status'] = $info['open_status'] == 1 && $isOpen == 1 ? $appoint_status : 4;
  86. $info['dis_time'] = $dis_time;
  87. return $info;
  88. }
  89. /**
  90. * 最新一期盒子缓存信息
  91. * @return array|mixed|null
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getCacheInfo()
  97. {
  98. $cacheKey = "caches:temp:box:new";
  99. $info = RedisCache::get($cacheKey);
  100. if (empty($info)) {
  101. $info = $this->model->where(['status' => 1, 'open_status' => 1])
  102. ->field('id,time_set')
  103. ->order('id desc')
  104. ->find();
  105. $info = $info ? $info->toArray() : [];
  106. if ($info) {
  107. RedisCache::set($cacheKey, $info, rand(5, 10));
  108. }
  109. }
  110. // 开奖的半个小时
  111. $curTime = time();
  112. $timesymdtring = sr_getcurtime(time(), 'Y-m-d ');
  113. $isMidhanle = false;
  114. $arr = explode('|', $info['time_set']);
  115. foreach ($arr as $key => $val) {
  116. $times = explode('-', $val);
  117. if ($curTime > strtotime($timesymdtring . $times[0]) && $curTime < strtotime($timesymdtring . $times[1])) {
  118. // 正在预约中
  119. $isMidhanle = true;
  120. }
  121. }
  122. if ($isMidhanle) {
  123. return false;
  124. }
  125. return $info;
  126. }
  127. }