聚合淘系支付宝、阿里巴巴、拼多多、京东京准通、抖音千川、得物推广和唯品会推广流水的支出数据;统一店铺名称、交易时间、备注和支出金额字段,并按日期直方图分页,避免整批读取大流水。
137 lines
5.8 KiB
PHP
137 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Mockery;
|
|
use Modules\EcommerceFinance\Repositories\PromotionMarketingExpenseRepository;
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
|
use Tests\TestCase;
|
|
|
|
class PromotionMarketingExpenseRepositoryTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Cache::flush();
|
|
}
|
|
|
|
public function test_it_merges_seven_sources_and_paginates_by_transaction_date(): void
|
|
{
|
|
$repository = $this->repository();
|
|
$repository->histogramRows = [
|
|
'tmall_spread_bills' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 10]],
|
|
'alibaba_bill' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 20]],
|
|
'pdd_spread_bills' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 30]],
|
|
'jd_jzt_finance_records' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 40]],
|
|
'dy_qianchuan_finance_transactions' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 50]],
|
|
'dw_ad_finance_records' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 60]],
|
|
'vip_marketingDailyReport' => [['report_date' => '2026-10-23', 'row_count' => 1, 'expense' => 70]],
|
|
];
|
|
$repository->detailRows = [
|
|
'tmall_spread_bills' => [$this->row('淘宝店', '2026-10-23 09:00:00', '淘宝备注', null, 10)],
|
|
'alibaba_bill' => [$this->row('阿里店', '2026-10-23', '智能营销方案', null, 20)],
|
|
'pdd_spread_bills' => [$this->row('拼多多店', '2026-10-23 09:30:00', '商品推广', '1', 30)],
|
|
'jd_jzt_finance_records' => [$this->row('京东店', '2026-10-23', '快车扣费', '推广费', 40)],
|
|
'dy_qianchuan_finance_transactions' => [$this->row('抖音店', '2026-10-23', '千川推广', null, 50)],
|
|
'dw_ad_finance_records' => [$this->row('得物店', '2026-10-23', '得物推服务', '现金', 60)],
|
|
'vip_marketingDailyReport' => [$this->row('唯品店', '2026-10-23', '推广费', null, 70)],
|
|
];
|
|
|
|
$result = $repository->paginate([
|
|
'start' => '2026-10-01',
|
|
'end' => '2026-10-31',
|
|
'store_name' => '',
|
|
], 2, 2);
|
|
|
|
$this->assertSame(7, $result['total']);
|
|
$this->assertSame(280.0, $result['summary']['expense']);
|
|
$this->assertSame(4, $result['last_page']);
|
|
$this->assertSame(['拼多多店', '京东店'], array_column($result['rows'], 'store_name'));
|
|
$this->assertSame('商品推广 · 现金', $result['rows'][0]['notes']);
|
|
$this->assertSame('快车扣费 · 推广费', $result['rows'][1]['notes']);
|
|
$this->assertSame([30.0, 40.0], array_column($result['rows'], 'expense'));
|
|
$this->assertTrue($repository->sawSql('tmall_spread_bills', "finType] = 'CREDIT'"));
|
|
$this->assertTrue($repository->sawSql('pdd_spread_bills', 'flowType] = 2'));
|
|
$this->assertTrue($repository->sawSql('jd_jzt_finance_records', "in_out_type] = 'OUT'"));
|
|
$this->assertTrue($repository->sawSql('dw_ad_finance_records', 'trade_amount_yuan'));
|
|
$this->assertTrue($repository->sawSql('vip_marketingDailyReport', 'accountName'));
|
|
}
|
|
|
|
public function test_store_options_are_merged_and_deduplicated(): void
|
|
{
|
|
$repository = $this->repository();
|
|
$repository->storeRows = [
|
|
'tmall_spread_bills' => [['store_name' => '乙店'], ['store_name' => '甲店']],
|
|
'alibaba_bill' => [['store_name' => '甲店']],
|
|
];
|
|
|
|
$this->assertSame(['乙店', '甲店'], $repository->storeOptions([
|
|
'start' => '2026-10-01',
|
|
'end' => '2026-10-31',
|
|
]));
|
|
}
|
|
|
|
private function repository(): PromotionMarketingExpenseRepository
|
|
{
|
|
return new class(Mockery::mock(UpstreamReadOnlyQueryFactory::class)) extends PromotionMarketingExpenseRepository {
|
|
public array $histogramRows = [];
|
|
|
|
public array $detailRows = [];
|
|
|
|
public array $storeRows = [];
|
|
|
|
public array $queries = [];
|
|
|
|
protected function select(string $connection, string $sql, array $bindings): array
|
|
{
|
|
$this->queries[] = compact('connection', 'sql', 'bindings');
|
|
foreach (array_merge(array_keys($this->histogramRows), array_keys($this->detailRows), array_keys($this->storeRows)) as $table) {
|
|
if (! str_contains($sql, $table)) {
|
|
continue;
|
|
}
|
|
if (str_contains($sql, 'GROUP BY')) {
|
|
return $this->histogramRows[$table] ?? [];
|
|
}
|
|
if (str_contains($sql, 'DISTINCT TOP (500)')) {
|
|
return $this->storeRows[$table] ?? [];
|
|
}
|
|
|
|
return $this->detailRows[$table] ?? [];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
public function sawSql(string $table, string $fragment): bool
|
|
{
|
|
foreach ($this->queries as $query) {
|
|
if (str_contains($query['sql'], $table) && str_contains($query['sql'], $fragment)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
};
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function row(
|
|
string $store,
|
|
string $occurredAt,
|
|
string $primary,
|
|
?string $secondary,
|
|
float $expense,
|
|
): array {
|
|
return [
|
|
'report_date' => substr($occurredAt, 0, 10),
|
|
'store_name' => $store,
|
|
'occurred_at' => $occurredAt,
|
|
'note_primary' => $primary,
|
|
'note_secondary' => $secondary,
|
|
'expense' => $expense,
|
|
];
|
|
}
|
|
}
|