/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.core.conditions;

import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;

import java.util.Objects;

/**
 * <p>
 * 条件构造抽象类
 * </p>
 *
 * @author hubin
 * @since 2018-05-25
 */
@SuppressWarnings("all")
public abstract class Wrapper<T> implements ISqlSegment {

    /**
     * <p>
     * 实体对象（子类实现）
     * </p>
     *
     * @return 泛型 T
     */
    public abstract T getEntity();

    /**
     * 查询条件 SQL 片段（子类实现）
     */
    public String getSqlSelect() {
        return null;
    }

    /**
     * 更新 SQL 片段（子类实现）
     */
    public String getSqlSet() {
        return null;
    }

    /**
     * 获取 MergeSegments
     */
    public abstract MergeSegments getExpression();

    /**
     * 查询条件为空(包含entity)
     */
    public boolean isEmptyOfWhere() {
        return isEmptyOfNormal() && isEmptyOfEntity();
    }

    /**
     * 查询条件不为空(包含entity)
     */
    public boolean nonEmptyOfWhere() {
        return !isEmptyOfWhere();
    }

    /**
     * 查询条件为空(不包含entity)
     */
    public boolean isEmptyOfNormal() {
        return CollectionUtils.isEmpty(getExpression().getNormal());
    }

    /**
     * 查询条件为空(不包含entity)
     */
    public boolean nonEmptyOfNormal() {
        return !isEmptyOfNormal();
    }

    /**
     * 深层实体判断属性
     *
     * @return true 不为空
     */
    public boolean nonEmptyOfEntity() {
        T entity = getEntity();
        if (entity == null) {
            return false;
        }
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
        if (tableInfo == null) {
            return false;
        }
        if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
            // 有主键
            return Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) &&
                tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
        } else {
            // 无主键
            return tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
        }
    }

    /**
     * 根据实体FieldStrategy属性来决定判断逻辑
     */
    private boolean fieldStrategyMatch(T entity, TableFieldInfo e) {
        switch (e.getFieldStrategy()) {
            case NOT_EMPTY:
                return StringUtils.checkValNotNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
            case NOT_NULL:
                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
            case DEFAULT:
                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
            case IGNORED:
                return true;
            default:
                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));

        }
    }

    /**
     * 深层实体判断属性
     *
     * @return true 为空
     */
    public boolean isEmptyOfEntity() {
        return !nonEmptyOfEntity();
    }
}

@@ -667,16 +667,43 @@
 tions;%0A%0A
+import java.util.Objects;%0A%0A
 import c
@@ -1132,35 +1132,8 @@
 r;%0A%0A
-import java.util.Objects;%0A%0A
 /**%0A
@@ -3212,13 +3212,12 @@
 NOT_
-EMPTY
+NULL
 :%0A  
@@ -3241,31 +3241,19 @@
 urn 
-StringUtils.checkValNot
+Objects.non
 Null
@@ -3322,32 +3322,31 @@
        case 
-NOT_NULL
+IGNORED
 :%0A          
@@ -3362,78 +3362,12 @@
 urn 
-Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()))
+true
 ;%0A  
@@ -3385,15 +3385,17 @@
 ase 
-DEFAULT
+NOT_EMPTY
 :%0A  
@@ -3411,35 +3411,47 @@
      return 
-Objects.non
+StringUtils.checkValNot
 Null(Reflect
@@ -3503,63 +3503,8 @@
 ));%0A
-            case IGNORED:%0A                return true;%0A
     
@@ -3615,17 +3615,16 @@
 ty()));%0A
-%0A
         

优化,调整位置 默认配置为NonNull位置放置最前