127 lines
3.5 KiB
PHP
127 lines
3.5 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\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 FILENAME = 'app/command/sync-mongo.json';
|
|
const INIT_MICROTIME = 946656000000; // '2000-01-01 00:00:00'
|
|
|
|
public function handle()
|
|
{
|
|
$contents = $this->getFile();
|
|
$microtime = $contents['microtime'];
|
|
$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);
|
|
}
|
|
|
|
/**
|
|
* 获取文件内容
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function getFile()
|
|
{
|
|
$file = storage_path(self::FILENAME);
|
|
|
|
if (!file_exists($file)) {
|
|
$dir = dirname($file);
|
|
if (null !== $dir && !is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
|
|
$this->saveFile(0, self::INIT_MICROTIME);
|
|
}
|
|
|
|
$contents = file_get_contents($file);
|
|
|
|
return json_decode($contents, 256);
|
|
}
|
|
|
|
/**
|
|
* 写入文件
|
|
*
|
|
* @param integer $status 状态 1运行中 0运行结束
|
|
* @param integer $microtime 最后查询时间
|
|
* @return void
|
|
*/
|
|
protected function saveFile(int $status, int $microtime)
|
|
{
|
|
$file = storage_path(self::FILENAME);
|
|
|
|
$contents = json_encode([
|
|
'status' => $status,
|
|
'microtime' => $microtime,
|
|
]);
|
|
|
|
file_put_contents($file, $contents);
|
|
}
|
|
}
|