<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Productchufabanci extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'chufatime_text',
        'chufatype_text',
        'fache_status_text',
        'fache_time_text',
        'daozhan_time_text'
    ];
    

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

    public function getFacheStatusList()
    {
        return ['0' => __('Fache_status 0'), '1' => __('Fache_status 1'), '2' => __('Fache_status 2')];
    }


    public function getChufatimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['chufatime']) ? $data['chufatime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getChufatypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['chufatype']) ? $data['chufatype'] : '');
        $list = $this->getChufatypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFacheStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['fache_status']) ? $data['fache_status'] : '');
        $list = $this->getFacheStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFacheTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['fache_time']) ? $data['fache_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDaozhanTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['daozhan_time']) ? $data['daozhan_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setChufatimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFacheTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDaozhanTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $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);
    }


    public function car()
    {
        return $this->belongsTo('Car', 'car_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function coach()
    {
        return $this->belongsTo('Coach', 'coach_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
