diff --git a/app/Constants/ApiCode.php b/app/Constants/ApiCode.php index 4ba78d9..f73c881 100644 --- a/app/Constants/ApiCode.php +++ b/app/Constants/ApiCode.php @@ -16,4 +16,14 @@ class ApiCode extends ReturnCode * @Message("登录token已失效") */ public const int LOGIN_TOKEN_ERROR = 10002; + + /** + * @Message("商品不存在") + */ + public const int ORDER_GOOD_INEXISTENCE = 20001; + + /** + * @Message("商品库存不足") + */ + public const int ORDER_GOOD_INSUFFICIENT_STOCK = 20002; } \ No newline at end of file diff --git a/app/Controller/Api/OrderController.php b/app/Controller/Api/OrderController.php new file mode 100644 index 0000000..359687a --- /dev/null +++ b/app/Controller/Api/OrderController.php @@ -0,0 +1,35 @@ +handle(); + } + + #[RequestMapping(path: 'place_order',methods: 'post')] + #[Scene(scene: 'place_order')] + public function placeOrder() + { + return (new PlaceOrderService)->handle(); + } +} diff --git a/app/Controller/Api/PayController.php b/app/Controller/Api/PayController.php new file mode 100644 index 0000000..b334f41 --- /dev/null +++ b/app/Controller/Api/PayController.php @@ -0,0 +1,22 @@ +request->path()); - + var_dump($throwable); $result = match ($urlArr[0]) { 'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()), 'admin', 'common' => $this->adminReturn->error($throwable->getMessage(),$throwable->getCode()), diff --git a/app/Service/Api/Order/CheckCartService.php b/app/Service/Api/Order/CheckCartService.php new file mode 100644 index 0000000..374d9c7 --- /dev/null +++ b/app/Service/Api/Order/CheckCartService.php @@ -0,0 +1,125 @@ +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')); + } +} \ No newline at end of file diff --git a/app/Service/Api/Order/PlaceOrderService.php b/app/Service/Api/Order/PlaceOrderService.php new file mode 100644 index 0000000..d80c336 --- /dev/null +++ b/app/Service/Api/Order/PlaceOrderService.php @@ -0,0 +1,26 @@ +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('商品库存不足'); + } + } +} \ No newline at end of file