feat : coupon
This commit is contained in:
@@ -12,6 +12,8 @@ namespace App\Service\Api\Order;
|
||||
|
||||
use App\Cache\Redis\Common\ConfigCache;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Model\CouponTemplate;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use App\Service\ServiceTrait\Common\CycleTrait;
|
||||
@@ -50,6 +52,18 @@ abstract class BaseOrderService extends BaseService
|
||||
#[Inject]
|
||||
protected ConfigCache $configCache;
|
||||
|
||||
/**
|
||||
* @var UserCoupon
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserCoupon $userCouponModel;
|
||||
|
||||
/**
|
||||
* @var CouponTemplate
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponTemplate $couponTemplateModel;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Order;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
@@ -36,6 +37,52 @@ class ConfirmationOrderService extends BaseOrderService
|
||||
|
||||
$this->compute();
|
||||
|
||||
$this->autoSelectCoupon();
|
||||
|
||||
return $this->return->success('success',$this->orderRes);
|
||||
}
|
||||
|
||||
private function autoSelectCoupon(): void
|
||||
{
|
||||
if ($this->couponId != 0) return;
|
||||
|
||||
$noUseCoupon = $this->couponTemplateModel->getNoUseCouponByUserId($this->userId);
|
||||
|
||||
if ($noUseCoupon->isEmpty()) return;
|
||||
|
||||
$noUseCoupon = $noUseCoupon->toArray();
|
||||
|
||||
$orderMaxPrice = max(array_column($this->orderRes['good'],'price'));
|
||||
$maxValidityEndTimeDate = max(array_column($noUseCoupon,'validity_end_time'));
|
||||
|
||||
$filtered = array_filter($noUseCoupon, function($item) use($maxValidityEndTimeDate) {
|
||||
return $item['validity_end_time'] === $maxValidityEndTimeDate;
|
||||
});
|
||||
|
||||
$selectMaxPrice = 0;
|
||||
$selectCouponInfo = [];
|
||||
foreach ($filtered as &$value) {
|
||||
$value['amount'] = match ($value['coupon_type']) {
|
||||
CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $value['amount'],
|
||||
CouponCode::COUPON_TYPE_DISCOUNT => $value['amount'] * $orderMaxPrice,
|
||||
default => 0
|
||||
};
|
||||
|
||||
if ($value['amount'] < $selectMaxPrice) continue;
|
||||
|
||||
$selectMaxPrice = $value['amount'];
|
||||
$selectCouponInfo = $value;
|
||||
}
|
||||
|
||||
$this->couponId = $selectCouponInfo['id'];
|
||||
$this->orderRes['coupon'] = $selectCouponInfo;
|
||||
$this->orderRes['coupon_id'] = $selectCouponInfo['id'];
|
||||
|
||||
$this->reloadCompute();
|
||||
}
|
||||
|
||||
private function reloadCompute(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user