125 lines
3.0 KiB
PHP
125 lines
3.0 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\Cache\Redis\Api\ApiRedisKey;
|
|
use App\Cache\Redis\Api\GoodCache;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\ApiCode;
|
|
use App\Exception\ErrException;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\ServiceTrait\Api\OrderTrait;
|
|
use App\Service\ServiceTrait\Common\CycleTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use function Hyperf\Config\config;
|
|
|
|
class CheckCartService extends BaseService
|
|
{
|
|
use CycleTrait,OrderTrait;
|
|
|
|
/**
|
|
* @var int 周期id
|
|
*/
|
|
private int $cycleId;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private array $goodIds = [];
|
|
|
|
/**
|
|
* @var string 库存key
|
|
*/
|
|
private string $stockKey;
|
|
|
|
/**
|
|
* @var RedisCache $redis
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private array $cartSecondData;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private array $cartFirstData;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $copies;
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->cycleId = (int)$this->initTodayCycleId();
|
|
$this->cartFirstData = [];
|
|
$this->cartSecondData = [];
|
|
$this->copies = 0;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
throw new ErrException(ApiCode::getMessage(ApiCode::LOGIN_ERROR),ApiCode::LOGIN_ERROR);
|
|
$kitchenId = (int)$this->request->input('kitchen_id');
|
|
|
|
$this->getGoodInfo($kitchenId);
|
|
|
|
$this->buildCartData(json_decode($this->request->input('cart'), true));
|
|
|
|
$this->checkGood($this->goodIds);
|
|
|
|
$this->checkStock();
|
|
|
|
$this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 获取商品信息
|
|
* @param int $kitchenId
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function getGoodInfo(int $kitchenId): void
|
|
{
|
|
$this->stockKey = ApiRedisKey::goodStockKey($this->cycleId,$kitchenId);
|
|
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
|
|
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
|
|
|
|
$mealGood = $this->redis->get($mealGoodKey);
|
|
$optionalGood = $this->redis->get($optionalGoodKey);
|
|
|
|
if (empty($mealGood) || empty($optionalGood)) throw new ErrException('商品不存在');
|
|
|
|
if ($this->redis->exists($this->stockKey)) throw new ErrException('库存不足');
|
|
|
|
$mealGood = json_decode($mealGood, true);
|
|
$optionalGood = json_decode($optionalGood, true);
|
|
|
|
$this->goodIds = array_merge(array_column($mealGood, 'id'), array_column($optionalGood, 'id'));
|
|
}
|
|
} |