fix(主系统绑定): 回填绑定标为 backfill、绑定时间按本地时区
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

迁移 20261031 两处回填的 source 由 manual 改 backfill(与超管手工登记可区分,符合设计 V10),新增迁移 20261041 回标存量行(按 note 精确识别、幂等),服务层 source 类型同步;BranchManagement 绑定弹层时间改本地时区(原按 UTC 截断,差 8 小时)。
This commit is contained in:
雨天
2026-10-13 10:45:56 +08:00
parent 93b75ad5f9
commit 17957fe120
6 changed files with 94 additions and 9 deletions
@@ -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. 待裁决 / 未决
@@ -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
@@ -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 $$;
+1 -1
View File
@@ -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;
};
@@ -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])');
});
});
+11 -2
View File
@@ -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<string, string |
export const BranchManagement: React.FC = () => {
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<Branch[]>([]);
const { initialLoading: loading, refreshing, startLoad, finishLoad } = useListLoadState();
const [saving, setSaving] = useState(false);
@@ -754,7 +763,7 @@ export const BranchManagement: React.FC = () => {
</span>
</td>
<td className="px-3 py-2 text-xs text-slate-500">
{(item.status === 'active' ? item.boundAt : item.unboundAt || item.boundAt)?.slice(0, 19).replace('T', ' ') || '-'}
{formatBoundAt(item.status === 'active' ? item.boundAt : item.unboundAt || item.boundAt)}
</td>
</tr>
))}