Goods.php 3.3 KB
<?php

namespace app\admin\model;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_group_text',
        'is_vip_price_text',
        'is_recommend_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getIsGroupList()
    {
        return ['1' => __('Is_group 1'), '2' => __('Is_group 2')];
    }

    public function getIsVipPriceList()
    {
        return ['1' => __('Is_vip_price 1'), '2' => __('Is_vip_price 2')];
    }

    public function getIsRecommendList()
    {
        return ['1' => __('Is_recommend 1'), '2' => __('Is_recommend 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getIsGroupTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_group']) ? $data['is_group'] : '');
        $list = $this->getIsGroupList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsVipPriceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_vip_price']) ? $data['is_vip_price'] : '');
        $list = $this->getIsVipPriceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
        $list = $this->getIsRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function brand()
    {
        return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function category()
    {
        return $this->belongsTo('Category', 'category_one_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function category2()
    {
        return $this->belongsTo('Category2', 'category2_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function level()
    {
        return $this->belongsTo('Level', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function country()
    {
        return $this->belongsTo('Country', 'country_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function part()
    {
        return $this->belongsTo('Part', 'part_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function categorygroup()
    {
        return $this->belongsTo('CategoryGroup', 'category_group_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function depot()
    {
        return $this->belongsTo('Depot', 'depot_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}