海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
4.5 KiB

<?php
namespace App\Exports;
use App\Common\ProductStatus;
use App\Models\Product;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class ProductExport implements FromQuery, WithHeadings, WithColumnFormatting
{
private int $supplier_id;
private string|array|null $_export_;
private string $export_dir;
public function __construct(int $supplier_id, string|array $_export_, string $export_dir)
{
$this->supplier_id = $supplier_id;
$this->_export_ = $_export_;
$this->export_dir = $export_dir;
}
public function query()
{
if ($this->_export_ == 'all') {
return Product::with('category:id,name')->where('supplier_id', $this->supplier_id);
} else if (!empty($this->_export_) && is_array($this->_export_)) {
return Product::with('category:id,name')->where('supplier_id', $this->supplier_id)->whereIn('id', $this->_export_);
} else {
return Product::with('category:id,name')->where('supplier_id', $this->supplier_id)->limit(20);
}
}
public function prepareRows($rows)
{
return $rows->transform(function ($row) {
# 复制图片到导出目录
if (is_array($row['pictures']) && !empty($row['pictures'])) {
foreach ($row['pictures'] as $key => $picture) {
try {
Storage::disk('public')->copy($picture, $this->export_dir . '产品主图/' . $row->id . '/' . ($key + 1) . '.' . pathinfo($picture)['extension']);
} catch (\Exception $exception) {
continue;
}
}
}
return [
'id' => $row->id,
'status' => ProductStatus::array()[$row->status] ?? '',
'category_name' => $row->category?->name ?? '',
'title' => $row->title,
'price' => $row->price,
'original_price' => $row->original_price,
'stock' => $row->stock,
'know' => $row->know,
'content' => $row->content,
'verify_mobile' => $row->verify_mobile,
'diy_form_id' => $row->diy_form_id,
'出发地' => $row->extends['field_0_departure_place'] ?? '',
'出发地经度' => $row->extends['field_0_departure_place_longitude'] ?? '',
'出发地纬度' => $row->extends['field_0_departure_place_latitude'] ?? '',
'目的地' => $row->extends['field_0_destination'] ?? '',
'目的地经度' => $row->extends['field_0_destination_longitude'] ?? '',
'目的地纬度' => $row->extends['field_0_destination_latitude'] ?? '',
'行程起始时间' => $row->extends['field_0_date?->start'] ?? '',
'行程结束时间' => $row->extends['field_0_date?->end'] ?? '',
'酒店名' => $row->extends['field_1_name'] ?? '',
'酒店地址' => $row->extends['field_1_address'] ?? '',
'酒店经度' => $row->extends['field_1_longitude'] ?? '',
'酒店纬度' => $row->extends['field_1_latitude'] ?? '',
'景区名' => $row->extends['field_2_name'] ?? '',
'景区地址' => $row->extends['field_2_address'] ?? '',
'景区经度' => $row->extends['field_2_longitude'] ?? '',
'景区纬度' => $row->extends['field_2_latitude'] ?? '',
'餐厅名' => $row->extends['field_3_name'] ?? '',
'餐厅地址' => $row->extends['field_3_address'] ?? '',
'餐厅经度' => $row->extends['field_3_longitude'] ?? '',
'餐厅纬度' => $row->extends['field_3_latitude'] ?? '',
'交通地址' => $row->extends['field_4_address'] ?? '',
'交通经度' => $row->extends['field_4_longitude'] ?? '',
'交通纬度' => $row->extends['field_4_latitude'] ?? '',
'购物地址' => $row->extends['field_5_address'] ?? '',
'购物经度' => $row->extends['field_5_longitude'] ?? '',
'购物纬度' => $row->extends['field_5_latitude'] ?? '',
];
});
}
public function headings(): array
{
return [
'产品ID', '状态', '分类', '产品标题', '销售价', '市场价', '库存', '旅客须知', '产品详情', '核销手机号', '信息收集表单ID',
'出发地', '出发地经度', '出发地纬度', '目的地', '目的地经度', '目的地纬度', '行程起始时间', '行程结束时间',
'酒店名', '酒店地址', '酒店经度', '酒店纬度', '景区名', '景区地址', '景区经度', '景区纬度',
'餐厅名', '餐厅地址', '餐厅经度', '餐厅纬度', '交通地址', '交通经度', '交通纬度', '购物地址', '购物经度', '购物纬度',
];
}
public function columnFormats(): array
{
for ($i = 65; $i <= 90; $i++) {
$format[chr($i)] = NumberFormat::FORMAT_TEXT;
}
return $format;
}
}