tech.guyi.web.quick.service.search.SearchCreator Maven / Gradle / Ivy
The newest version!
package tech.guyi.web.quick.service.search;
import org.springframework.context.ApplicationContext;
import tech.guyi.web.quick.core.utils.TypeConverterUtils;
import tech.guyi.web.quick.service.search.entry.SearchItem;
import tech.guyi.web.quick.service.search.entry.SearchType;
import tech.guyi.web.quick.service.search.getter.SearchValue;
import tech.guyi.web.quick.service.service.QuickService;
import tech.guyi.web.quick.service.utils.ReflectionUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author guyi
* 搜索实体创建器
*/
public class SearchCreator {
@Resource
private ApplicationContext context;
private Map services;
@PostConstruct
public void onApplicationStart(){
this.services = this.context.getBeansOfType(QuickService.class)
.values()
.stream()
.collect(Collectors.toMap(s -> s.entityClass().getSimpleName(), s -> s));
}
/**
* 表达式正则匹配
*/
private final Pattern patternEX = Pattern.compile("(\\$\\{([a-zA-Z]+)\\.([a-zA-Z]+)\\})");
/**
* 通过表达式获取值
* @param ex 表达式
* @return 返回值
*/
private Optional> fromEx(String ex){
Matcher matcher = patternEX.matcher(ex);
if (matcher.find()){
String entityName = matcher.group(2);
String fieldName = matcher.group(3);
return Optional.ofNullable(services.get(entityName))
.map(QuickService::findAll)
.map(list -> (List