All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.web.controller.BaseSearchController Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.kasinf.framework.rest.web.controller;

import cn.hutool.core.util.StrUtil;
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.core.response.BaseResponse;
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.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.stream.Collectors;

/**
 * @param   查询的实体
 * @param  查询实体的主键
 * @author lkhsh
 * 

* 只读的操作,只提供查询的接口 */ 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 BaseResponse showMe(@PathVariable("id") T t, Searchable searchable) { return setSuccess(searchable, t); } @GetMapping public BaseResponse searchMe(Searchable searchable) { Page page = getBaseService().findAll(searchable); return setSuccess(searchable, page); } @GetMapping("all") public BaseResponse searchAll(Searchable searchable) { List list = getBaseService().findAllWithSort(searchable); return setSuccess(searchable, list); } @SuppressWarnings("all") public BaseResponse 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); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy