kphcdr 1 سال پیش
والد
کامیت
7e84beb4ee

+ 9 - 1
app/Console/Commands/TestCommand.php

@@ -2,7 +2,8 @@
 
 namespace App\Console\Commands;
 
-use App\Models\Goods\GoodsSku;
+use App\Models\Goods\Goods;
+use App\Modules\Admin\Services\GoodsService;
 use Illuminate\Console\Command;
 use Nerd\CartesianProduct\CartesianProduct;
 
@@ -47,4 +48,11 @@ class TestCommand extends Command
         $cartesianProduct->appendSet(["红色", "白色"]);
         dump($cartesianProduct->compute());
     }
+
+    public function search()
+    {
+        Goods::get()->map(function (Goods $g) {
+            app(GoodsService::class)->createIndex($g);
+        });
+    }
 }

+ 30 - 0
app/Models/Goods/GoodsSearch.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Models\Goods;
+
+use App\Base\BaseModel;
+
+/**
+ * App\Models\Goods\GoodsSearch
+ *
+ * @property int                             $id
+ * @property int                             $goods_id
+ * @property string|null                     $content
+ * @property \Illuminate\Support\Carbon|null $created_at
+ * @property \Illuminate\Support\Carbon|null $updated_at
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch newModelQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch newQuery()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch query()
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch whereContent($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch whereCreatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch whereGoodsId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch whereId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSearch whereUpdatedAt($value)
+ * @mixin \Eloquent
+ */
+class GoodsSearch extends BaseModel
+{
+    protected $table = "goods_search";
+    protected $fillable = ["goods_id", "content"];
+
+}

+ 19 - 1
app/Modules/Admin/Services/GoodsService.php

@@ -6,6 +6,7 @@ use App\Base\BaseService;
 use App\Models\Goods\Goods;
 use App\Models\Goods\GoodsCategoryMap;
 use App\Models\Goods\GoodsCustom;
+use App\Models\Goods\GoodsSearch;
 use App\Models\Goods\GoodsSku;
 use App\Models\Goods\GoodsSpecMap;
 use Illuminate\Database\Eloquent\Builder;
@@ -53,7 +54,7 @@ class GoodsService extends BaseService
 
         $specIdArr = array_values(Arr::sort(Arr::get($params, "spec", [])));
 
-        DB::transaction(function () use ($params, $goods, $specIdArr) {
+        $goods = DB::transaction(function () use ($params, $goods, $specIdArr) {
             $goods->image_list = Arr::get($params, "image_list", []);
             $goods->sn = $params['sn'] ?? "";
             $goods->url_3d = $params['url_3d'] ?? "";
@@ -110,8 +111,25 @@ class GoodsService extends BaseService
             }
 
             GoodsSpecMap::where("goods_id", $goods->id)->whereNotIn("id", $doNotDelIdArr)->delete();
+            $this->createIndex($goods);
+            return $goods;
         });
 
+
+    }
+
+    public function createIndex(Goods $goods)
+    {
+        GoodsSearch::updateOrCreate([
+            "goods_id" => $goods->id,
+        ], [
+            "goods_id" => $goods->id,
+            "content" => $goods->name . $goods->en_name . implode("", array_map(function ($item) {
+                    return implode("", Arr::pluck($item, "attr.value"));
+                }, $goods->custom)) . strip_tags($goods->desc_html) . $goods->specMap->map(function (GoodsSpecMap $m) {
+                    return $m->value;
+                })->implode(""),
+        ]);
     }
 
     public function info($params)

+ 4 - 3
app/Modules/Mini/Services/PageService.php

@@ -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\GoodsSearch;
 use App\Models\Goods\GoodsSpecMap;
 use App\Models\Goods\Spec;
 use App\Models\Goods\SpecAttr;
@@ -205,10 +206,10 @@ class PageService extends BaseService
 
         })->when($params['keyword'], function (Builder $query) use ($params) {
             $query->where(function (Builder $query) use ($params) {
+                $goodsIdArr = GoodsSearch::where("content", "like", "%{$params['keyword']}%")->get(["goods_id"])->pluck("goods_id")->toArray();
                 $query->where("name", "like", "%" . $params['keyword'] . "%")
-                    ->orWhere("custom", "like", "%" . $params['keyword'] . "%")
-                    ->orWhere("desc_html", "like", "%" . $params['keyword'] . "%")
-                    ->orWhereIn("id", GoodsSpecMap::where("value", "like", "%{$params['keyword']}%")->get(["goods_id"])->pluck("goods_id")->toArray());
+                    ->orWhereIn("id", $goodsIdArr);
+
             });
         })->orderByDesc("weight")->paginate($pageSize);