Spaces:
Configuration error
Configuration error
Add RoleCountProjection, fix performance of getRoleCounts()
Browse files
src/main/java/com/attendenceSystem/module/user/provider/UserStatisticProvider.java
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
package com.attendenceSystem.module.user.provider;
|
| 2 |
|
| 3 |
-
import java.util.Arrays;
|
| 4 |
import java.util.Map;
|
| 5 |
import java.util.stream.Collectors;
|
| 6 |
|
|
@@ -30,8 +29,13 @@ public class UserStatisticProvider {
|
|
| 30 |
}
|
| 31 |
|
| 32 |
public Map<String, Long> getRoleCounts() {
|
| 33 |
-
return
|
| 34 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
-
|
| 37 |
}
|
|
|
|
| 1 |
package com.attendenceSystem.module.user.provider;
|
| 2 |
|
|
|
|
| 3 |
import java.util.Map;
|
| 4 |
import java.util.stream.Collectors;
|
| 5 |
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
public Map<String, Long> getRoleCounts() {
|
| 32 |
+
return userRepository.countUsersGroupByRole()
|
| 33 |
+
.stream()
|
| 34 |
+
.filter(p -> p != null && p.getRole() != null)
|
| 35 |
+
.collect(
|
| 36 |
+
Collectors.toMap(
|
| 37 |
+
p -> p.getRole().name(),
|
| 38 |
+
p -> p.getCount()));
|
| 39 |
}
|
| 40 |
+
|
| 41 |
}
|
src/main/java/com/attendenceSystem/module/user/repository/UserRepository.java
CHANGED
|
@@ -16,9 +16,11 @@ import com.attendenceSystem.module.user.entity.enums.Department;
|
|
| 16 |
import com.attendenceSystem.module.user.entity.enums.Specialization;
|
| 17 |
import com.attendenceSystem.module.user.entity.enums.Role;
|
| 18 |
import com.attendenceSystem.module.user.entity.enums.Status;
|
|
|
|
| 19 |
|
| 20 |
@Repository
|
| 21 |
public interface UserRepository extends JpaRepository<User, Long> {
|
|
|
|
| 22 |
Optional<User> findById(Long id);
|
| 23 |
|
| 24 |
Optional<User> findUserByUsername(String username);
|
|
@@ -60,4 +62,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
|
| 60 |
List<UserSimpleResponse> findFullNameByDepartmentAndRoleNot(
|
| 61 |
@Param("department") Department department,
|
| 62 |
@Param("role") Role role);
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
|
|
|
| 16 |
import com.attendenceSystem.module.user.entity.enums.Specialization;
|
| 17 |
import com.attendenceSystem.module.user.entity.enums.Role;
|
| 18 |
import com.attendenceSystem.module.user.entity.enums.Status;
|
| 19 |
+
import com.attendenceSystem.module.user.repository.projection.RoleCountProjection;
|
| 20 |
|
| 21 |
@Repository
|
| 22 |
public interface UserRepository extends JpaRepository<User, Long> {
|
| 23 |
+
|
| 24 |
Optional<User> findById(Long id);
|
| 25 |
|
| 26 |
Optional<User> findUserByUsername(String username);
|
|
|
|
| 62 |
List<UserSimpleResponse> findFullNameByDepartmentAndRoleNot(
|
| 63 |
@Param("department") Department department,
|
| 64 |
@Param("role") Role role);
|
| 65 |
+
|
| 66 |
+
@Query("SELECT u.role AS role, COUNT(u) AS count FROM User u GROUP BY u.role")
|
| 67 |
+
List<RoleCountProjection> countUsersGroupByRole();
|
| 68 |
}
|
src/main/java/com/attendenceSystem/module/user/repository/projection/RoleCountProjection.java
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.attendenceSystem.module.user.repository.projection;
|
| 2 |
+
|
| 3 |
+
import com.attendenceSystem.module.user.entity.enums.Role;
|
| 4 |
+
|
| 5 |
+
public interface RoleCountProjection {
|
| 6 |
+
Role getRole();
|
| 7 |
+
|
| 8 |
+
Long getCount();
|
| 9 |
+
|
| 10 |
+
}
|