123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AbnormalAccountWarnConf;
- use App\Models\Customer;
- use App\Models\CustomerDetails;
- use App\Models\CustomerDynamic;
- use App\Models\SourceQrcodes;
- use App\Models\UserSop\UserSopRule;
- use App\Models\WelcomeMsg;
- use App\RedisModel;
- 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);
- # 新添加客户入队列发送欢迎语
- if(!empty($welcomeCode)) {
- $newUserInfo = array(
- 'corpid' => $corpid, 'user_id' => $data['user_id'], 'external_userid' => $data['external_userid'],
- 'welcome_code' => $welcomeCode, 'push_time' => date('Y-m-d H:i:s'), 'state' => $data['state']
- );
- RedisModel::lPush(WelcomeMsg::WELCOME_USER_RDS, json_encode($newUserInfo));
- # 判断企业是否配置了个人SOP
- $isSetSopRule = UserSopRule::where('enable', 1)->where('corpid', $corpid)->where('status', 1)->exists();
- if($isSetSopRule) {
- $sopResult = RedisModel::lPush(UserSopRule::USER_SOP_RDS, json_encode($newUserInfo));
- Log::logInfo('配置了SOP的企业,客户信息入队列【Playlet::User_Sop_List】完成', [
- 'data' => $newUserInfo,
- 'result' => $sopResult
- ], 'UserSopRuleDealTrace');
- }
- Log::logInfo('待发送欢迎语数据插入队列【Playlet::Welcome_User_List】完成', [
- 'data' => $newUserInfo
- ], 'WelcomeCodeSendTrace');
- }
- # 校验是否是删除后重新添加的用户
- $hasSave = CustomerDetails::suffix($corpid)->where('user_id', $data['user_id'])
- ->where('external_userid', $data['external_userid'])
- ->where('corpid', $corpid)
- ->where('loss_status', 0)
- ->count();
- if($hasSave) {
- $changeStatusResult = CustomerDetails::suffix($corpid)->where('user_id', $data['user_id'])
- ->where('external_userid', $data['external_userid'])->where('corpid', $corpid)->update([
- 'loss_status' => 1, 'enable' => 1, 'can_receive' => 1
- ]);
- if(!$changeStatusResult) {
- Log::logError('客户删除后再添加时状态修改失败', [
- 'corpid' => $corpid, 'user_id' => $data['user_id'], 'external_userid' => $data['external_userid'], 'has_save' => $hasSave
- ], 'HasSaveExternalUser');
- }
- }
- # 异步获取客户信息
- // $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);
- CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
- }
- # 放入客服号总粉丝数预警队列中
- 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);
- CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
- 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');
- }
- CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
- 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;
- }
- }
|