use of com.albedo.java.common.core.exception.EntityExistException in project albedo by somowhere.
the class UserServiceImpl method saveOrUpdate.
/**
* 保存用户信息
*
* @param userDto DTO 对象
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void saveOrUpdate(UserDto userDto) {
boolean add = ObjectUtil.isEmpty(userDto.getId());
if (add) {
ArgumentAssert.notEmpty(userDto.getPassword(), "密码不能为空");
}
// username before comparing with database
if (exitUserByUserName(userDto)) {
throw new EntityExistException(UserDto.class, "username", userDto.getUsername());
}
// email before comparing with database
if (StringUtil.isNotEmpty(userDto.getEmail()) && exitUserByEmail(userDto)) {
throw new EntityExistException(UserDto.class, "email", userDto.getEmail());
}
// phone before comparing with database
if (StringUtil.isNotEmpty(userDto.getPhone()) && exitUserByPhone(userDto)) {
throw new EntityExistException(UserDto.class, "phone", userDto.getPhone());
}
UserDo userDo = add ? new UserDo() : repository.selectById(userDto.getId());
BeanUtil.copyProperties(userDto, userDo, true);
if (StringUtil.isNotEmpty(userDto.getPassword())) {
userDo.setPassword(passwordEncoder.encode(userDto.getPassword()));
}
super.saveOrUpdate(userDo);
userDto.setId(userDo.getId());
if (add || CollUtil.isNotEmpty(userDto.getRoleIdList())) {
ArgumentAssert.notEmpty(userDto.getRoleIdList(), "用户角色不能为空");
if (!add) {
SysCacheUtil.delUserCaches(userDo.getId(), userDo.getUsername());
}
List<UserRoleDo> userRoleDoList = userDto.getRoleIdList().stream().map(roleId -> UserRoleDo.builder().userId(userDo.getId()).roleId(roleId).build()).collect(Collectors.toList());
userRoleService.removeRoleByUserId(userDo.getId());
userRoleService.saveBatch(userRoleDoList);
}
}
Aggregations