com.kasinf.framework.rest.web.controller.BaseSearchController Maven / Gradle / Ivy
package com.kasinf.framework.rest.web.controller;
import cn.hutool.core.util.StrUtil;
import com.kasinf.framework.core.response.R;
import com.kasinf.framework.core.util.MessageUtils;
import com.kasinf.framework.core.util.ReflectUtils;
import com.kasinf.framework.rest.config.SearchableConfiguration;
import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.support.Searchable;
import com.kasinf.framework.rest.web.service.BaseService;
import com.kasinf.framework.rest.web.util.SearchableBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @param 查询的实体
* @param 查询实体的主键
* @author lkhsh
*
* 只读的操作,只提供查询的接口
*/
@SuppressWarnings("all")
public abstract class BaseSearchController implements BaseController {
@Autowired
protected SearchableConfiguration config;
@Autowired
protected ProjectionFactory factory;
protected BaseService baseService;
protected Class entityClass;
@Override
public Class getEntityClass() {
if (entityClass == null) {
entityClass = ReflectUtils.findParameterizedType(getClass(), 0);
}
return entityClass;
}
/**
* 获取实际的业务处理类
*
* @return 业务处理类
*/
@Override
public BaseService getBaseService() {
if (baseService == null) {
Class classz = ReflectUtils.findParameterizedType(getClass(), 0);
if (classz != null) {
String className = StrUtil.lowerFirst(classz.getSimpleName());
// 获取applicationContext中的bean,匹配以实体类开头的service
baseService = SearchableBeanUtils.getServiceLike(className);
}
}
return baseService;
}
@GetMapping("{id}")
public R showMe(@PathVariable("id") T t, Searchable searchable) {
return setSuccess(searchable, t);
}
@GetMapping
public R searchMe(Searchable searchable) {
Page page = getBaseService().findAll(searchable);
return setSuccess(searchable, page);
}
@GetMapping("all")
public R searchAll(Searchable searchable) {
List list = getBaseService().findAllWithSort(searchable);
return setSuccess(searchable, list);
}
@Override
public R success() {
int code = config.getHttpCode().get(HttpStatus.OK);
String msg = MessageUtils.isAvailable() ? MessageUtils.message("http.msg." + code) : "OK";
return R.ok(code, msg);
}
@Override
public R success(Object data) {
int code = config.getHttpCode().get(HttpStatus.OK);
String msg = MessageUtils.isAvailable() ? MessageUtils.message("http.msg." + code) : "OK";
return R.ok(code, msg, data);
}
public R setSuccess(Searchable searchable, Object object) {
// 有投影,则需要转换
if (searchable.hasProjection()) {
Class projection = searchable.getProjection();
if (object instanceof Page) {
Page page = (Page) object;
List list = page.getContent();
List content = (List) list.stream().map(l -> factory.createProjection(projection, l)).collect(Collectors.toList());
Page projectionPage = new PageImpl(content, page.getPageable(), page.getTotalElements());
return success(projectionPage);
} else if (object instanceof Collection) {
Collection collection = (Collection) object;
Object o = collection.stream().map(l -> factory.createProjection(projection, l)).collect(Collectors.toList());
return success(o);
} else {
Object o = factory.createProjection(projection, object);
return success(o);
}
}
return success(object);
}
}