Search in sources :

Example 1 with EntityExistException

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);
    }
}
Also used : Wraps(com.albedo.java.plugins.database.mybatis.conditions.Wraps) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) UserInfo(com.albedo.java.modules.sys.domain.vo.UserInfo) EntityExistException(com.albedo.java.common.core.exception.EntityExistException) UserEmailDto(com.albedo.java.modules.sys.domain.dto.UserEmailDto) UserRoleDo(com.albedo.java.modules.sys.domain.UserRoleDo) RedisUtil(com.albedo.java.common.util.RedisUtil) SysCacheUtil(com.albedo.java.modules.sys.util.SysCacheUtil) Valid(javax.validation.Valid) UserDo(com.albedo.java.modules.sys.domain.UserDo) CacheKey(com.albedo.java.common.core.cache.model.CacheKey) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) Set(java.util.Set) CacheKeyBuilder(com.albedo.java.common.core.cache.model.CacheKeyBuilder) QueryWrapperUtil(com.albedo.java.plugins.database.mybatis.util.QueryWrapperUtil) Collectors(java.util.stream.Collectors) SecurityUtil(com.albedo.java.common.security.util.SecurityUtil) MenuVo(com.albedo.java.modules.sys.domain.vo.MenuVo) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AbstractDataCacheServiceImpl(com.albedo.java.plugins.database.mybatis.service.impl.AbstractDataCacheServiceImpl) ArrayUtil(cn.hutool.core.util.ArrayUtil) UserExcelVo(com.albedo.java.modules.sys.domain.vo.UserExcelVo) IPage(com.baomidou.mybatisplus.core.metadata.IPage) BeanUtils(org.springframework.beans.BeanUtils) UserRepository(com.albedo.java.modules.sys.repository.UserRepository) DeptDo(com.albedo.java.modules.sys.domain.DeptDo) PageModel(com.albedo.java.common.core.vo.PageModel) RoleDo(com.albedo.java.modules.sys.domain.RoleDo) LocalDateTime(java.time.LocalDateTime) UserQueryCriteria(com.albedo.java.modules.sys.domain.dto.UserQueryCriteria) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) UserDto(com.albedo.java.modules.sys.domain.dto.UserDto) PasswordChangeVo(com.albedo.java.modules.sys.domain.vo.account.PasswordChangeVo) com.albedo.java.common.core.util(com.albedo.java.common.core.util) SecurityConstants(com.albedo.java.common.core.constant.SecurityConstants) CommonConstants(com.albedo.java.common.core.constant.CommonConstants) UserVo(com.albedo.java.modules.sys.domain.vo.UserVo) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) com.albedo.java.modules.sys.service(com.albedo.java.modules.sys.service) BizException(com.albedo.java.common.core.exception.BizException) DataScope(com.albedo.java.plugins.database.mybatis.datascope.DataScope) AllArgsConstructor(lombok.AllArgsConstructor) UserCacheKeyBuilder(com.albedo.java.modules.sys.cache.UserCacheKeyBuilder) PasswordRestVo(com.albedo.java.modules.sys.domain.vo.account.PasswordRestVo) Transactional(org.springframework.transaction.annotation.Transactional) UserRoleDo(com.albedo.java.modules.sys.domain.UserRoleDo) EntityExistException(com.albedo.java.common.core.exception.EntityExistException) UserDo(com.albedo.java.modules.sys.domain.UserDo) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ArrayUtil (cn.hutool.core.util.ArrayUtil)1 CacheKey (com.albedo.java.common.core.cache.model.CacheKey)1 CacheKeyBuilder (com.albedo.java.common.core.cache.model.CacheKeyBuilder)1 CommonConstants (com.albedo.java.common.core.constant.CommonConstants)1 SecurityConstants (com.albedo.java.common.core.constant.SecurityConstants)1 BizException (com.albedo.java.common.core.exception.BizException)1 EntityExistException (com.albedo.java.common.core.exception.EntityExistException)1 com.albedo.java.common.core.util (com.albedo.java.common.core.util)1 PageModel (com.albedo.java.common.core.vo.PageModel)1 SecurityUtil (com.albedo.java.common.security.util.SecurityUtil)1 RedisUtil (com.albedo.java.common.util.RedisUtil)1 UserCacheKeyBuilder (com.albedo.java.modules.sys.cache.UserCacheKeyBuilder)1 DeptDo (com.albedo.java.modules.sys.domain.DeptDo)1 RoleDo (com.albedo.java.modules.sys.domain.RoleDo)1 UserDo (com.albedo.java.modules.sys.domain.UserDo)1 UserRoleDo (com.albedo.java.modules.sys.domain.UserRoleDo)1 UserDto (com.albedo.java.modules.sys.domain.dto.UserDto)1 UserEmailDto (com.albedo.java.modules.sys.domain.dto.UserEmailDto)1 UserQueryCriteria (com.albedo.java.modules.sys.domain.dto.UserQueryCriteria)1 MenuVo (com.albedo.java.modules.sys.domain.vo.MenuVo)1