public class Constants {

    // shiro redis cache prefix
    public static String SHIRO_REDIS_SESSION = "shiro_redis_session:";
    public static String SHIRO_REDIS_CACHE = "shiro_redis_cache:";
    public static String SESSSION_USER = "SESSION_USER";

    public static final int USER_STATUS_ENABLE = 1;
    public static final int USER_STATUS_DISABLE = 2;

    public static final int ROLE_STATUS_ENABLE = 1;
    public static final int ROLE_STATUS_DISABLE = 2;

} public class Resource {
    private Long id;

    private Long pid;

    private String name;

    private String url;

    private String icon;

    private Integer status;

    private Long sort;

    private Date createTime;

    private Date updateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getSort() {
        return sort;
    }

    public void setSort(Long sort) {
        this.sort = sort;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
} public class Role {
    private Long id;

    private String name;

    private Integer status;

    private Date createTime;

    private Date updateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
} public class RoleResource {
    private Long id;

    private Long roleId;

    private Long resourceId;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    public Long getResourceId() {
        return resourceId;
    }

    public void setResourceId(Long resourceId) {
        this.resourceId = resourceId;
    }
} public class User implements Serializable {
    private Long id;

    private String username;

    private String password;

    private String salt;

    private Integer status;

    private String realName;

    private String remark;

    private String email;

    private Date createTime;

    private Date updateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
} public interface ResourceService {

    boolean addResource(Resource resource);

    boolean deleteResource(Long resourceId);

    boolean updateResource(Resource resource);

    Resource getResource(Long resourceId);

    List<Resource> getResourceList();

    List<Resource> getUserPermissions(Long userId);

    int getChildrenCount(Long pid);

    int batchUpdateSort(List<Resource> list);
} public interface UserService {

    boolean addUser(User user);

    boolean deleteUser(Long userId);

    boolean updateUser(User user);

    boolean updateUserStatus(Long id, Integer status);

    User getUser(Long userId);

    User getByUsername(String username);

    PageInfo getUserList(int pageNum, int pageSize, User user);

    boolean batchDelUser(String ids);

    JsonResult bindRole(Long userId, String roleIds);

    JsonResult batchBindRole(String userIds, String roleIds);
} @Component
public class RoleService {
    
    @Resource
    RoleMapper roleMapper;
    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    RoleResourceMapper roleResourceMapper;

    public boolean addRole(Role Role) {
        return roleMapper.insertSelective(Role) == 1;
    }

    public boolean deleteRole(Long roleId) {
        return roleMapper.deleteByPrimaryKey(roleId) == 1;
    }

    public JsonResult batchDeleteRole(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, ResultCode.PARAM_ERROR_MSG);
        }
        String[] arr = ids.split(",");
        List<Long> delList = new ArrayList<>();
        List<String> cannotDelList = new ArrayList<>();
        for(String id : arr) {
            long roleId = Long.valueOf(id);
            int count = this.getRoleUserCount(roleId);
            if(count > 0) {
                cannotDelList.add(id);
            } else {
                delList.add(Long.valueOf(id));
            }
        }
        if(cannotDelList.size() > 0) {
            String cannotDelIds = StringUtils.join(cannotDelList, ",");
            return new JsonResult(ResultCode.SUCCESS_CODE, "ID为(" + cannotDelIds + ")的角色已绑定操作员，无法删除");
        }
        int row = roleMapper.batchDelete(delList);
        return row > 0 ? JsonResult.success() : JsonResult.error();
    }

    public boolean updateRole(Role Role) {
        return roleMapper.updateByPrimaryKeySelective(Role) == 1;
    }

    public boolean updateRoleStatus(Long id, Integer status) {
        return roleMapper.updateRoleStatus(id, status) == 1;
    }

    public Role getRole(Long roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    public PageInfo getRoleList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleMapper.getRoleList();
        return new PageInfo(list);
    }

    public List<Role> getUserRoles(Long userId) {
        return roleMapper.getUserRoles(userId);
    }

    public int getRoleUserCount(Long roleId) {
        return userRoleMapper.getRoleUserCount(roleId);
    }

    public List<RoleResource> getResourcesByRoleId(Long roleId) {
        return roleResourceMapper.selectByRoleId(roleId);
    }

    public JsonResult bindResource(Long roleId, String resourceIds) {
        try {
            roleResourceMapper.deleteByRoleId(roleId);
            if(StringUtils.isEmpty(resourceIds)) {
                return JsonResult.success();
            }
        } catch (Exception e) {
            return JsonResult.error();
        }

        String[] ids = resourceIds.split(",");
        List<RoleResource> list = new ArrayList<>();
        for(String id : ids) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(Long.valueOf(id));
            list.add(roleResource);
        }
        int row = roleResourceMapper.batchInsert(list);
        return row > 0   ? JsonResult.success() : JsonResult.error();
    }
} @SuppressWarnings("serial")
public class DataGrid<T> implements Serializable {

