添加接口
This commit is contained in:
parent
6aaab95126
commit
78f4c659ad
@ -2,7 +2,9 @@ package com.knowledge.base.application.service;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.knowledge.base.application.exceptions.AppException;
|
||||
import com.knowledge.base.domain.user.model.RoleDO;
|
||||
import com.knowledge.base.domain.user.model.UserDO;
|
||||
import com.knowledge.base.domain.user.model.UserRoleDO;
|
||||
import com.knowledge.base.domain.user.service.iface.UserDomainService;
|
||||
import com.knowledge.base.infrastructure.converter.*;
|
||||
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
|
||||
@ -15,6 +17,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -48,16 +51,13 @@ public class UserAppServiceImpl implements UserAppService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateUser(Long id, UserDTO userDTO) {
|
||||
public boolean updateUser(Long userId, UserDTO userDTO) {
|
||||
UserDO userDO = UserDtoConverter.toDO(userDTO);
|
||||
try {
|
||||
userDomainService.updateUser(userDO);
|
||||
userDomainService.findRolesByUserId(id).forEach(role -> {
|
||||
userDomainService.removeUserRole(id, role.getRoleId());
|
||||
});
|
||||
userDTO.getUserRoles().forEach(roleId -> {
|
||||
userDomainService.addUserRole(id, roleId.longValue());
|
||||
});
|
||||
userDomainService.removeUserRoles(userId);
|
||||
userDomainService.batchAddUserRoles(userId,
|
||||
userDTO.getUserRoles().stream().map(RoleDTO::getId).collect(Collectors.toList()));
|
||||
} catch (Exception e) {
|
||||
throw new AppException("更新用户信息失败", e);
|
||||
}
|
||||
@ -76,8 +76,21 @@ public class UserAppServiceImpl implements UserAppService {
|
||||
|
||||
@Override
|
||||
public List<UserDTO> findAll() {
|
||||
return Optional.ofNullable(userDomainService.findAllUsers()).orElse(Lists.newArrayList()).stream()
|
||||
.map(e -> UserDtoConverter.toDTO(e))
|
||||
// userIds
|
||||
List<UserDO> allUsers = userDomainService.findAllUsers();
|
||||
List<Long> allUserIds = allUsers.stream().map(e -> e.getId()).collect(Collectors.toList());
|
||||
// userId ---> roleId
|
||||
Map<Long, List<UserRoleDO>> userRolesRelationMap = userDomainService.batchFindRolesByUserIds(allUserIds);
|
||||
// roleId ---> role详情
|
||||
Map<Long, RoleDO> roleMap = userDomainService.findAllRoles().stream().collect(Collectors.toMap(RoleDO::getId, e -> e));
|
||||
// userId ---> role详情
|
||||
Map<Long, List<RoleDO>> userRolesMap = allUserIds.stream().collect(Collectors.toMap(userId -> userId, userId -> {
|
||||
List<Long> roleIds = Optional.ofNullable(userRolesRelationMap.get(userId)).orElse(Lists.newArrayList()).stream()
|
||||
.map(UserRoleDO::getId).collect(Collectors.toList());
|
||||
return roleIds.stream().map(roleId -> roleMap.get(roleId)).collect(Collectors.toList());
|
||||
}));
|
||||
return Optional.ofNullable(allUsers).orElse(Lists.newArrayList()).stream()
|
||||
.map(e -> UserDtoConverter.toDTO(e, userRolesMap.get(e.getId())))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ package com.knowledge.base.domain.user.repository.iface;
|
||||
|
||||
import com.knowledge.base.domain.user.repository.po.UserRole;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRoleRepository {
|
||||
@ -12,4 +13,8 @@ public interface UserRoleRepository {
|
||||
UserRole findById(Long id);
|
||||
List<UserRole> findByUserId(Long userId);
|
||||
List<UserRole> findAll();
|
||||
|
||||
List<UserRole> findByUserIdIn(List<Long> userIds);
|
||||
|
||||
boolean removeUserRoles(Long userId);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.knowledge.base.domain.user.service.iface;
|
||||
import com.knowledge.base.domain.user.model.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserDomainService {
|
||||
@ -30,6 +31,9 @@ public interface UserDomainService {
|
||||
|
||||
// 用户-角色
|
||||
List<UserRoleDO> findRolesByUserId(Long userId);
|
||||
Map<Long, List<UserRoleDO>> batchFindRolesByUserIds(List<Long> userIds);
|
||||
boolean batchAddUserRoles(Long userId, List<Long> roleIds);
|
||||
boolean removeUserRoles(Long userId);
|
||||
boolean addUserRole(Long userId, Long roleId);
|
||||
boolean removeUserRole(Long userId, Long roleId);
|
||||
|
||||
|
||||
@ -4,9 +4,16 @@ import com.knowledge.base.domain.user.model.UserRoleDO;
|
||||
import com.knowledge.base.domain.user.repository.po.UserRole;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface UserRoleDomainSupport {
|
||||
List<UserRoleDO> findByUserId(Long userId);
|
||||
boolean addUserRole(Long userId, Long roleId);
|
||||
boolean removeUserRole(Long userId, Long roleId);
|
||||
|
||||
Map<Long, List<UserRoleDO>> batchFindRolesByUserIds(List<Long> userIds);
|
||||
|
||||
boolean batchAddUserRoles(Long userId, List<Long> roleIds);
|
||||
|
||||
boolean removeUserRoles(Long userId);
|
||||
}
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
package com.knowledge.base.domain.user.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import com.knowledge.base.domain.user.model.*;
|
||||
import com.knowledge.base.domain.user.service.iface.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@ -104,6 +107,25 @@ public class UserDomainServiceImpl implements UserDomainService {
|
||||
public List<UserRoleDO> findRolesByUserId(Long userId) {
|
||||
return userRoleDomainSupport.findByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, List<UserRoleDO>> batchFindRolesByUserIds(List<Long> userIds) {
|
||||
if(CollectionUtil.isEmpty(userIds)) {
|
||||
return MapUtil.newHashMap();
|
||||
}
|
||||
return userRoleDomainSupport.batchFindRolesByUserIds(userIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean batchAddUserRoles(Long userId, List<Long> roleIds) {
|
||||
return userRoleDomainSupport.batchAddUserRoles(userId, roleIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeUserRoles(Long userId) {
|
||||
return userRoleDomainSupport.removeUserRoles(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addUserRole(Long userId, Long roleId) {
|
||||
return userRoleDomainSupport.addUserRole(userId, roleId);
|
||||
|
||||
@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -38,4 +39,26 @@ public class UserRoleDomainSupportImpl implements UserRoleDomainSupport {
|
||||
public boolean removeUserRole(Long userId, Long roleId) {
|
||||
return userRoleRepository.deleteByUserIdAndRoleId(userId, roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, List<UserRoleDO>> batchFindRolesByUserIds(List<Long> userIds) {
|
||||
return userRoleRepository.findByUserIdIn(userIds).stream()
|
||||
.map(e -> UserRoleDomainConverter.toDO(e))
|
||||
.collect(Collectors.groupingBy(UserRoleDO::getUserId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean batchAddUserRoles(Long userId, List<Long> roleIds) {
|
||||
boolean res = true;
|
||||
for (Long roleId : roleIds) {
|
||||
boolean b = addUserRole(userId, roleId);
|
||||
res = res && b;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeUserRoles(Long userId) {
|
||||
return userRoleRepository.removeUserRoles(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ public class DynamicConfig {
|
||||
@Value("${cookie.domain.name:wisdompulse.cn}")
|
||||
private String cookieDomainName;
|
||||
|
||||
@Value("${import.schedule.cron:0 0 * * * *}")
|
||||
@Value("${import.schedule.cron:* 0/30 * * * ?}")
|
||||
private String importScheduleCron;
|
||||
|
||||
// token失效时间,默认24小时
|
||||
|
||||
@ -1,11 +1,17 @@
|
||||
package com.knowledge.base.infrastructure.converter;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.knowledge.base.domain.user.model.RoleDO;
|
||||
import com.knowledge.base.domain.user.model.UserDO;
|
||||
import com.knowledge.base.infrastructure.config.ConstantConfig;
|
||||
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
|
||||
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
@ -26,5 +32,15 @@ public class UserDtoConverter {
|
||||
dto.setAddTime(doObj.getAddTime() == null ? null : doObj.getAddTime().atZone(ZoneId.of("Asia/Shanghai")).format(ConstantConfig.DTF));
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static UserDTO toDTO(UserDO doObj, List<RoleDO> roleDOs) {
|
||||
if (doObj == null) return null;
|
||||
UserDTO dto = new UserDTO();
|
||||
BeanUtils.copyProperties(doObj, dto);
|
||||
dto.setAddTime(doObj.getAddTime() == null ? null : doObj.getAddTime().atZone(ZoneId.of("Asia/Shanghai")).format(ConstantConfig.DTF));
|
||||
List<RoleDTO> roleDTOS = Optional.ofNullable(roleDOs).orElse(Lists.newArrayList()).stream().map(RoleDtoConverter::toDTO).collect(Collectors.toList());
|
||||
dto.setUserRoles(roleDTOS);
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -55,12 +55,12 @@ public class UserWriteController {
|
||||
return ResponseEntity.ok(userAppService.deleteUser(id));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<?> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
|
||||
if(Objects.isNull(id) || Objects.isNull(userDTO)) {
|
||||
@PutMapping("/{userId}")
|
||||
public ResponseEntity<?> updateUser(@PathVariable Long userId, @RequestBody UserDTO userDTO) {
|
||||
if(Objects.isNull(userId) || Objects.isNull(userDTO)) {
|
||||
return ResponseEntity.badRequest().body(Map.of("msg", "参数错误:id或用户信息为空!"));
|
||||
}
|
||||
return ResponseEntity.ok(userAppService.updateUser(id, userDTO));
|
||||
return ResponseEntity.ok(userAppService.updateUser(userId, userDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
package com.knowledge.base.infrastructure.north.dto.user;
|
||||
|
||||
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Luke.ye
|
||||
@ -13,6 +14,6 @@ import java.util.Set;
|
||||
public class UserDTO {
|
||||
private Long id;
|
||||
private String username;
|
||||
private List<RoleDTO> userRoles;
|
||||
private String addTime;
|
||||
private Set<Integer> userRoles;
|
||||
}
|
||||
|
||||
@ -51,4 +51,16 @@ public class UserRoleRepositoryImpl implements UserRoleRepository {
|
||||
public List<UserRole> findAll() {
|
||||
return userRoleMapper.selectList(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserRole> findByUserIdIn(List<Long> userIds) {
|
||||
return userRoleMapper.selectList(new QueryWrapper<UserRole>().in("user_id", userIds));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeUserRoles(Long userId) {
|
||||
return userRoleMapper.delete(
|
||||
new QueryWrapper<UserRole>().eq("user_id", userId)
|
||||
) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user