|
@@ -6,6 +6,7 @@ use App\Base\BaseService;
|
|
|
use App\Models\Goods\Category;
|
|
|
use App\Models\Goods\Goods;
|
|
|
use App\Models\Goods\GoodsCategoryMap;
|
|
|
+use App\Models\Goods\GoodsSpecMap;
|
|
|
use App\Models\Goods\Spec;
|
|
|
use App\Models\Goods\SpecAttr;
|
|
|
use App\Models\Setting;
|
|
@@ -154,17 +155,21 @@ class PageService extends BaseService
|
|
|
|
|
|
public function search($params)
|
|
|
{
|
|
|
- $attrId = [];
|
|
|
+ $attrMap = [];
|
|
|
if ($attrs = Arr::get($params, "attrs", [])) {
|
|
|
- $attrId = json_decode($attrs, true);
|
|
|
+ $attrMap = json_decode($attrs, true);
|
|
|
}
|
|
|
$pageSize = Arr::get($params, "page_size", 10);
|
|
|
- $p = Goods::where("status", Goods::STATUS_OK)->when($attrId, function (Builder $query) use ($attrId) {
|
|
|
- $d = SpecAttr::whereIn("id", $attrId)->groupBy("spec_id")->get(['spec_id']);
|
|
|
- $specIdArr = $d->pluck("spec_id")->toArray();
|
|
|
- foreach ($specIdArr as $specId) {
|
|
|
- $query->whereJsonContains("spec", $specId);
|
|
|
+ $p = Goods::where("status", Goods::STATUS_OK)->when($attrMap, function (Builder $query) use ($attrMap) {
|
|
|
+ $mapQuery = GoodsSpecMap::query();
|
|
|
+ foreach ($attrMap as $attr) {
|
|
|
+ $mapQuery->orWhere(function (Builder $query) use ($attr) {
|
|
|
+ $query->where("spec_id", $attr['spec_id'])->Where("value", $attr['value']);
|
|
|
+ });
|
|
|
}
|
|
|
+ $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
|
|
|
+ $query->whereIn("id", $goodsIdArr);
|
|
|
+
|
|
|
})->when($params['keyword'], function (Builder $query) use ($params) {
|
|
|
$query->where("name", "like", "%" . $params['keyword'] . "%");
|
|
|
})->orderByDesc("weight")->paginate($pageSize);
|