package cn.hmsoft.jdbc.entity;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.util.StringUtils;

public class QueryOrder {

	// LinkedHashMap是HashMap的一个子类,保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.
	private LinkedHashMap<String, QueryOrderType> fields = new LinkedHashMap<String, QueryOrderType>();

	public LinkedHashMap<String, QueryOrderType> getFields() {
		return fields;
	}

	/******
	 * 对columnName字段进行默认排序
	 * 
	 * @param columnName
	 */
	public QueryOrder(String columnName) {
		if (!StringUtils.isEmpty(columnName))
			fields.put(columnName, QueryOrderType.ASC);
	}

	/******
	 * 对columnName字段进行orderValue方式排序
	 * 
	 * @param columnName
	 * @param orderValue
	 */
	public QueryOrder(String columnName, QueryOrderType orderValue) {
		fields.put(columnName, orderValue);
	}

	/****
	 * 对column集合进行排序，所有字段排序方式默认
	 * 
	 * @param columnArray
	 */
	public QueryOrder(String[] columnArray) {
		for (String column : columnArray) {
			fields.put(column, QueryOrderType.ASC);
		}
	}

	/****
	 * 对column集合进行排序，所有字段排序方式默认
	 * 
	 * @param columnArray
	 */
	public QueryOrder(List<String> columnList) {
		for (String column : columnList) {
			fields.put(column, QueryOrderType.ASC);
		}
	}

	/******
	 * 根据LinkedHashMap指定的排序字段和方式进行排序
	 * 
	 * @param orderMap
	 */
	public QueryOrder(LinkedHashMap<String, QueryOrderType> orderMap) {
		this.fields = orderMap;
	}

	public enum QueryOrderType {
		ASC("ASC"), DESC("DESC");
		private String value = "";

		private QueryOrderType(String value) {
			this.value = value;
		}

		public String toString() {
			return this.value;
		}

		/*****
		 * 查询排序的方式，默认ASC
		 */
		public enum QueryOrderValue {
			ASC("ASC"), DESC("DESC");
			private String value = "";

			private QueryOrderValue(String value) {
				this.value = value;
			}

			public String toString() {
				return this.value;
			}
		}

	}

}
