客户创建时间脚本
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\crm\model\Customer;
|
||||
use support\Command;
|
||||
use support\Db;
|
||||
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
// 假设你有对应的模型类
|
||||
// use app\model\CrmCustomer;
|
||||
// use app\model\InquiryLog;
|
||||
|
||||
class CustomerCreatedAtCommand extends SymfonyCommand
|
||||
{
|
||||
protected static $defaultName = 'customer:update-created-at';
|
||||
protected static $defaultDescription = '根据 inquiry_log 记录更新 crm_customer 的 created_at 字段';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setHelp('该命令将查询 crm_customer 表中 sn 不为空的记录,并根据 sn 在 inquiry_log 表中查找对应的 order_no 记录,将 inquiry_log 的 created_at 更新到 crm_customer 的 created_at 字段');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$output->writeln('<info>开始更新 crm_customer 的 created_at 字段...</info>');
|
||||
|
||||
try {
|
||||
// 使用事务确保数据一致性
|
||||
DB::transaction(function () use ($output) {
|
||||
// 查询所有 sn 字段不为空的客户记录
|
||||
$customers = Db::table('crm_customer')
|
||||
->where('sn', '<>', '')
|
||||
->whereNotNull('sn')
|
||||
->get(['id', 'sn']);
|
||||
$output->writeln('<info>'.count($customers).'</info>');
|
||||
|
||||
$updatedCount = 0;
|
||||
|
||||
foreach ($customers as $customer) {
|
||||
// 查找匹配的 inquiry_log 记录
|
||||
$inquiryLog = Db::table('inquiry_log')
|
||||
->where('order_no', $customer->sn)
|
||||
->first(['r_time','created_at']);
|
||||
|
||||
// 如果找到匹配记录,则更新客户的 created_at 字段
|
||||
if ($inquiryLog && isset($inquiryLog->r_time)) {
|
||||
$created_at = $inquiryLog->r_time?:$inquiryLog->created_at;
|
||||
DB::table('crm_customer')
|
||||
->where('id', $customer->id)
|
||||
->update(['created_at' => $created_at]);
|
||||
|
||||
$updatedCount++;
|
||||
$output->writeln("<comment>已更新客户 ID {$customer->id} 的 created_at 为: {$created_at}</comment>");
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln("<info>更新完成,总共更新了 {$updatedCount} 条记录。</info>");
|
||||
});
|
||||
|
||||
return self::SUCCESS;
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<error>执行过程中发生错误: ' . $e->getMessage() . '</error>');
|
||||
return self::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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