diff --git a/app/Controller/PurchaseLimitController.php b/app/Controller/PurchaseLimitController.php index 3f9abf7..69b2c48 100644 --- a/app/Controller/PurchaseLimitController.php +++ b/app/Controller/PurchaseLimitController.php @@ -35,4 +35,10 @@ class PurchaseLimitController extends BaseController return $this->success($res); } + public function test() + { + $res = $this->purchaseLimitService->test($this->request->all()); + return $this->success($res); + } + } diff --git a/app/Model/Combination.php b/app/Model/Combination.php new file mode 100644 index 0000000..b1e5833 --- /dev/null +++ b/app/Model/Combination.php @@ -0,0 +1,14 @@ +select('is_max','restrict_num','box_money','inventory','money') + ->first(); + + //获取购物车该商品购买数量 + $num = ShopCar::where([ + ['user_id', $params['user_id']], + ['good_id', $params['good_id']], + ]) + ->sum('num'); + //限购检验 + if($goods->restrict_num > 0 && $goods->restrict_num <= $num){ return false; + } + //获取规格表商品信息 + if($params['combination_id'] > 0) { + $combination = Combination::where([ + ['id', '=', $params['combination_id']], + ]) + ->select('wm_money', 'number') + ->first(); + $inventory = $combination->number; + $money = $combination->wm_money; }else{ - return '加入购物车成功'; + $inventory = $goods->inventory; + $money = $goods->money; + } + //库存校验 + if($goods->is_max == 2 && ($num + $params['num']) > $inventory) + { + return false; } + //更新购物车 + $exists = 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']); + } + $test = $exists->exists(); + if($test) + { + $update = ShopCar::where([ + ['user_id', '=', $params['user_id']], + ['good_id', '=', $params['good_id']], + ['market_id','=',$params['market_id']], + ]); + if($params['combination_id'] > 0) { + $update->where('combination_id',$params['combination_id']); + } + $update->increment('num', $params['num']); + }else{ + $son_id = empty($params['son_id']) ? 0 : $params['son_id']; + $dr_id = empty($params['dr_id']) ? 0 : $params['dr_id']; + $combination_id = empty($params['combination_id']) ? 0 : $params['combination_id']; + $qg_name = empty($params['qg_name']) ? ' ' : $params['qg_name']; + $qg_logo = empty($params['qg_logo']) ? ' ' :$params['qg_logo']; + ShopCar::insert( + [ + 'market_id' => $params['market_id'], + 'good_id' => $params['good_id'], + 'store_id' => $params['store_id'], + 'user_id' => $params['user_id'], + 'combination_id' => $combination_id, + 'num' => $params['num'], + 'spec' => $params['spec'], + 'son_id' => $son_id, + 'dr_id' => $dr_id, + 'qg_name' => $qg_name, + 'qg_logo' => $qg_logo, + 'money' => $money, + 'box_money' => $goods->box_money, + ] + ); + } + return $test; + // if($params['goods_id'] == 1561){ + // return false; + // }else{ + // return '加入购物车成功'; + // } } public function updateShopCar($params) @@ -23,4 +109,9 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface return '更新购物车成功'; } } + + public function test($params) + { + return $params; + } } \ No newline at end of file diff --git a/app/Service/PurchaseLimitServiceInterface.php b/app/Service/PurchaseLimitServiceInterface.php index 79f3df3..b460c81 100644 --- a/app/Service/PurchaseLimitServiceInterface.php +++ b/app/Service/PurchaseLimitServiceInterface.php @@ -9,4 +9,6 @@ interface PurchaseLimitServiceInterface public function addShopCar($params); public function updateShopCar($params); + + public function test($params); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index 28f4299..254b0f7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -58,6 +58,7 @@ Router::addGroup('/v1/',function (){ //加入购物车 Router::post('PurchaseLimit/addShopCar', 'App\Controller\PurchaseLimitController@addShopCar'); Router::post('PurchaseLimit/updateShopCar', 'App\Controller\PurchaseLimitController@updateShopCar'); + Router::post('PurchaseLimit/test', 'App\Controller\PurchaseLimitController@test'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); Router::addGroup('/wechat/',function () {