<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Productticket extends Model
{

    

    

    // 表名
    protected $table = 'product_ticket';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

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

    // 追加属性
    protected $append = [
        'roadtype_text',
        'is_qudao_text'
    ];
    

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $road_info = Db::name('product_road')->where('id',$row->road_id)->find();
            $row->roadtype = $road_info['roadtype'];
        });
        self::beforeInsert(function ($row) {
            $road_info = Db::name('product_road')->where('id',$row->road_id)->find();
            $row->roadtype = $road_info['roadtype'];
        });
    }
    
    public function getRoadtypeList()
    {
        return ['1' => __('Roadtype 1'), '2' => __('Roadtype 2'), '3' => __('Roadtype 3')];
    }

    public function getIsQudaoList()
    {
        return ['0' => __('Is_qudao 0'), '1' => __('Is_qudao 1')];
    }


    public function getRoadtypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['roadtype']) ? $data['roadtype'] : '');
        $list = $this->getRoadtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsQudaoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_qudao']) ? $data['is_qudao'] : '');
        $list = $this->getIsQudaoList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function product()
    {
        return $this->belongsTo('Product', 'product_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function road()
    {
        return $this->belongsTo('app\admin\model\Productroad', 'road_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
