# 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:                  rq               # rq=就诊日(医生当天写的计划);plannedFor 由 parser 用此填
  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`
# 看新出现的高频项,补到本表。
enum_mapping:
  # treat_stages 跟 treatment_actual.yaml 完全相同(W4 末加)
  treatStages:
    开髓: pulp_extirpation
    拔髓: pulp_extirpation
    根备: canal_preparation
    根管预备: canal_preparation
    根充: canal_filling
    根管充填: canal_filling
    # 牙髓切断术 / 活髓保存(pulpotomy 类 — VPT 终末术式)
    活髓切断: pulpotomy
    部分活髓切断: pulpotomy
    部分牙髓切断: pulpotomy
    冠髓切断: pulpotomy
    干髓术: pulpotomy
    干髓治疗: pulpotomy
    盖髓术: pulpotomy
    间接盖髓: pulpotomy
    直接盖髓: pulpotomy
    牙髓血运重建: pulpotomy
    活髓保存: pulpotomy
    植入: implant_placement
    种植体植入: implant_placement
    种植一期: implant_placement
    种植I期: implant_placement       # 罗马数字版(host 实测)
    简单种植: implant_placement
    复杂种植: implant_placement
    即刻种植: implant_placement
    延期种植: implant_placement
    基台: abutment_placement
    种植二期: abutment_placement
    种植II期: abutment_placement      # 罗马数字版(host 实测)
    上部修复: crown_placement
    种植冠修复: crown_placement
    种植戴牙: crown_placement
    种植三期: crown_placement
    龈上洁治: supragingival_scaling
    龈下刮治: subgingival_scaling
    刮治: subgingival_scaling
    牙周维护: periodontal_maintenance
    充填: composite_filling
    嵌体: inlay
    冠修复: crown_restoration
    桩核: post_core
    粘附件: bracket_placement
    托槽: bracket_placement
    矫治器: bracket_placement
    保持器: retainer
    加力: ortho_adjustment
    _default: ''

  category:
    # ── 牙周(periodontic)— 龈上洁治 / 牙周治疗 各种写法
    龈上洁治:                                       periodontic
    "龈上洁治。":                                    periodontic
    龈上洁治术:                                      periodontic
    "龈上洁治术。":                                  periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙":               periodontic
    龈上洁治+牙周治疗:                              periodontic
    全口龈上洁治:                                    periodontic
    "全口龈上洁治、抛光。":                          periodontic
    "全口龈上洁治，抛光。":                          periodontic
    "全口龈上洁治，抛光":                            periodontic
    "全口龈上洁治。":                                periodontic
    "全口龈上洁治术。":                              periodontic
    全口洁治:                                        periodontic
    "全口洁治。":                                    periodontic
    全口洁牙:                                        periodontic
    全口超声洁治:                                    periodontic
    全口超声波洁治:                                  periodontic
    全口龈上超声洁治:                                periodontic
    定期全口洁治:                                    periodontic
    "定期全口洁治\r\n":                              periodontic
    "全口洁治\r\n":                                  periodontic
    定期洁牙:                                        periodontic
    洁牙:                                            periodontic
    洁治:                                            periodontic
    "洁治。":                                        periodontic
    洗牙:                                            periodontic
    洁治 OHI:                                       periodontic
    "洁治+OHI":                                      periodontic
    "龈上洁治；细洁":                                periodontic
    "龈上洁治+细洁":                                 periodontic
    "龈上洁治+龈下细洁":                             periodontic
    龈下细洁:                                        periodontic
    龈上超声洁治:                                    periodontic
    牙周基础治疗:                                    periodontic
    牙周系统治疗:                                    periodontic
    "牙周系统治疗。":                                periodontic
    "OHI\r\n牙周系统治疗":                           periodontic
    牙周治疗:                                        periodontic
    牙周维护:                                        periodontic
    牙周刮治:                                        periodontic
    牙周刮治术:                                      periodontic
    龈下刮治:                                        periodontic
    龈下刮治术:                                      periodontic
    "龈下刮治及根面平整术(>5mm牙周袋)/牙周刮治术/超声龈下清创术": periodontic
    "龈下刮治及根面平整术(<5mm牙周袋)/牙周刮治术/超声龈下清创术": periodontic
    根面平整术:                                      periodontic
    PMTC:                                            periodontic
    "PMTC+全口涂氟":                                 periodontic
    # W4 末补
    牙周序列治疗:                                    periodontic
    系统性牙周治疗:                                  periodontic
    "龈上洁治术/预防性洁治/洗牙/洁牙":              periodontic
    "定期洁牙,牙周系统治疗":                        periodontic
    "全口洁治+OHI":                                  periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙全口龈上洁治":  periodontic

    # ── 充填修复(restorative)
    充填:                                            restorative
    "充填。":                                        restorative
    充填治疗:                                        restorative
    "充填治疗(乳牙单面洞)":                          restorative
    充填术:                                          restorative
    树脂充填:                                        restorative
    光固化树脂充填:                                  restorative
    非美学区树脂充填:                                restorative
    美学区简单树脂充填:                              restorative
    "垫底+充填":                                     restorative
    重新充填:                                        restorative
    预防性充填:                                      restorative
    树脂充填术:                                      restorative
    嵌体修复:                                        restorative
    嵌体:                                            restorative
    戴嵌体:                                          restorative
    龋齿充填:                                        restorative
    # W4 末补 — 玻璃离子(GIC)类充填(临床: 等价于树脂充填,主要用于乳牙/年轻恒牙/暂封)
    玻璃离子充填:                                    restorative
    玻璃离子充填术:                                  restorative
    "玻璃离子充填术。":                              restorative
    "玻璃离子充填。":                                restorative
    进口玻璃离子充填:                                restorative
    "进口玻璃离子充填术。":                          restorative
    "充填治疗(玻璃离子)":                            restorative
    玻璃离子暂封:                                    restorative
    玻璃离子预充填:                                  restorative
    # 其他高频补全
    充填或嵌体修复:                                  restorative
    "充填治疗(乳牙复面洞)":                          restorative
    "恒牙充填治疗(儿科)":                            restorative
    高嵌体修复:                                      restorative
    预防性树脂充填:                                  restorative

    # ── 牙髓 / 根管(endodontic)
    根管治疗:                                        endodontic
    "根管治疗(磨牙)":                                endodontic
    "根管治疗(前牙及双尖牙单根管)":                  endodontic
    "根管治疗(前牙及双尖牙多根管/显微根管)":         endodontic
    "根管治疗+冠修复":                               endodontic
    "根管治疗+充填":                                 endodontic
    根管再治疗:                                      endodontic
    根管预备:                                        endodontic
    根管充填:                                        endodontic
    根充:                                            endodontic
    RCT:                                             endodontic
    牙髓治疗:                                        endodontic
    根尖诱导成形术:                                  endodontic
    拔髓:                                            endodontic
    干髓术:                                          endodontic
    盖髓术:                                          endodontic
    间接盖髓:                                        endodontic
    直接盖髓:                                        endodontic
    MTA盖髓:                                         endodontic
    冲洗上药:                                        endodontic
    # W4 末补
    根管治疗后冠修复:                                endodontic
    "RCT+冠修复":                                    endodontic
    # W4 末补 — pulpotomy 类 + 牙髓相关术式(跟 treatment_actual.yaml 同源)
    活髓切断术:                                      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
    开髓引流:                                        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
    "根管治疗(封失活剂)":                            endodontic
    复杂根管治疗:                                    endodontic

    # ── 种植(implant)— 注意"种植上部修复"算 prostho;"种植修复"在 host 文本是合并语,归 implant
    种植:                                            implant
    种植手术:                                        implant
    种植体植入:                                      implant
    种植修复:                                        implant
    种植戴牙:                                        implant
    种植一期:                                        implant
    种植二期:                                        implant
    种植三期:                                        implant
    种植耗材:                                        implant
    种植基台:                                        implant
    种植取模:                                        implant
    种植拆线:                                        implant
    "种植冠修复(非美学区单颗)":                      implant
    "简单种植术(非美学区,单颗)":                     implant
    "复杂种植术(非美学区,连续缺失/多颗)":            implant
    "复杂种植术(非美学区,伴同期上颌窦底内提升/同期GBR术)": implant
    "复杂种植术(同期上颌窦底外提升术)":              implant
    "复杂种植术(美学区连续2颗以上或伴骨量不足及GBR术)": implant
    即刻种植术:                                      implant
    拔除后种植修复:                                  implant
    延期种植术:                                      implant
    "延期种植术(美学区,单颗)":                       implant
    # W4 末补
    拔除后种植:                                      implant

    # ── 修复(prostho)— 冠 / 桥 / 义齿 / 桩核
    冠修复:                                          prosthodontic
    "冠修复(非美学区)":                              prosthodontic
    "冠修复(简单美学区)":                            prosthodontic
    全冠修复:                                        prosthodontic
    牙冠修复:                                        prosthodontic
    重新冠修复:                                      prosthodontic
    戴冠:                                            prosthodontic
    粘冠:                                            prosthodontic
    拆冠:                                            prosthodontic
    戴牙:                                            prosthodontic
    桩核:                                            prosthodontic
    桩核冠:                                          prosthodontic
    固定修复:                                        prosthodontic
    活动修复:                                        prosthodontic
    重新修复:                                        prosthodontic
    修复:                                            prosthodontic
    预成冠修复:                                      prosthodontic
    牙体预备:                                        prosthodontic
    取模:                                            prosthodontic
    调合:                                            prosthodontic
    调颌:                                            prosthodontic
    种植上部修复:                                    prosthodontic
    种植体上部修复:                                  prosthodontic
    # W4 末补
    桩冠修复:                                        prosthodontic

    # ── 外科(surgical)— 拔除 / 智齿 / 残根残冠
    拔除:                                            surgical
    "拔除。":                                        surgical
    拔除术:                                          surgical
    拔牙:                                            surgical
    牙拔除术:                                        surgical
    "牙拔除术(松动乳牙)":                            surgical
    简单牙拔除术:                                    surgical
    松动恒牙拔除术:                                  surgical
    松动乳牙拔除术:                                  surgical
    择期拔除:                                        surgical
    阻生齿拔除术:                                    surgical
    阻生牙拔除:                                      surgical
    "智齿拔除术(正位)":                              surgical
    "智齿拔除术(复杂)":                              surgical
    残根拔除术:                                      surgical
    "残根(残冠)拔除术":                              surgical
    "正畸牙拔除术(正位)":                            surgical

    # ── 正畸(orthodontic)— 实测 host treatName 覆盖了"加力 / 安氏分类 / 矫治器/保持器" 全流程
    正畸:                                            orthodontic
    正畸治疗:                                        orthodontic
    隐形正畸:                                        orthodontic
    固定正畸:                                        orthodontic
    早期矫治:                                        orthodontic
    "合垫/预防性矫治/功能矫治/早期阻断性矫治":       orthodontic
    "简单前牙排齐/安氏I类非拔牙矫治":                orthodontic
    "安氏I类拔牙矫治(轻中度支抗)":                   orthodontic
    "安氏II类非拔牙矫治-涉及后牙咬合关系调整":       orthodontic
    "安氏II类拔牙矫治(轻中度支抗)":                  orthodontic
    "安氏II类拔牙矫治(强支抗/种植支抗)":             orthodontic
    "安氏III类非拔牙矫治-涉及后牙咬合关系调整":      orthodontic
    "安氏III类拔牙矫治(轻中度支抗)":                 orthodontic
    保持器:                                          orthodontic
    正畸保持器:                                      orthodontic
    正畸保持:                                        orthodontic
    正畸矫治器:                                      orthodontic
    正畸辅助治疗:                                    orthodontic
    正畸加力:                                        orthodontic
    "正畸加力。":                                    orthodontic
    加力:                                            orthodontic
    粘附件:                                          orthodontic
    粘接附件:                                        orthodontic
    "粘接全口附件。":                                orthodontic
    重粘托槽:                                        orthodontic
    "重粘托槽。":                                    orthodontic
    # W4 末补
    更换新矫治器:                                    orthodontic
    粘接上半口矫治器:                                orthodontic
    粘接全口附件:                                    orthodontic
    精调粘接附件:                                    orthodontic
    发放新矫治器:                                    orthodontic
    "去除矫正器,配戴保持器保持现有咬合关系":         orthodontic
    精细调整:                                        orthodontic
    "精细调整。":                                    orthodontic
    "精调粘接附件。":                                orthodontic
    发放矫治器:                                      orthodontic
    "发放新矫治器。":                                orthodontic
    "去除矫正器,配戴保持器保持现有咬合关系。":       orthodontic

    # ── 预防(preventive)— 涂氟 / 封闭 / 检查
    涂氟:                                            preventive
    全口涂氟:                                        preventive
    "牙面清洁,涂氟":                                 preventive
    窝沟封闭:                                        preventive
    "窝沟封闭。":                                    preventive
    窝沟封闭术:                                      preventive
    OHI:                                             preventive
    "OHI,涂氟":                                      preventive
    口腔卫生宣教:                                    preventive
    口腔检查:                                        preventive
    全口检查:                                        preventive
    常规检查:                                        preventive
    脱敏:                                            preventive
    抛光:                                            preventive
    全景片:                                          preventive
    CBCT:                                            preventive
    曲面断层:                                        preventive
    根尖片:                                          preventive

    # ── 美学(cosmetic)
    美白:                                            cosmetic
    冷光美白:                                        cosmetic
    牙齿美白:                                        cosmetic
    牙位图美白:                                      cosmetic
    贴面:                                            cosmetic
    贴面修复:                                        cosmetic

    # 长尾兜底:enum_mapping 未命中 → category='',parser 跳过该 fact(silently warn)
    # 维护:跑 SQL `SELECT JSONExtractString(t, 'treatName') treat_name, count() FROM
    #              fact_emr_treatment_out ARRAY JOIN JSONExtractArrayRaw(assumeNotNull(treat_plan)) AS t
    #              WHERE brand IN ('瑞泰','瑞尔') GROUP BY 1 ORDER BY 2 DESC LIMIT 200`
    # 看新出现的高频 treat_name,补到上方对应 category。
    # 故意不纳入(归 review / 非治疗 / 流程性 — 已被 transforms.route_by_pattern 分流走):
    #   常规复查 / 复查 / 定期复查 / 检查(无具体内容)/ 已交付纸质病历 / 缴费 / 咨询 /
    #   听方案 / 沟通治疗方案 / 方案沟通 / 拒绝拍片 / 未拍片 / 治疗中 / 未指定治疗项 / 转诊
    _default: ""
