kphcdr 1 year ago
parent
commit
40d9734c77
2 changed files with 41 additions and 1 deletions
  1. 36 0
      app/Console/Commands/TestCommand.php
  2. 5 1
      app/Modules/Admin/Services/SpecService.php

+ 36 - 0
app/Console/Commands/TestCommand.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Nerd\CartesianProduct\CartesianProduct;
+
+class TestCommand extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'test';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $cartesianProduct = new CartesianProduct();
+        $cartesianProduct->appendSet(["a"]);
+        $cartesianProduct->appendSet(['b', 'c']);
+        dd($cartesianProduct->compute());
+    }
+}

+ 5 - 1
app/Modules/Admin/Services/SpecService.php

@@ -96,7 +96,7 @@ class SpecService extends BaseService
      */
     public function cartesian(array $specIdArr)
     {
-        $specS = Spec::with("attrs")->whereIn("id", $specIdArr)->get();
+        $specS = Spec::with("attrs")->whereIn("id", $specIdArr)->where("is_custom", 0)->get();
 
         $attrS = collect();
         $cartesianProduct = new CartesianProduct();
@@ -105,7 +105,11 @@ class SpecService extends BaseService
             $cartesianProduct->appendSet($spec->attrs->pluck("name")->toArray());
             $attrS = $attrS->merge($spec->attrs);
         }
+        // TODO 这个第三方库有问题,如果只有一个规格的时候,格式不同要兼容一下
         $cartesian = $cartesianProduct->compute();
+        if ($specS->count() == 1) {
+            $cartesian = [$cartesian];
+        }
 
         foreach ($cartesian as $c) {