feat : coupon

This commit is contained in:
2025-03-05 10:31:19 +08:00
parent 63f60bc92b
commit 75f649676d
5 changed files with 110 additions and 11 deletions

View File

@@ -35,10 +35,14 @@ class ConfirmationOrderService extends BaseOrderService
$this->check();
$this->getCouponInfo();
$this->compute();
$this->autoSelectCoupon();
$this->reloadCompute();
return $this->return->success('success',$this->orderRes);
}
@@ -67,7 +71,7 @@ class ConfirmationOrderService extends BaseOrderService
foreach ($filtered as &$value) {
$value['amount'] = match ($value['coupon_type']) {
CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $value['amount'],
CouponCode::COUPON_TYPE_DISCOUNT => $value['ratio'] * $orderMaxPrice,
CouponCode::COUPON_TYPE_DISCOUNT => bcmul(bcsub("1", $value['ratio'],2),$orderMaxPrice,2),
default => 0
};
@@ -80,12 +84,7 @@ class ConfirmationOrderService extends BaseOrderService
$this->couponId = $selectCouponInfo['id'];
$this->orderRes['coupon'] = $selectCouponInfo;
$this->orderRes['coupon_id'] = $selectCouponInfo['id'];
$this->reloadCompute();
}
private function reloadCompute(): void
{
}
}

View File

@@ -57,9 +57,15 @@ class PlaceOrderService extends BaseOrderService
// 生成购物车信息 检测商品和库存等
$this->check();
// 如果传递了优惠券的值 则进行检测
$this->getCouponInfo();
// 计算
$this->compute();
// 如果有优惠券的情况下 重载计算
$this->reloadCompute();
// 下单 减少库存 写入数据库
$this->placeOrder();

View File

@@ -15,6 +15,7 @@ use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\SiteCache;
use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode;
use App\Constants\Common\CouponCode;
use App\Constants\Common\GoodCode;
use App\Constants\Common\OrderCode;
use App\Constants\Common\PayCode;
@@ -333,4 +334,55 @@ trait OrderTrait
$producer->produce($message);
}
/**
* 重载计算
* @return void
*/
protected function reloadCompute(): void
{
if ($this->couponId <= 0 || empty($this->orderRes['coupon_info']) || $this->orderRes['coupon_id'] <= 0) return;
if ($this->orderRes['coupon_info']['amount'] <= 0 && $this->orderRes['coupon_info']['coupon_type'] != CouponCode::COUPON_TYPE_INSTANT_REDUCTION) {
$orderMaxPrice = max(array_column($this->orderRes['good'],'price'));
$this->orderRes['coupon_info']['amount'] = match ($this->orderRes['coupon_info']['coupon_type']) {
CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $this->orderRes['coupon_info']['amount'],
CouponCode::COUPON_TYPE_DISCOUNT => bcmul(bcsub("1", $this->orderRes['coupon_info']['ratio'],2),$orderMaxPrice,2),
default => 0
};
}
$this->orderRes['favorable_good_price'] = $this->orderRes['coupon_info']['amount'];
$this->orderRes['good_after_discount_price'] = bcsub($this->orderRes['good_after_discount_price'],$this->orderRes['favorable_good_price'],2);
$this->orderRes['actual_price'] = bcsub($this->orderRes['actual_price'],$this->orderRes['favorable_good_price'],2);
}
/**
* @return void
*/
protected function getCouponInfo(): void
{
if ($this->couponId <= 0) return;
$couponInfo = $this->userCouponModel->where('coupon_id', $this->couponId)->where('user_id',$this->userId)->first();
if (empty($couponInfo)) throw new ErrException('优惠券信息错误');
if (in_array($couponInfo->status,[CouponCode::COUPON_STATUS_USED,CouponCode::COUPON_STATUS_EXPIRE])) throw new ErrException('优惠券已经被使用');
$couponTemplateInfo = $this->couponTemplateModel->getInfoById($couponInfo->coupon_template_id);
if (empty($couponTempleteInfo)) throw new ErrException('优惠券信息错误');
$this->couponId = $couponInfo->id;
$this->orderRes['coupon_info'] = [
'id' => $couponInfo->id,
'coupon_type' => $couponTemplateInfo->coupon_type,
'amount' => $couponTemplateInfo->amount,
'ratio' => $couponTemplateInfo->ratio,
'coupon_template_id' => $couponTemplateInfo->id,
'coupon_name' => $couponInfo->coupon_name,
'status' => $couponInfo->status,
'validity_start_time' => $couponInfo->validity_start_time,
'validity_end_time' => $couponInfo->validity_end_time,
];
$this->orderRes['coupon_id'] = $couponInfo->id;
}
}