Browse Source

限制用户一天购买的活动商品,目前0.01的订单笔数。

master
weigang 5 years ago
parent
commit
b540a400ad
  1. 6
      app/Constants/v3/ErrorCode.php
  2. 9
      app/Constants/v3/OrderState.php
  3. 10
      app/Controller/v3/OrderOnlineController.php
  4. 19
      app/Controller/v3/ShopCartUpdateController.php
  5. 30
      app/Service/v3/Implementations/OrderOnlineService.php
  6. 7
      app/Service/v3/Interfaces/OrderOnlineServiceInterface.php

6
app/Constants/v3/ErrorCode.php

@ -101,6 +101,12 @@ class ErrorCode extends AbstractConstants
*/ */
const ORDER_ONLINE_LIMIT_STORE_BUY_SELF = 613; const ORDER_ONLINE_LIMIT_STORE_BUY_SELF = 613;
/**
* 今天当前用户购买的活动商品订单笔数已经超过限制
* @Message("您今天已经参与活动商品秒杀的次数已经用完咯,明天再来吧")
*/
const ORDER_ONLINE_LIMIT_BUY_COUNT = 614;
/************************************/ /************************************/
/* 支付相关 651-700 */ /* 支付相关 651-700 */
/************************************/ /************************************/

9
app/Constants/v3/OrderState.php

@ -90,4 +90,13 @@ class OrderState extends AbstractConstants
*/ */
const CAN_REFUND_DIRECT = [self::COMPLETED, self::EVALUATED, self::REFUND_REFUSE]; 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
];
} }

10
app/Controller/v3/OrderOnlineController.php

@ -205,6 +205,16 @@ class OrderOnlineController extends BaseController
// 下单 // 下单
$params = $request->validated(); $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']) : []; $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : [];
$data = $this->orderOnlineService->do( $data = $this->orderOnlineService->do(
$params['market_id'], $params['market_id'],

19
app/Controller/v3/ShopCartUpdateController.php

@ -2,12 +2,21 @@
namespace App\Controller\v3; namespace App\Controller\v3;
use App\Constants\v3\ErrorCode;
use App\Controller\BaseController; use App\Controller\BaseController;
use App\Exception\ErrorCodeException;
use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface; use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface;
use App\Request\v3\ShopCartUpdateRequest; use App\Request\v3\ShopCartUpdateRequest;
class ShopCartUpdateController extends BaseController class ShopCartUpdateController extends BaseController
{ {
/**
* @Inject
* @var OrderOnlineServiceInterface
*/
protected $orderOnlineService;
/** /**
* @Inject * @Inject
* @var ShopCartUpdateServiceInterface * @var ShopCartUpdateServiceInterface
@ -19,6 +28,16 @@ class ShopCartUpdateController extends BaseController
$goodsId = $this->request->input('goods_id',0); $goodsId = $this->request->input('goods_id',0);
$num = $this->request->input('num',0); $num = $this->request->input('num',0);
$activityType = $this->request->input('activity_type',1); $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); $res = $this->shopCarServiceUpdate->do($userId,$goodsId,$num,$activityType);
return $this->success($res); return $this->success($res);
} }

30
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;
}
} }

7
app/Service/v3/Interfaces/OrderOnlineServiceInterface.php

@ -76,4 +76,11 @@ interface OrderOnlineServiceInterface
* @return mixed * @return mixed
*/ */
public function autoCancel(); public function autoCancel();
/**
* 校验今天是否买过x单[y,z]分钱的活动商品
* @param $userId
* @return mixed
*/
public function checkIfBuyFlashGoodsToday($userId);
} }
Loading…
Cancel
Save