Browse Source

更新购物车

master
Lemon 5 years ago
parent
commit
3b788e2dbb
  1. 4
      app/Controller/PurchaseLimitController.php
  2. 90
      app/Service/PurchaseLimitService.php

4
app/Controller/PurchaseLimitController.php

@ -29,8 +29,8 @@ class PurchaseLimitController extends BaseController
public function updateShopCar()
{
$res = $this->purchaseLimitService->updateShopCar($this->request->all());
if (!$res) {
return $this->result(ErrorCode::GOODS_FAILURE, '', '更新购物车失败');
if (isset($res['error'])) {
return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']);
}
return $this->success($res);
}

90
app/Service/PurchaseLimitService.php

@ -26,14 +26,19 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
])
->sum('num');
//限购检验
if($goods->restrict_num > 0 && $goods->restrict_num <= $num){
return false;
if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
{
$error = [
'error' => '超过商品限购数量'
];
return $error;
}
//获取规格表商品信息
if($params['combination_id'] > 0) {
if($params['combination_id'] > 0)
{
$combination = Combination::where([
['id', '=', $params['combination_id']],
])
['id', '=', $params['combination_id']],
])
->select('wm_money', 'number')
->first();
$inventory = $combination->number;
@ -42,23 +47,26 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
$inventory = $goods->inventory;
$money = $goods->money;
}
//库存校验
//库存校验 is_max 无限库存
if($goods->is_max == 2 && ($num + $params['num']) > $inventory)
{
return false;
$error = [
'error' => '库存不足'
];
return $error;
}
//更新购物车
$exists = ShopCar::where([
$exists_sql = ShopCar::where([
['user_id', '=', $params['user_id']],
['good_id', '=', $params['good_id']],
['market_id','=',$params['market_id']],
['combination_id','=', $params['combination_id']],
]);
if($params['combination_id'] > 0) {
$exists->where('combination_id',$params['combination_id']);
$exists_sql->where('combination_id',$params['combination_id']);
}
$test = $exists->exists();
if($test)
$exists = $exists_sql->exists();
if($exists)
{
$update = ShopCar::where([
['user_id', '=', $params['user_id']],
@ -93,7 +101,7 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
]
);
}
return $test;
return true;
// if($params['goods_id'] == 1561){
// return false;
// }else{
@ -103,11 +111,61 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
public function updateShopCar($params)
{
if($params['good_id'] == 1561){
return false;
}else{
return '更新购物车成功';
if($params['num'] <= 0)
{
ShopCar::where('id',$params['id'])->delete();
}else {
$shop_car = ShopCar::where('id',$params['id'])
->select('good_id','user_id')
->first();
//获取主表商品表信息
$goods = Goods::where([
['id','=',$shop_car['good_id']],
])
->select('is_max','restrict_num','box_money','inventory','money')
->first();
//获取购物车该商品购买数量
$num = ShopCar::where([
['user_id', $shop_car['user_id']],
['good_id', $shop_car['good_id']],
])
->sum('num');
//限购检验
if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
{
$error = [
'error' => '超过商品限购数量'
];
return $error;
}
if ($shop_car['combination_id'] > 0)
{
$combination = Combination::where([
['id', '=', $shop_car['combination_id']],
])
->select('wm_money', 'number')
->first();
$inventory = $combination->number;
}else{
$inventory = $goods->inventory;
}
//库存校验 is_max 无限库存
if($goods->is_max == 2 && $params['num'] > $inventory)
{
$error = [
'error' => '库存不足'
];
return $error;
}
ShopCar::where('id',$params['id'])
->update(['num' => $params['num']]);
return true;
}
// if($params['good_id'] == 1561){
// return false;
// }else{
// return '更新购物车成功';
// }
}
public function test($params)

Loading…
Cancel
Save