客户创建时间脚本
This commit is contained in:
@@ -58,10 +58,10 @@ class ImportCustomerCommand extends Command
|
||||
|
||||
// 获取所有国家数据,用于匹配
|
||||
$output->writeln("正在获取国家数据...");
|
||||
$countries = Country::select('id', 'cn_addr', 'en_addr')->get()->keyBy(function($item) {
|
||||
return strtolower(trim($item->cn_addr));
|
||||
});
|
||||
$output->writeln("获取到 " . $countries->count() . " 个国家数据");
|
||||
// $countries = Country::select('id', 'cn_addr', 'en_addr')->get()->keyBy(function($item) {
|
||||
// return strtolower(trim($item->cn_addr));
|
||||
// });
|
||||
// $output->writeln("获取到 " . $countries->count() . " 个国家数据");
|
||||
|
||||
$successCount = 0;
|
||||
$errorCount = 0;
|
||||
@@ -76,36 +76,38 @@ class ImportCustomerCommand extends Command
|
||||
// 读取各列数据 - 使用坐标方法
|
||||
try {
|
||||
$countryName = trim($worksheet->getCell('B' . $row)->getValue() ?? '');
|
||||
$customerName = trim($worksheet->getCell('C' . $row)->getValue() ?? '');
|
||||
$contacts = trim($worksheet->getCell('A' . $row)->getValue() ?? '');
|
||||
$email = trim($worksheet->getCell('C' . $row)->getValue() ?? '');
|
||||
$phone = trim($worksheet->getCell('D' . $row)->getValue() ?? '');
|
||||
$inquiryProduct = trim($worksheet->getCell('E' . $row)->getValue() ?? '');
|
||||
} catch (\Exception $cellError) {
|
||||
$output->writeln("<error>读取第{$row}行单元格时出错: " . $cellError->getMessage() . "</error>");
|
||||
continue;
|
||||
}
|
||||
|
||||
$output->writeln("读取数据: 国家={$countryName}, 客户={$customerName}, 产品={$inquiryProduct}");
|
||||
$output->writeln("读取数据: 国家={$countryName}, 客户={$contacts}, 产品={$inquiryProduct}");
|
||||
|
||||
// 跳过空行
|
||||
if (empty($customerName)) {
|
||||
continue;
|
||||
}
|
||||
// if (empty($contacts)) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// 查找国家ID
|
||||
$countryId = 0;
|
||||
if (!empty($countryName)) {
|
||||
$countryKey = strtolower($countryName);
|
||||
if ($countries->has($countryKey)) {
|
||||
$countryId = $countries->get($countryKey)->id;
|
||||
} else {
|
||||
// 尝试英文名称匹配
|
||||
$countryByEn = $countries->first(function($item) use ($countryName) {
|
||||
return strtolower(trim($item->en_addr)) === strtolower($countryName);
|
||||
});
|
||||
if ($countryByEn) {
|
||||
$countryId = $countryByEn->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (!empty($countryName)) {
|
||||
// $countryKey = strtolower($countryName);
|
||||
// if ($countries->has($countryKey)) {
|
||||
// $countryId = $countries->get($countryKey)->id;
|
||||
// } else {
|
||||
// // 尝试英文名称匹配
|
||||
// $countryByEn = $countries->first(function($item) use ($countryName) {
|
||||
// return strtolower(trim($item->en_addr)) === strtolower($countryName);
|
||||
// });
|
||||
// if ($countryByEn) {
|
||||
// $countryId = $countryByEn->id;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// 检查是否已存在相同客户名
|
||||
// $existingCustomer = Customer::where('contacts', $customerName)->first();
|
||||
@@ -115,24 +117,33 @@ class ImportCustomerCommand extends Command
|
||||
// }
|
||||
|
||||
// 创建客户记录
|
||||
$customerData = [
|
||||
'user_id' => $userId,
|
||||
'corp_abbr' => $corpAbbr,
|
||||
'channel_id' => $channelId,
|
||||
'country_id' => $countryId,
|
||||
'contacts' => $customerName,
|
||||
'desc_collect' => $inquiryProduct,
|
||||
'grade' => 0, // 默认等级
|
||||
'magnitude' => 1, // 默认商机
|
||||
'type' => 1, // 默认类型
|
||||
'sex' => 0, // 默认性别
|
||||
'enter_user_id' => $userId,
|
||||
// $customerData = [
|
||||
// 'user_id' => $userId,
|
||||
// 'corp_abbr' => $corpAbbr,
|
||||
// 'channel_id' => $channelId,
|
||||
// 'country_id' => $countryId,
|
||||
// 'contacts' => $customerName,
|
||||
// 'email' => $email,
|
||||
// 'phone' => $phone,
|
||||
// 'desc_collect' => $inquiryProduct,
|
||||
// 'grade' => 0, // 默认等级
|
||||
// 'magnitude' => 1, // 默认商机
|
||||
// 'type' => 1, // 默认类型
|
||||
// 'sex' => 0, // 默认性别
|
||||
// 'enter_user_id' => $userId,
|
||||
// ];
|
||||
$where = [
|
||||
['id', '<', 28110],
|
||||
['id', '>', 25714],
|
||||
];
|
||||
|
||||
$customer = Customer::create($customerData);
|
||||
$customer = Customer::where('contacts',$email)->where($where)->first();
|
||||
$customer->email = $email;
|
||||
$customer->phone = $phone;
|
||||
$customer->contacts = $contacts;
|
||||
$customer->save();
|
||||
$successCount++;
|
||||
|
||||
$output->writeln("成功导入客户: {$customerName} (ID: {$customer->id})");
|
||||
$output->writeln("成功导入客户: {$contacts} (ID: {$customer->id})");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$errorCount++;
|
||||
|
||||
Reference in New Issue
Block a user