# 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 # 格式化显示测量结果(列表简要版) def result_summary return '-' if result.blank? begin d = JSON.parse(result) "体重:#{d['weight_kg']}kg BMI:#{d['bmi']} 体脂:#{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
📊 基础测量
体重#{d['weight_kg']} kgBMI#{d['bmi']}
身体评分#{d['bodyScore']} 分身体年龄#{d['physicalAge']} 岁
🏃 体成分分析
体脂率#{d['bodyFatPercent']}%肌肉率#{d['musclePercent']}%
内脏脂肪#{d['visceralFat']}皮下脂肪#{d['subcutaneousFatPercent']}%
水分率#{d['moisturePercent']}%蛋白质#{d['proteinPercent']}%
骨量#{d['boneMass']} kg基础代谢#{d['bmr']} kcal
骨骼肌率#{d['smPercent']}%体型#{d['bodyType']}
🎯 目标建议
目标体重#{d['targetWeight']} kg体重控制#{d['weightControl']} kg
标准体重#{d['weightStandard']} kg标准BMI#{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