63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Constants\Common\CouponCode;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Extend\DateUtil;
|
|
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('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()));
|
|
}
|
|
|
|
/**
|
|
* @param int $type
|
|
* @param string $value
|
|
* @return array
|
|
*/
|
|
protected function getValidityTime(int $type,string $value): array
|
|
{
|
|
switch ($type) {
|
|
case CouponCode::VALIDITY_TIME_TYPE_CYCLE:
|
|
$validityValue = json_decode($value,true);
|
|
return [
|
|
'start_time' => date('Y-m-d H:i:s'),
|
|
'end_time' => date('Y-m-d H:i:s', time() + ($validityValue['day_num'] * DateUtil::DAY))
|
|
];
|
|
case CouponCode::VALIDITY_TIME_TYPE_FIX:
|
|
return json_decode($value,true);
|
|
default:
|
|
return [];
|
|
}
|
|
}
|
|
} |