feat: #20220804 新增导出

This commit is contained in:
赵世界 2022-08-04 14:25:12 +08:00
parent 088c41cee9
commit 2dc1308675
3 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace App\Exports;
use App\Models\Log;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use App\Models\GoodsSku;
use Illuminate\Support\Collection;
class GoodsSkusExport implements FromCollection, ShouldAutoSize
{
private $data;
private $type;
public function __construct($type)
{
$this->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];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Goods; namespace App\Http\Controllers\Goods;
use App\Exports\GoodsSkusExport;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\GoodsRequest; use App\Http\Requests\GoodsRequest;
use App\Http\Requests\GoodsSkuRequest; use App\Http\Requests\GoodsSkuRequest;
@ -340,4 +341,11 @@ class GoodsSkusController extends Controller
return response($this->res, $this->res['httpCode']); 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');
}
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use App\Http\Controllers\Goods\GoodsSkusController;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Web Routes | Web Routes
@ -26,3 +28,5 @@ Route::get('/login', function () {
Route::get('/register', function () { Route::get('/register', function () {
return view('welcome'); return view('welcome');
})->name('register'); })->name('register');
Route::get('goods_skus/export', [GoodsSkusController::class, 'export'])->name('goods_skus.export');