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; } }