BodyMeasurements.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 身体测量数据模型
  6. */
  7. class BodyMeasurements extends Model
  8. {
  9. // 表名
  10. protected $table = 'fa_body_measurements';
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'measurement_date_text'
  20. ];
  21. /**
  22. * 关联身体档案
  23. */
  24. public function profile()
  25. {
  26. return $this->belongsTo('BodyProfile', 'profile_id', 'id', [], 'LEFT')->setEagerlyType(0);
  27. }
  28. /**
  29. * 获取测量日期文本
  30. */
  31. public function getMeasurementDateTextAttr($value, $data)
  32. {
  33. return $data['measurement_date'] ? date('Y-m-d', $data['measurement_date']) : '';
  34. }
  35. /**
  36. * 设置测量日期
  37. */
  38. public function setMeasurementDateAttr($value)
  39. {
  40. return is_numeric($value) ? $value : strtotime($value);
  41. }
  42. /**
  43. * 获取所有测量数据的字段映射
  44. */
  45. public static function getMeasurementFields($gender = 0)
  46. {
  47. $commonFields = [
  48. 'chest' => '胸围',
  49. 'waist' => '腰围',
  50. 'hip' => '臀围',
  51. 'thigh' => '大腿围',
  52. 'calf' => '小腿围',
  53. 'upper_arm' => '上臂围',
  54. 'forearm' => '前臂围',
  55. 'neck' => '颈围',
  56. 'shoulder_width' => '肩宽',
  57. 'inseam' => '内缝长',
  58. 'outseam' => '外缝长',
  59. 'knee' => '膝围',
  60. 'arm_length' => '臂长',
  61. 'wrist' => '腕围',
  62. 'pants_length' => '裤长',
  63. 'shoe_size' => '鞋码',
  64. 'belly_belt' => '肚围',
  65. 'leg_root' => '腿根',
  66. 'ankle' => '脚踝',
  67. 'waist_lower' => '下腰围',
  68. 'mid_waist' => '中腰围',
  69. ];
  70. // 女性专用字段
  71. if ($gender == 2) {
  72. $commonFields['bust'] = '胸围/乳围';
  73. $commonFields['underbust'] = '下胸围';
  74. }
  75. return $commonFields;
  76. }
  77. /**
  78. * 计算身体比例数据
  79. */
  80. public function calculateBodyRatios()
  81. {
  82. $profile = $this->profile;
  83. if (!$profile || $profile->height <= 0) {
  84. return [];
  85. }
  86. $height = $profile->height;
  87. $ratios = [];
  88. // 计算各部位与身高的比例
  89. $measurementFields = [
  90. 'chest' => '胸围比例',
  91. 'waist' => '腰围比例',
  92. 'hip' => '臀围比例',
  93. 'thigh' => '大腿围比例',
  94. 'shoulder_width' => '肩宽比例'
  95. ];
  96. foreach ($measurementFields as $field => $label) {
  97. if ($this->$field > 0) {
  98. $ratios[$field] = round(($this->$field / $height) * 100, 2);
  99. }
  100. }
  101. // 计算腰臀比
  102. if ($this->waist > 0 && $this->hip > 0) {
  103. $ratios['waist_hip_ratio'] = round($this->waist / $this->hip, 2);
  104. }
  105. return $ratios;
  106. }
  107. /**
  108. * 获取理想测量范围建议
  109. */
  110. public function getIdealRanges($gender, $height, $weight)
  111. {
  112. if ($height <= 0) return [];
  113. $ranges = [];
  114. // 根据身高和性别计算理想范围
  115. if ($gender == 1) { // 男性
  116. $ranges = [
  117. 'chest' => [
  118. 'min' => round($height * 0.48, 1),
  119. 'max' => round($height * 0.52, 1)
  120. ],
  121. 'waist' => [
  122. 'min' => round($height * 0.42, 1),
  123. 'max' => round($height * 0.47, 1)
  124. ],
  125. 'hip' => [
  126. 'min' => round($height * 0.51, 1),
  127. 'max' => round($height * 0.55, 1)
  128. ]
  129. ];
  130. } else { // 女性
  131. $ranges = [
  132. 'bust' => [
  133. 'min' => round($height * 0.49, 1),
  134. 'max' => round($height * 0.53, 1)
  135. ],
  136. 'waist' => [
  137. 'min' => round($height * 0.37, 1),
  138. 'max' => round($height * 0.42, 1)
  139. ],
  140. 'hip' => [
  141. 'min' => round($height * 0.52, 1),
  142. 'max' => round($height * 0.56, 1)
  143. ]
  144. ];
  145. }
  146. return $ranges;
  147. }
  148. }