get(self::CURSOR_KEY) ?: 946656000000; $nextMicrotime = intval(microtime(true) * 1000); $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', 'bNo', 'iccid', 'imsi', 'comId', 'oType', 'saDate', 'sDate']) ->where('oRDate', '>', $utcDateTime) ->where('sDate', 'exists', true) ->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; if ($activated_at && Carbon::parse($activated_at) < Carbon::parse('2000-01-01 00:00:00')) { $activated_at = 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, 'order_status' => intval(!empty($value['bNo'])), 'created_at' => $value['sDate'] ? $value['sDate']->toDateTime()->format('Y-m-d H:i:s') : null, 'updated_at' => date('Y-m-d H:i:s'), ]; } $builder = Card::query()->toBase(); $sql = $builder->getGrammar()->compileInsert($builder, $values); $sql .= 'on conflict (sim) do update set activated_at=excluded.activated_at, virtual_activated_at=COALESCE(cards.virtual_activated_at, excluded.activated_at), order_status=excluded.order_status'; $builder->connection->insert($sql, Arr::flatten($values, 1)); if ($page * $this->limit >= $total) { break; } $page++; } app(ConfigService::class)->set(self::CURSOR_KEY, intval($nextMicrotime)); app(CardRepository::class)->forgetCached(); } }