This commit is contained in:
xiangwb
2017-11-15 14:06:09 +08:00
parent cbf6486631
commit 2e236d25b4
8 changed files with 41 additions and 103 deletions
@@ -22,4 +22,5 @@ public interface SysAuthorityRepository extends JpaRepository<SysAuthority, Stri
List<SysAuthority> getByEnableAndIdIn(String enable, List<String> ids);
List<SysAuthority> getByIdIn(List<String> ids);
}
@@ -16,5 +16,7 @@ public interface SysRoleRepository extends JpaRepository<SysRole, String> {
SysRole getByCode(String code);
List<SysRole> getByEnableAndIdIn(String enable,List<String> ids);
List<SysRole> getByEnableAndIdIn(String enable, List<String> ids);
List<SysRole> getByIdIn(List<String> ids);
}
@@ -1,72 +0,0 @@
package com.xwbing.service;
import com.xwbing.util.RestMessage;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Objects;
/**
* 说明:
* 项目名称: boot-module-demo
* 创建时间: 2017/5/5 16:44
* 作者: xiangwb
*/
public class BaseService<T> {
private JpaRepository<T, String> jpaRepository;
public BaseService(JpaRepository<T, String> jpaRepository) {
this.jpaRepository = jpaRepository;
}
/**
* 增
*
* @param t
* @return
*/
public RestMessage saveOrUpdate(T t) {
RestMessage result = new RestMessage();
t = jpaRepository.save(t);
if (Objects.isNull(t)) {
result.setMessage("保存数据失败");
} else {
result.setSuccess(true);
result.setMessage("保存数据成功");
}
return result;
}
/**
* 删
*
* @param id
* @return
*/
public RestMessage removeById(String id) {
RestMessage result = new RestMessage();
jpaRepository.delete(id);
result.setSuccess(true);
result.setMessage("删除数据成功");
return result;
}
/**
* 单个查找
*
* @param id
* @return
*/
public T findById(String id) {
return jpaRepository.findOne(id);
}
/**
* 列表查找
*
* @return
*/
public List<T> findList() {
return jpaRepository.findAll();
}
}
@@ -39,7 +39,7 @@ public class SysAuthorityService {
*/
public RestMessage save(SysAuthority sysAuthority) {
RestMessage result = new RestMessage();
boolean b = uniqueCode(sysAuthority.getCode(), "");
boolean b = uniqueCode(sysAuthority.getCode(), null);
if (!b)
throw new BusinessException("该编码已存在");
String id = PassWordUtil.createId();
@@ -123,7 +123,10 @@ public class SysAuthorityService {
* @return
*/
public List<SysAuthority> listByEnable(String enable) {
return sysAuthorityRepository.getByEnable(enable);
if (StringUtils.isNotEmpty(enable))
return sysAuthorityRepository.getByEnable(enable);
else
return sysAuthorityRepository.findAll();
}
/**
@@ -156,7 +159,10 @@ public class SysAuthorityService {
return list;
List<String> authorityIds = roleAuthorities.stream().map(SysRoleAuthority::getAuthorityId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(authorityIds))
list = sysAuthorityRepository.getByEnableAndIdIn(enable, authorityIds);
if (StringUtils.isNotEmpty(enable))
list = sysAuthorityRepository.getByEnableAndIdIn(enable, authorityIds);
else
list = sysAuthorityRepository.getByIdIn(authorityIds);
return list;
}
@@ -9,8 +9,6 @@ import com.xwbing.util.PassWordUtil;
import com.xwbing.util.RestMessage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -33,7 +31,6 @@ public class SysRoleService {
private SysUserRoleService sysUserRoleService;
@Resource
private SysRoleAuthorityService sysRoleAuthorityService;
private final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
/**
* 保存角色
@@ -43,7 +40,7 @@ public class SysRoleService {
*/
public RestMessage save(SysRole sysRole) {
RestMessage result = new RestMessage();
boolean b = uniqueCode(sysRole.getCode(), "");
boolean b = uniqueCode(sysRole.getCode(), null);
if (!b)
throw new BusinessException("该编码已存在");
String id = PassWordUtil.createId();
@@ -92,9 +89,8 @@ public class SysRoleService {
RestMessage result = new RestMessage();
String id = sysRole.getId();
SysRole old = getById(id);
if (old == null) {
if (old == null)
throw new BusinessException("该角色不存在");
}
boolean b = uniqueCode(sysRole.getCode(), id);
if (!b)
throw new BusinessException("该编码已存在");
@@ -131,7 +127,10 @@ public class SysRoleService {
* @return
*/
public List<SysRole> listAllByEnable(String enable) {
return sysRoleRepository.getByEnable(enable);
if (StringUtils.isNotEmpty(enable))
return sysRoleRepository.getByEnable(enable);
else
return sysRoleRepository.findAll();
}
/**
@@ -148,7 +147,10 @@ public class SysRoleService {
return list;
List<String> roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(roleIds))
list = sysRoleRepository.getByEnableAndIdIn(enable, roleIds);
if (StringUtils.isNotEmpty(enable))
list = sysRoleRepository.getByEnableAndIdIn(enable, roleIds);
else
list = sysRoleRepository.getByIdIn(roleIds);
return list;
}
@@ -70,7 +70,7 @@ public class SysAuthorityControl {
}
@LogInfo("根据父节点查询子节点(非递归)")
@PostMapping("listByParentId")
@GetMapping("listByParentId")
public JSONObject listByParentId(String parentId) {
if (StringUtils.isEmpty(parentId))
parentId = CommonConstant.ROOT;
@@ -81,16 +81,16 @@ public class SysAuthorityControl {
}
@LogInfo("根据是否启用查询所有权限")
@PostMapping("listAuthorityByEnable")
public JSONObject listAuthorityByEnable(@RequestParam String enable) {
if (StringUtils.isEmpty(enable))
return JSONObjResult.toJSONObj("是否启用不能为空");
@GetMapping("listAuthorityByEnable")
public JSONObject listAuthorityByEnable(String enable) {
// if (StringUtils.isEmpty(enable))
// return JSONObjResult.toJSONObj("是否启用不能为空");
List<SysAuthority> authoritys = sysAuthorityService.listByEnable(enable);
return JSONObjResult.toJSONObj(authoritys, true, "");
}
@LogInfo("递归查询所有权限")
@PostMapping("queryTree")
@GetMapping("queryTree")
public JSONObject queryTree(String enable) {
List<SysAuthVo> authoritys = sysAuthorityService.queryAllChildren(CommonConstant.ROOT, enable);
return JSONObjResult.toJSONObj(authoritys, true, "");
@@ -84,8 +84,8 @@ public class SysRoleControl {
}
@LogInfo("保存角色权限")
@PostMapping("saveAuthoritys")
public JSONObject saveAuthoritys(@RequestParam String authorityIds, @RequestParam String roleId) {
@PostMapping("saveAuthority")
public JSONObject saveAuthority(@RequestParam String authorityIds, @RequestParam String roleId) {
if (StringUtils.isEmpty(authorityIds))
return JSONObjResult.toJSONObj("权限主键集合不能为空");
if (StringUtils.isEmpty(roleId))
@@ -107,9 +107,9 @@ public class SysRoleControl {
@LogInfo("根据是否启用查询所有角色")
@GetMapping("listAllByEnable")
public JSONObject listAllByEnable(@RequestParam String enable) {
if (StringUtils.isEmpty(enable))
return JSONObjResult.toJSONObj("是否启用不能为空");
public JSONObject listAllByEnable(String enable) {
// if (StringUtils.isEmpty(enable))
// return JSONObjResult.toJSONObj("是否启用不能为空");
List<SysRole> sysRoles = sysRoleService.listAllByEnable(enable);
return JSONObjResult.toJSONObj(sysRoles, true, "");
}
@@ -183,23 +183,22 @@ public class SysUserControl {
}
@LogInfo("根据用户主键查找所拥有的角色")
@PostMapping("listRoles")
public JSONObject listRoles(@RequestParam String userId, @RequestParam String enable) {
@PostMapping("listRoleByUserId")
public JSONObject listRoleByUserId(@RequestParam String userId,String enable) {
if (StringUtils.isEmpty(userId))
return JSONObjResult.toJSONObj("用户主键不能为空");
if (StringUtils.isEmpty(enable))
return JSONObjResult.toJSONObj("是否启用不能为空");
// if (StringUtils.isEmpty(enable))
// return JSONObjResult.toJSONObj("是否启用不能为空");
List<SysRole> list = sysRoleService.listByUserIdEnable(userId, enable);
return JSONObjResult.toJSONObj(list, true, "");
}
@LogInfo("根据用户主键查找所拥有的权限")
@PostMapping("queryAuthority")
public JSONObject queryAuthority(@RequestParam String userId, @RequestParam String enable) {
@PostMapping("listAuthorityByUserId")
public JSONObject listAuthorityByUserId(@RequestParam String userId,String enable) {
if (StringUtils.isEmpty(userId))
return JSONObjResult.toJSONObj("用户主键不能为空");
List<SysAuthority> list = sysUserService.queryAuthority(userId,
enable);
List<SysAuthority> list = sysUserService.queryAuthority(userId, enable);
return JSONObjResult.toJSONObj(list, true, "");
}
}