OrderProfile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 订单档案详情记录模型
  7. * Class OrderProfile
  8. * @package app\common\model
  9. */
  10. class OrderProfile extends Model
  11. {
  12. // 表名
  13. protected $name = 'shop_order_profile';
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. // 追加属性
  20. protected $append = [
  21. 'body_types_text',
  22. 'bmi_level_text',
  23. 'gender_text',
  24. 'relation_text'
  25. ];
  26. /**
  27. * 关联订单
  28. * @return \think\model\relation\BelongsTo
  29. */
  30. public function order()
  31. {
  32. return $this->belongsTo('Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  33. }
  34. /**
  35. * 关联用户
  36. * @return \think\model\relation\BelongsTo
  37. */
  38. public function user()
  39. {
  40. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  41. }
  42. /**
  43. * 关联档案
  44. * @return \think\model\relation\BelongsTo
  45. */
  46. public function profile()
  47. {
  48. return $this->belongsTo('BodyProfile', 'profile_id', 'id', [], 'LEFT')->setEagerlyType(0);
  49. }
  50. /**
  51. * 获取体型选择文本
  52. * @param $value
  53. * @param $data
  54. * @return string
  55. */
  56. public function getBodyTypesTextAttr($value, $data)
  57. {
  58. if (empty($data['body_types'])) {
  59. return '';
  60. }
  61. $bodyTypes = json_decode($data['body_types'], true);
  62. if (!is_array($bodyTypes)) {
  63. return '';
  64. }
  65. $texts = [];
  66. foreach ($bodyTypes as $type) {
  67. if (isset($type['name'])) {
  68. $texts[] = $type['name'];
  69. }
  70. }
  71. return implode(', ', $texts);
  72. }
  73. /**
  74. * 获取BMI等级文本
  75. * @param $value
  76. * @param $data
  77. * @return string
  78. */
  79. public function getBmiLevelTextAttr($value, $data)
  80. {
  81. $levels = [
  82. 'underweight' => '偏瘦',
  83. 'normal' => '正常',
  84. 'overweight' => '偏胖',
  85. 'obese' => '肥胖'
  86. ];
  87. return isset($levels[$data['bmi_level']]) ? $levels[$data['bmi_level']] : '';
  88. }
  89. /**
  90. * 获取性别文本
  91. * @param $value
  92. * @param $data
  93. * @return string
  94. */
  95. public function getGenderTextAttr($value, $data)
  96. {
  97. $genders = [
  98. 0 => '未知',
  99. 1 => '男',
  100. 2 => '女'
  101. ];
  102. return isset($genders[$data['gender']]) ? $genders[$data['gender']] : '未知';
  103. }
  104. /**
  105. * 获取关系文本
  106. * @param $value
  107. * @param $data
  108. * @return string
  109. */
  110. public function getRelationTextAttr($value, $data)
  111. {
  112. $relations = [
  113. 'own' => '本人',
  114. 'family' => '家人',
  115. 'friend' => '朋友'
  116. ];
  117. return isset($relations[$data['relation']]) ? $relations[$data['relation']] : '';
  118. }
  119. /**
  120. * 身体照片获取器
  121. * @param $value
  122. * @param $data
  123. * @return array
  124. */
  125. public function getBodyPhotosAttr($value, $data)
  126. {
  127. if (empty($value)) {
  128. return [];
  129. }
  130. $photos = json_decode($value, true);
  131. return is_array($photos) ? $photos : [];
  132. }
  133. /**
  134. * 身体照片修改器
  135. * @param $value
  136. * @param $data
  137. * @return string
  138. */
  139. public function setBodyPhotosAttr($value, $data)
  140. {
  141. return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  142. }
  143. /**
  144. * 体型选择获取器
  145. * @param $value
  146. * @param $data
  147. * @return array
  148. */
  149. public function getBodyTypesAttr($value, $data)
  150. {
  151. if (empty($value)) {
  152. return [];
  153. }
  154. $types = json_decode($value, true);
  155. return is_array($types) ? $types : [];
  156. }
  157. /**
  158. * 体型选择修改器
  159. * @param $value
  160. * @param $data
  161. * @return string
  162. */
  163. public function setBodyTypesAttr($value, $data)
  164. {
  165. return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  166. }
  167. /**
  168. * AI报告内容获取器
  169. * @param $value
  170. * @param $data
  171. * @return array
  172. */
  173. public function getAiReportContentAttr($value, $data)
  174. {
  175. if (empty($value)) {
  176. return [];
  177. }
  178. $content = json_decode($value, true);
  179. return is_array($content) ? $content : [];
  180. }
  181. /**
  182. * AI报告内容修改器
  183. * @param $value
  184. * @param $data
  185. * @return string
  186. */
  187. public function setAiReportContentAttr($value, $data)
  188. {
  189. return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  190. }
  191. /**
  192. * 根据订单ID获取档案详情
  193. * @param int $orderId
  194. * @return OrderProfile|null
  195. */
  196. public static function getByOrderId($orderId)
  197. {
  198. return self::where('order_id', $orderId)->find();
  199. }
  200. /**
  201. * 根据订单号获取档案详情
  202. * @param string $orderSn
  203. * @return OrderProfile|null
  204. */
  205. public static function getByOrderSn($orderSn)
  206. {
  207. return self::where('order_sn', $orderSn)->find();
  208. }
  209. }