Browse Source

购物车页面,购物车更新补充,怕徐倒叙

master
weigang 5 years ago
parent
commit
6291b21adf
  1. 3
      app/Controller/v3/ShopCartController.php
  2. 5
      app/Model/v3/Goods.php
  3. 5
      app/Model/v3/GoodsActivity.php
  4. 7
      app/Model/v3/ShoppingCart.php
  5. 6
      app/Service/v3/Implementations/GoodsActivityService.php
  6. 10
      app/Service/v3/Implementations/GoodsService.php
  7. 8
      app/Service/v3/Implementations/OrderListService.php
  8. 4
      app/Service/v3/Implementations/OrderOnlineService.php
  9. 103
      app/Service/v3/Implementations/ShopCartService.php
  10. 6
      app/Service/v3/Implementations/ShopCartUpdateService.php
  11. 4
      app/Service/v3/Interfaces/GoodsActivityServiceInterface.php
  12. 4
      app/Service/v3/Interfaces/GoodsServiceInterface.php
  13. 2
      app/Service/v3/Interfaces/ShopCartServiceInterface.php

3
app/Controller/v3/ShopCartController.php

@ -17,6 +17,9 @@ class ShopCartController extends BaseController
{
$userId = $this->request->input('user_id');
$marketId = $this->request->input('market_id');
return $this->success($this->shopCartService->allForUser($userId, $marketId));
//获取购物车商品信息
$res['store_lists'] = $this->shopCartService->do($userId,$marketId);
//获取购物车失效商品信息

5
app/Model/v3/Goods.php

@ -128,4 +128,9 @@ class Goods extends Model
return $this->belongsTo(Store::class, 'store_id', 'id');
}
public function carts()
{
return $this->morphMany(ShoppingCart::class, 'goods');
}
}

5
app/Model/v3/GoodsActivity.php

@ -94,4 +94,9 @@ class GoodsActivity extends Model
{
return $this->belongsTo(Store::class, 'store_id', 'id');
}
public function carts()
{
return $this->morphMany(ShoppingCart::class, 'goods');
}
}

7
app/Model/v3/ShoppingCart.php

@ -21,8 +21,13 @@ class ShoppingCart extends Model
'activity_type'
];
public function store()
{
return $this->belongsTo(Store::class, 'store_id', 'id');
}
public function goods()
{
return $this->hasOne(Goods::class, 'id', 'goods_id');
return $this->morphTo();
}
}

6
app/Service/v3/Implementations/GoodsActivityService.php

@ -20,10 +20,8 @@ class GoodsActivityService implements GoodsActivityServiceInterface
}
public function check($goodsId, $num, $userId)
public function check(GoodsActivity $goods, $num, $userId)
{
$goods = GoodsActivity::query()->with('store')->where('id', $goodsId)->first();
// 活动是否已经结束
if ($goods->expire_time < time()) {
@ -52,7 +50,7 @@ class GoodsActivityService implements GoodsActivityServiceInterface
// 是否已经购买过(某个时间段内,时间段有商品的限制)
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$hasBuy = $ssdb->exec('get', SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId);
$hasBuy = $ssdb->exec('get', SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goods->id);
if ($hasBuy && $hasBuy >= $goods->time_limit_num) {
return ErrorCode::GOODS_ACTIVITY_BUY;
}

10
app/Service/v3/Implementations/GoodsService.php

@ -17,19 +17,19 @@ class GoodsService implements GoodsServiceInterface
}
public function check($goodsId,$num = 1)
public function check(Goods $goods,$num = 1)
{
$data = Goods::query()->with('store')->where('id', $goodsId)->first();
// 商户歇业
if($data->store->is_rest == 1){
if($goods->store->is_rest == 1){
return ErrorCode::GOODS_ON_SALE_NO;
}
// 商品下架或已删除
if($data->on_sale == 0 || !is_null($data->deleted_at)){
if($goods->on_sale == 0 || !is_null($goods->deleted_at)){
return ErrorCode::GOODS_ON_SALE_NO;
}
// 商品库存不足
if($data->is_infinite != 1 && $data->inventory < $num){
if($goods->is_infinite != 1 && $goods->inventory < $num){
return ErrorCode::GOODS_INVENTORY_ERROR;
}

8
app/Service/v3/Implementations/OrderListService.php

@ -50,7 +50,7 @@ class OrderListService implements OrderListServiceInterface
break;
}
$paginate = $builder->paginate($pagesize);
$paginate = $builder->orderBy('created_at', 'desc')->paginate($pagesize);
$orders = $paginate->toArray();
return ['has_more_pages' => $paginate->hasMorePages(), 'orders' => $orders['data']];
}
@ -94,7 +94,7 @@ class OrderListService implements OrderListServiceInterface
break;
}
$paginate = $builder->paginate($pagesize);
$paginate = $builder->orderBy('created_at', 'desc')->paginate($pagesize);
$orders = $paginate->toArray();
$res = ['has_more_pages' => $paginate->hasMorePages(), 'orders' => $orders['data']];
foreach ($res['orders'] as &$v){
@ -138,7 +138,7 @@ class OrderListService implements OrderListServiceInterface
$builder->whereBetween('lanzu_order_main.created_at',[strtotime($start_time.' 23:59:59'),strtotime($end_time.' 23:59:59')]);
}
$paginate = $builder->paginate($pagesize);
$paginate = $builder->orderBy('created_at', 'desc')->paginate($pagesize);
$orders = $paginate->toArray();
return ['has_more_pages' => $paginate->hasMorePages(), 'orders' => $orders['data']];
}
@ -151,7 +151,7 @@ class OrderListService implements OrderListServiceInterface
}])
->where(['user_id' => $userId, 'type' => OrderType::OFFLINE]);
$paginate = $builder->paginate($pagesize);
$paginate = $builder->orderBy('created_at', 'desc')->paginate($pagesize);
$orders = $paginate->toArray();
return ['has_more_pages' => $paginate->hasMorePages(), 'orders' => $orders['data']];
}

4
app/Service/v3/Implementations/OrderOnlineService.php

@ -149,13 +149,13 @@ class OrderOnlineService implements OrderOnlineServiceInterface
$goods = [];
if ($cart->activity_type == 1) {
$goods = Goods::query()->lockForUpdate()->find($cart->goods_id);
$check = $this->goodsService->check($goods->id, $cart->num);
$check = $this->goodsService->check($goods, $cart->num);
if (true !== $check) {
throw new ErrorCodeException($check, '[商品失效1]'.$cart->goods_id);
}
} elseif ($cart->activity_type == 2) {
$goods = GoodsActivity::query()->lockForUpdate()->find($cart->goods_id);
$check = $this->goodsActivityService->check($goods->id, $cart->num, $userId);
$check = $this->goodsActivityService->check($goods, $cart->num, $userId);
if (true !== $check) {
throw new ErrorCodeException($check, '[商品失效2]'.$cart->goods_id);
}

103
app/Service/v3/Implementations/ShopCartService.php

@ -181,4 +181,107 @@ class ShopCartService implements ShopCartServiceInterface
}
return $storeArr;
}
/**
* 获取用户购物车数据
* @param $userId
* @param $marketId
* @return array
*/
public function allForUser($userId, $marketId)
{
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) {
if (empty($cart->store)) {
continue;
}
$shoppingCart = $cart->toArray();
$store = $shoppingCart['store'];
unset($shoppingCart['store']);
// 商户是否歇业
$checkStore = $this->storeService->check($store['id']);
if (!$checkStore) {
$cartList['store_lists_invalid'][$store['id']]['store'] = $store;
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
$cartList['store_lists_invalid'][$store['id']]['subtotal'],
bcmul($cart->goods->price, $cart->num)
);
} else {
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
}
continue;
}
// 商品是否失效
if ($cart->activity_type == 2) {
$res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id);
} else {
$res = $this->goodsService->check($cart->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);
}
$totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
} else {
$cartList['store_lists_invalid'][$store['id']]['store'] = $store;
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
$cartList['store_lists_invalid'][$store['id']]['subtotal'],
bcmul($cart->goods->price, $cart->num)
);
} else {
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
}
}
}
$cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
$cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
foreach ($cartList['store_lists'] as $key => &$value) {
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
}
foreach ($cartList['store_lists_invalid'] as $key => &$value) {
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
}
$cartList['total'] = bcadd($totalAmount, '0', 2);
return $cartList;
}
}

6
app/Service/v3/Implementations/ShopCartUpdateService.php

@ -13,11 +13,14 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface
{
public function do($user_id,$goods_id,$num,$activity_type)
{
$goodsType = '';
//判断是普通商品还是特价商品
if(isset($params['activity']) && $params['activity'] == GoodsConstants::IS_ACTIVITY){
$builder = GoodsActivity::query();
$goodsType = GoodsActivity::class;
}else{
$builder = Goods::query();
$goodsType = Goods::class;
}
$goods = $builder->select('market_id','store_id')->find($goods_id);
if(empty($goods)){
@ -32,7 +35,8 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface
[
'market_id' => $goods->market_id,
'store_id' => $goods->store_id,
'num' => $num
'num' => $num,
'goods_type' => $goodsType
]
);
}

4
app/Service/v3/Interfaces/GoodsActivityServiceInterface.php

@ -2,10 +2,12 @@
namespace App\Service\v3\Interfaces;
use App\Model\v3\GoodsActivity;
interface GoodsActivityServiceInterface
{
public function do($goodsId);
public function check($goodsId,$num,$userId);
public function check(GoodsActivity $goods,$num,$userId);
public function undo();
public function getBanner($goodsId);
public function detail($goodsId);

4
app/Service/v3/Interfaces/GoodsServiceInterface.php

@ -2,10 +2,12 @@
namespace App\Service\v3\Interfaces;
use App\Model\v3\Goods;
interface GoodsServiceInterface
{
public function do($goodsId);
public function check($goodsId,$num = 1);
public function check(Goods $goods,$num = 1);
public function undo();
public function getBanner($goodsId);
}

2
app/Service/v3/Interfaces/ShopCartServiceInterface.php

@ -15,4 +15,6 @@ interface ShopCartServiceInterface
public function getTotal($userId,$marketId);
public function getGoodsByShopcartId($shopcartIds);
public function allForUser($userId, $marketId);
}
Loading…
Cancel
Save