企微短剧业务系统

MarkCustomerDealTag.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Console\Repair;
  3. use App\Log;
  4. use App\Models\Customer;
  5. use App\Models\Customer\DjCustomerLossRecord;
  6. use App\Models\CustomerDetails;
  7. use App\Models\DjUser;
  8. use App\RedisModel;
  9. use App\Service\CustomerTagService;
  10. use Illuminate\Console\Command;
  11. class MarkCustomerDealTag extends Command
  12. {
  13. protected $signature = 'MarkCustomerDealTag';
  14. protected $description = '从数据表中查询已流失用户,添加已删除标签';
  15. public function __construct() {
  16. parent::__construct();
  17. }
  18. public function handle()
  19. {
  20. $startId = 0;
  21. $limit = 1000;
  22. // 直接终止执行,防止手误出错
  23. return false;
  24. try{
  25. # 遍历查询数据表中数据
  26. // while (true) {
  27. // $list = DjCustomerLossRecord::getList($startId, $limit);
  28. // if($list->isEmpty()) {
  29. // break;
  30. // }
  31. $list = DjCustomerLossRecord::query()->whereIn('id', [381269])->selectRaw('id, name, avatar, loss_corpid_list, remain_corpid_list')->get();
  32. $this->info('本次共查询数据 '.$list->count().' 条,起始id为 '.$startId);
  33. $startId = $list->max('id');
  34. foreach($list as $item) {
  35. if(empty($item->remain_corpid_list) || empty($item->loss_corpid_list)) {
  36. $this->error('异常数据 id : '.$item->id );
  37. Log::logError('异常数据', [$item->toArray()], 'MarkCustomerDealTagError');
  38. continue;
  39. }
  40. $this->dealCorpData($item->name, $item->avatar, $item->loss_corpid_list, $item->id);
  41. }
  42. // }
  43. } catch (\Exception $exception) {
  44. $this->error($exception->getFile().'('.$exception->getLine().') : '.$exception->getMessage());
  45. Log::logError('程序异常', ['file' => $exception->getFile(), 'line' => $exception->getLine()
  46. , 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()], 'MarkCustomerDealTagError');
  47. }
  48. }
  49. public function dealCorpData($customerName, $customerAvatar, $lossCorpidList, $itemId) {
  50. # 解析企微ID
  51. $lossCorpidListArr = explode(',', $lossCorpidList);
  52. foreach ($lossCorpidListArr as $lossCorpid) {
  53. $search = [
  54. 'name' => $customerName, 'avatar' => $customerAvatar
  55. ];
  56. $customerInfo = Customer::getCustomerInfoBySearch($lossCorpid, $search);
  57. if($customerInfo->isEmpty()) {
  58. $this->error('查询客户结果为空,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】');
  59. Log::logError('查询客户结果为空,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError');
  60. continue;
  61. }
  62. if($customerInfo->count() > 1) {
  63. $this->error('查询客户结果条数不符合预期,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】');
  64. Log::logError('查询客户结果条数不符合预期,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError');
  65. }
  66. $externalUserInfo = $customerInfo->first();
  67. $externalUserId = $externalUserInfo->external_userid ?? null;
  68. if(empty($externalUserId)) {
  69. $this->error('外部联系人ID获取异常,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】');
  70. Log::logError('外部联系人ID获取异常,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError');
  71. continue;
  72. }
  73. $this->dealCorpUserData($lossCorpid, $externalUserId);
  74. }
  75. }
  76. public function dealCorpUserData($lossCorpid, $externalUserId) {
  77. # 查询客服关系
  78. $relationList = CustomerDetails::getRelationList($lossCorpid, $externalUserId);
  79. # 查询有许可的客服列表
  80. $redisKey = 'Playlet::activeUserList-'.$lossCorpid;
  81. $activeUserListCache = RedisModel::get($redisKey);
  82. if(empty($activeUserListCache)) {
  83. $activeUserList = DjUser::getActiveUserListById($lossCorpid, []);
  84. $activeUserList = $activeUserList->toArray();
  85. RedisModel::set($redisKey, json_encode($activeUserList));
  86. RedisModel::expire($redisKey, 1800);
  87. } else {
  88. $activeUserList = json_decode($activeUserListCache, 1);
  89. }
  90. $tagGroupName = $tagName = 'del2024';
  91. foreach($relationList as $relationInfo) {
  92. if(in_array($relationInfo->user_id, $activeUserList)) {
  93. $redisValue = [
  94. 'corpid' => $lossCorpid,
  95. 'user_id' => $relationInfo->user_id,//成员userid
  96. 'external_userid' => $externalUserId,
  97. 'tag_group_name' => $tagGroupName,
  98. 'tag_name_list' => [$tagName],
  99. 'type' => 2
  100. ];
  101. RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_MARK_RDS, json_encode($redisValue, 256));
  102. Log::logInfo('处理数据', $redisValue, 'MarkCustomerDealTagTrace');
  103. } else {
  104. Log::logError('客服状态不合法,企微ID', ['corpid' => $lossCorpid, 'external_user_id' => $externalUserId
  105. , 'user_id' => $relationInfo->user_id], 'MarkCustomerDealTagError');
  106. }
  107. }
  108. }
  109. }