# treatment_planned — EMR.plan 字段未来治疗计划(planned)
#
# W4 末重大语义修正:
#   - 原方案:从 EMR.treat_plan 拿"真治疗",标 planned(❌ 错!treat_plan 是 actual)
#   - 新方案:从 EMR.plan 字段拿"未来计划",标 planned(✅ 字段语义对齐)
#   - 字典(treat_name → category)完全跟 treatment_actual.yaml 相同,只是 kind 不同
#
# 消费 transforms 输出表 treatment_planned_rows
#   (来源:_treatment_planned_raw_emr ← _plan_raw route 后剩下的"真治疗")
#
# plan 字段是 2023+ host 启用,老数据(2017-2022)plan 几乎空 → 老患者 planned 信号较少,正常
canonical: treatment

emits:
  action: treatment_planned
  subjectType: treatment
  occurredAtField: occurredAt
  kind: planned

primary:
  table: treatment_planned_rows
  key: treat_external_id
  dedup_by: treat_external_id   # 炸行兜底:同 emr × treatName × tooth_position 因影像 JOIN 重复

field_mapping:
  externalId:                  treat_external_id
  # W4 末 DW upsert:updatedAt 进 source_event_id 幂等键,host UPDATE → 新 transaction(否则永久 dedup 漏 update)
  updatedAt:         updated_date
  createdAt:         created_date
  patientExternalId:           patient_id
  clinicId:                    organization_id
  occurredAt:                  created_date     # 精确就诊时刻(医生当天写计划的时点;rq 只到日);plannedFor 由 parser 用此填,100% 同 rq 日
  sourceEncounterExternalId:   emr_id
  category:                    category_raw    # transforms 已带 treat_name 原文,这里 enum_mapping 翻译
  subtype:                     treat_name      # 原始 treatName 留作 subtype
  toothPosition:               tooth_position
  treatStages:                 treat_stages     # W4 末:host stages array(planned 阶段通常空,但保留字段)
  doctorId:                    user_id          # 制定计划的医生(从 emr 父级继承)
  doctorName:                  doctor_name      # 计划医生名(快照)

# treatName 实测 21k distinct,top 100 cover 80%。下面是按实测高频 + 临床合理拼出来的白名单。
# 长尾兜底 _default: ""(parser 见空 category 跳过该 fact,数据损失换数据质量)。
#
# 维护:跑 SQL `SELECT DISTINCT treatName, count() FROM ... GROUP BY 1 ORDER BY 2 DESC LIMIT 200`
# 看新出现的高频项,补到本表。

# ── 字典分层(2026-07 重构):同 treatment_actual,keyword 用 planned 语义变体 ──
# 本地残差含 normalize 前的中文标点死条目(输入已归一不会再命中;行为保真原样保留,
# 清理属行为无关但需单独验证,另行提交)。
dict_includes:
  - ../../_shared/dict/treat-stages.yaml
  - ../../_shared/dict/treatment-category-core.yaml
  - ../../_shared/dict/treatment-category-planned-rules.yaml

