feat: 新增产品数据导出接口
This commit is contained in:
@@ -7,6 +7,8 @@ use app\admin\model\v1\ProductModel;
|
|||||||
use app\admin\model\v1\ProductParamsModel;
|
use app\admin\model\v1\ProductParamsModel;
|
||||||
use app\admin\model\v1\ProductRelatedModel;
|
use app\admin\model\v1\ProductRelatedModel;
|
||||||
use app\admin\validate\v1\ProductValidate;
|
use app\admin\validate\v1\ProductValidate;
|
||||||
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
|
||||||
class Product
|
class Product
|
||||||
{
|
{
|
||||||
@@ -214,4 +216,166 @@ class Product
|
|||||||
|
|
||||||
return success('操作成功');
|
return success('操作成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 导出
|
||||||
|
public function export()
|
||||||
|
{
|
||||||
|
$schema = [
|
||||||
|
'id' => 'ID',
|
||||||
|
'category_name' => '分类名称',
|
||||||
|
'spu' => '规格型号',
|
||||||
|
'name' => '产品名称',
|
||||||
|
'short_name' => '简称(产品副标题)',
|
||||||
|
'cover_image' => '产品封面图片',
|
||||||
|
'desc' => '描述',
|
||||||
|
'video_img' => '视频封面图片',
|
||||||
|
'video_url' => '视频地址',
|
||||||
|
'is_sale' => '是否在售',
|
||||||
|
'is_new' => '是否新品',
|
||||||
|
'is_hot' => '是否热销',
|
||||||
|
'is_show' => '上下架状态',
|
||||||
|
'stock_qty' => '库存数量',
|
||||||
|
'sort' => '排序值',
|
||||||
|
'detail' => '产品详情',
|
||||||
|
'params' => '产品参数',
|
||||||
|
'related' => '关联产品',
|
||||||
|
'status' => '状态',
|
||||||
|
'seo_title' => 'seo标题',
|
||||||
|
'seo_keywords' => 'seo关键词',
|
||||||
|
'seo_desc' => 'seo描述',
|
||||||
|
'created_at' => '添加时间',
|
||||||
|
'updated_at' => '最后更新时间'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 获取导出数据
|
||||||
|
$data = $this->getExportProductData();
|
||||||
|
|
||||||
|
// 获取Spreadsheet对象
|
||||||
|
$spreadsheet = new Spreadsheet();
|
||||||
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
|
|
||||||
|
// 写入表头
|
||||||
|
$title = array_values($schema);
|
||||||
|
$title_col = 'A';
|
||||||
|
foreach ($title as $value) {
|
||||||
|
// 单元格内容写入
|
||||||
|
$sheet->setCellValue($title_col . '1', $value);
|
||||||
|
$title_col++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入数据
|
||||||
|
$row = 2;
|
||||||
|
$keys = array_keys($schema);
|
||||||
|
foreach ($data as $item) {
|
||||||
|
$data_col = 'A';
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$sheet->setCellValue($data_col . $row, $item[$key]??'');
|
||||||
|
$data_col++;
|
||||||
|
}
|
||||||
|
$row++;
|
||||||
|
}
|
||||||
|
|
||||||
|
flush();
|
||||||
|
ob_flush();
|
||||||
|
$filename = date('YmdHms');
|
||||||
|
header('Access-Control-Expose-Headers: Content-Disposition');
|
||||||
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; Charset=UTF-8');
|
||||||
|
header('Content-Disposition: attachment;filename=' . $filename . '.xlsx');
|
||||||
|
header('Cache-Control: max-age=0');
|
||||||
|
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
||||||
|
$writer->save('php://output');
|
||||||
|
}
|
||||||
|
// 获取产品导出数据
|
||||||
|
private function getExportProductData()
|
||||||
|
{
|
||||||
|
$server = request()->server();
|
||||||
|
$image_host = $server['REQUEST_SCHEME'] . "://" . $server['SERVER_NAME'] . config('filesystem.disks.public.url') . '/';
|
||||||
|
$param = request()->param([
|
||||||
|
'name',
|
||||||
|
'spu',
|
||||||
|
'category_id',
|
||||||
|
'created_at',
|
||||||
|
'is_show',
|
||||||
|
]);
|
||||||
|
$products = ProductModel::field([
|
||||||
|
'id',
|
||||||
|
'category_id',
|
||||||
|
'spu',
|
||||||
|
'name',
|
||||||
|
'short_name',
|
||||||
|
'CONCAT("' . $image_host . '", `cover_image`)' => 'cover_image',
|
||||||
|
'desc',
|
||||||
|
'CONCAT("' . $image_host . '", `video_img`)' => 'video_img',
|
||||||
|
'CONCAT("' . $image_host . '", `video_url`)' => 'video_url',
|
||||||
|
'CASE WHEN is_new = 1 THEN "是" ELSE "否" END' => 'is_new',
|
||||||
|
'CASE WHEN is_hot = 1 THEN "是" ELSE "否" END' => 'is_hot',
|
||||||
|
'CASE WHEN is_sale = 1 THEN "是" ELSE "否" END' => 'is_sale',
|
||||||
|
'CASE WHEN is_show = 1 THEN "上架" ELSE "下架" END' => 'is_show',
|
||||||
|
'stock_qty',
|
||||||
|
'sort',
|
||||||
|
'detail',
|
||||||
|
'CASE WHEN status = 1 THEN "启用" WHEN status = -1 THEN "禁用" END' => 'status',
|
||||||
|
'seo_title',
|
||||||
|
'seo_keywords',
|
||||||
|
'seo_desc',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
])
|
||||||
|
->with('category')
|
||||||
|
->language(request()->lang_id)
|
||||||
|
->withSearch(['name_nullable', 'spu_nullable', 'created_at_nullable'], [
|
||||||
|
'name_nullable' => $param['name']??null,
|
||||||
|
'spu_nullable' => $param['spu']??null,
|
||||||
|
'created_at_nullable' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null,
|
||||||
|
])
|
||||||
|
->categoryNullable($param['category_id']??null)
|
||||||
|
->isShowNullable(isset($param['is_show']) ? (bool)$param['is_show'] : null)
|
||||||
|
->order(['id' => 'asc'])
|
||||||
|
->select()
|
||||||
|
->bindAttr('category', ['category_name' => 'name'])
|
||||||
|
->hidden(['category_id', 'category']);
|
||||||
|
|
||||||
|
if (!$products->isEmpty()) {
|
||||||
|
// 产品参数
|
||||||
|
$product_params = ProductParamsModel::withoutField(['id', 'created_at', 'updated_at'])
|
||||||
|
->productId($products->column('id'))
|
||||||
|
->select();
|
||||||
|
if (!$product_params->isEmpty()) {
|
||||||
|
$params = [];
|
||||||
|
foreach ($product_params as $item) {
|
||||||
|
$params[$item['product_id']][] = $item->name . ':' . $item->value;
|
||||||
|
}
|
||||||
|
$products->each(function($product) use($params) {
|
||||||
|
if (empty($params[$product->id])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$product->params = implode(PHP_EOL, $params[$product->id]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关联产品
|
||||||
|
$product_related = ProductRelatedModel::withoutField(['id', 'created_at', 'updated_at'])
|
||||||
|
->with(['product' => function($query) {
|
||||||
|
$query->field(['id', 'spu']);
|
||||||
|
}])
|
||||||
|
->productId($products->column('id'))
|
||||||
|
->order(['sort' => 'asc'])
|
||||||
|
->select();
|
||||||
|
if (!$product_related->isEmpty()) {
|
||||||
|
$related = [];
|
||||||
|
foreach ($product_related as $item) {
|
||||||
|
$related[$item['product_id']][] = $item->product->spu;
|
||||||
|
}
|
||||||
|
$products->each(function($product) use($related) {
|
||||||
|
if (empty($related[$product->id])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$product->related = implode(',', $related[$product->id]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $products->toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ class ProductParamsModel extends ProductParamsBaseModel
|
|||||||
// 要据产品ID查询
|
// 要据产品ID查询
|
||||||
public function scopeProductId($query, $id)
|
public function scopeProductId($query, $id)
|
||||||
{
|
{
|
||||||
return $query->where('product_id', $id);
|
if (is_array($id)) {
|
||||||
|
$query->where('product_id', 'in', $id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$query->where('product_id', '=', $id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ class ProductRelatedModel extends ProductRelatedBaseModel
|
|||||||
// 根据产品ID查询
|
// 根据产品ID查询
|
||||||
public function scopeProductId($query, $id)
|
public function scopeProductId($query, $id)
|
||||||
{
|
{
|
||||||
$query->where('product_id', $id);
|
if (is_array($id)) {
|
||||||
|
$query->where('product_id', 'in', $id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$query->where('product_id', '=', $id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,6 +127,9 @@ Route::group('v1', function () {
|
|||||||
// 产品删除
|
// 产品删除
|
||||||
Route::delete('delete/:id', 'Product/delete');
|
Route::delete('delete/:id', 'Product/delete');
|
||||||
|
|
||||||
|
// 产品导出
|
||||||
|
Route::get('export', 'Product/export');
|
||||||
|
|
||||||
// 产品属性特征
|
// 产品属性特征
|
||||||
Route::get('attrs', 'ProductAttr/index')->append(['scene' => 'list']);
|
Route::get('attrs', 'ProductAttr/index')->append(['scene' => 'list']);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user