diff --git a/modules/EcommerceFinance/Repositories/DouyinAuthoritativeBillRepository.php b/modules/EcommerceFinance/Repositories/DouyinAuthoritativeBillRepository.php index eb939975..b75b6a85 100644 --- a/modules/EcommerceFinance/Repositories/DouyinAuthoritativeBillRepository.php +++ b/modules/EcommerceFinance/Repositories/DouyinAuthoritativeBillRepository.php @@ -32,11 +32,12 @@ class DouyinAuthoritativeBillRepository $rows = $this->ordered($query, $sort, $direction)->forPage($page, $perPage)->get()->all(); $income = round((float) $summary->income, 2); $expense = round((float) $summary->expense, 2); + $state = $this->mirror->state($this->datasets->get(StoreReceivingAlipayBillMirrorRepository::DATASET)) ?? []; return [ 'data' => $this->mapRows($rows), 'total' => $total, 'current_page' => $page, 'last_page' => max(1, (int) ceil($total / $perPage)), 'per_page' => $perPage, 'summary' => ['count' => $total, 'income' => $income, 'expense' => $expense, 'balance' => round($income - $expense, 2)], - 'sync' => $this->mirror->state($this->datasets->get(StoreReceivingAlipayBillMirrorRepository::DATASET)) ?? [], + 'sync' => ['status' => $state['status'] ?? 'unknown', 'last_succeeded_at' => $state['last_succeeded_at'] ?? null], ]; } @@ -62,14 +63,14 @@ class DouyinAuthoritativeBillRepository public function filterOptions(): array { - $query = $this->query([]); + $query = $this->query([], true); return [ 'shops' => (clone $query)->whereNotNull('store_name')->where('store_name', '<>', '')->distinct()->orderBy('store_name')->pluck('store_name')->all(), 'bill_items' => (clone $query)->whereNotNull('bill_item')->where('bill_item', '<>', '')->distinct()->orderBy('bill_item')->pluck('bill_item')->all(), ]; } - private function query(array $filters): Builder + private function query(array $filters, bool $withOptions = false): Builder { $dataset = $this->datasets->get(StoreReceivingAlipayBillMirrorRepository::DATASET); if (! $this->mirror->ready($dataset) || ! $this->mirror->ready($this->datasets->get(StoreReceivingAlipayBillMirrorRepository::SHOP_DATASET))) { @@ -77,24 +78,29 @@ class DouyinAuthoritativeBillRepository } $db = $this->db(); $periods = $db->table('douyin_authoritative_periods')->get(); - $shops = $db->table('ops_jst_shop_info')->select('shop_id')->selectRaw('MAX(shop_name) AS shop_name')->groupBy('shop_id'); $legacy = $db->table($dataset->targetTable() . ' as b') - ->leftJoinSub($shops, 's', fn ($join) => $join->on('b.shop_id', '=', 's.shop_id')) ->where('b.channel', StoreReceivingDouyinBillMirrorRepository::SOURCE_CHANNEL); + if ($withOptions) { + $shops = $db->table('ops_jst_shop_info')->select('shop_id')->selectRaw('MAX(shop_name) AS shop_name')->groupBy('shop_id'); + $legacy->leftJoinSub($shops, 's', fn ($join) => $join->on('b.shop_id', '=', 's.shop_id')); + } foreach ($periods as $period) { $legacy->where(fn (Builder $q) => $q->where('b.fee_time', '<', $period->period_start) ->orWhere('b.fee_time', '>=', $period->period_end)->orWhereNull('b.fee_time')); } - $legacy->selectRaw("CAST(b.Id AS CHAR) AS record_code, s.shop_name AS store_name, - COALESCE(NULLIF(b.so_id, ''), b.p_auto_id) AS order_no, b.fee_name AS bill_item, b.fee_time AS occurred_at, + $legacy->selectRaw("CAST(b.Id AS CHAR) AS record_code, b.fee_time AS occurred_at, CASE WHEN b.inout_type = '1' THEN ABS(COALESCE(b.amount, 0)) ELSE 0 END AS income, CASE WHEN b.inout_type = '-1' THEN ABS(COALESCE(b.amount, 0)) ELSE 0 END AS expense, CASE WHEN b.inout_type = '1' THEN ABS(COALESCE(b.amount, 0)) WHEN b.inout_type = '-1' THEN -ABS(COALESCE(b.amount, 0)) ELSE 0 END AS balance, - b.Id AS source_id, NULL AS row_data"); + b.Id AS source_id, NULL AS generation"); $anchor = $db->table('douyin_authoritative_bills') ->whereIn('generation', $periods->pluck('active_generation')->all()) - ->select(['record_code', 'store_name', 'order_no', 'bill_item', 'occurred_at', 'income', 'expense', 'balance']) - ->selectRaw('NULL AS source_id, row_data'); + ->select(['record_code', 'occurred_at', 'income', 'expense', 'balance']) + ->selectRaw('NULL AS source_id, generation'); + if ($withOptions) { + $legacy->addSelect(['s.shop_name as store_name', 'b.fee_name as bill_item']); + $anchor->addSelect(['store_name', 'bill_item']); + } // Apply selective filters before UNION ALL to avoid materializing the full account for a monthly view. $this->filters($legacy, $filters, true); $this->filters($anchor, $filters, false); @@ -104,11 +110,17 @@ class DouyinAuthoritativeBillRepository private function filters(Builder $query, array $filters, bool $legacy): void { $date = $legacy ? 'b.fee_time' : 'occurred_at'; - $shop = $legacy ? 's.shop_name' : 'store_name'; $item = $legacy ? 'b.fee_name' : 'bill_item'; - foreach (['store_name' => $shop, 'bill_item' => $item] as $key => $column) { - if (($filters[$key] ?? '') !== '') $query->where($column, 'like', '%' . $this->escape($filters[$key]) . '%'); + if (($filters['store_name'] ?? '') !== '') { + $value = '%' . $this->escape($filters['store_name']) . '%'; + if ($legacy) { + $ids = $this->db()->table('ops_jst_shop_info')->where('shop_name', 'like', $value)->pluck('shop_id')->all(); + $query->whereIn('b.shop_id', $ids); + } else { + $query->where('store_name', 'like', $value); + } } + if (($filters['bill_item'] ?? '') !== '') $query->where($item, 'like', '%' . $this->escape($filters['bill_item']) . '%'); if (($filters['start'] ?? '') !== '') $query->where($date, '>=', $filters['start'] . ' 00:00:00'); if (($filters['end'] ?? '') !== '') $query->where($date, '<', date('Y-m-d', strtotime($filters['end'] . ' +1 day')) . ' 00:00:00'); if (($filters['keyword'] ?? '') !== '') { @@ -136,8 +148,14 @@ class DouyinAuthoritativeBillRepository $raw = $this->db()->table($this->datasets->get(StoreReceivingAlipayBillMirrorRepository::DATASET)->targetTable())->whereIn('Id', $ids)->get()->all(); foreach ($this->source->mapDouyinRows($raw) as $row) $legacy[$row['id']] = $row; } + $authoritative = []; + foreach (collect($rows)->filter(fn ($row) => $row->source_id === null)->groupBy('generation') as $generation => $group) { + $data = $this->db()->table('douyin_authoritative_bills')->where('generation', $generation) + ->whereIn('record_code', $group->pluck('record_code')->all())->get(['record_code', 'row_data']); + foreach ($data as $row) $authoritative[$generation . ':' . $row->record_code] = json_decode($row->row_data, true, 512, JSON_THROW_ON_ERROR); + } return array_map(fn ($row) => $row->source_id === null - ? json_decode($row->row_data, true, 512, JSON_THROW_ON_ERROR) + ? $authoritative[$row->generation . ':' . $row->record_code] : $legacy[(string) $row->source_id], $rows); }