From 2dc13086751143861460f057189d8df7cd0a716b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Thu, 4 Aug 2022 14:25:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#20220804=20=E6=96=B0=E5=A2=9E=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exports/GoodsSkusExport.php | 75 +++++++++++++++++++ .../Controllers/Goods/GoodsSkusController.php | 8 ++ routes/web.php | 4 + 3 files changed, 87 insertions(+) create mode 100644 app/Exports/GoodsSkusExport.php diff --git a/app/Exports/GoodsSkusExport.php b/app/Exports/GoodsSkusExport.php new file mode 100644 index 0000000..d9316a8 --- /dev/null +++ b/app/Exports/GoodsSkusExport.php @@ -0,0 +1,75 @@ +type = $type; + $this->data = $this->createData(); + } + + /** + * @return \Illuminate\Support\Collection + */ + public function collection() + { + return new Collection($this->data); + } + + private function createData() + { + $headTitle = [ + '商品编码', + '商品名称', + '商品种类', + '商品品牌', + '规格编码', + '规格名称', + '成本', + '库存', + ]; + $inventoryTime = strtotime(date('Y-m-d 07:00:00')); + $ids = Log::query()->where('target_type', 'sku') + ->where('target_field', $this->type) + ->where('created_at', '>', $inventoryTime) + ->pluck('sku_id') + ->toArray(); + $data = GoodsSku::query() + ->when($ids, function ($query, $ids) { + return $query->whereIn('id', $ids); + }) + ->with(['goods' => function ($query) { + $query->with(['type:id,name', 'brand:id,name']); + }]) + ->get() + ->toArray(); + if (empty($data)) { + return [$headTitle]; + } + $bodyData = []; + foreach ($data as $item) { + $arr[0] = $item['goods']['goods_code']; + $arr[1] = $item['goods']['title']; + $arr[2] = $item['goods']['type']['name']; + $arr[3] = $item['goods']['brand']['name']; + $arr[4] = $item['goods']['sku_code']; + $arr[5] = $item['goods']['title']; + $arr[6] = $item['goods']['cost']; + $arr[7] = $item['goods']['stock']; + $bodyData[] = $arr; + } + unset($arr); + return [$headTitle, $bodyData]; + } +} diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index 044d95e..97053f1 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Goods; +use App\Exports\GoodsSkusExport; use App\Http\Controllers\Controller; use App\Http\Requests\GoodsRequest; use App\Http\Requests\GoodsSkuRequest; @@ -340,4 +341,11 @@ class GoodsSkusController extends Controller return response($this->res, $this->res['httpCode']); } + + public function export(Request $request) + { + $type = $request->get('exportType'); + ob_end_clean(); + return Excel::download(new GoodsSkusExport($type), $type, '.xlsx'); + } } diff --git a/routes/web.php b/routes/web.php index 4f8529e..0af6dd8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,7 @@ name('register'); + +Route::get('goods_skus/export', [GoodsSkusController::class, 'export'])->name('goods_skus.export');