diff --git a/app/admin/common.php b/app/admin/common.php index 0ea57aa7..b0be5f20 100644 --- a/app/admin/common.php +++ b/app/admin/common.php @@ -103,7 +103,7 @@ if (!function_exists('xlsx_writer')) { * @param string $output 输出方式 php://output 或 文件路径 * @return \think\Response */ - function xlsx_writer(array|\think\model\Collection $data, array $schema, string $filename = '', string $output = 'php://output'): \think\Response + function xlsx_writer(array|\think\model\Collection|Generator $data, array $schema, string $filename = '', string $output = 'php://output'): \think\Response { // 获取Spreadsheet对象 $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); diff --git a/app/admin/controller/v1/ProductCompat.php b/app/admin/controller/v1/ProductCompat.php index 86178bfa..c929a56e 100644 --- a/app/admin/controller/v1/ProductCompat.php +++ b/app/admin/controller/v1/ProductCompat.php @@ -8,6 +8,7 @@ use app\admin\model\v1\ProductCompatModelsModel; use app\admin\model\v1\ProductCompatPartsModel; use app\admin\model\v1\ProductCompatTypesModel; use app\admin\validate\v1\ProductCompatValidate; +use Generator; class ProductCompat { @@ -299,6 +300,99 @@ class ProductCompat ); } + /** + * 产品兼容性数据导出 + */ + public function export() + { + $params = request()->param([ + 'compatible_model', + 'model', + 'type_id', + 'part_id', + 'brand_name' + ]); + + $list = ProductCompatModel::withoutField([ + 'language_id', + 'deleted_at' + ]) + ->with([ + 'types' => function ($query) { + $query->field(['id', 'name']); + }, + 'parts' => function ($query) { + $query->field(['id', 'name']); + }, + 'compatible_models' => function ($query) { + $query->field(['compat_id', 'compatible_model']); + } + ]) + ->language(request()->lang_id) + ->where(function ($query) use($params) { + if (!empty($params['type_id'])) { + $query->typeId($params['type_id']); + } + if (!empty($params['part_id'])) { + $query->partId($params['part_id']); + } + }) + ->withSearch(['model', 'brand_name', 'compatible_model'], [ + 'model' => $params['model'] ?? null, + 'brand_name' => $params['brand_name'] ?? null, + 'compatible_model' => $params['compatible_model'] ?? null, + ]) + ->order(['updated_at' => 'desc']) + ->cursor(); + + $schema = [ + 'id' => 'id', + 'type_name' => '产品类型', + 'brand_name' => '品牌', + 'model' => '型号', + 'part_name' => '配件', + 'level_name' => '级别', + 'spec_name' => '规格', + 'series_name' => '系列', + 'capacity' => '容量', + 'frequency' => '频率', + 'compatible_models' => '兼容型号', + 'praise_count' => '点赞数', + 'sort' => '排序', + 'created_at' => '新增时间', + 'updated_at' => '更新时间' + ]; + + return xlsx_writer($this->xlsxRowGenerator($list), $schema, '产品兼容性' . date('YmdHis')); + } + private function xlsxRowGenerator(Generator $list) + { + foreach ($list as $item) { + $compatible_models = []; + foreach ($item['compatible_models'] as $model) { + $compatible_models[] = $model['compatible_model']; + } + + yield [ + 'id' => $item['id'], + 'type_name' => $item['types']['type_name'], + 'brand_name' => $item['brand_name'], + 'model' => $item['model'], + 'part_name' => $item['parts']['part_name'], + 'level_name' => $item['level_name'], + 'spec_name' => $item['spec_name'], + 'series_name' => $item['series_name'], + 'capacity' => $item['capacity'], + 'frequency' => $item['frequency'], + 'compatible_models' => implode(',', $compatible_models), + 'praise_count' => $item['praise_count'], + 'sort' => $item['sort'], + 'created_at' => $item['created_at'], + 'updated_at' => $item['updated_at'] + ]; + } + } + /** * 产品兼容性数据添加 */ diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index f83e6ff3..59dfdddc 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -378,6 +378,9 @@ Route::group('v1', function () { // 兼容性数据导入 Route::post('import', 'ProductCompat/import'); + // 兼容性数据导出 + Route::get('export', 'ProductCompat/export'); + // 兼容性数据添加 Route::post('save', 'ProductCompat/save');