# treatment_actual — EMR.treat_plan 本次实际治疗(actual)
#
# W4 末重大语义修正(详见 docs/dw-data-source-issues.md):
#   - 原方案:从 fact_settlement_out 反推已做治疗(无牙位、颗粒度差、跟收费 1:N)
#   - 新方案:从 EMR.treat_plan 字段(真实临床记录,48.7% 带牙位)取本次治疗
#   - settlement 不再消费为 treatment_actual,只走 payment.yaml(LTV)+ refund.yaml(风险)
#
# 消费 transforms 输出表 treatment_actual_rows
#   (来源:_treatment_actual_raw_emr ← _treat_plan_raw route 后剩下的"真治疗")
#
# 字段结构跟 treatment_planned.yaml 几乎相同(同源 _treat_plan_raw / _plan_raw 结构对称),
# 仅 kind=actual + occurredAt 用 rq(treat_plan 是已发生事实,occurredAt 准确反映)
canonical: treatment

emits:
  action: treatment_completed
  subjectType: treatment
  occurredAtField: occurredAt
  kind: actual

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

field_mapping:
  externalId:                  treat_external_id
  patientExternalId:           patient_id
  clinicId:                    organization_id
  occurredAt:                  rq               # treat_plan 已发生,rq=就诊日,actual 时点
  sourceEncounterExternalId:   emr_id
  category:                    category_raw     # transforms 已带 treat_name 原文,这里 enum_mapping 翻译
  subtype:                     treat_name       # 原始 treatName 留作 subtype(给 chain milestone 字典匹配)
  toothPosition:               tooth_position   # ⭐ 关键升级:48.7% 带牙位,chain S3 牙位级判定可用
  doctorId:                    user_id          # 治疗医生(从 emr 父级继承)
  doctorName:                  doctor_name      # 治疗医生名(快照)

# treatName 实测 21k distinct,top 100 cover 80%。下面是按实测高频 + 临床合理拼出来的白名单。
# 字典跟 treatment_planned.yaml 完全相同(同源 treat_name,只是 kind 不同)。
# 长尾兜底 _default: ""(parser 见空 category 跳过该 fact,数据损失换数据质量)。
#
# 维护:跑 SQL `SELECT JSONExtractString(t,'treatName') treat_name, count() FROM
#              fact_emr_treatment_out ARRAY JOIN JSONExtractArrayRaw(coalesce(treat_plan,'[]')) AS t
#              WHERE brand IN ('瑞泰','瑞尔') GROUP BY 1 ORDER BY 2 DESC LIMIT 200`
# 看新出现的高频 treat_name,补到对应 category。
enum_mapping:
  category:
    # ── 牙周(periodontic)
    龈上洁治:                                       periodontic
    "龈上洁治。":                                    periodontic
    龈上洁治术:                                      periodontic
    "龈上洁治术。":                                  periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙":               periodontic
    龈上洁治+牙周治疗:                              periodontic
    全口龈上洁治:                                    periodontic
    # 注:W4 末加 transforms.normalize op 后,DW 中文标点(、，()；)已统一到 ASCII,
    # 字典只需 ASCII 形态(英文逗号 + 英括号);中文标点条目已被 normalize 折叠到这些
    "全口龈上洁治,抛光。":                          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 末补 DW top 200 漏配
    牙周序列治疗:                                    periodontic
    系统性牙周治疗:                                  periodontic
    "龈上洁治术/预防性洁治/洗牙/洁牙":              periodontic   # yaml 早期写的是"/洁牙/洗牙"顺序,这里补常见的反顺序
    "定期洁牙,牙周系统治疗":                        periodontic
    "全口洁治+OHI":                                  periodontic
    "龈上洁治术/预防性洁治/洁牙/洗牙全口龈上洁治":  periodontic   # host 行内炸行残留,本质洁牙

    # ── 充填修复(restorative)
    充填:                                            restorative
    "充填。":                                        restorative
    充填治疗:                                        restorative
    "充填治疗(乳牙单面洞)":                          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

    # ── 种植(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)
    正畸:                                            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

    # 长尾兜底:未命中 → category='',parser 跳过该 fact(silently warn)
    # 故意不纳入(已被 transforms.route_by_pattern 分流走):
    #   常规复查 / 复查 / 定期复查 / 检查(无具体内容) / 暂观 / 观察 / 无治疗 / 重复 / 取资料 / 拆线
    #   建议XX / 推荐XX(走 recommendation_record)
    _default: ""
