From 3b788e2dbb1a76bb83ce98a717059addcfbaf412 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Mon, 24 Aug 2020 11:03:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B4=AD=E7=89=A9=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/PurchaseLimitController.php | 4 +- app/Service/PurchaseLimitService.php | 90 ++++++++++++++++++---- 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/app/Controller/PurchaseLimitController.php b/app/Controller/PurchaseLimitController.php index 69b2c48..581e871 100644 --- a/app/Controller/PurchaseLimitController.php +++ b/app/Controller/PurchaseLimitController.php @@ -29,8 +29,8 @@ class PurchaseLimitController extends BaseController public function updateShopCar() { $res = $this->purchaseLimitService->updateShopCar($this->request->all()); - if (!$res) { - return $this->result(ErrorCode::GOODS_FAILURE, '', '更新购物车失败'); + if (isset($res['error'])) { + return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']); } return $this->success($res); } diff --git a/app/Service/PurchaseLimitService.php b/app/Service/PurchaseLimitService.php index 29004fc..a0cc64b 100644 --- a/app/Service/PurchaseLimitService.php +++ b/app/Service/PurchaseLimitService.php @@ -26,14 +26,19 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface ]) ->sum('num'); //限购检验 - if($goods->restrict_num > 0 && $goods->restrict_num <= $num){ - return false; + if($goods->restrict_num > 0 && $goods->restrict_num <= $num) + { + $error = [ + 'error' => '超过商品限购数量' + ]; + return $error; } //获取规格表商品信息 - if($params['combination_id'] > 0) { + if($params['combination_id'] > 0) + { $combination = Combination::where([ - ['id', '=', $params['combination_id']], - ]) + ['id', '=', $params['combination_id']], + ]) ->select('wm_money', 'number') ->first(); $inventory = $combination->number; @@ -42,23 +47,26 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface $inventory = $goods->inventory; $money = $goods->money; } - //库存校验 + //库存校验 is_max 无限库存 if($goods->is_max == 2 && ($num + $params['num']) > $inventory) { - return false; + $error = [ + 'error' => '库存不足' + ]; + return $error; } //更新购物车 - $exists = ShopCar::where([ + $exists_sql = ShopCar::where([ ['user_id', '=', $params['user_id']], ['good_id', '=', $params['good_id']], ['market_id','=',$params['market_id']], ['combination_id','=', $params['combination_id']], ]); if($params['combination_id'] > 0) { - $exists->where('combination_id',$params['combination_id']); + $exists_sql->where('combination_id',$params['combination_id']); } - $test = $exists->exists(); - if($test) + $exists = $exists_sql->exists(); + if($exists) { $update = ShopCar::where([ ['user_id', '=', $params['user_id']], @@ -93,7 +101,7 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface ] ); } - return $test; + return true; // if($params['goods_id'] == 1561){ // return false; // }else{ @@ -103,11 +111,61 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface public function updateShopCar($params) { - if($params['good_id'] == 1561){ - return false; - }else{ - return '更新购物车成功'; + if($params['num'] <= 0) + { + ShopCar::where('id',$params['id'])->delete(); + }else { + $shop_car = ShopCar::where('id',$params['id']) + ->select('good_id','user_id') + ->first(); + //获取主表商品表信息 + $goods = Goods::where([ + ['id','=',$shop_car['good_id']], + ]) + ->select('is_max','restrict_num','box_money','inventory','money') + ->first(); + //获取购物车该商品购买数量 + $num = ShopCar::where([ + ['user_id', $shop_car['user_id']], + ['good_id', $shop_car['good_id']], + ]) + ->sum('num'); + //限购检验 + if($goods->restrict_num > 0 && $goods->restrict_num <= $num) + { + $error = [ + 'error' => '超过商品限购数量' + ]; + return $error; + } + if ($shop_car['combination_id'] > 0) + { + $combination = Combination::where([ + ['id', '=', $shop_car['combination_id']], + ]) + ->select('wm_money', 'number') + ->first(); + $inventory = $combination->number; + }else{ + $inventory = $goods->inventory; + } + //库存校验 is_max 无限库存 + if($goods->is_max == 2 && $params['num'] > $inventory) + { + $error = [ + 'error' => '库存不足' + ]; + return $error; + } + ShopCar::where('id',$params['id']) + ->update(['num' => $params['num']]); + return true; } + // if($params['good_id'] == 1561){ + // return false; + // }else{ + // return '更新购物车成功'; + // } } public function test($params)