diff --git a/app/command/UpdateInquiryLogFromCrmCustomerCommand.php b/app/command/UpdateInquiryLogFromCrmCustomerCommand.php new file mode 100644 index 0000000..06afbc0 --- /dev/null +++ b/app/command/UpdateInquiryLogFromCrmCustomerCommand.php @@ -0,0 +1,144 @@ +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('DRY RUN 模式 - 不会实际执行更新操作'); + } + + $output->writeln('开始更新 inquiry_log 表...'); + $output->writeln("处理限制: {$limit} 条记录"); + + 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("找到 " . count($customers) . " 条客户记录需要处理"); + + $updatedCount = 0; + $skippedCount = 0; + $defaultTimestamp = strtotime('2025-08-26 00:00:00'); + + $output->writeln("找到 " . count($customers) . " 条客户记录需要处理"); + $step = []; + $updateId = []; + foreach ($customers as $customer) { + $output->writeln("处理客户 ID: {$customer->id}, SN: {$customer->sn}, User ID: {$customer->user_id}, obtain: {$customer->obtain}, created_at: {$customer->created_at}"); + // 查找匹配的 inquiry_log 记录 + $inquiryLog = Db::table('inquiry_log') + ->where('order_no', $customer->sn) + ->first(['id', 'recipient', 't_time']); + + if (!$inquiryLog) { + $step[] = $customer->id; + $output->writeln("跳过客户 ID {$customer->id} (sn: {$customer->sn}) - 未找到对应的 inquiry_log 记录"); + $skippedCount++; + continue; + } + $output->writeln("$inquiryLog->id"); + + // 准备更新数据 + $updateData = []; + + // 更新 recipient 字段 (user_id) + if ($customer->user_id != $inquiryLog->recipient) { + $updateData['recipient'] = $customer->user_id; + $output->writeln("客户 ID {$customer->id}: recipient 将从 {$inquiryLog->recipient} 更新为 {$customer->user_id}"); + } + + 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("更新obtain $obtain"); + } + + // 更新 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("客户 ID {$customer->id}: t_time 将从 " . ($inquiryLog->t_time ? date('Y-m-d H:i:s', $inquiryLog->t_time) : 'NULL') . " 更新为 {$timeStr}"); + } + 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("已更新 inquiry_log ID {$inquiryLog->id}"); + } else { + $output->writeln("客户 ID {$customer->id} - 无需更新"); + } + } + + $output->writeln("处理完成!"); + $output->writeln("总记录数: " . count($customers) . ""); + $updateIdstr = implode(",", $updateId); + $output->writeln("更新记录数: {$updatedCount},更新id:$updateIdstr"); + $stepstr = implode(",", $step); + $output->writeln("跳过记录数: {$skippedCount},跳过id: {$stepstr}"); + + if ($isDryRun) { + $output->writeln("DRY RUN 模式 - 未实际执行任何更新操作"); + } + }); + + return self::SUCCESS; + } catch (\Exception $e) { + $output->writeln('执行过程中发生错误: ' . $e->getMessage() . ''); + $output->writeln('错误位置: ' . $e->getFile() . ':' . $e->getLine() . ''); + return self::FAILURE; + } + } +} \ No newline at end of file diff --git a/app/common/model/CrmCustomer.php b/app/common/model/CrmCustomer.php index bc439db..2c262b3 100644 --- a/app/common/model/CrmCustomer.php +++ b/app/common/model/CrmCustomer.php @@ -40,7 +40,7 @@ class CrmCustomer extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'sn', 'corp_abbr', 'user_id', 'user_sn', 'type', 'grade', 'sex', 'channel_id', 'country_id', 'car_type_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'desc_collect', 'address', 'lat', 'lng', 'follow_last', 'follow_next', 'warn', 'follow_remark', 'enter_user_id', 'created_at', 'updated_at', 'deleted_at']; + protected $fillable = ['id', 'sn', 'corp_id', 'user_id', 'user_sn', 'type', 'grade', 'sex', 'channel_id', 'country_id', 'car_type_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'desc_collect', 'address', 'lat', 'lng', 'follow_last', 'follow_next', 'warn', 'follow_remark', 'enter_user_id', 'created_at', 'updated_at', 'deleted_at']; /** * 不允许自动填充模型字段 diff --git a/app/common/traits/ResponseTrait.php b/app/common/traits/ResponseTrait.php index a8adc90..cc20773 100644 --- a/app/common/traits/ResponseTrait.php +++ b/app/common/traits/ResponseTrait.php @@ -18,12 +18,13 @@ trait ResponseTrait */ public function success( $data = [], - string $message = '请求成功', - int $code = 200, + string $message = '请求成功', + string $status = 'success', + int $code = 200, int $httpStatus = 200, array $header = ['Content-Type' => 'application/json'] ): Response { - return $this->result('success', $code, $httpStatus, $message, $data, $header); + return $this->result($status, $code, $httpStatus, $message, $data, $header); } /** diff --git a/app/common/utils/email/Emails.php b/app/common/utils/email/Emails.php index 18b06c2..933e6b4 100644 --- a/app/common/utils/email/Emails.php +++ b/app/common/utils/email/Emails.php @@ -480,7 +480,7 @@ class Emails // 同步客户 $crmCustomer = new CrmCustomer(); $crmCustomer->sn = $inquiryLogResult->order_no; - $crmCustomer->corp_abbr = $recipientInfo->corp_id; + $crmCustomer->corp_id = $recipientInfo->corp_id; $crmCustomer->user_id = $assignedUid; $crmCustomer->type = 1; $crmCustomer->channel_id = $inquiryLogResult->channel; diff --git a/app/crm/controller/CustomerController.php b/app/crm/controller/CustomerController.php index e51b83f..0860af8 100644 --- a/app/crm/controller/CustomerController.php +++ b/app/crm/controller/CustomerController.php @@ -163,7 +163,7 @@ class CustomerController extends BaseController $perPage = $request->input('size', 10); $where = [ ['user_id', '=', $userId], -// ['corp_abbr', '=', $request->basic_data['corp_abbr']], + //['corp_abbr', '=', $request->basic_data['corp_abbr']], ['follow_next', '<>', 0], ]; @@ -306,7 +306,8 @@ class CustomerController extends BaseController $contacts = Util::getParam($request, 'contacts'); $email = Util::getParam($request, 'email'); $phone = Util::getParam($request, 'phone'); - if (Util::isEmpty($contacts) && Util::isEmpty($email) && Util::isEmpty($phone)) { + $whatsApp = Util::getParam($request, 'WhatsApp', ''); + if (Util::isEmpty($contacts) && Util::isEmpty($email) && Util::isEmpty($phone) && Util::isEmpty($whatsApp)) { return $this->error(trans('contacts_not_null')); } @@ -314,7 +315,7 @@ class CustomerController extends BaseController $corpId = $corpInfo->id; $data = [ 'user_id' => $request->user->uid, - 'corp_abbr' => $corpId, + 'corp_id' => $corpId, 'grade' => Util::getParam($request, 'grade', 0), 'channel_id' => $channel_id, 'country_id' => $country_id, @@ -322,7 +323,7 @@ class CustomerController extends BaseController 'magnitude' => Util::getParam($request, 'magnitude', 0), 'email' => $email, 'phone' => $phone, - 'WhatsApp' => Util::getParam($request, 'WhatsApp', ''), + 'WhatsApp' => $whatsApp, 'contacts' => $contacts, 'describe' => Util::getParam($request, 'describe'), 'lat' => Util::getParam($request, 'lat', 0), @@ -335,6 +336,15 @@ class CustomerController extends BaseController if (Util::isEmpty($id)) { $data['enter_user_id'] = $request->user->uid; + $distinct = [ + 'email' => $email, + 'phone' => $phone, + 'whatsApp' => $whatsApp, + ]; + $dis = (new CustomerService())->distinct($distinct,$corpId); + if($dis){ + return $this->success($dis,'客户线索重复,已经业务跟进','distinct'); + } $res = Customer::create($data); } else { unset($data['grade']); @@ -351,6 +361,25 @@ class CustomerController extends BaseController } } + /** + * 重复通知 + * @param Request $request + * @return \support\Response + */ + public function distinctNotice(Request $request){ + $customer_id = Util::getParam($request, 'customer_id'); + if (Util::isEmpty($customer_id)) { + return $this->error('客户id不能为空'); + } + $res = (new CustomerService())->distinctNotice($customer_id); + + if ($res) { + return $this->success($res, '成功'); + } else { + return $this->error(trans('fail')); + } + } + /** @@ -415,6 +444,8 @@ class CustomerController extends BaseController 'user_his' => $userId, 'user_id' => 0, ]); + + CustomerService::inquiryUpdate($customer); } //软删除 @@ -506,12 +537,14 @@ class CustomerController extends BaseController { $customerId = Util::getParam($request, 'customer_id'); $userId = $request->user->uid; + $user = User::where(['id' => $userId])->first(); if (Util::isEmpty($customerId)) { return $this->error(trans('customer_id').trans('not_null')); } $data = [ 'user_id' => $request->user->uid, 'obtain' => time(), + 'corp_id' => $user->corp_id, ]; try { @@ -531,13 +564,8 @@ class CustomerController extends BaseController 'user_his' => 0, 'user_id' => $userId, ]); - if (!empty($customer->sn) && $customer->type==1){ - $inquiryLog = InquiryLog::where('order_no', $customer->sn)->first(); - if (!empty($inquiryLog)) { - $inquiryLog->recipient = $userId; - $inquiryLog->save(); - } - } + + CustomerService::inquiryUpdate($customer, $data['user_id']); Db::commit(); } catch (\Throwable $e) { Db::rollback(); @@ -578,13 +606,8 @@ class CustomerController extends BaseController 'user_his' => $item->user_id, 'user_id' => $user['id'], ]); - if (!empty($customer->sn) && $customer->type==1){ - $inquiryLog = InquiryLog::where('order_no', $customer->sn)->first(); - if (!empty($inquiryLog)) { - $inquiryLog->recipient = $userId; - $inquiryLog->save(); - } - } + + CustomerService::inquiryUpdate($customer,$userId); $item->restore(); } @@ -592,6 +615,7 @@ class CustomerController extends BaseController $data = [ 'user_id' => $user->id, 'obtain' => time(), + 'corp_id' => $user->corp_id, ]; $res = $query->update($data); diff --git a/app/crm/controller/FollowController.php b/app/crm/controller/FollowController.php index 42d6a01..da8161b 100644 --- a/app/crm/controller/FollowController.php +++ b/app/crm/controller/FollowController.php @@ -103,7 +103,7 @@ class FollowController extends BaseController $corpId = $corpInfo->id; $data = [ 'user_id' => $request->user->uid, - 'corp_abbr' => $corpId, + 'corp_id' => $corpId, 'customer_id' => $customer_id, 'type' => $type, 'picture' => $picture, @@ -119,7 +119,7 @@ class FollowController extends BaseController $res = Follow::create($data); $customer = Customer::find($customer_id); - $customer->follow_last = strtotime($res->created_at); + $customer->follow_last = $res->created_at; $customer->follow_next = null; $customer->warn = 0; $customer->save(); diff --git a/app/crm/service/CustomerService.php b/app/crm/service/CustomerService.php index a3559d5..c20ceab 100644 --- a/app/crm/service/CustomerService.php +++ b/app/crm/service/CustomerService.php @@ -4,6 +4,7 @@ namespace app\crm\service; use app\common\model\Corp; use app\common\model\CustomerAudit; +use app\common\model\InquiryLog; use app\common\model\User; use app\common\traits\crud\CrudServiceTrait; use app\crm\logic\CustomerLogic; @@ -15,6 +16,7 @@ use app\crm\model\CustomerHis; use app\crm\model\Follow; use app\crm\utils\Util; use config\constants\Constants; +use Illuminate\Database\Eloquent\Builder; use support\Request; /** @@ -246,12 +248,44 @@ class CustomerService $customer->status = 2; $res = $customer->save(); + self::inquiryUpdate($customer); + //软删除 $customer->delete(); 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 $request @@ -319,4 +353,68 @@ class CustomerService return $customer; } + + /** + * 查询客户关系是否重复 + */ + public function distinct($params,$corpId){ + // 9.1 add 报重修正 + if (!$params['email'] && !$params['phone'] && !$params['whatsApp']) { + return false; + } + $customer = Customer::where('corp_id',$corpId) + ->where((function (Builder $query) use ($params) { + if(!empty($params['email'])) + $query->Orwhere('email', '=', $params['email']); + if(!empty($params['phone'])) + $query->Orwhere('phone', '=', $params['phone']); + if(!empty($params['whatsApp'])) + $query->Orwhere('whatsApp', '=', $params['whatsApp']); +// $query->where('email', '=', $params['email']) +// ->Orwhere('phone', '=', $params['phone']) +// ->Orwhere('whatsApp', '=', $params['whatsApp']); + return $query; + })) + ->first(); + + if (!empty($customer)) { + //客户重复,消息通知 +// if($corpId != $customer->corp_id){ +// return false; +// } + return $customer; + } + return false; + } + + /** + * 客户重复通知 + * @param $params + * @param $corpId + * @return Customer|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object + */ + public function distinctNotice($customer_id){ + + $customer = Customer::where('id', $customer_id) + ->first(); + if (!empty($customer)) { + $contacts = $customer->contacts??$customer['phone']; + $contacts = $contacts??$customer['whatsApp']; + $contacts = $contacts??$customer['email']; + $data = [ + 'sender_id' => 0, //发送人ID 0默认系统 + 'sender_type' => 'system', //发送者类型 user admin system + 'title' => '客户跟进提醒 - ' . $contacts, //标题 + 'content' => $contacts . ' 产生了新的询单,请及时跟进', //内容 + 'scene' => 1, //场景 0系统公共 1系统通知 + 'recipient' => [$customer->user_id], //接收人 空为全部 + 'push_method' => 0 //推送方式0系统推送1手动推送 + ]; + + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $data); + return $customer; + } + return false; + } } \ No newline at end of file diff --git a/app/crm/utils/Util.php b/app/crm/utils/Util.php index 0701f5e..7bf4efd 100644 --- a/app/crm/utils/Util.php +++ b/app/crm/utils/Util.php @@ -126,7 +126,7 @@ class Util public static function mask($str) { - $length = strlen($str); + $length = mb_strlen($str); if(empty($str)){ return ''; diff --git a/app/crmAdmin/controller/CustomerController.php b/app/crmAdmin/controller/CustomerController.php index c68bd18..6104fcd 100644 --- a/app/crmAdmin/controller/CustomerController.php +++ b/app/crmAdmin/controller/CustomerController.php @@ -29,7 +29,7 @@ class CustomerController extends BaseController try { $param = $request->all(); $corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first(); - $param['corp_abbr'] = $corp->id; + $param['corp_id'] = $corp->id; // $param['corp_abbr'] = $request->basic_data['corp_abbr']; $result = (new CustomerService())->list($param); @@ -49,7 +49,7 @@ class CustomerController extends BaseController try { $param = $request->all(); // $corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first(); -// $param['corp_abbr'] = $corp->id; +// $param['corp_id'] = $corp->id; $result = (new CustomerService())->export($param); $excelContent = file_get_contents($result['filepath']); diff --git a/app/crmAdmin/service/CustomerService.php b/app/crmAdmin/service/CustomerService.php index 635b6cf..41bf5d7 100644 --- a/app/crmAdmin/service/CustomerService.php +++ b/app/crmAdmin/service/CustomerService.php @@ -339,6 +339,7 @@ class CustomerService $query= Customer::whereIn('id',explode(',',$customerId)); $customer = $query->get(); + $user = User::where(['id' => $userId])->first(); foreach ($customer as $item) { //记录客户关系变化 @@ -347,16 +348,12 @@ class CustomerService 'user_his' => $item->user_id, 'user_id' => $userId, ]); - if (!empty($item->sn) && $item->type==1){ - $inquiryLog = InquiryLog::where('order_no', $item->sn)->first(); - if (!empty($inquiryLog)) { - $inquiryLog->recipient = $userId; - $inquiryLog->save(); - } - } + + \app\crm\service\CustomerService::inquiryUpdate($item,$userId); } $data = [ 'user_id' => $userId, + 'corp_id' => $user->corp_id, 'obtain' => time(), ]; $res = $query->update($data); @@ -389,6 +386,8 @@ class CustomerService 'user_his' => $item->user_id, 'user_id' => 0, ]); + + \app\crm\service\CustomerService::inquiryUpdate($item,0); } $res = $query->update(['user_id' => 0]); diff --git a/app/queue/redis/InquiryToCrm.php b/app/queue/redis/InquiryToCrm.php new file mode 100644 index 0000000..ad973fd --- /dev/null +++ b/app/queue/redis/InquiryToCrm.php @@ -0,0 +1,113 @@ +where('id', $data['id'])->where('sync_crm', 0)->first(); + + if ($info) { + // 获取用户信息 + $recipient = 0; + $corpId = 0; + $uinfo = null; + if ($info->recipient) { + $recipient = $info->recipient; + $uinfo = User::where('id', $info->recipient)->first(); + if ($uinfo) { + $corpId = $uinfo->corp_id; + } + } + // 同步客户数据 + $crmCustomer = new CrmCustomer(); + $crmCustomer->sn = $info->order_no; + $crmCustomer->corp_id = $corpId; + $crmCustomer->user_id = $recipient; + $crmCustomer->type = 1; + $crmCustomer->channel_id = $info->channel; + $crmCustomer->country_id = $info->country; + $crmCustomer->magnitude = $info->quality; + $crmCustomer->email = $info->email; + $crmCustomer->phone = $info->tel; + $crmCustomer->WhatsApp = $info->whatsapp; + $crmCustomer->contacts = $info->contacts; + $crmCustomer->desc_collect = $info->content; + $crmCustomer->enter_user_id = $info->enter_user_id; + $crmCustomer->obtain = time(); + if(empty($recipient)){ + $crmCustomer->deleted_at = time(); + } + // 修改同步CRM状态 + $info->sync_crm = 1; + // 保存 + if (!$crmCustomer->save() || !$info->save()) { + throw new BusinessException('同步失败~'); + } + + // // 发送邮件消息 + // if ($uinfo && $uinfo->email) { + // $options = [ + // 'id' => $info->id, + // 'email' => $uinfo->email + // ]; + // // Log::info('发送邮件消息:' . json_encode($options)); + // \Webman\RedisQueue\Redis::send('email-message', $options); + // } + + $wsdata = [ + 'sender_id' => 0, + 'sender_type' => 'system', + 'title' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'content' => '有新的客户线索,' . $info->order_no . ',请及时处理', + 'scene' => 1, + 'recipient' => [$info->recipient] + ]; + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $wsdata); + } + } catch (\Throwable $th) { + throw $th; + } + } + + // 消费失败回调 + /* + $package = [ + 'id' => 1357277951, // 消息ID + 'time' => 1709170510, // 消息时间 + 'delay' => 0, // 延迟时间 + 'attempts' => 2, // 消费次数 + 'queue' => 'send-mail', // 队列名 + 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 + 'max_attempts' => 5, // 最大重试次数 + 'error' => '错误信息' // 错误信息 + ] + */ + public function onConsumeFailure(\Throwable $e, $package) + { + echo "consume failure\n"; + echo $e->getMessage() . "\n"; + // 无需反序列化 + var_export($package); + } +} \ No newline at end of file diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php index a350c7e..297359a 100644 --- a/app/queue/redis/InquiryToCrmNew.php +++ b/app/queue/redis/InquiryToCrmNew.php @@ -105,7 +105,7 @@ class InquiryToCrmNew implements Consumer } $udata = [ - 'corp_abbr' => $uInfo['corpId'], + 'corp_id' => $uInfo['corpId'], 'user_id' => $info->recipient, 'channel_id' => $info->channel, 'country_id' => $info->country, @@ -122,7 +122,7 @@ class InquiryToCrmNew implements Consumer } else { $crmCustomer = new CrmCustomer(); $crmCustomer->sn = $info->order_no; - $crmCustomer->corp_abbr = $uInfo['corpId']; + $crmCustomer->corp_id = $uInfo['corpId']; $crmCustomer->user_id = $uInfo['recipient']; $crmCustomer->type = 1; $crmCustomer->channel_id = $info->channel; @@ -135,6 +135,9 @@ class InquiryToCrmNew implements Consumer $crmCustomer->desc_collect = $info->content; $crmCustomer->enter_user_id = $info->enter_user_id; $crmCustomer->obtain = time(); + if(empty($uInfo['recipient'])){ + $crmCustomer->deleted_at = time(); + } // 修改同步CRM状态 $info->sync_crm = 1; // 保存 diff --git a/app/queue/redis/InquiryToCrmUpdate.php b/app/queue/redis/InquiryToCrmUpdate.php new file mode 100644 index 0000000..5a5a330 --- /dev/null +++ b/app/queue/redis/InquiryToCrmUpdate.php @@ -0,0 +1,113 @@ +where('id', $data['id'])->first(); + + $corpId = 0; + if ($info->recipient) { + $uinfo = User::where('id', $info->recipient)->where('status', 1)->first(); + if ($uinfo) { + $corpId = $uinfo->corp_id; + } + } + + // 获取客户信息 + $crmCustomerInfo = CrmCustomer::where('sn', $info->order_no)->first(); + if ($crmCustomerInfo) { + $udata = [ + 'corp_id' => $corpId, + 'user_id' => $info->recipient, + 'channel_id' => $info->channel, + 'country_id' => $info->country, + 'magnitude' => $info->quality, + 'email' => $info->email, + 'phone' => $info->tel, + 'WhatsApp' => $info->whatsapp, + 'contacts' => $info->contacts, + 'desc_collect' => $info->content, + ]; + $crmCustomerInfo->update($udata); + } else { + // 同步客户数据 + $crmCustomer = new CrmCustomer(); + $crmCustomer->sn = $info->order_no; + $crmCustomer->corp_id = $corpId; + $crmCustomer->user_id = $info->recipient; + $crmCustomer->type = 1; + $crmCustomer->channel_id = $info->channel; + $crmCustomer->country_id = $info->country; + $crmCustomer->magnitude = $info->quality; + $crmCustomer->email = $info->email; + $crmCustomer->phone = $info->tel; + $crmCustomer->WhatsApp = $info->whatsapp; + $crmCustomer->contacts = $info->contacts; + $crmCustomer->desc_collect = $info->content; + $crmCustomer->enter_user_id = $info->enter_user_id; + if(empty($info->recipient)){ + $crmCustomer->deleted_at = time(); + } + // 修改同步CRM状态 + $info->sync_crm = 1; + // 保存 + if (!$crmCustomer->save() || !$info->save()) { + throw new BusinessException('同步失败~'); + } + + // 发送邮件消息 + if ($uinfo && $uinfo->email) { + $options = [ + 'id' => $info->id, + 'email' => $uinfo->email + ]; + \Webman\RedisQueue\Redis::send('email-message', $options); + } + } + } catch (\Throwable $th) { + throw $th; + } + } + + // 消费失败回调 + /* + $package = [ + 'id' => 1357277951, // 消息ID + 'time' => 1709170510, // 消息时间 + 'delay' => 0, // 延迟时间 + 'attempts' => 2, // 消费次数 + 'queue' => 'send-mail', // 队列名 + 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 + 'max_attempts' => 5, // 最大重试次数 + 'error' => '错误信息' // 错误信息 + ] + */ + public function onConsumeFailure(\Throwable $e, $package) + { + echo "consume failure\n"; + echo $e->getMessage() . "\n"; + // 无需反序列化 + var_export($package); + } +} \ No newline at end of file diff --git a/app/ticket/service/ConversationService.php b/app/ticket/service/ConversationService.php index 852fc0c..972938e 100644 --- a/app/ticket/service/ConversationService.php +++ b/app/ticket/service/ConversationService.php @@ -81,7 +81,7 @@ class ConversationService $ticket_status = $locale == 'zh'? Constants::TICKET_STATUS_ZH:Constants::TICKET_STATUS_EN; $content = str_replace($status_fanyi,$ticket_status,$data['content']); - $reply = $content?($content.'\n'.'工单内容:'):'' ; + $reply = $content?($content."\n".'工单内容:'):'' ; } $notice = ['sender_id' => 0, //发送人ID 0默认系统 diff --git a/config/route.php b/config/route.php index b2cb132..43e45b6 100644 --- a/config/route.php +++ b/config/route.php @@ -246,6 +246,7 @@ Route::group('/crm', function () { Route::any('/customer/getCountry', [app\crm\controller\CustomerController::class, 'getCountry']); Route::any('/customer/followNext', [app\crm\controller\CustomerController::class, 'followNext']); Route::any('/customer/report', [app\crm\controller\CustomerController::class, 'report']); + Route::any('/customer/distinctNotice', [app\crm\controller\CustomerController::class, 'distinctNotice']); Route::any('/tagGroup/list', [app\crm\controller\TagGroupController::class, 'list']); Route::any('/tagGroup/updatePivot', [app\crm\controller\TagGroupController::class, 'updatePivot']);