package com.zksy.common.utils;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zksy.common.domain.ConditionVo;

import java.net.URLDecoder;
import java.util.List;
/**
 * @author zkh
 * @since 2020-03-16
 */
public class SearchUtil {
    public static QueryWrapper parseWhereSql(String conditionJson) throws Exception{
        QueryWrapper<?> queryWrapper = new QueryWrapper();
        String[] empty = {"none"};
        if(StringUtils.isNotBlank(conditionJson)){
            conditionJson = URLDecoder.decode(conditionJson, "UTF-8");
            List<ConditionVo> conditionList = JSON.parseArray(conditionJson,ConditionVo.class);
            if(conditionList.size()>0){
                for(ConditionVo conditionVo : conditionList){
                    switch (conditionVo.getType()){
                        case "eq": queryWrapper.eq(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "ne": queryWrapper.ne(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "between": queryWrapper.between(conditionVo.getColumn(), conditionVo.getValue() == null ? empty : conditionVo.getValue().split(",")[0],conditionVo.getValue().split(",")[1]);break;
                        case "like": queryWrapper.like(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "leftLike": queryWrapper.likeLeft(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "rightLike": queryWrapper.likeRight(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "notLike": queryWrapper.notLike(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "exists": queryWrapper.exists(conditionVo.getValue());break;
                        case "notExists": queryWrapper.notExists(conditionVo.getValue());break;
                        case "gt": queryWrapper.gt(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "lt": queryWrapper.lt(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "ge": queryWrapper.ge(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "le": queryWrapper.le(conditionVo.getColumn(),conditionVo.getValue());break;
                        case "or": queryWrapper.or();break;
                        case "orLike2":
                            String[] columns = conditionVo.getColumn().split(",");
                            queryWrapper.and(wrapper ->
                                    wrapper.and(swrapper -> swrapper.like(columns[0], conditionVo.getValue()).or().like(columns[1], conditionVo.getValue())));
                            break;
                        case "in": queryWrapper.in(conditionVo.getColumn(), conditionVo.getValue() == null ? empty : conditionVo.getValue().split(","));break;
                        case "notIn": queryWrapper.notIn(conditionVo.getColumn(), conditionVo.getValue() == null ? empty : conditionVo.getValue().split(","));break;
                        case "isNull": queryWrapper.isNull(conditionVo.getColumn());break;
                        case "isNotNull": queryWrapper.isNotNull(conditionVo.getColumn());break;
                        case "orderByAsc": queryWrapper.orderByAsc(conditionVo.getColumn());break;
                        case "orderByDesc": queryWrapper.orderByDesc(conditionVo.getColumn());break;
                        default:
                    }
                }
            }
        }
        return queryWrapper;
    }

}
