123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace app\common\model;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 订单档案详情记录模型
- * Class OrderProfile
- * @package app\common\model
- */
- class OrderProfile extends Model
- {
- // 表名
- protected $name = 'shop_order_profile';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'body_types_text',
- 'bmi_level_text',
- 'gender_text',
- 'relation_text'
- ];
-
- /**
- * 关联订单
- * @return \think\model\relation\BelongsTo
- */
- public function order()
- {
- return $this->belongsTo('Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
-
- /**
- * 关联用户
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
-
- /**
- * 关联档案
- * @return \think\model\relation\BelongsTo
- */
- public function profile()
- {
- return $this->belongsTo('BodyProfile', 'profile_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
-
- /**
- * 获取体型选择文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getBodyTypesTextAttr($value, $data)
- {
- if (empty($data['body_types'])) {
- return '';
- }
-
- $bodyTypes = json_decode($data['body_types'], true);
- if (!is_array($bodyTypes)) {
- return '';
- }
-
- $texts = [];
- foreach ($bodyTypes as $type) {
- if (isset($type['name'])) {
- $texts[] = $type['name'];
- }
- }
-
- return implode(', ', $texts);
- }
-
- /**
- * 获取BMI等级文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getBmiLevelTextAttr($value, $data)
- {
- $levels = [
- 'underweight' => '偏瘦',
- 'normal' => '正常',
- 'overweight' => '偏胖',
- 'obese' => '肥胖'
- ];
-
- return isset($levels[$data['bmi_level']]) ? $levels[$data['bmi_level']] : '';
- }
-
- /**
- * 获取性别文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getGenderTextAttr($value, $data)
- {
- $genders = [
- 0 => '未知',
- 1 => '男',
- 2 => '女'
- ];
-
- return isset($genders[$data['gender']]) ? $genders[$data['gender']] : '未知';
- }
-
- /**
- * 获取关系文本
- * @param $value
- * @param $data
- * @return string
- */
- public function getRelationTextAttr($value, $data)
- {
- $relations = [
- 'own' => '本人',
- 'family' => '家人',
- 'friend' => '朋友'
- ];
-
- return isset($relations[$data['relation']]) ? $relations[$data['relation']] : '';
- }
-
- /**
- * 身体照片获取器
- * @param $value
- * @param $data
- * @return array
- */
- public function getBodyPhotosAttr($value, $data)
- {
- if (empty($value)) {
- return [];
- }
-
- $photos = json_decode($value, true);
- return is_array($photos) ? $photos : [];
- }
-
- /**
- * 身体照片修改器
- * @param $value
- * @param $data
- * @return string
- */
- public function setBodyPhotosAttr($value, $data)
- {
- return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
- }
-
- /**
- * 体型选择获取器
- * @param $value
- * @param $data
- * @return array
- */
- public function getBodyTypesAttr($value, $data)
- {
- if (empty($value)) {
- return [];
- }
-
- $types = json_decode($value, true);
- return is_array($types) ? $types : [];
- }
-
- /**
- * 体型选择修改器
- * @param $value
- * @param $data
- * @return string
- */
- public function setBodyTypesAttr($value, $data)
- {
- return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
- }
-
- /**
- * AI报告内容获取器
- * @param $value
- * @param $data
- * @return array
- */
- public function getAiReportContentAttr($value, $data)
- {
- if (empty($value)) {
- return [];
- }
-
- $content = json_decode($value, true);
- return is_array($content) ? $content : [];
- }
-
- /**
- * AI报告内容修改器
- * @param $value
- * @param $data
- * @return string
- */
- public function setAiReportContentAttr($value, $data)
- {
- return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
- }
-
- /**
- * 根据订单ID获取档案详情
- * @param int $orderId
- * @return OrderProfile|null
- */
- public static function getByOrderId($orderId)
- {
- return self::where('order_id', $orderId)->find();
- }
-
- /**
- * 根据订单号获取档案详情
- * @param string $orderSn
- * @return OrderProfile|null
- */
- public static function getByOrderSn($orderSn)
- {
- return self::where('order_sn', $orderSn)->find();
- }
- }
|