package it.integry.integrywmsnative.core.utility;

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

public class UtilityQuery {


    public static String concatFieldListInWhereCond(List<HashMap<String, Object>> inputData) {
        StringBuilder sbWhereCond = new StringBuilder();

        for(int i = 0; i < inputData.size(); i++) {

            HashMap<String, Object> singleInputData = inputData.get(i);
            Object[] keys = singleInputData.keySet().toArray();

            sbWhereCond.append("(");

            for(int k = 0; k < keys.length; k++) {
                Object value = singleInputData.get(keys[k]);

                sbWhereCond.append(keys[k] + (value == null ? " IS NULL" : " = " + UtilityDB.valueToString(value)));

                if(k < keys.length - 1) sbWhereCond.append(" AND ");
            }

            sbWhereCond.append(")");

            if(i < inputData.size() - 1) sbWhereCond.append(" OR ");
        }


        return sbWhereCond.toString();
    }

    public static String concatFieldsInWhereCond(HashMap<String, Object> inputData) {

        StringBuilder sbWhereCond = new StringBuilder();

        HashMap<String, Object> singleInputData = inputData;
        Object[] keys = singleInputData.keySet().toArray();

        sbWhereCond.append("(");

        for(int k = 0; k < keys.length; k++) {
            Object value = singleInputData.get(keys[k]);

            sbWhereCond.append(keys[k] + (value == null ? " IS NULL" : " = " + UtilityDB.valueToString(value)));

            if(k < keys.length - 1) sbWhereCond.append(" AND ");
        }

        sbWhereCond.append(")");



        return sbWhereCond.toString();
    }

}
