[代码完善](v2.5): v2.5 beta 角色、部门、岗位删除优化

更新详情:https://www.ydyno.com/archives/1225.html
pull/372/head
ZhengJie 2020-05-23 17:35:07 +08:00
parent 9de236d692
commit 7a2fbae8b6
12 changed files with 91 additions and 29 deletions

View File

@ -15,8 +15,6 @@
*/
package me.zhengjie.utils;
import me.zhengjie.exception.BadRequestException;
import org.hibernate.exception.ConstraintViolationException;
import java.io.PrintWriter;
import java.io.StringWriter;
@ -36,16 +34,4 @@ public class ThrowableUtil {
return sw.toString();
}
}
public static void throwForeignKeyException(Throwable e, String msg){
Throwable t = e.getCause();
while ((t != null) && !(t instanceof ConstraintViolationException)) {
t = t.getCause();
}
if (t != null) {
throw new BadRequestException(msg);
}
assert false;
throw new BadRequestException("删除失败");
}
}

View File

@ -57,4 +57,13 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
@Modifying
@Query(value = "delete from sys_roles_menus where menu_id = ?1",nativeQuery = true)
void untiedMenu(Long id);
/**
*
* @param deptIds /
* @return /
*/
@Query(value = "select count(1) from sys_role r, sys_roles_depts d where " +
"r.role_id = d.role_id and d.dept_id in ?1",nativeQuery = true)
int countByDepts(Set<Long> deptIds);
}

View File

@ -95,4 +95,28 @@ public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificat
* @param ids /
*/
void deleteAllByIdIn(Set<Long> ids);
/**
*
* @param ids /
* @return /
*/
@Query(value = "SELECT count(1) FROM sys_user u, sys_users_jobs j WHERE u.user_id = j.user_id AND j.job_id IN ?1", nativeQuery = true)
int countByJobs(Set<Long> ids);
/**
*
* @param deptIds /
* @return /
*/
@Query(value = "SELECT count(1) FROM sys_user u WHERE u.dept_id IN ?1", nativeQuery = true)
int countByDepts(Set<Long> deptIds);
/**
*
* @return /
*/
@Query(value = "SELECT count(1) FROM sys_user u, sys_users_roles r WHERE " +
"u.user_id = r.user_id AND r.role_id in ?1", nativeQuery = true)
int countByRoles(Set<Long> ids);
}

View File

@ -113,11 +113,9 @@ public class DeptController {
deptDtos = deptService.getDeleteDepts(deptList, deptDtos);
}
}
try {
deptService.delete(deptDtos);
}catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选部门中存在岗位或者角色关联,请取消关联后再试");
}
// 验证是否被角色或用户关联
deptService.verification(deptDtos);
deptService.delete(deptDtos);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -89,11 +89,9 @@ public class JobController {
@DeleteMapping
@PreAuthorize("@el.check('job:del')")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
try {
jobService.delete(ids);
}catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选岗位存在用户关联,请取消关联后再试");
}
// 验证是否被用户关联
jobService.verification(ids);
jobService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -136,11 +136,9 @@ public class RoleController {
RoleDto role = roleService.findById(id);
getLevels(role.getLevel());
}
try {
roleService.delete(ids);
} catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选角色存在用户关联,请取消关联后再试");
}
// 验证是否被用户关联
roleService.verification(ids);
roleService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}

View File

@ -116,4 +116,10 @@ public interface DeptService {
* @return
*/
List<Long> getDeptChildren(Long deptId, List<Dept> deptList);
/**
*
* @param deptDtos /
*/
void verification(Set<DeptDto> deptDtos);
}

View File

@ -79,4 +79,10 @@ public interface JobService {
* @throws IOException /
*/
void download(List<JobDto> queryAll, HttpServletResponse response) throws IOException;
/**
*
* @param ids /
*/
void verification(Set<Long> ids);
}

View File

@ -120,4 +120,10 @@ public interface RoleService {
* @return
*/
List<GrantedAuthority> mapToGrantedAuthorities(UserDto user);
/**
*
* @param ids /
*/
void verification(Set<Long> ids);
}

View File

@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
@ -57,6 +58,7 @@ public class DeptServiceImpl implements DeptService {
private final DeptMapper deptMapper;
private final UserRepository userRepository;
private final RedisUtils redisUtils;
private final RoleRepository roleRepository;
@Override
public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
@ -240,6 +242,17 @@ public class DeptServiceImpl implements DeptService {
deptRepository.updateSubCntById(count, deptId);
}
@Override
public void verification(Set<DeptDto> deptDtos) {
Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
if(userRepository.countByDepts(deptIds) > 0){
throw new BadRequestException("所选部门存在用户关联,请解除后再试!");
}
if(roleRepository.countByDepts(deptIds) > 0){
throw new BadRequestException("所选部门存在角色关联,请解除后再试!");
}
}
/**
*
* @param id /

View File

@ -16,8 +16,10 @@
package me.zhengjie.modules.system.service.impl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.JobQueryCriteria;
import me.zhengjie.utils.*;
import me.zhengjie.modules.system.repository.JobRepository;
@ -49,6 +51,7 @@ public class JobServiceImpl implements JobService {
private final JobRepository jobRepository;
private final JobMapper jobMapper;
private final RedisUtils redisUtils;
private final UserRepository userRepository;
@Override
public Map<String,Object> queryAll(JobQueryCriteria criteria, Pageable pageable) {
@ -114,4 +117,11 @@ public class JobServiceImpl implements JobService {
}
FileUtil.downloadExcel(list, response);
}
@Override
public void verification(Set<Long> ids) {
if(userRepository.countByJobs(ids) > 0){
throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
}
}
}

View File

@ -16,6 +16,7 @@
package me.zhengjie.modules.system.service.impl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.exception.EntityExistException;
@ -202,4 +203,11 @@ public class RoleServiceImpl implements RoleService {
redisUtils.delByKeys("menu::user:",userIds);
redisUtils.delByKeys("role::auth:",userIds);
}
@Override
public void verification(Set<Long> ids) {
if(userRepository.countByRoles(ids) > 0){
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
}
}
}