MCPcopy Create free account
hub / github.com/PlexPt/rocket-pt / OrderParam

Class OrderParam

src/main/java/com/rocketpt/server/common/base/OrderParam.java:18–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16import lombok.Data;
17
18@Data
19public class OrderParam {
20
21 /**
22 * 排序字段
23 */
24 protected String prop;
25
26 /**
27 * 排序规则
28 */
29 protected String sort;
30
31 public void validOrder(String[] orderKey) throws RocketPTException {
32 this.prop = StringUtils.isBlank(this.prop) ? null : this.prop;
33 this.sort = StringUtils.isBlank(this.sort) ? Constants.Order.DEFAULT_ORDER_TYPE
34 : this.sort;
35
36 if (Arrays.asList(Constants.Order.ORDER_TYPE).indexOf(this.sort) < 0) {
37 throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
38 }
39
40 if (!StringUtils.isBlank(this.prop) && Arrays.asList(orderKey).indexOf(this.prop) < 0) {
41 throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
42 }
43 }
44
45 public void validOrder() throws RocketPTException {
46 List<String> orderKey = getOrderKey();
47 prop = StringUtils.isBlank(prop) ? null : StrUtil.toUnderlineCase(prop);
48 sort = StringUtils.isBlank(sort) ? Constants.Order.DEFAULT_ORDER_TYPE : sort;
49
50 if (!ArrayUtil.contains(Constants.Order.ORDER_TYPE, sort)) {
51 throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
52 }
53
54 if (StringUtils.isNotBlank(prop) && !orderKey.contains(prop)) {
55 throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
56 }
57 }
58
59 /**
60 * @return 反射获取字段列表
61 */
62 private List<String> getOrderKey() {
63 List<String> list = new ArrayList<>();
64
65 Field[] fields = getClass().getDeclaredFields();
66 if (fields != null) {
67 for (Field field : fields) {
68 field.setAccessible(true);
69 String name = field.getName();
70
71 list.add(StrUtil.toUnderlineCase(name));
72 }
73 }
74 return list;
75 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected