Browse Source

加入购物车

master
Lemon 6 years ago
parent
commit
9c6694c2d6
  1. 6
      app/Controller/PurchaseLimitController.php
  2. 14
      app/Model/Combination.php
  3. 14
      app/Model/ShopCar.php
  4. 97
      app/Service/PurchaseLimitService.php
  5. 2
      app/Service/PurchaseLimitServiceInterface.php
  6. 1
      config/routes.php

6
app/Controller/PurchaseLimitController.php

@ -35,4 +35,10 @@ class PurchaseLimitController extends BaseController
return $this->success($res); return $this->success($res);
} }
public function test()
{
$res = $this->purchaseLimitService->test($this->request->all());
return $this->success($res);
}
} }

14
app/Model/Combination.php

@ -0,0 +1,14 @@
<?php
namespace App\Model;
class Combination extends Model
{
const INVENTORY_NOLIMIT = 1;
protected $table = 'ims_cjdc_spec_combination';
public $timestamps = false;
}

14
app/Model/ShopCar.php

@ -0,0 +1,14 @@
<?php
namespace App\Model;
class ShopCar extends Model
{
const INVENTORY_NOLIMIT = 1;
protected $table = 'ims_cjdc_shopcar';
public $timestamps = false;
}

97
app/Service/PurchaseLimitService.php

@ -2,17 +2,103 @@
namespace App\Service; namespace App\Service;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use App\Model\Goods;
use App\Model\Combination;
use App\Model\ShopCar;
class PurchaseLimitService implements PurchaseLimitServiceInterface class PurchaseLimitService implements PurchaseLimitServiceInterface
{ {
public function addShopCar($params) public function addShopCar($params)
{ {
if($params['good_id'] == 1561){
//获取主表商品表信息
$goods = Goods::where([
['id','=',$params['good_id']],
])
->select('is_max','restrict_num','box_money','inventory','money')
->first();
//获取购物车该商品购买数量
$num = ShopCar::where([
['user_id', $params['user_id']],
['good_id', $params['good_id']],
])
->sum('num');
//限购检验
if($goods->restrict_num > 0 && $goods->restrict_num <= $num){
return false; return false;
}
//获取规格表商品信息
if($params['combination_id'] > 0) {
$combination = Combination::where([
['id', '=', $params['combination_id']],
])
->select('wm_money', 'number')
->first();
$inventory = $combination->number;
$money = $combination->wm_money;
}else{ }else{
return '加入购物车成功';
$inventory = $goods->inventory;
$money = $goods->money;
}
//库存校验
if($goods->is_max == 2 && ($num + $params['num']) > $inventory)
{
return false;
} }
//更新购物车
$exists = 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']);
}
$test = $exists->exists();
if($test)
{
$update = ShopCar::where([
['user_id', '=', $params['user_id']],
['good_id', '=', $params['good_id']],
['market_id','=',$params['market_id']],
]);
if($params['combination_id'] > 0) {
$update->where('combination_id',$params['combination_id']);
}
$update->increment('num', $params['num']);
}else{
$son_id = empty($params['son_id']) ? 0 : $params['son_id'];
$dr_id = empty($params['dr_id']) ? 0 : $params['dr_id'];
$combination_id = empty($params['combination_id']) ? 0 : $params['combination_id'];
$qg_name = empty($params['qg_name']) ? ' ' : $params['qg_name'];
$qg_logo = empty($params['qg_logo']) ? ' ' :$params['qg_logo'];
ShopCar::insert(
[
'market_id' => $params['market_id'],
'good_id' => $params['good_id'],
'store_id' => $params['store_id'],
'user_id' => $params['user_id'],
'combination_id' => $combination_id,
'num' => $params['num'],
'spec' => $params['spec'],
'son_id' => $son_id,
'dr_id' => $dr_id,
'qg_name' => $qg_name,
'qg_logo' => $qg_logo,
'money' => $money,
'box_money' => $goods->box_money,
]
);
}
return $test;
// if($params['goods_id'] == 1561){
// return false;
// }else{
// return '加入购物车成功';
// }
} }
public function updateShopCar($params) public function updateShopCar($params)
@ -23,4 +109,9 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
return '更新购物车成功'; return '更新购物车成功';
} }
} }
public function test($params)
{
return $params;
}
} }

2
app/Service/PurchaseLimitServiceInterface.php

@ -9,4 +9,6 @@ interface PurchaseLimitServiceInterface
public function addShopCar($params); public function addShopCar($params);
public function updateShopCar($params); public function updateShopCar($params);
public function test($params);
} }

1
config/routes.php

@ -58,6 +58,7 @@ Router::addGroup('/v1/',function (){
//加入购物车 //加入购物车
Router::post('PurchaseLimit/addShopCar', 'App\Controller\PurchaseLimitController@addShopCar'); Router::post('PurchaseLimit/addShopCar', 'App\Controller\PurchaseLimitController@addShopCar');
Router::post('PurchaseLimit/updateShopCar', 'App\Controller\PurchaseLimitController@updateShopCar'); Router::post('PurchaseLimit/updateShopCar', 'App\Controller\PurchaseLimitController@updateShopCar');
Router::post('PurchaseLimit/test', 'App\Controller\PurchaseLimitController@test');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]);
Router::addGroup('/wechat/',function () { Router::addGroup('/wechat/',function () {

Loading…
Cancel
Save