GoodsService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\GoodsSku;
  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. 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. });
  101. }
  102. public function info($params)
  103. {
  104. $id = $params['id'];
  105. $model = Goods::findOrFail($id);
  106. $return = [
  107. "id" => $model->id,
  108. "name" => $model->name,
  109. "en_name" => $model->en_name,
  110. "image_list" => $model->image_list,
  111. "category" => $model->map->map(function (GoodsCategoryMap $map) {
  112. return [
  113. "id" => $map->category->id,
  114. "is_public" => $map->is_public,
  115. "name" => $map->category->name,
  116. "en_name" => $map->category->en_name,
  117. ];
  118. }),
  119. "spec" => $model->spec,
  120. "weight" => $model->weight,
  121. "desc_html" => $model->desc_html,
  122. "en_desc_html" => $model->en_desc_html,
  123. "spec_attr_html" => $model->spec_attr_html,
  124. "en_spec_attr_html" => $model->en_spec_attr_html,
  125. "faq_html" => $model->faq_html,
  126. "en_faq_html" => $model->en_faq_html,
  127. "sn" => $model->sn,
  128. "url_3d" => $model->url_3d,
  129. "spec_map" => $model->specMap->map(function (GoodsSpecMap $map) {
  130. return [
  131. "id" => $map->id,
  132. "spec_id" => $map->spec_id,
  133. "value" => $map->value,
  134. "name" => $map->spec->name,
  135. "en_name" => $map->spec->en_name,
  136. ];
  137. }),
  138. "custom" => $model->custom,
  139. ];
  140. return $return;
  141. }
  142. }