liukangdong
2024-12-23 b8e554b2db1933319eedc2a1b9e6a013dce6e299
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.doumee.core.model;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 分页请求参数
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@Data
@ApiModel("分页请求参数")
public class PageWrap<M> implements Serializable {
 
    // 降序
    public static final String DESC = "DESC";
 
    // 升序
    public static final String ASC = "ASC";
 
    @ApiModelProperty(value = "条件参数",required = true)
    private M model;
 
    @ApiModelProperty("目标页 ")
    private int page;
 
    @ApiModelProperty("一页多少行")
    private int capacity;
 
    @ApiModelProperty("排序参数")
    private List<SortData> sorts;
 
    /**
     * 处理异常排序对象
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    public List<SortData> getSorts () {
        List<SortData> sorts = new ArrayList<>();
        if (this.sorts == null) {
            return sorts;
        }
        for (SortData sort: this.sorts) {
            if (sort.getProperty() == null || sort.getProperty().trim().length() == 0) {
                continue;
            }
            if (sort.getDirection() == null || sort.getDirection().trim().length() == 0 || (!sort.getDirection().trim().equalsIgnoreCase("asc") && !sort.getDirection().trim().equalsIgnoreCase("desc"))) {
                continue;
            }
            sorts.add(sort);
        }
        return sorts;
    }
 
    public List<SortData> getSorts1 () {
        return sorts;
    }
 
 
 
    /**
     * 处理异常页码
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    public int getPage () {
        return page <= 0 ? 1 : page;
    }
 
    /**
     * 处理异常页容量
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    public int getCapacity () {
        return capacity <= 0 ? 10 : capacity;
    }
 
    /**
     * 获取排序字符串
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    @ApiModelProperty(hidden = true)
    public String getOrderByClause () {
        List<SortData> sorts = this.getSorts();
        StringBuilder stringBuilder = new StringBuilder();
        for (SortData sortData: sorts) {
            // 防注入
            if (!sortData.getProperty().matches("[a-zA-Z0-9_\\.]+")) {
                continue;
            }
            stringBuilder.append(sortData.getProperty().trim());
            stringBuilder.append(" ");
            stringBuilder.append(sortData.getDirection().trim());
            stringBuilder.append(",");
        }
        if (stringBuilder.length() == 0) {
            return null;
        }
        return "ORDER BY " + stringBuilder.substring(0, stringBuilder.length() - 1);
    }
 
    /**
     * 排序对象
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    @Data
    @ApiModel("排序对象")
    public static class SortData {
 
        @ApiModelProperty("排序字段 ")
        private String property;
 
        @ApiModelProperty("排序方向(ASC:升序,DESC:降序)")
        private String direction;
    }
 
    public <T> Page<T> toPage() {
        return new Page<>(page, capacity);
    }
}