tcm_exam.rb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # encoding:utf-8
  2. class TcmExam < ActiveRecord::Base
  3. has_paper_trail
  4. self.table_name = "tcm_exams"
  5. belongs_to :wx_user
  6. belongs_to :scale_user, :foreign_key => :scale_user_id
  7. STATUS_ENUM = [["待处理", "pending"], ["处理中", "processing"], ["已完成", "completed"], ["失败", "failed"]]
  8. def wx_user_contact
  9. return "-" if wx_user.blank?
  10. user = wx_user.user
  11. return "-" if user.blank?
  12. contact = user.tel
  13. contact = user.email if contact.blank?
  14. contact.blank? ? "-" : contact
  15. end
  16. def scale_user_name
  17. return "-" if scale_user.blank?
  18. scale_user.nick_name || "-"
  19. end
  20. # 解析 AI 报告内容
  21. def parsed_ai_summary
  22. return {} if ai_summary.blank?
  23. begin
  24. JSON.parse(ai_summary)
  25. rescue
  26. {}
  27. end
  28. end
  29. # 解析观察图片
  30. def parsed_observation_images
  31. return {} if observation_images.blank?
  32. begin
  33. JSON.parse(observation_images)
  34. rescue
  35. {}
  36. end
  37. end
  38. # 解析问诊答案
  39. def parsed_question_answers
  40. return {} if question_answers.blank?
  41. begin
  42. JSON.parse(question_answers)
  43. rescue
  44. {}
  45. end
  46. end
  47. # 解析最新体脂秤结果
  48. def parsed_latest_scale_result
  49. return {} if latest_scale_result.blank?
  50. begin
  51. JSON.parse(latest_scale_result)
  52. rescue
  53. {}
  54. end
  55. end
  56. # 状态标签
  57. def status_label
  58. case status
  59. when "pending" then "待处理"
  60. when "processing" then "处理中"
  61. when "completed" then "已完成"
  62. when "failed" then "失败"
  63. else status || "-"
  64. end
  65. end
  66. # 虚拟方法:调养与建议
  67. def advice_section
  68. parsed_ai_summary["advice"] || "-"
  69. end
  70. # 格式化内容,处理换行和 markdown 格式
  71. def format_content(content)
  72. return "-" if content.blank?
  73. # 处理换行符
  74. formatted = content.to_s
  75. .gsub(/\\n/, "<br>")
  76. .gsub(/\n/, "<br>")
  77. .gsub(/\*\*([^*]+)\*\*/, '<strong>\1</strong>') # **粗体**
  78. .gsub(/\*([^*]+)\*/, '<em>\1</em>') # *斜体*
  79. formatted
  80. end
  81. rails_admin do
  82. navigation_label '中医诊疗记录'
  83. parent ScaleDevice
  84. weight 3
  85. list do
  86. filters [:wx_user, :scale_user, :status, :created_at]
  87. field :id do
  88. label 'ID'
  89. end
  90. field :wx_user_id do
  91. label '微信用户ID'
  92. end
  93. field :wx_user do
  94. label '微信用户'
  95. pretty_value do
  96. bindings[:object].wx_user_contact
  97. end
  98. end
  99. field :scale_user_id do
  100. label '称用户ID'
  101. end
  102. field :scale_user do
  103. label '称用户名'
  104. pretty_value do
  105. bindings[:object].scale_user_name
  106. end
  107. end
  108. field :status do
  109. label '状态'
  110. pretty_value do
  111. bindings[:object].status_label
  112. end
  113. end
  114. field :ai_summary do
  115. label '报告详情'
  116. formatted_value do
  117. exam = bindings[:object]
  118. bindings[:view].link_to('查看报告', bindings[:view].rails_admin.show_path(model_name: 'tcm_exam', id: exam.id), class: 'btn btn-info btn-sm')
  119. end
  120. end
  121. field :created_at do
  122. label '创建时间'
  123. end
  124. end
  125. show do
  126. field :id
  127. field :wx_user_id do
  128. label '微信用户ID'
  129. end
  130. field :wx_user do
  131. label '微信用户'
  132. pretty_value do
  133. bindings[:object].wx_user_contact
  134. end
  135. end
  136. field :scale_user_id do
  137. label '称用户ID'
  138. end
  139. field :scale_user do
  140. label '称用户名'
  141. pretty_value do
  142. bindings[:object].scale_user_name
  143. end
  144. end
  145. field :status do
  146. label '状态'
  147. pretty_value do
  148. bindings[:object].status_label
  149. end
  150. end
  151. # 望诊图片
  152. field :observation_images do
  153. label '望诊图片'
  154. pretty_value do
  155. exam = bindings[:object]
  156. images = exam.parsed_observation_images
  157. return "-" if images.empty?
  158. image_labels = {
  159. 'tongue' => '舌面照',
  160. 'tongueBottom' => '舌底照',
  161. 'face' => '面部照',
  162. 'body' => '全身照'
  163. }
  164. html = '<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 10px 0;">'
  165. ['tongue', 'tongueBottom', 'face', 'body'].each do |key|
  166. urls = images[key]
  167. next if urls.blank? || urls.empty?
  168. url = urls.first
  169. next if url.blank?
  170. html += %Q{
  171. <div style="text-align: center;">
  172. <img src="#{url}" style="width: 100%; max-width: 200px; height: 150px; object-fit: cover; border-radius: 8px; border: 1px solid #e5e7eb; cursor: pointer;" onclick="window.open('#{url}')" />
  173. <div style="margin-top: 6px; font-size: 12px; color: #5d4037; font-weight: 600;">#{image_labels[key] || key}</div>
  174. </div>
  175. }
  176. end
  177. html += '</div>'
  178. html.html_safe
  179. end
  180. end
  181. # AI 报告内容 - 一般情况
  182. field :ai_summary do
  183. label '壹 · 一般情况'
  184. pretty_value do
  185. exam = bindings[:object]
  186. summary = exam.parsed_ai_summary
  187. content = summary['general'] || summary['summary'] || '-'
  188. %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
  189. end
  190. end
  191. # 问诊
  192. field :question_answers do
  193. label '贰 · 问诊 (十问歌)'
  194. pretty_value do
  195. exam = bindings[:object]
  196. summary = exam.parsed_ai_summary
  197. content = summary['inquiry'] || '-'
  198. %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
  199. end
  200. end
  201. # 望诊与闻诊
  202. field :extra_note do
  203. label '叁 · 望诊与闻诊'
  204. pretty_value do
  205. exam = bindings[:object]
  206. summary = exam.parsed_ai_summary
  207. content = summary['observation'] || '-'
  208. %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
  209. end
  210. end
  211. # 总体评估与体质辨识
  212. field :latest_scale_result do
  213. label '肆 · 总体评估与体质辨识'
  214. pretty_value do
  215. exam = bindings[:object]
  216. summary = exam.parsed_ai_summary
  217. content = summary['constitution'] || '-'
  218. %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
  219. end
  220. end
  221. # 调养与建议
  222. field :advice_section, :string do
  223. label '伍 · 调养与建议'
  224. pretty_value do
  225. exam = bindings[:object]
  226. summary = exam.parsed_ai_summary
  227. content = summary['advice'] || '-'
  228. %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
  229. end
  230. end
  231. # 总结
  232. field :model do
  233. label '陆 · 总结'
  234. pretty_value do
  235. exam = bindings[:object]
  236. summary = exam.parsed_ai_summary
  237. content = summary['summary']
  238. return "-" if content.blank?
  239. %Q{<div style="background: #fff7ed; border: 1px solid #fed7aa; border-radius: 10px; padding: 14px; margin: 10px 0; line-height: 1.7; color: #9a3412;">#{exam.format_content(content)}</div>}.html_safe
  240. end
  241. end
  242. field :created_at do
  243. label '创建时间'
  244. end
  245. field :updated_at do
  246. label '更新时间'
  247. end
  248. end
  249. # 禁用编辑和删除,只允许查看
  250. edit do
  251. field :status, :enum do
  252. enum do
  253. STATUS_ENUM
  254. end
  255. end
  256. end
  257. end
  258. end