From b540a400ad334b8d38ae9854ca7c357c9031ebab Mon Sep 17 00:00:00 2001 From: weigang Date: Tue, 20 Oct 2020 16:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=E7=94=A8=E6=88=B7=E4=B8=80?= =?UTF-8?q?=E5=A4=A9=E8=B4=AD=E4=B9=B0=E7=9A=84=E6=B4=BB=E5=8A=A8=E5=95=86?= =?UTF-8?q?=E5=93=81=EF=BC=8C=E7=9B=AE=E5=89=8D0.01=E7=9A=84=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=AC=94=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/v3/ErrorCode.php | 6 ++++ app/Constants/v3/OrderState.php | 9 ++++++ app/Controller/v3/OrderOnlineController.php | 10 +++++++ .../v3/ShopCartUpdateController.php | 19 ++++++++++++ .../v3/Implementations/OrderOnlineService.php | 30 +++++++++++++++++++ .../OrderOnlineServiceInterface.php | 7 +++++ 6 files changed, 81 insertions(+) diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index 3fefb2c..803c627 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -101,6 +101,12 @@ class ErrorCode extends AbstractConstants */ const ORDER_ONLINE_LIMIT_STORE_BUY_SELF = 613; + /** + * 今天当前用户购买的活动商品订单笔数已经超过限制 + * @Message("您今天已经参与活动商品秒杀的次数已经用完咯,明天再来吧") + */ + const ORDER_ONLINE_LIMIT_BUY_COUNT = 614; + /************************************/ /* 支付相关 651-700 */ /************************************/ diff --git a/app/Constants/v3/OrderState.php b/app/Constants/v3/OrderState.php index fb7cfb5..895c36a 100644 --- a/app/Constants/v3/OrderState.php +++ b/app/Constants/v3/OrderState.php @@ -90,4 +90,13 @@ class OrderState extends AbstractConstants */ const CAN_REFUND_DIRECT = [self::COMPLETED, self::EVALUATED, self::REFUND_REFUSE]; + /** + * @Message("限制当天购买秒杀商品订单数") + */ + const LIMIT_BUY_COUNT = [ + self::UNPAID, self::DELIVERY, self::PAID, + self::COMPLETED, self::EVALUATED, + self::REFUNDING, self::REFUND_REFUSE, self::REFUNDED_DIRECT + ]; + } \ No newline at end of file diff --git a/app/Controller/v3/OrderOnlineController.php b/app/Controller/v3/OrderOnlineController.php index 2c32fa0..d710b9a 100644 --- a/app/Controller/v3/OrderOnlineController.php +++ b/app/Controller/v3/OrderOnlineController.php @@ -205,6 +205,16 @@ class OrderOnlineController extends BaseController // 下单 $params = $request->validated(); + + // TODO 校验当前用户今天是否超过了购买活动秒杀商品的(特定价格)的订单笔数 + if (!$this->orderOnlineService->checkIfBuyFlashGoodsToday($params['user_id'])) { + throw new ErrorCodeException( + ErrorCode::ORDER_ONLINE_LIMIT_BUY_COUNT, + '['.env('LIMIT_BUY_COUNT').']', + ['params' => $params, 'limit_prices' => env('LIMIT_BUY_COUNT_GOODS_PRICES')] + ); + } + $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : []; $data = $this->orderOnlineService->do( $params['market_id'], diff --git a/app/Controller/v3/ShopCartUpdateController.php b/app/Controller/v3/ShopCartUpdateController.php index fae561c..36f5d82 100644 --- a/app/Controller/v3/ShopCartUpdateController.php +++ b/app/Controller/v3/ShopCartUpdateController.php @@ -2,12 +2,21 @@ namespace App\Controller\v3; +use App\Constants\v3\ErrorCode; use App\Controller\BaseController; +use App\Exception\ErrorCodeException; +use App\Service\v3\Interfaces\OrderOnlineServiceInterface; use Hyperf\Di\Annotation\Inject; use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface; use App\Request\v3\ShopCartUpdateRequest; class ShopCartUpdateController extends BaseController { + /** + * @Inject + * @var OrderOnlineServiceInterface + */ + protected $orderOnlineService; + /** * @Inject * @var ShopCartUpdateServiceInterface @@ -19,6 +28,16 @@ class ShopCartUpdateController extends BaseController $goodsId = $this->request->input('goods_id',0); $num = $this->request->input('num',0); $activityType = $this->request->input('activity_type',1); + + // TODO 校验当前用户今天是否超过了购买活动秒杀商品的(特定价格)的订单笔数 + if (!$this->orderOnlineService->checkIfBuyFlashGoodsToday($userId)) { + throw new ErrorCodeException( + ErrorCode::ORDER_ONLINE_LIMIT_BUY_COUNT, + '['.env('LIMIT_BUY_COUNT').']', + ['params' => $this->request->all(), 'limit_prices' => env('LIMIT_BUY_COUNT_GOODS_PRICES')] + ); + } + $res = $this->shopCarServiceUpdate->do($userId,$goodsId,$num,$activityType); return $this->success($res); } diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index f73e17c..ca7b8a1 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -809,4 +809,34 @@ class OrderOnlineService implements OrderOnlineServiceInterface } } + + /** + * 校验用户今天是否买过x单[y,z]分钱的活动商品 + * @param $userId + */ + public function checkIfBuyFlashGoodsToday($userId) + { + $mainTable = ApplicationContext::getContainer()->get(OrderMain::class)->getTable(); + $orderTable = ApplicationContext::getContainer()->get(Order::class)->getTable(); + $goodsTable = ApplicationContext::getContainer()->get(OrderGoods::class)->getTable(); + + $limitPrices = explode(',', env('LIMIT_BUY_COUNT_GOODS_PRICES')); + $limitCount = env('LIMIT_BUY_COUNT'); + + $countToday = OrderMain::query() + ->join($orderTable, $orderTable.'.order_main_id', '=', $mainTable.'.global_order_id') + ->join($goodsTable, $goodsTable.'.order_id', '=', $orderTable.'.id') + ->where($mainTable.'.updated_at', '>=', strtotime(date('Y-m-d 00:00:00'))) + ->where($mainTable.'.updated_at', '<=', strtotime(date('Y-m-d 23:59:59'))) + ->whereIn($mainTable.'.state', OrderState::LIMIT_BUY_COUNT) + ->where([$mainTable.'.user_id' => $userId]) + ->whereIn($goodsTable.'.price', $limitPrices) + ->count(); + + if ($countToday >= $limitCount) { + return false; + } + + return true; + } } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/OrderOnlineServiceInterface.php b/app/Service/v3/Interfaces/OrderOnlineServiceInterface.php index 77b3420..0fc4654 100644 --- a/app/Service/v3/Interfaces/OrderOnlineServiceInterface.php +++ b/app/Service/v3/Interfaces/OrderOnlineServiceInterface.php @@ -76,4 +76,11 @@ interface OrderOnlineServiceInterface * @return mixed */ public function autoCancel(); + + /** + * 校验今天是否买过x单[y,z]分钱的活动商品 + * @param $userId + * @return mixed + */ + public function checkIfBuyFlashGoodsToday($userId); } \ No newline at end of file