123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Console\Repair;
- use App\Log;
- use App\Models\Customer;
- use App\Models\Customer\DjCustomerLossRecord;
- use App\Models\CustomerDetails;
- use App\Models\DjUser;
- use App\RedisModel;
- use App\Service\CustomerTagService;
- use Illuminate\Console\Command;
- class MarkCustomerDealTag extends Command
- {
- protected $signature = 'MarkCustomerDealTag';
- protected $description = '从数据表中查询已流失用户,添加已删除标签';
- public function __construct() {
- parent::__construct();
- }
- public function handle()
- {
- $startId = 0;
- $limit = 1000;
- // 直接终止执行,防止手误出错
- return false;
- try{
- # 遍历查询数据表中数据
- // while (true) {
- // $list = DjCustomerLossRecord::getList($startId, $limit);
- // if($list->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');
- }
- }
- }
- }
|