优化学员列表

This commit is contained in:
none 2023-03-18 21:40:05 +08:00
parent 17d18da19b
commit 77a73be615
6 changed files with 175 additions and 78 deletions

View File

@ -66,7 +66,7 @@ public class UserController {
String createdAt = MapUtils.getString(params, "created_at");
String depIds = MapUtils.getString(params, "dep_ids");
UserPaginateFilter filter = new UserPaginateFilter(){{
UserPaginateFilter filter = new UserPaginateFilter() {{
setName(name);
setNickname(nickname);
setEmail(email);
@ -75,12 +75,15 @@ public class UserController {
setIsLock(isLock);
setIsVerify(isVerify);
setIsSetPassword(isSetPassword);
setCreatedAt(createdAt);
setDepIds(depIds);
setSortAlgo(sortAlgo);
setSortField(sortField);
}};
if (createdAt != null && createdAt.trim().length() > 0) {
filter.setCreatedAt(createdAt.split(","));
}
PaginationResult<User> result = userService.paginate(page, size, filter);
return JsonResponse.data(result);
}

View File

@ -3,16 +3,23 @@ package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import xyz.playedu.api.types.paginate.UserPaginateFilter;
import java.util.List;
/**
* @author tengteng
* @description 针对表users的数据库操作Mapper
* @createDate 2023-02-23 14:04:49
* @Entity xyz.playedu.api.domain.User
*/
* @author tengteng
* @description 针对表users的数据库操作Mapper
* @createDate 2023-02-23 14:04:49
* @Entity xyz.playedu.api.domain.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> paginate(UserPaginateFilter filter);
Long paginateCount(UserPaginateFilter filter);
}

View File

@ -1,8 +1,5 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
@ -19,8 +16,6 @@ import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.UserPaginateFilter;
import xyz.playedu.api.util.HelperUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@ -43,65 +38,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Override
public PaginationResult<User> paginate(int page, int size, UserPaginateFilter filter) {
QueryWrapper<User> wrapper = query().getWrapper().eq("1", "1");
if (filter.getEmail() != null && filter.getEmail().trim().length() > 0) {
wrapper.eq("email", filter.getEmail());
}
if (filter.getName() != null && filter.getName().trim().length() > 0) {
wrapper.eq("name", filter.getName());
}
if (filter.getNickname() != null && filter.getNickname().length() > 0) {
wrapper.eq("nickname", filter.getNickname());
}
if (filter.getIdCard() != null && filter.getIdCard().trim().length() > 0) {
wrapper.eq("id_card", filter.getIdCard());
}
if (filter.getIsActive() != null) {
wrapper.eq("is_active", filter.getIsActive());
}
if (filter.getIsLock() != null) {
wrapper.eq("is_lock", filter.getIsLock());
}
if (filter.getIsVerify() != null) {
wrapper.eq("is_verify", filter.getIsVerify());
}
if (filter.getIsSetPassword() != null) {
wrapper.eq("is_set_password", filter.getIsSetPassword());
}
if (filter.getCreatedAt() != null && filter.getCreatedAt().trim().length() > 0) {
Date[] createdAt = Arrays.stream(filter.getCreatedAt().split(",")).map(Date::new).toArray(Date[]::new);
wrapper.between("created_at", createdAt[0], createdAt[1]);
}
if (filter.getDepIds() != null && filter.getDepIds().trim().length() > 0) {
List<Integer> depIds = Arrays.stream(filter.getDepIds().split(",")).map(Integer::valueOf).toList();
List<Integer> userIds = userDepartmentService.getUserIdsByDepIds(depIds);
if (userIds == null || userIds.size() == 0) {
userIds = HelperUtil.zeroIntegerList();
}
wrapper.in("id", userIds);
}
String sortFiled = filter.getSortField();
if (sortFiled == null || sortFiled.trim().length() == 0) {
sortFiled = "id";
}
String sortAlgo = filter.getSortAlgo();
if (sortAlgo == null || sortAlgo.trim().length() == 0) {
sortAlgo = "desc";
}
if ("desc".equals(sortAlgo)) {
wrapper.orderByDesc(sortFiled);
} else {
wrapper.orderByAsc(sortFiled);
}
IPage<User> userPage = new Page<>(page, size);
userPage = page(userPage, wrapper);
filter.setPageStart((page - 1) * size);
filter.setPageSize(size);
PaginationResult<User> pageResult = new PaginationResult<>();
pageResult.setData(userPage.getRecords());
pageResult.setTotal(userPage.getTotal());
pageResult.setData(getBaseMapper().paginate(filter));
pageResult.setTotal(getBaseMapper().paginateCount(filter));
return pageResult;
}

View File

@ -25,7 +25,7 @@ public class UserPaginateFilter {
private Integer isSetPassword;
// 创建时间范围过滤
private String createdAt;
private String[] createdAt;
private String depIds;
@ -33,4 +33,7 @@ public class UserPaginateFilter {
private String sortField;
private String sortAlgo;
private Integer pageStart;
private Integer pageSize;
}

View File

@ -90,7 +90,7 @@
ORDER BY `resources`.`size` ASC
</when>
<when test="sortField == 'created_at'">
ORDER BY `resources`.`size` ASC
ORDER BY `resources`.`created_at` ASC
</when>
<otherwise>
ORDER BY `resources`.`id` ASC
@ -103,7 +103,7 @@
ORDER BY `resources`.`size` DESC
</when>
<when test="sortField == 'created_at'">
ORDER BY `resources`.`size` DESC
ORDER BY `resources`.`created_at` DESC
</when>
<otherwise>
ORDER BY `resources`.`id` DESC

View File

@ -27,12 +27,154 @@
</resultMap>
<sql id="Base_Column_List">
id,email,nickname,
name,avatar,password,
salt,id_card,credit1,
create_ip,create_city,is_active,
is_lock,is_verify,verify_at,
is_set_password,login_at,created_at,
id,email,nickname,
name,avatar,password,
salt,id_card,credit1,
create_ip,create_city,is_active,
is_lock,is_verify,verify_at,
is_set_password,login_at,created_at,
updated_at
</sql>
<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 != ''">
<choose>
<when test="depIds.indexOf('0') == 0">
AND `user_department`.`user_id` IS NULL
</when>
<otherwise>
AND `user_department`.`dep_id` IN (#{depIds})
</otherwise>
</choose>
</if>
<if test="name != null and name != ''">
AND `users`.`name` LIKE concat('%',#{name},'%')
</if>
<if test="nickname != null and nickname != ''">
AND `users`.`nickname` LIKE concat('%',#{nickname},'%')
</if>
<if test="email != null and email != ''">
AND `users`.`email` = #{email}
</if>
<if test="idCard != null and idCard != ''">
AND `users`.`id_card` = #{idCard}
</if>
<if test="isActive != null">
AND `users`.`is_active` = #{isActive}
</if>
<if test="isLock != null">
AND `users`.`is_lock` = #{isLock}
</if>
<if test="isVerify != null">
AND `users`.`is_verify` = #{isVerify}
</if>
<if test="isSetPassword != null">
AND `users`.`is_set_password` = #{isSetPassword}
</if>
<if test="createdAt != null">
AND `users`.`created_at` BETWEEN
<foreach collection="createdAt" item="createdAtItem" separator=" AND ">#{createdAtItem}</foreach>
</if>
</where>
</select>
<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 != ''">
<choose>
<when test="depIds.indexOf('0') == 0">
AND `user_department`.`user_id` IS NULL
</when>
<otherwise>
AND `user_department`.`dep_id` IN (#{depIds})
</otherwise>
</choose>
</if>
<if test="name != null and name != ''">
AND `users`.`name` LIKE concat('%',#{name},'%')
</if>
<if test="nickname != null and nickname != ''">
AND `users`.`nickname` LIKE concat('%',#{nickname},'%')
</if>
<if test="email != null and email != ''">
AND `users`.`email` = #{email}
</if>
<if test="idCard != null and idCard != ''">
AND `users`.`id_card` = #{idCard}
</if>
<if test="isActive != null">
AND `users`.`is_active` = #{isActive}
</if>
<if test="isLock != null">
AND `users`.`is_lock` = #{isLock}
</if>
<if test="isVerify != null">
AND `users`.`is_verify` = #{isVerify}
</if>
<if test="isSetPassword != null">
AND `users`.`is_set_password` = #{isSetPassword}
</if>
<if test="createdAt != null">
AND `users`.`created_at` BETWEEN
<foreach collection="createdAt" item="createdAtItem" separator=" AND ">#{createdAtItem}</foreach>
</if>
</where>
<if test="sortAlgo == 'asc'">
<choose>
<when test="sortField == 'size'">
ORDER BY `users`.`size` ASC
</when>
<when test="sortField == 'created_at'">
ORDER BY `users`.`created_at` ASC
</when>
<when test="sortField == 'credit1'">
ORDER BY `users`.`credit1` ASC
</when>
<otherwise>
ORDER BY `users`.`id` ASC
</otherwise>
</choose>
</if>
<if test="sortAlgo != 'asc'">
<choose>
<when test="sortField == 'size'">
ORDER BY `users`.`size` DESC
</when>
<when test="sortField == 'created_at'">
ORDER BY `users`.`created_at` DESC
</when>
<when test="sortField == 'credit1'">
ORDER BY `users`.`credit1` DESC
</when>
<otherwise>
ORDER BY `users`.`id` DESC
</otherwise>
</choose>
</if>
LIMIT #{pageStart}, #{pageSize};
</select>
</mapper>