瀏覽代碼

fix: tcm_exam详情页望诊记录和调养建议显示问题

1. 修复第三点望诊与闻诊不显示问题:改用虚拟字段observation_section
2. 修复调养与建议显示JSON问题:添加format_advice方法格式化嵌套JSON为分类列表

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Your Name 3 天之前
父節點
當前提交
9c444f547d
共有 1 個文件被更改,包括 58 次插入6 次删除
  1. 58 6
      app/models/tcm_exam.rb

+ 58 - 6
app/models/tcm_exam.rb

@@ -77,6 +77,11 @@ class TcmExam < ActiveRecord::Base
     parsed_ai_summary["advice"] || "-"
   end
 
+  # 虚拟方法:望诊记录
+  def observation_section
+    parsed_ai_summary["observation"] || "-"
+  end
+
   # 格式化内容,处理换行和 markdown 格式
   def format_content(content)
     return "-" if content.blank?
@@ -89,6 +94,51 @@ class TcmExam < ActiveRecord::Base
     formatted
   end
 
+  # 格式化调养与建议的嵌套 JSON 结构
+  def format_advice(advice)
+    return "-" if advice.blank?
+    
+    # 如果是字符串,尝试解析为 JSON
+    if advice.is_a?(String)
+      begin
+        advice = JSON.parse(advice)
+      rescue
+        return format_content(advice)
+      end
+    end
+    
+    # 如果不是 Hash,直接格式化
+    return format_content(advice.to_s) unless advice.is_a?(Hash)
+    
+    # 定义分类标签和图标
+    category_info = {
+      '饮食' => { icon: '🍵', color: '#059669' },
+      '运动' => { icon: '🏃', color: '#2563eb' },
+      '作息' => { icon: '🌙', color: '#7c3aed' },
+      '情志' => { icon: '💆', color: '#db2777' }
+    }
+    
+    html = ''
+    advice.each do |category, items|
+      info = category_info[category] || { icon: '📌', color: '#6b7280' }
+      html += %Q{<div style="margin-bottom: 16px;">}
+      html += %Q{<div style="font-weight: 600; color: #{info[:color]}; margin-bottom: 8px; font-size: 15px;">#{info[:icon]} #{category}</div>}
+      html += %Q{<ul style="margin: 0; padding-left: 20px; list-style-type: disc;">}
+      
+      if items.is_a?(Array)
+        items.each do |item|
+          html += %Q{<li style="margin-bottom: 6px; line-height: 1.6;">#{format_content(item)}</li>}
+        end
+      else
+        html += %Q{<li style="margin-bottom: 6px; line-height: 1.6;">#{format_content(items)}</li>}
+      end
+      
+      html += '</ul></div>'
+    end
+    
+    html
+  end
+
   rails_admin do
     navigation_label '中医诊疗记录'
     parent ScaleDevice
@@ -217,8 +267,8 @@ class TcmExam < ActiveRecord::Base
         end
       end
 
-      # 望诊与闻诊
-      field :extra_note do
+      # 望诊与闻诊 - 使用虚拟字段确保显示
+      field :observation_section do
         label '叁 · 望诊与闻诊'
         pretty_value do
           exam = bindings[:object]
@@ -239,14 +289,16 @@ class TcmExam < ActiveRecord::Base
         end
       end
 
-      # 调养与建议
-      field :advice_section, :string do
+      # 调养与建议 - 格式化嵌套 JSON
+      field :advice_section do
         label '伍 · 调养与建议'
         pretty_value do
           exam = bindings[:object]
           summary = exam.parsed_ai_summary
-          content = summary['advice'] || '-'
-          %Q{<div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px; margin: 10px 0; line-height: 1.7;">#{exam.format_content(content)}</div>}.html_safe
+          advice = summary['advice']
+          return "-" if advice.blank?
+          formatted = exam.format_advice(advice)
+          %Q{<div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px; margin: 10px 0; line-height: 1.7;">#{formatted}</div>}.html_safe
         end
       end