BodyMeasurements.php 4.1 KB

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