<?php

namespace app\admin\model\department;
use app\admin\model\department\Department;


class AuthAdmin extends \app\admin\model\Admin
{
    // 表名
    protected $name = 'admin';

    // 追加属性
    protected $append = [
        'dui_text',
        'zhuanye_text',
        'cengci_text'
    ];

    public function getDuiTextAttr($value, $data){
        $qudui = Department::where(['id'=>$data['depart_id']])->find();
        $dui_text = '';
        if(!empty($qudui['parent_id'])){
            $dui_text = Department::where(['id'=>$qudui['parent_id']])->value('name');
        }
        return $dui_text;
    }

    public function getZhuanyeTextAttr($value, $data){
        $qudui = Department::where(['id'=>$data['depart_id']])->find();
        $zhuanye_text = '';
        if(!empty($qudui['parent_id'])){
            $dui= Department::where(['id'=>$qudui['parent_id']])->find();
            if(!empty($dui['parent_id'])){
                $ji = Department::where(['id'=>$dui['parent_id']])->find();
                if(!empty($ji['parent_id'])){
                    $zhuanye_text = Department::where(['id'=>$ji['parent_id']])->value('name');
                }
            }
        }
        return $zhuanye_text;
    }
    public function getCengciTextAttr($value, $data){
        $qudui = Department::where(['id'=>$data['depart_id']])->find();
        $cengci_text = '';
        if(!empty($qudui['parent_id'])){
            $dui= Department::where(['id'=>$qudui['parent_id']])->find();
            if(!empty($dui['parent_id'])){
                $ji = Department::where(['id'=>$dui['parent_id']])->find();
                if(!empty($ji['parent_id'])){
                    $zhuanye = Department::where(['id'=>$ji['parent_id']])->find();
                    if(!empty($zhuanye['parent_id'])){
                        $cengci_text = Department::where(['id'=>$zhuanye['parent_id']])->value('name');
                    }
                }
            }
        }
        return $cengci_text;
    }
    /**
     * 关联部门中间表
     * @return \think\model\relation\HasMany
     */
    public function dadmin()
    {
        return $this->hasMany('\app\admin\model\department\Admin', 'admin_id', 'id');
    }

    /**
     * 关联部门表
     * @return \think\model\relation\BelongsToMany
     */
    public function departments()
    {
        return $this->belongsToMany('\app\admin\model\department\Department','DepartmentAdmin','department_id','admin_id');
    }

    /**
     * 关联角色组
     * @return \think\model\relation\HasMany
     */
    public function groups()
    {
        return $this->hasMany('\app\admin\model\department\AuthGroupAccess', 'uid', 'id');
    }


}