From 3c63227571ade11bbe37f37708366cfcb02704d9 Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 2 Sep 2020 10:34:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E6=88=B7=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/v3/SearchController.php | 7 +++- app/Model/v3/Goods.php | 19 +++++++-- app/Model/v3/Store.php | 37 +++++++++++++++- app/Request/v3/SearchStoreRequest.php | 42 +++++++++++++++++++ .../v3/Implementations/SearchService.php | 34 ++++++++++++++- .../v3/Implementations/ShopCartService.php | 2 +- .../v3/Interfaces/SearchServiceInterface.php | 2 +- .../Interfaces/ShopCartServiceInterface.php | 2 +- 8 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 app/Request/v3/SearchStoreRequest.php diff --git a/app/Controller/v3/SearchController.php b/app/Controller/v3/SearchController.php index 0a02244..c13db94 100644 --- a/app/Controller/v3/SearchController.php +++ b/app/Controller/v3/SearchController.php @@ -4,6 +4,7 @@ namespace App\Controller\v3; use App\Controller\BaseController; use App\Request\v3\SearchGoodsRequest; +use App\Request\v3\SearchStoreRequest; use App\Service\v3\Interfaces\SearchServiceInterface; use Hyperf\Di\Annotation\Inject; use Psr\Http\Message\ResponseInterface; @@ -40,15 +41,17 @@ class SearchController extends BaseController */ public function goods(SearchGoodsRequest $request) { - $params = $request->validated(); $data = $this->searchService->doForGoods($params); return $this->success(['goods' => $data]); } - public function stores() + public function stores(SearchStoreRequest $request) { + $params = $request->validated(); + $data = $this->searchService->doForStores($params); + return $this->success(['stores' => $data]); } } \ No newline at end of file diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 4e89989..4c0848c 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -7,6 +7,7 @@ use App\Constants\v3\SsdbKeys; use App\Model\Model; use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\TaskWorker\SSDBTask; +use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\SoftDeletes; use Hyperf\Utils\ApplicationContext; use Hyperf\Di\Annotation\Inject; @@ -17,6 +18,9 @@ class Goods extends Model { use SoftDeletes; + const ON_SALE_YES = 1; + const ON_SALE_NO = 2; + /** * @Inject * @var ShopCartServiceInterface @@ -51,9 +55,17 @@ class Goods extends Model 'cart_num', ]; + protected function boot(): void + { + parent::boot(); + self::addGlobalScope('normal', function (Builder $builder) { + $builder->where(['on_sale' => self::ON_SALE_YES]); + }); + } + public function scopeOrderByDefault($query, $sort) { - return $query->orderBy('id', $sort); + return $query->orderBy('sort', $sort)->orderBy('id', $sort); } public function scopeOrderBySales($query, $sort) @@ -68,8 +80,9 @@ class Goods extends Model public function getMonthSalesAttribute() { - $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); - return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id); + return 1; + // $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + // return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id); } public function getCartNumAttribute() diff --git a/app/Model/v3/Store.php b/app/Model/v3/Store.php index fe95339..2d66d3c 100644 --- a/app/Model/v3/Store.php +++ b/app/Model/v3/Store.php @@ -4,6 +4,7 @@ declare (strict_types=1); namespace App\Model\v3; use App\Model\Model; +use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\SoftDeletes; /** @@ -12,6 +13,13 @@ class Store extends Model { use SoftDeletes; + const IS_OPEN_YES = 1; + const IS_OPEN_NO = 2; + const STATUS_EXAMINING = 1; + const STATUS_PASS = 2; + const STATUS_REFUSE = 3; + const STATUS_EXPIRED = 4; + /** * The table associated with the model. * @@ -31,7 +39,34 @@ class Store extends Model */ protected $casts = []; - protected $appends = []; + protected $appends = [ + 'month_sales' + ]; + + protected function boot(): void + { + parent::boot(); + self::addGlobalScope('normal', function (Builder $builder) { + $builder->where(['is_open' => self::IS_OPEN_YES, 'status' => self::STATUS_PASS]); + }); + } + + public function scopeOrderByDefault($query, $sort) + { + return $query->orderBy('sort', $sort)->orderBy('id', $sort); + } + + public function scopeOrderBySales($query, $sort) + { + return $query->orderBy('sales', $sort); + } + public function getMonthSalesAttribute() { + return mt_rand(0,100); + } + public function goods() + { + return $this->hasMany(Goods::class, 'store_id', 'id'); + } } \ No newline at end of file diff --git a/app/Request/v3/SearchStoreRequest.php b/app/Request/v3/SearchStoreRequest.php new file mode 100644 index 0000000..0de9a4b --- /dev/null +++ b/app/Request/v3/SearchStoreRequest.php @@ -0,0 +1,42 @@ + 'required|nonempty|integer', + 'type_id' => 'nonempty|integer', + 'keyword' => 'nonempty', + 'order_by' => 'nonempty|in:default,sales', + 'sort' => 'nonempty|in:asc,desc', + 'page' => 'nonempty|integer', + 'pagesize' => 'nonempty|integer', + ]; + } + + /** + * @return array + */ + public function messages(): array + { + return [ + '*.*' => ':attribute无效', + ]; + } + + public function attributes(): array + { + return parent::attributes(); + } +} diff --git a/app/Service/v3/Implementations/SearchService.php b/app/Service/v3/Implementations/SearchService.php index c0e2bba..546834a 100644 --- a/app/Service/v3/Implementations/SearchService.php +++ b/app/Service/v3/Implementations/SearchService.php @@ -5,6 +5,7 @@ namespace App\Service\v3\Implementations; use App\Model\v3\Goods; +use App\Model\v3\Store; class SearchService implements \App\Service\v3\Interfaces\SearchServiceInterface { @@ -58,9 +59,38 @@ class SearchService implements \App\Service\v3\Interfaces\SearchServiceInterface return $data; } - public function doForStores() + public function doForStores($params) { - // TODO: Implement doForStores() method. + $builder = Store::query() + ->with(['goods' => function($query) { + return $query->limit(5) + ->select(['id', 'store_id', 'cover_img', 'name', 'spec', 'tags', 'original_price', 'price', 'inventory', 'sales as total_sales']); + }]) + ->where(['market_id' => $params['market_id']]); + + if (isset($params['type_id']) && $params['type_id']) { + $builder->where(['store_type_id' => $params['type_id']]); + } + + if (isset($params['keyword']) && $params['keyword']) { + $builder->where('name', 'like', '%'.$params['keyword'].'%'); + } + + if (isset($params['order_by']) && $params['order_by']) { + $sort = $params['sort'] ?? 'desc'; + switch ($params['order_by']) { + case 'sales': + $builder->orderBySales($sort); + break; + default: + $builder->orderByDefault($sort); + break; + } + } + + $builder->select(['id', 'logo', 'name']); + $stores = $builder->forPage($params['page'], $params['pagesize'])->get()->toArray(); + return $stores; } public function getHotKeywords($type) diff --git a/app/Service/v3/Implementations/ShopCartService.php b/app/Service/v3/Implementations/ShopCartService.php index 4f1a451..730ad4d 100644 --- a/app/Service/v3/Implementations/ShopCartService.php +++ b/app/Service/v3/Implementations/ShopCartService.php @@ -55,7 +55,7 @@ class ShopCartService implements ShopCartServiceInterface return $res; } - public function getGoodsNum() + public function check($goodsId) { return mt_rand(0,6); } diff --git a/app/Service/v3/Interfaces/SearchServiceInterface.php b/app/Service/v3/Interfaces/SearchServiceInterface.php index b04a88a..0767938 100644 --- a/app/Service/v3/Interfaces/SearchServiceInterface.php +++ b/app/Service/v3/Interfaces/SearchServiceInterface.php @@ -5,7 +5,7 @@ namespace App\Service\v3\Interfaces; interface SearchServiceInterface { public function doForGoods($params); - public function doForStores(); + public function doForStores($params); public function getHotKeywords($type); public function do(); public function check(); diff --git a/app/Service/v3/Interfaces/ShopCartServiceInterface.php b/app/Service/v3/Interfaces/ShopCartServiceInterface.php index 8c7848c..5eb25c2 100644 --- a/app/Service/v3/Interfaces/ShopCartServiceInterface.php +++ b/app/Service/v3/Interfaces/ShopCartServiceInterface.php @@ -6,7 +6,7 @@ interface ShopCartServiceInterface { public function detail(); - public function getGoodsNum(); + public function check($goodsId); public function undo(); } \ No newline at end of file