2018-12-25 15:24:57 +08:00

86 lines
2.6 KiB
PHP

<?php
namespace App\Domains\Virtual\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\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
class LogSync extends Command
{
protected $name = 'virtual:sync-log';
protected $description = '同步VD订单数据';
protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2];
protected static $payChannels = [10 => 'wx', 11 => 'alipay', 12 => 'bank'];
protected $limit = 1000;
protected $filename = 'sync-log.json';
protected $initCursor = 946656000000; // '2000-01-01 00:00:00'
public function handle()
{
$contents = $this->getFile();
$microtime = $contents['cursor'];
$now_microtime = intval(microtime(true) * 1000);
$this->saveFile(1, $microtime);
$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);
$total = $query->count();
$this->line('待同步条数:'.$total);
$page = 1;
while ($total) {
echo $page . 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,
'created_at' => $value['sDate']->toDateTime()->format('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
}
Card::upsert($values, 'sim', true);
if ($page * $this->limit >= $total) {
break;
}
$page++;
}
app(CardRepository::class)->forgetCached();
$this->saveFile(0, $now_microtime);
}
}