when($params['status'], function (Builder $query) use ($params) { return $query->where("status", $params['status']); })->when($params['name'], function (Builder $query) use ($params) { return $query->where("name", "like", "%{$params['name']}%"); })->when($params['category_id'], function (Builder $query) use ($params) { $gidArr = GoodsCategoryMap::whereCategoryId($params['category_id'])->groupBy("goods_id")->get(['goods_id'])->toArray(); $query->whereIn("id", $gidArr); })->paginate($params['page_size']); return [ "total" => $p->total(), "page_total" => $p->lastPage(), "list" => array_map(function (Goods $model) { return [ "id" => $model->id, "name" => $model->name, "en_name" => $model->en_name, "thumb" => Arr::first($model->image_list), "weight" => $model->weight, "status" => $model->status, "created_at" => $model->created_at->format("Y-m-d H:i:s"), ]; }, $p->items()), ]; } public function store($params) { if ($id = Arr::get($params, "id", 0)) { $goods = Goods::findOrFail($id); } else { $goods = new Goods(); } $specIdArr = array_values(Arr::sort(Arr::get($params, "spec", []))); $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'] ?? ""; $goods->spec = $specIdArr; $goods->name = Arr::get($params, "name", []); $goods->en_name = Arr::get($params, "en_name", []); $goods->weight = Arr::get($params, "weight", 0); $goods->desc_html = Arr::get($params, "desc_html", ""); $goods->en_desc_html = Arr::get($params, "en_desc_html", ""); $goods->spec_attr_html = Arr::get($params, "spec_attr_html", ""); $goods->en_spec_attr_html = Arr::get($params, "en_spec_attr_html", ""); $goods->en_faq_html = Arr::get($params, "en_faq_html", ""); $goods->faq_html = Arr::get($params, "faq_html", ""); $custom = Arr::get($params, "custom", []); foreach ($custom as $k => $v) { $custom[$k]['en_title'] = GoodsCustom::where("id", $v['id'])->value("en_title"); } $goods->custom = $custom; $goods->save(); $categoryArr = Arr::get($params, "category", []); GoodsCategoryMap::where("goods_id", $goods->id)->whereNotIn("category_id", Arr::pluck($categoryArr, "id"))->delete(); foreach ($categoryArr as $c) { /** * [ * "id" => 1 * "is_public" => 1 * ] */ GoodsCategoryMap::updateOrCreate([ "goods_id" => $goods->id, "category_id" => $c['id'], ], [ "goods_id" => $goods->id, "category_id" => $c['id'], 'is_public' => $c['is_public'], ]); } $specMap = Arr::get($params, "spec_map", []); $doNotDelIdArr = []; foreach ($specMap as $c) { $model = GoodsSpecMap::where("goods_id", $goods->id)->where("spec_id", $c['spec_id'])->first(); if (is_null($model)) { $model = new GoodsSpecMap(); $model->goods_id = $goods->id; $model->spec_id = $c['spec_id']; } $model->value = $c['value']; $model->save(); $doNotDelIdArr[] = $model->id; } 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(Arr::get($item, "attr"), "value")); }, $goods->custom)) . strip_tags($goods->desc_html) . $goods->specMap->map(function (GoodsSpecMap $m) { return $m->value; })->implode(""), ]); } public function info($params) { $id = $params['id']; $model = Goods::findOrFail($id); $return = [ "id" => $model->id, "name" => $model->name, "en_name" => $model->en_name, "image_list" => $model->image_list, "category" => $model->map->map(function (GoodsCategoryMap $map) { return [ "id" => $map->category->id, "is_public" => $map->is_public, "name" => $map->category->name, "en_name" => $map->category->en_name, ]; }), "spec" => $model->spec, "weight" => $model->weight, "desc_html" => $model->desc_html, "en_desc_html" => $model->en_desc_html, "spec_attr_html" => $model->spec_attr_html, "en_spec_attr_html" => $model->en_spec_attr_html, "faq_html" => $model->faq_html, "en_faq_html" => $model->en_faq_html, "sn" => $model->sn, "url_3d" => $model->url_3d, "spec_map" => $model->specMap->map(function (GoodsSpecMap $map) { return [ "id" => $map->id, "spec_id" => $map->spec_id, "value" => $map->value, "name" => $map->spec->name, "en_name" => $map->spec->en_name, ]; }), "custom" => $model->custom, ]; return $return; } }