|
|
|
|
@ -1,12 +1,15 @@
|
|
|
|
|
package com.biutag.supervision.service; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil; |
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.biutag.supervision.mapper.BaseUserMapper; |
|
|
|
|
import com.biutag.supervision.pojo.dto.UserDto; |
|
|
|
|
import com.biutag.supervision.pojo.entity.BaseAccount; |
|
|
|
|
import com.biutag.supervision.pojo.entity.BaseRole; |
|
|
|
|
import com.biutag.supervision.pojo.entity.BaseRoleUser; |
|
|
|
|
import com.biutag.supervision.pojo.entity.BaseUser; |
|
|
|
|
@ -14,7 +17,10 @@ import com.biutag.supervision.pojo.model.UserModel;
|
|
|
|
|
import com.biutag.supervision.pojo.param.UserQueryParam; |
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCrypt; |
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
@ -30,6 +36,8 @@ public class BaseUserService extends ServiceImpl<BaseUserMapper, BaseUser> {
|
|
|
|
|
|
|
|
|
|
private final BaseRoleService roleService; |
|
|
|
|
|
|
|
|
|
private final BaseAccountService accountService; |
|
|
|
|
|
|
|
|
|
public Page<UserModel> page(UserQueryParam queryParam) { |
|
|
|
|
LambdaQueryWrapper<BaseUser> queryWrapper = new LambdaQueryWrapper<BaseUser>() |
|
|
|
|
.like(StrUtil.isNotBlank(queryParam.getUserName()), BaseUser::getUserName, queryParam.getUserName()) |
|
|
|
|
@ -70,12 +78,44 @@ public class BaseUserService extends ServiceImpl<BaseUserMapper, BaseUser> {
|
|
|
|
|
return getOne(new LambdaQueryWrapper<BaseUser>().eq(BaseUser::getUserName, userName)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
@DS("slave") |
|
|
|
|
public boolean update(UserDto userDto) { |
|
|
|
|
public boolean save(UserDto userDto) { |
|
|
|
|
if (exists(new LambdaQueryWrapper<BaseUser>().eq(BaseUser::getUserName, userDto.getUserName()))) { |
|
|
|
|
throw new RuntimeException(String.format("该用户[登录名:%s]已存在", userDto.getUserName())); |
|
|
|
|
} |
|
|
|
|
BaseUser baseUser = new BaseUser(); |
|
|
|
|
BeanUtils.copyProperties(userDto, baseUser); |
|
|
|
|
baseUser.setUserId(IdUtil.getSnowflakeNextIdStr()); |
|
|
|
|
baseUser.setUpdateTime(LocalDateTime.now()); |
|
|
|
|
roleUserService.remove(userDto.getUserId()); |
|
|
|
|
baseUser.setCreateTime(LocalDateTime.now()); |
|
|
|
|
save(baseUser); |
|
|
|
|
if (!userDto.getRoleIds().isEmpty()) { |
|
|
|
|
roleUserService.saveBatch(userDto.getRoleIds().stream().map(roleId -> { |
|
|
|
|
BaseRoleUser baseRoleUser = new BaseRoleUser(); |
|
|
|
|
baseRoleUser.setUserId(baseUser.getUserId()); |
|
|
|
|
baseRoleUser.setRoleId(roleId); |
|
|
|
|
baseRoleUser.setCreateTime(LocalDateTime.now()); |
|
|
|
|
baseRoleUser.setUpdateTime(LocalDateTime.now()); |
|
|
|
|
return baseRoleUser; |
|
|
|
|
}).toList()); |
|
|
|
|
} |
|
|
|
|
BaseAccount account = new BaseAccount(); |
|
|
|
|
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); |
|
|
|
|
String encode = encoder.encode(userDto.getPassword()); |
|
|
|
|
account.setPassword(encode); |
|
|
|
|
account.setAccount(userDto.getUserName()); |
|
|
|
|
account.setAccountId(IdUtil.getSnowflakeNextIdStr()); |
|
|
|
|
account.setUserId(baseUser.getUserId()); |
|
|
|
|
account.setAccountType("username"); |
|
|
|
|
account.setDomain("@admin.com"); |
|
|
|
|
account.setCreateTime(LocalDateTime.now()); |
|
|
|
|
account.setUpdateTime(LocalDateTime.now()); |
|
|
|
|
return accountService.save(account); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@DS("slave") |
|
|
|
|
public boolean update(UserDto userDto) { |
|
|
|
|
if (!userDto.getRoleIds().isEmpty()) { |
|
|
|
|
roleUserService.saveBatch(userDto.getRoleIds().stream().map(roleId -> { |
|
|
|
|
BaseRoleUser baseRoleUser = new BaseRoleUser(); |
|
|
|
|
@ -86,7 +126,15 @@ public class BaseUserService extends ServiceImpl<BaseUserMapper, BaseUser> {
|
|
|
|
|
return baseRoleUser; |
|
|
|
|
}).toList()); |
|
|
|
|
} |
|
|
|
|
return updateById(baseUser); |
|
|
|
|
LambdaUpdateWrapper<BaseUser> updateWrapper = new LambdaUpdateWrapper<BaseUser>() |
|
|
|
|
.eq(BaseUser::getUserId, userDto.getUserId()) |
|
|
|
|
.set(BaseUser::getNickName, userDto.getNickName()) |
|
|
|
|
.set(BaseUser::getEmail, userDto.getEmail()) |
|
|
|
|
.set(BaseUser::getMobile, userDto.getMobile()) |
|
|
|
|
.set(BaseUser::getUserName, userDto.getUserType()) |
|
|
|
|
.set(BaseUser::getUserDesc, userDto.getUserDesc()) |
|
|
|
|
.set(BaseUser::getUpdateTime, LocalDateTime.now()); |
|
|
|
|
return update(updateWrapper); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|