Browse Source

笛卡尔积

kphcdr 1 year ago
parent
commit
3e0ff237b3

+ 4 - 23
app/Modules/Admin/Controllers/Admin/GoodsController.php

@@ -6,7 +6,6 @@ use App\Base\BaseController;
 use App\Models\Goods\Goods;
 use App\Modules\Admin\Services\GoodsService;
 use App\Modules\Admin\Services\SpecService;
-use Faker\Provider\Image;
 
 class GoodsController extends BaseController
 {
@@ -44,30 +43,12 @@ class GoodsController extends BaseController
 
     public function makeSkuTable()
     {
-        $this->valid([
-            "spec_list" => "array",
+        $data = $this->valid([
+            "spec_list" => "required|array",
         ]);
 
-        return $this->ok([
-            "sku_table" => [
-                [
-                    "id" => 0,
-                    "sn" => "规格型号",
-                    "url_3d" => "",
-                    "spec_attr" => [
-                        [
-                            "id" => 1,
-                            "name" => "属性一",
-                        ],
-                        [
-                            "id" => 2,
-                            "name" => "属性二",
-                        ],
-                    ],
-                    "is_use" => 1,
-                ],
-            ],
-        ]);
+        return $this->ok($this->specService->cartesian($data['spec_list']));
+
     }
 
     public function info()

+ 37 - 0
app/Modules/Admin/Services/SpecService.php

@@ -9,6 +9,7 @@ use App\Models\Goods\SpecAttr;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\DB;
+use Nerd\CartesianProduct\CartesianProduct;
 
 class SpecService extends BaseService
 {
@@ -87,4 +88,40 @@ class SpecService extends BaseService
         }
         return true;
     }
+
+    /**
+     * 生成笛卡尔积表格
+     *
+     * @param array $specIdArr
+     */
+    public function cartesian(array $specIdArr)
+    {
+        $specS = Spec::whereIn("id", $specIdArr)->get();
+
+        $attrS = collect();
+        $cartesianProduct = new CartesianProduct();
+        foreach ($specS as $spec) {
+            /** @var Spec $spec */
+            $cartesianProduct->appendSet($spec->attrs->pluck("name")->toArray());
+            $attrS = $attrS->merge($spec->attrs);
+        }
+        $cartesian = $cartesianProduct->compute();
+
+        foreach ($cartesian as $c) {
+
+            $return[] = [
+                "id" => 0,
+                "sn" => "",
+                "url_3d" => "",
+                "is_use" => 1,
+                "spec_attr" => array_map(function ($cname) use ($attrS) {
+                    return [
+                        "id" => $attrS->where("name", $cname)->value("id"),
+                        "name" => $cname,
+                    ];
+                }, $c),
+            ];
+        }
+        return $return;
+    }
 }

+ 2 - 1
composer.json

@@ -9,7 +9,8 @@
         "elfsundae/laravel-hashid": "^1.7",
         "guzzlehttp/guzzle": "^7.2",
         "laravel/framework": "^9.19",
-        "laravel/tinker": "^2.7"
+        "laravel/tinker": "^2.7",
+        "th3n3rd/cartesian-product": "^0.3.0"
     },
     "require-dev": {
         "barryvdh/laravel-ide-helper": "^2.12",

+ 60 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "3045e05484968b64f93c2d8b74a6adb2",
+    "content-hash": "a021d08d1957c8e8852276f05206aa32",
     "packages": [
         {
             "name": "brick/math",
@@ -5995,6 +5995,65 @@
             "time": "2023-01-20T17:44:14+00:00"
         },
         {
+            "name": "th3n3rd/cartesian-product",
+            "version": "v0.3.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/th3n3rd/cartesian-product.git",
+                "reference": "78b22f4feb4ef3efc1e99dfe5d932e17024b035d"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/th3n3rd/cartesian-product/zipball/78b22f4feb4ef3efc1e99dfe5d932e17024b035d",
+                "reference": "78b22f4feb4ef3efc1e99dfe5d932e17024b035d",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.3"
+            },
+            "require-dev": {
+                "devster/ubench": "~1.1",
+                "phpunit/phpunit": "~4.4"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "0.3-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Nerd\\CartesianProduct\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Marco Garofalo",
+                    "email": "marcogarofalo.personal@gmail.com"
+                }
+            ],
+            "description": "Memory efficient Cartesian Product implementation",
+            "keywords": [
+                "cartesian",
+                "product"
+            ],
+            "support": {
+                "issues": "https://github.com/th3n3rd/cartesian-product/issues",
+                "source": "https://github.com/th3n3rd/cartesian-product/tree/master"
+            },
+            "time": "2015-05-30T08:42:25+00:00"
+        },
+        {
             "name": "tijsverkoyen/css-to-inline-styles",
             "version": "2.2.6",
             "source": {