Files
transmit/app/inquery/controller/InqueryController.php
T

61 lines
1.6 KiB
PHP
Raw Normal View History

2025-12-15 02:22:19 +00:00
<?php
namespace app\inquery\controller;
use support\Request;
use support\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;
use app\inquery\utils\FbLeadHeaderMapper;
use support\exception\BusinessException;
class InqueryController
{
public function form(Request $request)
{
$filePath = public_path('excel/byd song plus_Leads_2025-12-04_2025-12-09.xlsx');
$spreadsheet = IOFactory::load($filePath);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray(null, true, true, true);
if (empty($rows)) {
throw new BusinessException('Excel 内容为空');
}
// 取第一行表头
$headerRow = array_shift($rows);
// 实例化映射器
$mapper = new FbLeadHeaderMapper();
// ① 如果有未知表头,会在这里抛出异常并终止处理
$colToField = $mapper->buildColumnFieldMap($headerRow);
// ② 开始处理数据行
$insertData = [];
$now = time();
foreach ($rows as $row) {
$item = [];
foreach ($colToField as $colIndex => $field) {
$item[$field] = $row[$colIndex] ?? null;
}
// 补充你需要的其他字段
$item['source_file'] = basename($filePath);
$item['created_at'] = $now;
$item['updated_at'] = $now;
$insertData[] = $item;
}
// ③ 批量入库(根据你的实际需求做去重等逻辑)
if ($insertData) {
Db::table('fb_leads')->insert($insertData);
}
}
}