From 17957fe1201d8bceebcb1ec0db2104ead478bfe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E5=A4=A9?= Date: Tue, 13 Oct 2026 10:45:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=B8=BB=E7=B3=BB=E7=BB=9F=E7=BB=91?= =?UTF-8?q?=E5=AE=9A):=20=E5=9B=9E=E5=A1=AB=E7=BB=91=E5=AE=9A=E6=A0=87?= =?UTF-8?q?=E4=B8=BA=20backfill=E3=80=81=E7=BB=91=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=8C=89=E6=9C=AC=E5=9C=B0=E6=97=B6=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 迁移 20261031 两处回填的 source 由 manual 改 backfill(与超管手工登记可区分,符合设计 V10),新增迁移 20261041 回标存量行(按 note 精确识别、幂等),服务层 source 类型同步;BranchManagement 绑定弹层时间改本地时区(原按 UTC 截断,差 8 小时)。 --- ...261027-分公司接入编码与主系统供应商绑定.md | 6 +-- .../20261031_main_supplier_bindings.sql | 6 +-- ...main_supplier_bindings_backfill_source.sql | 31 +++++++++++++ server/src/services/branchAccessCode.ts | 2 +- .../mainSupplierBindingBackfillSource.test.ts | 45 +++++++++++++++++++ src/pages/system/BranchManagement.tsx | 13 +++++- 6 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 migrations/20261041_main_supplier_bindings_backfill_source.sql create mode 100644 server/tests/mainSupplierBindingBackfillSource.test.ts diff --git a/docs/design/20261027-分公司接入编码与主系统供应商绑定.md b/docs/design/20261027-分公司接入编码与主系统供应商绑定.md index f78ff6d..aea0219 100644 --- a/docs/design/20261027-分公司接入编码与主系统供应商绑定.md +++ b/docs/design/20261027-分公司接入编码与主系统供应商绑定.md @@ -84,7 +84,7 @@ CREATE TABLE IF NOT EXISTS public.main_supplier_bindings ( status VARCHAR(16) NOT NULL DEFAULT 'active', -- active / unbound bound_at TIMESTAMPTZ NOT NULL DEFAULT now(), unbound_at TIMESTAMPTZ, - source VARCHAR(16) NOT NULL DEFAULT 'main_push', -- main_push / main_pull / manual + source VARCHAR(16) NOT NULL DEFAULT 'main_push', -- main_push / main_pull / manual / backfill note TEXT ); CREATE UNIQUE INDEX IF NOT EXISTS uq_main_supplier_bindings_key @@ -96,7 +96,7 @@ CREATE INDEX IF NOT EXISTS idx_main_supplier_bindings_branch - **平台级表**(放 `public`,与 `branches` 同级):它是**路由配置**,不是租户业务表 ⇒ **不进** `public.tenant_business_tables()`; - 也不进 `/api/data` 的 `ALLOWED_TABLES`(多租户平台守卫:不给通用网关写入口); - 唯一键保证「一个(主系统分公司 + 供应商)只指向一家我方分公司」,**与他们 `supplier_integration_client_suppliers` 的 PK 逐字对应**(后者多一个 `client_id` 维度,我们只有一个对接客户端); -- 它是**镜像**(他们的表是权威):`source` 标明这行从哪来(`main_push` 回调 / `main_pull` 回读 / `manual` 超管手工兜底); +- 它是**镜像**(他们的表是权威):`source` 标明这行从哪来(`main_push` 回调 / `main_pull` 回读 / `manual` 超管手工兜底 / `backfill` 迁移回填); - **出站可以不依赖它**:若 P0-2 ② 落地(`GET /preparations` 支持按 `subsystem_code` 过滤),我们直接按自己的编码拉全,镜像降为"离线排查/兜底"用。 ### 3.3 入站路由算法(唯一判定点) @@ -206,7 +206,7 @@ resolveOurBranchByBinding({ envelopeBranchCode, rowBranchCode, supplierCode, sub | V7 | 绑定冲突(同 (branch, supplier) 重新绑到另一个码) | 旧行 `status='unbound'` + `unbound_at`,新行 `active`;不静默择一 | | V8 | 解绑 | 新推送忽略;**在途单照常履约**(发车/核货/结案不受影响);历史台账可查 | | V9 | 登录 | 本地账号 + 本地密码可登录并**强制改密**;**不再调用**主系统验证接口(断网也能登);手机号换了不影响任何路由 | -| V10 | 存量 | `branch_code` 未被改动;老台账仍能按 `supplier_code` 查到;回填的绑定行 `source` 标注可区分 | +| V10 | 存量 | `branch_code` 未被改动;老台账仍能按 `supplier_code` 查到;回填的绑定行 `source` 标注为 `backfill`(可区分) | ## 10. 待裁决 / 未决 diff --git a/migrations/20261031_main_supplier_bindings.sql b/migrations/20261031_main_supplier_bindings.sql index 95d1f8d..4e1b606 100644 --- a/migrations/20261031_main_supplier_bindings.sql +++ b/migrations/20261031_main_supplier_bindings.sql @@ -35,7 +35,7 @@ COMMENT ON COLUMN public.main_supplier_bindings.main_branch_code IS COMMENT ON COLUMN public.main_supplier_bindings.access_code IS '冗余留档:对方绑的是哪个接入编码(= branches.access_code,改码时便于追溯)'; COMMENT ON COLUMN public.main_supplier_bindings.source IS - 'main_push=对方回调/事件同步;main_pull=定时回读;manual=超管手工登记(对方绑定接口未上线时的兜底)'; + 'main_push=对方回调/事件同步;main_pull=定时回读;manual=超管手工登记(对方绑定接口未上线时的兜底);backfill=迁移回填(存量兼容,见 docs/design/20261027-分公司接入编码与主系统供应商绑定.md §6)'; DO $$ BEGIN @@ -65,7 +65,7 @@ SELECT DISTINCT UPPER(TRIM(i.main_branch_code)), b.main_supplier_code, 'active', - 'manual', + 'backfill', '迁移回填:台账 main_system_order_import_items.main_branch_code 有据' FROM public.branches b JOIN public.main_system_order_import_items i @@ -83,7 +83,7 @@ SELECT b.branch_code, '*', b.main_supplier_code, 'active', - 'manual', + 'backfill', '迁移回填:仅有 branches.main_supplier_code,无主系统分公司证据 ⇒ 通配' FROM public.branches b WHERE b.main_supplier_code IS NOT NULL diff --git a/migrations/20261041_main_supplier_bindings_backfill_source.sql b/migrations/20261041_main_supplier_bindings_backfill_source.sql new file mode 100644 index 0000000..953efdf --- /dev/null +++ b/migrations/20261041_main_supplier_bindings_backfill_source.sql @@ -0,0 +1,31 @@ +-- 修正:存量「迁移回填」的绑定行 `source` 由 'manual' 回标为 'backfill'。 +-- +-- 为什么:`migrations/20261031_main_supplier_bindings.sql` 的回填把 `source` 硬写成 'manual' +-- ⇒ 与"超管在分公司管理里手工登记"**不可区分**,违反设计验收项 V10 +-- (docs/design/20261027-分公司接入编码与主系统供应商绑定.md §6「回填的绑定行 source 标注可区分」)。 +-- 现象与处置:docs/design/20261013-主系统订单被本地拉取机制与绑定缺陷.md(缺陷 D1)。 +-- +-- 隔离边界(勿破): +-- 1) 只改 `public.main_supplier_bindings.source` **一列**;不碰任何租户业务表、不改 `branch_code`、 +-- 不改绑定的 active/unbound 状态、不改 `bound_at`; +-- 2) 按 `note` 精确识别回填行(回填写的是"迁移回填:…"),**不误伤**真·手工行(note="超管手工登记"); +-- 3) 幂等:回标后 `source <> 'manual'`,第二次跑无匹配行。 + +UPDATE public.main_supplier_bindings + SET source = 'backfill' + WHERE source = 'manual' + AND note LIKE '迁移回填%'; + +DO $$ +BEGIN + IF to_regclass('public.main_supplier_bindings') IS NULL THEN + RAISE EXCEPTION '迁移未生效:public.main_supplier_bindings 不存在'; + END IF; + -- 自证:回标后不应再有"迁移回填 且 source=manual"的行 + IF EXISTS ( + SELECT 1 FROM public.main_supplier_bindings + WHERE source = 'manual' AND note LIKE '迁移回填%' + ) THEN + RAISE EXCEPTION '迁移未生效:仍存在 source=manual 的迁移回填行'; + END IF; +END $$; diff --git a/server/src/services/branchAccessCode.ts b/server/src/services/branchAccessCode.ts index 6054660..dba06cd 100644 --- a/server/src/services/branchAccessCode.ts +++ b/server/src/services/branchAccessCode.ts @@ -189,7 +189,7 @@ export type UpsertBindingInput = { accessCode: string; mainBranchCode: string; mainSupplierCode: string; - source?: 'main_push' | 'main_pull' | 'manual'; + source?: 'main_push' | 'main_pull' | 'manual' | 'backfill'; note?: string | null; }; diff --git a/server/tests/mainSupplierBindingBackfillSource.test.ts b/server/tests/mainSupplierBindingBackfillSource.test.ts new file mode 100644 index 0000000..73b34d1 --- /dev/null +++ b/server/tests/mainSupplierBindingBackfillSource.test.ts @@ -0,0 +1,45 @@ +import { readFileSync } from 'fs'; +import { describe, expect, it } from 'vitest'; + +const bindingsMigration = readFileSync( + new URL('../../migrations/20261031_main_supplier_bindings.sql', import.meta.url), + 'utf8' +); +const fixMigration = readFileSync( + new URL('../../migrations/20261041_main_supplier_bindings_backfill_source.sql', import.meta.url), + 'utf8' +); +const svc = readFileSync(new URL('../src/services/branchAccessCode.ts', import.meta.url), 'utf8'); +const page = readFileSync( + new URL('../../src/pages/system/BranchManagement.tsx', import.meta.url), + 'utf8' +); + +/** + * 缺陷 D1/D2 —— 见 docs/design/20261013-主系统订单被本地拉取机制与绑定缺陷.md。 + * D1:迁移回填的绑定行 `source` 必须与"超管手工登记"可区分(设计 V10)⇒ 用 `backfill`。 + * D2:绑定弹层时间必须按本地时区显示(此前直接切 UTC 字符串 ⇒ 差 8 小时)。 + */ +describe('迁移回填绑定的 source 标注(D1)与绑定弹层时间(D2)', () => { + it('20261031 回填行标为 backfill,不再写 manual;服务层类型允许 backfill', () => { + expect(bindingsMigration).toContain("'backfill'"); + expect(bindingsMigration).not.toMatch(/'active',\s*'manual'/); + expect(bindingsMigration).toContain( + 'backfill=迁移回填(存量兼容' + ); + expect(svc).toContain("'main_push' | 'main_pull' | 'manual' | 'backfill'"); + }); + + it('新增迁移把存量回填行回标(幂等、按 note 精确识别、不误伤手工行)', () => { + expect(fixMigration).toContain("SET source = 'backfill'"); + expect(fixMigration).toContain("WHERE source = 'manual'"); + expect(fixMigration).toContain("note LIKE '迁移回填%'"); + // 只改 source 一列:不该出现删改其它列的语句 + expect(fixMigration).not.toMatch(/DELETE\s+FROM\s+public\.main_supplier_bindings/i); + }); + + it('绑定弹层时间改按本地时区(不再 slice(0,19) 切 UTC)', () => { + expect(page).not.toContain(".slice(0, 19).replace('T', ' ')"); + expect(page).toContain('toLocaleString(UI_LOCALE_BCP47[locale])'); + }); +}); diff --git a/src/pages/system/BranchManagement.tsx b/src/pages/system/BranchManagement.tsx index f992d31..9c96beb 100644 --- a/src/pages/system/BranchManagement.tsx +++ b/src/pages/system/BranchManagement.tsx @@ -5,7 +5,8 @@ import { useAuth } from '../../hooks/useAuth'; import { notify } from '../../utils/notify'; import { confirmDialog } from '../../utils/confirmDialog'; import { MenuLabel } from '../../components/MenuLabel'; -import { useT } from '../../i18n'; +import { useT, useI18n } from '../../i18n'; +import { UI_LOCALE_BCP47 } from '../../i18n/config'; import { isPlatformBranchCode } from '../../utils/platformBranch'; import { BRANCH_TAX_LEVELS, normalizeBranchTaxLevel } from '../../utils/branchTaxLevel'; import { SearchableSelect } from '../../components/ui/SearchableSelect'; @@ -92,7 +93,15 @@ function validateLogin(value: string, t: (k: string, v?: Record { const t = useT(); + const { locale } = useI18n(); const { hasRole } = useAuth(); + /** 绑定时间按**本地时区**显示(后端给的是 UTC ISO;此前 `slice(0,19)` 直接切字符 ⇒ 差 8 小时) */ + const formatBoundAt = (iso: string | null | undefined) => { + const raw = String(iso || '').trim(); + if (!raw) return '-'; + const d = new Date(raw); + return Number.isNaN(d.getTime()) ? raw : d.toLocaleString(UI_LOCALE_BCP47[locale]); + }; const [branches, setBranches] = useState([]); const { initialLoading: loading, refreshing, startLoad, finishLoad } = useListLoadState(); const [saving, setSaving] = useState(false); @@ -754,7 +763,7 @@ export const BranchManagement: React.FC = () => { - {(item.status === 'active' ? item.boundAt : item.unboundAt || item.boundAt)?.slice(0, 19).replace('T', ' ') || '-'} + {formatBoundAt(item.status === 'active' ? item.boundAt : item.unboundAt || item.boundAt)} ))}