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.

121 lines
3.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  6. use App\Service\v3\Interfaces\GoodsServiceInterface;
  7. use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface;
  8. use App\Model\v3\ShoppingCart;
  9. use App\Model\v3\Goods;
  10. use App\Model\v3\GoodsActivity;
  11. use App\Constants\v3\Goods as GoodsConstants;
  12. use App\Service\v3\Interfaces\StoreServiceInterface;
  13. use Hyperf\Di\Annotation\Inject;
  14. class ShopCartUpdateService implements ShopCartUpdateServiceInterface
  15. {
  16. /**
  17. * @Inject
  18. * @var GoodsServiceInterface
  19. */
  20. protected $goodsService;
  21. /**
  22. * @Inject
  23. * @var GoodsActivityServiceInterface
  24. */
  25. protected $goodsActivityService;
  26. /**
  27. * @Inject
  28. * @var StoreServiceInterface
  29. */
  30. protected $storeService;
  31. public function do($userId,$goodsId,$num,$activityType)
  32. {
  33. $goodsType = '';
  34. //判断是普通商品还是特价商品
  35. if($activityType == GoodsConstants::IS_ACTIVITY){
  36. $builder = GoodsActivity::query();
  37. $goodsType = GoodsActivity::class;
  38. $goods = $builder->find($goodsId);
  39. if(empty($goods)){
  40. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  41. }
  42. //特价商品限购
  43. $goodsArr = ShoppingCart::query()
  44. ->where(['user_id' => $userId,'market_id' => $goods->market_id,'activity_type' => GoodsConstants::IS_ACTIVITY])
  45. ->pluck('goods_id')->toArray();
  46. array_push($goodsArr,$goodsId);
  47. $checkGoodsActivity = $this->goodsActivityService->checkOrderActivityCount($goodsArr);
  48. if(!$checkGoodsActivity){
  49. throw new ErrorCodeException(ErrorCode::GOODS_ACTIVITY_BUY);
  50. }
  51. $goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
  52. }else{
  53. $builder = Goods::query();
  54. $goodsType = Goods::class;
  55. $goods = $builder->find($goodsId);
  56. if(empty($goods)){
  57. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  58. }
  59. $goodsCheck = $this->goodsService->check($goods,$num);
  60. }
  61. if(empty($goods)){
  62. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  63. }
  64. if($goodsCheck !== true)
  65. {
  66. throw new ErrorCodeException($goodsCheck);
  67. }
  68. $shoppingCart = ShoppingCart::query()->lockForUpdate()->updateOrCreate(
  69. [
  70. 'user_id' => $userId,
  71. 'goods_id' => $goodsId,
  72. 'activity_type' => $activityType
  73. ],
  74. [
  75. 'market_id' => $goods->market_id,
  76. 'store_id' => $goods->store_id,
  77. 'num' => $num,
  78. 'goods_type' => $goodsType
  79. ]
  80. );
  81. $storeCheck = $this->storeService->check($goods->store_id);
  82. $shoppingCart->check = $storeCheck;
  83. return $shoppingCart;
  84. }
  85. public function check()
  86. {
  87. // TODO: Implement check() method.
  88. }
  89. public function undo($shopcartIds)
  90. {
  91. $shopcartIdsArr = explode(',',$shopcartIds);
  92. return ShoppingCart::destroy($shopcartIdsArr);
  93. }
  94. /**
  95. * 清空购物车
  96. * @param $userId
  97. * @param $marketId
  98. * @param array $shopcartIds
  99. * @return int|mixed
  100. */
  101. public function doClear($userId, $marketId, $shopcartIds = [])
  102. {
  103. $builder = ShoppingCart::query()->where([
  104. ['user_id','=',$userId],
  105. ['market_id','=',$marketId],
  106. ]);
  107. if (!empty($shopcartIds)) {
  108. $builder = $builder->whereIn('id', $shopcartIds);
  109. }
  110. return $builder->delete();
  111. }
  112. }