| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace app\common\service;
- use app\common\model\BoxModel;
- use utils\RedisCache;
- /**
- * 盒子期数服务 by wes
- * Class BoxService
- * @package app\common\service
- */
- class BoxService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new BoxModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取最新一期福袋信息
- * @return BoxModel|array|mixed|\think\Model|null
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getNewInfo()
- {
- $info = BoxModel::where(['status' => 1, 'open_status' => 1])->order('id desc')->find();
- $info = $info ? $info->toArray() : [];
- $id = isset($info['id']) ? $info['id'] : 0;
- if (empty($info) || $id <= 0) {
- sr_throw('暂未开启');
- }
- $cur_time = time();
- $time = sr_getcurtime($cur_time, 'Y-m-d ');
- $times = explode('|', $info['time_set']);
- $appoint_status = 1;
- // 1 还未开始 2 进行中 3已结束
- $dis_time = 0;
- // 当前时间大于 最后一场结束时间
- if ($appoint_status != 3) {
- $end_timesarr = explode('-', $times[count($times) - 1]);
- $end_randtime = strtotime($time . $end_timesarr[1]);
- if ($cur_time > $end_randtime) {
- $appoint_status = 3;
- }
- }
- // 是否拿到最近的一场未开始距离时间
- if ($appoint_status != 3) {
- foreach ($times as $key => $val) {
- $time_cur = explode('-', $val);
- $time_randbegin = strtotime($time . $time_cur[0]);
- $time_randend = strtotime($time . $time_cur[1]);
- if ($cur_time > $time_randbegin && $cur_time < $time_randend) {
- // 正在这一场进行中
- $appoint_status = 2;
- }
- }
- if ($appoint_status != 2) {
- $has_get = false;
- foreach ($times as $key => $val) {
- $time_cur = explode('-', $val);
- $time_randbegin = strtotime($time . $time_cur[0]);
- if ($cur_time < $time_randbegin && !$has_get) {
- // 正在这一场进行中
- $has_get = true;
- $dis_time = $time_randbegin - $cur_time;
- }
- }
- }
- }
- $isOpen = (int)SystemConfigService::make()->getConfigByName('fudai_is_apply', 1, 'fudai');
- $info['is_open'] = $isOpen;
- $info['appoint_status'] = $info['open_status'] == 1 && $isOpen == 1 ? $appoint_status : 4;
- $info['dis_time'] = $dis_time;
- return $info;
- }
- /**
- * 最新一期盒子缓存信息
- * @return array|mixed|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getCacheInfo()
- {
- $cacheKey = "caches:temp:box:new";
- $info = RedisCache::get($cacheKey);
- if (empty($info)) {
- $info = $this->model->where(['status' => 1, 'open_status' => 1])
- ->field('id,time_set')
- ->order('id desc')
- ->find();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- RedisCache::set($cacheKey, $info, rand(5, 10));
- }
- }
- // 开奖的半个小时
- $curTime = time();
- $timesymdtring = sr_getcurtime(time(), 'Y-m-d ');
- $isMidhanle = false;
- $arr = explode('|', $info['time_set']);
- foreach ($arr as $key => $val) {
- $times = explode('-', $val);
- if ($curTime > strtotime($timesymdtring . $times[0]) && $curTime < strtotime($timesymdtring . $times[1])) {
- // 正在预约中
- $isMidhanle = true;
- }
- }
- if ($isMidhanle) {
- return false;
- }
- return $info;
- }
- }
|