RoleJsonController.java 2.4 KB
package com.tianbo.controller.json;

import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.Role;
import com.tianbo.model.RoleExample;
import com.tianbo.service.RoleService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.json.ResponseModel;
import com.tianbo.util.tools.MD5Tools;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleJsonController extends BaseController{

    //service类
    @Autowired
    private RoleService roleService;

    @RequestMapping("list.json")
    @RequiresRoles("admin")
    @ResponseBody
    public ModelAndPage list(@ModelAttribute Page frontPage, @ModelAttribute Role role) throws Exception{

        RoleExample example = new RoleExample();
        example.setOrderByClause("role_id");
        //调用service方法得到用户列表
        List<Role> roles = roleService.selectByExample(example);

        ModelAndPage modelAndPage = new ModelAndPage(roles,new Page());

        return modelAndPage;
    }

    @RequestMapping("roleExamlpe.json")
    @ResponseBody
    public Role example(){
        return new Role();
    }

    @RequestMapping(value = "add.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public ResponseModel add(@RequestBody Role role){

        ResponseModel responseModel = new ResponseModel();

        if (checkRole(role)){
            int i = roleService.insert(role);
            if(i>0){
                responseModel.setStatus(200);
                responseModel.setMsg("添加角色成功");
            }else {
                responseModel.setStatus(500);
                responseModel.setMsg("添加角色失败");
            }
        }else {
            responseModel.setStatus(500);
            responseModel.setMsg("添加角色失败,已存在相同名称的角色");
        }
        return responseModel;




    }

    private Boolean checkRole( Role role){
        if (roleService.checkRole(role)){
            return  Boolean.FALSE; //存在用户,返回false
        }else {
            return Boolean.TRUE;  //不存在用户,返回true,可以添加用户
        }
    }

}