OrderGoods.php 2.0 KB
<?php

namespace app\common\model;

use think\Model;

class OrderGoods extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'goods_style_text',
        'make_type_text'
    ];

    /**
     * 商品
     */
    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id')->setEagerlyType(0);
    }

    /**
     * 尺寸
     */
    public function userSize(){
        return $this->belongsTo('UserSize', 'user_size_id');
    }

    /**
     * 关联商品规格表
     */
    public function spec()
    {
        return $this->belongsTo('GoodsSpec', 'spec_sku_id', 'spec_sku_id');
    }
    
    /**
     * 定制项详情
     */
    public function getGoodsStyleTextAttr($value,$data){
        $goods_style_text = [];
        if(!empty($data['goods_style'])){
            $goods_style = json_decode($data['goods_style'],true);
            foreach($goods_style as $k => $v){
                $style = Style::where('id',$k)->field('style_name,style_type')->find()->toArray();
                $style['style_value'] = '';
                switch ($style['style_type']) {
                	case '1':
                		$style['style_value'] = StyleValue::where('id',$v)->value('style_value_name');
                		break;
                	case '2':
                		$style['style_value'] = $v;
                		break;
                	case '3':
                		$style['style_value'] = cdnurl($v,true);
                		break;
                }
                $goods_style_text[] = $style;
            }
        }
        return $goods_style_text;
    }

    /**
     * 定制项名称
     */
    public function getMakeTypeTextAttr($value,$data){
        $make_type_arr = ['0'=>'','1'=>'模特款','2'=>'自定义'];
        return $make_type_arr[$data['make_type']];
    }
}