diff --git a/app/Controller/PurchaseLimitController.php b/app/Controller/PurchaseLimitController.php index 591ce00..37870db 100644 --- a/app/Controller/PurchaseLimitController.php +++ b/app/Controller/PurchaseLimitController.php @@ -16,25 +16,6 @@ class PurchaseLimitController extends BaseController */ protected $purchaseLimitService; - - public function addShopCar() - { - $res = $this->purchaseLimitService->addShopCar($this->request->all()); - if (isset($res['error'])) { - return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']); - } - return $this->success($res); - } - - public function updateShopCar() - { - $res = $this->purchaseLimitService->updateShopCar($this->request->all()); - if (isset($res['error'])) { - return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']); - } - return $this->success($res); - } - public function getStoreIdByMarketId() { $res = $this->purchaseLimitService->getStoreIdByMarketId($this->request->all()); diff --git a/app/Controller/ShopCarController.php b/app/Controller/ShopCarController.php new file mode 100644 index 0000000..8c6cb35 --- /dev/null +++ b/app/Controller/ShopCarController.php @@ -0,0 +1,39 @@ +shopCarService->addShopCar($this->request->all()); + if (isset($res['error'])) { + return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']); + } + return $this->success($res); + } + + public function updateShopCar() + { + $res = $this->shopCarService->updateShopCar($this->request->all()); + 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 2afa354..7434bb0 100644 --- a/app/Service/PurchaseLimitService.php +++ b/app/Service/PurchaseLimitService.php @@ -15,174 +15,6 @@ use App\Constants\SsdbKeysPrefix; class PurchaseLimitService implements PurchaseLimitServiceInterface { - public function addShopCar($params) - { - //获取ssdb购买记录 - $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); - $record_exists = $ssdb->exec('hexists',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'], $params['good_id']); - if($record_exists) - { - $error = [ - 'error' => '特价商品每日只能购买一次' - ]; - return $error; - } - //获取主表商品表信息 - $goods = Goods::where([ - ['id','=',$params['good_id']], - ]) - ->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) - { - $error = [ - 'error' => '超过商品限购数量' - ]; - return $error; - } - //获取规格表商品信息 - 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{ - $inventory = $goods->inventory; - $money = $goods->money; - } - //库存校验 is_max 无限库存 - if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory) - { - $error = [ - 'error' => '库存不足' - ]; - return $error; - } - //更新购物车 - $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_sql->where('combination_id',$params['combination_id']); - } - $exists = $exists_sql->exists(); - if($exists) - { - $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 true; - // if($params['goods_id'] == 1561){ - // return false; - // }else{ - // return '加入购物车成功'; - // } - } - - public function updateShopCar($params) - { - 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 != Goods::INVENTORY_NOLIMIT && $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 getStoreIdByMarketId($params) { $res = [ diff --git a/app/Service/PurchaseLimitServiceInterface.php b/app/Service/PurchaseLimitServiceInterface.php index 3b0d66d..c2626c4 100644 --- a/app/Service/PurchaseLimitServiceInterface.php +++ b/app/Service/PurchaseLimitServiceInterface.php @@ -6,10 +6,6 @@ namespace App\Service; interface PurchaseLimitServiceInterface { - public function addShopCar($params); - - public function updateShopCar($params); - public function getStoreIdByMarketId($params); public function ssdbPurchaseRecord($params); diff --git a/app/Service/ShopCarService.php b/app/Service/ShopCarService.php new file mode 100644 index 0000000..7b20bc8 --- /dev/null +++ b/app/Service/ShopCarService.php @@ -0,0 +1,185 @@ +get(SSDBTask::class); + $record_exists = $ssdb->exec('hexists',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'], $params['good_id']); + if($record_exists) + { + $error = [ + 'error' => '特价商品每日只能购买一次' + ]; + return $error; + } + //获取主表商品表信息 + $goods = Goods::where([ + ['id','=',$params['good_id']], + ]) + ->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) + { + $error = [ + 'error' => '超过商品限购数量' + ]; + return $error; + } + //获取规格表商品信息 + 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{ + $inventory = $goods->inventory; + $money = $goods->money; + } + //库存校验 is_max 无限库存 + if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory) + { + $error = [ + 'error' => '库存不足' + ]; + return $error; + } + //更新购物车 + $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_sql->where('combination_id',$params['combination_id']); + } + $exists = $exists_sql->exists(); + if($exists) + { + $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 true; + // if($params['goods_id'] == 1561){ + // return false; + // }else{ + // return '加入购物车成功'; + // } + } + + public function updateShopCar($params) + { + 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 != Goods::INVENTORY_NOLIMIT && $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 '更新购物车成功'; + // } + } +} \ No newline at end of file diff --git a/app/Service/ShopCarServiceInterface.php b/app/Service/ShopCarServiceInterface.php new file mode 100644 index 0000000..7691a71 --- /dev/null +++ b/app/Service/ShopCarServiceInterface.php @@ -0,0 +1,12 @@ + \App\JsonRpc\OrderService::class, \App\Service\FinancialRecordServiceInterface::class => \App\Service\FinancialRecordService::class, \App\Service\SeparateAccountsServiceInterface::class => \App\Service\SeparateAccountsService::class, + \App\Service\ShopCarServiceInterface::class => \App\Service\ShopCarService::class, ]; diff --git a/config/routes.php b/config/routes.php index 6ddd543..a0f385c 100644 --- a/config/routes.php +++ b/config/routes.php @@ -51,17 +51,18 @@ Router::addGroup('/v1/',function (){ Router::post('Order/addOnline', 'App\Controller\OrderController@addOnlineOrder'); Router::post('Order/addOffline', 'App\Controller\OrderController@addOfflineOrder'); Router::post('Order/onlineCancel', 'App\Controller\OrderController@onlineCancel'); + Router::post('Order/userComp', 'App\Controller\OrderController@userComp'); //小程序支付相关 Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline'); Router::post('wxminipay/offline', 'App\Controller\PaymentController@wxminiPayOffline'); - Router::post('Order/userComp', 'App\Controller\OrderController@userComp'); + //加入购物车 - Router::post('PurchaseLimit/addShopCar', 'App\Controller\PurchaseLimitController@addShopCar'); - Router::post('PurchaseLimit/updateShopCar', 'App\Controller\PurchaseLimitController@updateShopCar'); + Router::post('ShopCar/addShopCar', 'App\Controller\ShopCarController@addShopCar'); + Router::post('ShopCar/updateShopCar', 'App\Controller\ShopCarController@updateShopCar'); Router::post('PurchaseLimit/test', 'App\Controller\PurchaseLimitController@test'); Router::post('PurchaseLimit/getStoreIdByMarketId', 'App\Controller\PurchaseLimitController@getStoreIdByMarketId'); - Router::post('Order/userComp', 'App\Controller\OrderController@userComp'); + },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); Router::addGroup('/wechat/',function () {