企微短剧业务系统

ExternalUserService.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AbnormalAccountWarnConf;
  5. use App\Models\Customer;
  6. use App\Models\CustomerDetails;
  7. use App\Models\CustomerDynamic;
  8. use App\Models\SourceQrcodes;
  9. use App\Models\UserSop\UserSopRule;
  10. use App\Models\WelcomeMsg;
  11. use App\RedisModel;
  12. class ExternalUserService
  13. {
  14. /**
  15. * 外部联系人变更事件回调处理
  16. * */
  17. public static function changeExternalContactEvent($msgObj)
  18. {
  19. try {
  20. $changeType = trim($msgObj->ChangeType);
  21. $corpid = trim($msgObj->ToUserName);
  22. switch($changeType) {
  23. case 'add_external_contact': // 添加企业客户事件
  24. case 'edit_external_contact': // 编辑客户事件
  25. case 'add_half_external_contact': // 外部联系人免验证添加成员事件
  26. $data['corpid'] = $corpid;
  27. $data['user_id'] = trim($msgObj->UserID);
  28. $data['external_userid'] = trim($msgObj->ExternalUserID);
  29. $data['state'] = trim($msgObj->State);
  30. $data['change_type'] = $changeType; // 添加变更类型
  31. $welcomeCode = trim($msgObj->WelcomeCode);
  32. $createTime = trim($msgObj->CreateTime);
  33. # 新添加客户入队列发送欢迎语
  34. if(!empty($welcomeCode)) {
  35. $newUserInfo = array(
  36. 'corpid' => $corpid, 'user_id' => $data['user_id'], 'external_userid' => $data['external_userid'],
  37. 'welcome_code' => $welcomeCode, 'push_time' => date('Y-m-d H:i:s'), 'state' => $data['state']
  38. );
  39. RedisModel::lPush(WelcomeMsg::WELCOME_USER_RDS, json_encode($newUserInfo));
  40. # 判断企业是否配置了个人SOP
  41. $isSetSopRule = UserSopRule::where('enable', 1)->where('corpid', $corpid)->where('status', 1)->exists();
  42. if($isSetSopRule) {
  43. $sopResult = RedisModel::lPush(UserSopRule::USER_SOP_RDS, json_encode($newUserInfo));
  44. Log::logInfo('配置了SOP的企业,客户信息入队列【Playlet::User_Sop_List】完成', [
  45. 'data' => $newUserInfo,
  46. 'result' => $sopResult
  47. ], 'UserSopRuleDealTrace');
  48. }
  49. Log::logInfo('待发送欢迎语数据插入队列【Playlet::Welcome_User_List】完成', [
  50. 'data' => $newUserInfo
  51. ], 'WelcomeCodeSendTrace');
  52. }
  53. # 校验是否是删除后重新添加的用户
  54. $hasSave = CustomerDetails::suffix($corpid)->where('user_id', $data['user_id'])
  55. ->where('external_userid', $data['external_userid'])
  56. ->where('corpid', $corpid)
  57. ->where('loss_status', 0)
  58. ->count();
  59. if($hasSave) {
  60. $changeStatusResult = CustomerDetails::suffix($corpid)->where('user_id', $data['user_id'])
  61. ->where('external_userid', $data['external_userid'])->where('corpid', $corpid)->update(['loss_status' => 1, 'enable' => 1]);
  62. if(!$changeStatusResult) {
  63. Log::logError('客户删除后再添加时状态修改失败', [
  64. 'corpid' => $corpid, 'user_id' => $data['user_id'], 'external_userid' => $data['external_userid'], 'has_save' => $hasSave
  65. ], 'HasSaveExternalUser');
  66. }
  67. }
  68. # 异步获取客户信息
  69. // $rdsKey = Customer::CUSTOMER_UPDATE_WAITING_TEMPLATE_RDS;
  70. // $result = RedisModel::lPush($rdsKey, json_encode($data));
  71. //
  72. // if(!$result) {
  73. // Log::logError('新添加客户信息入队列失败', $data, 'UpdateExternalContact');
  74. // return false;
  75. // }
  76. # 获取客户信息
  77. $result = CustomerService::updateExternalInfo(
  78. $corpid, $data['user_id'], $data['external_userid'], $data['state'], $changeType
  79. );
  80. if(!$result) {
  81. Log::logError('外部联系人详情信息获取失败', [
  82. 'data' => $data
  83. ], 'ChangeExternalContactEvent');
  84. }
  85. # 渠道码处理入队列
  86. RedisModel::lPush(SourceQrcodes::SOURCE_QRCODE_DEAL_RDS, json_encode([
  87. 'corpid' => $corpid,
  88. 'user_id' => $data['user_id'],
  89. 'external_userid' => $data['external_userid'],
  90. 'state' => $data['state'],
  91. 'change_type' => $changeType,
  92. 'create_time' => $createTime
  93. ]));
  94. if($changeType != 'edit_external_contact') {
  95. # 添加客户动态信息
  96. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 1);
  97. if(!$result) {
  98. Log::logError('(添加客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  99. }
  100. # 客户充值信息入队列
  101. $result = RedisModel::lPush(Customer::CUSTOMER_PAID_INFO_RDS, json_encode(['corpid' => $corpid, 'external_userid' => $data['external_userid']]));
  102. if(!$result) {
  103. Log::logError('(添加客户)客户充值信息入队列失败', $data, 'CustomerPaidInfoDeal');
  104. }
  105. # 客户信息入队列以处理绑定投放相关数据
  106. $result = RedisModel::lPush(Customer::CUSTOMER_AD_RELATION_RDS, json_encode([
  107. 'corpid' => $corpid,
  108. 'external_userid' => $data['external_userid'],
  109. 'user_id' => $data['user_id']
  110. ]));
  111. if(!$result) {
  112. Log::logError('(添加客户)处理绑定投放相关数据入队列失败', $data, 'CustomerAdRelationDeal');
  113. }
  114. }
  115. if('add_external_contact' == $changeType) { // 客服客户数+1
  116. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], 1);
  117. }
  118. # 放入客服号总粉丝数预警队列中
  119. RedisModel::lPush(AbnormalAccountWarnConf::USER_SCALE_WARN_USER_LIST, json_encode([
  120. 'corpid' => $corpid, 'user_id' => $data['user_id']
  121. ]));
  122. break;
  123. case 'del_external_contact': // 删除企业客户事件
  124. $data['corpid'] = $corpid;
  125. $data['user_id'] = trim($msgObj->UserID);
  126. $data['external_userid'] = trim($msgObj->ExternalUserID);
  127. $data['source'] = trim($msgObj->Source);
  128. $result = ExternalContactService::deleteExternalContact(
  129. $corpid, $data['user_id'], $data['external_userid'], $data['source']
  130. );
  131. // $result = RedisModel::lPush(Customer::DEL_CUSTOMER_RDS, json_encode($data));
  132. if(!$result) {
  133. Log::logError('删除企业客户事件处理失败', $data, 'DeleteExternalContact');
  134. return false;
  135. }
  136. # 添加成员动态信息
  137. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 2);
  138. if(!$result) {
  139. Log::logError('(删除企业客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  140. }
  141. # 客服客户数-1
  142. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], -1);
  143. break;
  144. case 'del_follow_user': // 删除跟进成员事件
  145. $data['corpid'] = $corpid;
  146. $data['user_id'] = trim($msgObj->UserID);
  147. $data['external_userid'] = trim($msgObj->ExternalUserID);
  148. $result = ExternalContactService::deleteFollowUser($corpid, $data['user_id'], $data['external_userid']);
  149. // $result = RedisModel::lPush(Customer::DEL_FOLLOW_USER_RDS, json_encode($data));
  150. if(!$result) {
  151. Log::logError('删除跟进成员通知处理失败', $data, 'DeleteFollowUser');
  152. return false;
  153. }
  154. # 添加成员动态信息
  155. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 3);
  156. if(!$result) {
  157. Log::logError('(删除跟进成员)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  158. }
  159. break;
  160. case 'customer_refused': // 客户接替失败事件
  161. $data['corpid'] = $corpid;
  162. $data['user_id'] = trim($msgObj->UserID);
  163. $data['external_userid'] = trim($msgObj->ExternalUserID);
  164. $data['fail_reason'] = trim($msgObj->FailReason);
  165. $result = RedisModel::lPush(Customer::CUSTOMER_REFUSED_RDS, json_encode($data));
  166. if(!$result) {
  167. Log::logError('新添加客户信息入队列失败', $data, 'CustomerRefused');
  168. return false;
  169. }
  170. break;
  171. }
  172. } catch (\Exception $e) {
  173. Log::logError('外部联系人变更事件回调处理发生异常', [
  174. 'line' => $e->getLine(),
  175. 'msg' => $e->getMessage(),
  176. 'data' => $msgObj
  177. ], 'ChangeExternalContactEvent-Exception');
  178. return false;
  179. }
  180. return true;
  181. }
  182. }