package com.github.wz2cool.dynamic.mybatis.mapper.provider;

import com.github.wz2cool.dynamic.DynamicQuery;
import com.github.wz2cool.dynamic.FilterDescriptorBase;
import com.github.wz2cool.dynamic.SortDescriptorBase;
import com.github.wz2cool.dynamic.mybatis.ParamExpression;
import com.github.wz2cool.dynamic.mybatis.QueryHelper;
import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
import com.github.wz2cool.dynamic.mybatis.mapper.helper.DynamicQuerySqlHelper;
import com.github.wz2cool.dynamic.exception.PropertyNotFoundException;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Frank
 * \* Date: 8/8/2017
 * \* Time: 3:13 PM
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class DynamicQueryProvider extends MapperTemplate {
    private final static QueryHelper queryHelper = new QueryHelper();

    public DynamicQueryProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String selectCountByDynamicQuery(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(DynamicQuerySqlHelper.getBindFilterParams());
        sql.append(SqlHelper.selectCount(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(DynamicQuerySqlHelper.getWhereClause());
        return sql.toString();
    }

    public String deleteByDynamicQuery(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(DynamicQuerySqlHelper.getBindFilterParams());
        sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
        sql.append(DynamicQuerySqlHelper.getWhereClause());
        return sql.toString();
    }

    public String selectByDynamicQuery(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(DynamicQuerySqlHelper.getBindFilterParams());
        sql.append("SELECT");
        sql.append(String.format("<if test=\"%s.%s\">distinct</if>",
                MapperConstants.DYNAMIC_QUERY_PARAMS, MapperConstants.DISTINCT));
        //支持查询指定列
        sql.append(DynamicQuerySqlHelper.getSelectColumnsClause());
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(DynamicQuerySqlHelper.getWhereClause());
        sql.append(DynamicQuerySqlHelper.getSortClause());
        return sql.toString();
    }

    public String selectRowBoundsByDynamicQuery(MappedStatement ms) {
        return selectByDynamicQuery(ms);
    }

    public String updateSelectiveByDynamicQuery(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(DynamicQuerySqlHelper.getBindFilterParams());
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
        sql.append(SqlHelper.updateSetColumns(entityClass, "record", true, isNotEmpty()));
        sql.append(DynamicQuerySqlHelper.getWhereClause());
        return sql.toString();
    }

    public String updateByDynamicQuery(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(DynamicQuerySqlHelper.getBindFilterParams());
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
        sql.append(SqlHelper.updateSetColumns(entityClass, "record", false, isNotEmpty()));
        sql.append(DynamicQuerySqlHelper.getWhereClause());
        return sql.toString();
    }

    /// region for xml query.
    // add filterParams prefix
    public static Map<String, Object> getDynamicQueryParamInternal(final DynamicQuery dynamicQuery)
            throws PropertyNotFoundException {
        Class<?> entityClass = dynamicQuery.getEntityClass();
        FilterDescriptorBase[] filters = dynamicQuery.getFilters();
        SortDescriptorBase[] sorts = dynamicQuery.getSorts();
        String[] selectFields = dynamicQuery.getSelectedProperties();

        ParamExpression whereParamExpression = queryHelper.toWhereExpression(entityClass, filters);
        String whereExpression = whereParamExpression.getExpression();
        Map<String, Object> paramMap = whereParamExpression.getParamMap();
        for (Map.Entry<String, Object> param : paramMap.entrySet()) {
            String key = param.getKey();
            String newKey = String.format("%s.%s", MapperConstants.DYNAMIC_QUERY_PARAMS, key);
            whereExpression = whereExpression.replace(key, newKey);
        }
        paramMap.put(MapperConstants.WHERE_EXPRESSION, whereExpression);

        ParamExpression sortExpression = queryHelper.toSortExpression(entityClass, sorts);
        paramMap.put(MapperConstants.SORT_EXPRESSION, sortExpression.getExpression());
        paramMap.put(MapperConstants.DISTINCT, dynamicQuery.isDistinct());

        String selectColumnExpression = queryHelper.toSelectColumnsExpression(entityClass, selectFields);
        paramMap.put(MapperConstants.SELECT_COLUMNS_EXPRESSION, selectColumnExpression);
        return paramMap;
    }
    // endregion
}
@@ -3074,435 +3074,160 @@
     
-Class%3C?%3E entityClass = getEntityClass(ms
+return updateByDynamicQuery(ms, true
 );%0A    
+%7D%0A%0A
     
-StringBuilder sql = new StringBuilder();%0A        sql.append(DynamicQuerySqlHelper.getBindFilterParams());%0A        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), %22example%22));%0A        sql.append(SqlHelper.updateSetColumns(entityClass, %22record%22, true, isNotEmpty()));%0A        sql.append(DynamicQuerySqlHelper.getWhereClause());%0A        return sql.toString(
+public String updateByDynamicQuery(MappedStatement ms) %7B%0A        return updateByDynamicQuery(ms, false
 );%0A 
@@ -3229,37 +3229,38 @@
 e);%0A    %7D%0A%0A    p
-ublic
+rivate
  String updateBy
@@ -3282,32 +3282,48 @@
 ppedStatement ms
+, boolean noNull
 ) %7B%0A        Clas
@@ -3635,21 +3635,22 @@
 ecord%22, 
-false
+noNull
 , isNotE
@@ -3753,24 +3753,25 @@
 g();%0A    %7D%0A%0A
+%0A
     /// regi

remove duplicate code