Browse Source

商户搜索

master
weigang 6 years ago
parent
commit
3c63227571
  1. 7
      app/Controller/v3/SearchController.php
  2. 19
      app/Model/v3/Goods.php
  3. 37
      app/Model/v3/Store.php
  4. 42
      app/Request/v3/SearchStoreRequest.php
  5. 34
      app/Service/v3/Implementations/SearchService.php
  6. 2
      app/Service/v3/Implementations/ShopCartService.php
  7. 2
      app/Service/v3/Interfaces/SearchServiceInterface.php
  8. 2
      app/Service/v3/Interfaces/ShopCartServiceInterface.php

7
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]);
}
}

19
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()

37
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');
}
}

42
app/Request/v3/SearchStoreRequest.php

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Request\v3;
use App\Request\BaseFormRequest;
class SearchStoreRequest extends BaseFormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'market_id' => '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();
}
}

34
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)

2
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);
}

2
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();

2
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();
}
Loading…
Cancel
Save