优化:🔥 深度优化后端 CRUD 公共组件,并抽取前端下载功能到 CRUD 公共组件

1. 后端抽取导出功能到 CRUD 公共组件
2. 查询列表及导出接口支持排序参数
3. 深度优化 BaseServiceImpl 中的 CRUD 公共实现
4. 前端抽取公共下载组件
5. 优化部分细节并修复部分错误
This commit is contained in:
2023-02-13 21:15:06 +08:00
parent 142d315a8d
commit 03b57fb021
25 changed files with 457 additions and 307 deletions

View File

@@ -16,14 +16,13 @@
package top.charles7c.cnadmin.common.model.query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Min;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.validator.constraints.Range;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Sort;
@@ -32,7 +31,6 @@ import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
/**
@@ -44,7 +42,7 @@ import cn.hutool.core.util.StrUtil;
@Data
@ParameterObject
@Schema(description = "分页查询条件")
public class PageQuery implements Serializable {
public class PageQuery extends SortQuery {
private static final long serialVersionUID = 1L;
@@ -52,60 +50,44 @@ public class PageQuery implements Serializable {
* 页码
*/
@Schema(description = "页码")
private int page;
@Min(value = 1, message = "页码最小值为 1")
private Integer page;
/**
* 每页记录
* 每页
*/
@Schema(description = "每页记录")
private int size;
/**
* 排序条件
*/
@Schema(description = "排序条件", example = "sort=published,desc&sort=title,asc")
private String[] sort;
@Schema(description = "每页")
@Range(min = 1, max = 1000, message = "每页条数(取值范围 1-1000")
private Integer size;
/** 默认页码1 */
private static final int DEFAULT_PAGE = 1;
/** 默认每页记录数10 */
/** 默认每页条数10 */
private static final int DEFAULT_SIZE = 10;
private static final String DELIMITER = ",";
/** 默认每页最大条数1000 */
private static final int DEFAULT_MAX_SIZE = 1000;
public PageQuery() {
this.page = DEFAULT_PAGE;
this.size = DEFAULT_SIZE;
this(DEFAULT_PAGE, DEFAULT_SIZE);
}
public int getPage() {
return page < 0 ? DEFAULT_PAGE : page;
public PageQuery(Integer page, Integer size) {
this.setPage(page);
this.setSize(size);
}
/**
* 解析排序条件为 Spring 分页排序实体
*
* @return Spring 分页排序实体
*/
public Sort getSort() {
if (ArrayUtil.isEmpty(sort)) {
return Sort.unsorted();
}
public void setPage(Integer page) {
this.page = page == null ? DEFAULT_PAGE : page;
}
List<Sort.Order> orders = new ArrayList<>(sort.length);
if (sort[0].contains(DELIMITER)) {
// e.g "sort=published,desc&sort=title,asc"
for (String s : sort) {
String[] sortArr = s.split(DELIMITER);
Sort.Order order = new Sort.Order(Sort.Direction.valueOf(sortArr[1].toUpperCase()), sortArr[0]);
orders.add(order);
}
public void setSize(Integer size) {
if (size == null) {
this.size = DEFAULT_SIZE;
} else if (size > DEFAULT_MAX_SIZE) {
this.size = DEFAULT_MAX_SIZE;
} else {
// e.g "sort=published,desc"
Sort.Order order = new Sort.Order(Sort.Direction.valueOf(sort[1].toUpperCase()), sort[0]);
orders.add(order);
this.size = size;
}
return Sort.by(orders);
}
/**

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.model.query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Sort;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.consts.CharConstants;
/**
* 排序查询条件
*
* @author Charles7c
* @since 2023/2/12 21:30
*/
@Data
@ParameterObject
@Schema(description = "排序查询条件")
public class SortQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 排序条件
*/
@Schema(description = "排序条件", example = "sort=published,desc&sort=title,asc")
private String[] sort;
/**
* 解析排序条件为 Spring 分页排序实体
*
* @return Spring 分页排序实体
*/
public Sort getSort() {
if (ArrayUtil.isEmpty(sort)) {
return Sort.unsorted();
}
List<Sort.Order> orders = new ArrayList<>(sort.length);
if (StrUtil.contains(sort[0], CharConstants.COMMA)) {
// e.g "sort=published,desc&sort=title,asc"
for (String s : sort) {
List<String> sortList = StrUtil.split(s, CharConstants.COMMA);
Sort.Order order =
new Sort.Order(Sort.Direction.valueOf(sortList.get(1).toUpperCase()), sortList.get(0));
orders.add(order);
}
} else {
// e.g "sort=published,desc"
Sort.Order order = new Sort.Order(Sort.Direction.valueOf(sort[1].toUpperCase()), sort[0]);
orders.add(order);
}
return Sort.by(orders);
}
}

View File

@@ -102,7 +102,7 @@ public class PageDataVO<V> {
* @param page
* 页码
* @param size
* 每页记录
* 每页
* @param list
* 列表数据
* @param <V>