BodyMeasurements.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ];
  67. // 女性专用字段
  68. if ($gender == 2) {
  69. $commonFields['bust'] = '胸围/乳围';
  70. $commonFields['underbust'] = '下胸围';
  71. }
  72. return $commonFields;
  73. }
  74. /**
  75. * 计算身体比例数据
  76. */
  77. public function calculateBodyRatios()
  78. {
  79. $profile = $this->profile;
  80. if (!$profile || $profile->height <= 0) {
  81. return [];
  82. }
  83. $height = $profile->height;
  84. $ratios = [];
  85. // 计算各部位与身高的比例
  86. $measurementFields = [
  87. 'chest' => '胸围比例',
  88. 'waist' => '腰围比例',
  89. 'hip' => '臀围比例',
  90. 'thigh' => '大腿围比例',
  91. 'shoulder_width' => '肩宽比例'
  92. ];
  93. foreach ($measurementFields as $field => $label) {
  94. if ($this->$field > 0) {
  95. $ratios[$field] = round(($this->$field / $height) * 100, 2);
  96. }
  97. }
  98. // 计算腰臀比
  99. if ($this->waist > 0 && $this->hip > 0) {
  100. $ratios['waist_hip_ratio'] = round($this->waist / $this->hip, 2);
  101. }
  102. return $ratios;
  103. }
  104. /**
  105. * 获取理想测量范围建议
  106. */
  107. public function getIdealRanges($gender, $height, $weight)
  108. {
  109. if ($height <= 0) return [];
  110. $ranges = [];
  111. // 根据身高和性别计算理想范围
  112. if ($gender == 1) { // 男性
  113. $ranges = [
  114. 'chest' => [
  115. 'min' => round($height * 0.48, 1),
  116. 'max' => round($height * 0.52, 1)
  117. ],
  118. 'waist' => [
  119. 'min' => round($height * 0.42, 1),
  120. 'max' => round($height * 0.47, 1)
  121. ],
  122. 'hip' => [
  123. 'min' => round($height * 0.51, 1),
  124. 'max' => round($height * 0.55, 1)
  125. ]
  126. ];
  127. } else { // 女性
  128. $ranges = [
  129. 'bust' => [
  130. 'min' => round($height * 0.49, 1),
  131. 'max' => round($height * 0.53, 1)
  132. ],
  133. 'waist' => [
  134. 'min' => round($height * 0.37, 1),
  135. 'max' => round($height * 0.42, 1)
  136. ],
  137. 'hip' => [
  138. 'min' => round($height * 0.52, 1),
  139. 'max' => round($height * 0.56, 1)
  140. ]
  141. ];
  142. }
  143. return $ranges;
  144. }
  145. }