diff --git a/app/Controller/v3/GoodsController.php b/app/Controller/v3/GoodsController.php index 060ddfb..6faa017 100644 --- a/app/Controller/v3/GoodsController.php +++ b/app/Controller/v3/GoodsController.php @@ -54,7 +54,7 @@ class GoodsController extends BaseController } if(isset($params['user_id'])) { - $res['shopcart']['count'] = $this->shopCartService->check(111); + $res['shopcart']['count'] = $this->shopCartService->check($params['user_id'],$params['goods_id']); $res['shopcart']['total'] = $this->shopCartService->getTotal($params['user_id'],$params['market_id']); $res['detail']->store->is_collected = (bool)$this->collectService->check($params['user_id'], $res['detail']->store->id); }else{ diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 4831453..452d025 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -100,7 +100,7 @@ class Goods extends Model public function getCartNumAttribute() { - return (integer)$this->shopCartService->check($this->id); + return (integer)$this->shopCartService->check($this->request->user->id, $this->id); } public function getIsEffectiveAttribute() diff --git a/app/Service/v3/Implementations/ShopCartService.php b/app/Service/v3/Implementations/ShopCartService.php index a8276fd..0247fd6 100644 --- a/app/Service/v3/Implementations/ShopCartService.php +++ b/app/Service/v3/Implementations/ShopCartService.php @@ -76,9 +76,15 @@ class ShopCartService implements ShopCartServiceInterface return $storeArr; } - public function check($goodsId) + public function check($userId,$goodsId) { - return mt_rand(0,6); + $ShoppingCart = ShoppingCart::query()->where([ + ['user_id','=',$userId], + ['goods_id','=',$goodsId], + ]) + ->select('num') + ->get(); + return $ShoppingCart->num; } public function undo($userId,$marketId) @@ -130,13 +136,47 @@ class ShopCartService implements ShopCartServiceInterface public function countGoods($userId,$marketId) { - return mt_rand(1,100); + $ShoppingCart = ShoppingCart::query()->where([ + ['user_id','=',$userId], + ['market_id','=',$marketId], + ])->get(); + $num = 0; + foreach ($ShoppingCart as $cart){ + if($cart->activity_type == 2){ + $goods = GoodsActivity::query()->find($cart->goods_id); + $goodsCheck = $this->goodsActivityService->check($goods,$cart->num,$cart->user_id); + }else{ + $goods = Goods::query()->find($cart->goods_id); + $goodsCheck = $this->goodsService->check($goods,$cart->num); + } + if($goodsCheck){ + $num+= $cart->num; + } + } + return $num; } public function getTotal($userId,$marketId) { - $randomFloat = rand(100,999)/100; - return $randomFloat; + $ShoppingCart = ShoppingCart::query()->where([ + ['user_id','=',$userId], + ['market_id','=',$marketId], + ])->get(); + $total = 0; + foreach ($ShoppingCart as $cart){ + if($cart->activity_type == 2){ + $goods = GoodsActivity::query()->find($cart->goods_id); + $goodsCheck = $this->goodsActivityService->check($goods,$cart->num,$cart->user_id); + }else{ + $goods = Goods::query()->find($cart->goods_id); + $goodsCheck = $this->goodsService->check($goods,$cart->num); + } + if($goodsCheck){ + $subTotal = bcmul($goods->price,$cart->num); + $total = bcadd($subTotal,$total,2); + } + } + return $total; } public function getGoodsByShopcartId($shopcartIds) @@ -191,14 +231,13 @@ class ShopCartService implements ShopCartServiceInterface public function allForUser($userId, $marketId) { - bcscale(6); + //bcscale(6); // 查询当前用户的市场下的购物车数据 $carts = ShoppingCart::query() ->with(['store', 'goods']) ->where(['user_id' => $userId]) ->where(['market_id' => $marketId]) ->get(); - $cartList = []; $totalAmount = 0; foreach ($carts as $key => &$cart) { @@ -231,8 +270,10 @@ class ShopCartService implements ShopCartServiceInterface // 商品是否失效 if ($cart->activity_type == 2) { + $goods = GoodsActivity::query()->find($cart->goods_id); $res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id); } else { + $goods = Goods::query()->find($cart->goods_id); $res = $this->goodsService->check($cart->goods, $cart->num); } diff --git a/app/Service/v3/Interfaces/ShopCartServiceInterface.php b/app/Service/v3/Interfaces/ShopCartServiceInterface.php index 6a8ade3..5f636ce 100644 --- a/app/Service/v3/Interfaces/ShopCartServiceInterface.php +++ b/app/Service/v3/Interfaces/ShopCartServiceInterface.php @@ -6,7 +6,7 @@ interface ShopCartServiceInterface { public function do($userId,$marketId); - public function check($goodsId); + public function check($userId,$goodsId); public function undo($userId,$marketId);