BodyMeasurements.php 4.3 KB

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