Browse Source

购物车 接口分离

master
Lemon 5 years ago
parent
commit
b2eabe8a4a
  1. 19
      app/Controller/PurchaseLimitController.php
  2. 39
      app/Controller/ShopCarController.php
  3. 168
      app/Service/PurchaseLimitService.php
  4. 4
      app/Service/PurchaseLimitServiceInterface.php
  5. 185
      app/Service/ShopCarService.php
  6. 12
      app/Service/ShopCarServiceInterface.php
  7. 1
      config/autoload/dependencies.php
  8. 9
      config/routes.php

19
app/Controller/PurchaseLimitController.php

@ -16,25 +16,6 @@ class PurchaseLimitController extends BaseController
*/ */
protected $purchaseLimitService; protected $purchaseLimitService;
public function addShopCar()
{
$res = $this->purchaseLimitService->addShopCar($this->request->all());
if (isset($res['error'])) {
return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']);
}
return $this->success($res);
}
public function updateShopCar()
{
$res = $this->purchaseLimitService->updateShopCar($this->request->all());
if (isset($res['error'])) {
return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']);
}
return $this->success($res);
}
public function getStoreIdByMarketId() public function getStoreIdByMarketId()
{ {
$res = $this->purchaseLimitService->getStoreIdByMarketId($this->request->all()); $res = $this->purchaseLimitService->getStoreIdByMarketId($this->request->all());

39
app/Controller/ShopCarController.php

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Service\ShopCarService;
use Hyperf\Di\Annotation\Inject;
use App\Service\ShopCarServiceInterface;
use App\Constants\ErrorCode;
class ShopCarController extends BaseController
{
/**
* @Inject
* @var ShopCarServiceInterface
*/
protected $shopCarService;
public function addShopCar()
{
$res = $this->shopCarService->addShopCar($this->request->all());
if (isset($res['error'])) {
return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']);
}
return $this->success($res);
}
public function updateShopCar()
{
$res = $this->shopCarService->updateShopCar($this->request->all());
if (isset($res['error'])) {
return $this->result(ErrorCode::GOODS_FAILURE, '', $res['error']);
}
return $this->success($res);
}
}

168
app/Service/PurchaseLimitService.php

@ -15,174 +15,6 @@ use App\Constants\SsdbKeysPrefix;
class PurchaseLimitService implements PurchaseLimitServiceInterface class PurchaseLimitService implements PurchaseLimitServiceInterface
{ {
public function addShopCar($params)
{
//获取ssdb购买记录
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$record_exists = $ssdb->exec('hexists',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'], $params['good_id']);
if($record_exists)
{
$error = [
'error' => '特价商品每日只能购买一次'
];
return $error;
}
//获取主表商品表信息
$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)
{
$error = [
'error' => '超过商品限购数量'
];
return $error;
}
//获取规格表商品信息
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{
$inventory = $goods->inventory;
$money = $goods->money;
}
//库存校验 is_max 无限库存
if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory)
{
$error = [
'error' => '库存不足'
];
return $error;
}
//更新购物车
$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_sql->where('combination_id',$params['combination_id']);
}
$exists = $exists_sql->exists();
if($exists)
{
$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 true;
// if($params['goods_id'] == 1561){
// return false;
// }else{
// return '加入购物车成功';
// }
}
public function updateShopCar($params)
{
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 != Goods::INVENTORY_NOLIMIT && $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 getStoreIdByMarketId($params) public function getStoreIdByMarketId($params)
{ {
$res = [ $res = [

4
app/Service/PurchaseLimitServiceInterface.php

@ -6,10 +6,6 @@ namespace App\Service;
interface PurchaseLimitServiceInterface interface PurchaseLimitServiceInterface
{ {
public function addShopCar($params);
public function updateShopCar($params);
public function getStoreIdByMarketId($params); public function getStoreIdByMarketId($params);
public function ssdbPurchaseRecord($params); public function ssdbPurchaseRecord($params);

185
app/Service/ShopCarService.php

@ -0,0 +1,185 @@
<?php
namespace App\Service;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use App\Model\Goods;
use App\Model\Combination;
use App\Model\ShopCar;
use App\Constants\LogLabel;
use App\Commons\Log;
use Hyperf\Utils\ApplicationContext;
use App\TaskWorker\SSDBTask;
use App\Constants\SsdbKeysPrefix;
class ShopCarService implements ShopCarServiceInterface
{
public function addShopCar($params)
{
//获取ssdb购买记录
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$record_exists = $ssdb->exec('hexists',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'], $params['good_id']);
if($record_exists)
{
$error = [
'error' => '特价商品每日只能购买一次'
];
return $error;
}
//获取主表商品表信息
$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)
{
$error = [
'error' => '超过商品限购数量'
];
return $error;
}
//获取规格表商品信息
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{
$inventory = $goods->inventory;
$money = $goods->money;
}
//库存校验 is_max 无限库存
if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory)
{
$error = [
'error' => '库存不足'
];
return $error;
}
//更新购物车
$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_sql->where('combination_id',$params['combination_id']);
}
$exists = $exists_sql->exists();
if($exists)
{
$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 true;
// if($params['goods_id'] == 1561){
// return false;
// }else{
// return '加入购物车成功';
// }
}
public function updateShopCar($params)
{
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 != Goods::INVENTORY_NOLIMIT && $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 '更新购物车成功';
// }
}
}

12
app/Service/ShopCarServiceInterface.php

@ -0,0 +1,12 @@
<?php
namespace App\Service;
interface ShopCarServiceInterface
{
public function addShopCar($params);
public function updateShopCar($params);
}

1
config/autoload/dependencies.php

@ -31,4 +31,5 @@ return [
\App\JsonRpc\OrderServiceInterface::class => \App\JsonRpc\OrderService::class, \App\JsonRpc\OrderServiceInterface::class => \App\JsonRpc\OrderService::class,
\App\Service\FinancialRecordServiceInterface::class => \App\Service\FinancialRecordService::class, \App\Service\FinancialRecordServiceInterface::class => \App\Service\FinancialRecordService::class,
\App\Service\SeparateAccountsServiceInterface::class => \App\Service\SeparateAccountsService::class, \App\Service\SeparateAccountsServiceInterface::class => \App\Service\SeparateAccountsService::class,
\App\Service\ShopCarServiceInterface::class => \App\Service\ShopCarService::class,
]; ];

9
config/routes.php

@ -51,17 +51,18 @@ Router::addGroup('/v1/',function (){
Router::post('Order/addOnline', 'App\Controller\OrderController@addOnlineOrder'); Router::post('Order/addOnline', 'App\Controller\OrderController@addOnlineOrder');
Router::post('Order/addOffline', 'App\Controller\OrderController@addOfflineOrder'); Router::post('Order/addOffline', 'App\Controller\OrderController@addOfflineOrder');
Router::post('Order/onlineCancel', 'App\Controller\OrderController@onlineCancel'); Router::post('Order/onlineCancel', 'App\Controller\OrderController@onlineCancel');
Router::post('Order/userComp', 'App\Controller\OrderController@userComp');
//小程序支付相关 //小程序支付相关
Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline'); Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline');
Router::post('wxminipay/offline', 'App\Controller\PaymentController@wxminiPayOffline'); Router::post('wxminipay/offline', 'App\Controller\PaymentController@wxminiPayOffline');
Router::post('Order/userComp', 'App\Controller\OrderController@userComp');
//加入购物车 //加入购物车
Router::post('PurchaseLimit/addShopCar', 'App\Controller\PurchaseLimitController@addShopCar');
Router::post('PurchaseLimit/updateShopCar', 'App\Controller\PurchaseLimitController@updateShopCar');
Router::post('ShopCar/addShopCar', 'App\Controller\ShopCarController@addShopCar');
Router::post('ShopCar/updateShopCar', 'App\Controller\ShopCarController@updateShopCar');
Router::post('PurchaseLimit/test', 'App\Controller\PurchaseLimitController@test'); Router::post('PurchaseLimit/test', 'App\Controller\PurchaseLimitController@test');
Router::post('PurchaseLimit/getStoreIdByMarketId', 'App\Controller\PurchaseLimitController@getStoreIdByMarketId'); Router::post('PurchaseLimit/getStoreIdByMarketId', 'App\Controller\PurchaseLimitController@getStoreIdByMarketId');
Router::post('Order/userComp', 'App\Controller\OrderController@userComp');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]);
Router::addGroup('/wechat/',function () { Router::addGroup('/wechat/',function () {

Loading…
Cancel
Save