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.

91 lines
2.4 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
  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 Hyperf\Di\Annotation\Inject;
  13. class ShopCartUpdateService implements ShopCartUpdateServiceInterface
  14. {
  15. /**
  16. * @Inject
  17. * @var GoodsServiceInterface
  18. */
  19. protected $goodsService;
  20. /**
  21. * @Inject
  22. * @var GoodsActivityServiceInterface
  23. */
  24. protected $goodsActivityService;
  25. public function do($userId,$goodsId,$num,$activityType)
  26. {
  27. $goodsType = '';
  28. //判断是普通商品还是特价商品
  29. if($activityType == GoodsConstants::IS_ACTIVITY){
  30. $builder = GoodsActivity::query();
  31. $goodsType = GoodsActivity::class;
  32. $goods = $builder->find($goodsId);
  33. $goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
  34. }else{
  35. $builder = Goods::query();
  36. $goodsType = Goods::class;
  37. $goods = $builder->find($goodsId);
  38. $goodsCheck = $this->goodsService->check($goods,$num);
  39. }
  40. if(empty($goods)){
  41. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  42. }
  43. if($goodsCheck !== true)
  44. {
  45. throw new ErrorCodeException($goodsCheck);
  46. }
  47. return ShoppingCart::query()->lockForUpdate()->updateOrCreate(
  48. [
  49. 'user_id' => $userId,
  50. 'goods_id' => $goodsId,
  51. 'activity_type' => $activityType
  52. ],
  53. [
  54. 'market_id' => $goods->market_id,
  55. 'store_id' => $goods->store_id,
  56. 'num' => $num,
  57. 'goods_type' => $goodsType
  58. ]
  59. );
  60. }
  61. public function check()
  62. {
  63. // TODO: Implement check() method.
  64. }
  65. public function undo($shopcartIds)
  66. {
  67. $shopcartIdsArr = explode(',',$shopcartIds);
  68. return ShoppingCart::destroy($shopcartIdsArr);
  69. }
  70. /**
  71. * 清空购物车
  72. * @param $userId
  73. * @param $marketId
  74. */
  75. public function doClear($userId, $marketId)
  76. {
  77. return ShoppingCart::query()->where([
  78. ['user_id','=',$userId],
  79. ['market_id','=',$marketId],
  80. ])
  81. ->delete();
  82. }
  83. }