You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

122 lines
3.7 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;
use App\Exception\ErrorCodeException;
use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
use App\Service\v3\Interfaces\GoodsServiceInterface;
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;
use App\Service\v3\Interfaces\StoreServiceInterface;
use Hyperf\Di\Annotation\Inject;
class ShopCartUpdateService implements ShopCartUpdateServiceInterface
{
/**
* @Inject
* @var GoodsServiceInterface
*/
protected $goodsService;
/**
* @Inject
* @var GoodsActivityServiceInterface
*/
protected $goodsActivityService;
/**
* @Inject
* @var StoreServiceInterface
*/
protected $storeService;
public function do($userId,$goodsId,$num,$activityType)
{
$goodsType = '';
//判断是普通商品还是特价商品
if($activityType == GoodsConstants::IS_ACTIVITY){
$builder = GoodsActivity::query();
$goodsType = GoodsActivity::class;
$goods = $builder->find($goodsId);
if(empty($goods)){
throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
}
//特价商品限购
$goodsArr = ShoppingCart::query()
->where(['user_id' => $userId,'market_id' => $goods->market_id,'activity_type' => GoodsConstants::IS_ACTIVITY])
->pluck('goods_id')->toArray();
array_push($goodsArr,$goodsId);
$checkGoodsActivity = $this->goodsActivityService->checkOrderActivityCount($goodsArr);
if(!$checkGoodsActivity){
throw new ErrorCodeException(ErrorCode::GOODS_ACTIVITY_RESTRICT_LIMIT);
}
$goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
}else{
$builder = Goods::query();
$goodsType = Goods::class;
$goods = $builder->find($goodsId);
if(empty($goods)){
throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
}
$goodsCheck = $this->goodsService->check($goods,$num);
}
if(empty($goods)){
throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
}
if($goodsCheck !== true)
{
throw new ErrorCodeException($goodsCheck);
}
$shoppingCart = ShoppingCart::query()->lockForUpdate()->updateOrCreate(
[
'user_id' => $userId,
'goods_id' => $goodsId,
'activity_type' => $activityType
],
[
'market_id' => $goods->market_id,
'store_id' => $goods->store_id,
'num' => $num,
'goods_type' => $goodsType
]
);
$storeCheck = $this->storeService->check($goods->store_id);
$shoppingCart->check = $storeCheck;
return $shoppingCart;
}
public function check()
{
// TODO: Implement check() method.
}
public function undo($shopcartIds)
{
$shopcartIdsArr = explode(',',$shopcartIds);
return ShoppingCart::destroy($shopcartIdsArr);
}
/**
* 清空购物车
* @param $userId
* @param $marketId
* @param array $shopcartIds
* @return int|mixed
*/
public function doClear($userId, $marketId, $shopcartIds = [])
{
$builder = ShoppingCart::query()->where([
['user_id','=',$userId],
['market_id','=',$marketId],
]);
if (!empty($shopcartIds)) {
$builder = $builder->whereIn('id', $shopcartIds);
}
return $builder->delete();
}
}