Browse Source

更新或插入购物车 并区分特价商品

master
Lemon 5 years ago
parent
commit
28e1697a93
  1. 7
      app/Controller/v3/ShopCartUpdateController.php
  2. 10
      app/Model/v3/ShoppingCart.php
  3. 40
      app/Service/v3/Implementations/ShopCartUpdateService.php
  4. 2
      app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php

7
app/Controller/v3/ShopCartUpdateController.php

@ -15,7 +15,12 @@ class ShopCartUpdateController extends BaseController
protected $shopCarServiceUpdate;
public function update()
{
$res = $this->shopCarServiceUpdate->do($this->request->all());
$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);
return $this->success($res);
}

10
app/Model/v3/ShoppingCart.php

@ -11,6 +11,16 @@ class ShoppingCart extends Model
protected $table = 'lanzu_shopping_cart';
protected $fillable = [
'id',
'user_id',
'goods_id',
'market_id',
'store_id',
'num',
'activity_type'
];
public function goods()
{
return $this->hasOne(Goods::class, 'id', 'goods_id');

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

@ -5,25 +5,33 @@ namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;
use App\Exception\ErrorCodeException;
use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface;
use App\Model\v3\ShoppingCart;
use App\Model\v3\Goods;
use App\Model\v3\GoodsActivity;
use App\Constants\v3\Goods as GoodsConstants;
class ShopCartUpdateService implements ShopCartUpdateServiceInterface
{
public function do($params)
public function do($user_id,$goods_id,$market_id,$num,$activity_type)
{
switch ($params['goods_id'])
{
case 1000:
throw new ErrorCodeException(ErrorCode::GOODS_INVENTORY_ERROR);
break;
case 2000:
throw new ErrorCodeException(ErrorCode::GOODS_RESTRICT_LIMIT);
break;
case 3000:
throw new ErrorCodeException(ErrorCode::GOODS_ACTIVITY_BUY);
break;
default:
return true;
}
//判断是普通商品还是特价商品
if(isset($params['activity']) && $params['activity'] == GoodsConstants::IS_ACTIVITY){
$builder = GoodsActivity::query();
}else{
$builder = Goods::query();
}
$goods = $builder->select('store_id')->find($goods_id);
return ShoppingCart::query()->updateOrCreate(
[
'user_id' => $user_id,
'goods_id' => $goods_id,
'activity_type' => $activity_type
],
[
'market_id' => $market_id,
'store_id' => $goods->store_id,
'num' => $num
]
);
}
public function check()

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

@ -4,7 +4,7 @@ namespace App\Service\v3\Interfaces;
interface ShopCartUpdateServiceInterface
{
public function do($params);
public function do($user_id,$goods_id,$market_id,$num,$activity_type);
public function check();

Loading…
Cancel
Save