【add】用户角色分页查询接口新增

This commit is contained in:
nixj 2024-06-18 08:47:04 +08:00
parent 3a41badcda
commit 5100f2a6d2
7 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.chint.application.dtos.system;
import lombok.Data;
@Data
public class RolePageDto {
private String name;
private String employeeNo;
private String roleName;
private Long roleId;
private Long roleUserId;
}

View File

@ -0,0 +1,10 @@
package com.chint.application.dtos.system;
import com.chint.domain.value_object.BaseQuery;
import lombok.Data;
@Data
public class RolePageQuery extends BaseQuery {
private String name="";
private String employeeNo="";
}

View File

@ -3,6 +3,7 @@ package com.chint.application.system;
import cn.hutool.core.bean.BeanUtil;
import com.chint.application.dtos.response.UserRes;
import com.chint.application.dtos.system.RoleOrgParam;
import com.chint.application.dtos.system.RolePageQuery;
import com.chint.application.dtos.system.SupplierRedirectDTO;
import com.chint.application.dtos.system.UserLoginSupplierParam;
import com.chint.application.services.SupplierLoginService;
@ -15,6 +16,7 @@ import com.chint.domain.repository.RoleRepository;
import com.chint.domain.repository.RoleUserRepository;
import com.chint.domain.repository.UserRepository;
import com.chint.domain.service.SystemDomainService;
import com.chint.domain.service.UserDomainService;
import com.chint.domain.value_object.system.RoleOrganizationVO;
import com.chint.domain.value_object.system.RoleVO;
import com.chint.infrastructure.util.BaseContext;
@ -56,6 +58,8 @@ public class UserController {
@Autowired
private SupplierLoginService supplierLoginService;
@Autowired
private UserDomainService userDomainService;
@ApiOperation("根据Id查询用户信息")
@PostMapping("/query")
@ -157,4 +161,11 @@ public class UserController {
User currentUser = BaseContext.getCurrentUser().loadRoleOrg();
return Result.Success(SUCCESS, BeanUtil.copyProperties(currentUser, UserRes.class));
}
@ApiOperation("用户角色分页查询")
@PostMapping("/role/pageQuery")
public Result<PageResult<?>> rolePageQuery(@RequestBody RolePageQuery rolePageQuery) {
return Result.Success(SUCCESS,userDomainService.userPageQuery(rolePageQuery));
}
}

View File

@ -0,0 +1,67 @@
package com.chint.domain.service;
import com.chint.application.dtos.system.RolePageDto;
import com.chint.application.dtos.system.RolePageQuery;
import com.chint.domain.aggregates.user.Role;
import com.chint.domain.aggregates.user.RoleUser;
import com.chint.domain.aggregates.user.User;
import com.chint.infrastructure.repository.jdbc.JdbcRoleRepository;
import com.chint.infrastructure.repository.jdbc.JdbcRoleUserRepository;
import com.chint.infrastructure.repository.jdbc.JdbcUserRepository;
import com.chint.infrastructure.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class UserDomainService {
@Autowired
private JdbcUserRepository jdbcUserRepository;
@Autowired
private JdbcRoleRepository jdbcRoleRepository;
@Autowired
private JdbcRoleUserRepository jdbcRoleUserRepository;
public PageResult<RolePageDto> userPageQuery(RolePageQuery rolePageQuery) {
Page<User> page=jdbcUserRepository.findAllByNameContainsAndEmployeeNoContains(rolePageQuery.getName(),rolePageQuery.getEmployeeNo(),rolePageQuery.getPageResult());
List<User> users=page.getContent();
if(users.isEmpty()){
return PageResult.totalPageNum(page.getTotalElements(), new ArrayList<>());
}
List<Long> userIds=users.stream().map(User::getUserId).toList();
List<RoleUser> roleUsers=jdbcRoleUserRepository.findAllByUserIdIn(userIds);
if(roleUsers.isEmpty()){
List<RolePageDto> result = users.stream().map(temp -> {
RolePageDto obj = new RolePageDto();
obj.setName(temp.getName());
obj.setEmployeeNo(temp.getEmployeeNo());
return obj;
}).toList();
return PageResult.totalPageNum(page.getTotalElements(), result);
}
List<Long> roleIds=roleUsers.stream().map(RoleUser::getRoleId).toList();
List<Role> roles=jdbcRoleRepository.findAllByIdIn(roleIds);
List<RolePageDto> result = users.stream().map(temp -> {
RolePageDto obj = new RolePageDto();
obj.setName(temp.getName());
obj.setEmployeeNo(temp.getEmployeeNo());
List<RoleUser> roleUser = roleUsers.stream()
.filter(s -> s.getUserId().equals(temp.getUserId()))
.toList();
if(!roleUser.isEmpty()){
obj.setRoleId(roleUser.get(0).getRoleId());
obj.setRoleUserId(roleUser.get(0).getId());
List<Role> roleList=roles.stream().filter(s->s.getId().equals(obj.getRoleId())).toList();
if(!roleList.isEmpty()){
obj.setRoleName(roleList.get(0).getRoleName());
}
}
return obj;
}).toList();
return PageResult.totalPageNum(page.getTotalElements(), result);
}
}

View File

@ -6,7 +6,11 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface JdbcRoleRepository extends CrudRepository<Role, Long> {
Page<Role> findAll(Pageable pageable);
List<Role> findAllByIdIn(List<Long> ids);
}

View File

@ -13,5 +13,7 @@ public interface JdbcRoleUserRepository extends CrudRepository<RoleUser, Long> {
List<RoleUser> findByRoleId(Long roleId);
List<RoleUser> findAllByUserIdIn(List<Long> userIds);
}

View File

@ -1,6 +1,8 @@
package com.chint.infrastructure.repository.jdbc;
import com.chint.domain.aggregates.user.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@ -15,4 +17,6 @@ public interface JdbcUserRepository extends CrudRepository<User, Long> {
List<User> findByStatusIsNull();
List<User> findAllByNameContains(String name);
Page<User> findAllByNameContainsAndEmployeeNoContains(String name, String employeeNo, Pageable pageable);
}