getDay(); $this->line('开始' . $this->description . '#:' . $day); $startMicrotime = new UTCDateTime($day->copy()->startOfDay()); $endMicrotime = new UTCDateTime($day->copy()->endOfDay()); if ($this->option('month')) { $startMicrotime = new UTCDateTime($day->copy()->startOfMonth()); } $query = TblCard::where('pNo', 'No00000000768') ->where('bNo', 'exists', true) ->where('saDate', 'exists', true) ->where('saDate', '>=', $startMicrotime) ->where('saDate', '<=', $endMicrotime); $total = $query->count(); $this->line('待同步条数:' . $total); $page = 1; while ($total) { echo '.'; 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'] ? (string) Carbon::createFromTimestampMs(strval($item['saDate'])) : null, ]; })->all(); $exists = Card::select('sim')->whereIn('sim', array_pluck($array, 'sim')) ->whereNotNull('virtual_activated_at')->get()->pluck('sim')->toArray(); foreach ($array as $key => $value) { if (in_array($value['sim'], $exists)) { unset($array); } } if (!empty($array)) { $array = array_values($array); $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(); } protected function getDay() { if ($day = $this->argument('day')) { if (!preg_match('/\d{4}-\d{1,2}-\d{1,2}/', $day)) { throw new \App\Exceptions\InvalidArgumentException('请输入正确的日期 #示例: 2018-10-01'); } return Carbon::parse($day)->startOfDay(); } return Carbon::yesterday()->startOfDay(); } protected function getOptions() { return [ ['--month', null, InputOption::VALUE_NONE, '首次同步按月同步'], ]; } protected function getArguments() { return [ ['day', InputArgument::OPTIONAL, '要同步的数据日期,默认昨天 #示例: 2018-10-01'], ]; } }