StitchAttrUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.iamberry.rst.utils;
  2. import com.iamberry.rst.core.page.PagedResult;
  3. import org.springframework.web.servlet.ModelAndView;
  4. import java.lang.reflect.Field;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. /**
  8. * 解决rst列表参数拼接问题
  9. */
  10. public class StitchAttrUtil {
  11. public static StitchAttrUtil getSa(){
  12. return new StitchAttrUtil();
  13. }
  14. private StitchAttrUtil(){
  15. propertySet = new HashSet<String>(Arrays.asList(noProperty));
  16. };
  17. /**
  18. * 组装参数时,会过滤以下参数
  19. * 推荐使用:addDatePro(),添加过滤
  20. */
  21. // TODO: 2018/8/28 time 的转换调整,支持一个类中有有多种time的格式,因此注释掉初始化的time的Set,只能使用addDatePro()
  22. // private final String timeDate[] = {};
  23. /**
  24. * 控制时间参数格式
  25. * 推荐使用:addDatePro(),添加过滤
  26. */
  27. private final String noProperty[] = {
  28. "serialVersionUID",
  29. "awaitingSignclosedProductInfoList",
  30. "signclosedProductInfoList"
  31. };
  32. private Set<String> propertySet;
  33. private Map<String,SimpleDateFormat> timeMap = new HashMap<String,SimpleDateFormat>();
  34. /**
  35. * 组装ModelAndView
  36. * @param object
  37. * @param modelAndView
  38. * @param url
  39. * @param pagedResult
  40. * @throws IllegalAccessException
  41. */
  42. public void setModelAndView(Object object, ModelAndView modelAndView, String url, PagedResult<?> pagedResult) {
  43. StringBuilder sb = new StringBuilder(url);
  44. if(pagedResult.getTotal() != 0) {
  45. pagedResult.setPages((int) Math.ceil((double)pagedResult.getTotal()/pagedResult.getPageSize()));
  46. }
  47. sb.append("?pageSize=" + pagedResult.getPageSize());
  48. sb.append("&totalNum=" + pagedResult.getTotal() );
  49. this.setUrlByObj(sb,object);
  50. sb.append("&&pageNO=");
  51. Map<String, Object> map = this.getObjToMap(object);
  52. modelAndView.addAllObjects(map);
  53. modelAndView.addObject("page", pagedResult);
  54. modelAndView.addObject("url", sb.toString());
  55. return;
  56. }
  57. /**
  58. * 将obj 转为map,
  59. * 属性值为null的不会 put 到 Map 中
  60. * 属性名称在 noProperty 数组中的属性不会 put 到 Map 中
  61. * @param object
  62. * @return
  63. * @throws IllegalAccessException
  64. */
  65. public Map<String, Object> getObjToMap(Object object) {
  66. Map<String, Object> map = new HashMap<>();
  67. Class<?> clazz = object.getClass();
  68. for (Field field : clazz.getDeclaredFields()) {
  69. field.setAccessible(true);
  70. String fieldName = field.getName();
  71. Object value = null;
  72. try {
  73. value = field.get(object);
  74. }catch (IllegalAccessException e){
  75. }
  76. if (value != null && !propertySet.contains(fieldName)) {
  77. map.put(fieldName, value);
  78. }
  79. }
  80. return map;
  81. }
  82. /**
  83. * 拼接url
  84. * @param sb
  85. * @param object
  86. * @throws IllegalAccessException
  87. */
  88. public void setUrlByObj(StringBuilder sb,Object object){
  89. Class<?> clazz = object.getClass();
  90. for (Field field : clazz.getDeclaredFields()) {
  91. field.setAccessible(true);
  92. String fieldName = field.getName();
  93. Object value = null;
  94. try {
  95. value = field.get(object);
  96. }catch (IllegalAccessException e){
  97. }
  98. if (value != null && !propertySet.contains(fieldName)) {
  99. /* 遍历map,将时间格式进行转换 */
  100. for (Map.Entry<String, SimpleDateFormat> entry : timeMap.entrySet()) {
  101. if(entry.getKey().equals(fieldName)){
  102. value = entry.getValue().format(value);
  103. }
  104. }
  105. sb.append("&"+fieldName+ "=" + value.toString());
  106. }
  107. }
  108. }
  109. /**
  110. * 添加组装时被过滤的属性名称
  111. * @param pros
  112. */
  113. public StitchAttrUtil addNoPro(String...pros){
  114. for(String pro : pros){
  115. if(pro != null && !"".equals(pro))
  116. propertySet.add(pro);
  117. }
  118. return this;
  119. }
  120. /**
  121. * 添加时间参数格式
  122. * @param fom //"yyyy-MM-dd HH:mm:ss"
  123. * @param pros
  124. */
  125. public StitchAttrUtil addDatePro(String fom,String...pros){
  126. SimpleDateFormat sdf = new SimpleDateFormat(fom);
  127. for(String pro : pros){
  128. if(pro != null && !"".equals(pro)){
  129. timeMap.put(pro,sdf);
  130. }
  131. }
  132. return this;
  133. }
  134. }