101 lines
2.4 KiB
PHP
101 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* This crontab file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cron\Good;
|
|
|
|
use App\Cache\Redis\Common\CommonRedisKey;
|
|
use App\Cache\Redis\Common\CycleCache;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Extend\DateUtil;
|
|
use App\Lib\Log;
|
|
use App\Model\Cycle;
|
|
use Hyperf\Crontab\Annotation\Crontab;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
#[Crontab(rule: "0 5 * * *", name: "CycleCreateTask", singleton: true , callback: "execute", memo: "创建新的周期任务")]
|
|
class CycleCreateTask
|
|
{
|
|
/**
|
|
* @var Cycle $cycleModel
|
|
*/
|
|
#[Inject]
|
|
protected Cycle $cycleModel;
|
|
|
|
/**
|
|
* @var CycleCache $cycleCache
|
|
*/
|
|
#[Inject]
|
|
protected CycleCache $cycleCache;
|
|
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* 非常重要的逻辑--生成周期 (不可删除)
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function execute(): void
|
|
{
|
|
$maxDate = $this->cycleModel->max('dates') ?? 0;
|
|
|
|
$nextMax = date('Ymd',strtotime('+3 day',time()));
|
|
|
|
if ($maxDate != 0 && $nextMax <= $maxDate) {
|
|
return;
|
|
}
|
|
|
|
$nextCycleRange = DateUtil::getDateFromRange(date('Ymd'),$nextMax);
|
|
|
|
$this->cycleCache->setAllCycleCache();
|
|
|
|
$key = CommonRedisKey::getCycleList();
|
|
$noDataInsertArr = [];
|
|
$noDataArr = [];
|
|
foreach ($nextCycleRange as $date) {
|
|
$one = $this->redis->zScore($key,$date,'system');
|
|
|
|
if ($one) continue;
|
|
|
|
$noDataInsertArr[] = [
|
|
'dates' => $date,
|
|
'create_time' => date('Y-m-d H:i:s'),
|
|
];
|
|
$noDataArr[] = $date;
|
|
}
|
|
|
|
$insertSql = (new Cycle)->insert($noDataInsertArr);
|
|
|
|
if (!$insertSql) $this->log->error('添加周期失败!!!');
|
|
|
|
$res = $this->cycleModel->whereIn('dates',$noDataArr)->get();
|
|
if (empty($res)) return;
|
|
|
|
$res = $res->toArray();
|
|
|
|
foreach ($res as $one) {
|
|
$this->redis->zAdd($key,$one['id'],$one['dates'],'system');
|
|
}
|
|
|
|
$this->log->notice('添加周期成功,添加的周期为'.json_encode($noDataArr,JSON_UNESCAPED_UNICODE));
|
|
}
|
|
} |