content: package me.flyray.rbac.controller;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.shiro.authz.annotation.RequiresPermissions;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  import me.flyray.rbac.annotation.SysLog;
  import me.flyray.rbac.entity.SysRoleEntity;
  import me.flyray.rbac.service.SysRoleMenuService;
  import me.flyray.rbac.service.SysRoleService;
  import me.flyray.rbac.utils.Constant;
  import me.flyray.rbac.utils.PageUtils;
  import me.flyray.rbac.utils.Query;
  import me.flyray.rbac.utils.R;
  import me.flyray.rbac.validator.ValidatorUtils;
  
  /**
   * 角色管理
   * 
   * @author chenshun
   * @email sunlightcs@gmail.com
   * @date 2016年11月8日 下午2:18:33
   */
  @RestController
  @RequestMapping("/sys/role")
  public class SysRoleController extends AbstractController {
  	@Autowired
  	private SysRoleService sysRoleService;
  	@Autowired
  	private SysRoleMenuService sysRoleMenuService;
  	
  	/**
  	 * 角色列表
  	 */
  	@RequestMapping("/list")
  	@RequiresPermissions("sys:role:list")
  	public R list(@RequestParam Map<String, Object> params){
  		//如果不是超级管理员，则只查询自己创建的角色列表
  		if(getUserId() != Constant.SUPER_ADMIN){
  			params.put("createUserId", getUserId());
  		}
  		
  		//查询列表数据
  		Query query = new Query(params);
  		List<SysRoleEntity> list = sysRoleService.queryList(query);
  		int total = sysRoleService.queryTotal(query);
  		
  		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
  		
  		return R.ok().put("page", pageUtil);
  	}
  	
  	/**
  	 * 角色列表
  	 */
  	@RequestMapping("/select")
  	@RequiresPermissions("sys:role:select")
  	public R select(@RequestParam("orgId") Long orgId){
  		System.out.println(orgId);
  		List<SysRoleEntity> list = null;
  		
  		if (orgId != null) {
  			Map<String, Object> map = new HashMap<>();
  			
  			//如果不是超级管理员，则只查询自己所拥有的角色列表
  			if(getUserId() != Constant.SUPER_ADMIN){
  				map.put("createUserId", getUserId());
  			}
  			map.put("orgId", orgId);
  			list = sysRoleService.queryList(map);
  		}
  		
  		return R.ok().put("list", list);
  	}
  	
  	/**
  	 * 角色信息
  	 */
  	@RequestMapping("/info/{roleId}")
  	@RequiresPermissions("sys:role:info")
  	public R info(@PathVariable("roleId") Long roleId){
  		SysRoleEntity role = sysRoleService.queryObject(roleId);
  		
  		//查询角色对应的菜单
  		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
  		role.setMenuIdList(menuIdList);
  		
  		return R.ok().put("role", role);
  	}
  	
  	/**
  	 * 保存角色
  	 */
  	@SysLog("保存角色")
  	@RequestMapping("/save")
  	@RequiresPermissions("sys:role:save")
  	public R save(@RequestBody SysRoleEntity role){
  		ValidatorUtils.validateEntity(role);
  		
  		role.setCreateUserId(getUserId());
  		sysRoleService.save(role);
  		
  		return R.ok();
  	}
  	
  	/**
  	 * 修改角色
  	 */
  	@SysLog("修改角色")
  	@RequestMapping("/update")
  	@RequiresPermissions("sys:role:update")
  	public R update(@RequestBody SysRoleEntity role){
  		ValidatorUtils.validateEntity(role);
  		
  		role.setCreateUserId(getUserId());
  		sysRoleService.update(role);
  		
  		return R.ok();
  	}
  	
  	/**
  	 * 删除角色
  	 */
  	@SysLog("删除角色")
  	@RequestMapping("/delete")
  	@RequiresPermissions("sys:role:delete")
  	public R delete(@RequestBody Long[] roleIds){
  		sysRoleService.deleteBatch(roleIds);
  		
  		return R.ok();
  	}
  }
  