<?php
declare (strict_types = 1);

namespace app\model\system;

use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\Model;

/**
 * @mixin \think\Model
 */
class AdminRole extends BaseModel
{
    use ModelTrait;
    use JwtAuthModelTrait;
    private $roleData;
    private $sassId = 0;

    public function setSassId($sassid) {
        $this->sassId = $sassid;
    }


    /**
     * @param $post
     */
    public function saveRole($post) {
        $save['name'] = $post['name'];
        $save['status'] = $post['status'];
        if($post['id']) {
            $info = $this->where("name",$post['name'])->find();
            if(!empty($info) && $info['id'] != $post['id'] ) {
                return self::setErrorInfo('角色名重复了');
            }
            $this->where('id',$post['id'])->save($save);
            return true;
        } else {
            $this->insert([
                'name'   => $post['name'],
                'status' => $post['status']
            ]);
        }
        return true;
    }


    /**
     * 获取角色数据
     * @param $id
     * @param string $field
     * @return mixed
     */
    public function getRoleId($id,$field = '*') {
        $this->getRoleData();
        foreach ($this->roleData as $v) {
            if($v['id'] == $id) {
                return $field == '*' ? $v : $v[$field];
            }
        }
    }


    public function getRoleData(){
        if(empty($this->roleData))
            $this->roleData = $this->where('status',1)->select();
        return $this->roleData;
    }
}
