Screen.php 2.7 KB
<?php

namespace app\admin\model;

use think\Model;


class Screen extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'is_view_text',
        'count_time',
        'team_info'
    ];
    

    
    public function getIsViewList()
    {
        return ['0' => __('Is_view 0'), '1' => __('Is_view 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 getIsViewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_view']) ? $data['is_view'] : '');
        $list = $this->getIsViewList();
        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);
    }

    //倒计时时间
    public function getCountTimeAttr($value,$data){
        $time = time();
        $list = [];
        if ($data['starttime']<=$time && $data['endtime']>=$time){
            $hour = intval((($data['endtime']-$time)%86400)/3600);
            $minute = intval(((($data['endtime']-$time)%86400)%3600)/60);
            $second = intval(((($data['endtime']-$time)%86400)%3600)%60);
            $list = $hour."时".$minute."分".$second."秒";
        }else if ($data['endtime']<=$time){
            $list = "00:00:00";
        }
        return $list;
    }

    //战队信息
    public function getTeamInfoAttr($value,$data){
        $list = [];
        $value = db('study_score_log')->where('campus_id',$data['campus_id'])->field('team_id')->select();
        foreach ($value as $k => $v){
            $list = db('team')->where('id',$value[$k]['team_id'])->field('title,score')->find();
        }
        return $list;
    }


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