$final = CustomerService::filterRegular($excel_array, $field_list);
$total = $final['total'];
$successNum = count($final['final']);
$failNum = count($final['info']);
if ($total == 0) {
return '上传文件内容不能为空';
}
if ($total > 200000) {
return '本次上传名单数量为{$total},单次上传名单数量大于20万,请拆分后导入';
}
$head = ['公司名称', '联系人', '手机号', '客户类型', '所在城市', '所在区域', '责任业务员', '合作类型', '错误信息'];
$fileInfo = self::exportExcel($head, $final['info']);
$jobData = [
'name' => '批量导入客户管理',
'total' => $total,
'success' => $successNum,
'fail' => $failNum,
'down_path' => $fileInfo['path'],
'create_time' => time()
];
Db::table('down')->insert($jobData);
$lists = array_chunk($final['final'], 30000);
foreach ($lists as $list) {
$result = Db::table($table)->limit($limit)->insertAll($list);
if (!$result) {
return '导入失败,请重新尝试';
}
}
public static function filterRegular($excel_array,$field_list){
$isMob = "/^1[345789]{1}\d{9}$/";
$time = time();
$all_data = [];
foreach ($excel_array as $key => $value) {
$data = [];
foreach ($field_list as $field_key => $field_value) {
if (!isset($value[$field_key])) {
throw new RuntimeException('第 ' . ($key + 2) . ' 行第 ' . $field_key . ' 列缺少数据');
}
$data[$field_value] = $value[$field_key];
}
$data['create_time'] = $time;
$data['update_time'] = $time;
$all_data[] = $data;
}
$fileData = [];
$errorInfos = [];
$i = 0;
foreach ($all_data as &$item) {
if (!$item['company_name']) {
$item['reason'] = '缺少公司信息';
$errorInfos[] = $item;
} elseif (!$item['tel']) {
$item['reason'] = '缺少号码信息';
$errorInfos[] = $item;
} elseif (!preg_match($isMob, (string)$item['tel'])) {
$item['reason'] = '该号码不正确,请修改后上传';
$errorInfos[] = $item;
} else {
$fileData[] = $item;
}
$i++;
}
$selfFilter = self::checkUnique($fileData, 'tel');
$selfUnique = $selfFilter['unique'];
$selfRepeat = $selfFilter['repeat'];
$errorInfos = array_merge($errorInfos, $selfRepeat);
return ['final' => $selfUnique, 'info' => $errorInfos, 'total' => $i];
}
public static function checkUnique($arr, $key): array
{
$key_array = [];
$temp_array = $arr;
$repeat_array = [];
foreach ($arr as $value) {
if (!isset($temp_array[$value[$key]])) {
$key_array[] = $value;
$temp_array[$value[$key]] = $value[$key];
} else {
$value['reason'] = '该号码在excel表中重复';
$repeat_array[] = $value;
}
}
return ['unique' => $key_array, 'repeat' => $repeat_array];
}
public static function exportExcel($head, $body, string $title = 'Sheet1')
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->setActiveSheetIndex(0);
$char_index = range('A', 'Z');
$a = 'A';
foreach ($char_index as $item) {
$char_index[] = $a . $item;
}
foreach ($head as $key => $val) {
$sheet->setCellValue($char_index[$key] . '1', $val);
}
foreach ($body as $key => $val) {
unset($val['create_time']);
unset($val['update_time']);
$row = $key + 2;
$col = 0;
foreach ($val as $v) {
$sheet->setCellValue($char_index[$col] . $row, $v);
$col++;
}
}
$spreadsheet->getActiveSheet()->setTitle($title);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . '批量导入客户管理' . '.xlsx"');
header('Cache-Control: max-age=0');
$dir = 'public/uploads/error/' . date('Ymd');
$filePath = $dir . DIRECTORY_SEPARATOR . uniqid(time(), true) . '.xlsx';
$savePath = app()->getRootPath() . $filePath;
$objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');
$objWriter->save($savePath);
return ['path' => substr($filePath,6)];
}