isEmpty()) { // break; // } $list = DjCustomerLossRecord::query()->whereIn('id', [381269])->selectRaw('id, name, avatar, loss_corpid_list, remain_corpid_list')->get(); $this->info('本次共查询数据 '.$list->count().' 条,起始id为 '.$startId); $startId = $list->max('id'); foreach($list as $item) { if(empty($item->remain_corpid_list) || empty($item->loss_corpid_list)) { $this->error('异常数据 id : '.$item->id ); Log::logError('异常数据', [$item->toArray()], 'MarkCustomerDealTagError'); continue; } $this->dealCorpData($item->name, $item->avatar, $item->loss_corpid_list, $item->id); } // } } catch (\Exception $exception) { $this->error($exception->getFile().'('.$exception->getLine().') : '.$exception->getMessage()); Log::logError('程序异常', ['file' => $exception->getFile(), 'line' => $exception->getLine() , 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()], 'MarkCustomerDealTagError'); } } public function dealCorpData($customerName, $customerAvatar, $lossCorpidList, $itemId) { # 解析企微ID $lossCorpidListArr = explode(',', $lossCorpidList); foreach ($lossCorpidListArr as $lossCorpid) { $search = [ 'name' => $customerName, 'avatar' => $customerAvatar ]; $customerInfo = Customer::getCustomerInfoBySearch($lossCorpid, $search); if($customerInfo->isEmpty()) { $this->error('查询客户结果为空,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】'); Log::logError('查询客户结果为空,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError'); continue; } if($customerInfo->count() > 1) { $this->error('查询客户结果条数不符合预期,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】'); Log::logError('查询客户结果条数不符合预期,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError'); } $externalUserInfo = $customerInfo->first(); $externalUserId = $externalUserInfo->external_userid ?? null; if(empty($externalUserId)) { $this->error('外部联系人ID获取异常,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】'); Log::logError('外部联系人ID获取异常,企微ID【'.$lossCorpid.'】,记录ID【'.$itemId.'】', [], 'MarkCustomerDealTagError'); continue; } $this->dealCorpUserData($lossCorpid, $externalUserId); } } public function dealCorpUserData($lossCorpid, $externalUserId) { # 查询客服关系 $relationList = CustomerDetails::getRelationList($lossCorpid, $externalUserId); # 查询有许可的客服列表 $redisKey = 'Playlet::activeUserList-'.$lossCorpid; $activeUserListCache = RedisModel::get($redisKey); if(empty($activeUserListCache)) { $activeUserList = DjUser::getActiveUserListById($lossCorpid, []); $activeUserList = $activeUserList->toArray(); RedisModel::set($redisKey, json_encode($activeUserList)); RedisModel::expire($redisKey, 1800); } else { $activeUserList = json_decode($activeUserListCache, 1); } $tagGroupName = $tagName = 'del2024'; foreach($relationList as $relationInfo) { if(in_array($relationInfo->user_id, $activeUserList)) { $redisValue = [ 'corpid' => $lossCorpid, 'user_id' => $relationInfo->user_id,//成员userid 'external_userid' => $externalUserId, 'tag_group_name' => $tagGroupName, 'tag_name_list' => [$tagName], 'type' => 2 ]; RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_MARK_RDS, json_encode($redisValue, 256)); Log::logInfo('处理数据', $redisValue, 'MarkCustomerDealTagTrace'); } else { Log::logError('客服状态不合法,企微ID', ['corpid' => $lossCorpid, 'external_user_id' => $externalUserId , 'user_id' => $relationInfo->user_id], 'MarkCustomerDealTagError'); } } } }