<?php

namespace app\admin\model;

use think\Model;
use think\Session;
use traits\model\SoftDelete;
use app\admin\model\department\Department;

class Admin extends Model
{
    use SoftDelete;

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $hidden = [
        'password',
        'salt',
    ];

    // 追加属性
    protected $append = [
        'department_text'
    ];

    public static function init()
    {
        self::beforeWrite(function ($row) {
            $changed = $row->getChangedData();
            //如果修改了用户或或密码则需要重新登录
            if (isset($changed['username']) || isset($changed['password']) || isset($changed['salt'])) {
                $row->token = '';
            }
        });
    }

    public function getDepartmentTextAttr($value, $data){
        $text = '';
        if(!empty($data['depart_id'])){
            $qudui = Department::where(['id'=>$data['depart_id']])->find();
            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'])){
                            $parent = Department::where(['id'=>$zhuanye['parent_id']])->find();

                            $text = $parent['name'].">>>".$zhuanye['name'].">>>".$ji['name'].">>>".$dui['name'].">>>".$qudui['name'];
                        }
                    }
                }
            }
        }
        return $text;
    }


}
