<?php
namespace app\admin\model;
use app\BaseModel;
class Admin extends BaseModel
{
public function search($srcfrom)
{
$src = [
'searchval' => ''
];
$src = array_cover($srcfrom, $src);
$data = $this
->where('id', '>', 2)
->where('id', '<>', session('userid'))
->when(strlen($src['searchval']) > 0, function($query) use($src){
$query->where('xingming|username', 'like', '%' . $src['searchval'] . '%');
})
->with([
'adSchool' => function($query){
$query->field('id, jiancheng');
}
,'glGroup'
])
->hidden([
'password'
,'create_time'
,'update_time'
,'delete_time'
])
->select();
return $data;
}
public function srcAuth($user_id)
{
$data = self::where('id', $user_id)
->field('id')
->with([
'glGroup'
])
->find();
$arr = array();
foreach ($data->glGroup as $key => $value) {
$temp = explode(",", $value->rules);
$arr = array_merge($arr, $temp);
}
$arr = array_unique($arr);
return $arr;
}
public function searchOne($id)
{
$adminInfo = $this->where('id', $id)
->field('id, username, xingming, teacher_id')
->find();
return $adminInfo;
}
public function glGroup()
{
return $this->belongsToMany('AuthGroup', 'AuthGroupAccess', 'group_id', 'uid');
}
public function password($username)
{
$pasW = $this
->where('username', $username)
->value('password');
return $pasW;
}
public function setShengriAttr($value)
{
strlen($value) >0 ? $value = strtotime($value) : $value = '';
return $value;
}
public function getShengriAttr($value)
{
return date('Y-m-d', $value);
}
public function getCreateTimeAttr($value)
{
return date('Y-m-d', $value);
}
public function getSexAttr($value)
{
$sex = [
'0' => '女'
,'1' => '男'
,'2' => '保密'
];
$str = '';
if(isset($sex[$value]))
{
$str = $sex[$value];
}else{
$str = '未知';
}
return $str;
}
public function getLasttimeAttr($value)
{
return date('Y年m月d日 H:i:s', $value);
}
public function getThistimeAttr($value)
{
return date('Y年m月d日 H:i:s', $value);
}
public function getGroupnames($userid)
{
if($userid == 1 || $userid == 2)
{
return '超级管理员';
}
$admininfo = $this->where('id', $userid)
->field('id')
->with([
'glGroup'=>function($query){
$query->where('status', 1);
}
])
->find();
$groupname = '';
foreach ($admininfo->gl_group as $key => $value) {
if($key == 0){
$groupname = $value->title;
}else{
$groupname = $groupname . '、' . $value->title;
}
}
return $groupname;
}
public function adSchool()
{
return $this->belongsTo('\app\system\model\School', 'school_id', 'id');
}
}