CaiWuXiTong/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php
renshen 885961084e 流程统计:接入店铺申请流程(开店/变更/关店)共享镜像
数据源:jiansun.dbo.fys_shopInfo_flow(申请单明细,4572 行,含店铺/平台/负责人/利润中心/商标/变更原因/关店原因等)
+ 中台库 dbo.flow_flow(按 flowId 关联审批状态与当前审批人),两个数据集已登记并完成首次全量。

- 新增 ShopInfoFlowMirrorRepository:左连接两套镜像,支持单据编号/申请人/申请日期/当前审批人/
  ERP店铺名称/店铺运营负责人/利润中心/商标号等文本筛选,流程类型、审批状态、平台为可搜索多选。
- 审批状态文案取自 OA 前端字典:0暂存/1流程中/2拒绝/3完成/4撤回/5待签署/6已签署/7待审批;
  流程类型:8店铺开店/9店铺变更/10店铺关店/12主体注销自动流程。
- 页面按目录表渲染(查询条件、筛选字段、导出),列与外部店铺申请页对齐。
- TableRegistry 登记为只读上游页,镜像未就绪时页面给提示而不是报错。
2026-10-08 14:38:16 +08:00

138 lines
7.2 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\GenericBusinessMirrorRepository;
use Modules\EcommerceFinance\Repositories\ShopInfoFlowMirrorRepository;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Tests\TestCase;
class ShopInfoFlowMirrorRepositoryTest extends TestCase
{
private ShopInfoFlowMirrorRepository $repository;
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
'upstream_mirrors.datasets' => [
ShopInfoFlowMirrorRepository::FORM_DATASET => [
'source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'fys_shopInfo_flow',
'target' => 'shop_info_flow_form', 'keys' => ['Id'],
],
ShopInfoFlowMirrorRepository::FLOW_DATASET => [
'source_key' => 'js_data_middle_platform', 'schema' => 'dbo', 'object' => 'flow_flow',
'target' => 'oa_flow_flow', 'keys' => ['Id'],
],
],
]);
DB::purge('business_mirror');
$schema = Schema::connection('business_mirror');
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
$table->string('dataset_key')->primary();
$table->dateTime('initial_completed_at')->nullable();
});
$schema->create('shop_info_flow_form', function (Blueprint $table): void {
foreach ([
'Id', 'flowId', 'flowType', 'applyUser', 'applyTime', 'applyDepartment', 'remark', 'platform',
'responsibleDepartment', 'shopHead', 'customerManager', 'shopType', 'shopState', 'shopName',
'erpShopName', 'trademarkNumber', 'unifiedSocialCreditCode', 'informationChangeType',
'changeReason', 'changeSuccessful', 'closeReason', 'expectedClosingTime', 'refundType',
'refundAmount', 'refundIssue',
] as $column) {
$table->string($column)->nullable();
}
});
$schema->create('oa_flow_flow', function (Blueprint $table): void {
foreach (['Id', 'flowId', 'flowType', 'flowStatus', 'subject', 'approver', 'updateTime'] as $column) {
$table->string($column)->nullable();
}
});
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
'dataset_key' => ShopInfoFlowMirrorRepository::FORM_DATASET,
'initial_completed_at' => '2026-10-08 10:00:00',
]);
// 三单:开店(完成)、变更(流程中,有当前审批人)、关店(拒绝)。
$this->insertApplication(1, 'shp-1', 8, '拼多多', '甲店', '拼多多-甲店', '张三', '2026-10-01 09:00:00');
$this->insertApplication(2, 'shp-2', 9, '京东', '乙店', '京东-乙店', '李四', '2026-10-02 09:00:00');
$this->insertApplication(3, 'shp-3', 10, '抖音小店', '丙店', '抖音-丙店', '王五', '2026-10-03 09:00:00');
DB::connection('business_mirror')->table('oa_flow_flow')->insert([
['Id' => 1, 'flowId' => 'shp-1', 'flowType' => 8, 'flowStatus' => 3, 'subject' => '[店铺信息-开店流程]', 'approver' => '', 'updateTime' => '2026-10-01 10:00:00'],
['Id' => 2, 'flowId' => 'shp-2', 'flowType' => 9, 'flowStatus' => 1, 'subject' => '[店铺信息-变更流程]', 'approver' => '审核人A', 'updateTime' => '2026-10-02 10:00:00'],
['Id' => 3, 'flowId' => 'shp-3', 'flowType' => 10, 'flowStatus' => 2, 'subject' => '[店铺信息-关店流程]', 'approver' => '', 'updateTime' => '2026-10-03 10:00:00'],
]);
$this->repository = new ShopInfoFlowMirrorRepository(
new BusinessMirrorDatasetRegistry(),
new GenericBusinessMirrorRepository(app('db')),
);
}
private function insertApplication(int $id, string $flowId, int $flowType, string $platform, string $shop, string $erpShop, string $user, string $applyTime): void
{
DB::connection('business_mirror')->table('shop_info_flow_form')->insert([
'Id' => $id, 'flowId' => $flowId, 'flowType' => $flowType, 'platform' => $platform,
'shopName' => $shop, 'erpShopName' => $erpShop, 'applyUser' => $user, 'applyTime' => $applyTime,
'shopHead' => $user, 'responsibleDepartment' => '运营中心A',
]);
}
public function test_it_lists_applications_with_oa_approval_state(): void
{
$result = $this->repository->paginate(1, 10);
$this->assertSame(3, $result['total']);
$rows = collect($result['data'])->keyBy('flow_id');
$this->assertSame('店铺开店', $rows['shp-1']['flow_type']);
$this->assertSame('完成', $rows['shp-1']['approval_status']);
$this->assertSame('店铺变更', $rows['shp-2']['flow_type']);
$this->assertSame('流程中', $rows['shp-2']['approval_status']);
$this->assertSame('审核人A', $rows['shp-2']['current_approver']);
$this->assertSame('店铺关店', $rows['shp-3']['flow_type']);
$this->assertSame('拒绝', $rows['shp-3']['approval_status']);
// 默认按申请时间倒序。
$this->assertSame(['shp-3', 'shp-2', 'shp-1'], array_column($result['data'], 'flow_id'));
}
public function test_it_filters_by_type_status_platform_and_dates(): void
{
$this->assertSame(1, $this->repository->paginate(1, 10, ['flow_type' => ['店铺开店']])['total']);
$this->assertSame(
2,
$this->repository->paginate(1, 10, ['flow_type' => ['店铺开店', '店铺变更']])['total'],
);
$this->assertSame(1, $this->repository->paginate(1, 10, ['approval_status' => ['流程中']])['total']);
$this->assertSame(2, $this->repository->paginate(1, 10, ['platform' => ['拼多多', '京东']])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['apply_from' => '2026-10-03'])['total']);
$this->assertSame(2, $this->repository->paginate(1, 10, ['apply_to' => '2026-10-02'])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['apply_user' => '李四'])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['erp_shop_name' => '丙店'])['total']);
$this->assertSame(0, $this->repository->paginate(1, 10, ['flow_id' => '不存在'])['total']);
}
public function test_missing_oa_row_leaves_approval_state_blank(): void
{
DB::connection('business_mirror')->table('shop_info_flow_form')->insert([
'Id' => 4, 'flowId' => 'shp-4', 'flowType' => 8, 'platform' => '天猫',
'shopName' => '丁店', 'applyUser' => '赵六', 'applyTime' => '2026-10-04 09:00:00',
]);
$rows = collect($this->repository->paginate(1, 10)['data'])->keyBy('flow_id');
$this->assertSame('', $rows['shp-4']['approval_status']);
$this->assertSame('', $rows['shp-4']['current_approver']);
}
}