123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace App\Modules\Admin\Services;
- 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\GoodsSpecMap;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- class GoodsService extends BaseService
- {
- public function paginate($params)
- {
- $p = Goods::query()->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;
- }
- }
|