Files
amb_wechatapp/application/admin/model/app/testpaper/Testpaper.php
T

104 lines
2.2 KiB
PHP

<?php
namespace app\admin\model\app\testpaper;
use think\Model;
class Testpaper extends Model
{
// 表名
protected $name = 'app_testpaper';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'random_type_text',
'status_text',
'random_type_val',
'score_amount',
'question_amount'
];
public function getRandomTypeValAttr($value, $data)
{
if(!$data['random_type_val']){
return [];
}
return json_decode($data['random_type_val'],true);
}
public function getRandomTypeList()
{
return ['random' => __('Random_type random'), 'type' => __('Random_type type')];
}
public function getStatusList()
{
return ['1' => __('Status 1'), '0' => __('Status 0')];
}
public function getRandomTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['random_type']) ? $data['random_type'] : '');
$list = $this->getRandomTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function group()
{
return $this->belongsTo('Group', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 获取试卷总分
* @param $value
* @param $data
* @return mixed
*/
public function getScoreAmountAttr($value, $data)
{
$count = Question::where([
'testpaper_id'=>$data['id']
])->sum('score');
return $count;
}
/**
* 获取题目数量
* @param $value
* @param $data
* @return mixed
*/
public function getQuestionAmountAttr($value, $data)
{
$count = Question::where([
'testpaper_id'=>$data['id']
])->count();
return $count;
}
}