GoodsService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Base\BaseService;
  4. use App\Models\Goods\Goods;
  5. use App\Models\Goods\GoodsCategoryMap;
  6. use App\Models\Goods\GoodsCustom;
  7. use App\Models\Goods\GoodsSearch;
  8. use App\Models\Goods\GoodsSpecMap;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Support\Arr;
  11. use Illuminate\Support\Facades\DB;
  12. class GoodsService extends BaseService
  13. {
  14. public function paginate($params)
  15. {
  16. $p = Goods::query()->when($params['status'], function (Builder $query) use ($params) {
  17. return $query->where("status", $params['status']);
  18. })->when($params['name'], function (Builder $query) use ($params) {
  19. return $query->where("name", "like", "%{$params['name']}%");
  20. })->when($params['category_id'], function (Builder $query) use ($params) {
  21. $gidArr = GoodsCategoryMap::whereCategoryId($params['category_id'])->groupBy("goods_id")->get(['goods_id'])->toArray();
  22. $query->whereIn("id", $gidArr);
  23. })->paginate($params['page_size']);
  24. return [
  25. "total" => $p->total(),
  26. "page_total" => $p->lastPage(),
  27. "list" => array_map(function (Goods $model) {
  28. return [
  29. "id" => $model->id,
  30. "name" => $model->name,
  31. "en_name" => $model->en_name,
  32. "thumb" => Arr::first($model->image_list),
  33. "weight" => $model->weight,
  34. "status" => $model->status,
  35. "created_at" => $model->created_at->format("Y-m-d H:i:s"),
  36. ];
  37. }, $p->items()),
  38. ];
  39. }
  40. public function store($params)
  41. {
  42. if ($id = Arr::get($params, "id", 0)) {
  43. $goods = Goods::findOrFail($id);
  44. } else {
  45. $goods = new Goods();
  46. }
  47. $specIdArr = array_values(Arr::sort(Arr::get($params, "spec", [])));
  48. $goods = DB::transaction(function () use ($params, $goods, $specIdArr) {
  49. $goods->image_list = Arr::get($params, "image_list", []);
  50. $goods->sn = $params['sn'] ?? "";
  51. $goods->url_3d = $params['url_3d'] ?? "";
  52. $goods->spec = $specIdArr;
  53. $goods->name = Arr::get($params, "name", []);
  54. $goods->en_name = Arr::get($params, "en_name", []);
  55. $goods->weight = Arr::get($params, "weight", 0);
  56. $goods->desc_html = Arr::get($params, "desc_html", "");
  57. $goods->en_desc_html = Arr::get($params, "en_desc_html", "");
  58. $goods->spec_attr_html = Arr::get($params, "spec_attr_html", "");
  59. $goods->en_spec_attr_html = Arr::get($params, "en_spec_attr_html", "");
  60. $goods->en_faq_html = Arr::get($params, "en_faq_html", "");
  61. $goods->faq_html = Arr::get($params, "faq_html", "");
  62. $custom = Arr::get($params, "custom", []);
  63. foreach ($custom as $k => $v) {
  64. $custom[$k]['en_title'] = GoodsCustom::where("id", $v['id'])->value("en_title");
  65. }
  66. $goods->custom = $custom;
  67. $goods->save();
  68. $categoryArr = Arr::get($params, "category", []);
  69. GoodsCategoryMap::where("goods_id", $goods->id)->whereNotIn("category_id", Arr::pluck($categoryArr, "id"))->delete();
  70. foreach ($categoryArr as $c) {
  71. /**
  72. * [
  73. * "id" => 1
  74. * "is_public" => 1
  75. * ]
  76. */
  77. GoodsCategoryMap::updateOrCreate([
  78. "goods_id" => $goods->id,
  79. "category_id" => $c['id'],
  80. ], [
  81. "goods_id" => $goods->id,
  82. "category_id" => $c['id'],
  83. 'is_public' => $c['is_public'],
  84. ]);
  85. }
  86. $specMap = Arr::get($params, "spec_map", []);
  87. $doNotDelIdArr = [];
  88. foreach ($specMap as $c) {
  89. $model = GoodsSpecMap::where("goods_id", $goods->id)->where("spec_id", $c['spec_id'])->first();
  90. if (is_null($model)) {
  91. $model = new GoodsSpecMap();
  92. $model->goods_id = $goods->id;
  93. $model->spec_id = $c['spec_id'];
  94. }
  95. $model->value = $c['value'];
  96. $model->save();
  97. $doNotDelIdArr[] = $model->id;
  98. }
  99. GoodsSpecMap::where("goods_id", $goods->id)->whereNotIn("id", $doNotDelIdArr)->delete();
  100. $this->createIndex($goods);
  101. return $goods;
  102. });
  103. }
  104. public function createIndex(Goods $goods)
  105. {
  106. GoodsSearch::updateOrCreate([
  107. "goods_id" => $goods->id,
  108. ], [
  109. "goods_id" => $goods->id,
  110. "content" => $goods->name . $goods->en_name . implode("", array_map(function ($item) {
  111. return implode("", Arr::pluck(Arr::get($item, "attr"), "value"));
  112. }, $goods->custom)) . strip_tags($goods->desc_html) . $goods->specMap->map(function (GoodsSpecMap $m) {
  113. return $m->value;
  114. })->implode(""),
  115. ]);
  116. }
  117. public function info($params)
  118. {
  119. $id = $params['id'];
  120. $model = Goods::findOrFail($id);
  121. $return = [
  122. "id" => $model->id,
  123. "name" => $model->name,
  124. "en_name" => $model->en_name,
  125. "image_list" => $model->image_list,
  126. "category" => $model->map->map(function (GoodsCategoryMap $map) {
  127. return [
  128. "id" => $map->category->id,
  129. "is_public" => $map->is_public,
  130. "name" => $map->category->name,
  131. "en_name" => $map->category->en_name,
  132. ];
  133. }),
  134. "spec" => $model->spec,
  135. "weight" => $model->weight,
  136. "desc_html" => $model->desc_html,
  137. "en_desc_html" => $model->en_desc_html,
  138. "spec_attr_html" => $model->spec_attr_html,
  139. "en_spec_attr_html" => $model->en_spec_attr_html,
  140. "faq_html" => $model->faq_html,
  141. "en_faq_html" => $model->en_faq_html,
  142. "sn" => $model->sn,
  143. "url_3d" => $model->url_3d,
  144. "spec_map" => $model->specMap->map(function (GoodsSpecMap $map) {
  145. return [
  146. "id" => $map->id,
  147. "spec_id" => $map->spec_id,
  148. "value" => $map->value,
  149. "name" => $map->spec->name,
  150. "en_name" => $map->spec->en_name,
  151. ];
  152. }),
  153. "custom" => $model->custom,
  154. ];
  155. return $return;
  156. }
  157. }