enum_mapping:
  treatStages:
    _default: ''

  category:
    # ── 牙周(periodontic)
    "龈上洁治。": periodontic
    "龈上洁治术。": periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙": periodontic
    "龈上洁治+牙周治疗": periodontic
    "全口龈上洁治、抛光。": periodontic
    "全口龈上洁治，抛光。": periodontic
    "全口龈上洁治，抛光": periodontic
    "全口龈上洁治。": periodontic
    "全口龈上洁治术。": periodontic
    "全口洁治。": periodontic
    "定期全口洁治\r\n": periodontic
    "全口洁治\r\n": periodontic
    "洁治。": periodontic
    "洁治+OHI": periodontic
    "龈上洁治；细洁": periodontic
    "龈上洁治+细洁": periodontic
    "龈上洁治+龈下细洁": periodontic
    "牙周系统治疗。": periodontic
    "OHI\r\n牙周系统治疗": periodontic
    "龈下刮治及根面平整术(>5mm牙周袋)/牙周刮治术/超声龈下清创术": periodontic
    "龈下刮治及根面平整术(<5mm牙周袋)/牙周刮治术/超声龈下清创术": periodontic
    "PMTC+全口涂氟": periodontic
    "龈上洁治术/预防性洁治/洗牙/洁牙": periodontic
    "定期洁牙,牙周系统治疗": periodontic
    "全口洁治+OHI": periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙全口龈上洁治": periodontic
    # ── 充填修复(restorative)
    "充填。": restorative
    "充填治疗(乳牙单面洞)": restorative
    "垫底+充填": restorative
    "玻璃离子充填术。": restorative
    "玻璃离子充填。": restorative
    "进口玻璃离子充填术。": restorative
    "充填治疗(玻璃离子)": restorative
    "充填治疗(乳牙复面洞)": restorative
    "恒牙充填治疗(儿科)": restorative
    # ── 牙髓/根管(endodontic)
    "根管治疗(磨牙)": endodontic
    "根管治疗(前牙及双尖牙单根管)": endodontic
    "根管治疗(前牙及双尖牙多根管/显微根管)": endodontic
    "根管治疗+冠修复": endodontic
    "根管治疗+充填": endodontic
    "RCT+冠修复": endodontic
    "活髓切断术+预成冠修复": endodontic
    "活髓切断术+预成冠": endodontic
    "活髓切断+预成冠修复": endodontic
    "活髓切断+预成冠": endodontic
    "活髓切断术+全瓷冠冠修复": endodontic
    "活髓切断术+全瓷预成冠修复": endodontic
    "活髓,活髓切断术": endodontic
    "乳牙活髓切断术+冠修复": endodontic
    "乳牙活髓保存+冠修复": endodontic
    "活髓保存+冠修复": endodontic
    "乳牙活髓保存术+冠修复": endodontic
    "冠髓切断术(乳牙)": endodontic
    "冠髓切断术(乳牙)+预成冠修复": endodontic
    "冠髓切断术+预成冠修复": endodontic
    "恒牙冠髓切断术(儿科)": endodontic
    "间接盖髓术+预成冠修复": endodontic
    "开髓,封药": endodontic
    "开髓,封失活剂": endodontic
    "乳牙牙髓治疗+冠修复": endodontic
    "根管治疗术(乳磨牙封失活剂法)": endodontic
    "根管治疗术(乳磨牙直接拔髓多步法)": endodontic
    "根管治疗术(复杂乳牙封失活剂法)": endodontic
    "根管治疗术(复杂乳牙直接拔髓多次法)": endodontic
    "根管治疗术(乳前牙直接拔髓多步法)": endodontic
    "根管治疗(封失活剂)": endodontic
    # ── 种植(implant)
    "种植冠修复(非美学区单颗)": implant
    "简单种植术(非美学区,单颗)": implant
    "复杂种植术(非美学区,连续缺失/多颗)": implant
    "复杂种植术(非美学区,伴同期上颌窦底内提升/同期GBR术)": implant
    "复杂种植术(同期上颌窦底外提升术)": implant
    "复杂种植术(美学区连续2颗以上或伴骨量不足及GBR术)": implant
    "延期种植术(美学区,单颗)": implant
    # ── 修复(冠/桥/义齿/桩核)(prosthodontic)
    "冠修复(非美学区)": prosthodontic
    "冠修复(简单美学区)": prosthodontic
    取模: prosthodontic
    调合: prosthodontic
    调颌: prosthodontic
    # ── 外科(拔除)(surgical)
    "拔除。": surgical
    "牙拔除术(松动乳牙)": surgical
    "智齿拔除术(正位)": surgical
    "智齿拔除术(复杂)": surgical
    "残根(残冠)拔除术": surgical
    "正畸牙拔除术(正位)": surgical
    # ── 正畸(orthodontic)
    "合垫/预防性矫治/功能矫治/早期阻断性矫治": orthodontic
    "简单前牙排齐/安氏I类非拔牙矫治": orthodontic
    "安氏I类拔牙矫治(轻中度支抗)": orthodontic
    "安氏II类非拔牙矫治-涉及后牙咬合关系调整": orthodontic
    "安氏II类拔牙矫治(轻中度支抗)": orthodontic
    "安氏II类拔牙矫治(强支抗/种植支抗)": orthodontic
    "安氏III类非拔牙矫治-涉及后牙咬合关系调整": orthodontic
    "安氏III类拔牙矫治(轻中度支抗)": orthodontic
    "正畸加力。": orthodontic
    "粘接全口附件。": orthodontic
    "重粘托槽。": orthodontic
    "去除矫正器,配戴保持器保持现有咬合关系": orthodontic
    "精细调整。": orthodontic
    "精调粘接附件。": orthodontic
    "发放新矫治器。": orthodontic
    "去除矫正器,配戴保持器保持现有咬合关系。": orthodontic
    # ── 预防(preventive)
    "牙面清洁,涂氟": preventive
    "窝沟封闭。": preventive
    "OHI,涂氟": preventive
    口腔检查: preventive
    全口检查: preventive
    常规检查: preventive
    全景片: preventive
    CBCT: preventive
    曲面断层: preventive
    根尖片: preventive
    # 长尾兜底:未命中 → category='',parser 跳过该 fact
    _default: ""
