# encoding:utf-8 class ScaleResult < ActiveRecord::Base has_paper_trail self.table_name = "scale_result" belongs_to :wx_user belongs_to :scale_user, :foreign_key => :user_id def wx_user_contact return "-" if wx_user.blank? user = wx_user.user return "-" if user.blank? contact = user.tel contact = user.email if contact.blank? contact.blank? ? "-" : contact end # 从测量结果中获取时间戳 def measurement_time return nil if result.blank? begin data = JSON.parse(result) timestamp = data['timestamp'] return nil if timestamp.blank? Time.parse(timestamp) rescue => e nil end end # 格式化数值,保留2位小数 def fmt(val) return '-' if val.nil? val.is_a?(Numeric) ? format('%.2f', val) : val.to_s end # 格式化显示测量结果(列表简要版) def result_summary return '-' if result.blank? begin d = JSON.parse(result) "体重:#{fmt(d['weight_kg'])}kg BMI:#{fmt(d['bmi'])} 体脂:#{fmt(d['bodyFatPercent'])}%" rescue '-' end end # 格式化显示测量结果(详情完整版) def result_formatted return '-' if result.blank? begin d = JSON.parse(result) userdata = d['userdata'] || {} html = '' # 用户信息 if userdata.present? html += '' html += "" html += "" html += "" html += "" end # 基础测量 html += '' html += "" html += "" html += "" html += "" # 体成分 html += '' html += "" html += "" html += "" html += "" html += "" html += "" html += "" html += "" html += "" html += "" # 目标建议 html += '' html += "" html += "" html += "" html += "" # 设备信息 html += '' html += "" html += "" html += '
👤 用户信息
昵称#{userdata['nickName']}性别#{userdata['sex'] == 0 ? '男' : '女'}
年龄#{userdata['age']}岁身高#{userdata['height']}cm
📊 基础测量
体重#{fmt(d['weight_kg'])} kgBMI#{fmt(d['bmi'])}
身体评分#{fmt(d['bodyScore'])} 分身体年龄#{fmt(d['physicalAge'])} 岁
🏃 体成分分析
体脂率#{fmt(d['bodyFatPercent'])}%肌肉率#{fmt(d['musclePercent'])}%
内脏脂肪#{fmt(d['visceralFat'])}皮下脂肪#{fmt(d['subcutaneousFatPercent'])}%
水分率#{fmt(d['moisturePercent'])}%蛋白质#{fmt(d['proteinPercent'])}%
骨量#{fmt(d['boneMass'])} kg基础代谢#{fmt(d['bmr'])} kcal
骨骼肌率#{fmt(d['smPercent'])}%体型#{d['bodyType']}
🎯 目标建议
目标体重#{fmt(d['targetWeight'])} kg体重控制#{fmt(d['weightControl'])} kg
标准体重#{fmt(d['weightStandard'])} kg标准BMI#{fmt(d['bmiStandard'])}
📱 设备信息
MAC地址#{d['mac']}测量时间#{d['timestamp']}
' html.html_safe rescue => e "解析错误: #{e.message}" end end rails_admin do navigation_label '测量记录' parent ScaleDevice weight 2 list do filters [:wx_user] field :id do label 'ID' end field :wx_user_id do label '微信用户ID' end field :wx_user do label '微信用户' pretty_value do bindings[:object].wx_user_contact end end field :user_id do label '称用户ID' end field :scale_user do label '称用户名' end field :result do label '测量结果' pretty_value do bindings[:object].result_summary end end field :measurement_time do label '测量时间' pretty_value do time = bindings[:object].measurement_time time.present? ? time.strftime('%Y-%m-%d %H:%M:%S') : '-' end sortable false end end show do field :id do label 'ID' end field :wx_user_id do label '微信用户ID' end field :wx_user do label '微信用户' pretty_value do bindings[:object].wx_user_contact end end field :user_id do label '称用户ID' end field :scale_user do label '称用户名' end field :result do label '测量结果' pretty_value do bindings[:object].result_formatted end end field :measurement_time do label '测量时间' pretty_value do time = bindings[:object].measurement_time time.present? ? time.strftime('%Y-%m-%d %H:%M:%S') : '-' end end end edit do field :wx_user do label '微信用户' end field :scale_user do label '称用户名' end field :result do label '测量结果' end end end end