package com.uas.search.util;

import java.util.ArrayList;
import java.util.List;

import com.uas.search.constant.SearchConstants;
import com.uas.search.exception.SearchException;

/**
 * 单据搜索相关的工具类
 * 
 * @author sunyj
 * @since 2016年10月18日 下午5:58:42
 */
public class OrderSearchUtils {

	/**
	 * 获取每个表在搜索关键词时，所能搜索的索引域
	 * 
	 * @param tableName
	 *            表名
	 * @return 域的列表
	 */
	public static List<String> getKeywordFields(String tableName) {
		List<String> keywordFields = new ArrayList<>();
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			keywordFields.add(SearchConstants.ORDER_CODE_FIELD);
			keywordFields.add(SearchConstants.ORDER_BUYERNAME_FIELD);
			keywordFields.add(SearchConstants.ORDER_BUYERENNAME_FIELD);
			keywordFields.add(SearchConstants.ORDER_SELLERENNAME_FIELD);
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			keywordFields.add(SearchConstants.ORDER_INVOICE_CODE_FIELD);
			keywordFields.add(SearchConstants.ORDER_INVOICE_BUYERNAME_FIELD);
			keywordFields.add(SearchConstants.ORDER_INVOICE_BUYERENNAME_FIELD);
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			keywordFields.add(SearchConstants.PURCHASE_CODE_FIELD);
			keywordFields.add(SearchConstants.PURCHASE_SELLERENNAME_FIELD);
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			keywordFields.add(SearchConstants.PURCHASE_INVOICE_CODE_FIELD);
			keywordFields.add(SearchConstants.PURCHASE_INVOICE_SELLERENNAME_FIELD);
		} else {
			throw new SearchException("表不存在：" + tableName);
		}
		return keywordFields;
	}

	/**
	 * 获取每个表在索引中的id域
	 * 
	 * @param tableName
	 *            表名
	 * @return id域
	 */
	public static String getIdField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_ID_FIELD;
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return SearchConstants.ORDER_INVOICE_ID_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_ID_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_INVOICE_ID_FIELD;
		} else {
			throw new SearchException("表不存在：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的code域
	 * 
	 * @param tableName
	 *            表名
	 * @return code域
	 */
	public static String getCodeField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_CODE_FIELD;
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return SearchConstants.ORDER_INVOICE_CODE_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_CODE_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_INVOICE_CODE_FIELD;
		} else {
			throw new SearchException("表不存在：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的status域
	 * 
	 * @param tableName
	 *            表名
	 * @return status域
	 */
	public static String getStatusField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_STATUS_FIELD;
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return SearchConstants.ORDER_INVOICE_STATUS_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_STATUS_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_INVOICE_STATUS_FIELD;
		} else {
			throw new SearchException("表不存在：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的明细details域
	 * 
	 * @param tableName
	 *            表名
	 * @return details域
	 */
	public static String getDetailsField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_DETAILS_FIELD;
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return SearchConstants.ORDER_INVOICE_DETAILS_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_DETAILS_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_INVOICE_DETAILS_FIELD;
		} else {
			throw new SearchException("表不存在：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的buyeruu域
	 * 
	 * @param tableName
	 *            表名
	 * @return buyeruu域
	 */
	public static String getBuyeruuField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_BUYERUU_FIELD;
		} else {
			throw new SearchException("表中不存在买方个人uu：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的buyerenuu域
	 * 
	 * @param tableName
	 *            表名
	 * @return buyerenuu域
	 */
	public static String getBuyerenuuField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_BUYERENUU_FIELD;
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return SearchConstants.ORDER_INVOICE_BUYERENUU_FIELD;
		} else {
			throw new SearchException("表中不存在买方企业uu：" + tableName);
		}
	}

	/**
	 * 获取每个表在索引中的sellerenuu域
	 * 
	 * @param tableName
	 *            表名
	 * @return sellerenuu域
	 */
	public static String getSellerenuuField(String tableName) {
		if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return SearchConstants.ORDER_SELLERENUU_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_SELLERENUU_FIELD;
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return SearchConstants.PURCHASE_INVOICE_SELLERENUU_FIELD;
		} else {
			throw new SearchException("表中不存在卖方企业uu：" + tableName);
		}
	}

}
