package com.dangdang.ddframe.rdb.sharding.parsing.parser.context;

import com.dangdang.ddframe.rdb.sharding.constant.ShardingOperator;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLNumberExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLPlaceholderExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLTextExpression;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.LinkedList;
import java.util.List;

/**
 * 条件对象.
 *
 * @author zhangliang
 */
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
public final class Condition {
    
    @Getter
    private final Column column;
    
    @Getter
    private final ShardingOperator operator;
    
    private final List<Comparable<?>> values = new LinkedList<>();
    
    private final List<Integer> indices = new LinkedList<>();
    
    // TODO 增加index位置的list
    
    public Condition(final Column column, final SQLExpression sqlExpression) {
        this(column, ShardingOperator.EQUAL);
        init(sqlExpression);
    }
    
    public Condition(final Column column, final SQLExpression beginSQLExpression, final SQLExpression endSQLExpression) {
        this(column, ShardingOperator.BETWEEN);
        init(beginSQLExpression);
        init(endSQLExpression);
    }
    
    public Condition(final Column column, final List<SQLExpression> sqlExpressions) {
        this(column, ShardingOperator.IN);
        for (SQLExpression each : sqlExpressions) {
            init(each);
        }
    }
    
    private void init(final SQLExpression sqlExpression) {
        if (sqlExpression instanceof SQLPlaceholderExpression) {
            indices.add(((SQLPlaceholderExpression) sqlExpression).getIndex());
        } else if (sqlExpression instanceof SQLTextExpression) {
            values.add(((SQLTextExpression) sqlExpression).getText());
        } else if (sqlExpression instanceof SQLNumberExpression) {
            values.add((Comparable) ((SQLNumberExpression) sqlExpression).getNumber());
        }
    }
    
    /**
     * 获取分片值.
     *
     * @param parameters 参数列表
     * @return 分片值
     */
    public List<Comparable<?>> getValues(final List<Object> parameters) {
        List<Comparable<?>> result = new LinkedList<>(values);
        for (int each : indices) {
            Object parameter = parameters.get(each);
            if (parameter instanceof Comparable<?>) {
                result.add((Comparable<?>) parameter);
            }
        }
        return result;
    }
}

@@ -626,35 +626,117 @@
 nked
-List;%0Aimport java.util.List
+HashMap;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry
 ;%0A%0A/
@@ -1016,21 +1016,29 @@
 e final 
-List%3C
+Map%3CInteger, 
 Comparab
@@ -1044,22 +1044,32 @@
 ble%3C?%3E%3E 
-v
+positionV
 alue
-s
+Map
  = new L
@@ -1065,36 +1065,39 @@
 Map = new Linked
-List
+HashMap
 %3C%3E();%0A    %0A    p
@@ -1113,20 +1113,19 @@
 nal 
-List
+Map
 %3CInteger
 %3E in
@@ -1124,17 +1124,35 @@
 eger
-%3E indices
+, Integer%3E positionIndexMap
  = n
@@ -1160,20 +1160,23 @@
 w Linked
-List
+HashMap
 %3C%3E();%0A  
@@ -1353,32 +1353,35 @@
 it(sqlExpression
+, 0
 );%0A    %7D%0A    %0A  
@@ -1575,24 +1575,27 @@
 QLExpression
+, 0
 );%0A        i
@@ -1614,16 +1614,19 @@
 pression
+, 1
 );%0A    %7D
@@ -1756,24 +1756,47 @@
 erator.IN);%0A
+        int count = 0;%0A
         for 
@@ -1852,27 +1852,55 @@
    init(each
+, count
 );%0A
+            count++;%0A
         %7D%0A  
@@ -1955,32 +1955,52 @@
 on sqlExpression
+, final int position
 ) %7B%0A        if (
@@ -2068,20 +2068,39 @@
     
-indices.add(
+positionIndexMap.put(position, 
 ((SQ
@@ -2228,27 +2228,47 @@
         
-values.add(
+positionValueMap.put(position, 
 ((SQLTex
@@ -2390,19 +2390,39 @@
     
-values.add(
+positionValueMap.put(position, 
 (Com
@@ -2721,14 +2721,33 @@
 t%3C%3E(
+positionValueMap.
 values
+()
 );%0A 
@@ -2762,26 +2762,67 @@
 or (
-int each : indices
+Entry%3CInteger, Integer%3E entry : positionIndexMap.entrySet()
 ) %7B%0A
@@ -2868,19 +2868,31 @@
 rs.get(e
-ach
+ntry.getValue()
 );%0A     
@@ -2901,16 +2901,18 @@
      if 
+(!
 (paramet
@@ -2939,16 +2939,199 @@
 able%3C?%3E)
+) %7B%0A                continue;%0A            %7D%0A            if (entry.getKey() %3C result.size()) %7B%0A                result.add(entry.getKey(), (Comparable%3C?%3E) parameter);%0A            %7D else
  %7B%0A     

add position for getValue from Condition