diff --git a/app/Service/v3/Implementations/ShopCartService.php b/app/Service/v3/Implementations/ShopCartService.php index 0247fd6..a2d708b 100644 --- a/app/Service/v3/Implementations/ShopCartService.php +++ b/app/Service/v3/Implementations/ShopCartService.php @@ -83,7 +83,7 @@ class ShopCartService implements ShopCartServiceInterface ['goods_id','=',$goodsId], ]) ->select('num') - ->get(); + ->first(); return $ShoppingCart->num; } @@ -181,45 +181,67 @@ class ShopCartService implements ShopCartServiceInterface public function getGoodsByShopcartId($shopcartIds) { - $shopcartIds = explode(',',$shopcartIds); - $storeIds = Db::table('lanzu_shopping_cart') - ->whereIn('id',$shopcartIds) - ->pluck('store_id') - ->toArray(); - $stores = Store::query()->with(['ShoppingCart' => function($query) use ($shopcartIds){ - $query->whereIn('id',$shopcartIds); - }])->whereIn('id',$storeIds) - ->get() - ->toArray(); - $storeArr = []; - foreach ($stores as $key => &$store){ - $sotreType = $this->storeService->check($store['id']); - if(!$sotreType){ + bcscale(6); + // 查询当前用户的市场下的购物车数据 + $shopcartIdsArr = explode(',',$shopcartIds); + $carts = ShoppingCart::query() + ->with(['store', 'goods']) + ->whereIn('id',$shopcartIdsArr) + ->get(); + $cartList = []; + $totalAmount = 0; + foreach ($carts as $key => &$cart) { + + if (empty($cart->store)||empty($cart->goods)) { continue; } - $subtotal = 0; - foreach ($store['shopping_cart'] as $k => &$shopcart){ - if($shopcart['activity_type'] == 1){ - $goodsType = $this->goodsService->check($shopcart['goods_id']); - }else{ - $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']); - } - if($goodsType !== true){ - unset($store['shopping_cart'][$k]); - continue; - } - if($shopcart['activity_type'] == 1){ - $builder = Goods::query(); - }else{ - $builder = GoodsActivity::query(); + + $shoppingCart = $cart->toArray(); + $store = $shoppingCart['store']; + unset($shoppingCart['store']); + + // 商户是否歇业 + $checkStore = $this->storeService->check($store['id']); + if (!$checkStore) { + continue; + } + + // 商品是否失效 + if ($cart->activity_type == 2) { + $goods = GoodsActivity::query()->find($cart->goods_id); + $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id); + } else { + $goods = Goods::query()->find($cart->goods_id); + $res = $this->goodsService->check($goods, $cart->num); + } + + if ($res === true) { + + $cartList['store_lists'][$store['id']]['store'] = $store; + $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart; + + if (isset($cartList['store_lists'][$store['id']]['subtotal'])) { + $cartList['store_lists'][$store['id']]['subtotal'] = bcadd( + $cartList['store_lists'][$store['id']]['subtotal'], + bcmul($cart->goods->price, $cart->num) + ); + } else { + $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num); } - $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray(); - $subtotal+= $shopcart['num'] * $shopcart['goods']['price']; + + $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num)); } - $store['subtotal'] = $subtotal; - $storeArr[] = $store; } - return $storeArr; + + $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : []; + + foreach ($cartList['store_lists'] as $key => &$value) { + $value['subtotal'] = bcadd($value['subtotal'], '0' , 2); + } + + $cartList['total'] = bcadd($totalAmount, '0', 2); + + return $cartList['store_lists']; } /** @@ -231,7 +253,7 @@ class ShopCartService implements ShopCartServiceInterface public function allForUser($userId, $marketId) { - //bcscale(6); + bcscale(6); // 查询当前用户的市场下的购物车数据 $carts = ShoppingCart::query() ->with(['store', 'goods']) @@ -271,10 +293,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); + $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id); } else { $goods = Goods::query()->find($cart->goods_id); - $res = $this->goodsService->check($cart->goods, $cart->num); + $res = $this->goodsService->check($goods, $cart->num); } if ($res === true) {