企微短剧业务系统

ExternalUserService.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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([
  62. 'loss_status' => 1, 'enable' => 1, 'can_receive' => 1
  63. ]);
  64. if(!$changeStatusResult) {
  65. Log::logError('客户删除后再添加时状态修改失败', [
  66. 'corpid' => $corpid, 'user_id' => $data['user_id'], 'external_userid' => $data['external_userid'], 'has_save' => $hasSave
  67. ], 'HasSaveExternalUser');
  68. }
  69. }
  70. # 异步获取客户信息
  71. // $rdsKey = Customer::CUSTOMER_UPDATE_WAITING_TEMPLATE_RDS;
  72. // $result = RedisModel::lPush($rdsKey, json_encode($data));
  73. //
  74. // if(!$result) {
  75. // Log::logError('新添加客户信息入队列失败', $data, 'UpdateExternalContact');
  76. // return false;
  77. // }
  78. # 获取客户信息
  79. $result = CustomerService::updateExternalInfo(
  80. $corpid, $data['user_id'], $data['external_userid'], $data['state'], $changeType
  81. );
  82. if(!$result) {
  83. Log::logError('外部联系人详情信息获取失败', [
  84. 'data' => $data
  85. ], 'ChangeExternalContactEvent');
  86. }
  87. # 渠道码处理入队列
  88. RedisModel::lPush(SourceQrcodes::SOURCE_QRCODE_DEAL_RDS, json_encode([
  89. 'corpid' => $corpid,
  90. 'user_id' => $data['user_id'],
  91. 'external_userid' => $data['external_userid'],
  92. 'state' => $data['state'],
  93. 'change_type' => $changeType,
  94. 'create_time' => $createTime
  95. ]));
  96. if($changeType != 'edit_external_contact') {
  97. # 添加客户动态信息
  98. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 1);
  99. if(!$result) {
  100. Log::logError('(添加客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  101. }
  102. # 客户充值信息入队列
  103. $result = RedisModel::lPush(Customer::CUSTOMER_PAID_INFO_RDS, json_encode(['corpid' => $corpid, 'external_userid' => $data['external_userid']]));
  104. if(!$result) {
  105. Log::logError('(添加客户)客户充值信息入队列失败', $data, 'CustomerPaidInfoDeal');
  106. }
  107. # 客户信息入队列以处理绑定投放相关数据
  108. $result = RedisModel::lPush(Customer::CUSTOMER_AD_RELATION_RDS, json_encode([
  109. 'corpid' => $corpid,
  110. 'external_userid' => $data['external_userid'],
  111. 'user_id' => $data['user_id']
  112. ]));
  113. if(!$result) {
  114. Log::logError('(添加客户)处理绑定投放相关数据入队列失败', $data, 'CustomerAdRelationDeal');
  115. }
  116. }
  117. if('add_external_contact' == $changeType) { // 客服客户数+1
  118. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], 1);
  119. CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
  120. }
  121. # 放入客服号总粉丝数预警队列中
  122. RedisModel::lPush(AbnormalAccountWarnConf::USER_SCALE_WARN_USER_LIST, json_encode([
  123. 'corpid' => $corpid, 'user_id' => $data['user_id']
  124. ]));
  125. break;
  126. case 'del_external_contact': // 删除企业客户事件
  127. $data['corpid'] = $corpid;
  128. $data['user_id'] = trim($msgObj->UserID);
  129. $data['external_userid'] = trim($msgObj->ExternalUserID);
  130. $data['source'] = trim($msgObj->Source);
  131. $result = ExternalContactService::deleteExternalContact(
  132. $corpid, $data['user_id'], $data['external_userid'], $data['source']
  133. );
  134. // $result = RedisModel::lPush(Customer::DEL_CUSTOMER_RDS, json_encode($data));
  135. if(!$result) {
  136. Log::logError('删除企业客户事件处理失败', $data, 'DeleteExternalContact');
  137. return false;
  138. }
  139. # 添加成员动态信息
  140. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 2);
  141. if(!$result) {
  142. Log::logError('(删除企业客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  143. }
  144. # 客服客户数-1
  145. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], -1);
  146. CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
  147. break;
  148. case 'del_follow_user': // 删除跟进成员事件
  149. $data['corpid'] = $corpid;
  150. $data['user_id'] = trim($msgObj->UserID);
  151. $data['external_userid'] = trim($msgObj->ExternalUserID);
  152. $result = ExternalContactService::deleteFollowUser($corpid, $data['user_id'], $data['external_userid']);
  153. // $result = RedisModel::lPush(Customer::DEL_FOLLOW_USER_RDS, json_encode($data));
  154. if(!$result) {
  155. Log::logError('删除跟进成员通知处理失败', $data, 'DeleteFollowUser');
  156. return false;
  157. }
  158. # 添加成员动态信息
  159. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 3);
  160. if(!$result) {
  161. Log::logError('(删除跟进成员)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  162. }
  163. CustomerService::checkCustomerLossStatusInCorp($corpid, $data['external_userid']);
  164. break;
  165. case 'customer_refused': // 客户接替失败事件
  166. $data['corpid'] = $corpid;
  167. $data['user_id'] = trim($msgObj->UserID);
  168. $data['external_userid'] = trim($msgObj->ExternalUserID);
  169. $data['fail_reason'] = trim($msgObj->FailReason);
  170. $result = RedisModel::lPush(Customer::CUSTOMER_REFUSED_RDS, json_encode($data));
  171. if(!$result) {
  172. Log::logError('新添加客户信息入队列失败', $data, 'CustomerRefused');
  173. return false;
  174. }
  175. break;
  176. }
  177. } catch (\Exception $e) {
  178. Log::logError('外部联系人变更事件回调处理发生异常', [
  179. 'line' => $e->getLine(),
  180. 'msg' => $e->getMessage(),
  181. 'data' => $msgObj
  182. ], 'ChangeExternalContactEvent-Exception');
  183. return false;
  184. }
  185. return true;
  186. }
  187. }