123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\common\model;
- use think\Model;
- /**
- * 身体测量数据模型
- */
- class BodyMeasurements extends Model
- {
- // 表名
- protected $table = 'fa_body_measurements';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'measurement_date_text'
- ];
- /**
- * 关联身体档案
- */
- public function profile()
- {
- return $this->belongsTo('BodyProfile', 'profile_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 获取测量日期文本
- */
- public function getMeasurementDateTextAttr($value, $data)
- {
- return $data['measurement_date'] ? date('Y-m-d', $data['measurement_date']) : '';
- }
- /**
- * 设置测量日期
- */
- public function setMeasurementDateAttr($value)
- {
- return is_numeric($value) ? $value : strtotime($value);
- }
- /**
- * 获取所有测量数据的字段映射
- */
- public static function getMeasurementFields($gender = 0)
- {
- $commonFields = [
- 'chest' => '胸围',
- 'waist' => '腰围',
- 'hip' => '臀围',
- 'thigh' => '大腿围',
- 'calf' => '小腿围',
- 'upper_arm' => '上臂围',
- 'forearm' => '前臂围',
- 'neck' => '颈围',
- 'shoulder_width' => '肩宽',
- 'inseam' => '内缝长',
- 'outseam' => '外缝长',
- 'knee' => '膝围',
- 'arm_length' => '臂长',
- 'wrist' => '腕围',
- 'pants_length' => '裤长',
- 'shoe_size' => '鞋码',
- 'belly_belt' => '肚围',
- 'leg_root' => '腿根',
- 'ankle' => '脚踝',
- 'waist_lower' => '下腰围',
- 'mid_waist' => '中腰围',
- ];
- // 女性专用字段
- if ($gender == 2) {
- $commonFields['bust'] = '胸围/乳围';
- $commonFields['underbust'] = '下胸围';
- }
- return $commonFields;
- }
- /**
- * 计算身体比例数据
- */
- public function calculateBodyRatios()
- {
- $profile = $this->profile;
- if (!$profile || $profile->height <= 0) {
- return [];
- }
- $height = $profile->height;
- $ratios = [];
- // 计算各部位与身高的比例
- $measurementFields = [
- 'chest' => '胸围比例',
- 'waist' => '腰围比例',
- 'hip' => '臀围比例',
- 'thigh' => '大腿围比例',
- 'shoulder_width' => '肩宽比例'
- ];
- foreach ($measurementFields as $field => $label) {
- if ($this->$field > 0) {
- $ratios[$field] = round(($this->$field / $height) * 100, 2);
- }
- }
- // 计算腰臀比
- if ($this->waist > 0 && $this->hip > 0) {
- $ratios['waist_hip_ratio'] = round($this->waist / $this->hip, 2);
- }
- return $ratios;
- }
- /**
- * 获取理想测量范围建议
- */
- public function getIdealRanges($gender, $height, $weight)
- {
- if ($height <= 0) return [];
- $ranges = [];
- // 根据身高和性别计算理想范围
- if ($gender == 1) { // 男性
- $ranges = [
- 'chest' => [
- 'min' => round($height * 0.48, 1),
- 'max' => round($height * 0.52, 1)
- ],
- 'waist' => [
- 'min' => round($height * 0.42, 1),
- 'max' => round($height * 0.47, 1)
- ],
- 'hip' => [
- 'min' => round($height * 0.51, 1),
- 'max' => round($height * 0.55, 1)
- ]
- ];
- } else { // 女性
- $ranges = [
- 'bust' => [
- 'min' => round($height * 0.49, 1),
- 'max' => round($height * 0.53, 1)
- ],
- 'waist' => [
- 'min' => round($height * 0.37, 1),
- 'max' => round($height * 0.42, 1)
- ],
- 'hip' => [
- 'min' => round($height * 0.52, 1),
- 'max' => round($height * 0.56, 1)
- ]
- ];
- }
- return $ranges;
- }
- }
|