# referral — 转介绍(老带新 / 渠道归因)
# fact 挂在被推荐人(referee = patient_id);推荐人信息进 content 快照
# 消费 transforms 输出表 referral_rows(从 fact_client_out 过滤有 recommend_id 的行 + derive 外部 id)
canonical: referral

emits:
  action: referral_recorded
  subjectType: referral
  occurredAtField: recordedAt

primary:
  table: referral_rows
  key: referral_external_id
  dedup_by: referral_external_id

field_mapping:
  externalId:           referral_external_id
  # W4 末 DW upsert:updatedAt 进 source_event_id 幂等键,host UPDATE → 新 transaction(否则永久 dedup 漏 update)
  updatedAt:         last_visit_time
  createdAt:         first_visit_time
  patientExternalId:    patient_id              # 被推荐人(referee)= 该行的 patient_id
  # clinicId 不映射:fact_client_out(ADS 聚合)无 organization_id 列,referral 来自主档无明确 clinic
  recordedAt:           first_visit_time        # 近似:推荐发生时间 ≈ 被推荐人首次到诊
  referrerExternalId:   recommend_id            # 推荐人 host id
  referrerName:         recommend_name          # 推荐人名(快照)
  referrerChartNumber:  recommend_file_num      # 推荐人病历号(辅助)
  # referrerType / channel:host 没明确字段,留 null(parser 兜底 'patient' 是潜在做法,这里先保真)
