91 lines
2.3 KiB
PHP
91 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Api\Order;
|
|
|
|
use App\Constants\Common\CouponCode;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class ConfirmationOrderService extends BaseOrderService
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->isAutoSelectCoupon = 1;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$this->siteId = (int)$this->request->input('site_id');
|
|
$this->couponId = (int)$this->request->input('coupon_id',0);
|
|
|
|
$this->check();
|
|
|
|
$this->compute();
|
|
|
|
$this->autoSelectCoupon();
|
|
|
|
return $this->return->success('success',$this->orderRes);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
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['ratio'] * $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
|
|
{
|
|
|
|
}
|
|
} |