SpecService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Base\BaseService;
  4. use App\Exceptions\ClientException;
  5. use App\Models\Goods\Spec;
  6. use App\Models\Goods\SpecAttr;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Facades\DB;
  10. use Nerd\CartesianProduct\CartesianProduct;
  11. class SpecService extends BaseService
  12. {
  13. public function paginate($params)
  14. {
  15. $p = Spec::query()->when($params['name'], function (Builder $query) use ($params) {
  16. return $query->where("name", 'like', "%{$params['name']}%");
  17. })->orderByDesc("id")->paginate($params['page_size']);
  18. return [
  19. "total" => $p->total(),
  20. "page_total" => $p->lastPage(),
  21. "list" => array_map(function (Spec $u) {
  22. return $u->format(Spec::FORMAT_ATTR);
  23. }, $p->items()),
  24. ];
  25. }
  26. public function specStore($params)
  27. {
  28. $id = $params['id'] ?? 0;
  29. if ($id) {
  30. $spec = Spec::find($id);
  31. } else {
  32. $spec = new Spec();
  33. }
  34. $spec->name = Arr::get($params, 'name');
  35. $spec->index_weight = Arr::get($params, 'index_weight');
  36. $spec->category_weight = Arr::get($params, 'category_weight');
  37. $spec->search_weight = Arr::get($params, 'search_weight');
  38. $spec->is_custom = (int)Arr::get($params, 'is_custom');
  39. DB::transaction(function () use ($params, $spec) {
  40. $spec->save();
  41. $attr = Arr::get($params, "attr", []);
  42. $holdIdArr = Arr::pluck($attr, "id");
  43. SpecAttr::query()->where("spec_id", $spec->id)->whereNotIn("id", $holdIdArr)->delete();
  44. foreach ($attr as $k => $v) {
  45. $id = Arr::get($v, "id");
  46. if ($id) {
  47. $m = SpecAttr::whereSpecId($spec->id)->find($id);
  48. if (is_null($m)) {
  49. logger()->error("无法保存这个属性值:" . $v['name'] ?? "");
  50. throw new ClientException("无法保存这个属性值:" . $v['name'] ?? "");
  51. }
  52. } else {
  53. $m = new SpecAttr();
  54. }
  55. $m->spec_id = $spec->id;
  56. $m->name = $v['name'] ?? "";
  57. $m->save();
  58. }
  59. });
  60. return true;
  61. }
  62. public function specInfo($data)
  63. {
  64. $id = $data['id'];
  65. $spec = Spec::findOrFail($id);
  66. return $spec->format(Spec::FORMAT_ATTR);
  67. }
  68. public function specDelete($data)
  69. {
  70. $id = $data['id'] ?? 0;
  71. // @TODO kphcdr 判断是否有商品还在使用这个规则,否则无法删除
  72. $spec = Spec::find($id);
  73. if ($spec) {
  74. SpecAttr::where("spec_id", $spec->id)->delete();
  75. $spec->delete();
  76. }
  77. return true;
  78. }
  79. /**
  80. * 生成笛卡尔积表格
  81. *
  82. * @param array $specIdArr
  83. */
  84. public function cartesian(array $specIdArr)
  85. {
  86. $specS = Spec::with("attrs")->whereIn("id", $specIdArr)->where("is_custom", 0)->get();
  87. if ($specS->isEmpty()) {
  88. return [];
  89. }
  90. $attrS = collect();
  91. $cartesianProduct = new CartesianProduct();
  92. foreach ($specS as $spec) {
  93. /** @var Spec $spec */
  94. $cartesianProduct->appendSet($spec->attrs->pluck("name")->toArray());
  95. $attrS = $attrS->merge($spec->attrs);
  96. }
  97. $cartesian = $cartesianProduct->compute();
  98. foreach ($cartesian as $c) {
  99. if (is_array($c)) {
  100. $specAttr = array_map(function ($cname) use ($attrS) {
  101. /** @var SpecAttr $attr */
  102. $attr = $attrS->where("name", $cname)->first();
  103. return [
  104. "id" => $attr->id,
  105. "name" => $cname,
  106. "spec_name" => optional($attr->spec)->name,
  107. ];
  108. }, $c);
  109. } else {
  110. /** @var SpecAttr $attr */
  111. $attr = $attrS->where("name", $c)->first();
  112. $specAttr = [[
  113. "id" => $attr->id,
  114. "name" => $c,
  115. "spec_name" => optional($attr->spec)->name,
  116. ]];
  117. }
  118. $return[] = [
  119. "id" => 0,
  120. "sn" => "",
  121. "url_3d" => "",
  122. "is_use" => 1,
  123. "spec_attr" => $specAttr,
  124. ];
  125. }
  126. return $return;
  127. }
  128. }