本文共 7780 字,大约阅读时间需要 25 分钟。
一、参数校验
public class RoleParam { private Integer id; @NotBlank(message = "角色名称不可以为空") @Length(min = 2, max = 20, message = "角色名称长度需要在2-20个字之间") private String name; @Min(value = 1, message = "角色类型不合法") @Max(value = 2, message = "角色类型不合法") private Integer type = 1; @NotNull(message = "角色状态不可以为空") @Min(value = 0, message = "角色状态不合法") @Max(value = 1, message = "角色状态不合法") private Integer status; @Length(min = 0, max = 200, message = "角色备注长度需要在200个字符以内") private String remark;}
2、Service层
@Servicepublic class SysRoleService { @Autowired private SysRoleMapper sysRoleMapper; @Autowired private SysRoleAclMapper sysRoleAclMapper; @Autowired private SysRoleUserMapper sysRoleUserMapper; @Autowired private SysUserMapper sysUserMapper; @Resource private SysLogService sysLogService; public void save(RoleParam param) { BeanValidator.check(param); if (checkExist(param.getName(), param.getId())) { throw new ParamException("角色名称已经存在"); } //类名.builder().属性(data) SysRole role = SysRole.builder().name(param.getName()).status(param.getStatus()).type(param.getType()) .remark(param.getRemark()).build(); role.setOperator(RequestHolder.getCurrentUser().getUsername()); role.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest())); role.setOperateTime(new Date()); sysRoleMapper.insertSelective(role); sysLogService.saveRoleLog(null, role); } private boolean checkExist(String name, Integer id) { return sysRoleMapper.countByName(name, id)>0; } public void update(RoleParam param) { BeanValidator.check(param); if (checkExist(param.getName(), param.getId())) { throw new ParamException("角色名称已经存在"); } SysRole before = sysRoleMapper.selectByPrimaryKey(param.getId()); Preconditions.checkNotNull(before, "待更新的角色不存在"); SysRole after = SysRole.builder().id(param.getId()).name(param.getName()).status(param.getStatus()).type(param.getType()) .remark(param.getRemark()).build(); after.setOperator(RequestHolder.getCurrentUser().getUsername()); after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest())); after.setOperateTime(new Date()); sysRoleMapper.updateByPrimaryKeySelective(after); sysLogService.saveRoleLog(before, after); } public void delete(RoleParam param) { SysRole role = sysRoleMapper.selectByPrimaryKey(param.getId()); Preconditions.checkNotNull(role, "待删除的角色不存在,无法删除"); sysRoleMapper.deleteByPrimaryKey(param.getId()); } public ListgetAll() { return sysRoleMapper.getAll(); } //通过权限ID获得角色列表 public List getRoleListByAclId(int aclId) { List roleIdList = sysRoleAclMapper.getRoleIdListByAclId(aclId); if (CollectionUtils.isEmpty(roleIdList)) { return Lists.newArrayList(); } return sysRoleMapper.getByIdList(roleIdList); } //通过角色列表获得用户列表 public List getUserListByRoleList(List roleList) { if (CollectionUtils.isEmpty(roleList)) { return Lists.newArrayList(); } List roleIdList = roleList.stream().map(role -> role.getId()).collect(Collectors.toList()); List userIdList = sysRoleUserMapper.getUserIdListByRoleIdList(roleIdList); if (CollectionUtils.isEmpty(userIdList)) { return Lists.newArrayList(); } return sysUserMapper.getByIdList(userIdList); } //通过用户ID获得角色列表 public List getRoleListByUserId(int userId) { List roleIdList = sysRoleUserMapper.getRoleIdListByUserId(userId); if (CollectionUtils.isEmpty(roleIdList)) { return Lists.newArrayList(); } return sysRoleMapper.getByIdList(roleIdList); }}
3、Controler
package com.mmall.controller;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import com.mmall.common.JsonData;import com.mmall.model.SysUser;import com.mmall.param.RoleParam;import com.mmall.service.*;import com.mmall.util.StringUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import java.util.List;import java.util.Map;import java.util.Set;import java.util.stream.Collectors;@Controller@RequestMapping("/sys/role")public class SysRoleController { @Autowired private SysRoleService sysRoleService; @Autowired private SysTreeService sysTreeService; @Autowired private SysRoleUserService sysRoleUserService; @Autowired private SysRoleAclService sysRoleAclService; @Autowired private SysUserService sysUserService; @RequestMapping("role.page") public ModelAndView page() { return new ModelAndView("role"); } @RequestMapping("/save.json") @ResponseBody public JsonData saveRole(RoleParam param) { sysRoleService.save(param); return JsonData.success(); } @RequestMapping("/update.json") @ResponseBody public JsonData updateRole(RoleParam param) { sysRoleService.update(param); return JsonData.success(); } @RequestMapping("/delete.json") @ResponseBody public JsonData deleteRole(RoleParam param) { sysRoleService.delete(param); return JsonData.success(); } @RequestMapping("/list.json") @ResponseBody public JsonData list() { return JsonData.success(sysRoleService.getAll()); } @RequestMapping("/roleTree.json") @ResponseBody public JsonData roleTree(@RequestParam("roleId") int roleId) { return JsonData.success(sysTreeService.roleTree(roleId)); } //保存角色与权限点的关系 @RequestMapping("/changeAcls.json") @ResponseBody public JsonData changeAcls( @RequestParam("roleId") int roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) { ListaclIdList = StringUtil.splitToListInt(aclIds); sysRoleAclService.changeRoleAcls(roleId, aclIdList); return JsonData.success(); } //保存角色与用户的关系 @RequestMapping("/changeUsers.json") @ResponseBody public JsonData changeUsers( @RequestParam("roleId") int roleId, @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) { //把UserId转为list List userIdList = StringUtil.splitToListInt(userIds); sysRoleUserService.changeRoleUsers(roleId, userIdList); return JsonData.success(); } //加载角色用户数据 @RequestMapping("/users.json") @ResponseBody public JsonData users(@RequestParam("roleId") int roleId) { //通过roleId获取Userlist List selectedUserList = sysRoleUserService.getListByRoleId(roleId); //获取所有的user,然后减去前面获取的,就剩下没有获取的 List allUserList = sysUserService.getAll(); List unselectedUserList = Lists.newArrayList(); //优化for循环 Set selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet()); //如果是有效的并且在未选中的列表中就添加 for(SysUser sysUser : allUserList) { if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) { unselectedUserList.add(sysUser); } } /*当选择的用户的状态已经无效就不展示,使用流式过滤 selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList()); */ Map > map = Maps.newHashMap(); map.put("selected", selectedUserList); map.put("unselected", unselectedUserList); return JsonData.success(map); }}
转载地址:http://ctphn.baihongyu.com/