Study.php 2.1 KB
<?php

namespace app\admin\model;

use think\Model;


class Study extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'gender_text',
        'team_rank',
        'barcode_images',
        'rank'
    ];
    

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


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public  function getTeamRankAttr($value,$data){
            $list = $this->where('team_id',$data['team_id'])->order('earn_score','desc')->column('id');
            $order = 0;
            foreach ($list as $k=> $v){
                if($data['id'] == $v){
                    $order = $k+1;
                    break;
                }
            }
            return $order;
    }

    public  function getRankAttr($value,$data){
        $list = $this->order('earn_score','desc')->column('id');
        $order = 0;
        foreach ($list as $k=> $v){
            if($data['id'] == $v){
                $order = $k+1;
                break;
            }
        }
        return $order;
    }

    public function getBarcodeImagesAttr($value,$data){
        $list = $this->where('id',$data['id'])->value('barcode');
        $order = 'http://campus.shs.broing.cn'.$list;
        return $order;
    }


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


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


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