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.

107 lines
2.9 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. $goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
  43. }else{
  44. $builder = Goods::query();
  45. $goodsType = Goods::class;
  46. $goods = $builder->find($goodsId);
  47. if(empty($goods)){
  48. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  49. }
  50. $goodsCheck = $this->goodsService->check($goods,$num);
  51. }
  52. if(empty($goods)){
  53. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  54. }
  55. if($goodsCheck !== true)
  56. {
  57. throw new ErrorCodeException($goodsCheck);
  58. }
  59. $shoppingCart = ShoppingCart::query()->lockForUpdate()->updateOrCreate(
  60. [
  61. 'user_id' => $userId,
  62. 'goods_id' => $goodsId,
  63. 'activity_type' => $activityType
  64. ],
  65. [
  66. 'market_id' => $goods->market_id,
  67. 'store_id' => $goods->store_id,
  68. 'num' => $num,
  69. 'goods_type' => $goodsType
  70. ]
  71. );
  72. $storeCheck = $this->storeService->check($goods->store_id);
  73. $shoppingCart->check = $storeCheck;
  74. return $shoppingCart;
  75. }
  76. public function check()
  77. {
  78. // TODO: Implement check() method.
  79. }
  80. public function undo($shopcartIds)
  81. {
  82. $shopcartIdsArr = explode(',',$shopcartIds);
  83. return ShoppingCart::destroy($shopcartIdsArr);
  84. }
  85. /**
  86. * 清空购物车
  87. * @param $userId
  88. * @param $marketId
  89. */
  90. public function doClear($userId, $marketId)
  91. {
  92. return ShoppingCart::query()->where([
  93. ['user_id','=',$userId],
  94. ['market_id','=',$marketId],
  95. ])
  96. ->delete();
  97. }
  98. }