41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Constants\Common\CouponCode;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Model\UserCoupon;
|
|
use Exception;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
trait CouponTrait
|
|
{
|
|
/**
|
|
* @var UserCoupon
|
|
*/
|
|
#[Inject]
|
|
protected UserCoupon $userCouponModel;
|
|
|
|
/**
|
|
* @param $orderType
|
|
* @param $orderInfo
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
protected function rollbackCoupon($orderType,$orderInfo): void
|
|
{
|
|
if ($orderType != OrderCode::ORDER_TYPE_GOOD) return;
|
|
|
|
if ($orderInfo->coupon_id <= 0) return;
|
|
|
|
$couponInfo = $this->userCouponModel->where('coupon_id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first();
|
|
|
|
if (empty($couponInfo)) return;
|
|
|
|
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
|
|
|
|
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE;
|
|
|
|
if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray()));
|
|
}
|
|
} |