<?php

namespace app\admin\model\score;

use think\Model;
use traits\model\SoftDelete;

class Log extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'status_text',
        'authtime_text'
    ];
    

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


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


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

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


    public function scoreevent()
    {
        return $this->belongsTo('Event', 'score_event_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function employee()
    {
        return $this->belongsTo('Employee', 'score_employee_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


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


    public function team()
    {
        return $this->belongsTo('Team', 'score_team_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
