53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* This crontab file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cron\Coupon;
|
|
|
|
use App\Constants\Common\CouponCode;
|
|
use App\Lib\Log;
|
|
use App\Model\UserCoupon;
|
|
use Exception;
|
|
use Hyperf\Crontab\Annotation\Crontab;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
#[Crontab(rule: "* * * * *", name: "UserCouponTask", singleton: true , callback: "execute", memo: "每秒执行优惠券过期")]
|
|
class UserCouponTask
|
|
{
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* @var UserCoupon
|
|
*/
|
|
#[Inject]
|
|
protected UserCoupon $userCouponModel;
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function execute(): void
|
|
{
|
|
try {
|
|
$this->userCouponModel
|
|
->where('status', CouponCode::COUPON_STATUS_UNUSED)
|
|
->where('validity_end_time', '<', date('Y-m-d H:i:s'))
|
|
->update(['status' => CouponCode::COUPON_STATUS_EXPIRE]);
|
|
}catch (Exception $e){
|
|
$this->log->error(__CLASS__.$e->getMessage());
|
|
}
|
|
}
|
|
} |