feat: 添加产品兼容性数据导出接口
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 5s
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 5s
This commit is contained in:
@@ -103,7 +103,7 @@ if (!function_exists('xlsx_writer')) {
|
|||||||
* @param string $output 输出方式 php://output 或 文件路径
|
* @param string $output 输出方式 php://output 或 文件路径
|
||||||
* @return \think\Response
|
* @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对象
|
||||||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use app\admin\model\v1\ProductCompatModelsModel;
|
|||||||
use app\admin\model\v1\ProductCompatPartsModel;
|
use app\admin\model\v1\ProductCompatPartsModel;
|
||||||
use app\admin\model\v1\ProductCompatTypesModel;
|
use app\admin\model\v1\ProductCompatTypesModel;
|
||||||
use app\admin\validate\v1\ProductCompatValidate;
|
use app\admin\validate\v1\ProductCompatValidate;
|
||||||
|
use Generator;
|
||||||
|
|
||||||
class ProductCompat
|
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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 产品兼容性数据添加
|
* 产品兼容性数据添加
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -378,6 +378,9 @@ Route::group('v1', function () {
|
|||||||
// 兼容性数据导入
|
// 兼容性数据导入
|
||||||
Route::post('import', 'ProductCompat/import');
|
Route::post('import', 'ProductCompat/import');
|
||||||
|
|
||||||
|
// 兼容性数据导出
|
||||||
|
Route::get('export', 'ProductCompat/export');
|
||||||
|
|
||||||
// 兼容性数据添加
|
// 兼容性数据添加
|
||||||
Route::post('save', 'ProductCompat/save');
|
Route::post('save', 'ProductCompat/save');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user