querywrapper 的map解析器

这个是我公司用的东西,也不知道是哪扣来的,大致功能就是从前端返回一些例如下图的东西,实现equal等等方法,其实不太好,但是小公司开发省事
使用MapFilter.getQueryWrapper(“一个map”,“当前对应的实体类.class”)返回一个querywrapper
在这里插入图片描述

package com.jp.yycx.business.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.support.SqlKeyword;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapFilter {

    public static Map<String, Object> newMap (Map<String, Object> oldMap){
        if (!Func.isEmpty(oldMap)) {
            Map<String, Object> newMap = JSON.parseObject(JSON.toJSONString(oldMap));
            oldMap.forEach((k, v) -> {
                if (!Func.hasEmpty(new Object[]{k, v}) && !k.endsWith("_ignore")) {
                    if (k.endsWith("_equal")) {
                    } else if (k.endsWith("_notequal")) {
                    } else if (k.endsWith("_notlike")) {
                    } else if (k.endsWith("_gt")) {
                    } else if (k.endsWith("_lt")) {
                    } else if (k.endsWith("_dategt")) {
                    } else if (k.endsWith("_dateequal")) {
                    } else if (k.endsWith("_datelt")) {
                    } else if (k.endsWith("_null")) {
                    } else if (k.endsWith("_notnull")) {
                    } else if(k.endsWith("_key")){
                    } else {
                        Object obj = newMap.remove(k);
                    }

                }
            });
            return newMap;
        }
        return null;
    }

    /**
     * 创建QueryWrapper
     * @param query
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query,  Class<T> clazz) {
        QueryWrapper<T> qw = new QueryWrapper();
        qw.setEntity(BeanUtil.newInstance(clazz));
        buildCondition(query, qw);
        return qw;
    }

    public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) {
        if (!Func.isEmpty(query)) {
            query.forEach((k, v) -> {
                if (!Func.hasEmpty(new Object[]{k, v}) && !k.endsWith("_ignore")) {
                    if (k.endsWith("_equal")) {
                        qw.eq(getColumn(k, "_equal"), v);
                    } else if (k.endsWith("_notequal")) {
                        qw.ne(getColumn(k, "_notequal"), v);
                    } else if (k.endsWith("_notlike")) {
                        qw.notLike(getColumn(k, "_notlike"), v);
                    } else if (k.endsWith("_gt")) {
                        qw.gt(getColumn(k, "_gt"), v);
                    } else if (k.endsWith("_lt")) {
                        qw.lt(getColumn(k, "_lt"), v);
                    } else if (k.endsWith("_dategt")) {
                        qw.gt(getColumn(k, "_dategt"), v);
                    } else if (k.endsWith("_dateequal")) {
                        qw.eq(getColumn(k, "_dateequal"), v);
                    } else if (k.endsWith("_datege")) {
                        qw.ge(getColumn(k, "_datege"), v);
                    } else if (k.endsWith("_datele")) {
                        qw.le(getColumn(k, "_datele"), v);
                    } else if (k.endsWith("_datelt")) {
                        qw.lt(getColumn(k, "_datelt"), v);
                    } else if (k.endsWith("_null")) {
                        qw.isNull(getColumn(k, "_null"));
                    } else if (k.endsWith("_notnull")) {
                        qw.isNotNull(getColumn(k, "_notnull"));
                    } else if (k.endsWith("_in")) {
                        qw.in(getColumn(k, "_in"),Func.toStrList(",", v.toString()));
                    } else if (k.endsWith("_notIn")) {
                        qw.notIn(getColumn(k, "_notIn"),Func.toStrList(",", v.toString()));
                    }else if (k.endsWith("_like")) {
                        qw.like(getColumn(k, "_like"),v);
                    } else if (k.endsWith("_keyLike")) {
                        List<String> columnLikes = Func.toStrList(",", (String) query.get("keyLike_column"));
                        if(!Func.isEmpty(columnLikes)) {
                            qw.and(wrapper -> {
                                columnLikes.forEach(column -> {
                                    wrapper.or(r -> {
                                        r.like(StringUtil.humpToUnderline(column), v);
                                    });
                                });
                            });
                        }
                    }

                }
            });
        }
    }

    private static String getColumn(String column, String keyword) {
        return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值