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.
|
|
<?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 Hyperf\Di\Annotation\Inject;class ShopCartUpdateService implements ShopCartUpdateServiceInterface{ /** * @Inject * @var GoodsServiceInterface */ protected $goodsService;
/** * @Inject * @var GoodsActivityServiceInterface */ protected $goodsActivityService;
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); } $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); }
return 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 ] ); }
public function check() { // TODO: Implement check() method.
}
public function undo($shopcartIds) { $shopcartIdsArr = explode(',',$shopcartIds); return ShoppingCart::destroy($shopcartIdsArr); }
/** * 清空购物车 * @param $userId * @param $marketId */ public function doClear($userId, $marketId) { return ShoppingCart::query()->where([ ['user_id','=',$userId], ['market_id','=',$marketId], ]) ->delete(); }}
|