['taobao_alipay_bill_rows', 'online_order_no', null], 'taobao-wechat' => ['taobao_wechat_bill_rows', 'online_order_no', null], 'pinduoduo' => ['pdd_settlement_bill_rows', 'online_order_no', null], 'jd-transactions' => ['jd_daily_bill_rows', 'effective_order_no', 'jd_daily_bill_periods'], 'jingxi' => ['jd_daily_bill_rows', 'effective_order_no', 'jd_daily_bill_periods'], 'douyin' => ['douyin_authoritative_bills', 'effective_order_no', 'douyin_authoritative_periods'], ]; public function __construct(private readonly DatabaseManager $databases, private readonly AccountLedgerRemarkMap $remarks) {} /** @return array{jd_through:?string,douyin_failed:bool,douyin_last_sync:?string} */ public function status(): array { $db = $this->db(); $end = $db->table('jd_daily_bill_periods')->max('period_end'); $state = $db->table('business_mirror_table_states') ->where('dataset_key', StoreReceivingAlipayBillMirrorRepository::DATASET)->first(); return [ 'jd_through' => $end ? date('Y-m-d', strtotime($end . ' -1 day')) : null, 'douyin_failed' => ($state->status ?? '') === 'failed', 'douyin_last_sync' => $state->last_succeeded_at ?? null, ]; } /** @param array> $orders @return \Generator> */ public function records(array $orders, string $from, string $to): \Generator { $db = $this->db(); foreach ($orders as $source => $numbers) { [$table, $column, $periodsTable] = self::SOURCES[$source]; if (! $db->getSchemaBuilder()->hasTable($table)) throw new RuntimeException('订单收入账单尚未接入。'); $periods = $periodsTable ? $db->table($periodsTable)->get() : collect(); foreach (array_chunk(array_values(array_unique($numbers)), 500) as $chunk) { $query = $db->table($table)->whereIn($column, $chunk) ->where('occurred_at', '>=', $from)->where('occurred_at', '<', $to); if ($periodsTable) $query->whereIn('generation', $periods->pluck('active_generation')->all()); if ($source === 'jingxi') $query->where('store_name', 'like', '%京喜%'); if ($source === 'jd-transactions') $query->where('store_name', 'not like', '%京喜%'); foreach ($this->bounded($query) as $record) { $raw = (array) $record; $row = isset($raw['row_data']) ? json_decode($raw['row_data'], true, 512, JSON_THROW_ON_ERROR) : $raw; $row['online_order_no'] = (string) $raw[$column]; $row['income'] = $raw['income_amount'] ?? $raw['income'] ?? '0.00'; $row['expense'] = $raw['expense_amount'] ?? $raw['expense'] ?? '0.00'; yield $this->decorate($row, $source); } // 校准过的月份只认活动代;其余日期沿用正式抖音页面的原始镜像。 if ($source === 'douyin') { $query = $db->table('ops_jst_original_bill_info')->where('channel', StoreReceivingDouyinBillMirrorRepository::SOURCE_CHANNEL) ->where(function (Builder $q) use ($chunk): void { $q->whereIn('so_id', $chunk)->orWhere(fn (Builder $fallback) => $fallback ->where(fn (Builder $blank) => $blank->whereNull('so_id')->orWhere('so_id', '')) ->whereIn('p_auto_id', $chunk)); })->where('fee_time', '>=', $from)->where('fee_time', '<', $to); foreach ($periods as $period) { $query->where(fn (Builder $q) => $q->where('fee_time', '<', $period->period_start)->orWhere('fee_time', '>=', $period->period_end)); } // 完全由校准账期覆盖时不触碰千万行原始表。 $covered = $periods->contains(fn ($p) => $p->period_start <= $from && $p->period_end >= $to); if (! $covered) foreach ($this->bounded($query) as $record) { yield $this->decorate([ 'record_code' => (string) $record->Id, 'online_order_no' => (string) ($record->so_id ?: $record->p_auto_id), 'bill_item' => $record->fee_name, 'occurred_at' => $record->fee_time, 'income' => $record->inout_type === '1' ? abs($record->amount) : 0, 'expense' => $record->inout_type === '-1' ? abs($record->amount) : 0, ], $source); } } } } } private function decorate(array $row, string $source): array { $row = $this->remarks->decorate($row, $source); $row['source'] = $source; return $row; } private function bounded(Builder $query): iterable { $db = $query->getConnection(); if ($db->getDriverName() !== 'mysql') return $query->get(); return $db->select('SET STATEMENT max_statement_time=20 FOR ' . $query->toSql(), $query->getBindings()); } private function db(): \Illuminate\Database\ConnectionInterface { return $this->databases->connection(config('upstream_mirrors.connection', 'business_mirror')); } }