修复权限关联外键约束导致删除失败,以及岗位涉及到的bug

This commit is contained in:
xuwenbo
2020-05-20 17:26:14 +08:00
parent 950e944a85
commit 6277b5bdf4
10 changed files with 33 additions and 9 deletions

View File

@ -123,7 +123,7 @@ public class DeptController {
try { try {
deptService.removeByIds(deptIds); deptService.removeByIds(deptIds);
}catch (Throwable e){ }catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选部门中存在岗位或者角色关联,请取消关联后再试"); throw new BadRequestException( "所选部门中存在岗位或者角色关联,请取消关联后再试");
} }
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -98,7 +98,7 @@ public class JobController {
try { try {
jobService.removeByIds(ids); jobService.removeByIds(ids);
}catch (Throwable e){ }catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选岗位存在用户关联,请取消关联后再试"); throw new BadRequestException( "所选岗位存在用户关联,请取消关联后再试");
} }
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -140,9 +140,9 @@ public class RoleController {
getLevels(role.getLevel()); getLevels(role.getLevel());
} }
try { try {
roleService.removeByIds(ids); roleService.delete(ids);
} catch (Throwable e){ } catch (Throwable e){
ThrowableUtil.throwForeignKeyException(e, "所选角色存在用户关联,请取消关联后再试"); throw new BadRequestException("所选角色存在用户关联,请取消关联后再试");
} }
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -166,7 +166,7 @@ public class SysUserController {
throw new BadRequestException("角色权限不足,不能删除:" + userService.findByName(SecurityUtils.getUsername()).getUsername()); throw new BadRequestException("角色权限不足,不能删除:" + userService.findByName(SecurityUtils.getUsername()).getUsername());
} }
} }
userService.removeByIds(ids); userService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -107,4 +107,6 @@ public interface RoleService extends BaseService<Role>{
* @return 权限信息 * @return 权限信息
*/ */
Collection<GrantedAuthority> mapToGrantedAuthorities(UserDto user); Collection<GrantedAuthority> mapToGrantedAuthorities(UserDto user);
void delete(Set<Long> ids);
} }

View File

@ -17,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import java.io.IOException; import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
/** /**
@ -88,4 +89,5 @@ public interface UserService extends BaseService<User>{
*/ */
void update(User resources); void update(User resources);
void delete(Set<Long> ids);
} }

View File

@ -26,10 +26,10 @@ public class JobQueryCriteria {
@Query @Query
private Boolean enabled; private Boolean enabled;
@Query(propName = "id", joinName = "dept") @Query
private Long deptId; private Long deptId;
@Query(propName = "id", joinName = "dept", type = Query.Type.IN) @Query(propName = "deptIds", type = Query.Type.IN)
private Set<Long> deptIds; private Set<Long> deptIds;
@Query(type = Query.Type.BETWEEN) @Query(type = Query.Type.BETWEEN)

View File

@ -26,7 +26,7 @@ public class UserQueryCriteria{
@Query @Query
private Long id; private Long id;
@Query(propName = "id", type = Query.Type.IN, joinName = "dept") @Query(propName = "deptId", type = Query.Type.IN)
private Set<Long> deptIds; private Set<Long> deptIds;
@Query(blurry = "email,username,nickName") @Query(blurry = "email,username,nickName")

View File

@ -271,4 +271,14 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implement
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) {
for (Long id : ids) {
rolesMenusService.lambdaUpdate().eq(RolesMenus::getRoleId, id).remove();
rolesDeptsService.lambdaUpdate().eq(RolesDepts::getRoleId,id).remove();
}
this.removeByIds(ids);
}
} }

View File

@ -20,6 +20,7 @@ import co.yixiang.utils.RedisUtils;
import co.yixiang.utils.SecurityUtils; import co.yixiang.utils.SecurityUtils;
import co.yixiang.utils.StringUtils; import co.yixiang.utils.StringUtils;
import co.yixiang.utils.ValidationUtil; import co.yixiang.utils.ValidationUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import co.yixiang.dozer.service.IGenerator; import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
@ -72,7 +73,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, User> imp
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
private final UsersRolesService usersRolesService; private final UsersRolesService usersRolesService;
public SysUserServiceImpl(IGenerator generator, SysUserMapper userMapper, UserAvatarService userAvatarService, JobService jobService, DeptService deptService, RoleService roleService, RoleMapper roleMapper, RedisUtils redisUtils, UsersRolesService usersRolesService) { public SysUserServiceImpl(IGenerator generator, SysUserMapper userMapper, UserAvatarService userAvatarService, JobService jobService, DeptService deptService, RoleMapper roleMapper, RedisUtils redisUtils, UsersRolesService usersRolesService) {
this.generator = generator; this.generator = generator;
this.userMapper = userMapper; this.userMapper = userMapper;
this.userAvatarService = userAvatarService; this.userAvatarService = userAvatarService;
@ -271,4 +272,13 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, User> imp
this.saveOrUpdate(user); this.saveOrUpdate(user);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) {
for (Long id : ids) {
usersRolesService.lambdaUpdate().eq(UsersRoles ::getUserId,id).remove();
}
this.removeByIds(ids);
}
} }