Merge branch 'feature/inquiry'
This commit is contained in:
@@ -0,0 +1,144 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\command;
|
||||||
|
|
||||||
|
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 Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
class UpdateInquiryLogFromCrmCustomerCommand extends SymfonyCommand
|
||||||
|
{
|
||||||
|
protected static $defaultName = 'inquiry:update-from-crm-customer';
|
||||||
|
protected static $defaultDescription = '将crm_customer表的部分字段更新到inquiry_log表';
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->setHelp('该命令将crm_customer表的user_id更新到inquiry_log表的recipient字段,将obtain字段更新到t_time字段。')
|
||||||
|
->addOption('dry-run', null, InputOption::VALUE_NONE, '仅显示将要执行的更新操作,不实际执行')
|
||||||
|
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, '限制处理的记录数量', 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$isDryRun = $input->getOption('dry-run');
|
||||||
|
$limit = (int) $input->getOption('limit');
|
||||||
|
|
||||||
|
if ($isDryRun) {
|
||||||
|
$output->writeln('<info>DRY RUN 模式 - 不会实际执行更新操作</info>');
|
||||||
|
}
|
||||||
|
|
||||||
|
$output->writeln('<info>开始更新 inquiry_log 表...</info>');
|
||||||
|
$output->writeln("<info>处理限制: {$limit} 条记录</info>");
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 使用事务确保数据一致性
|
||||||
|
DB::transaction(function () use ($output, $isDryRun, $limit) {
|
||||||
|
// 查询需要更新的记录
|
||||||
|
$customers = Db::table('crm_customer')
|
||||||
|
->select('id', 'sn', 'user_id', 'obtain','created_at')
|
||||||
|
->whereNotNull('sn')
|
||||||
|
->where('sn', '<>', '')
|
||||||
|
->offset($limit)
|
||||||
|
->limit(4000)
|
||||||
|
->orderBy('id', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$output->writeln("<info>找到 " . count($customers) . " 条客户记录需要处理</info>");
|
||||||
|
|
||||||
|
$updatedCount = 0;
|
||||||
|
$skippedCount = 0;
|
||||||
|
$defaultTimestamp = strtotime('2025-08-26 00:00:00');
|
||||||
|
|
||||||
|
$output->writeln("<info>找到 " . count($customers) . " 条客户记录需要处理</info>");
|
||||||
|
$step = [];
|
||||||
|
$updateId = [];
|
||||||
|
foreach ($customers as $customer) {
|
||||||
|
$output->writeln("<info>处理客户 ID: {$customer->id}, SN: {$customer->sn}, User ID: {$customer->user_id}, obtain: {$customer->obtain}, created_at: {$customer->created_at}</info>");
|
||||||
|
// 查找匹配的 inquiry_log 记录
|
||||||
|
$inquiryLog = Db::table('inquiry_log')
|
||||||
|
->where('order_no', $customer->sn)
|
||||||
|
->first(['id', 'recipient', 't_time']);
|
||||||
|
|
||||||
|
if (!$inquiryLog) {
|
||||||
|
$step[] = $customer->id;
|
||||||
|
$output->writeln("<comment>跳过客户 ID {$customer->id} (sn: {$customer->sn}) - 未找到对应的 inquiry_log 记录</comment>");
|
||||||
|
$skippedCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$output->writeln("<info>$inquiryLog->id</info>");
|
||||||
|
|
||||||
|
// 准备更新数据
|
||||||
|
$updateData = [];
|
||||||
|
|
||||||
|
// 更新 recipient 字段 (user_id)
|
||||||
|
if ($customer->user_id != $inquiryLog->recipient) {
|
||||||
|
$updateData['recipient'] = $customer->user_id;
|
||||||
|
$output->writeln("<comment>客户 ID {$customer->id}: recipient 将从 {$inquiryLog->recipient} 更新为 {$customer->user_id}</comment>");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($customer->obtain)&& !empty($customer->user_id)){
|
||||||
|
if($customer->user_id == $inquiryLog->recipient){
|
||||||
|
$obtain = $inquiryLog->t_time;
|
||||||
|
}else{
|
||||||
|
$obtain = strtotime('2025-08-01 10:00:00');
|
||||||
|
$obtain = $customer->created_at > $obtain?$customer->created_at:$obtain;
|
||||||
|
}
|
||||||
|
Db::table('crm_customer')
|
||||||
|
->where('id', $customer->id)
|
||||||
|
->update(['obtain' => $obtain]);
|
||||||
|
$output->writeln("<info>更新obtain $obtain</info>");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新 t_time 字段 (obtain)
|
||||||
|
$newTTime = $customer->obtain > 0 ? $customer->obtain : $defaultTimestamp;
|
||||||
|
if ($newTTime != $inquiryLog->t_time && !empty($updateData['recipient'])) {
|
||||||
|
$updateData['t_time'] = $newTTime;
|
||||||
|
$timeStr = date('Y-m-d H:i:s', $newTTime);
|
||||||
|
$output->writeln("<comment>客户 ID {$customer->id}: t_time 将从 " . ($inquiryLog->t_time ? date('Y-m-d H:i:s', $inquiryLog->t_time) : 'NULL') . " 更新为 {$timeStr}</comment>");
|
||||||
|
}
|
||||||
|
if($customer->user_id == 0 && $inquiryLog->recipient != 0){
|
||||||
|
$updateData['recipient'] = 0;
|
||||||
|
$updateData['t_time'] = time();
|
||||||
|
$updateData['status'] = 1;
|
||||||
|
$updateData['r_time'] = time();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有需要更新的字段,执行更新
|
||||||
|
if (!empty($updateData)) {
|
||||||
|
$updateId[] = $customer->id;
|
||||||
|
if (!$isDryRun) {
|
||||||
|
Db::table('inquiry_log')
|
||||||
|
->where('id', $inquiryLog->id)
|
||||||
|
->update($updateData);
|
||||||
|
}
|
||||||
|
$updatedCount++;
|
||||||
|
$output->writeln("<info>已更新 inquiry_log ID {$inquiryLog->id}</info>");
|
||||||
|
} else {
|
||||||
|
$output->writeln("<comment>客户 ID {$customer->id} - 无需更新</comment>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$output->writeln("<info>处理完成!</info>");
|
||||||
|
$output->writeln("<info>总记录数: " . count($customers) . "</info>");
|
||||||
|
$updateIdstr = implode(",", $updateId);
|
||||||
|
$output->writeln("<info>更新记录数: {$updatedCount},更新id:$updateIdstr</info>");
|
||||||
|
$stepstr = implode(",", $step);
|
||||||
|
$output->writeln("<info>跳过记录数: {$skippedCount},跳过id: {$stepstr}</info>");
|
||||||
|
|
||||||
|
if ($isDryRun) {
|
||||||
|
$output->writeln("<info>DRY RUN 模式 - 未实际执行任何更新操作</info>");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$output->writeln('<error>执行过程中发生错误: ' . $e->getMessage() . '</error>');
|
||||||
|
$output->writeln('<error>错误位置: ' . $e->getFile() . ':' . $e->getLine() . '</error>');
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -444,6 +444,8 @@ class CustomerController extends BaseController
|
|||||||
'user_his' => $userId,
|
'user_his' => $userId,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
CustomerService::inquiryUpdate($customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//软删除
|
//软删除
|
||||||
@@ -562,13 +564,8 @@ class CustomerController extends BaseController
|
|||||||
'user_his' => 0,
|
'user_his' => 0,
|
||||||
'user_id' => $userId,
|
'user_id' => $userId,
|
||||||
]);
|
]);
|
||||||
if (!empty($customer->sn) && $customer->type==1){
|
|
||||||
$inquiryLog = InquiryLog::where('order_no', $customer->sn)->first();
|
CustomerService::inquiryUpdate($customer, $data['user_id']);
|
||||||
if (!empty($inquiryLog)) {
|
|
||||||
$inquiryLog->recipient = $userId;
|
|
||||||
$inquiryLog->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Db::rollback();
|
Db::rollback();
|
||||||
@@ -609,13 +606,8 @@ class CustomerController extends BaseController
|
|||||||
'user_his' => $item->user_id,
|
'user_his' => $item->user_id,
|
||||||
'user_id' => $user['id'],
|
'user_id' => $user['id'],
|
||||||
]);
|
]);
|
||||||
if (!empty($customer->sn) && $customer->type==1){
|
|
||||||
$inquiryLog = InquiryLog::where('order_no', $customer->sn)->first();
|
CustomerService::inquiryUpdate($customer,$userId);
|
||||||
if (!empty($inquiryLog)) {
|
|
||||||
$inquiryLog->recipient = $userId;
|
|
||||||
$inquiryLog->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$item->restore();
|
$item->restore();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace app\crm\service;
|
|||||||
|
|
||||||
use app\common\model\Corp;
|
use app\common\model\Corp;
|
||||||
use app\common\model\CustomerAudit;
|
use app\common\model\CustomerAudit;
|
||||||
|
use app\common\model\InquiryLog;
|
||||||
use app\common\model\User;
|
use app\common\model\User;
|
||||||
use app\common\traits\crud\CrudServiceTrait;
|
use app\common\traits\crud\CrudServiceTrait;
|
||||||
use app\crm\logic\CustomerLogic;
|
use app\crm\logic\CustomerLogic;
|
||||||
@@ -247,12 +248,44 @@ class CustomerService
|
|||||||
$customer->status = 2;
|
$customer->status = 2;
|
||||||
$res = $customer->save();
|
$res = $customer->save();
|
||||||
|
|
||||||
|
self::inquiryUpdate($customer);
|
||||||
|
|
||||||
//软删除
|
//软删除
|
||||||
$customer->delete();
|
$customer->delete();
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 询单修改
|
||||||
|
*/
|
||||||
|
public static function inquiryUpdate($customer,$userId = null)
|
||||||
|
{
|
||||||
|
if(empty($customer->sn) || $customer->type!=1){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$inquiryLog = InquiryLog::where('order_no', $customer->sn)->first();
|
||||||
|
if(empty($inquiryLog)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$userId = $userId??$customer->user_id;
|
||||||
|
|
||||||
|
if ($userId == 0){
|
||||||
|
$inquiryLog->recipient = 0;
|
||||||
|
$inquiryLog->t_time = time();
|
||||||
|
$inquiryLog->status = 1;
|
||||||
|
$inquiryLog->r_time = time();
|
||||||
|
$inquiryLog->save();
|
||||||
|
}else {
|
||||||
|
$inquiryLog->recipient = $customer->user_id;
|
||||||
|
$inquiryLog->t_time = time();
|
||||||
|
$inquiryLog->save();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $customer
|
* @param $customer
|
||||||
* @param $request
|
* @param $request
|
||||||
|
|||||||
@@ -348,13 +348,8 @@ class CustomerService
|
|||||||
'user_his' => $item->user_id,
|
'user_his' => $item->user_id,
|
||||||
'user_id' => $userId,
|
'user_id' => $userId,
|
||||||
]);
|
]);
|
||||||
if (!empty($item->sn) && $item->type==1){
|
|
||||||
$inquiryLog = InquiryLog::where('order_no', $item->sn)->first();
|
\app\crm\service\CustomerService::inquiryUpdate($item,$userId);
|
||||||
if (!empty($inquiryLog)) {
|
|
||||||
$inquiryLog->recipient = $userId;
|
|
||||||
$inquiryLog->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$data = [
|
$data = [
|
||||||
'user_id' => $userId,
|
'user_id' => $userId,
|
||||||
@@ -391,6 +386,8 @@ class CustomerService
|
|||||||
'user_his' => $item->user_id,
|
'user_his' => $item->user_id,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
\app\crm\service\CustomerService::inquiryUpdate($item,0);
|
||||||
}
|
}
|
||||||
$res = $query->update(['user_id' => 0]);
|
$res = $query->update(['user_id' => 0]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user