<?php
namespace app\model;
use think\Model;
class Allot extends Model{
protected $type = [
'time'=>'timestamp:Y-m-d',
'file'=>'json'
];
public function frameData(){
return $this->hasOne(Frame::class,'id','frame');
}
public function peopleData(){
return $this->hasOne(People::class,'id','people')->field(['id','name']);
}
public function userData(){
return $this->hasOne(User::class,'id','user')->field(['id','name']);
}
public function recordData(){
return $this->hasMany(Record::class,'source','id')->with(['userData'])->where([['type','=','allot']])->append(['extension'])->order('id desc');
}
public function getTotalAttr($val,$data){
return floatval($val);
}
public function setPeopleAttr($val){
return empty($val)?0:$val;
}
public function getPeopleAttr($val){
return empty($val)?null:$val;
}
public function setMoreAttr($val){
return json_encode((object)$val);
}
public function getMoreAttr($val){
return json_decode($val);
}
public function getExtensionAttr($val,$data){
$source=[];
$source['examine']=[0=>'未审核',1=>'已审核'][$data['examine']];
return $source;
}
public static function onBeforeUpdate($model){
$source=$model::where([['id','=',$model['id']]])->find();
if(!empty($source['examine'])){
exit(json(['state'=>'error','info'=>'[ ERROR ] 单据已审核!'],200)->send());
}
}
}