Files
fdesk/app/command/InquiryFbLeadCommand.php
T
2025-12-10 12:15:17 +00:00

165 lines
5.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\command;
use app\inquiry\utils\PhoneCountryHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use support\Db;
/**
* 将 fb_leads 数据清洗后导入 inquiry_log
*/
class InquiryFbLeadCommand extends Command
{
protected static $defaultName = 'iq-fb-to';
protected static $defaultDescription = '将 fb_leads 数据清洗后导入 inquiry_log';
// 这些常量根据你实际业务改
protected const DEFAULT_ENTER_USER_ID = 186;
protected const DEFAULT_CHANNEL = 10;
protected function execute(InputInterface $input, OutputInterface $output): int
{
// 只取未同步的记录
$leads = Db::table('fb_leads')
->where('sync_status', 0)
->get();
if ($leads->isEmpty()) {
$output->writeln('<comment>没有需要同步的 fb_leads(sync_status = 0)</comment>');
return self::SUCCESS;
}
$now = time();
Db::transaction(function () use ($leads, $now, $output) {
$insert = [];
$ids = [];
foreach ($leads as $lead) {
$leadArr = (array)$lead;
$insert[] = $this->mapLeadToInquiry($leadArr, $now);
// 假设 fb_leads 的主键字段名为 id
$ids[] = $leadArr['id'];
}
if ($insert) {
// 1) 写入 inquiry_log
Db::table('inquiry_log')->insert($insert);
// 2) 回写 fb_leads 的同步状态
Db::table('fb_leads')
->whereIn('id', $ids)
->update([
'sync_status' => 1,
'updated_at' => $now,
]);
$output->writeln('<info>已导入 ' . count($insert) . ' 条数据到 inquiry_log,并更新 fb_leads.sync_status = 1</info>');
}
});
return self::SUCCESS;
}
/**
* 将一条 fb_leads 记录映射为 inquiry_log 记录
*/
protected function mapLeadToInquiry(array $lead, int $now): array
{
// 联系方式
$email = (string)($lead['email'] ?? '');
$tel = (string)($lead['phone'] ?? '');
$wa = $tel; // 先用电话号码当 WhatsApp
// 根据电话或 WhatsApp 区号识别国家
$countryId = PhoneCountryHelper::detectCountryId($tel ?: $wa);
// 所有默认值按你的要求设置
$data = [
'order_no' => $this->orderNum(),
'enter_user_id' => static::DEFAULT_ENTER_USER_ID,
'recipient' => 0,
'quality' => 2,
'channel' => static::DEFAULT_CHANNEL,
'email' => $email,
'tel' => $tel,
'whatsapp' => $wa,
'country' => $countryId ?: 0,
'contacts' => (string)($lead['full_name'] ?? ''),
'content' => $this->buildContentHtml($lead),
'status' => 0,
'r_time' => 0,
't_time' => 0,
'sync_crm' => 0,
'created_at' => $now,
'updated_at' => $now,
];
return $data;
}
/**
* 生成订单号
*/
protected function orderNum(): string
{
return 'SN' . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}
/**
* 根据 fb_leads 的字段拼出富文本内容
*/
protected function buildContentHtml(array $lead): string
{
$e = function ($v) {
return htmlspecialchars((string)$v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
};
$lines = [];
if (!empty($lead['car_model'])) {
$lines[] = '<p><strong>意向车型:</strong>' . $e($lead['car_model']) . '</p>';
}
if (!empty($lead['car_quantity']) || !empty($lead['car_quantity_text'])) {
$qty = $lead['car_quantity'] ?? $lead['car_quantity_text'] ?? '';
$lines[] = '<p><strong>意向数量:</strong>' . $e($qty) . '</p>';
}
if (!empty($lead['favorite_color'])) {
$lines[] = '<p><strong>意向颜色:</strong>' . $e($lead['favorite_color']) . '</p>';
}
if (!empty($lead['usage_type'])) {
$lines[] = '<p><strong>用途:</strong>' . $e($lead['usage_type']) . '</p>';
}
if (!empty($lead['seat_preference'])) {
$lines[] = '<p><strong>座位数偏好:</strong>' . $e($lead['seat_preference']) . '</p>';
}
$lines[] = '<hr />';
$lines[] = '<p><strong>广告与表单信息:</strong></p>';
$lines[] = '<p>平台:' . $e($lead['platform'] ?? '') . '</p>';
$lines[] = '<p>Campaign:' . $e($lead['campaign_name'] ?? '') . ' (#' . $e($lead['campaign_id'] ?? '') . ')</p>';
$lines[] = '<p>Adset:' . $e($lead['adset_name'] ?? '') . ' (#' . $e($lead['adset_id'] ?? '') . ')</p>';
$lines[] = '<p>Ad:' . $e($lead['ad_name'] ?? '') . ' (#' . $e($lead['ad_id'] ?? '') . ')</p>';
$lines[] = '<p>Form:' . $e($lead['form_name'] ?? '') . ' (#' . $e($lead['form_id'] ?? '') . ')</p>';
$lines[] = '<hr />';
$lines[] = '<p><strong>Meta 信息:</strong></p>';
$lines[] = '<p>Lead ID:' . $e($lead['fb_lead_id'] ?? '') . '</p>';
$lines[] = '<p>FB 创建时间原始值:' . $e($lead['fb_created_time'] ?? '') . '</p>';
if (!empty($lead['inbox_url'])) {
$url = $e($lead['inbox_url']);
$lines[] = '<p>Inbox 链接:<a href="' . $url . '" target="_blank">' . $url . '</a></p>';
}
return implode("\n", $lines);
}
}