<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;
use think\Db;
class Trainactive extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'pingjia_time_text',
        'sign_starttime_text',
        'sign_endtime_text',
        'status_text',
        'userauth_status_text'
    ];
    

    protected static function init(){
        //文件名
        self::afterUpdate(function ($row) {

            $update = [];

            $changed = $row->getChangedData();
            if (isset($changed['files'])) {
                //文件
                $files = Db::name('attachment')->where('url','IN',$row['files'])->field('url,filename')->select();

                $update['files_json'] = json_encode($files);
            }

            //评价时间不能是null
            if(empty($row['pingjia_time'])){
                $update['pingjia_time'] = 0;
            }

            if(!empty($update)){
                Db::name('train_active')->where('id',$row['id'])->update($update);
            }
        });
        //文件名
        self::afterInsert(function ($row) {
            if (!empty($row['files'])) {
                $files = Db::name('attachment')->where('url','IN',$row['files'])->field('url,filename')->select();

                $update = ['files_json'=>json_encode($files)];
                Db::name('train_active')->where('id',$row['id'])->update($update);
            }
        });
    }
    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }

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


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


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


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


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


    public function getSignEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_endtime']) ? $data['sign_endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $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 getUserauthStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['userauth_status']) ? $data['userauth_status'] : '');
        $list = $this->getUserauthStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

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

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

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

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

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


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


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


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