<?php

namespace app\admin\model\teacher;

use think\Model;
use traits\model\SoftDelete;
use app\admin\model\teacher\Exams;

class Collection extends Model
{



    use SoftDelete;

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

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

    // 追加属性
    protected $append = [
        'sim_type_text',
        'yikao_count'
    ];

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            if(!empty($row['createtime'])){
                $pk = $row->getPk();
                $create_time = date('Y-m-d H:i:s',$row['createtime']);
                $update_time = date('Y-m-d H:i:s',$row['updatetime']);
                $row->getQuery()->where($pk, $row[$pk])->update(['create_time' => $create_time,'update_time'=>$update_time]);
            }
        });
        self::afterInsert(function ($row) {
            if(!empty($row['createtime'])){
                $pk = $row->getPk();
                $create_time = date('Y-m-d H:i:s',$row['createtime']);
                $update_time = date('Y-m-d H:i:s',$row['updatetime']);
                $row->getQuery()->where($pk, $row[$pk])->update(['create_time' => $create_time,'update_time'=>$update_time]);
            }
        });
    }

    public function getSimTypeTextAttr($value, $data)
    {
        $sim_type_text = '';
        if($data['sim_type']=='0001'){
            $sim_type_text = 'FZD04B型侦毒器';
        }else if ($data['sim_type']=='0002'){
            $sim_type_text = 'FZB006型毒剂报警器';
        }else if ($data['sim_type']=='0003'){
            $sim_type_text = '防化兵用毒剂报警器';
        }
        return $sim_type_text;
    }


    public function getYikaoCountAttr($value, $data)
    {
        return Exams::where(['exam_collection_id'=>$data['exam_collection_id'],'exam_collection_type'=>3,'starttime'=>['>',0]])->count();
    }


    public  function allexam()
    {
        return $this->hasMany('Exams','exam_collection_id','exam_collection_id')->where('deletetime is null');
    }








}
