254 lines
8.7 KiB
PHP
254 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace BYDAuto\Commands;
|
|
|
|
use App\Mail\HelloMail;
|
|
use BYDAuto\Models\Activity;
|
|
use BYDAuto\Models\Contact;
|
|
use BYDAuto\Models\Testdrive;
|
|
use BYDAuto\Notifications\TestdirveNotification;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class BYDAutoCommand extends Command
|
|
{
|
|
protected $signature = 'bydauto {action?}';
|
|
|
|
protected $description = 'BYDAuto Command';
|
|
|
|
public function handle()
|
|
{
|
|
$action = $this->argument('action');
|
|
$method = "action_{$action}";
|
|
if (method_exists($this, $method)) {
|
|
$this->{$method}();
|
|
} else {
|
|
$this->error("不支持的操作 {$action}");
|
|
}
|
|
}
|
|
|
|
public function action_data_fix()
|
|
{
|
|
$source = ['alipromote:108600010', 'alipromote:108600017'];
|
|
$items = Testdrive::query()->whereIn('source', $source)->get();
|
|
foreach ($items as $item) {
|
|
$propertyList = $item->rawdata['property_list'];
|
|
$name = collect($propertyList)->firstWhere('key', '姓名');
|
|
$item->name = $name['value'] ?? '';
|
|
$mobile = collect($propertyList)->firstWhere('key', '手机号');
|
|
$item->mobile = $mobile['value'] ?? '';
|
|
$item->save();
|
|
}
|
|
$this->info('Data fix done');
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
public function action_mail_test()
|
|
{
|
|
$users = Contact::query()->get();
|
|
foreach ($users as $user) {
|
|
$message = new HelloMail(name: $user->remark);
|
|
Mail::to($user->email)->send($message);
|
|
}
|
|
}
|
|
|
|
public function action_init()
|
|
{
|
|
$this->call('db:wipe');
|
|
$this->call('migrate');
|
|
}
|
|
|
|
// 漏发试驾数据
|
|
// public function action_push_missing()
|
|
// {
|
|
// $items = Testdrive::query()
|
|
// ->where('date_updated', '>', '2024-11-21 09:06:00')
|
|
// ->get();
|
|
// $contacts = Contact::query()
|
|
// ->where('status', Contact::STATUS_PUBLISHED)
|
|
// ->orderBy('id')
|
|
// ->get();
|
|
// $count = $contacts->count();
|
|
// $offset = cache()->get('push2contact:offset', 0);
|
|
// $sendList = [];
|
|
// foreach ($items as $item) {
|
|
// if ($this->isDirtyName($item->name)) {
|
|
// info("试驾数据 {$item->id} 姓名异常 {$item->name}");
|
|
// $this->info("试驾数据 {$item->id} 姓名异常 {$item->name}");
|
|
// $item->update(['status' => Testdrive::STATUS_REJECTED]);
|
|
// continue;
|
|
// }
|
|
// $contact = $contacts[$offset];
|
|
// $item->update(['status' => Testdrive::STATUS_PUBLISHED, 'contact_id' => $contact->id, 'contact_info' => $contact->toArray()]);
|
|
// if (!isset($sendList[$contact->id])) {
|
|
// $sendList[$contact->id] = [$item];
|
|
// } else {
|
|
// $sendList[$contact->id][] = $item;
|
|
// }
|
|
// $offset = ($offset + 1) % $count;
|
|
// }
|
|
// cache()->forever('push2contact:offset', $offset);
|
|
// // 开始发送
|
|
// foreach ($sendList as $contactId => $items) {
|
|
// $contact = $contacts->firstWhere('id', $contactId);
|
|
// $this->info("发送试驾数据给联系人 {$contact->id} 总数 " . count($items));
|
|
// info("发送试驾数据给联系人 {$contact->id} 总数 " . count($items));
|
|
// $contact->notify(new TestdirveNotification($items));
|
|
// }
|
|
// return Command::SUCCESS;
|
|
// }
|
|
|
|
// 同步试驾数据给到经销商联系人 按顺序每个联系人分配一个试驾数据
|
|
public function action_push2contact()
|
|
{
|
|
// 所有已发送的试驾数据
|
|
$sendItems = Testdrive::query()
|
|
->where('status', Testdrive::STATUS_PUBLISHED)
|
|
->pluck('mobile')
|
|
->toArray();
|
|
|
|
// // 需要发送的试驾数据
|
|
// $items = Testdrive::query()
|
|
// ->whereNotNull('source')
|
|
// ->where('status', Testdrive::STATUS_DRAFT)
|
|
// ->orderBy('id')
|
|
// ->get();
|
|
|
|
$items = collect();
|
|
$num = $this->ask('ID 正序取出', 100);
|
|
$items1 = Testdrive::query()
|
|
->whereNotNull('source')
|
|
->where('status', Testdrive::STATUS_DRAFT)
|
|
->orderBy('id')
|
|
->take($num)
|
|
->get();
|
|
|
|
$num = $this->ask('ID 倒叙取出', 100);
|
|
$items2 = Testdrive::query()
|
|
->whereNotNull('source')
|
|
->where('status', Testdrive::STATUS_DRAFT)
|
|
->orderBy('id', 'desc')
|
|
->take($num)
|
|
->get();
|
|
|
|
$items = $items->merge($items1)->merge($items2);
|
|
|
|
$rawTotal = $items->count();
|
|
// 根据手机号码去除重
|
|
$items = $items->unique('mobile');
|
|
// 根据已发送的手机号码去重
|
|
$items = $items->filter(fn($item) => !in_array($item->mobile, $sendItems));
|
|
|
|
$confirm = $this->confirm("共有 {$rawTotal} 条数据, 去重后 {$items->count()} 条数据,是否继续?");
|
|
if (!$confirm) return Command::SUCCESS;
|
|
|
|
// 联系人
|
|
$contacts = Contact::query()->where('status', Contact::STATUS_PUBLISHED)->orderBy('id')->get();
|
|
|
|
$count = $contacts->count();
|
|
$offset = cache()->get('push2contact:offset', 0);
|
|
|
|
$sendList = [];
|
|
|
|
// 分配数据
|
|
foreach ($items as $item) {
|
|
if ($this->isDirtyName($item->name)) {
|
|
info("试驾数据 {$item->id} 姓名异常 {$item->name}");
|
|
$this->info("试驾数据 {$item->id} 姓名异常 {$item->name}");
|
|
$item->update(['status' => Testdrive::STATUS_REJECTED]);
|
|
continue;
|
|
}
|
|
|
|
$contact = $contacts[$offset];
|
|
|
|
$item->update([
|
|
'status' => Testdrive::STATUS_PUBLISHED,
|
|
'contact_id' => $contact->id,
|
|
'contact_info' => $contact->toArray(),
|
|
]);
|
|
|
|
if (!isset($sendList[$contact->id])) {
|
|
$sendList[$contact->id] = [$item];
|
|
} else {
|
|
$sendList[$contact->id][] = $item;
|
|
}
|
|
|
|
Activity::create([
|
|
'name' => '试驾通知',
|
|
'content' => ['testdrive' => $item->id, 'contact' => $contact->id],
|
|
'related_id' => $contact->id,
|
|
'related_type' => Contact::class,
|
|
]);
|
|
$offset = ($offset + 1) % $count;
|
|
}
|
|
cache()->forever('push2contact:offset', $offset);
|
|
|
|
// 开始发送
|
|
foreach ($sendList as $contactId => $items) {
|
|
$contact = $contacts->firstWhere('id', $contactId);
|
|
$this->info("发送试驾数据给联系人 {$contact->id} 总数 " . count($items));
|
|
info("发送试驾数据给联系人 {$contact->id} 总数 " . count($items));
|
|
$contact->notify(new TestdirveNotification($items));
|
|
}
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
public function action_map_activity_testdirve()
|
|
{
|
|
$activities = Activity::query()
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
foreach ($activities as $activity) {
|
|
$testdriveId = $activity->content['testdrive'] ?? null;
|
|
if ($testdriveId) {
|
|
$testdrive = Testdrive::query()->find($testdriveId);
|
|
if ($testdrive) {
|
|
$testdrive->update([
|
|
'contact_id' => $activity->related_id,
|
|
'contact_info' => $activity->related->toArray(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
$this->info('Map activity testdrive done');
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
public function action_testdrive()
|
|
{
|
|
$items = Testdrive::query()->orderBy('id', 'desc')->get();
|
|
$this->table(['id', 'date_created', 'status', 'name', 'mobile'], $items->map(function (Testdrive $v) {
|
|
return [$v->id, $v->date_created, $v->status, $v->name, $v->mobile];
|
|
}));
|
|
}
|
|
|
|
public function action_contact()
|
|
{
|
|
$items = Contact::query()->orderBy('id')->get();
|
|
$this->table(['id', 'email', 'remark', 'status'], $items->map(function (Contact $v) {
|
|
return [$v->id, $v->email, $v->remark, $v->status];
|
|
}));
|
|
}
|
|
|
|
public function action_contact_add()
|
|
{
|
|
$data['email'] = $this->ask('Email');
|
|
$data['remark'] = $this->ask('Remark');
|
|
$data['status'] = Contact::STATUS_PUBLISHED;
|
|
|
|
$contact = new Contact($data);
|
|
$contact->save();
|
|
|
|
$this->info('Contact added');
|
|
}
|
|
|
|
protected function isDirtyName($str, $max = 3)
|
|
{
|
|
if (!preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $str)) {
|
|
return false;
|
|
}
|
|
return mb_strlen($str) > $max;
|
|
}
|
|
}
|