	private long total;
	private List<T> rows;
	private Object option;
	
    public DataGrid(PageInfo pageInfo) {
        this.total = pageInfo.getTotal();
        this.rows = pageInfo.getList();
    }

    public DataGrid(PageInfo pageInfo, Object option) {
        this.total = pageInfo.getTotal();
        this.rows = pageInfo.getList();
        this.option = option;
    }
	
	public long getTotal() {
		return total;
	}
	public void setTotal(long total) {
		this.total = total;
	}
	public List<T> getRows() {
		return rows;
	}
	public void setRows(List<T> rows) {
		this.rows = rows;
	}
	
	public Object getOption() {
		return option;
	}

	public void setOption(Object option) {
		this.option = option;
	}

	@Override
    public String toString() {
		String rowsstr = "null";
		if (rows != null)
			rowsstr = "" + rows.size();
		String optionstr = "null";
		if (optionstr != null)
			optionstr = option.toString();
        return "DataGrid{" +
                "total=" + total +
                ", rows=" + rowsstr +
                ", option=" + optionstr +
                "}";
    }
	
	
} @SuppressWarnings("serial")
public class JsonResult<T> implements Serializable {

	private int code;
	private String msg;
	private T body;
	
	public JsonResult() {
	}
	
	public JsonResult(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	public JsonResult(int code, String msg, T body) {
		this.code = code;
		this.msg = msg;
		this.body = body;
	}

    public static JsonResult success() {
        return new JsonResult(ResultCode.SUCCESS_CODE, ResultCode.SUCCESS_MSG);
    }

    public static JsonResult error() {
        return new JsonResult(ResultCode.ERROR_CODE, ResultCode.ERROR_MSG);
    }
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public T getBody() {
		return body;
	}
	public void setBody(T body) {
		this.body = body;
	}

	@Override
    public String toString() {
		String bodystr = "null";
		if (body != null)
			bodystr = body.toString();
        return "JsonResult{" +
                "code=" + code +
                ", message=" + msg +
                ", body=" + bodystr +
                "}";
    }
	
} public class ResultCode implements Serializable {
    //成功
    public static final int SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "操作成功";

    //失败
    public static final int ERROR_CODE = 500;
    public static final String ERROR_MSG = "服务器内部错误，请重试";

    /**通用错误码：6xx,记录业务级别通用错误，比如：数据库错误**/
    public static final int PARAM_ERROR_CODE = 601;
    public static final String PARAM_ERROR_MSG = "参数错误，请检查参数";

    public static final int DB_ERROR_CODE = 602;
    public static final String DB_ERROR_MSG = "数据库操作失败，请稍后再试";

    //Token有误
    public static final int TOKEN_ERROR_CODE = 603;
    public static final String TOKEN_ERROR_MSG = "Token过期，请重新登录";


    /**业务相关错误码：7xx,记录业务级别具体错误，比如登陆密码错误**/
    public static final int ERROR_CODE_701 = 701;
    public static final int ERROR_CODE_702 = 702;
    public static final int ERROR_CODE_703 = 703;
    public static final int ERROR_CODE_704 = 704;
    public static final int ERROR_CODE_705 = 705;
    public static final int ERROR_CODE_706 = 706;
    public static final int ERROR_CODE_707 = 707;
    public static final int ERROR_CODE_708 = 708;
    public static final int ERROR_CODE_709 = 709;

} public class Tree implements Serializable {

    private Long id;
    private Long pid;
    private String text;
    private String iconCls;
    private Map attributes;
    private List<Tree> children;
    private Long sort;
    private String url;
    private boolean checked;

    public Tree(){}

    public Tree(Long id, Long pid, String text) {
        this.id = id;
        this.pid = pid;
        this.text = text;
    }

    public Tree(Long id, Long pid, String text, String iconCls) {
        this.id = id;
        this.pid = pid;
        this.text = text;
        this.iconCls = iconCls;
    }

    public Tree(Long id, Long pid, String text, String iconCls, Long sort) {
        this.id = id;
        this.pid = pid;
        this.text = text;
        this.iconCls = iconCls;
        this.sort = sort;
    }

    public Tree(Long id, Long pid, String text, String iconCls, Long sort, String url) {
        this.id = id;
        this.pid = pid;
        this.text = text;
        this.iconCls = iconCls;
        this.sort = sort;
        this.url = url;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public Map getAttributes() {
        return attributes;
    }

    public void setAttributes(Map attributes) {
        this.attributes = attributes;
    }

    public List<Tree> getChildren() {
        return children;
    }

    public void setChildren(List<Tree> children) {
        this.children = children;
    }

    public Long getSort() {
        return sort;
    }

    public void setSort(Long sort) {
        this.sort = sort;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
} public class StringUtils {

    public static final String[] EMPTY_STRING_ARRAY = new String[0];
    private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");

    public static boolean isBlank(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return str != null && str.length() > 0;
    }

    public static boolean isEquals(String s1, String s2) {
        return s1 == null && s2 == null?true:(s1 != null && s2 != null?s1.equals(s2):false);
    }

    public static boolean isInteger(String str) {
        return str != null && str.length() != 0?INT_PATTERN.matcher(str).matches():false;
    }

    public static int parseInteger(String str) {
        return !isInteger(str)?0: Integer.parseInt(str);
    }

    public static boolean isContains(String[] values, String value) {
        if(value != null && value.length() > 0 && values != null && values.length > 0) {
            String[] arr$ = values;
            int len$ = values.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String v = arr$[i$];
                if(value.equals(v)) {
                    return true;
                }
            }
        }

        return false;
    }

    public static boolean isNumeric(String str) {
        if(str == null) {
            return false;
        } else {
            int sz = str.length();

            for(int i = 0; i < sz; ++i) {
                if(!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }


    public static String[] split(String str, char ch) {
        ArrayList list = null;
        int ix = 0;
        int len = str.length();

        for(int i = 0; i < len; ++i) {
            char c = str.charAt(i);
            if(c == ch) {
                if(list == null) {
                    list = new ArrayList();
                }

                list.add(str.substring(ix, i));
                ix = i + 1;
            }
        }

        if(ix > 0) {
            list.add(str.substring(ix));
        }

        return list == null?EMPTY_STRING_ARRAY:(String[])((String[])list.toArray(EMPTY_STRING_ARRAY));
    }

    public static String join(String[] array) {
        if(array.length == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            String[] arr$ = array;
            int len$ = array.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String s = arr$[i$];
                sb.append(s);
            }

            return sb.toString();
        }
    }

    public static String join(String[] array, char split) {
        if(array.length == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < array.length; ++i) {
                if(i > 0) {
                    sb.append(split);
                }

                sb.append(array[i]);
            }

            return sb.toString();
        }
    }

    public static String join(String[] array, String split) {
        if(array.length == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < array.length; ++i) {
                if(i > 0) {
                    sb.append(split);
                }

                sb.append(array[i]);
            }

            return sb.toString();
        }
    }
	
	public static String toString(String[] strArray, String separator) {
		String strResult = "";
		if (strArray == null || separator == null) {
			strResult = null;
		} else {
			StringBuffer strBuffer = new StringBuffer();
			for (int i = 0; i < strArray.length; i++) {
				if (strArray[i] == null || strArray[i].length() == 0)
					continue;
				strBuffer.append(separator);
				strBuffer.append(strArray[i]);
			}
			if (strBuffer.length() > 0) {
				strBuffer.delete(0, separator.length());
			}
			strResult = strBuffer.toString();
		}
		return strResult;
	}

	public static String join(List<String> list, String separator, String quot, String defaultValue) {
		StringBuffer sb = new StringBuffer();
		if (list == null || list.size() == 0) {
			return defaultValue;
		} else {
			for (String value : list) {
				if (sb.length() > 0 && separator != null) {
					sb.append(separator);
				}
				if (quot != null) {
					sb.append(quot);
				}
				sb.append(value);
				if (quot != null) {
					sb.append(quot);
				}
			}
			return sb.toString();
		}
	}

	public static String join(Collection<String> coll, String split) {
		if(coll.isEmpty()) {
			return "";
		} else {
			StringBuilder sb = new StringBuilder();
			boolean isFirst = true;

			String s;
			for(Iterator i$ = coll.iterator(); i$.hasNext(); sb.append(s)) {
				s = (String)i$.next();
				if(isFirst) {
					isFirst = false;
				} else {
					sb.append(split);
				}
			}

			return sb.toString();
		}
	}

}
import com.github.pagehelper.PageInfo;
import com.yuzhi.back.common.Constants;
import com.yuzhi.back.dao.entity.Resource;
import com.yuzhi.back.dao.entity.Role;
import com.yuzhi.back.dao.entity.RoleResource;
import com.yuzhi.back.dao.entity.User;
import com.yuzhi.back.service.ResourceService;
import com.yuzhi.back.service.UserService;
import com.yuzhi.back.service.impl.RoleService;
import com.yuzhi.back.common.DataGrid;
import com.yuzhi.back.common.JsonResult;
import com.yuzhi.back.common.ResultCode;
import com.yuzhi.back.common.Tree;
import com.yuzhi.back.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
package com.yuzhi.back.web.controller;



/**
 * Created by xuejt on 2015/4/3.
 */
@Controller
@RequestMapping(value = "/")
public class RoleController {

    @javax.annotation.Resource