| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package com.water.common.controller;
-
- import com.water.common.entity.R;
- import com.water.common.entity.query.PageQuery;
- import com.water.common.entity.query.PageResult;
- import com.water.common.entity.query.QueryCondition;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * 基础控制器
- */
- public abstract class BaseController<T> {
-
- /**
- * 成功响应
- */
- protected R<T> success(T data) {
- return R.success(data);
- }
-
- /**
- * 成功响应
- */
- protected R<T> success(String message, T data) {
- return R.success(message, data);
- }
-
- /**
- * 成功响应
- */
- protected R<Void> success() {
- return R.success();
- }
-
- /**
- * 成功响应
- */
- protected R<Void> success(String message) {
- return R.success(message);
- }
-
- /**
- * 失败响应
- */
- protected R<Void> error(String message) {
- return R.error(message);
- }
-
- /**
- * 失败响应
- */
- protected R<Void> error(int code, String message) {
- return R.error(code, message);
- }
-
- /**
- * 分页响应
- */
- protected R<PageResult<T>> page(PageResult<T> pageResult) {
- return success(pageResult);
- }
-
- /**
- * 列表响应
- */
- protected R<List<T>> list(List<T> list) {
- return success(list);
- }
-
- /**
- * 映射响应
- */
- protected R<Map<String, Object>> map(Map<String, Object> map) {
- return success(map);
- }
-
- /**
- * 验证查询条件
- */
- protected void validateQueryCondition(QueryCondition queryCondition) {
- if (queryCondition == null) {
- throw new IllegalArgumentException("查询条件不能为空");
- }
-
- PageQuery page = queryCondition.getPage();
- if (page == null) {
- page = new PageQuery();
- queryCondition.setPage(page);
- }
-
- if (page.getCurrent() == null || page.getCurrent() < 1) {
- page.setCurrent(1);
- }
-
- if (page.getSize() == null || page.getSize() < 1) {
- page.setSize(10);
- }
-
- if (page.getSize() > 1000) {
- throw new IllegalArgumentException("每页数量不能超过1000");
- }
- }
- }
|