123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Service;
- class ExternalUserService
- {
- /**
- * 外部联系人变更事件回调处理
- * */
- public static function changeExternalContactEvent($msgObj)
- {
- try {
- $changeType = trim($msgObj->ChangeType);
- $corpid = trim($msgObj->ToUserName);
- switch($changeType) {
- case 'add_external_contact': // 添加企业客户事件
- case 'edit_external_contact': // 编辑客户事件
- case 'add_half_external_contact': // 外部联系人免验证添加成员事件
- $data['corpid'] = $corpid;
- $data['user_id'] = trim($msgObj->UserID);
- $data['external_userid'] = trim($msgObj->ExternalUserID);
- $data['state'] = trim($msgObj->State);
- $data['change_type'] = $changeType; // 添加变更类型
- $welcomeCode = trim($msgObj->WelcomeCode);
- $createTime = trim($msgObj->CreateTime);
- # 异步获取客户信息
- // $rdsKey = Customer::CUSTOMER_UPDATE_WAITING_TEMPLATE_RDS;
- // $result = RedisModel::lPush($rdsKey, json_encode($data));
- //
- // if(!$result) {
- // Log::logError('新添加客户信息入队列失败', $data, 'UpdateExternalContact');
- // return false;
- // }
- # 获取客户信息
- $result = CustomerService::updateExternalInfo(
- $corpid, $data['user_id'], $data['external_userid'], $data['state'], $changeType
- );
- if(!$result) {
- Log::logError('外部联系人详情信息获取失败', [
- 'data' => $data
- ], 'ChangeExternalContactEvent');
- }
- # 渠道码处理入队列
- RedisModel::lPush(SourceQrcodes::SOURCE_QRCODE_DEAL_RDS, json_encode([
- 'corpid' => $corpid,
- 'user_id' => $data['user_id'],
- 'external_userid' => $data['external_userid'],
- 'state' => $data['state'],
- 'change_type' => $changeType,
- 'create_time' => $createTime
- ]));
- if($changeType != 'edit_external_contact') {
- # 添加客户动态信息
- $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 1);
- if(!$result) {
- Log::logError('(添加客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
- }
- # 客户充值信息入队列
- $result = RedisModel::lPush(Customer::CUSTOMER_PAID_INFO_RDS, json_encode(['corpid' => $corpid, 'external_userid' => $data['external_userid']]));
- if(!$result) {
- Log::logError('(添加客户)客户充值信息入队列失败', $data, 'CustomerPaidInfoDeal');
- }
- # 客户信息入队列以处理绑定投放相关数据
- $result = RedisModel::lPush(Customer::CUSTOMER_AD_RELATION_RDS, json_encode([
- 'corpid' => $corpid,
- 'external_userid' => $data['external_userid'],
- 'user_id' => $data['user_id']
- ]));
- if(!$result) {
- Log::logError('(添加客户)处理绑定投放相关数据入队列失败', $data, 'CustomerAdRelationDeal');
- }
- }
- if('add_external_contact' == $changeType) { // 客服客户数+1
- RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], 1);
- }
- # 放入客服号总粉丝数预警队列中
- RedisModel::lPush(AbnormalAccountWarnConf::USER_SCALE_WARN_USER_LIST, json_encode([
- 'corpid' => $corpid, 'user_id' => $data['user_id']
- ]));
- break;
- case 'del_external_contact': // 删除企业客户事件
- $data['corpid'] = $corpid;
- $data['user_id'] = trim($msgObj->UserID);
- $data['external_userid'] = trim($msgObj->ExternalUserID);
- $data['source'] = trim($msgObj->Source);
- $result = ExternalContactService::deleteExternalContact(
- $corpid, $data['user_id'], $data['external_userid'], $data['source']
- );
- // $result = RedisModel::lPush(Customer::DEL_CUSTOMER_RDS, json_encode($data));
- if(!$result) {
- Log::logError('删除企业客户事件处理失败', $data, 'DeleteExternalContact');
- return false;
- }
- # 添加成员动态信息
- $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 2);
- if(!$result) {
- Log::logError('(删除企业客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
- }
- # 客服客户数-1
- RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], -1);
- break;
- case 'del_follow_user': // 删除跟进成员事件
- $data['corpid'] = $corpid;
- $data['user_id'] = trim($msgObj->UserID);
- $data['external_userid'] = trim($msgObj->ExternalUserID);
- $result = ExternalContactService::deleteFollowUser($corpid, $data['user_id'], $data['external_userid']);
- // $result = RedisModel::lPush(Customer::DEL_FOLLOW_USER_RDS, json_encode($data));
- if(!$result) {
- Log::logError('删除跟进成员通知处理失败', $data, 'DeleteFollowUser');
- return false;
- }
- # 添加成员动态信息
- $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 3);
- if(!$result) {
- Log::logError('(删除跟进成员)客户动态信息存储失败', $data, 'CustomerDynamicSave');
- }
- break;
- case 'customer_refused': // 客户接替失败事件
- $data['corpid'] = $corpid;
- $data['user_id'] = trim($msgObj->UserID);
- $data['external_userid'] = trim($msgObj->ExternalUserID);
- $data['fail_reason'] = trim($msgObj->FailReason);
- $result = RedisModel::lPush(Customer::CUSTOMER_REFUSED_RDS, json_encode($data));
- if(!$result) {
- Log::logError('新添加客户信息入队列失败', $data, 'CustomerRefused');
- return false;
- }
- break;
- }
- } catch (\Exception $e) {
- Log::logError('外部联系人变更事件回调处理发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'data' => $msgObj
- ], 'ChangeExternalContactEvent-Exception');
- return false;
- }
- return true;
- }
- }
|