SmartWrapper.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package org.example.music.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import lombok.Getter;
  7. import java.util.Map;
  8. import java.util.Objects;
  9. /**
  10. * @author wzt on 2020/2/11.
  11. * @version 1.0
  12. */
  13. @Getter
  14. public class SmartWrapper<T> {
  15. private boolean isContainOrderByCommand = false;
  16. private int current = 1;
  17. private int size = 10;
  18. private QueryWrapper<T> queryWrapper = new QueryWrapper<>();
  19. private Map<String, ModelFieldAttr> modelFieldToTableField = null;
  20. public static <T> SmartWrapper<T> newWrapper() {
  21. return new SmartWrapper<>();
  22. }
  23. public IPage<T> generatePage() {
  24. return new Page<>(this.current, this.size);
  25. }
  26. /**
  27. * 解析JSON查询字符串, 构建QueryWrapper对象
  28. *
  29. * @param queryObject
  30. * @param clazz
  31. * @return
  32. */
  33. public void parse(JSONObject queryObject, Class<T> clazz) {
  34. if (Objects.isNull(queryObject)) {
  35. return;
  36. }
  37. // 获取模型字段元信息
  38. modelFieldToTableField = ModelAttrUtils.generateModelAttr(clazz);
  39. // 解析查询条件
  40. this.parseQueryCondition(queryObject, clazz);
  41. // 解析排序条件
  42. this.parseOrderByCondition(queryObject);
  43. // 解析分页信息
  44. this.parsePage(queryObject);
  45. }
  46. private void parsePage(JSONObject queryObject) {
  47. if (Objects.isNull(queryObject)) {
  48. return;
  49. }
  50. Object current = queryObject.get("current");
  51. Object size = queryObject.get("size");
  52. if (current == null || size == null) {
  53. return;
  54. }
  55. this.current = (int) current;
  56. this.size = (int) size;
  57. }
  58. /**
  59. * 解析排序条件
  60. *
  61. * @param queryObject
  62. * @return
  63. */
  64. private void parseOrderByCondition(JSONObject queryObject) {
  65. }
  66. /**
  67. * 根据自定义语法解析查询条件
  68. *
  69. * @param queryObject
  70. * @param clazz
  71. * @return
  72. */
  73. private void parseQueryCondition(JSONObject queryObject, Class<T> clazz) {
  74. }
  75. }