188 lines
7.9 KiB
PHP
188 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Repositories\JushuitanShopPromotionFeeMirrorRepository;
|
|
use Tests\TestCase;
|
|
|
|
class JushuitanShopPromotionFeeMirrorRepositoryTest extends TestCase
|
|
{
|
|
private JushuitanShopPromotionFeeMirrorRepository $repository;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
$this->createTables();
|
|
$this->repository = new JushuitanShopPromotionFeeMirrorRepository(app('db'));
|
|
}
|
|
|
|
public function test_it_aggregates_active_shop_fees_by_month_and_uses_current_shop_names(): void
|
|
{
|
|
DB::connection('business_mirror')->table('ops_jst_shop_info')->insert([
|
|
['Id' => 1, 'shop_id' => 101, 'shop_name' => '天猫-甲店'],
|
|
['Id' => 2, 'shop_id' => 102, 'shop_name' => '京东-乙店'],
|
|
]);
|
|
DB::connection('business_mirror')->table(JushuitanShopPromotionFeeMirrorRepository::TABLE)->insert([
|
|
$this->row('P-1', 101, '天猫-旧名', '2026-11-01', 100.20, 0),
|
|
$this->row('P-2', 101, '天猫-旧名', '2026-11-02', 0.80, 0),
|
|
$this->row('P-3', 102, '旧店', '2026-11-03', 200.00, 0),
|
|
$this->row('P-4', 101, '天猫-旧名', '2026-11-04', 50.00, 1),
|
|
$this->row('P-5', 102, '旧店', '2026-12-01', 9.99, 0),
|
|
]);
|
|
$this->markInitialized();
|
|
|
|
$this->assertSame('ready', $this->repository->syncStatus()['code']);
|
|
$this->assertSame(101.0, $this->repository->monthlyStoreTotals(['2026-11'])['2026-11']['天猫-甲店']);
|
|
$this->assertSame(200.0, $this->repository->monthlyStoreTotals(['2026-11'])['2026-11']['京东-乙店']);
|
|
$this->assertArrayNotHasKey('2026-12', $this->repository->monthlyStoreTotals(['2026-11']));
|
|
}
|
|
|
|
public function test_upsert_uses_source_primary_key_and_updates_deleted_flag(): void
|
|
{
|
|
$this->assertSame(1, $this->repository->upsertRecords([
|
|
$this->mirrorRecord('P-1', 101, 10.00, 0),
|
|
]));
|
|
$this->assertSame(1, $this->repository->upsertRecords([
|
|
$this->mirrorRecord('P-1', 101, 12.50, 1),
|
|
]));
|
|
|
|
$row = DB::connection('business_mirror')
|
|
->table(JushuitanShopPromotionFeeMirrorRepository::TABLE)
|
|
->where('source_record_key', 'P-1')
|
|
->first();
|
|
$this->assertSame(1, DB::connection('business_mirror')
|
|
->table(JushuitanShopPromotionFeeMirrorRepository::TABLE)
|
|
->count());
|
|
$this->assertSame(12.5, (float) $row->amount);
|
|
$this->assertSame(1, (int) $row->del_flag);
|
|
$this->assertSame('jushuitan_open_api', $row->source_key);
|
|
$this->assertSame(64, strlen((string) $row->row_hash));
|
|
}
|
|
|
|
private function markInitialized(): void
|
|
{
|
|
DB::connection('business_mirror')->table(JushuitanShopPromotionFeeMirrorRepository::STATE_TABLE)->insert([
|
|
'dataset_key' => JushuitanShopPromotionFeeMirrorRepository::DATASET_KEY,
|
|
'source_key' => 'jushuitan_open_api',
|
|
'source_object' => 'shop-promotion-fee',
|
|
'target_table' => JushuitanShopPromotionFeeMirrorRepository::TABLE,
|
|
'status' => 'succeeded',
|
|
'initial_completed_at' => '2026-12-01 10:00:00',
|
|
'last_attempted_at' => '2026-12-01 10:00:00',
|
|
'last_succeeded_at' => '2026-12-01 10:00:00',
|
|
'mirror_row_count' => 5,
|
|
'created_at' => '2026-12-01 10:00:00',
|
|
'updated_at' => '2026-12-01 10:00:00',
|
|
]);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function row(
|
|
string $key,
|
|
int $shopId,
|
|
string $shopName,
|
|
string $feeDate,
|
|
float $amount,
|
|
int $deleted,
|
|
): array {
|
|
return [
|
|
'source_record_key' => $key,
|
|
'shop_id' => $shopId,
|
|
'shop_name' => $shopName,
|
|
'fee_date' => $feeDate,
|
|
'fee_time' => $feeDate . ' 08:00:00',
|
|
'amount' => $amount,
|
|
'del_flag' => $deleted,
|
|
'synced_at' => '2026-12-01 03:20:00',
|
|
];
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function mirrorRecord(string $key, int $shopId, float $amount, int $deleted): array
|
|
{
|
|
return [
|
|
'source_record_key' => $key,
|
|
'auto_id' => 1,
|
|
'shop_id' => $shopId,
|
|
'shop_name' => '天猫-甲店',
|
|
'fee_date' => '2026-11-01',
|
|
'fee_time' => '2026-11-01 08:00:00',
|
|
'fee_name' => '万相台无界-现金收支',
|
|
'csg_code' => 'A',
|
|
'csg_name' => '推广费',
|
|
'channel' => 'TAOBAO',
|
|
'description' => null,
|
|
'journal_type_name' => null,
|
|
'account_code' => null,
|
|
'amount' => $amount,
|
|
'oc_amount' => $amount,
|
|
'currency' => 'CNY',
|
|
'del_flag' => $deleted,
|
|
];
|
|
}
|
|
|
|
private function createTables(): void
|
|
{
|
|
$schema = Schema::connection('business_mirror');
|
|
$schema->create('ops_jst_shop_info', function (Blueprint $table): void {
|
|
$table->unsignedBigInteger('Id');
|
|
$table->unsignedBigInteger('shop_id');
|
|
$table->string('shop_name');
|
|
});
|
|
$schema->create(JushuitanShopPromotionFeeMirrorRepository::TABLE, function (Blueprint $table): void {
|
|
$table->string('source_key', 32)->default('jushuitan_open_api');
|
|
$table->string('source_record_key', 191)->primary();
|
|
$table->unsignedBigInteger('auto_id')->nullable();
|
|
$table->unsignedBigInteger('shop_id');
|
|
$table->string('shop_name', 255)->nullable();
|
|
$table->date('fee_date')->nullable();
|
|
$table->dateTime('fee_time')->nullable();
|
|
$table->string('fee_name', 191)->nullable();
|
|
$table->string('csg_code', 64)->nullable();
|
|
$table->string('csg_name', 191)->nullable();
|
|
$table->string('channel', 64)->nullable();
|
|
$table->text('description')->nullable();
|
|
$table->string('journal_type_name', 191)->nullable();
|
|
$table->string('account_code', 64)->nullable();
|
|
$table->decimal('amount', 20, 2)->default(0);
|
|
$table->decimal('oc_amount', 20, 2)->nullable();
|
|
$table->string('currency', 16)->nullable();
|
|
$table->unsignedTinyInteger('del_flag')->default(0);
|
|
$table->char('row_hash', 64)->nullable();
|
|
$table->timestamp('synced_at')->nullable();
|
|
$table->timestamps();
|
|
});
|
|
$schema->create(JushuitanShopPromotionFeeMirrorRepository::STATE_TABLE, function (Blueprint $table): void {
|
|
$table->string('dataset_key', 191)->primary();
|
|
$table->string('source_key', 64);
|
|
$table->string('source_object', 191);
|
|
$table->string('target_table', 64);
|
|
$table->string('status', 24);
|
|
$table->string('run_mode', 24)->nullable();
|
|
$table->dateTime('window_started_at')->nullable();
|
|
$table->dateTime('window_ends_at')->nullable();
|
|
$table->dateTime('initial_completed_at')->nullable();
|
|
$table->dateTime('last_full_succeeded_at')->nullable();
|
|
$table->dateTime('last_attempted_at')->nullable();
|
|
$table->dateTime('last_succeeded_at')->nullable();
|
|
$table->unsignedBigInteger('last_upserted_rows')->default(0);
|
|
$table->unsignedBigInteger('mirror_row_count')->default(0);
|
|
$table->string('last_error_code', 120)->nullable();
|
|
$table->json('high_watermark')->nullable();
|
|
$table->timestamps();
|
|
});
|
|
}
|
|
}
|