# treatment_actual — EMR.treat_plan 本次实际治疗(actual)
#
# W4 末重大语义修正(详见 apps/pac-docs/content/docs/operations/dw-data-issues.mdx):
#   - 原方案:从 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
  # 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 只到日);created_date=医生录治疗的时点,100% 同 rq 日,可跟预约/其它事件比先后
  sourceEncounterExternalId:   emr_id
  category:                    category_raw     # transforms 已带 treat_name 原文,这里 enum_mapping 翻译
  subtype:                     treat_name       # 原始 treatName 留作 subtype(给 chain milestone 字典 fallback 匹配)
  toothPosition:               tooth_position   # ⭐ 48.7% 带牙位,chain S3 牙位级判定可用
  treatStages:                 treat_stages     # ⭐ W4 末:host stages array,enum_mapping 翻译到 PAC step
  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。

# ── 字典分层(2026-07 重构):临床通用层在 _shared/dict,本地只留 jvs-dw 特有词 ──
# 共享:treatStages 全量 / category 干净临床核心 / keyword+strip(actual 语义变体)。
# 本地:价格表项名·标点变体·长句(host 数据痕迹) + 判例词(拆线→surgical 绑本 host route
# 分流语境;全景片/CBCT 等影像项计作 preventive 是本 host 数据实用主义)。
dict_includes:
  - ../../_shared/dict/treat-stages.yaml
  - ../../_shared/dict/treatment-category-core.yaml
  - ../../_shared/dict/treatment-category-actual-rules.yaml

enum_mapping:
  treatStages:
    # 长尾兜底:host stage 词字典外 → 空(不进 PAC,treat_stages 数组减一元素)
    _default: ''

  category:
    # ── 牙周(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
    "根管治疗(封失活剂)": 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
    "正畸牙拔除术(正位)": 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(silently warn)
    # 故意不纳入(已被 transforms.route_by_pattern 分流走):
    #   常规复查 / 复查 / 定期复查 / 检查(无具体内容) / 暂观 / 观察 / 无治疗 / 重复 / 取资料 / 拆线
    #   建议XX / 推荐XX(走 recommendation_record)
    _default: ""
