fixed: 学员的多部门查询

This commit is contained in:
none 2023-05-09 10:30:31 +08:00
parent 08cad48fa4
commit 669e4fe47b
5 changed files with 51 additions and 36 deletions

View File

@ -16,6 +16,7 @@
package xyz.playedu.api.controller.backend;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -49,6 +50,7 @@ import java.util.stream.Collectors;
* @create 2023/3/24 16:08
*/
@RestController
@Slf4j
@RequestMapping("/backend/v1/course/{courseId}/user")
public class CourseUserController {
@ -86,8 +88,7 @@ public class CourseUserController {
// 所属部门
List<Integer> depIds = courseService.getDepIdsByCourseId(courseId);
if (depIds != null && depIds.size() > 0) {
filter.setDepIds(
depIds.stream().map(String::valueOf).collect(Collectors.joining(",", "", "")));
filter.setDepIds(depIds);
}
PaginationResult<User> result = userService.paginate(page, size, filter);

View File

@ -188,7 +188,12 @@ public class DepartmentController {
String name = MapUtils.getString(params, "name");
String email = MapUtils.getString(params, "email");
String idCard = MapUtils.getString(params, "id_card");
String depIds = String.valueOf(id);
List<Integer> depIds =
new ArrayList<>() {
{
add(id);
}
};
String courseIdsStr = MapUtils.getString(params, "course_ids");
String showMode = MapUtils.getString(params, "show_mode");

View File

@ -99,8 +99,17 @@ public class UserController {
Integer isVerify = MapUtils.getInteger(params, "is_verify");
Integer isSetPassword = MapUtils.getInteger(params, "is_set_password");
String createdAt = MapUtils.getString(params, "created_at");
String depIds = MapUtils.getString(params, "dep_ids");
String depIdsStr = MapUtils.getString(params, "dep_ids");
List<Integer> depIds = null;
if (depIdsStr != null && depIdsStr.trim().length() > 0) {
if ("0".equals(depIdsStr)) {
depIds = new ArrayList<>();
} else {
depIds = Arrays.stream(depIdsStr.split(",")).map(Integer::valueOf).toList();
}
}
List<Integer> finalDepIds = depIds;
UserPaginateFilter filter =
new UserPaginateFilter() {
{
@ -111,7 +120,7 @@ public class UserController {
setIsLock(isLock);
setIsVerify(isVerify);
setIsSetPassword(isSetPassword);
setDepIds(depIds);
setDepIds(finalDepIds);
setSortAlgo(sortAlgo);
setSortField(sortField);
}

View File

@ -17,6 +17,8 @@ package xyz.playedu.api.types.paginate;
import lombok.Data;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
*
@ -36,7 +38,7 @@ public class UserPaginateFilter {
// 创建时间范围过滤
private String[] createdAt;
private String depIds;
private List<Integer> depIds;
// 排序控制
private String sortField;

View File

@ -36,24 +36,23 @@
<select id="paginateCount" resultType="java.lang.Long">
SELECT count(1)
FROM `users`
<if test="depIds != null and depIds != ''">
<choose>
<when test="depIds.indexOf('0') == 0">
LEFT JOIN `user_department` ON `user_department`.`user_id` = `users`.`id`
</when>
<otherwise>
INNER JOIN `user_department` ON `user_department`.`user_id` = `users`.`id`
</otherwise>
</choose>
</if>
<where>
<if test="depIds != null and depIds != ''">
<if test="depIds != null">
<choose>
<when test="depIds.indexOf('0') == 0">
AND `user_department`.`user_id` IS NULL
<when test="depIds.size == 0">
AND `users`.`id` IN (
SELECT `users`.`id` from `users` LEFT JOIN `user_department` ON `user_department`.`user_id` =
`users`.`id`
WHERE `user_department`.`user_id` IS NULL
)
</when>
<otherwise>
AND `user_department`.`dep_id` IN (#{depIds})
AND `users`.`id` IN (
SELECT `users`.`id` from `users` LEFT JOIN `user_department` ON `user_department`.`user_id` =
`users`.`id`
WHERE `user_department`.`dep_id` IN (<foreach collection="depIds" item="depId" separator=",">
#{depId}</foreach>)
)
</otherwise>
</choose>
</if>
@ -78,7 +77,7 @@
<if test="isSetPassword != null">
AND `users`.`is_set_password` = #{isSetPassword}
</if>
<if test="createdAt != null">
<if test="createdAt != null and createdAt.size == 2">
AND `users`.`created_at` BETWEEN
<foreach collection="createdAt" item="createdAtItem" separator=" AND ">#{createdAtItem}</foreach>
</if>
@ -88,24 +87,23 @@
<select id="paginate" resultType="xyz.playedu.api.domain.User">
SELECT `users`.*
FROM `users`
<if test="depIds != null and depIds != ''">
<choose>
<when test="depIds.indexOf('0') == 0">
LEFT JOIN `user_department` ON `user_department`.`user_id` = `users`.`id`
</when>
<otherwise>
INNER JOIN `user_department` ON `user_department`.`user_id` = `users`.`id`
</otherwise>
</choose>
</if>
<where>
<if test="depIds != null and depIds != ''">
<if test="depIds != null">
<choose>
<when test="depIds.indexOf('0') == 0">
AND `user_department`.`user_id` IS NULL
<when test="depIds.size == 0">
AND `users`.`id` IN (
SELECT `users`.`id` from `users` LEFT JOIN `user_department` ON `user_department`.`user_id` =
`users`.`id`
WHERE `user_department`.`user_id` IS NULL
)
</when>
<otherwise>
AND `user_department`.`dep_id` IN (#{depIds})
AND `users`.`id` IN (
SELECT `users`.`id` from `users` LEFT JOIN `user_department` ON `user_department`.`user_id` =
`users`.`id`
WHERE `user_department`.`dep_id` IN (<foreach collection="depIds" item="depId" separator=",">
#{depId}</foreach>)
)
</otherwise>
</choose>
</if>
@ -130,7 +128,7 @@
<if test="isSetPassword != null">
AND `users`.`is_set_password` = #{isSetPassword}
</if>
<if test="createdAt != null">
<if test="createdAt != null and createdAt.size == 2">
AND `users`.`created_at` BETWEEN
<foreach collection="createdAt" item="createdAtItem" separator=" AND ">#{createdAtItem}</foreach>
</if>