From bf4ff8b1fa9a3afc14b19e138fc9270f6a4b655c Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Mon, 7 Sep 2020 22:04:20 +0800 Subject: [PATCH] =?UTF-8?q?=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/Constants/v3/ErrorCode.php | 6 ++ app/Controller/v3/HomeController.php | 2 +- app/Controller/v3/ShopCartController.php | 10 ++- .../v3/ShopCartUpdateController.php | 7 +- app/Request/v3/ShopCartUpdateRequest.php | 44 +++++++++++ .../v3/Implementations/GoodsService.php | 5 +- .../v3/Implementations/ShopCartService.php | 75 ++++++++++++++++--- .../Implementations/ShopCartUpdateService.php | 9 ++- .../v3/Implementations/StoreService.php | 24 +++++- .../Interfaces/ShopCartServiceInterface.php | 4 +- .../ShopCartUpdateServiceInterface.php | 2 +- .../v3/Interfaces/StoreServiceInterface.php | 2 +- 12 files changed, 162 insertions(+), 28 deletions(-) create mode 100644 app/Request/v3/ShopCartUpdateRequest.php diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index 9cdc096..6a49d46 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -153,6 +153,12 @@ class ErrorCode extends AbstractConstants */ const GOODS_ON_SALE_NO = 1103; + /** + * 已下架 + * @Message("商品不存在") + */ + const GOODS_NOT_EXISTS = 1104; + /************************************/ /* 活动商品相关 1151-1200 */ /************************************/ diff --git a/app/Controller/v3/HomeController.php b/app/Controller/v3/HomeController.php index 04de8ca..cd0ce9a 100644 --- a/app/Controller/v3/HomeController.php +++ b/app/Controller/v3/HomeController.php @@ -149,7 +149,7 @@ class HomeController extends BaseController public function storeIndex() { - $data['detail'] = $this->storeInfoService->detail(1); + $data['detail'] = $this->storeService->detail(1); $data['order_online'] = [ 'count' => 6549, 'total' => 12654.12 diff --git a/app/Controller/v3/ShopCartController.php b/app/Controller/v3/ShopCartController.php index e8d4e64..9f396e2 100644 --- a/app/Controller/v3/ShopCartController.php +++ b/app/Controller/v3/ShopCartController.php @@ -15,19 +15,23 @@ class ShopCartController extends BaseController protected $shopCartService; public function detail() { + $userId = $this->request->input('user_id'); + $marketId = $this->request->input('market_id'); //获取购物车商品信息 - $res['store_lists'] = $this->shopCartService->do(); + $res['store_lists'] = $this->shopCartService->do($userId,$marketId); //获取购物车失效商品信息 $res['store_lists_invalid'] = $this->shopCartService->undo(); //计算购物车价格 - $res['total'] = $this->shopCartService->getTotal(); + $res['total'] = $this->shopCartService->getTotal($userId,$marketId); return $this->success($res); } public function info() { + $userId = $this->request->input('user_id'); + $marketId = $this->request->input('market_id'); $res['shopcart']['count'] = $this->shopCartService->countGoods(); - $res['shopcart']['total'] = $this->shopCartService->getTotal(); + $res['shopcart']['total'] = $this->shopCartService->getTotal($userId,$marketId); return $this->success($res); } } \ No newline at end of file diff --git a/app/Controller/v3/ShopCartUpdateController.php b/app/Controller/v3/ShopCartUpdateController.php index 67dbca9..83b526b 100644 --- a/app/Controller/v3/ShopCartUpdateController.php +++ b/app/Controller/v3/ShopCartUpdateController.php @@ -5,7 +5,7 @@ namespace App\Controller\v3; use App\Controller\BaseController; use Hyperf\Di\Annotation\Inject; use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface; - +use App\Request\v3\ShopCartUpdateRequest; class ShopCartUpdateController extends BaseController { /** @@ -13,14 +13,13 @@ class ShopCartUpdateController extends BaseController * @var ShopCartUpdateServiceInterface */ protected $shopCarServiceUpdate; - public function update() + public function update(ShopCartUpdateRequest $request) { $user_id = $this->request->input('user_id',0); - $market_id = $this->request->input('market_id',''); $goods_id = $this->request->input('goods_id',0); $num = $this->request->input('num',''); $activity_type = $this->request->input('activity_type',1); - $res = $this->shopCarServiceUpdate->do($user_id,$goods_id,$market_id,$num,$activity_type); + $res = $this->shopCarServiceUpdate->do($user_id,$goods_id,$num,$activity_type); return $this->success($res); } diff --git a/app/Request/v3/ShopCartUpdateRequest.php b/app/Request/v3/ShopCartUpdateRequest.php new file mode 100644 index 0000000..c467aab --- /dev/null +++ b/app/Request/v3/ShopCartUpdateRequest.php @@ -0,0 +1,44 @@ + 'required|nonempty|integer', + 'num' => 'required|nonempty|integer', + 'user_id' => 'required|nonempty|integer|exists:lanzu_user,id', + ]; + } + + /** + * @return array + */ + public function messages(): array + { + return [ + 'user_id.*' => ':attribute无效', + 'goods_id.*' => ':attribute无效', + 'num.*' => ':attribute无效', + ]; + } + + public function attributes(): array + { + return [ + 'user_id' => '用户id', + 'goods_id' => '商品ID', + 'num' => '数量', + ]; + } +} diff --git a/app/Service/v3/Implementations/GoodsService.php b/app/Service/v3/Implementations/GoodsService.php index 1ed75e7..e76c61a 100644 --- a/app/Service/v3/Implementations/GoodsService.php +++ b/app/Service/v3/Implementations/GoodsService.php @@ -20,14 +20,15 @@ class GoodsService implements GoodsServiceInterface public function check($goodsId,$num = 1) { $data = Goods::query()->with('store')->where('id', $goodsId)->first(); + // 商户歇业 if($data->store->is_rest == 1){ return ErrorCode::GOODS_ON_SALE_NO; } - + // 商品下架或已删除 if($data->on_sale == 0 || !is_null($data->deleted_at)){ return ErrorCode::GOODS_ON_SALE_NO; } - + // 商品库存不足 if($data->is_infinite != 1 && $data->inventory < $num){ return ErrorCode::GOODS_INVENTORY_ERROR; } diff --git a/app/Service/v3/Implementations/ShopCartService.php b/app/Service/v3/Implementations/ShopCartService.php index a50e49b..005c59d 100644 --- a/app/Service/v3/Implementations/ShopCartService.php +++ b/app/Service/v3/Implementations/ShopCartService.php @@ -2,24 +2,81 @@ namespace App\Service\v3\Implementations; +use App\Model\v3\Goods; +use App\Model\v3\GoodsActivity; use App\Model\v3\ShoppingCart; use App\Model\v3\Store; +use App\Service\v3\Interfaces\GoodsActivityServiceInterface; +use App\Service\v3\Interfaces\GoodsServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; +use App\Service\v3\Interfaces\StoreServiceInterface; use Hyperf\DbConnection\Db; +use Hyperf\Di\Annotation\Inject; class ShopCartService implements ShopCartServiceInterface { - public function do() + /** + * @Inject + * @var StoreServiceInterface + */ + protected $storeService; + /** + * @Inject + * @var GoodsServiceInterface + */ + protected $goodsService; + + /** + * @Inject + * @var GoodsActivityServiceInterface + */ + protected $goodsActivityService; + public function do($userId,$marketId) { - $storeIds = Db::table('lanzu_shopping_cart')->where('user_id',15)->pluck('store_id')->toArray(); - $res = Store::query()->with(['ShoppingCart' => function($query) { - $query->with('goods'); + $storeIds = Db::table('lanzu_shopping_cart')->where([ + ['user_id','=',$userId], + ['market_id','=',$marketId], + ])->pluck('store_id')->toArray(); + $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){ + $query->where([ + ['user_id','=',$userId], + ['market_id','=',$marketId], + ]); }])->whereIn('id',$storeIds) - ->get(); - foreach ($res as &$k){ - $k->subtotal = '99.90'; + ->get() + ->toArray(); + $storeArr = []; + foreach ($stores as $key => &$store){ + $sotreType = $this->storeService->check($store['id']); + if(!$sotreType){ + unset($stores[$key]); + continue; + } + $subtotal = 0; + foreach ($store['shopping_cart'] as &$shopcart){ + if($shopcart['activity_type'] == 1){ + $builder = Goods::query(); + }else{ + $builder = GoodsActivity::query(); + } + $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray(); + foreach ($shopcart['goods'] as $goods){ + if($shopcart['activity_type'] == 1){ + $goodsType = $this->goodsService->check($goods['id']); + }else{ + $goodsType = $this->goodsActivityService->check($goods['id'],$shopcart['num'],$shopcart['user_id']); + } + if($goodsType !== true){ + unset($shopcart['goods'][$key]); + continue; + } + $subtotal+= $shopcart['num'] * $shopcart['goods']['price']; + } + } + $store['subtotal'] = $subtotal; + $storeArr[] = $store; } - return $res; + return $storeArr; } public function check($goodsId) @@ -60,7 +117,7 @@ class ShopCartService implements ShopCartServiceInterface return mt_rand(1,100); } - public function getTotal() + public function getTotal($userId,$marketId) { $randomFloat = rand(100,999)/100; return $randomFloat; diff --git a/app/Service/v3/Implementations/ShopCartUpdateService.php b/app/Service/v3/Implementations/ShopCartUpdateService.php index fce914b..7ce4e95 100644 --- a/app/Service/v3/Implementations/ShopCartUpdateService.php +++ b/app/Service/v3/Implementations/ShopCartUpdateService.php @@ -11,7 +11,7 @@ use App\Model\v3\GoodsActivity; use App\Constants\v3\Goods as GoodsConstants; class ShopCartUpdateService implements ShopCartUpdateServiceInterface { - public function do($user_id,$goods_id,$market_id,$num,$activity_type) + public function do($user_id,$goods_id,$num,$activity_type) { //判断是普通商品还是特价商品 if(isset($params['activity']) && $params['activity'] == GoodsConstants::IS_ACTIVITY){ @@ -19,7 +19,10 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface }else{ $builder = Goods::query(); } - $goods = $builder->select('store_id')->find($goods_id); + $goods = $builder->select('market_id','store_id')->find($goods_id); + if(empty($goods)){ + throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS); + } return ShoppingCart::query()->updateOrCreate( [ 'user_id' => $user_id, @@ -27,7 +30,7 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface 'activity_type' => $activity_type ], [ - 'market_id' => $market_id, + 'market_id' => $goods->market_id, 'store_id' => $goods->store_id, 'num' => $num ] diff --git a/app/Service/v3/Implementations/StoreService.php b/app/Service/v3/Implementations/StoreService.php index 6b29057..3cf5a14 100644 --- a/app/Service/v3/Implementations/StoreService.php +++ b/app/Service/v3/Implementations/StoreService.php @@ -14,9 +14,29 @@ class StoreService implements StoreServiceInterface // TODO: Implement do() method. } - public function check() + public function check($storeId) { - // TODO: Implement check() method. + $store = Store::query()->where([ + ['id','=',$storeId], + ['is_rest','=',0], + ['status','=',2] + ]) + ->select('time1','time2','time3','time4') + ->first(); + if(!empty($store)){ + $nowTime = time(); + $time1 = strtotime(($store->time1)); + $time2 = strtotime(($store->time2)); + if($time1 < $nowTime && $nowTime < $time2){ + return true; + } + $time3 = strtotime(($store->time3)); + $time4 = strtotime(($store->time4)); + if($time3 < $nowTime && $nowTime < $time4){ + return true; + } + } + return false; } public function undo() diff --git a/app/Service/v3/Interfaces/ShopCartServiceInterface.php b/app/Service/v3/Interfaces/ShopCartServiceInterface.php index eb9c0cc..311be25 100644 --- a/app/Service/v3/Interfaces/ShopCartServiceInterface.php +++ b/app/Service/v3/Interfaces/ShopCartServiceInterface.php @@ -4,7 +4,7 @@ namespace App\Service\v3\Interfaces; interface ShopCartServiceInterface { - public function do(); + public function do($userId,$marketId); public function check($goodsId); @@ -12,5 +12,5 @@ interface ShopCartServiceInterface public function countGoods(); - public function getTotal(); + public function getTotal($userId,$marketId); } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php b/app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php index 06ead28..dcd4893 100644 --- a/app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php +++ b/app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php @@ -4,7 +4,7 @@ namespace App\Service\v3\Interfaces; interface ShopCartUpdateServiceInterface { - public function do($user_id,$goods_id,$market_id,$num,$activity_type); + public function do($user_id,$goods_id,$num,$activity_type); public function check(); diff --git a/app/Service/v3/Interfaces/StoreServiceInterface.php b/app/Service/v3/Interfaces/StoreServiceInterface.php index 452c429..376575e 100644 --- a/app/Service/v3/Interfaces/StoreServiceInterface.php +++ b/app/Service/v3/Interfaces/StoreServiceInterface.php @@ -5,7 +5,7 @@ namespace App\Service\v3\Interfaces; interface StoreServiceInterface { public function do(); - public function check(); + public function check($storeId); public function undo(); public function detail($storeId); } \ No newline at end of file