diff --git a/app/Console/Commands/Concerns/QueuesBillPerformanceResync.php b/app/Console/Commands/Concerns/QueuesBillPerformanceResync.php index 358a76eb..a2890f9c 100644 --- a/app/Console/Commands/Concerns/QueuesBillPerformanceResync.php +++ b/app/Console/Commands/Concerns/QueuesBillPerformanceResync.php @@ -2,6 +2,7 @@ namespace App\Console\Commands\Concerns; +use Illuminate\Support\Carbon; use Modules\EcommerceFinance\Services\BillPerformanceResyncDispatcher; use Throwable; @@ -11,12 +12,17 @@ trait QueuesBillPerformanceResync BillPerformanceResyncDispatcher $dispatcher, string $platform, array $result, + bool $periodEndExclusive = false, ): bool { try { + $periodEnd = (string) ($result['period_end'] ?? ''); + if ($periodEndExclusive && $periodEnd !== '') { + $periodEnd = Carbon::parse($periodEnd)->subDay()->toDateString(); + } $queued = $dispatcher->dispatch( $platform, (string) ($result['period_start'] ?? ''), - (string) ($result['period_end'] ?? ''), + $periodEnd, ); } catch (Throwable $exception) { report($exception); diff --git a/app/Console/Commands/ImportDouyinAuthoritativeBills.php b/app/Console/Commands/ImportDouyinAuthoritativeBills.php index c096c2b2..d74fe9aa 100644 --- a/app/Console/Commands/ImportDouyinAuthoritativeBills.php +++ b/app/Console/Commands/ImportDouyinAuthoritativeBills.php @@ -2,12 +2,16 @@ namespace App\Console\Commands; +use App\Console\Commands\Concerns\QueuesBillPerformanceResync; use Illuminate\Console\Command; +use Modules\EcommerceFinance\Services\BillPerformanceResyncDispatcher; use Modules\EcommerceFinance\Services\DouyinAuthoritativeBillImporter; use Throwable; class ImportDouyinAuthoritativeBills extends Command { + use QueuesBillPerformanceResync; + protected $signature = 'business-mirror:import-douyin-authoritative {path : 胜算原始明细 UTF-8 CSV} {month : 所属月份 YYYY-MM} {--count= : 核对笔数} {--income= : 核对收入合计} {--expense= : 核对支出合计} @@ -15,7 +19,10 @@ class ImportDouyinAuthoritativeBills extends Command protected $description = '校验并原子启用指定月份或月初至截止日的胜算抖音明细,保留旧版本用于回滚'; - public function handle(DouyinAuthoritativeBillImporter $importer): int + public function handle( + DouyinAuthoritativeBillImporter $importer, + BillPerformanceResyncDispatcher $performanceResync, + ): int { try { $result = $importer->import((string) $this->argument('path'), (string) $this->argument('month'), @@ -25,6 +32,10 @@ class ImportDouyinAuthoritativeBills extends Command return self::FAILURE; } $this->info('胜算抖音明细已启用,范围:[' . $result['period_start'] . ', ' . $result['period_end'] . '),笔数:' . $result['row_count']); + if (! $this->queueBillPerformanceResync($performanceResync, 'douyin', $result, true)) { + return self::FAILURE; + } + return self::SUCCESS; } } diff --git a/app/Console/Commands/ImportJdDailyBills.php b/app/Console/Commands/ImportJdDailyBills.php index 3b0ccb3c..15d13591 100644 --- a/app/Console/Commands/ImportJdDailyBills.php +++ b/app/Console/Commands/ImportJdDailyBills.php @@ -2,20 +2,27 @@ namespace App\Console\Commands; +use App\Console\Commands\Concerns\QueuesBillPerformanceResync; use Illuminate\Console\Command; use Illuminate\Support\Facades\Cache; +use Modules\EcommerceFinance\Services\BillPerformanceResyncDispatcher; use Modules\EcommerceFinance\Services\JdDailyBillImporter; use Throwable; class ImportJdDailyBills extends Command { + use QueuesBillPerformanceResync; + protected $signature = 'business-mirror:import-jd-daily {path : 胜算京东日账单指定月份月初至截止日期 UTF-8 CSV} {month : YYYY-MM} {--through= : 截止日期 YYYY-MM-DD,默认月末} {--count= : 核对笔数} {--income= : 核对收入} {--expense= : 核对支出}'; protected $description = '核对并启用胜算京东日账单月份镜像,保留旧版本'; - public function handle(JdDailyBillImporter $importer): int + public function handle( + JdDailyBillImporter $importer, + BillPerformanceResyncDispatcher $performanceResync, + ): int { $lock = Cache::lock('jd-daily-bill-import', 21600); if (! $lock->get()) { @@ -26,7 +33,20 @@ class ImportJdDailyBills extends Command $result = $importer->import((string) $this->argument('path'), (string) $this->argument('month'), (int) $this->option('count'), (string) $this->option('income'), (string) $this->option('expense'), $this->option('through')); $this->info('京东日账单已启用,笔数:' . $result['row_count']); - return self::SUCCESS; + $queuedJd = $this->queueBillPerformanceResync( + $performanceResync, + 'jd-transactions', + $result, + true, + ); + $queuedJingxi = $this->queueBillPerformanceResync( + $performanceResync, + 'jingxi', + $result, + true, + ); + + return $queuedJd && $queuedJingxi ? self::SUCCESS : self::FAILURE; } catch (Throwable $e) { $this->error('京东日账单未启用,保留上一版本。' . ($e instanceof \InvalidArgumentException ? $e->getMessage() : '请检查文件、重复记录及镜像连接。')); return self::FAILURE; diff --git a/modules/EcommerceFinance/Services/BillPerformanceResyncDispatcher.php b/modules/EcommerceFinance/Services/BillPerformanceResyncDispatcher.php index 84366c7e..00aeb68e 100644 --- a/modules/EcommerceFinance/Services/BillPerformanceResyncDispatcher.php +++ b/modules/EcommerceFinance/Services/BillPerformanceResyncDispatcher.php @@ -16,6 +16,9 @@ class BillPerformanceResyncDispatcher 'taobao-wechat' => '淘系微信', 'pinduoduo' => '拼多多', 'dewu' => '得物', + 'jd-transactions' => '京东账单', + 'jingxi' => '京喜账单', + 'douyin' => '抖音', ]; public function __construct( diff --git a/tests/Feature/Console/BillImportPerformanceResyncTest.php b/tests/Feature/Console/BillImportPerformanceResyncTest.php index 106fe85e..fa43153f 100644 --- a/tests/Feature/Console/BillImportPerformanceResyncTest.php +++ b/tests/Feature/Console/BillImportPerformanceResyncTest.php @@ -5,6 +5,8 @@ namespace Tests\Feature\Console; use Mockery; use Modules\EcommerceFinance\Services\BillPerformanceResyncDispatcher; use Modules\EcommerceFinance\Services\DewuSettlementBillImporter; +use Modules\EcommerceFinance\Services\DouyinAuthoritativeBillImporter; +use Modules\EcommerceFinance\Services\JdDailyBillImporter; use Modules\EcommerceFinance\Services\PinduoduoSettlementBillImporter; use Modules\EcommerceFinance\Services\TaobaoAlipaySettlementBillImporter; use Modules\EcommerceFinance\Services\TaobaoWechatSettlementBillImporter; @@ -13,6 +15,72 @@ use Tests\TestCase; class BillImportPerformanceResyncTest extends TestCase { + public function test_jd_daily_import_queues_both_receipt_accounts_for_the_closed_month(): void + { + $importer = Mockery::mock(JdDailyBillImporter::class); + $importer->shouldReceive('import') + ->once() + ->with('jd.csv', '2026-10', 321, '100.00', '20.00', '2026-10-31') + ->andReturn([ + 'row_count' => 321, + 'period_start' => '2026-10-01', + 'period_end' => '2026-11-01', + ]); + + $resync = Mockery::mock(BillPerformanceResyncDispatcher::class); + foreach (['jd-transactions', 'jingxi'] as $platform) { + $resync->shouldReceive('dispatch') + ->once() + ->with($platform, '2026-10-01', '2026-10-31') + ->andReturn(['jobs' => 1, 'months' => ['2026-10']]); + } + + $this->app->instance(JdDailyBillImporter::class, $importer); + $this->app->instance(BillPerformanceResyncDispatcher::class, $resync); + + $this->artisan('business-mirror:import-jd-daily', [ + 'path' => 'jd.csv', + 'month' => '2026-10', + '--through' => '2026-10-31', + '--count' => 321, + '--income' => '100.00', + '--expense' => '20.00', + ])->expectsOutputToContain('业绩明细重算已排队:2026-10(新增 1 个任务)') + ->assertSuccessful(); + } + + public function test_douyin_import_queues_the_closed_month_without_enqueueing_next_month(): void + { + $importer = Mockery::mock(DouyinAuthoritativeBillImporter::class); + $importer->shouldReceive('import') + ->once() + ->with('douyin.csv', '2026-10', 10, '100.00', '20.00', '2026-10-31') + ->andReturn([ + 'row_count' => 10, + 'period_start' => '2026-10-01', + 'period_end' => '2026-11-01', + ]); + + $resync = Mockery::mock(BillPerformanceResyncDispatcher::class); + $resync->shouldReceive('dispatch') + ->once() + ->with('douyin', '2026-10-01', '2026-10-31') + ->andReturn(['jobs' => 1, 'months' => ['2026-10']]); + + $this->app->instance(DouyinAuthoritativeBillImporter::class, $importer); + $this->app->instance(BillPerformanceResyncDispatcher::class, $resync); + + $this->artisan('business-mirror:import-douyin-authoritative', [ + 'path' => 'douyin.csv', + 'month' => '2026-10', + '--through' => '2026-10-31', + '--count' => 10, + '--income' => '100.00', + '--expense' => '20.00', + ])->expectsOutputToContain('业绩明细重算已排队:2026-10(新增 1 个任务)') + ->assertSuccessful(); + } + /** @return iterable}> */ public static function importCommands(): iterable { diff --git a/tests/Unit/EcommerceFinance/BillPerformanceResyncDispatcherTest.php b/tests/Unit/EcommerceFinance/BillPerformanceResyncDispatcherTest.php index 4656dec1..ae5f62ae 100644 --- a/tests/Unit/EcommerceFinance/BillPerformanceResyncDispatcherTest.php +++ b/tests/Unit/EcommerceFinance/BillPerformanceResyncDispatcherTest.php @@ -9,6 +9,19 @@ use Tests\TestCase; class BillPerformanceResyncDispatcherTest extends TestCase { + public function test_every_supported_receipt_source_has_a_performance_platform_label(): void + { + $this->assertSame([ + 'taobao-alipay' => '淘系支付宝', + 'taobao-wechat' => '淘系微信', + 'pinduoduo' => '拼多多', + 'dewu' => '得物', + 'jd-transactions' => '京东账单', + 'jingxi' => '京喜账单', + 'douyin' => '抖音', + ], BillPerformanceResyncDispatcher::PLATFORM_LABELS); + } + public function test_it_queues_every_month_covered_by_an_imported_period(): void { $queue = Mockery::mock(AccountMappingResyncDispatcher::class);