getDay(); $startMicrotime = new UTCDateTime($day->startOfDay()); $endMicrotime = new UTCDateTime($day->endOfDay()); $query = TblCard::where('pNo', 'No00000000768')->where('isDel', 0) ->where('saDate', 'exists', true) ->where('bNo', 'exists', true) ->where('saDate', '>=', $startMicrotime) ->where('saDate', '<=', $endMicrotime); $total = $query->count(); $this->line('待同步条数:'.$total); $page = 1; while ($total) { if (($page - 1) * $this->limit >= $total) { break; } $res = $query->select(['cNo', 'saDate']) ->forPage($page, $this->limit)->get(); $array = $res->map(function ($item) { return [ 'sim' => $item['cNo'], 'activated_at' => $item['saDate'] ? $item['saDate']->toDateTime()->format('Y-m-d H:i:s') : null, ]; })->all(); $table = app(Card::class)->getTable(); $as = 'as_table'; $reference = 'sim'; $updates = "activated_at={$as}.activated_at::timestamp, virtual_activated_at=COALESCE({$table}.activated_at, {$as}.activated_at::timestamp)::timestamp"; $parameters = collect($array)->map(function ($item) { return "({$item['sim']}, '{$item['activated_at']}')"; })->implode(', '); $from = "FROM (VALUES $parameters) AS {$as}(sim, activated_at)"; $where = "WHERE {$table}.{$reference} = {$as}.{$reference}::int8"; $sql = trim("UPDATE {$table} SET {$updates} {$from} {$where}"); $simArray = implode(',', array_pluck($array, 'sim')); DB::statement($sql); DB::statement("select fix_timelines('{{$simArray}}'::INT8[]);"); $page++; } app(CardRepository::class)->forgetCached(); app(OrderCardPartitionRepository::class)->forgetCached(); } }