Files
hyperf_service/app/Service/Api/Order/ConfirmationOrderService.php
2025-08-06 21:37:30 +08:00

104 lines
2.9 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->orderType = (int)$this->request->input('order_type');
$this->check();
$this->getCouponInfo();
$this->compute();
$this->autoSelectCoupon();
$this->reloadCompute();
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 => bcmul(bcsub("100", (string)$value['ratio'],2),(string)$orderMaxPrice,2),
CouponCode::COUPON_TYPE_DISCOUNT => bcmul(
bcdiv(
bcsub(
"100",
(string)$value['ratio'],
2
),
"100",
2
),
bcadd((string)max(array_column($this->orderRes['good'],'price')),(string)$this->orderRes['sundry_price'],2),
2
),
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'];
}
}