import { createHash } from 'node:crypto'; /** * 由**业务幂等键**派生**确定性 UUID**。 * * 用途:主系统的 `preparations.price_changed` 是**纯事实事件**(没有 `request_id`,无法被"决策"), * 但我方待审台账的主键/唯一键需要 UUID。用事件自带的业务幂等键 * (`pc___`,同一行同一对新旧价恒定)派生 ⇒ **重推得到同一个 UUID** * ⇒ "同一事实只落一行"这件事由数据库唯一键天然保证,不需要额外的去重表。 * * 纯函数、零依赖(便于单测;`mainSystemActualQtyInbox` 会连带初始化连接池,不能直接 import 测)。 */ export function deterministicUuid(seed: string): string { const hex = createHash('sha1').update(String(seed)).digest('hex').slice(0, 32).split(''); hex[12] = '5'; // 版本位(v5 风格,仅为形态合规) hex[16] = ((parseInt(hex[16], 16) & 0x3) | 0x8).toString(16); // variant 位 const h = hex.join(''); return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20, 32)}`; }