<?php

namespace app\admin\model\teacher;

use app\admin\model\Report;
use think\Model;
use think\Db;
use traits\model\SoftDelete;


class Exams extends Model
{



    use SoftDelete;

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

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

    // 追加属性
    protected $append = [
        'sim_type_text',
        'fault_names',
        'xianxiang_names',
        'yuanyin_names',
        'buwei_names',
        'fangfa_names',
    ];

    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 getFaultNamesAttr($value, $data)
    {
        $fault_name = '';
        $fault_ids = \app\admin\model\teacher\ExamsFault::where(['exam_id'=>$data['exam_id'],'flag'=>1])->column('fault_id');
        $fault_name = Db::name('fault')->where('fault_id','in',$fault_ids)->column('name');
        return !empty($fault_name)? implode(',',$fault_name):'';
    }

    //现象
    public function getXianxiangNamesAttr($value, $data)
    {
        $xianxian_content = Db::name('real_exam_score')->where('exam_id',$data['exam_id'])->value('xianxian_content');
        $xianxiang_name = !empty($xianxian_content) ? json_decode($xianxian_content,true):'';
        return $xianxiang_name??'';
    }
    //原因
    public function getYuanyinNamesAttr($value, $data)
    {
        $yuanyin_content = Db::name('real_exam_score')->where('exam_id',$data['exam_id'])->value('yuanyin_content');
        $yuanyin_name = !empty($yuanyin_content) ? json_decode($yuanyin_content,true):'';
        return $yuanyin_name??'';
    }

    //部位
    public function getBuweiNamesAttr($value, $data)
    {
        $buwei_content = Db::name('real_exam_score')->where('exam_id',$data['exam_id'])->value('buwei_content');
        $buwei_name = !empty($buwei_content) ? json_decode($buwei_content,true):'';
        return $buwei_name??'';
    }

    //方法
    public function getFangfaNamesAttr($value, $data)
    {
        $fangfa_content = Db::name('real_exam_score')->where('exam_id',$data['exam_id'])->value('fangfa_content');
        $yuanyin_name = !empty($fangfa_content) ? json_decode($fangfa_content,true):'';
        return $fangfa_name??'';
    }

    public function collection() {
        return $this->belongsTo('Collection', 'exam_collection_id');
    }

    public function fault(){
        return $this->hasMany('app\admin\model\teacher\ExamsFault','exam_id','exam_id');
    }





}
