87 lines
2.8 KiB
PHP
87 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Real\Commands\Sync;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Card\Card;
|
|
use MongoDB\BSON\UTCDateTime;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use App\Domains\Config\Services\ConfigService;
|
|
use App\Domains\Card\Repositories\BlocRepository;
|
|
use App\Domains\Card\Repositories\CardRepository;
|
|
|
|
class MongoSync extends Command
|
|
{
|
|
protected $name = 'real:sync-mongo';
|
|
|
|
protected $description = '同步卡基础信息数据';
|
|
|
|
protected static $carrierOperators = [1, 0, 2];
|
|
|
|
protected $limit = 1000;
|
|
|
|
const CURSOR_KEY = 'sync_mongo_cursor';
|
|
|
|
public function handle()
|
|
{
|
|
$nextMicrotime = $microtime = app(ConfigService::class)->get(self::CURSOR_KEY) ?: 946656000000;
|
|
|
|
$utcDateTime = new UTCDateTime($microtime);
|
|
|
|
Artisan::call('real:sync-bloc');
|
|
$blocs = app(BlocRepository::class)->get()->pluck('id', 'sn')->toArray();
|
|
|
|
$query = DB::connection('mongo')->table('tblCard')
|
|
->select(['cNo', 'iccid', 'imsi', 'comId', 'oType', 'saDate', 'sDate'])
|
|
->where('isDel', '<>', 1)
|
|
->where('sDate', '>', $utcDateTime)
|
|
->orderBy('sDate');
|
|
|
|
$total = $query->count();
|
|
|
|
$this->line('待同步条数:'.$total);
|
|
|
|
$page = 1;
|
|
|
|
while ($total) {
|
|
echo 'sync_mongo_cursor page #: ' . $page . ' nextMicrotime #: ' . $nextMicrotime . PHP_EOL;
|
|
$res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get();
|
|
|
|
$values = [];
|
|
|
|
foreach ($res as $key => $value) {
|
|
$activated_at = $value['saDate'] ? $value['saDate']->toDateTime()->format('Y-m-d H:i:s') : null;
|
|
$sim = intval(preg_replace('/\D/', '', $value['cNo']));
|
|
$values[$sim] = [
|
|
'sim' => $sim,
|
|
'imsi' => $value['imsi'] ?? '',
|
|
'iccid' => $value['iccid'] ?? '',
|
|
'bloc_id' => $blocs[$value['comId']] ?? 0,
|
|
'carrier_operator' => self::$carrierOperators[$value['oType']] ?? 255,
|
|
'activated_at' => $activated_at,
|
|
'virtual_activated_at' => $activated_at,
|
|
'created_at' => $value['sDate']->toDateTime()->format('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
$nextMicrotime = (string) $value['sDate'];
|
|
|
|
$nextMicrotime = ($nextMicrotime > $microtime) ? $nextMicrotime : $microtime;
|
|
}
|
|
|
|
Card::upsert($values, 'sim', true);
|
|
|
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextMicrotime);
|
|
|
|
if ($page * $this->limit >= $total) {
|
|
break;
|
|
}
|
|
|
|
$page++;
|
|
}
|
|
|
|
app(CardRepository::class)->forgetCached();
|
|
}
|
|
}
|