68 lines
1.5 KiB
PHP
68 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Constants\ApiCode;
|
|
use App\Exception\ErrException;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
trait OrderTrait
|
|
{
|
|
/**
|
|
* @param array $data
|
|
* @return void
|
|
*/
|
|
protected function buildCartData(array $data): void
|
|
{
|
|
$copies = 0;
|
|
foreach ($data as $oneCopies) {
|
|
$one = [];
|
|
foreach ($oneCopies as $oneGood) {
|
|
$this->cartFirstData[$oneGood] = ($this->cartFirstData[$oneGood] ?? 0) + 1;
|
|
$one[$oneGood] = ($one[$oneGood] ?? 0) + 1;
|
|
}
|
|
|
|
$this->cartSecondData[] = $one;
|
|
|
|
$copies++;
|
|
}
|
|
|
|
$this->copies = $copies;
|
|
}
|
|
|
|
/**
|
|
* @param array $goodIds
|
|
* @return void
|
|
*/
|
|
protected function checkGood(array $goodIds): void
|
|
{
|
|
foreach ($this->cartFirstData as $key => $one) {
|
|
if (in_array($key, $goodIds)) continue;
|
|
|
|
throw new ErrException('商品不存在');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function checkStock(): void
|
|
{
|
|
foreach ($this->cartFirstData as $key => $one) {
|
|
if (($this->redis->zScore($this->stockKey,$key) ?? 0) >= $one) continue;
|
|
|
|
throw new ErrException('商品库存不足');
|
|
}
|
|
}
|
|
} |