GroupOut.php 1.78 KB
<?php

namespace App\Console\Commands;

use App\Helpers\IMHelper;
use App\Http\Service\UserService;
use App\Models\GroupHasMember;
use App\Models\SystemConfig;
use Illuminate\Console\Command;


class GroupOut extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'group:out';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动退出团队';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        GroupHasMember::with(['group:id,user_id', 'member:id,business_id'])->where('status', 2)
            ->where('updated_at', '<=', now()->subDays($this->getLimitDay()))
            ->eachById(function (GroupHasMember $member) {
                $response = IMHelper::sendSingleChatMessage($member->getAttribute('member_id'), $member->getRelation('group')?->getAttribute('user_id') ?? 0, [
                    "MsgBody"         => IMHelper::createOnlineMessage('TIMCustomElem', [
                        "Data" => ['businessID' => 'invite_tip', 'content' => '', 'text' => '自动退出团队', 'tipStatus' => 8, 'title' => '', 'version' => 4],
                        "Desc" => "notification", "Ext" => "url"
                    ]),
                    "OfflinePushInfo" => IMHelper::createOfflineMessage('', '自动退出团队')
                ]);

                UserService::deleteGroupMember($member->getRelation('member'));
                info('自动退出团队', $response->json());
            });

        return self::SUCCESS;
    }


    public function getLimitDay()
    {
        return SystemConfig::query()->where('identifier', 'F_bk65qzuTXvyemFAl4DR')->value('content') ?? 7;
    }

}