From 561c42d830d4c5b6db8a62880b5ffe719788b7bc Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Sat, 3 Oct 2026 13:28:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=97=E9=93=BA=E4=B8=BB=E4=BD=93?= =?UTF-8?q?=E6=9C=9F=E9=97=B4=E4=B8=8E=E7=88=AC=E8=99=AB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 店铺维度报表按期初数据+系统主体+爬虫新主体生成主体期间,一行一期间,修复流程历史重复行 - 变更日当天归新主体、老主体失效显示变更日前一天;关店和其他非正常状态两列显示 - - 新增店铺维度报表逻辑配置页(R1-R8)、主体商标数据逻辑配置页和预警页面 - 爬虫店铺主体按税号归一,首次抓到的新主体写入期间变更表并自动生成新期间 - 店铺基础信息、商标展示按爬虫优先;新增爬虫数据质量检查与预警处理 --- app/Console/Commands/CheckCrawlerWarnings.php | 38 ++ .../Commands/SyncCrawlerShopSubjects.php | 259 ++++++++ .../Commands/SyncStoreReportMirror.php | 5 +- app/Http/Controllers/Business/Pages.php | 66 ++ config/business_data.php | 2 + config/business_navigation.php | 39 +- config/store_report_initial_periods.php | 587 ++++++++++++++++++ config/upstream_mirrors.php | 28 + ...00_create_business_data_warnings_table.php | 51 ++ ...store_report_shop_period_changes_table.php | 38 ++ deploy/local-init.sh | 27 +- docs/上游业务镜像清单.md | 29 + docs/业务数据分层存储.md | 2 +- .../BusinessInformationMirrorRepository.php | 229 ++++++- .../DataWarningMirrorRepository.php | 189 ++++++ .../Repositories/StoreReportRepository.php | 165 +++-- .../Services/CrawlerDataGuard.php | 241 +++++++ .../Services/DataLogicSettings.php | 105 ++++ .../Services/ShopEntityTrademarkLogic.php | 120 ++++ .../Services/StoreReportLogic.php | 83 +++ .../StoreReportMirrorSynchronizer.php | 98 ++- .../Services/StoreReportPeriodBuilder.php | 260 ++++++++ .../pages/partials/prototype-page.blade.php | 10 +- .../entity-trademark-data-logic.blade.php | 207 ++++++ .../store-report-data-logic.blade.php | 324 ++++++++++ .../trademark-management/warnings.blade.php | 267 ++++++++ .../pages/workspace/warnings.blade.php | 1 - .../components/layouts/admin/menu.blade.php | 4 +- routes/admin.php | 5 + tests/Feature/Business/ManagementCrudTest.php | 35 +- .../Console/StoreReportMirrorSyncTest.php | 69 ++ .../BusinessDataPlacementTest.php | 2 +- ...usinessInformationMirrorRepositoryTest.php | 43 ++ .../StoreReportRepositoryFilterTest.php | 72 +-- .../StoreReportRepositoryTest.php | 165 +++-- 35 files changed, 3626 insertions(+), 239 deletions(-) create mode 100644 app/Console/Commands/CheckCrawlerWarnings.php create mode 100644 app/Console/Commands/SyncCrawlerShopSubjects.php create mode 100644 config/store_report_initial_periods.php create mode 100644 database/migrations/2026_10_03_000000_create_business_data_warnings_table.php create mode 100644 database/migrations/2026_10_03_000100_create_store_report_shop_period_changes_table.php create mode 100644 modules/EcommerceFinance/Repositories/DataWarningMirrorRepository.php create mode 100644 modules/EcommerceFinance/Services/CrawlerDataGuard.php create mode 100644 modules/EcommerceFinance/Services/DataLogicSettings.php create mode 100644 modules/EcommerceFinance/Services/ShopEntityTrademarkLogic.php create mode 100644 modules/EcommerceFinance/Services/StoreReportLogic.php create mode 100644 modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php create mode 100644 resources/views/business/pages/trademark-management/entity-trademark-data-logic.blade.php create mode 100644 resources/views/business/pages/trademark-management/store-report-data-logic.blade.php create mode 100644 resources/views/business/pages/trademark-management/warnings.blade.php delete mode 100644 resources/views/business/pages/workspace/warnings.blade.php diff --git a/app/Console/Commands/CheckCrawlerWarnings.php b/app/Console/Commands/CheckCrawlerWarnings.php new file mode 100644 index 00000000..a2772e9d --- /dev/null +++ b/app/Console/Commands/CheckCrawlerWarnings.php @@ -0,0 +1,38 @@ +check(); + } catch (Throwable $exception) { + $this->error('预警检查失败:' . $exception->getMessage()); + + return self::FAILURE; + } + + if ($summary === []) { + $this->info('预警检查完成:没有发现需要处理的记录。'); + + return self::SUCCESS; + } + + $this->table( + ['预警类型', '本次检查命中'], + collect($summary)->map(fn (int $count, string $type): array => [$type, $count])->values()->all(), + ); + + return self::SUCCESS; + } +} diff --git a/app/Console/Commands/SyncCrawlerShopSubjects.php b/app/Console/Commands/SyncCrawlerShopSubjects.php new file mode 100644 index 00000000..f7763a31 --- /dev/null +++ b/app/Console/Commands/SyncCrawlerShopSubjects.php @@ -0,0 +1,259 @@ +getSchemaBuilder(); + + if (! $schema->hasTable('ops_crawl_shop_company')) { + $this->error('爬虫店铺主体镜像尚未完成首次同步,本次不生成期间变更。'); + + return self::FAILURE; + } + + if (! $schema->hasTable('store_report_shop_period_changes')) { + $this->error('期间变更表尚未迁移,请先执行数据库迁移。'); + + return self::FAILURE; + } + + $official = collect($connection->table('ops_fys_company_info') + ->select(['unifiedSocialCreditCode', 'businessLicenseName']) + ->get()) + ->keyBy(fn (object $row): string => trim((string) $row->unifiedSocialCreditCode)); + + $existing = $connection->table('store_report_shop_period_changes') + ->select(['erp_store_name', 'company_name', 'observed_on']) + ->orderBy('observed_on') + ->orderBy('id') + ->get(); + $latestChange = []; + $changesByStore = []; + foreach ($existing as $row) { + $name = trim((string) $row->erp_store_name); + $latestChange[$name] = (string) $row->company_name; + $changesByStore[$name][] = $row; + } + + // 已知主体基线:优先用期间构建器的当前期间(期初数据 + 系统主体 + 历史爬虫变更), + // 保证只为“真的发生变化”的店铺记录新期间。 + $baseline = $this->baselineCompanies($connection); + $flow = $this->flowCompanies($connection); + $dryRun = (bool) $this->option('dry-run'); + $created = 0; + $conflicts = 0; + $skipped = 0; + $newWarnings = []; + + foreach ($this->latestCrawlRows($connection) as $row) { + $storeName = trim((string) ($row->shop_name ?? '')); + if ($storeName === '') { + continue; + } + + $company = $this->resolveCompany($row, $official); + $observedOn = trim((string) ($row->crawl_date ?? '')); + if ($company === null || $observedOn === '') { + $skipped++; + continue; + } + + $known = $latestChange[$storeName] ?? ($baseline[$storeName] ?? null); + if ($known !== null && mb_strtolower(trim($known)) === mb_strtolower($company)) { + $skipped++; + continue; + } + + // 同一天流程表给出不同主体:以爬虫为准,同时留待人工复审。 + $flowCompany = $flow[$storeName][$observedOn] ?? null; + if ($flowCompany !== null && mb_strtolower(trim($flowCompany)) !== mb_strtolower($company)) { + $conflicts++; + $newWarnings[] = [ + 'warning_key' => 'subject-conflict:' . md5($storeName . '|' . $observedOn . '|' . $company), + 'warning_type' => CrawlerDataGuard::TYPE_SUBJECT_CONFLICT, + 'risk_level' => 'high', + 'related_object' => $storeName, + 'risk_description' => sprintf('店铺「%s」%s 流程记录主体为「%s」,爬虫抓取为「%s」,系统已按爬虫调整期间。', $storeName, $observedOn, $flowCompany, $company), + 'evidence' => sprintf('flow=%s,crawl=%s,crawl_date=%s', $flowCompany, $company, $observedOn), + 'impact' => '主体期间与收入归属', + 'trigger_rule' => 'R6', + 'due_at' => now()->addDays(3), + ]; + } + + $businessKey = hash('sha256', $storeName . '|' . $company . '|' . $observedOn); + if (! $dryRun) { + $connection->table('store_report_shop_period_changes')->updateOrInsert( + ['business_key' => $businessKey], + [ + 'erp_store_name' => $storeName, + 'company_name' => $company, + 'unified_social_credit_code' => trim((string) ($row->tax_no ?? '')) ?: null, + 'observed_on' => $observedOn, + 'source' => 'crawl_shop_company', + 'updated_at' => now(), + 'created_at' => now(), + ], + ); + } + + $latestChange[$storeName] = $company; + $created++; + $this->line(sprintf('%s:%s 起主体变更为 %s%s', $storeName, $observedOn, $company, $dryRun ? '(演练)' : '')); + } + + if (! $dryRun && $newWarnings !== []) { + foreach ($newWarnings as $warning) { + $warning['responsible_person'] = $guard->responsiblePersonFor((string) $warning['warning_type']); + $warnings->upsert($warning); + } + } + + $this->info(sprintf( + '%s:新增主体变更 %d 条,同日冲突预警 %d 条,跳过 %d 家店铺。', + $dryRun ? '演练完成' : '同步完成', + $created, + $conflicts, + $skipped, + )); + + return self::SUCCESS; + } + + /** + * 当前系统认可的主体:把期间构建器的当前期间(最后一条)作为比较基线。 + * + * @return array + */ + private function baselineCompanies($connection): array + { + if (! $connection->getSchemaBuilder()->hasTable('ops_fys_shop_info')) { + return []; + } + + $stores = $connection->table('ops_fys_shop_info') + ->select(['Id', 'erpShopName', 'unifiedSocialCreditCode']) + ->get() + ->map(static fn (object $row): array => (array) $row) + ->all(); + $companies = $connection->table('ops_fys_company_info') + ->select(['Id', 'unifiedSocialCreditCode', 'businessLicenseName']) + ->get() + ->map(static fn (object $row): array => (array) $row) + ->all(); + $byCreditCode = []; + foreach ($companies as $company) { + $code = trim((string) ($company['unifiedSocialCreditCode'] ?? '')); + if ($code !== '') { + $byCreditCode[$code] = (string) $company['businessLicenseName']; + } + } + foreach ($stores as &$store) { + $code = trim((string) ($store['unifiedSocialCreditCode'] ?? '')); + $store = [ + 'erp_store_name' => (string) ($store['erpShopName'] ?? ''), + 'company_name' => $byCreditCode[$code] ?? null, + 'system_company_name' => $byCreditCode[$code] ?? null, + ]; + } + unset($store); + + $periods = app(StoreReportPeriodBuilder::class)->build($stores); + $baseline = []; + foreach ($periods as $name => $list) { + $last = end($list); + $company = trim((string) ($last['company_name'] ?? '')); + if ($company !== '') { + $baseline[$name] = $company; + } + } + + return $baseline; + } + + /** @return list */ + private function latestCrawlRows($connection): array + { + return $connection->table('ops_crawl_shop_company') + ->orderBy('shop_name') + ->orderByDesc('crawl_date') + ->orderByDesc('id') + ->get() + ->groupBy(static fn (object $row): string => (string) $row->shop_name) + ->map(static fn ($rows): object => $rows->first()) + ->values() + ->all(); + } + + /** + * 主体名称以 tax_no 对应的工商主体名为准,保证与主体报表合并键一致; + * 工商资料缺失时回退爬虫抓到的名称。 + */ + private function resolveCompany(object $row, $official): ?string + { + $taxNo = trim((string) ($row->tax_no ?? '')); + if ($taxNo !== '' && $official->has($taxNo)) { + $name = trim((string) $official->get($taxNo)->businessLicenseName); + if ($name !== '') { + return $name; + } + } + + $name = trim((string) ($row->company_name ?? '')); + + return $name === '' ? null : $name; + } + + /** @return array> */ + private function flowCompanies($connection): array + { + if (! $connection->getSchemaBuilder()->hasTable('store_report_lifecycle_rows')) { + return []; + } + + $state = $connection->table('store_report_import_states') + ->where('dataset_key', 'store-report.store-lifecycles') + ->first(); + $generation = trim((string) ($state->active_generation ?? '')); + if ($generation === '') { + return []; + } + + $result = []; + foreach ($connection->table('store_report_lifecycle_rows') + ->where('generation', $generation) + ->select(['erp_store_name', 'company_name', 'effective_at']) + ->get() as $row) { + $name = trim((string) $row->erp_store_name); + $date = substr(trim((string) $row->effective_at), 0, 10); + if ($name === '' || $date === '') { + continue; + } + $result[$name][$date] = (string) $row->company_name; + } + + return $result; + } +} diff --git a/app/Console/Commands/SyncStoreReportMirror.php b/app/Console/Commands/SyncStoreReportMirror.php index e6ca9adf..ba82fb3a 100644 --- a/app/Console/Commands/SyncStoreReportMirror.php +++ b/app/Console/Commands/SyncStoreReportMirror.php @@ -38,9 +38,12 @@ class SyncStoreReportMirror extends Command } $this->info(sprintf( - '店铺报表镜像:生命周期 %s/%d 行,账单 %s/%d 行,日汇总 %s/%d 行。', + '店铺报表镜像:流程期间 %s/%d 行,主体变更 %s(检查 %d 店,新增 %d 条),账单 %s/%d 行,日汇总 %s/%d 行。', $result['lifecycles']['status'], $result['lifecycles']['source_rows'], + $result['period_changes']['status'], + $result['period_changes']['checked'], + $result['period_changes']['created'], $result['bills']['status'], $result['bills']['source_rows'], $result['daily']['status'], diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 5bbde4e4..a98dd9b4 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -26,6 +26,7 @@ use Illuminate\Database\QueryException; use Modules\EcommerceFinance\Exceptions\UpstreamSnapshotPendingException; use Modules\EcommerceFinance\Repositories\AlibabaBillRepository; use Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository; +use Modules\EcommerceFinance\Repositories\DataWarningMirrorRepository; use Modules\EcommerceFinance\Repositories\EdgeBandingRepository; use Modules\EcommerceFinance\Repositories\InventoryRepository; use Modules\EcommerceFinance\Repositories\InstallationDetailMirrorRepository; @@ -509,10 +510,75 @@ class Pages extends Controller 'supplier-payables-report' => $this->contentForSupplierPayablesReport(), 'payable-cloud-warehouse-vehicle-fee' => $this->contentForDingPayableWorkflows(), 'courier-surcharge' => $this->contentForCourierSurcharge(), + 'warnings' => $this->contentForWarnings(), default => null, }; } + /** + * 店铺主体、商标爬虫数据预警。列表和复审动作都只读写共享业务镜像库, + * 页面不直接访问上游。 + */ + protected function contentForWarnings(): array + { + $clean = static fn (mixed $value, int $length = 40): string => mb_substr(trim((string) $value), 0, $length); + $filters = [ + 'warning_type' => $clean(request('warning_type', '')), + 'risk_level' => $clean(request('risk_level', ''), 16), + 'review_status' => $clean(request('review_status', ''), 16), + 'warning_number' => $clean(request('warning_number', '')), + 'related_object' => $clean(request('related_object', '')), + 'responsible_person' => $clean(request('responsible_person', '')), + ]; + $page = max((int) request('page', 1), 1); + $repository = app(DataWarningMirrorRepository::class); + $payload = $repository->paginate($page, 50, $filters); + + return [ + 'view' => 'business.pages.trademark-management.warnings', + 'warningRows' => $payload['data'], + 'warningTotal' => $payload['total'], + 'warningPending' => $payload['pending'], + 'warningReady' => $repository->ready(), + 'warningFilters' => $filters, + ]; + } + + /** + * 预警人工复审。确认=系统按爬虫强制覆盖的结果正确;驳回=以流程数据为准, + * 由后续期间合并任务改回并重算收入归属。 + */ + public function reviewWarning(Request $request, int $warning) + { + $data = $request->validate([ + 'decision' => ['required', 'in:confirmed,rejected'], + 'responsible_person' => ['nullable', 'string', 'max:191'], + 'review_note' => ['nullable', 'string', 'max:500'], + ]); + + $repository = app(DataWarningMirrorRepository::class); + $record = $repository->find($warning); + abort_if($record === null, 404); + + $reviewer = (string) (user()->name ?? user()->email ?? '系统用户'); + $repository->review( + $warning, + (string) $data['decision'], + $reviewer, + $data['review_note'] ?? null, + $data['responsible_person'] ?? null, + ); + + $label = $data['decision'] === 'confirmed' ? '已确认' : '已驳回'; + $redirect = redirect() + ->route('business.pages.show', ['section' => 'trademark-management', 'page' => 'warnings']) + ->with('success', "预警 {$record['warning_number']} {$label}。"); + + return $request->expectsJson() + ? response()->json(['success' => true, 'message' => "预警 {$record['warning_number']} {$label}。"]) + : $redirect; + } + /** * 快递附加费报价维护。 * diff --git a/config/business_data.php b/config/business_data.php index 01a618bf..2b5a5644 100644 --- a/config/business_data.php +++ b/config/business_data.php @@ -47,6 +47,8 @@ return [ 'operations_replica.rpt.fys_companyInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '公司资料'], 'operations_replica.rpt.fys_shopInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '店铺资料/店铺报表'], 'operations_replica.rpt.fys_TrademarkInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '商标资料'], + 'operations_replica.rpt.crawl_shop_company' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '店铺基础信息/店铺主体期间'], + 'operations_replica.rpt.crawl_shop_trademark' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '店铺基础信息/店铺商标'], 'operations_replica.rpt.jst_sku' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '商品资料/售后费用'], 'operations_replica.rpt.jst_warehouseInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '售后费用'], 'operations_replica.rpt.jst_shopInfo' => ['role' => 'dimension', 'target_store' => 'mariadb', 'consumer' => '预留聚水潭店铺维表'], diff --git a/config/business_navigation.php b/config/business_navigation.php index 3536904c..4d91c534 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -195,6 +195,18 @@ return [ 'fields' => [], ], ], + [ + 'slug' => 'entity-trademark-data-logic', + 'title' => '店铺主体商标数据逻辑配置', + ], + [ + 'slug' => 'store-report-data-logic', + 'title' => '店铺维度报表逻辑配置', + ], + [ + 'slug' => 'warnings', + 'title' => '预警页面', + ], ], ], ], @@ -647,33 +659,6 @@ return [ ], ], ], - [ - 'slug' => 'warnings', - 'title' => '预警页面', - 'directory' => [ - 'icon' => 'warning', - 'status' => '尚未同步', - 'description' => '集中展示销量、税务、主体、人员、商标和续签风险,并可关联待办编号追踪处理结果。', - 'empty' => '预警数据尚未同步。完成规则引擎和预警来源接入后,这里会显示风险记录。', - 'fields' => [ - ['name' => 'warning_number', 'label' => '预警编号'], - ['name' => 'warning_type', 'label' => '预警类型'], - ['name' => 'risk_level', 'label' => '风险等级'], - ['name' => 'related_object', 'label' => '关联对象'], - ['name' => 'risk_description', 'label' => '风险描述'], - ['name' => 'impact_amount', 'label' => '影响金额', 'kind' => 'number'], - ['name' => 'trigger_rule', 'label' => '触发规则'], - ['name' => 'triggered_at', 'label' => '触发时间', 'kind' => 'date'], - ['name' => 'owner', 'label' => '责任人'], - ['name' => 'collaborator', 'label' => '协同人'], - ['name' => 'due_at', 'label' => '截止时间', 'kind' => 'date'], - ['name' => 'todo_number', 'label' => '待办编号'], - ['name' => 'status', 'label' => '处理状态'], - ['name' => 'evidence', 'label' => '处理证据'], - ['name' => 'closed_by_date', 'label' => '关闭人/日期'], - ], - ], - ], ], ], ]; diff --git a/config/store_report_initial_periods.php b/config/store_report_initial_periods.php new file mode 100644 index 00000000..a8965b32 --- /dev/null +++ b/config/store_report_initial_periods.php @@ -0,0 +1,587 @@ + '2026-07-01', + 'shops' => [ + '京东-万客达家具旗舰店' => [ + 'previous_company' => '常州微振家居有限公司', + 'company' => '武汉市朵莹米贸易有限公司', + 'changed_at' => '2026-07-27', + ], + '京东-亿家木家具旗舰店' => [ + 'previous_company' => '常州玺之橙家居有限公司', + 'company' => '株洲市森境商贸有限公司', + 'changed_at' => '2026-09-14', + ], + '京东-亿家达家居旗舰店' => [ + 'previous_company' => '常州州圣家居有限公司', + 'company' => '广州芯喻科技有限公司', + 'changed_at' => '2026-09-11', + ], + '京东-俚库节华专卖店' => [ + 'previous_company' => '常州淘之牧家居有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-10', + ], + '京东-全颐家具旗舰店' => [ + 'previous_company' => '常州酷积吉电子商务有限公司', + 'company' => '深圳市匠师科技有限公司', + 'changed_at' => '2026-07-30', + ], + '京东-励树旗舰店' => [ + 'previous_company' => '常州在木居家居有限公司', + 'company' => '珠海市澄澄成贸易有限公司', + 'changed_at' => '2026-08-05', + ], + '京东-半隅雅居旗舰店' => [ + 'previous_company' => '常州尚之荷电子商务有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-27', + ], + '京东-卓禾客厅家具旗舰店' => [ + 'previous_company' => '常州幂佳电子商务有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-08-25', + ], + '京东-卓禾桌旗舰店' => [ + 'previous_company' => '常州素柚电子商务有限公司', + 'company' => '武汉市焕独树科技有限公司', + 'changed_at' => '2026-09-11', + ], + '京东-墨尚思家具官方旗舰店' => [ + 'previous_company' => '常州米之信家居有限公司', + 'company' => '深圳市澄镜贸易有限公司', + 'changed_at' => '2026-08-05', + ], + '京东-宛初家具旗舰店' => [ + 'previous_company' => '常州迈之时家居有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-28', + ], + '京东-宜造旗舰店' => [ + 'previous_company' => '常州泰赫家居有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-24', + ], + '京东-左印卧室家具旗舰店' => [ + 'previous_company' => '常州格之简电子商务有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '京东-左印旗舰店' => [ + 'previous_company' => '常州正琳电子商务有限公司', + 'company' => '义乌市答馥贸易有限公司', + 'changed_at' => '2026-09-11', + ], + '京东-巷予家具旗舰店' => [ + 'previous_company' => '常州鹏冒家居有限公司', + 'company' => '株洲市晞飏盛商贸有限公司', + 'changed_at' => '2026-08-05', + ], + '京东-新卓禾旗舰店' => [ + 'previous_company' => '常州仁之云家居有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '京东-木本诗家具旗舰店' => [ + 'previous_company' => '常州瓦铭家居有限公司', + 'company' => '深圳市匠师科技有限公司', + 'changed_at' => '2026-08-03', + ], + '京东-未时旗舰店' => [ + 'previous_company' => '常州和之非家居有限公司', + 'company' => '义乌市答馥贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '京东-柯诺苏家具旗舰店' => [ + 'previous_company' => '常州旭致家居有限公司', + 'company' => '株洲市晞飏盛商贸有限公司', + 'changed_at' => '2026-08-05', + ], + '京东-橡野里家具小店' => [ + 'previous_company' => '武进区潞城木易家具店', + 'company' => '武汉市焕独树科技有限公司', + 'changed_at' => '2026-07-09', + ], + '京东-爱家猫' => [ + 'previous_company' => '常州峰麟电子商务有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '京东-爱木儿旗舰店' => [ + 'previous_company' => '常州泽之云家居有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-28', + ], + '京东-礼想木旗舰店' => [ + 'previous_company' => '常州歆之吉家居有限公司', + 'company' => '武汉市金逐玉贸易有限公司', + 'changed_at' => '2026-08-15', + ], + '京东-第一森林客厅家具旗舰店' => [ + 'previous_company' => '常州连音电子商务有限公司', + 'company' => '广州芯喻科技有限公司', + 'changed_at' => '2026-08-04', + ], + '京东-第一森林家具旗舰店' => [ + 'previous_company' => '常州旭可电子商务有限公司', + 'company' => '义乌市答馥贸易有限公司', + 'changed_at' => '2026-07-27', + ], + '京东-米客米家家具旗舰店' => [ + 'previous_company' => '常州欧鹰朗家居有限公司', + 'company' => '上海臻磊居电子商务有限公司', + 'changed_at' => '2026-09-10', + ], + '京东-米昊旗舰店' => [ + 'previous_company' => '常州州圣家居有限公司', + 'company' => '义乌市育旬贸易有限公司', + 'changed_at' => '2026-07-30', + ], + '京东-素成木旗舰店' => [ + 'previous_company' => '常州泱涵家居有限公司', + 'company' => '武汉市金逐玉贸易有限公司', + 'changed_at' => '2026-09-11', + ], + '京东-纪辰家具旗舰店' => [ + 'previous_company' => '常州念晶家居有限公司', + 'company' => '杭州照萍芋贸易有限公司', + 'changed_at' => '2026-09-14', + ], + '京东-贝壳家具旗舰店' => [ + 'previous_company' => '常州致圣家居有限公司', + 'company' => '常州研选供应链有限公司', + 'changed_at' => '2026-08-03', + ], + '京东-赛初家具旗舰店' => [ + 'previous_company' => '常州嘉之钰家居有限公司', + 'company' => '武汉市焕独树科技有限公司', + 'changed_at' => '2026-09-11', + ], + '京东-首升瓦舟迪专卖店' => [ + 'previous_company' => '常州绒灿家居有限公司', + 'company' => '株洲市棱斛盛商贸有限公司', + 'changed_at' => '2026-08-05', + ], + '抖音-乾廷润凯家具专卖店' => [ + 'previous_company' => '常州迈而昕家居有限公司', + 'company' => '武汉市焕独树科技有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-亿家达官方旗舰店' => [ + 'previous_company' => '常州格之简电子商务有限公司', + 'company' => '广州市椿风贸易有限公司', + 'changed_at' => '2026-07-22', + ], + '抖音-亿家达家居专卖店' => [ + 'previous_company' => '常州庆之图家居有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-08-24', + ], + '抖音-佰汇家具店' => [ + 'previous_company' => '常州巴宣家居有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-10', + ], + '抖音-凌木家具店' => [ + 'previous_company' => '常州马月家居有限公司', + 'company' => '武汉市焕独树科技有限公司', + 'changed_at' => '2026-07-17', + ], + '抖音-务原家具店' => [ + 'previous_company' => '常州格之麦家居有限公司', + 'company' => '深圳市宙桦奇贸易有限公司', + 'changed_at' => '2026-08-04', + ], + '抖音-勤松苏得荷家具专卖店' => [ + 'previous_company' => '常州米之信家居有限公司', + 'company' => '上海禄美佳商贸有限公司', + 'changed_at' => '2026-07-26', + ], + '抖音-吉友爱塑荣家居专卖店' => [ + 'previous_company' => '常州洁才家居有限公司', + 'company' => '深圳市鑫鑫向洋科技有限公司', + 'changed_at' => '2026-08-24', + ], + '抖音-和辉家具店' => [ + 'previous_company' => '常州阳之红家居有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-07', + ], + '抖音-常州科斯能家居有限公司' => [ + 'previous_company' => '常州俪能家居有限公司', + 'company' => '义乌市络翘贸易有限公司', + 'changed_at' => '2026-08-04', + ], + '抖音-德朗蒂家具店' => [ + 'previous_company' => '常州蓝瞳家居有限公司', + 'company' => '广州市墨拓贸易有限公司', + 'changed_at' => '2026-07-17', + ], + '抖音-星程家具店' => [ + 'previous_company' => '常州初之语家居有限公司', + 'company' => '株洲市晞飏盛商贸有限公司', + 'changed_at' => '2026-08-03', + ], + '抖音-林舸家具店' => [ + 'previous_company' => '常州迈之时家居有限公司', + 'company' => '珠海市澄澄成贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-柯诺苏来导专卖店' => [ + 'previous_company' => '常州瓦铭家居有限公司', + 'company' => '珠海市澄澄成贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-柯诺苏艾利华家具专卖店' => [ + 'previous_company' => '常州初之语家居有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-07', + ], + '抖音-樛木家具店' => [ + 'previous_company' => '常州绒美家居有限公司', + 'company' => '深圳市澄镜贸易有限公司', + 'changed_at' => '2026-08-03', + ], + '抖音-畅森家具店' => [ + 'previous_company' => '常州茶木家居有限公司', + 'company' => '义乌市答馥贸易有限公司', + 'changed_at' => '2026-08-10', + ], + '抖音-素屑家塑荣家具专卖店' => [ + 'previous_company' => '常州洁才家居有限公司', + 'company' => '珠海市澄澄成贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-纪辰欢而专卖店' => [ + 'previous_company' => '常州幂佳电子商务有限公司', + 'company' => '广州市鼎流拓科技有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-艺煦白墙专卖店' => [ + 'previous_company' => '常州梦彬家居有限公司', + 'company' => '深圳市澄镜贸易有限公司', + 'changed_at' => '2026-08-04', + ], + '抖音-蓝悦家具店' => [ + 'previous_company' => '常州初之语家居有限公司', + 'company' => '株洲市晞飏盛商贸有限公司', + 'changed_at' => '2026-08-03', + ], + '抖音-赛初家具旗舰店' => [ + 'previous_company' => '常州绒美家居有限公司', + 'company' => '义乌市育旬贸易有限公司', + 'changed_at' => '2026-08-04', + ], + '抖音-赛初莱夫特家具专卖店' => [ + 'previous_company' => '常州义之迈电子商务有限公司', + 'company' => '珠海市澄澄成贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '抖音-越堂家具店' => [ + 'previous_company' => '常州幂佳电子商务有限公司', + 'company' => '广州市鼎流拓科技有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-UCU官方旗舰店' => [ + 'previous_company' => '常州格之简电子商务有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-09-14', + ], + '拼多多-久伴吾家具旗舰店' => [ + 'previous_company' => '常州格之简电子商务有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-17', + ], + '拼多多-亿佳木住宅家具官方旗舰店' => [ + 'previous_company' => '常州洁秋家居有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-07-16', + ], + '拼多多-亿家木点好多专卖店' => [ + 'previous_company' => '常州欣之露电子商务有限公司', + 'company' => '上海臻潭寨电子商务有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-亿家达书房家具官方旗舰店' => [ + 'previous_company' => '常州连音电子商务有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-亿家达住宅家具官方旗舰店' => [ + 'previous_company' => '常州悠益电子商务有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-17', + ], + '拼多多-亿家达卧室家具官方旗舰店' => [ + 'previous_company' => '常州锋翎家居有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-亿家达官方旗舰店' => [ + 'previous_company' => '常州连音电子商务有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-16', + ], + '拼多多-亿家达实木家具官方旗舰店' => [ + 'previous_company' => '常州连音电子商务有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-亿家达客厅家具官方旗舰店' => [ + 'previous_company' => '常州锋翎家居有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-亿家达旗舰店' => [ + 'previous_company' => '常州锋翎家居有限公司', + 'company' => '青岛哲晖义商贸有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-亿家达桌椅官方旗舰店' => [ + 'previous_company' => '常州锋翎家居有限公司', + 'company' => '武汉市鼎新致远科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-优乐熊家具旗舰店' => [ + 'previous_company' => '常州旭可电子商务有限公司', + 'company' => '深圳市鑫鑫向洋科技有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-优乐猫家具旗舰店' => [ + 'previous_company' => '常州君之兰家居有限公司', + 'company' => '义乌市竺芽电子商务有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-全米家具旗舰店' => [ + 'previous_company' => '常州酷积吉电子商务有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-华斯乐畅专卖店' => [ + 'previous_company' => '常州云栀电子商务有限公司', + 'company' => '武汉市跨星河贸易有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-华斯予之福专卖店' => [ + 'previous_company' => '常州予之福家居有限公司', + 'company' => '上海臻潭寨电子商务有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-卓禾住宅家具旗舰店' => [ + 'previous_company' => '常州歆森电子商务有限公司', + 'company' => '安阳县峋庭电子商务有限公司', + 'changed_at' => '2026-08-14', + ], + '拼多多-卓禾家纺家具家装旗舰店' => [ + 'previous_company' => '常州庆之图家居有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-卓禾家纺家具旗舰店' => [ + 'previous_company' => '常州笛莫电子商务有限公司', + 'company' => '安阳县峋庭电子商务有限公司', + 'changed_at' => '2026-08-14', + ], + '拼多多-卓禾旗舰店' => [ + 'previous_company' => '常州庆之图家居有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-08-12', + ], + '拼多多-哈利木丫官方旗舰店' => [ + 'previous_company' => '常州旭可电子商务有限公司', + 'company' => '深圳市鑫鑫向洋科技有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-墨尚思家具旗舰店' => [ + 'previous_company' => '常州苡宝家居有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-宜卡木住宅家具旗舰店' => [ + 'previous_company' => '常州书心家居有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-08-24', + ], + '拼多多-宜自然住宅家具旗舰店' => [ + 'previous_company' => '常州笛莫电子商务有限公司', + 'company' => '深圳市澜祺科技有限公司', + 'changed_at' => '2026-07-16', + ], + '拼多多-宜造家具旗舰店' => [ + 'previous_company' => '常州州圣家居有限公司', + 'company' => '深圳市澄镜贸易有限公司', + 'changed_at' => '2026-07-16', + ], + '拼多多-屿林间家具旗舰店' => [ + 'previous_company' => '常州欣之露电子商务有限公司', + 'company' => '广州市墨拓贸易有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-左印家具旗舰店' => [ + 'previous_company' => '常州歆森电子商务有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-07-17', + ], + '拼多多-左昊家具旗舰店' => [ + 'previous_company' => '常州青号电子商务有限公司', + 'company' => '深圳市匠师科技有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-归来舍下住宅家具旗舰店' => [ + 'previous_company' => '常州凯之贝家居有限公司', + 'company' => '青岛铧胜靖商贸有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-惠木家特坤森专卖店' => [ + 'previous_company' => '常州冬之实家居有限公司', + 'company' => '义乌市竺芽电子商务有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-慕加家具旗舰店' => [ + 'previous_company' => '常州青号电子商务有限公司', + 'company' => '青岛哲晖义商贸有限公司', + 'changed_at' => '2026-09-14', + ], + '拼多多-星空夏日家纺家具家装官方旗舰店' => [ + 'previous_company' => '常州山之海家居有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-木小乐住宅家具旗舰店' => [ + 'previous_company' => '常州烟之雨家居有限公司', + 'company' => '青岛疆拓杰商贸有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-木庭乐家具官方旗舰店' => [ + 'previous_company' => '常州赫晟电子商务有限公司', + 'company' => '广州芯喻科技有限公司', + 'changed_at' => '2026-07-13', + ], + '拼多多-木野拾趣家具官方旗舰店' => [ + 'previous_company' => '常州笛莫电子商务有限公司', + 'company' => '深圳市鑫鑫向洋科技有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-橡野里家具旗舰店' => [ + 'previous_company' => '常州橙兮电子商务有限公司', + 'company' => '武汉市朵莹米贸易有限公司', + 'changed_at' => '2026-07-17', + ], + '拼多多-橡野里纳熙专卖店' => [ + 'previous_company' => '常州仁之云家居有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-欧意朗住宅家具旗舰店' => [ + 'previous_company' => '常州羽之间电子商务有限公司', + 'company' => '安阳县峋庭电子商务有限公司', + 'changed_at' => '2026-08-12', + ], + '拼多多-欧意朗办公家具旗舰店' => [ + 'previous_company' => '常州莱夫特家居有限公司', + 'company' => '安阳县峋庭电子商务有限公司', + 'changed_at' => '2026-08-12', + ], + '拼多多-欧意朗家具官方旗舰店' => [ + 'previous_company' => '常州森静电子商务有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-08-12', + ], + '拼多多-欧意朗生活家具旗舰店' => [ + 'previous_company' => '常州锋翎家居有限公司', + 'company' => '安阳县峋庭电子商务有限公司', + 'changed_at' => '2026-08-12', + ], + '拼多多-润万卓家具旗舰店' => [ + 'previous_company' => '常州平灿家居有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-24', + ], + '拼多多-润典达旗舰店' => [ + 'previous_company' => '常州橙兮电子商务有限公司', + 'company' => '上海禄美佳商贸有限公司', + 'changed_at' => '2026-07-16', + ], + '拼多多-满富油家具旗舰店' => [ + 'previous_company' => '常州皓熠电子商务有限公司', + 'company' => '深圳市宙桦奇贸易有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-爱木儿家具旗舰店' => [ + 'previous_company' => '常州艺之瑾家居有限公司', + 'company' => '南召县栎安商贸有限公司', + 'changed_at' => '2026-09-10', + ], + '拼多多-百里香晟陶专卖店' => [ + 'previous_company' => '常州晟陶电子商务有限公司', + 'company' => '青岛铧胜靖商贸有限公司', + 'changed_at' => '2026-09-14', + ], + '拼多多-礼想木家具旗舰店' => [ + 'previous_company' => '常州雅之荷电子商务有限公司', + 'company' => '武汉市朵莹米贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-第一森林旗舰店' => [ + 'previous_company' => '常州思温家居有限公司', + 'company' => '杭州照萍芋贸易有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-米昊家具旗舰店' => [ + 'previous_company' => '常州素柚电子商务有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-07-30', + ], + '拼多多-美栖家家具官方旗舰店' => [ + 'previous_company' => '常州州圣家居有限公司', + 'company' => '深圳市匠师科技有限公司', + 'changed_at' => '2026-07-14', + ], + '拼多多-花木涧家具旗舰店' => [ + 'previous_company' => '常州盈橡家居有限公司', + 'company' => '杭州背多爱贸易有限公司', + 'changed_at' => '2026-09-14', + ], + '拼多多-莫埃家具旗舰店' => [ + 'previous_company' => '常州昀禾家居有限公司', + 'company' => '青岛婳欣妍商贸有限公司', + 'changed_at' => '2026-07-15', + ], + '拼多多-迈磊凯家具专营店' => [ + 'previous_company' => '常州目之本电子商务有限公司', + 'company' => '武汉市福流贸易有限公司', + 'changed_at' => '2026-07-16', + ], + '淘宝-平提家具源头工厂' => [ + 'previous_company' => '常州平提电子商务有限公司', + 'company' => '义乌市育旬贸易有限公司', + 'changed_at' => '2026-08-20', + ], + '淘宝-康帕斯家具店' => [ + 'previous_company' => '永康市康帕斯贸易有限公司', + 'company' => '深圳市澄镜贸易有限公司', + 'changed_at' => '2026-08-20', + ], + '淘宝-知木家具源头工厂店' => [ + 'previous_company' => '常州聪恩家居有限公司', + 'company' => '广州市鼎流拓科技有限公司', + 'changed_at' => '2026-08-14', + ], + '阿里巴巴-达轩家居' => [ + 'previous_company' => '常州末屿家居有限公司', + 'company' => '上海臻希鹏电子商务有限公司', + 'changed_at' => '2026-09-24', + ], + ], +]; diff --git a/config/upstream_mirrors.php b/config/upstream_mirrors.php index 7399f7e2..c5582a12 100644 --- a/config/upstream_mirrors.php +++ b/config/upstream_mirrors.php @@ -20,6 +20,16 @@ return [ env('APP_ENV', 'production') === 'production' ), + // 店铺主体、商标爬虫预警的默认责任人;留空时回退到系统内第一个启用用户。 + 'crawler_warnings' => [ + 'responsible' => [ + '爬虫未更新' => trim((string) env('CRAWLER_WARNING_OWNER_STALE', '')), + '主体名称不一致' => trim((string) env('CRAWLER_WARNING_OWNER_SUBJECT_NAME', '')), + '同日主体冲突' => trim((string) env('CRAWLER_WARNING_OWNER_SUBJECT_CONFLICT', '')), + '关联缺失' => trim((string) env('CRAWLER_WARNING_OWNER_TRADEMARK', '')), + ], + ], + // 店铺经营报表由生产刷新节点自动完成首次同步并持续增量;开发机只读。 'store_report' => [ 'enabled' => filter_var(env('STORE_REPORT_MIRROR_ENABLED', true), FILTER_VALIDATE_BOOL), @@ -103,6 +113,24 @@ return [ 'operations_replica.rpt.fys_companyInfo' => ['source_key' => 'operations_replica', 'schema' => 'rpt', 'object' => 'fys_companyInfo', 'target' => 'ops_fys_company_info', 'keys' => ['Id'], 'watermark' => 'updateTIme'], 'operations_replica.rpt.fys_shopInfo' => ['source_key' => 'operations_replica', 'schema' => 'rpt', 'object' => 'fys_shopInfo', 'target' => 'ops_fys_shop_info', 'keys' => ['Id'], 'watermark' => 'updateTime'], 'operations_replica.rpt.fys_TrademarkInfo' => ['source_key' => 'operations_replica', 'schema' => 'rpt', 'object' => 'fys_TrademarkInfo', 'target' => 'ops_fys_trademark_info', 'keys' => ['Id'], 'watermark' => 'updateTime'], + // 店铺后台每天抓取的店铺→主体、店铺→商标映射。页面按最新 crawl_date + // 取每个店铺的当前值;update_time 作为同步水位线覆盖当天修正。 + 'operations_replica.rpt.crawl_shop_company' => [ + 'source_key' => 'operations_replica', + 'schema' => 'rpt', + 'object' => 'crawl_shop_company', + 'target' => 'ops_crawl_shop_company', + 'keys' => ['id'], + 'watermark' => 'update_time', + ], + 'operations_replica.rpt.crawl_shop_trademark' => [ + 'source_key' => 'operations_replica', + 'schema' => 'rpt', + 'object' => 'crawl_shop_trademark', + 'target' => 'ops_crawl_shop_trademark', + 'keys' => ['id'], + 'watermark' => 'update_time', + ], 'operations_replica.rpt.jst_Orders' => [ 'source_key' => 'operations_replica', 'schema' => 'rpt', diff --git a/database/migrations/2026_10_03_000000_create_business_data_warnings_table.php b/database/migrations/2026_10_03_000000_create_business_data_warnings_table.php new file mode 100644 index 00000000..bc8b50af --- /dev/null +++ b/database/migrations/2026_10_03_000000_create_business_data_warnings_table.php @@ -0,0 +1,51 @@ +connection()); + + if ($schema->hasTable('business_data_warnings')) { + return; + } + + $schema->create('business_data_warnings', function (Blueprint $table): void { + $table->bigIncrements('id'); + $table->string('warning_key', 191)->unique(); + $table->string('warning_number', 32)->unique(); + $table->string('warning_type', 64); + $table->string('risk_level', 16); + $table->string('related_object', 191)->nullable(); + $table->text('risk_description')->nullable(); + $table->text('evidence')->nullable(); + $table->string('impact', 191)->nullable(); + $table->string('trigger_rule', 16)->nullable(); + $table->dateTime('triggered_at'); + $table->string('responsible_person', 191)->nullable(); + $table->dateTime('due_at')->nullable(); + $table->string('review_status', 32)->default('pending'); + $table->string('reviewer', 191)->nullable(); + $table->dateTime('reviewed_at')->nullable(); + $table->text('review_note')->nullable(); + $table->timestamps(); + + $table->index(['review_status', 'risk_level'], 'business_data_warnings_status_level_index'); + $table->index('warning_type', 'business_data_warnings_type_index'); + }); + } + + public function down(): void + { + Schema::connection($this->connection())->dropIfExists('business_data_warnings'); + } +}; diff --git a/database/migrations/2026_10_03_000100_create_store_report_shop_period_changes_table.php b/database/migrations/2026_10_03_000100_create_store_report_shop_period_changes_table.php new file mode 100644 index 00000000..960c948f --- /dev/null +++ b/database/migrations/2026_10_03_000100_create_store_report_shop_period_changes_table.php @@ -0,0 +1,38 @@ +connection()); + + if (! $schema->hasTable('store_report_shop_period_changes')) { + $schema->create('store_report_shop_period_changes', function (Blueprint $table): void { + $table->bigIncrements('id'); + $table->string('erp_store_name', 191); + $table->string('company_name', 191)->nullable(); + $table->string('unified_social_credit_code', 64)->nullable(); + $table->date('observed_on'); + $table->string('source', 32)->default('crawl_shop_company'); + $table->string('business_key', 64); + $table->timestamps(); + $table->unique('business_key', 'store_report_period_changes_key_unique'); + $table->index(['erp_store_name', 'observed_on'], 'store_report_period_changes_store_time_index'); + }); + } + } + + public function down(): void + { + Schema::connection($this->connection())->dropIfExists('store_report_shop_period_changes'); + } +}; diff --git a/deploy/local-init.sh b/deploy/local-init.sh index 6dfa66b8..904b582b 100644 --- a/deploy/local-init.sh +++ b/deploy/local-init.sh @@ -84,7 +84,9 @@ calculate_local_image_fingerprint() { deploy/entrypoint.sh \ deploy/local-healthcheck.sh - find resources/assets resources/views resources/lang -type f -print0 + # resources/ 整体挂载进容器,Blade 视图在运行时编译,改视图不需要重建镜像; + # 只有需要编译进 public/ 的前端源码(resources/assets)才参与镜像指纹。 + find resources/assets resources/lang -type f -print0 find public -type f \ ! -path 'public/js/*' \ ! -path 'public/vendor/*' \ @@ -92,7 +94,7 @@ calculate_local_image_fingerprint() { ! -name 'mix-manifest.json' \ -print0 } \ - | sort -z \ + | LC_ALL=C sort -z \ | xargs -0 sha256sum \ | sha256sum \ | awk '{ print $1 }' @@ -405,11 +407,24 @@ if [ "$ready" -ne 1 ]; then exit 1 fi -http_code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 15 \ - http://127.0.0.1:18088/auth/login || true)" +# 应用刚启动时首个请求(共享库连接、缓存预热)可能明显偏慢, +# 这里按等待窗口重试,避免把“只是慢”误判成启动失败。 +login_ready=0 +for login_attempt in $(seq 1 8); do + http_code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 45 \ + http://127.0.0.1:18088/auth/login || true)" -if [ "$http_code" != "200" ]; then - echo "错误:登录页返回 HTTP ${http_code:-000}。" >&2 + if [ "$http_code" = "200" ]; then + login_ready=1 + break + fi + + echo " 等待登录页响应... ($login_attempt/8,当前 HTTP ${http_code:-000})" + sleep 5 +done + +if [ "$login_ready" -ne 1 ]; then + echo "错误:登录页在等待窗口内未返回 HTTP 200。" >&2 exit 1 fi diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index 30956928..bd38a913 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -37,6 +37,8 @@ | 公司资料 | `rpt.fys_companyInfo` | `ops_fys_company_info` | | 店铺资料 | `rpt.fys_shopInfo` | `ops_fys_shop_info` | | 商标资料 | `rpt.fys_TrademarkInfo` | `ops_fys_trademark_info` | +| 店铺对应主体(爬虫) | `rpt.crawl_shop_company` | `ops_crawl_shop_company` | +| 店铺对应商标(爬虫) | `rpt.crawl_shop_trademark` | `ops_crawl_shop_trademark` | | 订单 | `rpt.jst_Orders`、`rpt.jst_OrderItem` | `ops_jst_orders`、`ops_jst_order_item` | | 商品与仓库 | `rpt.jst_sku`、`rpt.jst_warehouseInfo`、`rpt.jst_skumap` | `ops_jst_*`(普通商品资料已切换) | | 推广费用 | `rpt.ads_itemid_sales_cost_ratio_v2_di` | `ops_item_sales_cost_ratio` | @@ -110,3 +112,30 @@ php artisan business-mirror:sync-tables jiansun.dbo.jst_small_payment_order_map ``` 两张表完成后,页面按映射表的工单创建时间稳定分页,并以 `woNo` 关联最新的工单主记录。筛选支持日期、工单号、店铺、支付宝批次号、线上订单号、状态和打款原因;表单详情从镜像中的 `businessFieldList` 解析,导出与页面使用同一筛选口径。首次全量未完成、查询索引未就绪或刷新失败时均保留明确状态,不能把空结果解释为没有小额打款工单。 + +## 店铺主体与商标爬虫数据(2026-10-03 接入) + +店铺后台每天抓取的店铺对应主体、店铺对应商标数据进入共享镜像,是店铺主数据的最高优先级来源: + +| 业务范围 | 上游对象 | 镜像目标 | +| --- | --- | --- | +| 店铺对应主体 | `rpt.crawl_shop_company` | `ops_crawl_shop_company` | +| 店铺对应商标 | `rpt.crawl_shop_trademark` | `ops_crawl_shop_trademark` | + +规则: + +1. 两个数据集按 `update_time` 增量同步,页面按每个店铺最新的 `crawl_date` 取值;爬虫没有覆盖的店铺继续使用财务系统原链路。 +2. 店铺基础信息的「店铺主体」「商标」「商标号」三列优先显示爬虫结果;多商标在同一格内按商标号排序后用「、」串联,名称和号码顺序一一对应。 +3. 展示、筛选、排序使用同一口径,商标名和商标号必须来自同一数据源,不允许只切换其中一个。 +4. 爬虫主体与当前存续期间不一致时,`php artisan store-report:sync-crawler-subjects` 以抓到变化的 `crawl_date` 作为新主体生效日,老主体到同一天 00:00 结束;同一天流程表给出不同主体时以爬虫为准并生成待人工复审的预警。 +5. `php artisan crawler:warnings:check` 检查爬虫断更、主体名称不一致、期间异常和关联缺失,写入「规则信息 → 预警页面」,每条预警指派责任人。 + +首次全量必须在低峰期显式执行,顺序如下: + +```bash +php artisan business-mirror:sync-tables operations_replica.rpt.crawl_shop_company --full +php artisan business-mirror:sync-tables operations_replica.rpt.crawl_shop_trademark --full +php artisan store-report:sync-crawler-subjects --dry-run # 先核对将要生成的主体期间变更 +php artisan store-report:sync-crawler-subjects +php artisan crawler:warnings:check +``` diff --git a/docs/业务数据分层存储.md b/docs/业务数据分层存储.md index c1d69fff..cbce9914 100644 --- a/docs/业务数据分层存储.md +++ b/docs/业务数据分层存储.md @@ -40,7 +40,7 @@ | 页面/业务域 | 数据集 | 目标层 | 说明 | | --- | --- | --- | --- | | 公司、店铺、商标 | `fys_companyInfo`、`fys_shopInfo`、`fys_TrademarkInfo` | MariaDB 维表 | 行数小、点查与关联频繁 | -| 店铺经营报表 | `store_report_lifecycle_rows` + `store_report_daily_rows` | MariaDB 维表/汇总 | 页面已改为共享日汇总,不依赖 `runtime/bills.sqlite`,不扫描原始账单 | +| 店铺经营报表 | 期初主体配置 + `store_report_shop_period_changes` + `store_report_daily_rows` | MariaDB 汇总/配置 | 主体期间按期初数据和爬虫变更生成,一行一期间;页面读共享日汇总,不扫描原始账单。`store_report_lifecycle_rows` 仅保留流程原始记录备查 | | 普通商品资料 | `jst_sku` + 采购价镜像 | MariaDB 维表/服务表 | 约 55 万商品仍适合行存筛选;保留现有完整导出校准 | | 推广费用 | `ads_itemid_sales_cost_ratio_v2_di` | ClickHouse 事实表 | `jst_skumap` 不再作为页面就绪条件 | | 售后费用、非京喜退款 | 订单、订单明细、退款主表、退款明细 | ClickHouse 事实表 + MariaDB SKU/仓库/店铺维表 | 四张事实表必须成组对账和切换,不能逐张切换造成跨库半成品 | diff --git a/modules/EcommerceFinance/Repositories/BusinessInformationMirrorRepository.php b/modules/EcommerceFinance/Repositories/BusinessInformationMirrorRepository.php index 48035ab6..89afc37b 100644 --- a/modules/EcommerceFinance/Repositories/BusinessInformationMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/BusinessInformationMirrorRepository.php @@ -55,13 +55,21 @@ class BusinessInformationMirrorRepository throw new RuntimeException('business_mirror_dataset_not_ready'); } - $query = $this->storeQuery()->orderByDesc('s.Id'); + $query = $this->storeQuery($this->crawlerSubqueries())->orderByDesc('s.Id'); $this->applyStoreFilters($query, $filters); - return $query - ->get() - ->map(static fn (object $row): array => (array) $row) - ->all(); + $rows = $query->get()->map(static fn (object $row): array => (array) $row)->all(); + $trademarks = $this->crawlerTrademarkPairsForAll( + array_map(static fn (array $row): string => (string) ($row['erp_store_name'] ?? ''), $rows), + ); + + return array_map( + fn (array $row): array => $this->applyCrawlerPriority( + $row, + $trademarks[(string) ($row['erp_store_name'] ?? '')] ?? [], + ), + $rows, + ); } /** @@ -227,13 +235,14 @@ SQL); /** @return array{data: array>, total: int} */ private function stores(int $page, int $perPage, array $filters): array { - $query = $this->storeQuery(); + $crawler = $this->crawlerSubqueries(); + $query = $this->storeQuery($crawler); $this->applyFieldFilters($query, $filters, [ - 'store_id' => 's.sotreId', 'store_name' => 's.erpShopName', 'store_entity' => 'c.businessLicenseName', - 'trademark' => 't.trademarkName', 'trademark_number' => 's.trademarkNumber', + 'store_id' => 's.sotreId', 'store_name' => 's.erpShopName', 'business_department' => 's.responsibleDepartment', 'store_manager' => 's.shopHead', 'department_manager' => 's.customerDeptManager', 'opened_at' => 's.openTime', 'renewed_at' => 's.renewalTime', ], [], ['platform' => 's.platform']); + $this->applyCrawlerPriorityFilters($query, $filters, $crawler); $storeStatus = trim((string) ($filters['store_status'] ?? '')); if ($storeStatus !== '') { $statusCode = ['正常' => '0', '关闭中' => '3', '已关闭' => '6'][$storeStatus] ?? $storeStatus; @@ -247,17 +256,196 @@ SQL); $query->whereNull('s.shopBankAccount')->orWhere('s.shopBankAccount', '=', ''); }); } - return $this->page($query, $page, $perPage, 's.Id', $filters, [ + $result = $this->page($query, $page, $perPage, 's.Id', $filters, [ 'platform' => 's.platform', 'store_id' => 's.sotreId', 'store_name' => 's.erpShopName', - 'store_entity' => 'c.businessLicenseName', 'store_status' => 's.shopState', - 'trademark' => 't.trademarkName', 'trademark_number' => 's.trademarkNumber', + 'store_status' => 's.shopState', 'business_department' => 's.responsibleDepartment', 'store_manager' => 's.shopHead', 'department_manager' => 's.customerDeptManager', 'opened_at' => 's.openTime', 'renewed_at' => 's.renewalTime', 'withdrawal_account' => 's.shopBankAccount', ]); + + $result['data'] = array_map( + fn (array $row): array => $this->applyCrawlerPriority( + $row, + $this->crawlerTrademarkPairsFor((string) ($row['erp_store_name'] ?? '')), + ), + $result['data'], + ); + + return $result; } - private function storeQuery() + /** + * 店铺基础信息页的展示优先级:爬虫抓到的店铺主体、商标名称和商标号优先, + * 爬虫没有覆盖的店铺保留财务系统原有链路的结果。 + * + * @param array $row + * @return array + */ + /** + * @param array $row + * @param list $trademarkPairs + * @return array + */ + private function applyCrawlerPriority(array $row, array $trademarkPairs = []): array + { + // 多商标拼接放在 PHP 层:SQLite 测试库不支持 group_concat 的 order by 语法。 + if ($trademarkPairs !== []) { + $row['crawler_trademark_name'] = implode('、', array_filter(array_column($trademarkPairs, 'name'))); + $row['crawler_trademark_number'] = implode('、', array_filter(array_column($trademarkPairs, 'number'))); + } + + // 保留财务系统登记主体,供店铺维度报表生成期初期间;展示仍按爬虫优先。 + $row['system_company_name'] = $row['company_name'] ?? null; + $company = trim((string) ($row['crawler_company_name'] ?? '')); + if ($company !== '') { + $row['company_name'] = $company; + } + + $trademarkName = trim((string) ($row['crawler_trademark_name'] ?? '')); + if ($trademarkName !== '') { + $row['trademark_name'] = $trademarkName; + } + + $trademarkNumber = trim((string) ($row['crawler_trademark_number'] ?? '')); + if ($trademarkNumber !== '') { + $row['trademark_number'] = $trademarkNumber; + } + + unset( + $row['crawler_company_name'], + $row['crawler_trademark_name'], + $row['crawler_trademark_number'], + ); + + return $row; + } + + /** + * 某店铺爬虫抓到的全部商标,按商标号排序,保证名称与号码一一对应。 + * + * @return list + */ + /** @return list */ + private function crawlerTrademarkPairsFor(string $storeName): array + { + $all = $this->crawlerTrademarkPairsForAll([$storeName]); + + return $all[trim($storeName)] ?? []; + } + + /** + * 批量加载多个店铺的爬虫商标,避免列表页逐行查询。 + * + * @param list $storeNames + * @return array>|array{} + */ + private function crawlerTrademarkPairsForAll(array $storeNames): array + { + $names = array_values(array_unique(array_filter(array_map('trim', $storeNames)))); + if ($names === [] || ! $this->connection->getSchemaBuilder()->hasTable('ops_crawl_shop_trademark')) { + return []; + } + + $result = []; + foreach (array_chunk($names, 500) as $chunk) { + $ranked = $this->connection->table('ops_crawl_shop_trademark') + ->selectRaw('id, shop_name, trademark_no, trademark_name, crawl_date, row_number() over (partition by shop_name, trademark_no order by crawl_date desc, id desc) AS row_rank') + ->whereIn('shop_name', $chunk); + $rows = $this->connection->table($ranked, 'crawler_trademark') + ->where('row_rank', 1) + ->orderBy('shop_name') + ->orderBy('trademark_no') + ->get(); + + foreach ($rows as $row) { + $store = trim((string) $row->shop_name); + $number = trim((string) $row->trademark_no); + if ($store === '' || $number === '') { + continue; + } + $result[$store][] = [ + 'number' => $number, + 'name' => trim((string) $row->trademark_name), + ]; + } + } + + return $result; + } + + /** @param array $crawler @param array $filters */ + private function applyCrawlerPriorityFilters($query, array $filters, array $crawler): void + { + $entity = mb_substr(trim((string) ($filters['store_entity'] ?? '')), 0, 191); + if ($entity !== '') { + $like = '%' . addcslashes($entity, '%_\\') . '%'; + $query->where(function ($query) use ($crawler, $like): void { + $query->where('c.businessLicenseName', 'like', $like); + if ($crawler['store_entity'] !== null) { + $query->orWhere($crawler['store_entity'], 'like', $like); + } + }); + } + + foreach ([ + 'trademark' => ['crawler' => 'trademark_name', 'fallback' => 't.trademarkName'], + 'trademark_number' => ['crawler' => 'trademark_no', 'fallback' => 's.trademarkNumber'], + ] as $key => $columns) { + $value = mb_substr(trim((string) ($filters[$key] ?? '')), 0, 191); + if ($value === '') { + continue; + } + + $like = '%' . addcslashes($value, '%_\\') . '%'; + if ($crawler['trademark_table'] === null) { + $query->where($columns['fallback'], 'like', $like); + continue; + } + + // 多商标店铺不能在联表结果里只匹配最新一条,这里按全量商标做 EXISTS 匹配。 + $query->where(function ($query) use ($columns, $like, $crawler): void { + $query->where($columns['fallback'], 'like', $like) + ->orWhereExists(function ($sub) use ($columns, $like, $crawler): void { + $sub->selectRaw('1') + ->from($crawler['trademark_table'] . ' AS crawler_trademark_filter') + ->whereColumn('crawler_trademark_filter.shop_name', 's.erpShopName') + ->where('crawler_trademark_filter.' . $columns['crawler'], 'like', $like); + }); + }); + } + } + + /** + * 爬虫表的“每店铺最新一版”子查询。镜像缺失时返回 null,页面回退原链路。 + * + * @return array{company:?object,store_entity:?string,trademark_table:?string} + */ + private function crawlerSubqueries(): array + { + $absence = ['company' => null, 'store_entity' => null, 'trademark_table' => null]; + $schema = $this->connection->getSchemaBuilder(); + if (! $schema->hasTable('ops_crawl_shop_company') && ! $schema->hasTable('ops_crawl_shop_trademark')) { + return $absence; + } + + $company = null; + if ($schema->hasTable('ops_crawl_shop_company')) { + $ranked = $this->connection->table('ops_crawl_shop_company') + ->selectRaw('id, shop_name, company_name, tax_no, crawl_date, row_number() over (partition by shop_name order by crawl_date desc, id desc) AS row_rank'); + $company = $this->connection->table($ranked, 'crawler_company') + ->where('row_rank', 1) + ->select(['shop_name', 'company_name', 'tax_no']); + } + + return [ + 'company' => $company, + 'store_entity' => $company === null ? null : 'crawler_company.company_name', + 'trademark_table' => $schema->hasTable('ops_crawl_shop_trademark') ? 'ops_crawl_shop_trademark' : null, + ]; + } + + private function storeQuery(array $crawler = ['company' => null]) { $latestCompanies = $this->connection->table('ops_fys_company_info') ->selectRaw('unifiedSocialCreditCode, MAX(Id) AS latest_id') @@ -283,6 +471,11 @@ SQL); }, ) ->leftJoin('ops_fys_trademark_info AS t', 't.Id', '=', 'trademark_keys.latest_id') + ->when($crawler['company'] !== null, function ($query) use ($crawler): void { + $query->leftJoinSub($crawler['company'], 'crawler_company', function ($join): void { + $join->on('crawler_company.shop_name', '=', 's.erpShopName'); + }); + }) ->selectRaw(<<<'SQL' s.Id AS id, s.sotreId AS store_id, @@ -301,9 +494,19 @@ s.customerDeptManager AS department_manager, s.shopState AS store_status, s.shopBankAccount AS withdrawal_account, s.shopType AS store_type, -s.channelType AS channel_mode +s.channelType AS channel_mode, +NULL AS crawler_company_name, +NULL AS crawler_trademark_name, +NULL AS crawler_trademark_number SQL); + if ($crawler['company'] !== null) { + $query->addSelect([ + 'crawler_company.company_name AS crawler_company_name', + 'crawler_company.tax_no AS crawler_tax_no', + ]); + } + return $query; } diff --git a/modules/EcommerceFinance/Repositories/DataWarningMirrorRepository.php b/modules/EcommerceFinance/Repositories/DataWarningMirrorRepository.php new file mode 100644 index 00000000..47b0b91a --- /dev/null +++ b/modules/EcommerceFinance/Repositories/DataWarningMirrorRepository.php @@ -0,0 +1,189 @@ +connection = $mirror->connection(); + } + + public function ready(): bool + { + return $this->connection->getSchemaBuilder()->hasTable('business_data_warnings'); + } + + /** + * 生成或更新一条预警。同 warning_key 重复触发时只更新风险描述和影响, + * 保留首次触发时间、责任人指派和处理留痕。 + * + * @param array $warning + */ + public function upsert(array $warning): void + { + $key = (string) $warning['warning_key']; + $existing = $this->connection->table('business_data_warnings')->where('warning_key', $key)->first(); + + if ($existing !== null) { + $this->connection->table('business_data_warnings')->where('warning_key', $key)->update([ + 'warning_type' => $warning['warning_type'], + 'risk_level' => $warning['risk_level'], + 'related_object' => $warning['related_object'] ?? null, + 'risk_description' => $warning['risk_description'] ?? null, + 'evidence' => $warning['evidence'] ?? null, + 'impact' => $warning['impact'] ?? null, + 'trigger_rule' => $warning['trigger_rule'] ?? null, + 'updated_at' => now(), + ]); + + return; + } + + $this->connection->table('business_data_warnings')->insert([ + 'warning_key' => $key, + 'warning_number' => $this->nextWarningNumber(), + 'warning_type' => $warning['warning_type'], + 'risk_level' => $warning['risk_level'], + 'related_object' => $warning['related_object'] ?? null, + 'risk_description' => $warning['risk_description'] ?? null, + 'evidence' => $warning['evidence'] ?? null, + 'impact' => $warning['impact'] ?? null, + 'trigger_rule' => $warning['trigger_rule'] ?? null, + 'triggered_at' => now(), + 'responsible_person' => $warning['responsible_person'] ?? null, + 'due_at' => $warning['due_at'] ?? null, + 'review_status' => 'pending', + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + /** + * 预警页面数据。按触发时间倒序,等待复审的排在前面。 + * + * @param array $filters + * @return array{data: array>, total: int, pending: int} + */ + public function paginate(int $page, int $perPage, array $filters = []): array + { + if (! $this->ready()) { + return ['data' => [], 'total' => 0, 'pending' => 0]; + } + + $query = $this->connection->table('business_data_warnings'); + + foreach (['warning_type', 'risk_level', 'review_status'] as $exact) { + $value = trim((string) ($filters[$exact] ?? '')); + if ($value !== '') { + $query->where($exact, '=', $value); + } + } + + foreach (['warning_number', 'related_object', 'responsible_person'] as $text) { + $value = trim((string) ($filters[$text] ?? '')); + if ($value !== '') { + $query->where($text, 'like', '%' . addcslashes($value, '%_\\') . '%'); + } + } + + $total = (clone $query)->count(); + $pending = (clone $query)->where('review_status', '=', 'pending')->count(); + + $rows = $query + ->orderByRaw("case when review_status = 'pending' then 0 else 1 end") + ->orderByDesc('triggered_at') + ->orderByDesc('id') + ->forPage(max($page, 1), $perPage) + ->get() + ->map(static function (object $row): array { + $row = (array) $row; + $row['triggered_at'] = $row['triggered_at'] !== null + ? Carbon::parse($row['triggered_at'])->format('Y-m-d H:i') + : null; + $row['reviewed_at'] = $row['reviewed_at'] !== null + ? Carbon::parse($row['reviewed_at'])->format('Y-m-d H:i') + : null; + $row['due_at'] = $row['due_at'] !== null + ? Carbon::parse($row['due_at'])->format('Y-m-d') + : null; + $row['review_status_label'] = match ((string) $row['review_status']) { + 'pending' => '待复审', + 'confirmed' => '已确认', + 'rejected' => '已驳回', + default => (string) $row['review_status'], + }; + + return $row; + }) + ->all(); + + return ['data' => $rows, 'total' => $total, 'pending' => $pending]; + } + + /** @param array> $warnings */ + public function upsertMany(array $warnings): int + { + $count = 0; + foreach ($warnings as $warning) { + $this->upsert($warning); + $count++; + } + + return $count; + } + + public function find(int $id): ?array + { + if (! $this->ready()) { + return null; + } + + $row = $this->connection->table('business_data_warnings')->where('id', $id)->first(); + + return $row === null ? null : (array) $row; + } + + /** + * 人工复审:确认时保持系统按爬虫生成的结果,驳回时记录依据由后续 + * 期间合并任务改回流程数据。 + */ + public function review(int $id, string $status, string $reviewer, ?string $note, ?string $responsiblePerson = null): bool + { + if (! $this->ready()) { + return false; + } + + $update = [ + 'review_status' => $status, + 'reviewer' => $reviewer, + 'reviewed_at' => now(), + 'review_note' => $note, + 'updated_at' => now(), + ]; + if ($responsiblePerson !== null && trim($responsiblePerson) !== '') { + $update['responsible_person'] = trim($responsiblePerson); + } + + return $this->connection->table('business_data_warnings')->where('id', $id)->update($update) > 0; + } + + private function nextWarningNumber(): string + { + $date = now()->format('Ymd'); + $prefix = 'WARN-' . $date . '-'; + $latest = $this->connection->table('business_data_warnings') + ->where('warning_number', 'like', $prefix . '%') + ->orderByDesc('warning_number') + ->value('warning_number'); + $sequence = $latest === null ? 0 : (int) substr((string) $latest, -3); + + return $prefix . str_pad((string) ($sequence + 1), 3, '0', STR_PAD_LEFT); + } +} diff --git a/modules/EcommerceFinance/Repositories/StoreReportRepository.php b/modules/EcommerceFinance/Repositories/StoreReportRepository.php index 137693d0..4c9c0a72 100644 --- a/modules/EcommerceFinance/Repositories/StoreReportRepository.php +++ b/modules/EcommerceFinance/Repositories/StoreReportRepository.php @@ -5,13 +5,15 @@ namespace Modules\EcommerceFinance\Repositories; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\DatabaseManager; use Illuminate\Support\Facades\Schema; +use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder; use RuntimeException; /** - * 将经营分析副本库中的店铺资料与共享日汇总组合为店铺维度报表。 + * 将店铺主体期间与共享日汇总组合为店铺维度报表。 * * 页面不扫描千万级原始账单,也不依赖开发机或镜像中不存在的 SQLite。 - * 生命周期区间统一使用 [effective_at, expires_at),避免主体切换日重复统计。 + * 期间由 StoreReportPeriodBuilder 按期初数据和爬虫变更生成:变更店铺的老主体 + * 失效时间为变更日前一天、新主体从变更日当天生效,收入按实际日期归集,不重复统计。 */ class StoreReportRepository { @@ -26,12 +28,14 @@ class StoreReportRepository /** 主表 rpt.fys_shopinfo 中这些平台不支持变更主体(能否变更=否),其余平台均为是。 */ public const UNCHANGEABLE_PLATFORMS = ['唯品会MP', '淘工厂']; private readonly ConnectionInterface $connection; + private readonly StoreReportPeriodBuilder $periods; public function __construct(DatabaseManager $database) { $this->connection = $database->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); + $this->periods = new StoreReportPeriodBuilder($database); } /** @@ -77,7 +81,7 @@ class StoreReportRepository /** * @param array> $stores - * @param array $filters 未覆盖字段已在上游 allStores 过滤;这里过滤主体、生命周期区间及归属覆盖字段。 + * @param array $filters 未覆盖字段已在上游 allStores 过滤;这里过滤主体、主体期间及归属覆盖字段。 * @return array> */ public function rows(array $stores, ?int $reportYear = null, array $filters = []): array @@ -87,17 +91,19 @@ class StoreReportRepository } $dailyGeneration = $this->activeGeneration(self::DAILY_DATASET); - $lifecycleGeneration = $this->activeGeneration(self::LIFECYCLES_DATASET); - $names = array_values(array_unique(array_filter(array_map( - static fn (array $row): string => trim((string) ($row['erp_store_name'] ?? '')), - $stores, - )))); - $lifecycles = $this->lifecycles($names, $lifecycleGeneration); - $lifecycleSales = $this->lifecycleSales($names, $lifecycleGeneration, $dailyGeneration); - $fallbackSales = $this->fallbackSales( - array_values(array_diff($names, array_keys($lifecycles))), - $dailyGeneration, - ); + // 期间由期初数据、系统主体和爬虫变更生成,同一店铺不再展开流程历史重复行。 + $periods = $this->periods->build($stores); + $multiPeriod = []; + $singleNames = []; + foreach ($periods as $name => $list) { + if (count($list) > 1) { + $multiPeriod[$name] = $list; + } else { + $singleNames[] = $name; + } + } + $singleSales = $this->singlePeriodSales($singleNames, $dailyGeneration); + $splitSales = $this->periodSales($multiPeriod, $dailyGeneration); $reportYear = $reportYear ?? (int) now()->format('Y'); $currentMonth = now()->startOfMonth(); $rollingMonths = collect(range(12, 1))->map( @@ -111,17 +117,18 @@ class StoreReportRepository continue; } - $periods = $lifecycles[$storeName] ?? [[ + $list = $periods[$storeName] ?? [[ 'company_name' => $store['company_name'] ?? null, 'effective_at' => self::DEFAULT_EFFECTIVE_AT, - 'expires_at' => self::DEFAULT_EXPIRES_AT, - 'lifecycle_id' => null, + 'expires_at' => null, + 'source' => 'fallback', ]]; + $split = count($list) > 1; - foreach ($periods as $period) { - $saleData = $period['lifecycle_id'] === null - ? ($fallbackSales[$storeName] ?? ['period' => 0.0, 'months' => []]) - : ($lifecycleSales[(string) $period['lifecycle_id']] ?? ['period' => 0.0, 'months' => []]); + foreach ($list as $index => $period) { + $saleData = $split + ? ($splitSales[$storeName][$index] ?? ['period' => 0.0, 'months' => []]) + : ($singleSales[$storeName] ?? ['period' => 0.0, 'months' => []]); $monthly = []; $monthlyIncome = []; for ($month = 1; $month <= 12; $month++) { @@ -140,14 +147,19 @@ class StoreReportRepository $effectiveAt = $this->date($period['effective_at'] ?? null, self::DEFAULT_EFFECTIVE_AT); $expiresAt = $this->date($period['expires_at'] ?? null, self::DEFAULT_EXPIRES_AT); + $status = $this->status($store['store_status'] ?? null); + // 关店和其他非正常状态不展示期间日期,避免把已关闭店铺计入在营期间。 + $normal = $status === '开店中'; $row = [ ...$store, 'store_id' => $storeName, - 'status' => $this->status($store['store_status'] ?? null), + 'status' => $status, 'can_change' => $this->canChange($store['platform'] ?? null), 'entity' => $period['company_name'] ?: ($store['company_name'] ?? null), - 'effective_at' => $effectiveAt, - 'invalid_at' => $expiresAt === self::DEFAULT_EXPIRES_AT ? '存续中' : $expiresAt, + 'effective_at' => $normal ? $effectiveAt : '-', + 'invalid_at' => $normal + ? ($expiresAt === self::DEFAULT_EXPIRES_AT ? '存续中' : $expiresAt) + : '-', 'period_sales' => (float) $saleData['period'], 'rolling_12m_income' => array_intersect($rollingMonths, array_keys($saleData['months'])) === [] ? null @@ -156,7 +168,7 @@ class StoreReportRepository ...$monthlyIncome, ]; - if (! $this->matchesFilters($row, $expiresAt, $filters)) { + if (! $this->matchesFilters($row, $effectiveAt, $expiresAt, $filters)) { continue; } @@ -206,80 +218,63 @@ class StoreReportRepository return $rows; } - /** @return array>> */ - private function lifecycles(array $names, string $generation): array + /** + * 多期间店铺按每个期间的主体生效日、失效日(含当天)拆分日汇总收入。 + * + * @param array>> $periodsByStore + * @return array}>> + */ + private function periodSales(array $periodsByStore, string $dailyGeneration): array { - $result = []; - foreach ($this->nameChunks($names) as $chunk) { - $placeholders = implode(',', array_fill(0, count($chunk), '?')); - $rows = $this->select( - "SELECT id, erp_store_name, company_name, effective_at, expires_at - FROM store_report_lifecycle_rows - WHERE generation = ? AND erp_store_name IN ($placeholders) - ORDER BY erp_store_name, effective_at, id", - [$generation, ...$chunk], - ); - - foreach ($rows as $row) { - $result[(string) $row['erp_store_name']][] = [ - 'lifecycle_id' => (int) $row['id'], - 'company_name' => $row['company_name'], - 'effective_at' => $row['effective_at'], - 'expires_at' => $row['expires_at'], + $flat = []; + foreach ($periodsByStore as $name => $list) { + foreach ($list as $index => $period) { + $flat[] = [ + 'key' => $name . "\x1f" . $index, + 'store' => $name, + 'effective_at' => $this->date($period['effective_at'] ?? null, self::DEFAULT_EFFECTIVE_AT), + 'expires_at' => $this->date($period['expires_at'] ?? null, self::DEFAULT_EXPIRES_AT), ]; } } - return $result; - } - - /** @return array}> */ - private function lifecycleSales(array $names, string $lifecycleGeneration, string $dailyGeneration): array - { - if ($names === []) { - return []; - } - $month = $this->connection->getDriverName() === 'sqlite' ? "substr(d.occurred_on, 1, 7)" : "DATE_FORMAT(d.occurred_on, '%Y-%m')"; - // 生命周期记录的生效时间严格来自存续表;只有没有存续表记录的店铺 - // 才会在 fallbackSales() 中使用默认的 2026-07-01。 - $lowerBound = $this->connection->getDriverName() === 'sqlite' - ? 'date(d.occurred_on) >= date(l.effective_at)' - : 'd.occurred_on >= l.effective_at'; - $upperBound = $this->connection->getDriverName() === 'sqlite' - ? 'date(d.occurred_on) < date(COALESCE(l.expires_at, ?))' - : 'd.occurred_on < COALESCE(l.expires_at, ?)'; - $result = []; - foreach ($this->nameChunks($names) as $chunk) { - $placeholders = implode(',', array_fill(0, count($chunk), '?')); + $collected = []; + foreach (array_chunk($flat, 200) as $chunk) { + $selects = []; + $bindings = []; + foreach ($chunk as $row) { + $selects[] = 'SELECT ? AS period_key, ? AS store_name, ? AS effective_at, ? AS expires_at'; + array_push($bindings, $row['key'], $row['store'], $row['effective_at'], $row['expires_at']); + } $rows = $this->select( - "SELECT l.id AS lifecycle_id, {$month} AS bill_month, + "SELECT p.period_key, {$month} AS bill_month, SUM(COALESCE(d.income, 0)) AS income_total - FROM store_report_lifecycle_rows AS l + FROM (" . implode(' UNION ALL ', $selects) . ") AS p INNER JOIN store_report_daily_rows AS d - ON d.store_name = l.erp_store_name + ON d.store_name = p.store_name AND d.generation = ? - AND {$lowerBound} - AND {$upperBound} - WHERE l.generation = ? AND l.erp_store_name IN ($placeholders) - GROUP BY l.id, {$month}", - [ - $dailyGeneration, - self::DEFAULT_EXPIRES_AT, - $lifecycleGeneration, - ...$chunk, - ], + AND d.occurred_on >= p.effective_at + AND d.occurred_on <= p.expires_at + GROUP BY p.period_key, {$month}", + [...$bindings, $dailyGeneration], ); - $this->collectSales($result, $rows, 'lifecycle_id'); + $this->collectSales($collected, $rows, 'period_key'); + } + + $result = []; + foreach ($collected as $key => $sales) { + [$store, $index] = explode("\x1f", (string) $key, 2) + [1 => '0']; + $result[$store][(int) $index] = $sales; } return $result; } /** @return array}> */ - private function fallbackSales(array $names, string $dailyGeneration): array + private function singlePeriodSales(array $names, string $dailyGeneration): array { if ($names === []) { return []; @@ -359,15 +354,17 @@ class StoreReportRepository } /** - * 生命周期字段的行级过滤:主体、能否变更、负责人、利润中心精确匹配;生效、失效时间左闭右开。 + * 期间字段的行级过滤:主体、能否变更、负责人、利润中心精确匹配;生效、失效时间左闭右开。 * * 店铺负责人/部门负责人/利润中心展示店铺账号镜像(PostgreSQL)覆盖后的值, * 与页面筛选保持一致,因此在这里按行值匹配而不是上游镜像 SQL。 * - * @param array $row + * @param array $row 展示行(关店店铺的期间字段已显示为“-”) + * @param string $effectiveAt 期间真实生效日,不受状态展示影响 + * @param string $expiresAt 期间真实失效日,存续中按 2999-01-01 参与过滤 * @param array $filters */ - private function matchesFilters(array $row, string $expiresAt, array $filters): bool + private function matchesFilters(array $row, string $effectiveAt, string $expiresAt, array $filters): bool { foreach (['entity', 'store_manager', 'department_manager', 'profit_center'] as $key) { $expected = trim((string) ($filters[$key] ?? '')); @@ -380,7 +377,7 @@ class StoreReportRepository return false; } - foreach (['effective_at' => (string) $row['effective_at'], 'invalid_at' => $expiresAt] as $key => $value) { + foreach (['effective_at' => $effectiveAt, 'invalid_at' => $expiresAt] as $key => $value) { $from = trim((string) ($filters[$key . '_from'] ?? '')); $to = trim((string) ($filters[$key . '_to'] ?? '')); if ($from !== '' && strcmp($value, $from) < 0) { diff --git a/modules/EcommerceFinance/Services/CrawlerDataGuard.php b/modules/EcommerceFinance/Services/CrawlerDataGuard.php new file mode 100644 index 00000000..d6a8363c --- /dev/null +++ b/modules/EcommerceFinance/Services/CrawlerDataGuard.php @@ -0,0 +1,241 @@ + */ + private const RESPONSIBLE_BY_TYPE = [ + self::TYPE_STALE => '数据同步责任人', + self::TYPE_SUBJECT_NAME => '主体资料责任人', + self::TYPE_SUBJECT_CONFLICT => '主体变更责任人', + self::TYPE_TRADEMARK_MISSING => '商标资料责任人', + ]; + + /** @return array 各类型检查数量统计 */ + public function check(): array + { + $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); + $warnings = $this->staleWarnings($connection); + $warnings = [...$warnings, ...$this->subjectNameWarnings($connection)]; + $warnings = [...$warnings, ...$this->trademarkWarnings($connection)]; + + $repository = app(DataWarningMirrorRepository::class); + foreach ($warnings as $warning) { + $warning['responsible_person'] ??= $this->responsiblePerson((string) $warning['warning_type']); + $repository->upsert($warning); + } + + $summary = []; + foreach ($warnings as $warning) { + $summary[$warning['warning_type']] = ($summary[$warning['warning_type']] ?? 0) + 1; + } + + return $summary; + } + + /** 供期间合并命令复用同一套责任人指派口径。 */ + public function responsiblePersonFor(string $type): ?string + { + return $this->responsiblePerson($type); + } + + /** @return list> */ + private function staleWarnings($connection): array + { + $sources = [ + ['table' => 'ops_crawl_shop_company', 'label' => '店铺对应主体'], + ['table' => 'ops_crawl_shop_trademark', 'label' => '店铺对应商标'], + ]; + $warnings = []; + + foreach ($sources as $source) { + if (! $connection->getSchemaBuilder()->hasTable($source['table'])) { + continue; + } + + $row = $connection->table($source['table']) + ->selectRaw('MAX(crawl_date) AS latest_date, MAX(update_time) AS latest_update, COUNT(*) AS row_count') + ->first(); + $latestDate = trim((string) ($row->latest_date ?? '')); + if ($latestDate === '') { + continue; + } + + $days = now()->startOfDay()->diffInDays(Carbon::parse($latestDate)->startOfDay()); + if ($days < 1) { + continue; + } + + $warnings[] = [ + 'warning_key' => 'crawler-stale:' . $source['table'], + 'warning_type' => self::TYPE_STALE, + 'risk_level' => 'high', + 'related_object' => $source['label'], + 'risk_description' => sprintf('%s 已 %d 天没有新抓取数据,最新抓取日期 %s。', $source['label'], $days, $latestDate), + 'evidence' => sprintf('最新 crawl_date=%s,最新 update_time=%s,累计 %d 行。', $latestDate, (string) ($row->latest_update ?? ''), (int) ($row->row_count ?? 0)), + 'impact' => sprintf('%d 天未更新', $days), + 'trigger_rule' => 'R7', + 'due_at' => now()->addDay(), + ]; + } + + return $warnings; + } + + /** @return list> */ + private function subjectNameWarnings($connection): array + { + if (! $connection->getSchemaBuilder()->hasTable('ops_crawl_shop_company')) { + return []; + } + + $companies = collect($connection->table('ops_fys_company_info') + ->select(['unifiedSocialCreditCode', 'businessLicenseName']) + ->get()) + ->keyBy(fn (object $row): string => trim((string) $row->unifiedSocialCreditCode)); + + $seen = []; + $warnings = []; + foreach ($this->latestCompanyRows($connection) as $row) { + $taxNo = trim((string) ($row->tax_no ?? '')); + $crawlName = trim((string) ($row->company_name ?? '')); + if ($taxNo === '' || $crawlName === '' || ! $companies->has($taxNo)) { + continue; + } + + $officialName = trim((string) $companies->get($taxNo)->businessLicenseName); + if ($officialName === '' || $officialName === $crawlName) { + continue; + } + + $key = $taxNo . '|' . $crawlName; + if (isset($seen[$key])) { + continue; + } + $seen[$key] = true; + + $warnings[] = [ + 'warning_key' => 'subject-name:' . md5($key), + 'warning_type' => self::TYPE_SUBJECT_NAME, + 'risk_level' => 'low', + 'related_object' => (string) ($row->shop_name ?? ''), + 'risk_description' => sprintf('店铺「%s」爬虫主体名与工商主体名不一致。', (string) $row->shop_name), + 'evidence' => sprintf('tax_no=%s,爬虫=%s,工商=%s', $taxNo, $crawlName, $officialName), + 'impact' => '名称核对', + 'trigger_rule' => 'R8', + 'due_at' => now()->addDays(7), + ]; + } + + return $warnings; + } + + /** @return list> */ + private function trademarkWarnings($connection): array + { + if (! $connection->getSchemaBuilder()->hasTable('ops_crawl_shop_trademark')) { + return []; + } + + $known = collect($connection->table('ops_fys_trademark_info') + ->where('isDelete', 0) + ->pluck('trademarkNumber')) + ->map(static fn (mixed $value): string => trim((string) $value)) + ->filter() + ->flip(); + + $seen = []; + $warnings = []; + foreach ($this->latestTrademarkRows($connection) as $row) { + $number = trim((string) ($row->trademark_no ?? '')); + if ($number === '' || $known->has($number) || isset($seen[$number])) { + continue; + } + $seen[$number] = true; + + $warnings[] = [ + 'warning_key' => 'trademark-missing:' . md5($number), + 'warning_type' => self::TYPE_TRADEMARK_MISSING, + 'risk_level' => 'mid', + 'related_object' => $number, + 'risk_description' => sprintf('爬虫商标号 %s 在商标基础资料中查不到对应记录。', $number), + 'evidence' => sprintf('店铺=%s,商标名称=%s', (string) ($row->shop_name ?? ''), (string) ($row->trademark_name ?? '')), + 'impact' => '商标资料缺失', + 'trigger_rule' => 'R9', + 'due_at' => now()->addDays(3), + ]; + } + + return $warnings; + } + + /** @return list */ + private function latestCompanyRows($connection): array + { + return $connection->table('ops_crawl_shop_company') + ->orderBy('shop_name') + ->orderByDesc('crawl_date') + ->orderByDesc('id') + ->get() + ->groupBy(static fn (object $row): string => (string) $row->shop_name) + ->map(static fn ($rows): object => $rows->first()) + ->values() + ->all(); + } + + /** @return list */ + private function latestTrademarkRows($connection): array + { + return $connection->table('ops_crawl_shop_trademark') + ->orderBy('shop_name') + ->orderByDesc('crawl_date') + ->orderByDesc('id') + ->get() + ->groupBy(static fn (object $row): string => (string) $row->shop_name) + ->flatMap(static fn ($rows) => $rows->groupBy(static fn (object $row): string => trim((string) $row->trademark_no))->map(static fn ($same): object => $same->first())) + ->values() + ->all(); + } + + private function responsiblePerson(string $type): ?string + { + $configured = trim((string) config('upstream_mirrors.crawler_warnings.responsible.' . $type, '')); + if ($configured !== '') { + return $configured; + } + + try { + $user = User::query()->where('enabled', true)->orderBy('id')->first(); + + return $user !== null ? (string) $user->name : (self::RESPONSIBLE_BY_TYPE[$type] ?? '数据责任人'); + } catch (Throwable $exception) { + Log::warning('预警责任人回退到默认标签。', ['exception' => $exception::class]); + + return self::RESPONSIBLE_BY_TYPE[$type] ?? '数据责任人'; + } + } +} diff --git a/modules/EcommerceFinance/Services/DataLogicSettings.php b/modules/EcommerceFinance/Services/DataLogicSettings.php new file mode 100644 index 00000000..8d70af6b --- /dev/null +++ b/modules/EcommerceFinance/Services/DataLogicSettings.php @@ -0,0 +1,105 @@ + + */ + public static function defaults(): array + { + return [ + 'crawl_priority' => '1', + 'trademark_separator' => '、', + 'same_day_conflict_policy' => 'crawl_wins', + 'missing_trademark_action' => 'warn_keep', + 'stale_warning_enabled' => '1', + 'stale_after_days' => '1', + 'owner_stale' => '', + 'owner_subject_name' => '', + 'owner_subject_conflict' => '', + 'owner_trademark' => '', + ]; + } + + /** + * @return array + */ + public static function current(): array + { + $stored = setting(self::KEY, []); + if (is_string($stored)) { + $decoded = json_decode($stored, true); + $stored = is_array($decoded) ? $decoded : []; + } + + return array_merge(self::defaults(), is_array($stored) ? $stored : []); + } + + public static function value(string $key, mixed $default = null): mixed + { + $current = self::current(); + + return $current[$key] ?? ($default ?? (self::defaults()[$key] ?? null)); + } + + /** @param array $values */ + public static function save(array $values): void + { + $clean = []; + foreach (self::defaults() as $key => $default) { + if (! array_key_exists($key, $values)) { + continue; + } + $clean[$key] = mb_substr(trim((string) $values[$key]), 0, 191); + } + + setting([self::KEY => array_merge(self::current(), $clean)])->save(); + } + + public static function separator(): string + { + $value = trim((string) self::value('trademark_separator', '、')); + + return $value === '' ? '、' : $value; + } + + public static function crawlWinsOnConflict(): bool + { + return (string) self::value('same_day_conflict_policy', 'crawl_wins') === 'crawl_wins'; + } + + public static function staleWarningEnabled(): bool + { + return (string) self::value('stale_warning_enabled', '1') === '1'; + } + + public static function staleAfterDays(): int + { + return max((int) self::value('stale_after_days', 1), 1); + } + + public static function ownerFor(string $warningType): string + { + $map = [ + CrawlerDataGuard::TYPE_STALE => 'owner_stale', + CrawlerDataGuard::TYPE_SUBJECT_NAME => 'owner_subject_name', + CrawlerDataGuard::TYPE_SUBJECT_CONFLICT => 'owner_subject_conflict', + CrawlerDataGuard::TYPE_TRADEMARK_MISSING => 'owner_trademark', + ]; + $key = $map[$warningType] ?? null; + + return $key === null ? '' : trim((string) self::value($key, '')); + } +} diff --git a/modules/EcommerceFinance/Services/ShopEntityTrademarkLogic.php b/modules/EcommerceFinance/Services/ShopEntityTrademarkLogic.php new file mode 100644 index 00000000..3198fe68 --- /dev/null +++ b/modules/EcommerceFinance/Services/ShopEntityTrademarkLogic.php @@ -0,0 +1,120 @@ + 规则列表。 + * + * @return array>> + */ + public static function rules(): array + { + return [ + '数据来源与优先级' => [ + [ + 'code' => 'R1', + 'object' => '店铺主体、店铺商标', + 'logic' => '优先使用每天从店铺后台抓取的数据;爬虫没有覆盖的店铺回退到财务系统流程数据(统一社会信用代码关联主体、商标号关联商标)。', + 'source' => 'crawl_shop_company / crawl_shop_trademark(爬虫)', + 'priority' => '爬虫优先,流程数据兜底', + ], + [ + 'code' => 'R2', + 'object' => '店铺匹配与主体归一键', + 'logic' => '爬虫 shop_name 与店铺基础信息 erpShopName 精确匹配;主体按 tax_no 解析工商主体名称后再关联,保证与主体维度经营报表的合并键一致。', + 'source' => 'crawl_shop_company + fys_shopInfo + fys_companyInfo', + 'priority' => '精确匹配,不做模糊匹配', + ], + [ + 'code' => 'R3', + 'object' => '多商标展示', + 'logic' => '同一店铺存在多个商标时,商标名称和商标号都按同一顺序用「、」串联,两个字段一一对应。', + 'source' => 'crawl_shop_trademark', + 'priority' => '名称与号码同源、同顺序', + ], + [ + 'code' => 'R4', + 'object' => '商标号展示与查询', + 'logic' => '商标号和商标名同时切换数据来源,展示、筛选、排序、导出四处使用同一口径。', + 'source' => 'crawl_shop_trademark 优先,原商标号链路兜底', + 'priority' => '同源同口径', + ], + ], + '期间切割与冲突处理' => [ + [ + 'code' => 'R5', + 'object' => '店铺主体生效期间', + 'logic' => '爬虫最新一次抓取的主体与当前存续期间不一致时,以抓到变化的 crawl_date 作为新主体生效日;老主体到同一天 00:00 结束,收入按左闭右开区间归集。', + 'source' => 'crawl_shop_company + store_report_lifecycle_rows', + 'priority' => '爬虫驱动期间新增', + ], + [ + 'code' => 'R6', + 'object' => '同日主体冲突', + 'logic' => '流程表与爬虫同一天给出不同主体时,以爬虫为准直接调整期间,同时生成预警并指派责任人人工复审;复审驳回后改回流程主体并重算该店铺期间和收入归属。', + 'source' => 'crawl_shop_company + fys_shopInfo_flow', + 'priority' => '爬虫强制覆盖 + 人工复审', + ], + ], + '数据质量监控' => [ + [ + 'code' => 'R7', + 'object' => '爬虫更新检测', + 'logic' => '最新抓取日期距今超过 1 天,或数据更新时间停止前进时生成预警。', + 'source' => 'crawl_shop_company / crawl_shop_trademark 的 crawl_date、update_time', + 'priority' => '每天检查', + ], + [ + 'code' => 'R8', + 'object' => '主体名称一致性', + 'logic' => '爬虫公司名与 tax_no 对应工商主体名不一致时生成预警,保留人工核对。', + 'source' => 'crawl_shop_company + fys_companyInfo', + 'priority' => '每天检查', + ], + [ + 'code' => 'R9', + 'object' => '关联完整性', + 'logic' => '主体期间重复存续、期间断档或重叠,以及爬虫主体/商标在财务系统查不到对应资料时生成预警。', + 'source' => 'store_report_lifecycle_rows + fys_companyInfo + fys_TrademarkInfo', + 'priority' => '每天检查', + ], + ], + ]; + } + + /** + * 扁平规则行,供页面表格渲染。 + * + * @return list> + */ + public static function rows(): array + { + $rows = []; + foreach (self::rules() as $category => $rules) { + foreach ($rules as $rule) { + $rows[] = [ + 'rule_code' => $rule['code'], + 'rule_category' => $category, + 'rule_object' => $rule['object'], + 'rule_logic' => $rule['logic'], + 'data_source' => $rule['source'], + 'priority_note' => $rule['priority'], + ]; + } + } + + return $rows; + } +} diff --git a/modules/EcommerceFinance/Services/StoreReportLogic.php b/modules/EcommerceFinance/Services/StoreReportLogic.php new file mode 100644 index 00000000..76a4dd2f --- /dev/null +++ b/modules/EcommerceFinance/Services/StoreReportLogic.php @@ -0,0 +1,83 @@ +>> + */ + public static function rules(): array + { + return [ + '主体期间来源' => [ + [ + 'code' => 'R1', + 'object' => '期初数据(已变更店铺)', + 'logic' => '变更店铺由负责人提供的期初数据锁定:老主体 2026-07-01 生效、失效时间为店铺变更到新主体的前一天;新主体从变更当天生效,失效时间显示「存续中」。', + 'source' => '店铺变更初始数据(' . self::INITIAL_PERIODS_CONFIG . ')', + 'priority' => '期初数据优先,覆盖流程历史', + ], + [ + 'code' => 'R2', + 'object' => '其他店铺的期初期间', + 'logic' => '未列入期初数据的店铺自 2026-07-01 起按财务系统登记主体生成一条期间,失效时间显示「存续中」;系统主体缺失时退回店铺基础信息页展示的主体。', + 'source' => 'ops_fys_shop_info + ops_fys_company_info', + 'priority' => '一条期间,从 2026-07-01 起', + ], + [ + 'code' => 'R3', + 'object' => '爬虫抓到的新主体', + 'logic' => '爬虫主体先按统一社会信用代码归一到工商主体名称;与当前期间最后一个主体不同时生成新期间,生效时间为首次抓到日期,老期间失效时间改为前一天,新期间失效时间显示「存续中」。', + 'source' => 'crawl_shop_company + store_report_shop_period_changes', + 'priority' => '爬虫驱动新增期间,日期一经记录不再前移', + ], + [ + 'code' => 'R4', + 'object' => '主体名称笔误', + 'logic' => '爬虫名称与系统名称不同、但税号归一到同一工商主体时不算主体变更,只保留名称不一致预警,不新增期间。', + 'source' => 'crawl_shop_company + fys_companyInfo', + 'priority' => '同税号视为同一主体', + ], + ], + '期间切割与展示' => [ + [ + 'code' => 'R5', + 'object' => '一行一期间', + 'logic' => '同一店铺的每个主体期间在报表中只显示一行,不再展开流程表历史记录;期间按日期严格衔接,不重复、不留空档。', + 'source' => 'StoreReportPeriodBuilder', + 'priority' => '去重后按主体期间展示', + ], + [ + 'code' => 'R6', + 'object' => '流程历史行的处理', + 'logic' => '报表期间不再直接读取 fys_shopInfo_flow 的原始行(历史重复行来源);流程数据仅作备查和同日冲突预警,不参与期间生成。', + 'source' => 'store_report_lifecycle_rows(备查)', + 'priority' => '不作为期间来源', + ], + [ + 'code' => 'R7', + 'object' => '关店与非正常状态', + 'logic' => '店铺状态为关店、关店中或其他非正常状态时,生效时间和失效时间都显示「-」;只有正常在营店铺展示期间日期。', + 'source' => 'ops_fys_shop_info.shopState', + 'priority' => '状态优先于期间展示', + ], + [ + 'code' => 'R8', + 'object' => '收入归属', + 'logic' => '期间销量按日汇总的实际发生日期归属:变更日当天归新主体,老主体统计到变更日前一天,主体切换不重复统计、不少计收入。', + 'source' => 'store_report_daily_rows + 主体期间', + 'priority' => '按日归属,区间不重叠', + ], + ], + ]; + } +} diff --git a/modules/EcommerceFinance/Services/StoreReportMirrorSynchronizer.php b/modules/EcommerceFinance/Services/StoreReportMirrorSynchronizer.php index a09bfa9f..1b874fba 100644 --- a/modules/EcommerceFinance/Services/StoreReportMirrorSynchronizer.php +++ b/modules/EcommerceFinance/Services/StoreReportMirrorSynchronizer.php @@ -24,7 +24,7 @@ class StoreReportMirrorSynchronizer ); } - /** @return array{lifecycles:array,bills:array,daily:array} */ + /** @return array{lifecycles:array,period_changes:array,bills:array,daily:array} */ public function sync( ?int $batchSize = null, ?int $maxBatches = null, @@ -47,6 +47,7 @@ class StoreReportMirrorSynchronizer fn (int $cursor, int $limit): array => $this->upstream->lifecycleBatch($cursor, $limit), fn (array $rows, string $generation): array => $this->mapLifecycles($rows, $generation), ); + $periodChanges = $this->syncShopPeriodChanges(); $bills = $this->syncDataset( StoreReportRepository::BILLS_DATASET, 'store_report_bill_rows', @@ -58,11 +59,98 @@ class StoreReportMirrorSynchronizer return [ 'lifecycles' => $lifecycles, + 'period_changes' => $periodChanges, 'bills' => $bills, 'daily' => $this->syncDailySummary($dailyBatchSize, $dailyMaxBatches), ]; } + /** + * 把爬虫首次抓到的新主体写入店铺主体期间变更记录。 + * + * 爬虫主体名称先按税号归一到工商主体资料,避免同一主体的名称笔误被当成变更; + * 只有与当前期间最后一个主体不同才新增记录,重复执行不会重复插入, + * 记录下来的首次抓到日期会固定新主体的生效时间,后续爬虫更新不再前移。 + * + * @return array{status:string,checked:int,created:int,reason?:string} + */ + private function syncShopPeriodChanges(): array + { + $schema = Schema::connection($this->connection->getName()); + if (! $schema->hasTable('ops_crawl_shop_company')) { + return ['status' => 'skipped', 'checked' => 0, 'created' => 0, 'reason' => 'crawl_mirror_not_ready']; + } + if (! $schema->hasTable(StoreReportPeriodBuilder::CHANGES_TABLE)) { + return ['status' => 'skipped', 'checked' => 0, 'created' => 0, 'reason' => 'change_table_missing']; + } + + $ranked = $this->connection->table('ops_crawl_shop_company') + ->selectRaw('id, shop_name, company_name, tax_no, crawl_date, row_number() over (partition by shop_name order by crawl_date desc, id desc) AS row_rank'); + $latest = $this->connection->table($ranked, 'crawler_latest') + ->where('row_rank', 1) + ->get(['shop_name', 'company_name', 'tax_no', 'crawl_date']); + if ($latest->isEmpty()) { + return ['status' => 'succeeded', 'checked' => 0, 'created' => 0]; + } + + $masterNames = []; + if ($schema->hasTable('ops_fys_company_info')) { + $rankedCompanies = $this->connection->table('ops_fys_company_info') + ->selectRaw('unifiedSocialCreditCode, businessLicenseName, row_number() over (partition by unifiedSocialCreditCode order by Id desc) AS row_rank'); + foreach ($this->connection->table($rankedCompanies, 'company_latest') + ->where('row_rank', 1) + ->get(['unifiedSocialCreditCode', 'businessLicenseName']) as $row) { + $code = trim((string) ($row->unifiedSocialCreditCode ?? '')); + $name = trim((string) ($row->businessLicenseName ?? '')); + if ($code !== '' && $name !== '') { + $masterNames[$code] = $name; + } + } + } + + $changes = []; + foreach ($latest as $row) { + $name = trim((string) ($row->shop_name ?? '')); + $taxNo = trim((string) ($row->tax_no ?? '')); + $company = $masterNames[$taxNo] ?? trim((string) ($row->company_name ?? '')); + $observed = substr(trim((string) ($row->crawl_date ?? '')), 0, 10); + if ($name === '' || $company === '' || $observed === '') { + continue; + } + $changes[$name] = ['company' => $company, 'tax_no' => $taxNo, 'observed_on' => $observed]; + } + if ($changes === []) { + return ['status' => 'succeeded', 'checked' => 0, 'created' => 0]; + } + + $periods = app(StoreReportPeriodBuilder::class)->build(array_map( + static fn (string $name): array => ['erp_store_name' => $name], + array_keys($changes), + )); + + $created = 0; + foreach ($changes as $name => $change) { + $list = $periods[$name] ?? []; + $last = $list === [] ? null : $list[count($list) - 1]; + if ($this->sameCompany($last['company_name'] ?? null, $change['company'])) { + continue; + } + + $created += (int) $this->connection->table(StoreReportPeriodBuilder::CHANGES_TABLE)->insertOrIgnore([ + 'erp_store_name' => $name, + 'company_name' => $change['company'], + 'unified_social_credit_code' => $change['tax_no'] === '' ? null : $change['tax_no'], + 'observed_on' => $change['observed_on'], + 'source' => 'crawl_shop_company', + 'business_key' => hash('sha256', implode('|', [$name, $change['company'], $change['observed_on']])), + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + return ['status' => 'succeeded', 'checked' => count($changes), 'created' => $created]; + } + /** * @param callable(int,int):array> $fetch * @param callable(array>,string):array{upserts:array>,deletes:array} $map @@ -518,6 +606,14 @@ class StoreReportMirrorSynchronizer return mb_substr($value, 0, $length); } + private function sameCompany(mixed $left, mixed $right): bool + { + $left = mb_strtolower(trim((string) ($left ?? ''))); + $right = mb_strtolower(trim((string) ($right ?? ''))); + + return $left !== '' && $left === $right; + } + private function dateTime(mixed $value): ?string { $value = trim((string) ($value ?? '')); diff --git a/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php b/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php new file mode 100644 index 00000000..3126d56d --- /dev/null +++ b/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php @@ -0,0 +1,260 @@ +connection = $databases->connection( + (string) config('upstream_mirrors.connection', 'business_mirror'), + ); + } + + public static function initialEffectiveAt(): string + { + $configured = trim((string) (config('store_report_initial_periods.initial_effective_at') ?? '')); + + return $configured === '' ? self::INITIAL_EFFECTIVE_AT : $configured; + } + + /** + * @param array> $stores 报表店铺行;只需要 erp_store_name,company_name 作为系统主体缺失时的回退值。 + * @return array> + */ + public function build(array $stores): array + { + $names = []; + $fallbackCompanies = []; + $knownEntities = []; + foreach ($stores as $store) { + $name = trim((string) ($store['erp_store_name'] ?? '')); + if ($name === '') { + continue; + } + $names[$name] = $name; + $company = trim((string) ($store['company_name'] ?? '')); + if ($company !== '') { + $fallbackCompanies[$name] = $company; + } + // 店铺行已带系统主体时不再重复查询镜像;null 表示系统确实没有登记主体。 + if (array_key_exists('system_company_name', $store)) { + $knownEntities[$name] = $this->cleanCompany($store['system_company_name']); + } + } + + return $this->assemble(array_values($names), $fallbackCompanies, $knownEntities); + } + + /** + * @param list $names + * @param array $fallbackCompanies + * @param array $knownEntities + * @return array> + */ + private function assemble(array $names, array $fallbackCompanies, array $knownEntities = []): array + { + if ($names === []) { + return []; + } + + $initial = (array) config('store_report_initial_periods.shops', []); + $initialDate = self::initialEffectiveAt(); + $missing = array_values(array_diff($names, array_keys($knownEntities))); + $entities = $knownEntities + $this->systemEntities($missing); + $changes = $this->crawledChanges($names); + $periods = []; + + foreach ($names as $name) { + $rule = $initial[$name] ?? null; + $changedAt = is_array($rule) ? $this->date($rule['changed_at'] ?? null) : null; + // 变更日必须晚于期初日,否则老主体期间为空,直接按新主体从期初日起算。 + if (is_array($rule) && $changedAt !== null && $changedAt > $initialDate) { + $list = [ + [ + 'company_name' => $this->cleanCompany($rule['previous_company'] ?? null), + 'effective_at' => $initialDate, + 'expires_at' => $this->dayBefore($changedAt), + 'source' => 'initial', + ], + [ + 'company_name' => $this->cleanCompany($rule['company'] ?? null), + 'effective_at' => $changedAt, + 'expires_at' => null, + 'source' => 'initial', + ], + ]; + } else { + $list = [[ + 'company_name' => (is_array($rule) ? $this->cleanCompany($rule['company'] ?? null) : null) + ?? ($entities[$name] ?? ($fallbackCompanies[$name] ?? null)), + 'effective_at' => $initialDate, + 'expires_at' => null, + 'source' => 'baseline', + ]]; + } + + foreach ($changes[$name] ?? [] as $change) { + $company = $this->cleanCompany($change['company_name'] ?? null); + $observed = $this->date($change['observed_on'] ?? null); + if ($company === null || $observed === null || $observed < $initialDate) { + continue; + } + + $last = &$list[count($list) - 1]; + if ($this->sameCompany($last['company_name'], $company)) { + continue; + } + // 变更必须晚于当前期间生效日,避免爬虫记录早于期初数据时产生重叠。 + if (strcmp($observed, (string) $last['effective_at']) <= 0) { + continue; + } + + $last['expires_at'] = $this->dayBefore($observed); + $list[] = [ + 'company_name' => $company, + 'effective_at' => $observed, + 'expires_at' => null, + 'source' => 'crawl', + ]; + unset($last); + } + unset($last); + + $periods[$name] = $list; + } + + return $periods; + } + + /** + * 财务系统登记的店铺主体(与 rpt.fys_shopInfo + fys_companyInfo 一致)。 + * + * @param list $names + * @return array + */ + private function systemEntities(array $names): array + { + $entities = []; + foreach (array_chunk($names, 500) as $chunk) { + $placeholders = implode(',', array_fill(0, count($chunk), '?')); + try { + $rows = $this->connection->select( + "SELECT s.erpShopName AS store_name, c.businessLicenseName AS company_name + FROM ops_fys_shop_info AS s + LEFT JOIN ( + SELECT unifiedSocialCreditCode, MAX(Id) AS latest_id + FROM ops_fys_company_info + WHERE unifiedSocialCreditCode IS NOT NULL + GROUP BY unifiedSocialCreditCode + ) AS latest ON latest.unifiedSocialCreditCode = s.unifiedSocialCreditCode + LEFT JOIN ops_fys_company_info AS c ON c.Id = latest.latest_id + WHERE s.erpShopName IN ({$placeholders})", + $chunk, + ); + } catch (Throwable) { + return []; + } + foreach ($rows as $row) { + $name = trim((string) ($row->store_name ?? '')); + $company = $this->cleanCompany($row->company_name ?? null); + if ($name !== '' && $company !== null) { + $entities[$name] = $company; + } + } + } + + return $entities; + } + + /** + * 爬虫首次抓到的新主体记录,按抓到日期升序构成后续期间。 + * + * @param list $names + * @return array> + */ + private function crawledChanges(array $names): array + { + $changes = []; + foreach (array_chunk($names, 500) as $chunk) { + $placeholders = implode(',', array_fill(0, count($chunk), '?')); + try { + $rows = $this->connection->select( + "SELECT erp_store_name, company_name, observed_on + FROM " . self::CHANGES_TABLE . " + WHERE erp_store_name IN ({$placeholders}) + ORDER BY observed_on ASC, id ASC", + $chunk, + ); + } catch (Throwable) { + return []; + } + foreach ($rows as $row) { + $name = trim((string) ($row->erp_store_name ?? '')); + if ($name === '') { + continue; + } + $changes[$name][] = [ + 'company_name' => $row->company_name ?? null, + 'observed_on' => $row->observed_on ?? null, + ]; + } + } + + return $changes; + } + + private function cleanCompany(mixed $value): ?string + { + $company = trim((string) ($value ?? '')); + + return $company === '' ? null : $company; + } + + private function sameCompany(?string $left, ?string $right): bool + { + return mb_strtolower(trim((string) $left)) === mb_strtolower(trim((string) $right)); + } + + private function date(mixed $value): ?string + { + $value = trim((string) ($value ?? '')); + if ($value === '') { + return null; + } + + try { + return Carbon::parse($value)->format('Y-m-d'); + } catch (Throwable) { + return null; + } + } + + private function dayBefore(string $date): string + { + return Carbon::parse($date)->subDay()->format('Y-m-d'); + } +} diff --git a/resources/views/business/pages/partials/prototype-page.blade.php b/resources/views/business/pages/partials/prototype-page.blade.php index e98cb3dc..70a75315 100644 --- a/resources/views/business/pages/partials/prototype-page.blade.php +++ b/resources/views/business/pages/partials/prototype-page.blade.php @@ -13,7 +13,15 @@ ], 'warnings' => [ 'title' => '预警页面', 'description' => '集中查看经营、税务、人员与资质风险', - 'columns' => ['预警编号', '风险等级', '类型', '关联对象', '影响金额', '当前状态'], + 'columns' => ['预警编号', '预警类型', '风险等级', '关联对象', '风险描述', '数据依据', '影响', '触发规则', '触发时间', '责任人', '截止时间', '处理状态', '复审人', '复审时间', '复审备注'], + ], + 'entity-trademark-data-logic' => [ + 'title' => '店铺主体商标数据逻辑配置', 'description' => '店铺主体与商标数据的来源优先级、期间切割和冲突处理规则', + 'columns' => ['规则编号', '规则类别', '规则对象', '规则逻辑', '数据来源', '优先级说明', '最近命中次数', '生效时间', '状态'], + ], + 'store-report-data-logic' => [ + 'title' => '店铺维度报表逻辑配置', 'description' => '店铺维度报表的期初数据、爬虫新主体和生效/失效时间规则', + 'columns' => ['规则编号', '规则类别', '规则对象', '规则逻辑', '数据来源', '优先级说明', '状态'], ], 'tasks' => [ 'title' => '待办事项', 'description' => '跟进任务节点、责任人与处理时限', diff --git a/resources/views/business/pages/trademark-management/entity-trademark-data-logic.blade.php b/resources/views/business/pages/trademark-management/entity-trademark-data-logic.blade.php new file mode 100644 index 00000000..d7d87220 --- /dev/null +++ b/resources/views/business/pages/trademark-management/entity-trademark-data-logic.blade.php @@ -0,0 +1,207 @@ +@php + use Modules\EcommerceFinance\Services\ShopEntityTrademarkLogic; + use Illuminate\Support\Facades\DB; + + $rules = ShopEntityTrademarkLogic::rules(); + $sources = [ + [ + 'name' => '店铺对应主体(爬虫)', + 'object' => 'rpt.crawl_shop_company', + 'frequency' => '每天抓取', + 'notice' => '最新抓取日期、更新时间停止前进时报警并指派责任人。', + ], + [ + 'name' => '店铺对应商标(爬虫)', + 'object' => 'rpt.crawl_shop_trademark', + 'frequency' => '每天抓取', + 'notice' => '多商标按同一顺序用「、」串联名称和号码。', + ], + [ + 'name' => '流程主体期间', + 'object' => 'fys_shopInfo_flow', + 'frequency' => '流程触发', + 'notice' => '爬虫没有覆盖的店铺继续沿用;同日冲突以爬虫为准并留预警。', + ], + [ + 'name' => '工商主体资料', + 'object' => 'fys_companyInfo', + 'frequency' => '只读', + 'notice' => '用于按 tax_no 归一主体名称。', + ], + [ + 'name' => '商标基础资料', + 'object' => 'fys_TrademarkInfo', + 'frequency' => '只读', + 'notice' => '爬虫商标号查不到资料时报警,但页面仍显示爬虫结果。', + ], + ]; + + // 数据源新鲜度:直接读共享业务镜像的同步状态,页面不访问上游。 + $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); + $sourceHealth = []; + foreach ([ + '店铺对应主体(爬虫)' => ['table' => 'ops_crawl_shop_company', 'dataset' => ShopEntityTrademarkLogic::CRAWL_COMPANY_DATASET], + '店铺对应商标(爬虫)' => ['table' => 'ops_crawl_shop_trademark', 'dataset' => ShopEntityTrademarkLogic::CRAWL_TRADEMARK_DATASET], + ] as $name => $meta) { + $health = ['status' => '尚未首次同步', 'latest_date' => null, 'latest_update' => null, 'shops' => 0]; + try { + if ($connection->getSchemaBuilder()->hasTable($meta['table'])) { + $row = $connection->table($meta['table']) + ->selectRaw('MAX(crawl_date) AS latest_date, MAX(update_time) AS latest_update, COUNT(DISTINCT shop_name) AS shops') + ->first(); + $health['latest_date'] = $row->latest_date ?? null; + $health['latest_update'] = $row->latest_update ?? null; + $health['shops'] = (int) ($row->shops ?? 0); + if ($health['latest_date'] !== null) { + $days = now()->startOfDay()->diffInDays(\Illuminate\Support\Carbon::parse($health['latest_date'])->startOfDay()); + $health['status'] = $days < 1 ? '今天已更新' : ($days === 1 ? '昨天更新' : $days . ' 天未更新'); + } + } + } catch (\Throwable) { + $health['status'] = '读取失败'; + } + $sourceHealth[$name] = $health; + } +@endphp + + + +
+
+ 本页是店铺主体、店铺商标数据当前实际使用的运行口径。实现与页面说明不一致时以负责人确认后的口径为准; + 发生同日主体冲突、爬虫断更、期间异常或关联缺失时,系统按这些规则强制处理并生成预警,由指定责任人人工复审。 +
+ +
+

