104 lines
3.1 KiB
PHP
104 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Constants\Common\CouponCode;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Exception\ErrException;
|
|
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()));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
protected function couponLock(): void
|
|
{
|
|
if ($this->couponId <= 0) return;
|
|
|
|
$couponInfo = $this->userCouponModel->where('id', $this->couponId)->where('user_id',$this->userId)->first();
|
|
|
|
if (empty($couponInfo)) return;
|
|
|
|
$couponInfo->status = CouponCode::COUPON_STATUS_USED;
|
|
$couponInfo->use_time = date('Y-m-d H:i:s');
|
|
|
|
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) throw new Exception('优惠券已过期');
|
|
|
|
if (!$couponInfo->save()) throw new Exception('couponLock:error:couponLockUpdateError:'.json_encode($couponInfo->toArray()));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ErrException
|
|
*/
|
|
protected function rollbackCouponLock(): void
|
|
{
|
|
if ($this->couponId <= 0) return;
|
|
|
|
$couponInfo = $this->userCouponModel->where('id', $this->couponId)->where('user_id',$this->userId)->first();
|
|
|
|
if (empty($couponInfo)) return;
|
|
|
|
if ($couponInfo->status != CouponCode::COUPON_STATUS_USED) return;
|
|
|
|
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
|
|
$couponInfo->use_time = '1970-01-01 00:00:00';
|
|
|
|
if (!$couponInfo->save()) throw new ErrException('系统错误-rollback_coupon失败');
|
|
}
|
|
|
|
/**
|
|
* @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 [];
|
|
}
|
|
}
|
|
} |