diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index b05f653..329673b 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -298,7 +298,7 @@ class ErrorCode extends AbstractConstants /** * 已卖光 - * @Message("商品已卖光") + * @Message("库存不足") */ const GOODS_ACTIVITY_INVENTORY_ERROR = 1153; diff --git a/app/Controller/v3/GoodsController.php b/app/Controller/v3/GoodsController.php index 3425853..1bed88a 100644 --- a/app/Controller/v3/GoodsController.php +++ b/app/Controller/v3/GoodsController.php @@ -6,6 +6,7 @@ use App\Constants\v3\ErrorCode; use App\Controller\BaseController; use App\Exception\ErrorCodeException; use App\Model\v3\Category; +use App\Model\v3\GoodsActivity; use App\Model\v3\GoodsCategory; use App\Request\v3\GoodsEditorRequest; use App\Service\v3\Interfaces\CategoryServiceInterface; @@ -199,4 +200,17 @@ class GoodsController extends BaseController $bannerId = $this->request->input('banner_id',0); return $this->success($this->goodsService->bannerDelete($bannerId)); } + + public function getList() + { + $page = $this->request->input('page',0); + $pagesize = $this->request->input('pagesize',10); + $activity = $this->request->input('activity',''); + $marketId = $this->request->input('market_id',-1); + $res = ''; + if($activity == 'bachelor'){ + $res = $this->goodsActivityService->getList($marketId,$page,$pagesize); + } + return $this->success($res); + } } \ No newline at end of file diff --git a/app/Controller/v3/ShopCartUpdateController.php b/app/Controller/v3/ShopCartUpdateController.php index 00e83a9..d1f7130 100644 --- a/app/Controller/v3/ShopCartUpdateController.php +++ b/app/Controller/v3/ShopCartUpdateController.php @@ -5,6 +5,7 @@ namespace App\Controller\v3; use App\Constants\v3\ErrorCode; use App\Controller\BaseController; use App\Exception\ErrorCodeException; +use App\Model\v3\ShoppingCart; use App\Service\v3\Interfaces\OrderOnlineServiceInterface; use Hyperf\Di\Annotation\Inject; use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface; @@ -28,19 +29,28 @@ class ShopCartUpdateController extends BaseController $goodsId = $this->request->input('goods_id',0); $num = $this->request->input('num',0); $activityType = $this->request->input('activity_type',1); - - if ($activityType == 2) { - // 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')] - ); + if($num <= 0){ + $shopcartIds = ShoppingCart::query()->where([ + 'user_id' => $userId, + 'goods_id' => $goodsId, + 'activity_type' => $activityType + ]) + ->value('id'); + $res = $this->shopCarServiceUpdate->undo($shopcartIds); + }else{ + if ($activityType == 2) { + // 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); } diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 923974b..892904c 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -82,7 +82,8 @@ class Goods extends Model 'is_effective', 'noneffective_note', 'details_imgs_url', - 'goods_name' + 'goods_name', + 'activity_type' ]; protected function boot(): void @@ -93,6 +94,11 @@ class Goods extends Model }); } + public function getActivityTypeAttribute() + { + return 1; + } + public function scopeOrderByDefault($query, $sort) { return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort); diff --git a/app/Model/v3/GoodsActivity.php b/app/Model/v3/GoodsActivity.php index 5df8ea2..dfaa39f 100644 --- a/app/Model/v3/GoodsActivity.php +++ b/app/Model/v3/GoodsActivity.php @@ -53,6 +53,7 @@ class GoodsActivity extends Model 'noneffective_note', 'total_seconds', 'details_imgs_url', + 'activity_type' ]; // protected $visible = [ @@ -68,6 +69,11 @@ class GoodsActivity extends Model }); } + public function getActivityTypeAttribute() + { + return 2; + } + public function getMonthSalesAttribute() { $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); diff --git a/app/Request/v3/ShopCartUpdateRequest.php b/app/Request/v3/ShopCartUpdateRequest.php index c467aab..008fc9b 100644 --- a/app/Request/v3/ShopCartUpdateRequest.php +++ b/app/Request/v3/ShopCartUpdateRequest.php @@ -16,7 +16,7 @@ class ShopCartUpdateRequest extends BaseFormRequest { return [ 'goods_id' => 'required|nonempty|integer', - 'num' => 'required|nonempty|integer', + 'num' => 'required|integer', 'user_id' => 'required|nonempty|integer|exists:lanzu_user,id', ]; } diff --git a/app/Service/v3/Implementations/GoodsActivityService.php b/app/Service/v3/Implementations/GoodsActivityService.php index a29b14e..359c66b 100644 --- a/app/Service/v3/Implementations/GoodsActivityService.php +++ b/app/Service/v3/Implementations/GoodsActivityService.php @@ -5,8 +5,10 @@ namespace App\Service\v3\Implementations; use App\Commons\Log; use App\Constants\v3\ActivityType; use App\Constants\v3\ErrorCode; +use App\Constants\v3\Goods as GoodsConstants; use App\Constants\v3\LogLabel; use App\Constants\v3\SsdbKeys; +use App\Model\v3\Store; use App\Service\v3\Interfaces\GoodsActivityServiceInterface; use App\Service\v3\Interfaces\GoodsInventoryServiceInterface; use App\TaskWorker\SSDBTask; @@ -191,4 +193,45 @@ class GoodsActivityService implements GoodsActivityServiceInterface return true; } + + public function getList($marketId,$page,$pagesize) + { + $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable(); + $goodsTable = ApplicationContext::getContainer()->get(GoodsActivity::class)->getTable(); + + $builder = GoodsActivity::query() + ->join($storeTable, ''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id') + ->with(['store']) + //->where([''.$goodsTable.'.type' => $type]) + ->where(function ($query) use ($marketId, $goodsTable) { + $query->whereJsonContains(''.$goodsTable.'.market_ids', [(string)$marketId]) + ->orWhereJsonLength(''.$goodsTable.'.market_ids', '=', 0); + }) + ->where([''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES]) + // ->where(function ($query) use ($goodsTable) { + // $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1); + // }) + ->whereRaw(''.$goodsTable.'.deleted_at IS NULL') + ->where([''.$storeTable.'.market_id' => $marketId]) + ->where([''.$storeTable.'.is_open' => \App\Constants\v3\Store::IS_OPEN_YES]) + ->where([''.$storeTable.'.is_rest' => \App\Constants\v3\Store::IS_REST_NO]) + ->where('time1', '<=', date('H:i')) + ->where(function ($query) { + $query->where('time2', '>=', date('H:i')) + ->orWhere('time4', '>=', date('H:i')); + }) + ->where(''.$goodsTable.'.expire_time', '>', time()); + $paginate = $builder->select(''.$goodsTable.'.*')->addSelect(''.$goodsTable.'.sales as total_sales') + ->orderBy(''.$goodsTable.'.sort', 'DESC') + ->orderBy(''.$goodsTable.'.expire_time', 'ASC') + ->orderBy(''.$goodsTable.'.created_at', 'DESC') + ->paginate($pagesize); + $goodsArr = $paginate->toArray(); + $goods = collect($goodsArr['data']); + $goods = $goods->sortBy(function ($product, $key) { + return $product['noneffective_note']; + }); + $goods = $goods->values()->all(); + return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods]; + } } \ No newline at end of file diff --git a/app/Service/v3/Implementations/ShopCartUpdateService.php b/app/Service/v3/Implementations/ShopCartUpdateService.php index 72fd76f..8b22179 100644 --- a/app/Service/v3/Implementations/ShopCartUpdateService.php +++ b/app/Service/v3/Implementations/ShopCartUpdateService.php @@ -69,7 +69,20 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface if($goodsCheck !== true) { - throw new ErrorCodeException($goodsCheck); + if($goodsCheck === ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR){ + $shopcartNum = ShoppingCart::query() + ->where([ + 'user_id' => $userId, + 'goods_id' => $goodsId, + 'activity_type' => $activityType + ]) + ->value('num'); + if($num > $shopcartNum){ + throw new ErrorCodeException($goodsCheck); + } + }else{ + throw new ErrorCodeException($goodsCheck); + } } $shoppingCart = ShoppingCart::query()->lockForUpdate()->updateOrCreate( diff --git a/app/Service/v3/Interfaces/GoodsActivityServiceInterface.php b/app/Service/v3/Interfaces/GoodsActivityServiceInterface.php index cac9c86..e056799 100644 --- a/app/Service/v3/Interfaces/GoodsActivityServiceInterface.php +++ b/app/Service/v3/Interfaces/GoodsActivityServiceInterface.php @@ -14,4 +14,5 @@ interface GoodsActivityServiceInterface public function cacheRecord($goodsId, $num, $userId); public function clearCacheRecord($goodsId, $num, $userId); public function checkOrderActivityCount($goodsIds, $limitNum=1); + public function getList($marketId,$page,$pagesize); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index a82953c..bd6c86a 100644 --- a/config/routes.php +++ b/config/routes.php @@ -100,6 +100,7 @@ Router::addGroup('/v3/', function () { Router::post('home/market', 'App\Controller\v3\HomeController@marketInfo'); Router::post('location/addrSuggestion', 'App\Controller\v3\LocationController@addrSuggestion'); Router::post('userAddress/deliveryDistance', 'App\Controller\v3\UserAddressController@deliveryDistance'); + Router::post('goods/getList', 'App\Controller\v3\GoodsController@getList'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); // 需要登录的路由