BrokerUserConfigCommand.php 2.01 KB
<?php

namespace App\Console\Commands;

use App\Enums\BrokerPushLevelRecordStatusEnum;
use App\Jobs\BrokerPushLevelRecordSendJob;
use App\Models\BrokerPushLevelRecord;
use App\Models\BrokerUserConfig;
use App\Models\BrokerUserConfigItem;
use DB;
use Illuminate\Console\Command;

class BrokerUserConfigCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'broker:user-config {method}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '经纪人运营-用户配置';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        $method = $this->argument('method');

        switch ($method) {
            case 'singer_num':
                BrokerUserConfigItem::query()
                    ->leftJoin('users', 'users.id', 'broker_user_config_items.user_id')
                    ->where('config_id', BrokerUserConfig::query()->whereDate('begin_at', now()->toDateString())->value('id'))
                    ->where('broker_user_config_items.status', 1)
                    ->where('broker_user_config_items.is_sync', 0)
                    ->update([
                        'users.business_singer_limit'      => DB::raw('CASE WHEN broker_user_config_items.singer_num = "" THEN users.business_singer_limit ELSE broker_user_config_items.singer_num END'),
                        'broker_user_config_items.is_sync' => 1
                    ]);
                break;
            case 'level_message':
                BrokerPushLevelRecord::query()
                    ->where('status', BrokerPushLevelRecordStatusEnum::WAITING)
                    ->whereBetween('publish_at', [now()->subHour()->toDateTimeString(), now()->toDateTimeString()])
                    ->eachById(fn(BrokerPushLevelRecord $record) => BrokerPushLevelRecordSendJob::dispatchSync($record));
                break;
        }

        return self::SUCCESS;
    }
}