<?php

namespace app\index\controller;

use app\index\controller\Base;
use app\index\model\RoleModel;
class Role extends Base{

    // 角色列表
    public function roleList()
    {
        $param = input('param.');

        $limit = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $param['pageNumber'] = isset($param['pageNumber']) ? $param['pageNumber'] : 1;
        $offset = ($param['pageNumber'] - 1) * $limit;

        $where = [];
        if (!empty($param['searchText'])) {
            $where['role_name'] = ['like', '%' . $param['searchText'] . '%'];
        }

        $user = new RoleModel();
        $selectResult = $user->getRoleByWhere($where, $offset, $limit);

        $return['total'] = $user->getAllRole($where);  // 总数据
        $return['rows'] = $selectResult;

        json_return(200 , $return , 'success');
    }

    // 添加角色
    public function roleAdd()
    {
        $param = input('post.');
        $validate = new \app\index\validate\RoleValidate;
        if (!$validate->check($param)) {
            return json(json_error_exception('1006',$validate->getError()));
        }
        $role = new RoleModel();
        $flag = $role->insertRole($param);
        if($flag['code'] != 1){
            return json(json_error_exception('1006',$flag['msg']));
        }
        json_return(200,[],'success');
    }

    // 删除角色
    public function roleDel()
    {
        $id = input('post.id');
        $role = new RoleModel();
        $flag = $role->delRole($id);
        if($flag['code'] != 1){
            return json(json_error_exception('1006',$flag['msg']));
        }
        $this->removRoleCache();
        json_return(200,[],'success');
    }
}