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) { $activate_at = $value['saDate'] ? $value['saDate']->toDateTime()->format('Y-m-d H:i:s') : null; $values[] = [ 'sim' => intval($value['cNo']), 'imsi' => $value['imsi'] ?? '', 'iccid' => $value['iccid'] ?? '', 'bloc_id' => $blocs[$value['comId']] ?? 0, 'carrier_operator' => self::$carrierOperators[$value['oType']] ?? 255, 'activate_at' => $activate_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'); 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); } }