数据来源

+ + + + + + + + + + + + @foreach ($sources as $source) + + + + + + + + @endforeach + +
来源上游对象更新频率规则要点当前状态
{{ $source['name'] }}{{ $source['object'] }}{{ $source['frequency'] }}{{ $source['notice'] }} + @php $health = $sourceHealth[$source['name']] ?? null; @endphp + @if ($health === null) + 只读 + @else + {{ $health['status'] }} + @if ($health['latest_date'] !== null) +
+ 最新抓取 {{ $health['latest_date'] }}
+ 更新时间 {{ $health['latest_update'] }}
+ 覆盖店铺 {{ $health['shops'] }} +
+ @endif + @endif +
+
+ +
+

规则清单

+ + + + + + + + + + + + + + @foreach ($rules as $category => $categoryRules) + @foreach ($categoryRules as $rule) + + + + + + + + + + @endforeach + @endforeach + +
规则编号规则类别规则对象规则逻辑数据来源优先级说明状态
{{ $rule['code'] }}{{ $category }}{{ $rule['object'] }}{{ $rule['logic'] }}{{ $rule['source'] }}{{ $rule['priority'] }}已生效
+
+
diff --git a/resources/views/business/pages/trademark-management/store-report-data-logic.blade.php b/resources/views/business/pages/trademark-management/store-report-data-logic.blade.php new file mode 100644 index 00000000..0fc0cd15 --- /dev/null +++ b/resources/views/business/pages/trademark-management/store-report-data-logic.blade.php @@ -0,0 +1,324 @@ +@php + use Illuminate\Support\Facades\DB; + use Modules\EcommerceFinance\Services\StoreReportLogic; + use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder; + + $rules = StoreReportLogic::rules(); + $initialDate = StoreReportPeriodBuilder::initialEffectiveAt(); + $initialShops = (array) config('store_report_initial_periods.shops', []); + $changeDates = array_filter(array_map( + static fn (array $rule): string => (string) ($rule['changed_at'] ?? ''), + $initialShops, + )); + sort($changeDates); + + $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); + $schema = $connection->getSchemaBuilder(); + $changeTotal = 0; + $health = [ + 'stores' => ['status' => '尚未同步', 'detail' => ''], + 'crawler' => ['status' => '尚未同步', 'detail' => ''], + 'changes' => ['status' => '尚未生成', 'detail' => ''], + 'daily' => ['status' => '尚未同步', 'detail' => ''], + ]; + + try { + if ($schema->hasTable('ops_fys_shop_info')) { + $row = $connection->table('ops_fys_shop_info') + ->selectRaw('COUNT(*) AS total, SUM(CASE WHEN shopState = 0 THEN 1 ELSE 0 END) AS normal') + ->first(); + $health['stores'] = [ + 'status' => '已同步', + 'detail' => sprintf('店铺 %d 家,其中在营 %d 家', (int) ($row->total ?? 0), (int) ($row->normal ?? 0)), + ]; + } + + if ($schema->hasTable('ops_crawl_shop_company')) { + $row = $connection->table('ops_crawl_shop_company') + ->selectRaw('COUNT(DISTINCT shop_name) AS shops, MAX(crawl_date) AS latest_date') + ->first(); + $health['crawler'] = [ + 'status' => '已同步', + 'detail' => sprintf( + '覆盖店铺 %d 家,最新抓取 %s', + (int) ($row->shops ?? 0), + (string) ($row->latest_date ?? '暂无'), + ), + ]; + } + + if ($schema->hasTable(StoreReportPeriodBuilder::CHANGES_TABLE)) { + $row = $connection->table(StoreReportPeriodBuilder::CHANGES_TABLE) + ->selectRaw('COUNT(*) AS total, COUNT(DISTINCT erp_store_name) AS shops, MAX(observed_on) AS latest_date') + ->first(); + $health['changes'] = [ + 'status' => '已生效', + 'total' => (int) ($row->total ?? 0), + 'detail' => sprintf( + '变更记录 %d 条,涉及店铺 %d 家,最近 %s', + (int) ($row->total ?? 0), + (int) ($row->shops ?? 0), + (string) ($row->latest_date ?? '暂无'), + ), + ]; + $changeTotal = (int) ($row->total ?? 0); + } + + if ($schema->hasTable('store_report_daily_rows')) { + $row = $connection->table('store_report_daily_rows') + ->selectRaw('MAX(occurred_on) AS latest_date, COUNT(DISTINCT store_name) AS shops') + ->first(); + $health['daily'] = [ + 'status' => '已同步', + 'detail' => sprintf( + '日汇总覆盖店铺 %d 家,最新日期 %s', + (int) ($row->shops ?? 0), + (string) ($row->latest_date ?? '暂无'), + ), + ]; + } + } catch (\Throwable) { + // 镜像暂时不可用时页面保留规则说明,不阻塞查看配置。 + } + + $recentChanges = collect(); + try { + if ($schema->hasTable(StoreReportPeriodBuilder::CHANGES_TABLE)) { + $recentChanges = $connection->table(StoreReportPeriodBuilder::CHANGES_TABLE) + ->orderByDesc('observed_on') + ->orderByDesc('id') + ->limit(20) + ->get(); + } + } catch (\Throwable) { + $recentChanges = collect(); + } +@endphp + + + +
+
+ 本页是店铺维度报表当前实际使用的期间口径:报表按「期初数据 + 财务系统主体 + 爬虫新主体」生成店铺主体期间, + 用于把店铺销量拆分到不同主体,避免主体少申报或多申报收入。同一店铺同一期间只显示一行, + 变更日当天归新主体,老主体统计到变更日前一天;关店和非正常状态的两列时间显示「-」。 +
+ +
+
+ 期初起始日 + {{ $initialDate }} +
+
+ 期初变更店铺 + {{ count($initialShops) }} 家 +
+
+ 期初变更日期范围 + {{ $changeDates === [] ? '暂无' : reset($changeDates) . ' 至 ' . end($changeDates) }} +
+
+ 爬虫已识别新主体 + {{ $changeTotal }} 条 +
+
+ +
+

数据来源

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
来源数据对象更新方式规则要点当前状态
期初数据{{ StoreReportLogic::INITIAL_PERIODS_CONFIG }}负责人维护已变更店铺的老主体、新主体和变更日期,是期间计算的基准。 + 已生效 +
变更店铺 {{ count($initialShops) }} 家
+
店铺主体(财务系统)ops_fys_shop_info + ops_fys_company_info共享镜像未列入期初数据的店铺按系统登记主体自 2026-07-01 起生成期间。 + {{ $health['stores']['status'] }} +
{{ $health['stores']['detail'] }}
+
店铺对应主体(爬虫)ops_crawl_shop_company每天抓取爬虫主体按税号归一到工商主体名称,名称笔误不产生新期间。 + {{ $health['crawler']['status'] }} +
{{ $health['crawler']['detail'] }}
+
爬虫主体变更记录{{ StoreReportPeriodBuilder::CHANGES_TABLE }}同步任务写入记录首次抓到的新主体日期,固定新主体生效时间,后续抓取不再前移。 + {{ $health['changes']['status'] }} +
{{ $health['changes']['detail'] }}
+
店铺日汇总store_report_daily_rows共享镜像收入按实际发生日期归属到对应主体期间,变更日当天归新主体。 + {{ $health['daily']['status'] }} +
{{ $health['daily']['detail'] }}
+
+
+ +
+

规则清单

+ + + + + + + + + + + + + + @foreach ($rules as $category => $categoryRules) + @foreach ($categoryRules as $rule) + + + + + + + + + + @endforeach + @endforeach + +
规则编号规则类别规则对象规则逻辑数据来源优先级说明状态
{{ $rule['code'] }}{{ $category }}{{ $rule['object'] }}{{ $rule['logic'] }}{{ $rule['source'] }}{{ $rule['priority'] }}已生效
+
+ +
+

最近爬虫识别的新主体

+ + + + + + + + + + + + @forelse ($recentChanges as $change) + + + + + + + + @empty + + + + @endforelse + +
店铺新主体统一社会信用代码首次抓到日期生成期间
{{ $change->erp_store_name }}{{ $change->company_name }}{{ $change->unified_social_credit_code ?: '—' }}{{ $change->observed_on }}{{ $change->observed_on }} 起,失效时间显示存续中
尚未识别到爬虫新主体。爬虫抓到的店铺主体与当前期间主体不同时,这里会记录并生成新期间。
+
+
diff --git a/resources/views/business/pages/trademark-management/warnings.blade.php b/resources/views/business/pages/trademark-management/warnings.blade.php new file mode 100644 index 00000000..a686fb93 --- /dev/null +++ b/resources/views/business/pages/trademark-management/warnings.blade.php @@ -0,0 +1,267 @@ +@php + $warningTypes = [ + ['type' => '爬虫未更新', 'level' => '高', 'trigger' => '最新抓取日期距今超过 1 天,或更新时间停止前进', 'rule' => 'R7'], + ['type' => '同日主体冲突', 'level' => '高', 'trigger' => '流程表与爬虫同一天给出不同主体,系统已按爬虫覆盖期间', 'rule' => 'R6'], + ['type' => '期间异常', 'level' => '中', 'trigger' => '同一店铺期间重复存续、断档或重叠', 'rule' => 'R9'], + ['type' => '关联缺失', 'level' => '中', 'trigger' => '爬虫主体或商标号在财务系统查不到对应资料', 'rule' => 'R9'], + ['type' => '主体名称不一致', 'level' => '低', 'trigger' => '爬虫公司名与 tax_no 对应工商主体名不一致', 'rule' => 'R8'], + ]; + $warningColumns = [ + '预警编号', '预警类型', '风险等级', '关联对象', '风险描述', '数据依据', '影响', + '触发规则', '触发时间', '责任人', '截止时间', '处理状态', '复审人', '复审时间', '复审备注', + ]; + $rows = $warningRows ?? []; + $filters = $warningFilters ?? []; + $levelLabels = ['high' => '高', 'mid' => '中', 'low' => '低']; +@endphp + + + +
+
+ 预警集中展示爬虫断更、同日主体冲突、期间异常、关联缺失和主体名称不一致四类数据问题。 + 每条预警都指派责任人并记录处理状态、复审人、复审时间和复审备注;同日主体冲突会先按爬虫强制覆盖期间,再等待责任人确认或驳回。 +
+ +
+

预警规则

+ + + + + + + + + + + @foreach ($warningTypes as $warning) + + + + + + + @endforeach + +
预警类型风险等级触发条件触发规则
{{ $warning['type'] }} + {{ $warning['level'] }} + {{ $warning['trigger'] }}{{ $warning['rule'] }}
+
+ +
+

预警记录

+
+ + + + + + + + 重置 + + 共 {{ (int) ($warningTotal ?? 0) }} 条,待复审 {{ (int) ($warningPending ?? 0) }} 条 + +
+ + + + @foreach ($warningColumns as $column) + + @endforeach + + + + + @forelse ($rows as $row) + + + + + + + + + + + + + + + + + + + @empty + + + + @endforelse + +
{{ $column }}复审动作
{{ $row['warning_number'] }}{{ $row['warning_type'] }} + + {{ $levelLabels[$row['risk_level']] ?? $row['risk_level'] }} + + {{ $row['related_object'] }}{{ $row['risk_description'] }}{{ $row['evidence'] }}{{ $row['impact'] }}{{ $row['trigger_rule'] }}{{ $row['triggered_at'] }}{{ $row['responsible_person'] }}{{ $row['due_at'] }}{{ $row['review_status_label'] }}{{ $row['reviewer'] }}{{ $row['reviewed_at'] }}{{ $row['review_note'] }} + @if ($row['review_status'] === 'pending') +
+ @csrf + + + + +
+ @else + 已处理 + @endif +
+ @if ($warningReady ?? false) + 当前没有符合条件的预警记录。 + @else + 预警数据表尚未迁移。执行数据库迁移后,这里会显示风险记录并指派责任人。 + @endif +
+
+
diff --git a/resources/views/business/pages/workspace/warnings.blade.php b/resources/views/business/pages/workspace/warnings.blade.php deleted file mode 100644 index 8eab897a..00000000 --- a/resources/views/business/pages/workspace/warnings.blade.php +++ /dev/null @@ -1 +0,0 @@ -@include('business.pages.partials.prototype-page') diff --git a/resources/views/components/layouts/admin/menu.blade.php b/resources/views/components/layouts/admin/menu.blade.php index 2de00eac..9fdbf6eb 100644 --- a/resources/views/components/layouts/admin/menu.blade.php +++ b/resources/views/components/layouts/admin/menu.blade.php @@ -163,13 +163,15 @@ ['title' => '规则信息', 'items' => [ ['trademark-management', 'subject-trademark-rules', '主体商标规则', 'rule'], ['trademark-management', 'store-change-rules', '店铺变更规则', 'rule'], + ['trademark-management', 'entity-trademark-data-logic', '店铺主体商标数据逻辑配置', 'account_tree'], + ['trademark-management', 'store-report-data-logic', '店铺维度报表逻辑配置', 'table_chart'], + ['trademark-management', 'warnings', '预警页面', 'warning_amber'], ]], ['title' => '业务进度', 'items' => [ ['company-management', 'company-progress', '公司进度表', 'timeline'], ['store-management', 'change-progress', '店铺变更进度表', 'swap_horiz'], ['trademark-management', 'trademark-progress', '商标进度表', 'format_list_bulleted'], ['workspace', 'tasks', '待办事项', 'assignment_turned_in'], - ['workspace', 'warnings', '预警页面', 'warning_amber'], ]], ['title' => '财务报表', 'items' => [ ['prototype-finance-reports', 'profit-report', '公司利润报表', 'request_quote'], diff --git a/routes/admin.php b/routes/admin.php index 822423c1..d3d2f905 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -153,6 +153,11 @@ Route::prefix('workspace/courier-payables/courier-surcharge') Route::get('workspace/{section}/{page?}/{tab?}', 'Business\Pages@show') ->name('business.pages.show'); +Route::post('workspace/trademark-management/warnings/{warning}/review', 'Business\Pages@reviewWarning') + ->middleware('business.page:trademark-management,warnings') + ->where('warning', '[0-9]+') + ->name('business.warnings.review'); + Route::post('workspace/{section}/{page}/import/{tab?}', 'Business\Pages@import') ->middleware('import') ->name('business.pages.import'); diff --git a/tests/Feature/Business/ManagementCrudTest.php b/tests/Feature/Business/ManagementCrudTest.php index 16cd5e19..4bfbe51d 100644 --- a/tests/Feature/Business/ManagementCrudTest.php +++ b/tests/Feature/Business/ManagementCrudTest.php @@ -325,7 +325,7 @@ class ManagementCrudTest extends FeatureTestCase $rulesPage = collect($sections['trademark-management']['pages'])->firstWhere('slug', 'rules-information'); $this->assertNotNull($rulesPage); $this->assertSame( - ['subject-trademark-rules', 'store-change-rules'], + ['subject-trademark-rules', 'store-change-rules', 'entity-trademark-data-logic', 'store-report-data-logic', 'warnings'], array_column($rulesPage['children'], 'slug') ); $this->assertSame( @@ -333,7 +333,7 @@ class ManagementCrudTest extends FeatureTestCase array_column($sections['store-management']['pages'], 'slug') ); $this->assertSame( - ['todos', 'warnings'], + ['todos'], array_column($sections['other']['pages'], 'slug') ); @@ -399,6 +399,35 @@ class ManagementCrudTest extends FeatureTestCase ->assertSeeText('核定使用商品') ->assertSeeText('保管部门'); + $this->get(route('business.pages.show', [ + 'section' => 'trademark-management', + 'page' => 'entity-trademark-data-logic', + ])) + ->assertOk() + ->assertSeeText('规则清单') + ->assertSeeText('爬虫优先') + ->assertSeeText('R5'); + + $this->get(route('business.pages.show', [ + 'section' => 'trademark-management', + 'page' => 'warnings', + ])) + ->assertOk() + ->assertSeeText('预警类型') + ->assertSeeText('同日主体冲突') + ->assertSeeText('责任人'); + + $this->get(route('business.pages.show', [ + 'section' => 'trademark-management', + 'page' => 'store-report-data-logic', + ])) + ->assertOk() + ->assertSeeText('店铺维度报表逻辑配置') + ->assertSeeText('规则清单') + ->assertSeeText('期初数据') + ->assertSeeText('爬虫抓到的新主体') + ->assertSeeText('R8'); + $expectedHeaders = [ ['section' => 'company-management', 'page' => 'subject-operating-report', 'headers' => ['市', '主体', '最近12个月累计收入', '1月收入', '12月收入']], ['section' => 'company-management', 'page' => 'company-cost-arrangement', 'headers' => ['公司', '公司类型', '发票类型', '当月收入预测', '往期成本缺口', '供应商成本安排', '快递成本安排', '当月税金预测', '人工成本预测', '其他费用预测']], @@ -408,7 +437,7 @@ class ManagementCrudTest extends FeatureTestCase ['section' => 'store-management', 'page' => 'store-report', 'headers' => ['店铺ID(唯一值)', '1月销量', '12月销量']], ['section' => 'store-management', 'page' => 'change-progress', 'headers' => ['接替公司', '完成时间', '办理人']], ['section' => 'other', 'page' => 'todos', 'headers' => ['待办编号', '是否逾期', '关闭日期']], - ['section' => 'other', 'page' => 'warnings', 'headers' => ['风险等级', '影响金额', '处理证据']], + ['section' => 'trademark-management', 'page' => 'warnings', 'headers' => ['预警编号', '风险等级', '责任人', '处理状态', '复审备注']], ]; foreach ($expectedHeaders as $page) { diff --git a/tests/Feature/Console/StoreReportMirrorSyncTest.php b/tests/Feature/Console/StoreReportMirrorSyncTest.php index c3604853..039f8040 100644 --- a/tests/Feature/Console/StoreReportMirrorSyncTest.php +++ b/tests/Feature/Console/StoreReportMirrorSyncTest.php @@ -115,6 +115,47 @@ class StoreReportMirrorSyncTest extends TestCase ]; } + public function test_it_records_first_seen_crawler_subject_change_once(): void + { + $mirror = DB::connection('business_mirror'); + $mirror->table('ops_fys_shop_info')->insert([ + ['erpShopName' => '店铺A', 'unifiedSocialCreditCode' => 'CODE-A', 'shopState' => '0'], + ['erpShopName' => '店铺B', 'unifiedSocialCreditCode' => 'CODE-C', 'shopState' => '0'], + ]); + $mirror->table('ops_fys_company_info')->insert([ + ['unifiedSocialCreditCode' => 'CODE-A', 'businessLicenseName' => '主体甲'], + ['unifiedSocialCreditCode' => 'CODE-B', 'businessLicenseName' => '主体乙'], + ['unifiedSocialCreditCode' => 'CODE-C', 'businessLicenseName' => '主体丙'], + ]); + $mirror->table('ops_crawl_shop_company')->insert([ + // 同一店铺历史最新一行仍是老主体,最新一行抓到新主体。 + ['id' => 1, 'shop_name' => '店铺A', 'company_name' => '主体甲', 'tax_no' => 'CODE-A', 'crawl_date' => '2026-09-28'], + ['id' => 2, 'shop_name' => '店铺A', 'company_name' => '主体乙', 'tax_no' => 'CODE-B', 'crawl_date' => '2026-09-29'], + // 名称笔误但税号相同:归一后仍是主体丙,不产生新期间。 + ['id' => 3, 'shop_name' => '店铺B', 'company_name' => '主体丙(笔误)', 'tax_no' => 'CODE-C', 'crawl_date' => '2026-09-29'], + ]); + + $upstream = $this->createMock(StoreReportUpstreamRepository::class); + $upstream->method('lifecycleBatch')->willReturn([]); + $upstream->method('billBatch')->willReturn([]); + $synchronizer = new StoreReportMirrorSynchronizer($upstream, app('db')); + + $first = $synchronizer->sync(100, 2); + $second = $synchronizer->sync(100, 2); + + $this->assertSame('succeeded', $first['period_changes']['status']); + $this->assertSame(2, $first['period_changes']['checked']); + $this->assertSame(1, $first['period_changes']['created']); + $this->assertSame(0, $second['period_changes']['created']); + $this->assertSame(1, $mirror->table('store_report_shop_period_changes')->count()); + $this->assertDatabaseHas('store_report_shop_period_changes', [ + 'erp_store_name' => '店铺A', + 'company_name' => '主体乙', + 'unified_social_credit_code' => 'CODE-B', + 'observed_on' => '2026-09-29', + ], 'business_mirror'); + } + private function activeState(string $dataset, string $generation, int $cursor, int $rows): void { DB::connection('business_mirror')->table('store_report_import_states')->insert([ @@ -185,5 +226,33 @@ class StoreReportMirrorSyncTest extends TestCase $table->dateTime('updated_at')->nullable(); $table->unique(['generation', 'store_name', 'occurred_on']); }); + $schema->create('store_report_shop_period_changes', function (Blueprint $table): void { + $table->increments('id'); + $table->string('erp_store_name'); + $table->string('company_name')->nullable(); + $table->string('unified_social_credit_code')->nullable(); + $table->date('observed_on'); + $table->string('source')->default('crawl_shop_company'); + $table->string('business_key')->unique(); + $table->timestamps(); + }); + $schema->create('ops_crawl_shop_company', function (Blueprint $table): void { + $table->increments('id'); + $table->string('shop_name'); + $table->string('company_name')->nullable(); + $table->string('tax_no')->nullable(); + $table->date('crawl_date'); + }); + $schema->create('ops_fys_shop_info', function (Blueprint $table): void { + $table->increments('Id'); + $table->string('erpShopName'); + $table->string('unifiedSocialCreditCode')->nullable(); + $table->string('shopState')->nullable(); + }); + $schema->create('ops_fys_company_info', function (Blueprint $table): void { + $table->increments('Id'); + $table->string('unifiedSocialCreditCode')->nullable(); + $table->string('businessLicenseName')->nullable(); + }); } } diff --git a/tests/Unit/EcommerceFinance/BusinessDataPlacementTest.php b/tests/Unit/EcommerceFinance/BusinessDataPlacementTest.php index 63cf644e..81723dfe 100644 --- a/tests/Unit/EcommerceFinance/BusinessDataPlacementTest.php +++ b/tests/Unit/EcommerceFinance/BusinessDataPlacementTest.php @@ -75,7 +75,7 @@ class BusinessDataPlacementTest extends TestCase $upstream = require base_path('config/upstream_mirrors.php'); $businessData = require base_path('config/business_data.php'); - $this->assertCount(52, $upstream['datasets']); + $this->assertCount(54, $upstream['datasets']); $upstreamKeys = array_keys($upstream['datasets']); $policyKeys = array_keys($businessData['datasets']); sort($upstreamKeys); diff --git a/tests/Unit/EcommerceFinance/BusinessInformationMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/BusinessInformationMirrorRepositoryTest.php index 1424a393..2df592e3 100644 --- a/tests/Unit/EcommerceFinance/BusinessInformationMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/BusinessInformationMirrorRepositoryTest.php @@ -145,6 +145,49 @@ class BusinessInformationMirrorRepositoryTest extends TestCase }); } + public function test_crawler_company_and_trademark_take_priority_over_flow_snapshot(): void + { + $schema = Schema::connection('business_mirror'); + $schema->create('ops_crawl_shop_company', function (Blueprint $table): void { + $table->integer('id'); $table->string('shop_name'); $table->string('company_name')->nullable(); + $table->string('tax_no')->nullable(); $table->string('crawl_date')->nullable(); $table->dateTime('update_time')->nullable(); + }); + $schema->create('ops_crawl_shop_trademark', function (Blueprint $table): void { + $table->integer('id'); $table->string('shop_name'); $table->string('trademark_no')->nullable(); + $table->string('trademark_name')->nullable(); $table->string('crawl_date')->nullable(); $table->dateTime('update_time')->nullable(); + }); + + $connection = DB::connection('business_mirror'); + $connection->table('ops_fys_company_info')->insert([ + $this->company(1, '工商代码', '系统登记主体'), + $this->company(2, '抓取代码', '爬虫抓取主体'), + ]); + $connection->table('ops_fys_shop_info')->insert([ + ['Id' => 21, 'openTime' => '2026-08-14 10:00:00', 'shopName' => '上游店铺名', 'erpShopName' => '测试店', 'unifiedSocialCreditCode' => '工商代码', 'trademarkNumber' => '旧商标号', 'platform' => '京东', 'responsibleDepartment' => '电商', 'shopHead' => '负责人', 'shopState' => '0', 'shopType' => '直营', 'channelType' => '线上'], + ]); + $connection->table('ops_fys_trademark_info')->insert([ + ['Id' => 9, 'trademarkNumber' => '旧商标号', 'trademarkName' => '旧商标', 'internationalClassification' => '20', 'trademarkStatus' => '1', 'registrationDate' => null, 'termOfValidity' => null, 'remark' => null, 'isDelete' => 0], + ]); + $connection->table('ops_crawl_shop_company')->insert([ + ['id' => 1, 'shop_name' => '测试店', 'company_name' => '爬虫抓取主体', 'tax_no' => '抓取代码', 'crawl_date' => '2026-09-29', 'update_time' => '2026-09-29 08:00:00'], + ]); + $connection->table('ops_crawl_shop_trademark')->insert([ + ['id' => 1, 'shop_name' => '测试店', 'trademark_no' => '1000', 'trademark_name' => '甲商标', 'crawl_date' => '2026-09-29', 'update_time' => '2026-09-29 08:00:00'], + ['id' => 2, 'shop_name' => '测试店', 'trademark_no' => '2000', 'trademark_name' => '乙商标', 'crawl_date' => '2026-09-29', 'update_time' => '2026-09-29 08:00:00'], + ]); + $this->markReady(['operations_replica.rpt.fys_shopInfo']); + + $payload = $this->repository->paginate('store-information.read', 1, 10); + $this->assertSame('爬虫抓取主体', $payload['data'][0]['company_name']); + $this->assertSame('系统登记主体', $payload['data'][0]['system_company_name']); + $this->assertSame('甲商标、乙商标', $payload['data'][0]['trademark_name']); + $this->assertSame('1000、2000', $payload['data'][0]['trademark_number']); + + $filtered = $this->repository->paginate('store-information.read', 1, 10, ['trademark_number' => '2000']); + $this->assertSame(1, $filtered['total']); + $this->assertSame('测试店', $filtered['data'][0]['erp_store_name']); + } + private function markReady(?array $keys = null): void { $keys ??= array_keys((array) config('upstream_mirrors.datasets')); diff --git a/tests/Unit/EcommerceFinance/StoreReportRepositoryFilterTest.php b/tests/Unit/EcommerceFinance/StoreReportRepositoryFilterTest.php index 1d700dd5..8a5ad8a1 100644 --- a/tests/Unit/EcommerceFinance/StoreReportRepositoryFilterTest.php +++ b/tests/Unit/EcommerceFinance/StoreReportRepositoryFilterTest.php @@ -8,11 +8,10 @@ use Illuminate\Support\Facades\Schema; use Modules\EcommerceFinance\Repositories\StoreReportRepository; use Tests\TestCase; -/** 店铺维度报表查询条件:生命周期字段按左闭右开区间过滤。 */ +/** 店铺维度报表查询条件:主体期间字段按左闭右开区间过滤。 */ class StoreReportRepositoryFilterTest extends TestCase { private StoreReportRepository $repository; - private string $lifecycleGeneration = '11111111-1111-4111-8111-111111111111'; private string $dailyGeneration = '22222222-2222-4222-8222-222222222222'; /** @var array> */ @@ -25,9 +24,20 @@ class StoreReportRepositoryFilterTest extends TestCase { parent::setUp(); - config(['database.connections.business_mirror' => [ - 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', - ], 'upstream_mirrors.connection' => 'business_mirror']); + config([ + 'database.connections.business_mirror' => [ + 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', + ], + 'upstream_mirrors.connection' => 'business_mirror', + // 店铺A 在 2026-08-20 由主体甲变更到主体乙:主体甲统计到 08-19。 + 'store_report_initial_periods.shops' => [ + '店铺A' => [ + 'previous_company' => '主体甲', + 'company' => '主体乙', + 'changed_at' => '2026-08-20', + ], + ], + ]); DB::purge('business_mirror'); $schema = Schema::connection('business_mirror'); $schema->create('store_report_import_states', function (Blueprint $table): void { @@ -35,15 +45,6 @@ class StoreReportRepositoryFilterTest extends TestCase $table->string('active_generation')->nullable(); $table->dateTime('initial_completed_at')->nullable(); }); - $schema->create('store_report_lifecycle_rows', function (Blueprint $table): void { - $table->increments('id'); - $table->string('generation'); - $table->string('business_key'); - $table->string('erp_store_name'); - $table->string('company_name')->nullable(); - $table->dateTime('effective_at')->nullable(); - $table->dateTime('expires_at')->nullable(); - }); $schema->create('store_report_daily_rows', function (Blueprint $table): void { $table->increments('id'); $table->string('generation'); @@ -53,27 +54,9 @@ class StoreReportRepositoryFilterTest extends TestCase $table->decimal('expense', 20, 4)->default(0); }); DB::connection('business_mirror')->table('store_report_import_states')->insert([ - ['dataset_key' => StoreReportRepository::LIFECYCLES_DATASET, 'active_generation' => $this->lifecycleGeneration, 'initial_completed_at' => now()], + ['dataset_key' => StoreReportRepository::LIFECYCLES_DATASET, 'active_generation' => 'lifecycle-live', 'initial_completed_at' => now()], ['dataset_key' => StoreReportRepository::DAILY_DATASET, 'active_generation' => $this->dailyGeneration, 'initial_completed_at' => now()], ]); - DB::connection('business_mirror')->table('store_report_lifecycle_rows')->insert([ - [ - 'generation' => $this->lifecycleGeneration, - 'business_key' => 'lifecycle-a-1', - 'erp_store_name' => '店铺A', - 'company_name' => '主体甲', - 'effective_at' => '2026-07-15 00:00:00', - 'expires_at' => '2026-08-01 00:00:00', - ], - [ - 'generation' => $this->lifecycleGeneration, - 'business_key' => 'lifecycle-a-2', - 'erp_store_name' => '店铺A', - 'company_name' => '主体乙', - 'effective_at' => '2026-08-10 00:00:00', - 'expires_at' => null, - ], - ]); $this->repository = new StoreReportRepository(app('db')); } @@ -91,47 +74,48 @@ class StoreReportRepositoryFilterTest extends TestCase ['主体甲', '主体乙', '主体丙'], array_column($rows, 'entity'), ); + $this->assertSame('2026-08-19', $rows[0]['invalid_at']); $this->assertSame('存续中', $rows[1]['invalid_at']); - $this->assertSame('2026-08-01', $rows[0]['invalid_at']); + $this->assertSame('存续中', $rows[2]['invalid_at']); } public function test_effective_at_filter_is_left_closed_and_right_open(): void { $rows = $this->repository->rows($this->stores, 2026, [ - 'effective_at_from' => '2026-07-15', - 'effective_at_to' => '2026-08-10', + 'effective_at_from' => '2026-08-20', + 'effective_at_to' => '2026-08-20', ]); - // 左边界 07-15 命中主体甲;右边界 08-10 命中的主体乙被排除(右开); - // 无生命周期兜底行(默认 2026-07-01)低于左边界被排除。 - $this->assertSame(['主体甲'], array_column($rows, 'entity')); + // 右开:生效日正好等于上界的期间不命中。 + $this->assertSame([], array_column($rows, 'entity')); $rows = $this->repository->rows($this->stores, 2026, [ - 'effective_at_from' => '2026-07-16', + 'effective_at_from' => '2026-07-02', ]); + // 左闭:期初期间(2026-07-01)低于下界被排除。 $this->assertSame(['主体乙'], array_column($rows, 'entity')); } public function test_invalid_at_filter_uses_resolved_expiry_and_excludes_ongoing(): void { $rows = $this->repository->rows($this->stores, 2026, [ - 'invalid_at_from' => '2026-07-01', + 'invalid_at_from' => '2026-08-01', 'invalid_at_to' => '2026-09-01', ]); - // 主体乙与兜底行均为存续(2999-01-01),不在失效区间内。 + // 主体乙和主体丙均为存续中(2999-01-01),不在失效区间内。 $this->assertSame(['主体甲'], array_column($rows, 'entity')); $rows = $this->repository->rows($this->stores, 2026, [ - 'invalid_at_from' => '2026-08-01', + 'invalid_at_from' => '2026-08-19', ]); // 只有下界时,存续中(2999-01-01)尚未失效,同样满足条件。 $this->assertSame(['主体甲', '主体乙', '主体丙'], array_column($rows, 'entity')); } - public function test_entity_filter_matches_lifecycle_and_fallback_companies(): void + public function test_entity_filter_matches_period_and_baseline_companies(): void { $rows = $this->repository->rows($this->stores, 2026, ['entity' => '主体甲']); diff --git a/tests/Unit/EcommerceFinance/StoreReportRepositoryTest.php b/tests/Unit/EcommerceFinance/StoreReportRepositoryTest.php index ca11d1f4..ad25f65a 100644 --- a/tests/Unit/EcommerceFinance/StoreReportRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/StoreReportRepositoryTest.php @@ -17,12 +17,11 @@ use RuntimeException; use Tests\TestCase; /** - * 店铺维度报表数据组合:共享生命周期和日汇总的拆行与收入归属。 + * 店铺维度报表数据组合:期初数据、系统主体、爬虫变更的期间拆分与收入归属。 */ class StoreReportRepositoryTest extends TestCase { private StoreReportRepository $repository; - private string $lifecycleGeneration = '11111111-1111-4111-8111-111111111111'; private string $dailyGeneration = '22222222-2222-4222-8222-222222222222'; protected function setUp(): void @@ -39,14 +38,15 @@ class StoreReportRepositoryTest extends TestCase $table->string('active_generation')->nullable(); $table->dateTime('initial_completed_at')->nullable(); }); - $schema->create('store_report_lifecycle_rows', function (Blueprint $table): void { + $schema->create('store_report_shop_period_changes', function (Blueprint $table): void { $table->increments('id'); - $table->string('generation'); - $table->string('business_key'); $table->string('erp_store_name'); $table->string('company_name')->nullable(); - $table->dateTime('effective_at')->nullable(); - $table->dateTime('expires_at')->nullable(); + $table->string('unified_social_credit_code')->nullable(); + $table->date('observed_on'); + $table->string('source')->default('crawl_shop_company'); + $table->string('business_key')->unique(); + $table->timestamps(); }); $schema->create('store_report_daily_rows', function (Blueprint $table): void { $table->increments('id'); @@ -56,8 +56,19 @@ class StoreReportRepositoryTest extends TestCase $table->decimal('income', 20, 4)->default(0); $table->decimal('expense', 20, 4)->default(0); }); + $schema->create('ops_fys_shop_info', function (Blueprint $table): void { + $table->increments('Id'); + $table->string('erpShopName'); + $table->string('unifiedSocialCreditCode')->nullable(); + $table->string('shopState')->nullable(); + }); + $schema->create('ops_fys_company_info', function (Blueprint $table): void { + $table->increments('Id'); + $table->string('unifiedSocialCreditCode')->nullable(); + $table->string('businessLicenseName')->nullable(); + }); DB::connection('business_mirror')->table('store_report_import_states')->insert([ - ['dataset_key' => StoreReportRepository::LIFECYCLES_DATASET, 'active_generation' => $this->lifecycleGeneration, 'initial_completed_at' => now()], + ['dataset_key' => StoreReportRepository::LIFECYCLES_DATASET, 'active_generation' => 'lifecycle-live', 'initial_completed_at' => now()], ['dataset_key' => StoreReportRepository::DAILY_DATASET, 'active_generation' => $this->dailyGeneration, 'initial_completed_at' => now()], ]); $this->repository = new StoreReportRepository(app('db')); @@ -70,38 +81,44 @@ class StoreReportRepositoryTest extends TestCase parent::tearDown(); } - public function test_lifecycle_boundaries_split_monthly_income_without_double_counting(): void + public function test_initial_data_splits_shop_periods_and_income_at_change_day(): void { - $this->insertLifecycle('店铺A', '主体甲', '2026-05-01 00:00:00', '2026-07-01 00:00:00'); - $this->insertLifecycle('店铺A', '主体乙', '2026-07-01 00:00:00', null); - $this->insertBill('店铺A', '2026-04-30 10:00:00', 9); - $this->insertBill('店铺A', '2026-05-01 00:00:00', 100); - $this->insertBill('店铺A', '2026-06-30 23:59:59', 60); - $this->insertBill('店铺A', '2026-07-01 00:00:00', 70); - $this->insertBill('店铺A', '2026-08-01 10:00:00', 80); - $this->insertBill('店铺A', '2027-01-01 10:00:00', 5); + config(['store_report_initial_periods.shops' => [ + '店铺A' => [ + 'previous_company' => '主体甲', + 'company' => '主体乙', + 'changed_at' => '2026-07-10', + ], + ]]); + $this->insertBill('店铺A', '2026-06-30', 9); + $this->insertBill('店铺A', '2026-07-01', 100); + $this->insertBill('店铺A', '2026-07-09', 60); + $this->insertBill('店铺A', '2026-07-10', 70); + $this->insertBill('店铺A', '2026-08-01', 80); + $this->insertBill('店铺A', '2027-01-01', 5); $rows = $this->repository->rows([ - ['erp_store_name' => '店铺A', 'company_name' => '旧主体', 'store_status' => '3'], + ['erp_store_name' => '店铺A', 'company_name' => '旧主体', 'store_status' => '0'], ], 2026); $this->assertSame(['主体甲', '主体乙'], array_column($rows, 'entity')); - // 主体切换日 07-01 的账单只归主体乙(左闭右开),切换前的 04-30 不计入。 + // 变更日前一天(07-09)仍归老主体,变更日(07-10)当天归新主体,窗口前的 06-30 不计入。 $this->assertSame([160.0, 155.0], array_column($rows, 'period_sales')); - $this->assertSame([100.0, 0.0], array_column($rows, 'sales_05')); - $this->assertSame([0.0, 70.0], array_column($rows, 'sales_07')); - $this->assertSame(['关店中', '关店中'], array_column($rows, 'status')); - $this->assertSame(['2026-07-01', '存续中'], array_column($rows, 'invalid_at')); + $this->assertSame([0.0, 0.0], array_column($rows, 'sales_05')); + $this->assertSame([160.0, 70.0], array_column($rows, 'sales_07')); + $this->assertSame([0.0, 80.0], array_column($rows, 'sales_08')); + $this->assertSame(['2026-07-01', '2026-07-10'], array_column($rows, 'effective_at')); + $this->assertSame(['2026-07-09', '存续中'], array_column($rows, 'invalid_at')); + $this->assertSame(['开店中', '开店中'], array_column($rows, 'status')); } - public function test_fallback_row_uses_company_table_subject_and_default_window(): void + public function test_store_without_initial_data_uses_store_row_company_from_initial_date(): void { - // 未匹配生命周期时按默认窗口 [2026-07-01, 2999-01-01) 归属账单。 - $this->insertBill('店铺A', '2026-05-03 10:00:00', 25.5); - $this->insertBill('店铺A', '2026-07-05 10:00:00', 30); + $this->insertBill('店铺A', '2026-05-03', 25.5); + $this->insertBill('店铺A', '2026-07-05', 30); $rows = $this->repository->rows([ - ['erp_store_name' => '店铺A', 'company_name' => '公司表主体', 'opened_at' => '2026-05-01'], + ['erp_store_name' => '店铺A', 'company_name' => '公司表主体', 'store_status' => '0', 'opened_at' => '2026-05-01'], ], 2026); $this->assertCount(1, $rows); @@ -112,20 +129,61 @@ class StoreReportRepositoryTest extends TestCase $this->assertSame('存续中', $rows[0]['invalid_at']); } - public function test_lifecycle_effective_date_before_default_is_preserved(): void + public function test_closed_store_shows_dash_for_period_columns_but_keeps_income(): void { - $this->insertLifecycle('店铺A', '历史主体', '2026-05-15 00:00:00', '2026-06-20 00:00:00'); - $this->insertBill('店铺A', '2026-05-20 10:00:00', 18); - $this->insertBill('店铺A', '2026-07-05 10:00:00', 30); + $this->insertBill('店铺A', '2026-07-05', 42); $rows = $this->repository->rows([ - ['erp_store_name' => '店铺A', 'company_name' => '店铺主体', 'store_status' => '0'], + ['erp_store_name' => '店铺A', 'company_name' => '主体甲', 'store_status' => '6'], ], 2026); $this->assertCount(1, $rows); - $this->assertSame('2026-05-15', $rows[0]['effective_at']); - $this->assertSame('2026-06-20', $rows[0]['invalid_at']); - $this->assertSame(18.0, $rows[0]['period_sales']); + $this->assertSame('关店', $rows[0]['status']); + $this->assertSame('-', $rows[0]['effective_at']); + $this->assertSame('-', $rows[0]['invalid_at']); + $this->assertSame('主体甲', $rows[0]['entity']); + $this->assertSame(42.0, $rows[0]['period_sales']); + } + + public function test_crawler_change_appends_period_and_ignores_repeated_company(): void + { + $this->insertChange('店铺B', '主体新', '2026-08-20'); + $this->insertChange('店铺B', '主体新', '2026-08-25'); + $this->insertBill('店铺B', '2026-08-19', 10); + $this->insertBill('店铺B', '2026-08-20', 20); + + $rows = $this->repository->rows([ + ['erp_store_name' => '店铺B', 'company_name' => '主体旧', 'store_status' => '0'], + ], 2026); + + $this->assertSame(['主体旧', '主体新'], array_column($rows, 'entity')); + $this->assertSame(['2026-07-01', '2026-08-20'], array_column($rows, 'effective_at')); + $this->assertSame(['2026-08-19', '存续中'], array_column($rows, 'invalid_at')); + $this->assertSame([10.0, 20.0], array_column($rows, 'period_sales')); + } + + public function test_crawler_change_uses_system_entity_as_base_when_row_shows_crawler_company(): void + { + DB::connection('business_mirror')->table('ops_fys_shop_info')->insert([ + 'erpShopName' => '店铺C', 'unifiedSocialCreditCode' => 'CODE-C', 'shopState' => '0', + ]); + DB::connection('business_mirror')->table('ops_fys_company_info')->insert([ + 'unifiedSocialCreditCode' => 'CODE-C', 'businessLicenseName' => '主体旧', + ]); + $this->insertChange('店铺C', '主体新', '2026-09-29', 'CODE-NEW'); + DB::connection('business_mirror')->table('ops_fys_company_info')->insert([ + 'unifiedSocialCreditCode' => 'CODE-NEW', 'businessLicenseName' => '主体新', + ]); + $this->insertBill('店铺C', '2026-09-28', 10); + $this->insertBill('店铺C', '2026-09-29', 20); + + // 店铺基础信息页按爬虫优先展示主体新,报表期间仍以系统主体为基数拆分。 + $rows = $this->repository->rows([ + ['erp_store_name' => '店铺C', 'company_name' => '主体新', 'store_status' => '0'], + ], 2026); + + $this->assertSame(['主体旧', '主体新'], array_column($rows, 'entity')); + $this->assertSame([10.0, 20.0], array_column($rows, 'period_sales')); } public function test_unfinished_shared_daily_summary_is_reported_instead_of_returning_misleading_zeroes(): void @@ -141,12 +199,11 @@ class StoreReportRepositoryTest extends TestCase public function test_upstream_business_information_exposes_shared_store_report_rows(): void { - $this->insertLifecycle('店铺A', '主体甲', '2026-01-01 00:00:00', null); - $this->insertBill('店铺A', '2026-06-30 10:00:00', 60); + $this->insertBill('店铺A', '2026-07-05', 60); $mirror = Mockery::mock(BusinessInformationMirrorRepository::class); $mirror->shouldReceive('ready')->with('store-information.read')->andReturnTrue(); $mirror->shouldReceive('allStores')->once()->andReturn([[ - 'erp_store_name' => '店铺A', 'company_name' => '公司表主体', 'store_status' => '3', + 'erp_store_name' => '店铺A', 'company_name' => '主体甲', 'store_status' => '0', ]]); $accounts = Mockery::mock(StoreAccountMirrorRepository::class); $accounts->shouldReceive('ownershipByStoreName')->andReturn([]); @@ -168,12 +225,9 @@ class StoreReportRepositoryTest extends TestCase public function test_subject_operating_report_aggregates_income_by_entity(): void { Carbon::setTestNow('2026-09-01 10:00:00'); - $this->insertLifecycle('店铺A', '主体甲', '2026-01-01 00:00:00', null); - $this->insertLifecycle('店铺B', '主体甲', '2026-01-01 00:00:00', null); - $this->insertBill('店铺A', '2026-06-10 10:00:00', 10); - $this->insertBill('店铺A', '2026-07-10 10:00:00', 20); - $this->insertBill('店铺A', '2026-08-10 10:00:00', 30); - $this->insertBill('店铺B', '2026-08-11 10:00:00', 40); + $this->insertBill('店铺A', '2026-07-10', 20); + $this->insertBill('店铺A', '2026-08-10', 30); + $this->insertBill('店铺B', '2026-08-11', 40); $mirror = Mockery::mock(BusinessInformationMirrorRepository::class); $mirror->shouldReceive('ready')->with('store-information.read')->andReturnTrue(); $mirror->shouldReceive('allStores')->once()->andReturn([ @@ -196,32 +250,33 @@ class StoreReportRepositoryTest extends TestCase $this->assertCount(1, $rows); $this->assertSame('主体甲', $rows[0]->entity); - $this->assertSame(100.0, $rows[0]->rolling_12m_income); - $this->assertSame(10.0, $rows[0]->income_06); + $this->assertSame(90.0, $rows[0]->rolling_12m_income); $this->assertSame(20.0, $rows[0]->income_07); $this->assertSame(70.0, $rows[0]->income_08); } - private function insertBill(string $store, string $occurredAt, float $income): void + private function insertBill(string $store, string $occurredOn, float $income): void { DB::connection('business_mirror')->table('store_report_daily_rows')->insert([ 'generation' => $this->dailyGeneration, 'store_name' => $store, - 'occurred_on' => substr($occurredAt, 0, 10), + 'occurred_on' => $occurredOn, 'income' => $income, 'expense' => 0, ]); } - private function insertLifecycle(string $store, string $company, ?string $from, ?string $to): void + private function insertChange(string $store, string $company, string $observedOn, ?string $taxNo = null): void { - DB::connection('business_mirror')->table('store_report_lifecycle_rows')->insert([ - 'generation' => $this->lifecycleGeneration, - 'business_key' => hash('sha256', implode('|', [$store, (string) $from, (string) $to])), + DB::connection('business_mirror')->table('store_report_shop_period_changes')->insert([ 'erp_store_name' => $store, 'company_name' => $company, - 'effective_at' => $from, - 'expires_at' => $to, + 'unified_social_credit_code' => $taxNo, + 'observed_on' => $observedOn, + 'source' => 'crawl_shop_company', + 'business_key' => hash('sha256', implode('|', [$store, $company, $observedOn])), + 'created_at' => now(), + 'updated_at' => now(), ]); } }