Java实现多个属性过滤后,联动统计过滤数据的count值
业务场景:一个多字段的对象,获取列表,需要根据多个码值的字段过滤筛选,并且统计每个码值对应的过滤后的count值
1. 定义 FileterCounter 对象,id 获取函数,name 获取函数,函数式编程
import lombok.Data;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
@Data
public class FilterCounter<T> {
private List<T> list;
private Function<T,Object> idGetter;
private Function<T,Object> nameGetter;
private Comparator<FilterEntity> sortBy;
}
2. FilterEntity 对象,用来返回 统计的 码值的信息:id、name、count
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
public class FilterEntity implements Serializable {
private Object id;
private Object name;
private Long count;
}
3. 过滤器定义
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
public class FilterSearch<T> {
// mybatis-plus 所继承的 BaseMapper
private final BaseMapper baseMapper;
public FilterSearch(BaseMapper<?> baseMapper){
this.baseMapper = baseMapper;
}
// 接收 like 查询的 Map 和counter对象
private Map<String,Object> likeMap = new HashMap<>();
private Map<String ,FilterCounter<?>> likeCounters = new HashMap<>();
// 接收 equals查询
private Map<String,Object> equalsMap = new HashMap<>();
private Map<String ,FilterCounter<?>> equalsCounters = new HashMap<>();
// 接收 in 查询
private Map<String,Collection<?>> inMap = new HashMap<>();
private Map<String ,FilterCounter<?>> inCounters = new HashMap<>();
// 排序
private Map<String,Boolean> orders = new LinkedHashMap<>();
// 正序排序
public FilterSearch<T> orderBy(String field){
orders.put(field,true);
return this;
}
//倒序
public FilterSearch<T> orderByDesc(String field){
orders.put(field,false);
return this;
}
public FilterSearch<T> like(String field,Object value,FilterCounter<?> likeCount){
likeMap.put(field,value);
likeCounters.put(field,likeCount);
return this;
}
public FilterSearch<T> equals(String field,Object value,FilterCounter<?> equalsCount){
equalsMap.put(field,value);
equalsCounters.put(field,equalsCount);
return this;
}
public FilterSearch<T> in(String field,Collection<?> value,FilterCounter<?> inCount){
inMap.put(field,value);
inCounters.put(field,inCount);
return this;
}
// 获取查询的 QueryWrapper
private QueryWrapper<T> getWrapper(){
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
equalsMap.forEach(queryWrapper::eq);
likeMap.forEach(queryWrapper::like);
for (Map.Entry<String, Collection<?>> entry : inMap.entrySet()) {
Collection<?> value = entry.getValue();
if (value == null){
continue;
}
if (value.isEmpty()){
return null;
}
String key = entry.getKey();
if (value.size() == 1){
for (Object o : value) {
queryWrapper.eq(key,o);
}
}else {
queryWrapper.in(key,value);
}
}
return queryWrapper;
}
// 添加排序
public QueryWrapper<T> orderWrapper(){
QueryWrapper<T> wrapper = getWrapper();
if (wrapper == null){
return null;
}
if (!orders.isEmpty()){
orders.forEach((k,v)->wrapper.orderBy(true,v,k));
}
return wrapper;
}
// 获取筛选后的list 数据
public List<T> getList(){
QueryWrapper<T> wrapper = orderWrapper();
if (wrapper == null){
return Collections.EMPTY_LIST;
}
return baseMapper.selectList(wrapper);
}
// 业务需要,可忽略
public List<T> getListNoDesc(Class<T> tClass){
QueryWrapper<T> wrapper = orderWrapper();
if (wrapper == null){
return Collections.EMPTY_LIST;
}
wrapper.select(tClass,item->!item.getColumn().equals("description"));
return baseMapper.selectList(wrapper);
}
// 分页查询
public IPage<T> getPage(IPage<T> page ){
QueryWrapper<T> queryWrapper = orderWrapper();
if (queryWrapper == null){
return page;
}
return baseMapper.selectPage(page,queryWrapper);
}
private Map<String,?> findMap (String key){
if(likeMap.containsKey(key)){
return likeMap;
}
if (equalsMap.containsKey(key)){
return equalsMap;
}
if (inMap.containsKey(key)){
return inMap;
}
return null;
}
private Long selectCount(String key,Object value){
equalsMap.put(key,value);
Long count = baseMapper.selectCount(getWrapper());
equalsMap.remove(key);
return count;
}
public Long getCount(String key,Object value){
Map map = findMap(key);
if (map == null){
return selectCount(key, value);
}else {
Object o = map.remove(key);
Long aLong = selectCount(key, value);
map.put(key,o);
return aLong;
}
}
// 统计count 值
private Map<String,List<FilterEntity>> count(Map<String,FilterCounter<?>> counters,Map map){
Map<String,List<FilterEntity>> rmap = new HashMap<>();
counters.forEach((k,fc)->{
if (fc != null){
Object oldV = map.remove(k);
List<Serializable> list = (List<Serializable>) fc.getList();
List<FilterEntity> clist = new ArrayList<>(list.size());
rmap.remove(k,clist);
Function<Serializable, Object> idGetter = (Function<Serializable, Object>) fc.getIdGetter();
Function<Serializable, Object> nameGetter = (Function<Serializable, Object>) fc.getNameGetter();
for (Serializable v : list) {
Object id = idGetter.apply(v);
equalsMap.put(k,id);
QueryWrapper<T> wrapper = getWrapper();
long count = wrapper == null ? 0 : baseMapper.selectCount(wrapper);
equalsMap.remove(k);
clist.add(new FilterEntity().setId(id).setName(nameGetter.apply(v)).setCount(count));
}
Comparator<FilterEntity> sortBy = fc.getSortBy();
if (sortBy != null){
clist.sort(sortBy);
}
map.put(k,oldV);
}
});
return rmap;
}
public Map<String,List<FilterEntity>> countes(){
Map<String, List<FilterEntity>> count = count(likeCounters, likeMap);
count.putAll(count(equalsCounters,equalsMap));
count.putAll(count(inCounters,inMap));
return count;
}
public IPage<Map<String,Object>> subCount(String key, IPage<Map<String,Object>> supage){
Map map = findMap(key);
QueryWrapper<T> wrapper = getWrapper();
if (map == null){
return baseMapper.selectMapsPage(supage,
wrapper.select(key + ",count("+key+") c").groupBy(key).orderByDesc("c"));
}else {
Object oldV = map.remove(key);
IPage count = baseMapper.selectMapsPage(supage,
wrapper.select(key + ",count(" + key + ") c").groupBy(key).orderByDesc("c"));
map.put(key,oldV);
return count;
}
}
}
结束!今天的你也很辛苦!