vd/app/Domains/Real/Commands/Sync/MongoSync.php
2018-12-07 19:03:07 +08:00

95 lines
2.2 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Card;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CardRepository;
class MongoSync extends Command
{
protected $name = 'real:sync-mongo';
protected $description = '同步卡基础信息数据';
protected static $carrierOperators = [1, 0, 2];
protected $limit = 10;
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'];
$this->saveFile(1, $microtime);
$utcDateTime = new UTCDateTime($microtime);
$query = DB::connection('mongo')->table('tblCard')->select(['cNo', 'iccid', 'imsi', 'comId', 'oType'])->where('isDel', '<>', 1)->where('sDate', '>', $utcDateTime);
$total = $query->count();
$page = 1;
while ($total) {
$res = $query->offset(($page - 1) * $limit)->limit($limit)->get();
dd($res);
if ($page * $limit >= $total) {
break;
}
}
$this->saveFile(0, $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);
}
}