@@ -152,9 +152,9 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
||||
*/
|
||||
@Operation(summary = "信息", description = "根据ID查询单个信息")
|
||||
@GetMapping("/info")
|
||||
protected R info(@RequestAttribute() JSONObject requestParams,
|
||||
protected R<T> info(@RequestAttribute() JSONObject requestParams,
|
||||
@RequestParam() Long id) {
|
||||
return R.ok(service.info(requestParams, id));
|
||||
return R.ok((T) service.info(requestParams, id));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,7 +164,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
||||
*/
|
||||
@Operation(summary = "查询", description = "查询多个信息")
|
||||
@PostMapping("/list")
|
||||
protected R list(@RequestAttribute() JSONObject requestParams,
|
||||
protected R<List<T>> list(@RequestAttribute() JSONObject requestParams,
|
||||
@RequestAttribute(COOL_LIST_OP) CrudOption<T> option) {
|
||||
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
||||
List list = (List) switch (queryModeEnum) {
|
||||
@@ -183,7 +183,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
||||
*/
|
||||
@Operation(summary = "分页", description = "分页查询多个信息")
|
||||
@PostMapping("/page")
|
||||
protected R page(@RequestAttribute() JSONObject requestParams,
|
||||
protected R<PageResult<T>> page(@RequestAttribute() JSONObject requestParams,
|
||||
@RequestAttribute(COOL_PAGE_OP) CrudOption<T> option) {
|
||||
Integer page = requestParams.getInt("page", 1);
|
||||
Integer size = requestParams.getInt("size", 20);
|
||||
@@ -218,15 +218,8 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
||||
*
|
||||
* @param page 分页返回数据
|
||||
*/
|
||||
protected PageResult pageResult(Page page) {
|
||||
PageResult result = new PageResult();
|
||||
Map<String, Object> pagination = new HashMap<>();
|
||||
pagination.put("size", page.getPageSize());
|
||||
pagination.put("page", page.getPageNumber());
|
||||
pagination.put("total", page.getTotalRow());
|
||||
result.setList(page.getRecords());
|
||||
result.setPagination(pagination);
|
||||
return result;
|
||||
protected PageResult<T> pageResult(Page<T> page) {
|
||||
return PageResult.of( page );
|
||||
}
|
||||
|
||||
public Class<T> currentEntityClass() {
|
||||
|
||||
@@ -21,10 +21,10 @@ public class CoolExceptionHandler {
|
||||
public R handleRRException(CoolException e) {
|
||||
R r = new R();
|
||||
if (ObjUtil.isNotEmpty(e.getData())) {
|
||||
r.put("data", e.getData());
|
||||
r.setData( e.getData() );
|
||||
} else {
|
||||
r.put("code", e.getCode());
|
||||
r.put("message", e.getMessage());
|
||||
r.setCode( e.getCode() );
|
||||
r.setMessage( e.getMessage() );
|
||||
}
|
||||
if (ObjUtil.isNotEmpty(e.getCause())) {
|
||||
log.error(e.getCause().getMessage(), e.getCause());
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
package com.cool.core.request;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PageResult {
|
||||
private List<?> list;
|
||||
private Map<String, Object> pagination;
|
||||
@Schema( title = "分页数据模型" )
|
||||
public class PageResult<T> {
|
||||
@Schema( title = "分页数据" )
|
||||
private List<T> list;
|
||||
private Pagination pagination = new Pagination();
|
||||
|
||||
@Data
|
||||
public static class Pagination {
|
||||
@Schema( title = "页码" )
|
||||
private Long page;
|
||||
@Schema( title = "本页数量" )
|
||||
private Long size;
|
||||
@Schema( title = "总页数" )
|
||||
private Long total;
|
||||
}
|
||||
|
||||
static public <B> PageResult<B> of(Page<B> page ){
|
||||
PageResult<B> result = new PageResult<B>();
|
||||
result.setList(page.getRecords());
|
||||
result.pagination.setPage( page.getPageNumber() );
|
||||
result.pagination.setSize( page.getPageSize() );
|
||||
result.pagination.setTotal( page.getTotalRow() );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,42 @@
|
||||
package com.cool.core.request;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 返回信息
|
||||
*/
|
||||
public class R extends HashMap<String, Object> {
|
||||
@Schema(title = "响应数据结构")
|
||||
@Data
|
||||
public class R<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
@Schema(title = "编码:1000表示成功,其他值表示失败")
|
||||
private int code = 1000;
|
||||
|
||||
@Schema(title = "消息内容")
|
||||
private String message = "success";
|
||||
|
||||
@Schema(title = "响应数据")
|
||||
private T data;
|
||||
|
||||
@Schema(title = "响应数据")
|
||||
private Map<String, Object> dataMap = new HashMap<String, Object>();
|
||||
|
||||
|
||||
public R() {
|
||||
put("code", 1000);
|
||||
put("message", "success");
|
||||
|
||||
}
|
||||
|
||||
public R( int code, String message, T data ) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static R error() {
|
||||
@@ -23,14 +49,14 @@ public class R extends HashMap<String, Object> {
|
||||
|
||||
public static R error(int code, String msg) {
|
||||
R r = new R();
|
||||
r.put("code", code);
|
||||
r.put("message", msg);
|
||||
r.code = code;
|
||||
r.message = msg;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static R okMsg(String msg) {
|
||||
R r = new R();
|
||||
r.put("message", msg);
|
||||
r.message = msg;
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -38,12 +64,21 @@ public class R extends HashMap<String, Object> {
|
||||
return new R();
|
||||
}
|
||||
|
||||
public static R ok(Object data) {
|
||||
return new R().put("data", data);
|
||||
public static <B> R<B> ok(B data) {
|
||||
return new R<B>(1000 , "ok", data);
|
||||
}
|
||||
|
||||
public R put(String key, Object value) {
|
||||
super.put(key, value);
|
||||
|
||||
public R<T> put(String key, Object value) {
|
||||
if ( key.equals( "code") ) {
|
||||
this.code = (int)value;
|
||||
} else if ( key.equals( "message") ) {
|
||||
this.message = (String)value;
|
||||
} else if ( key.equals( "data") ) {
|
||||
this.data = (T) value;
|
||||
} else {
|
||||
dataMap.put(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user