8 changed files with 242 additions and 195 deletions
-
19app/Controller/PurchaseLimitController.php
-
39app/Controller/ShopCarController.php
-
168app/Service/PurchaseLimitService.php
-
4app/Service/PurchaseLimitServiceInterface.php
-
185app/Service/ShopCarService.php
-
12app/Service/ShopCarServiceInterface.php
-
1config/autoload/dependencies.php
-
9config/routes.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); |
|||
} |
|||
|
|||
} |
|||
@ -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 '更新购物车成功';
|
|||
// }
|
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Service; |
|||
|
|||
|
|||
interface ShopCarServiceInterface |
|||
{ |
|||
public function addShopCar($params); |
|||
|
|||
public function updateShopCar($params); |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue