vd/app/Domains/Real/Commands/Sync/MongoSync.php
2019-03-07 17:29:52 +08:00

59 lines
1.5 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use App\Models\Mongo\TblCard;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Support\Collection;
use App\Domains\Real\Jobs\MongoSyncJob;
use Illuminate\Support\Facades\Artisan;
use App\Domains\Config\Services\ConfigService;
class MongoSync extends Command
{
protected $name = 'real:sync-mongo';
protected $description = '同步卡基础信息数据';
protected $limit = 1000;
const CURSOR_KEY = 'sync_mongo_cursor';
public function handle()
{
$microtime = app(ConfigService::class)->get(self::CURSOR_KEY) ?: intval(microtime(true) * 1000);
$nextMicrotime = intval(microtime(true) * 1000);
$utcDateTime = new UTCDateTime($microtime);
$total = TblCard::where('pNo', 'No00000000768')->where('oRDate', '>', $utcDateTime)->where('sDate', 'exists', true)->count();
$this->line('待同步条数:'.$total);
if ($total) {
Artisan::call('real:sync-bloc');
Artisan::call('real:sync-company');
}
$page = 2;
$jobs = new Collection();
while ($total) {
if (($page - 1) * $this->limit >= $total) {
break;
}
$jobs->push(new MongoSyncJob($page, $this->limit, $utcDateTime));
$page++;
}
$total && MongoSyncJob::withChain($jobs->toArray())
->dispatch(1, $this->limit, $utcDateTime)
->allOnQueue('sync');
app(ConfigService::class)->set(self::CURSOR_KEY, intval($nextMicrotime));
}
}