BaseController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.simuwang.manage.api.base;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.simuwang.base.common.conts.HttpStatus;
  5. import com.simuwang.base.common.page.PageDomain;
  6. import com.simuwang.base.common.page.TableDataInfo;
  7. import com.simuwang.base.common.page.TableSupport;
  8. import com.simuwang.base.common.result.AjaxResult;
  9. import com.simuwang.base.common.util.DateUtils;
  10. import com.simuwang.base.common.util.PageUtils;
  11. import com.simuwang.base.common.util.SqlUtil;
  12. import com.simuwang.base.common.util.StringUtil;
  13. import com.smppw.common.pojo.enums.status.ResultCode;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.web.bind.WebDataBinder;
  17. import org.springframework.web.bind.annotation.InitBinder;
  18. import java.beans.PropertyEditorSupport;
  19. import java.util.Date;
  20. import java.util.List;
  21. /**
  22. * FileName: BaseController
  23. * Author: chenjianhua
  24. * Date: 2024/9/8 10:02
  25. * Description: ${DESCRIPTION}
  26. */
  27. public class BaseController {
  28. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  29. /**
  30. * 将前台传递过来的日期格式的字符串,自动转化为Date类型
  31. */
  32. @InitBinder
  33. public void initBinder(WebDataBinder binder)
  34. {
  35. // Date 类型转换
  36. binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
  37. {
  38. @Override
  39. public void setAsText(String text)
  40. {
  41. setValue(DateUtils.parseDate(text));
  42. }
  43. });
  44. }
  45. /**
  46. * 设置请求分页数据
  47. */
  48. protected void startPage()
  49. {
  50. PageUtils.startPage();
  51. }
  52. /**
  53. * 设置请求排序数据
  54. */
  55. protected void startOrderBy()
  56. {
  57. PageDomain pageDomain = TableSupport.buildPageRequest();
  58. if (StringUtil.isNotEmpty(pageDomain.getOrderBy()))
  59. {
  60. String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
  61. PageHelper.orderBy(orderBy);
  62. }
  63. }
  64. /**
  65. * 清理分页的线程变量
  66. */
  67. protected void clearPage()
  68. {
  69. PageUtils.clearPage();
  70. }
  71. /**
  72. * 响应请求分页数据
  73. */
  74. @SuppressWarnings({ "rawtypes", "unchecked" })
  75. protected TableDataInfo getDataTable(List<?> list)
  76. {
  77. TableDataInfo rspData = new TableDataInfo();
  78. rspData.setCode(HttpStatus.SUCCESS);
  79. rspData.setMsg(null);
  80. rspData.setRows(list);
  81. rspData.setTotal(new PageInfo(list).getTotal());
  82. return rspData;
  83. }
  84. /**
  85. * 响应返回结果
  86. *
  87. * @param rows 影响行数
  88. * @return 操作结果
  89. */
  90. protected AjaxResult toAjax(int rows)
  91. {
  92. return rows > 0 ? success() : error();
  93. }
  94. /**
  95. * 响应返回结果
  96. *
  97. * @param result 结果
  98. * @return 操作结果
  99. */
  100. protected AjaxResult toAjax(boolean result)
  101. {
  102. return result ? success() : error();
  103. }
  104. /**
  105. * 返回成功
  106. */
  107. public AjaxResult success()
  108. {
  109. return AjaxResult.success();
  110. }
  111. /**
  112. * 返回失败消息
  113. */
  114. public AjaxResult error()
  115. {
  116. return AjaxResult.error();
  117. }
  118. /**
  119. * 返回成功消息
  120. */
  121. public AjaxResult success(String message)
  122. {
  123. return AjaxResult.success(message);
  124. }
  125. /**
  126. * 返回成功数据
  127. */
  128. public static AjaxResult success(Object data)
  129. {
  130. return AjaxResult.success("操作成功", data);
  131. }
  132. /**
  133. * 返回失败消息
  134. */
  135. public AjaxResult error(String message)
  136. {
  137. return AjaxResult.error(message);
  138. }
  139. /**
  140. * 返回错误码消息
  141. */
  142. public AjaxResult error(AjaxResult.Type type, String message)
  143. {
  144. return new AjaxResult(type, message);
  145. }
  146. }