Files
hyperf_service/app/Service/ServiceTrait/Common/CycleTrait.php
2025-03-10 16:52:23 +08:00

55 lines
1.3 KiB
PHP

<?php
namespace App\Service\ServiceTrait\Common;
use App\Cache\Redis\Common\ConfigCache;
use App\Cache\Redis\Common\CycleCache;
use App\Constants\ConfigCode;
use App\Constants\RedisCode;
use App\Model\Cycle;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Redis;
trait CycleTrait
{
/**
* @var CycleCache
*/
#[Inject]
protected CycleCache $cycleCache;
/**
* @var Cycle
*/
#[Inject]
protected Cycle $cycleModel;
/**
* @var ConfigCache
*/
#[Inject]
protected ConfigCache $configCache;
/**
* @return float|bool|Redis|int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function initTodayCycleId(): float|bool|Redis|int
{
$TodayCutOffTime = $this->configCache->getConfigValueByKey(ConfigCode::TODAY_CUT_OFF_TIME_KEY);
if (date('H:i:s') >= ($TodayCutOffTime)) {
$day = date('Y-m-d',strtotime('+1 day'));
} else {
$day = date('Y-m-d',strtotime('today'));
}
$cycleCacheId = $this->cycleCache->getCycleCache($day);
if (!empty($cycleCacheId)) return $cycleCacheId;
return $this->cycleModel->getIdByDate($day);
}
}