海南旅游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.
 
 
 

176 lines
6.0 KiB

<?php
namespace App\Imports;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Facades\Excel;
class ProductImport implements ToCollection
{
private int $supplier_id;
private string $extract_path;
private array $keys = [];
public function __construct(int $supplier_id, $extract_path)
{
$this->supplier_id = $supplier_id;
$this->extract_path = $extract_path;
}
public function collection(Collection $collection)
{
if ($collection->isEmpty()) {
return;
}
//校验数组count()是否正确
if (count($collection[0]) < 35) {
throw new \Exception('Excel产品信息格式不正确');
}
$this->keys = array_flip($collection[0]->toArray());
//去除第一行的标题
unset($collection[0]);
//$collection
$this->createData($collection);
}
private function createData($rows)
{
$keys = $this->keys;
foreach ($rows as $product_index => $row) {
$category = Category::where(['agent_id' => 0, 'name' => trim($row[$keys['分类']])])->first();
if (!$category) continue;
$insert_data = [
'supplier_id' => $this->supplier_id,
'category_id' => $category->id,
'type' => $category->publish_type,
'title' => $row[$keys['产品标题']] ?? '',
'price' => $row[$keys['销售价']] ?? 0,
'original_price' => $row[$keys['市场价']] ?? 0,
'stock' => $row[$keys['库存']] ?? 0,
'know' => $row[$keys['旅客须知']] ?? '',
'content' => $row[$keys['产品详情']] ?? '',
'verify_mobile' => $row[$keys['核销手机号']] ?? '',
'diy_form_id' => $row[$keys['信息收集表单ID']] ?? '',
'pictures' => $this->get_pictures($product_index),
];
# 扩展字段
$insert_data['extends'] = $this->get_extends($category->publish_type, $row);
if ($category->publish_type == 0) {
$insert_data['longitude'] = $insert_data['extends']['field_0_departure_place_longitude'] ?? 0;
$insert_data['latitude'] = $insert_data['extends']['field_0_departure_place_latitude'] ?? 0;
$insert_data['address'] = $insert_data['extends']['field_0_departure_place'] ?? '';
} else {
$insert_data['longitude'] = $insert_data['extends']['field_'.$category->publish_type.'_longitude'] ?? 0;
$insert_data['latitude'] = $insert_data['extends']['field_'.$category->publish_type.'_latitude'] ?? 0;
$insert_data['address'] = $insert_data['extends']['field_'.$category->publish_type.'_address'] ?? '';
}
$product = Product::create($insert_data);
if ($product->id) {
$this->insert_spec($product_index, $product->id);
}
}
}
# 插入规格
private function insert_spec($product_index, $product_id)
{
$spec_file = $this->extract_path . "/产品规格{$product_index}.xlsx";
if (file_exists($spec_file)) {
Excel::import(new ProductSpecImport($product_id), $spec_file);
}
}
# 遍历图片文件,并移动到storage/app/public/supplier/import目录下
private function get_pictures($product_index): array
{
$storage_disk = Storage::disk('public');
$http_path = 'supplier/import/' . $this->supplier_id . '/' . date('Y-m');
if (!$storage_disk->exists($http_path)) {
$storage_disk->makeDirectory($http_path); //Storage的makeDirectory才能递归创建
}
$image_path = $this->extract_path . "/产品主图/$product_index";
chdir($image_path);
$pictures = [];
foreach (glob('*') as $file) {
$ext = strtolower(pathinfo($file)['extension']);
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'jfif'])) {
continue;
}
$filename = '/' . md5_file($file) . '.' . $ext;
$move_file = realpath($file);
# Storage::move只适用于storage/app目录下的文件,且是相对路径
if (rename($move_file, $storage_disk->path($http_path . $filename))) {
$pictures[] = $http_path . $filename;
}
}
return $pictures;
}
# 获取扩展字段
private function get_extends(int $publish_type, $row): array
{
$keys = $this->keys;
return match ($publish_type) {
0 => [
'field_0_departure_place' => $row[$keys['出发地']] ?? '',
'field_0_departure_place_longitude' => $row[$keys['出发地经度']] ?? 0,
'field_0_departure_place_latitude' => $row[$keys['出发地纬度']] ?? 0,
'field_0_destination' => $row[$keys['目的地']] ?? '',
'field_0_destination_longitude' => $row[$keys['目的地经度']] ?? 0,
'field_0_destination_latitude' => $row[$keys['目的地纬度']] ?? 0,
'field_0_date' => [
# Excel日期是从1900-01-01起,PHP日期是1970-01-01起,所以要减去25569天数得到正确日期
'start' => !empty($row[$keys['行程起始时间']]) ? date('Y-m-d', strtotime('+ ' . ($row[$keys['行程起始时间']] - 25569) . 'day', 0)) : '',
'end' => !empty($row[$keys['行程结束时间']]) ? date('Y-m-d', strtotime('+ ' . ($row[$keys['行程结束时间']] - 25569) . 'day', 0)) : '',
],
],
1 => [
'field_1_name' => $row[$keys['酒店名']] ?? '',
'field_1_address' => $row[$keys['酒店地址']] ?? '',
'field_1_longitude' => $row[$keys['酒店经度']] ?? 0,
'field_1_latitude' => $row[$keys['酒店纬度']] ?? 0,
],
2 => [
'field_2_name' => $row[$keys['景区名']] ?? '',
'field_2_address' => $row[$keys['景区地址']] ?? '',
'field_2_longitude' => $row[$keys['景区经度']] ?? 0,
'field_2_latitude' => $row[$keys['景区纬度']] ?? 0,
],
3 => [
'field_3_name' => $row[$keys['餐厅名']] ?? '',
'field_3_address' => $row[$keys['餐厅地址']] ?? '',
'field_3_longitude' => $row[$keys['餐厅经度']] ?? 0,
'field_3_latitude' => $row[$keys['餐厅纬度']] ?? 0,
],
4 => [
'field_4_address' => $row[$keys['交通地址']] ?? '',
'field_4_longitude' => $row[$keys['交通经度']] ?? 0,
'field_4_latitude' => $row[$keys['交通纬度']] ?? 0,
],
5 => [
'field_5_address' => $row[$keys['购物地址']] ?? '',
'field_5_longitude' => $row[$keys['购物经度']] ?? 0,
'field_5_latitude' => $row[$keys['购物纬度']] ?? 0,
],
default => [],
};
}
}