fix : coupon date
This commit is contained in:
45
app/Cron/Coupon/UserCouponTask.php
Normal file
45
app/Cron/Coupon/UserCouponTask.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?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 Hyperf\Crontab\Annotation\Crontab;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
|
#[Crontab(rule: "* * * * *", name: "UserCouponTask", singleton: true , callback: "execute", memo: "每秒执行优惠券过期")]
|
||||||
|
class UserCouponTask
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Log
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Log $log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var UserCoupon
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected UserCoupon $userCouponModel;
|
||||||
|
|
||||||
|
public function execute()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->userCouponModel
|
||||||
|
->whereTime('validity_end_time', '<', date('Y-m-d H:i:s'))
|
||||||
|
->where('status', CouponCode::COUPON_STATUS_UNUSED)
|
||||||
|
->update(['status' => CouponCode::COUPON_STATUS_EXPIRE]);
|
||||||
|
}catch (Exception $e){
|
||||||
|
$this->log->error(__CLASS__.$e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,8 +33,10 @@ trait CouponTrait
|
|||||||
$couponInfo = $this->userCouponModel->where('id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first();
|
$couponInfo = $this->userCouponModel->where('id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first();
|
||||||
|
|
||||||
if (empty($couponInfo)) return;
|
if (empty($couponInfo)) return;
|
||||||
|
if ($couponInfo->status != CouponCode::COUPON_STATUS_USED) return;
|
||||||
|
|
||||||
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
|
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
|
||||||
|
$couponInfo->use_time = null;
|
||||||
|
|
||||||
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE;
|
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user