package com.uas.search.util;

import com.alibaba.fastjson.JSONObject;
import com.uas.search.constant.SearchConstants;
import com.uas.search.constant.model.BaseOrderDetail;
import com.uas.search.exception.SearchException;
import com.uas.search.model.*;
import org.apache.lucene.document.Document;

import java.util.*;

/**
 * 将Document转换为对象的工具类
 * 
 * @author sunyj
 * @since 2016年10月24日 下午2:24:39
 */
public class DocumentToObjectUtils {

	/**
	 * 将Document转换为对象
	 * 
	 * @param document
	 * @return
	 */
	public static Object toObject(Document document, String tableName) {
		if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
			return toKind(document);
		} else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
			return toBrand(document);
		} else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
			return toComponent(document);
		} else if (tableName.equals(SearchConstants.GOODS_TABLE_NAME)) {
			return toGoods(document);
		} else if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
			return toOrder(document);
		} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
			return toOrderInvoice(document);
		} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
			return toPurchase(document);
		} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
			return toPurchaseInvoice(document);
		}
		throw new SearchException("不支持的表：" + tableName);
	}

	/**
	 * 将Document转换为类目对象
	 * 
	 * @param document
	 * @return
	 */
	public static Kind toKind(Document document) {
		if (document == null) {
			return null;
		}
		Kind kind = new Kind();
		kind.setId(Long.valueOf(document.get(SearchConstants.KIND_ID_FIELD)));
		kind.setNameCn(document.get(SearchConstants.KIND_NAMECN_FIELD));
		kind.setIsLeaf(Short.valueOf(document.get(SearchConstants.KIND_ISLEAF_FIELD)));
		kind.setLevel(Short.valueOf(document.get(SearchConstants.KIND_LEVEL_FIELD)));
        if (!StringUtils.isEmpty(document.get(SearchConstants.KIND_VISIT_COUNT_FIELD))) {
            kind.setVisitCount(Long.valueOf(document.get(SearchConstants.KIND_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.KIND_SEARCH_COUNT_FIELD))) {
            kind.setSearchCount(Long.valueOf(document.get(SearchConstants.KIND_SEARCH_COUNT_FIELD)));
        }
		return kind;
	}

	/**
	 * 将Document转换为品牌对象
	 * 
	 * @param document
	 * @return
	 */
	public static Brand toBrand(Document document) {
		if (document == null) {
			return null;
		}
		Brand brand = new Brand();
		brand.setId(Long.valueOf(document.get(SearchConstants.BRAND_ID_FIELD)));
		brand.setNameCn(document.get(SearchConstants.BRAND_NAMECN_FIELD));
		brand.setUuid(document.get(SearchConstants.BRAND_UUID_FIELD));
        if (!StringUtils.isEmpty(document.get(SearchConstants.BRAND_NAMEEN_FIELD))) {
            brand.setNameEn(document.get(SearchConstants.BRAND_NAMEEN_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.BRAND_VISIT_COUNT_FIELD))) {
            brand.setVisitCount(Long.valueOf(document.get(SearchConstants.BRAND_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.BRAND_SEARCH_COUNT_FIELD))) {
            brand.setSearchCount(Long.valueOf(document.get(SearchConstants.BRAND_SEARCH_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.BRAND_WEIGHT_FIELD))) {
            brand.setWeight(Double.valueOf(document.get(SearchConstants.BRAND_WEIGHT_FIELD)));
        }
		return brand;
	}

	/**
	 * 将Document转换为器件对象
	 * 
	 * @param document
	 * @return
	 */
	public static Component toComponent(Document document) {
		if (document == null) {
			return null;
		}
		Component component = new Component();
		component.setId(Long.valueOf(document.get(SearchConstants.COMPONENT_ID_FIELD)));
		component.setCode(document.get(SearchConstants.COMPONENT_CODE_FIELD));
		component.setUuid(document.get(SearchConstants.COMPONENT_UUID_FIELD));
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_VISIT_COUNT_FIELD))) {
            component.setVisitCount(Long.valueOf(document.get(SearchConstants.COMPONENT_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD))) {
            component.setSearchCount(Long.valueOf(document.get(SearchConstants.COMPONENT_SEARCH_COUNT_FIELD)));
        }

		Kind kind = new Kind();
		kind.setId(Long.valueOf(document.get(SearchConstants.COMPONENT_KI_ID_FIELD)));
		kind.setNameCn(document.get(SearchConstants.COMPONENT_KI_NAME_FIELD));
		kind.setLevel(Short.valueOf(document.get(SearchConstants.COMPONENT_KI_LEVEL_FIELD)));
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD))) {
            kind.setVisitCount(Long.valueOf(document.get(SearchConstants.COMPONENT_KI_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD))) {
            kind.setSearchCount(Long.valueOf(document.get(SearchConstants.COMPONENT_KI_SEARCH_COUNT_FIELD)));
        }
		component.setKind(kind);

		Brand brand = new Brand();
		brand.setId(Long.valueOf(document.get(SearchConstants.COMPONENT_BR_ID_FIELD)));
		brand.setNameCn(document.get(SearchConstants.COMPONENT_BR_NAMECN_FIELD));
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_BR_NAMEEN_FIELD))) {
            brand.setNameEn(document.get(SearchConstants.COMPONENT_BR_NAMEEN_FIELD));
        }
		brand.setUuid(document.get(SearchConstants.COMPONENT_BR_UUID_FIELD));
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD))) {
            brand.setVisitCount(Long.valueOf(document.get(SearchConstants.COMPONENT_BR_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD))) {
            brand.setSearchCount(Long.valueOf(document.get(SearchConstants.COMPONENT_BR_SEARCH_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_BR_WEIGHT_FIELD))) {
            brand.setWeight(Double.valueOf(document.get(SearchConstants.COMPONENT_BR_WEIGHT_FIELD)));
        }
		component.setBrand(brand);

		if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_RESERVE_FIELD))) {
			component.setReserve(Double.valueOf(document.get(SearchConstants.COMPONENT_RESERVE_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_SAMPLE_QTY_FIELD))) {
			component.setSampleQty(Double.valueOf(document.get(SearchConstants.COMPONENT_SAMPLE_QTY_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_ORIGINAL_QTY_FIELD))) {
			component.setOriginalQty(Double.valueOf(document.get(SearchConstants.COMPONENT_ORIGINAL_QTY_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.COMPONENT_INACTION_STOCK_QTY_FIELD))) {
			component.setInactionStockQty(
					Double.valueOf(document.get(SearchConstants.COMPONENT_INACTION_STOCK_QTY_FIELD)));
		}
		return component;
	}

	/**
	 * 将Document转换为批次对象
	 * 
	 * @param document
	 * @return
	 */
	public static Goods toGoods(Document document) {
		if (document == null) {
			return null;
		}
		Goods goods = new Goods();
		TradeGoods tradeGoods = new TradeGoods();
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_ID_FIELD))) {
			tradeGoods.setId(Long.valueOf(document.get(SearchConstants.GOODS_GO_ID_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD))) {
			tradeGoods.setReserve(Double.valueOf(document.get(SearchConstants.GOODS_GO_RESERVE_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_STATUS_FIELD))) {
			tradeGoods.setStatus(Long.valueOf(document.get(SearchConstants.GOODS_GO_STATUS_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_MINPRICERMB_FIELD))) {
			tradeGoods.setMinPriceRMB(Double.valueOf(document.get(SearchConstants.GOODS_GO_MINPRICERMB_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD))) {
			tradeGoods.setMinPriceUSD(Double.valueOf(document.get(SearchConstants.GOODS_GO_MINPRICEUSD_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CRNAME_FIELD))) {
			tradeGoods.setCrName(document.get(SearchConstants.GOODS_CRNAME_FIELD));
		}
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD))) {
            tradeGoods.setVisitCount(Long.valueOf(document.get(SearchConstants.GOODS_GO_VISIT_COUNT_FIELD)));
        }
		goods.setTradeGoods(tradeGoods);

		Store store = new Store();
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_ST_UUID_FIELD))) {
			store.setUuid(document.get(SearchConstants.GOODS_ST_UUID_FIELD));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_ST_TYPE_FIELD))) {
			store.setType(document.get(SearchConstants.GOODS_ST_TYPE_FIELD));
		}
		goods.setStore(store);

		Component component = new Component();
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_ID_FIELD))) {
            component.setId(Long.valueOf(document.get(SearchConstants.GOODS_CMP_ID_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_CODE_FIELD))) {
            component.setCode(document.get(SearchConstants.GOODS_CMP_CODE_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD))) {
            component.setDescription(document.get(SearchConstants.GOODS_CMP_DESCRIPTION_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD))) {
            component.setVisitCount(Long.valueOf(document.get(SearchConstants.GOODS_CMP_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD))) {
            component.setSearchCount(Long.valueOf(document.get(SearchConstants.GOODS_CMP_SEARCH_COUNT_FIELD)));
        }

		Kind kind = new Kind();
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_ID_FIELD))) {
			kind.setId(Long.valueOf(document.get(SearchConstants.GOODS_KI_ID_FIELD)));
		}
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_NAME_CN_FIELD))) {
		    kind.setNameCn(document.get(SearchConstants.GOODS_KI_NAME_CN_FIELD));
        }
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_LEVEL_FIELD))) {
			kind.setLevel(Short.valueOf(document.get(SearchConstants.GOODS_KI_LEVEL_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_ISLEAF_FIELD))) {
			kind.setIsLeaf(Short.valueOf(document.get(SearchConstants.GOODS_KI_ISLEAF_FIELD)));
		}
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD))) {
            kind.setVisitCount(Long.valueOf(document.get(SearchConstants.GOODS_KI_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD))) {
            kind.setSearchCount(Long.valueOf(document.get(SearchConstants.GOODS_KI_SEARCH_COUNT_FIELD)));
        }
		component.setKind(kind);

		Brand brand = new Brand();
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_ID_FIELD))) {
			brand.setId(Long.valueOf(document.get(SearchConstants.GOODS_BR_ID_FIELD)));
		}
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_NAME_CN_FIELD))) {
		    brand.setNameCn(document.get(SearchConstants.GOODS_BR_NAME_CN_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_UUID_FIELD))) {
		    brand.setUuid(document.get(SearchConstants.GOODS_BR_UUID_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_NAME_EN_FIELD))) {
            brand.setNameEn(document.get(SearchConstants.GOODS_BR_NAME_EN_FIELD));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD))) {
            brand.setVisitCount(Long.valueOf(document.get(SearchConstants.GOODS_BR_VISIT_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD))) {
            brand.setSearchCount(Long.valueOf(document.get(SearchConstants.GOODS_BR_SEARCH_COUNT_FIELD)));
        }
        if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_BR_WEIGHT_FIELD))) {
            brand.setWeight(Double.valueOf(document.get(SearchConstants.GOODS_BR_WEIGHT_FIELD)));
        }
		component.setBrand(brand);
		goods.setComponent(component);

		Products products = new Products();
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_PR_ID_FIELD))) {
			products.setId(Long.valueOf(document.get(SearchConstants.GOODS_PR_ID_FIELD)));
		}
		if (!StringUtils.isEmpty(document.get(SearchConstants.GOODS_PR_PCMPCODE_FIELD))) {
			products.setPcmpCode(document.get(SearchConstants.GOODS_PR_PCMPCODE_FIELD));
		}
		goods.setProducts(products);
		return goods;
	}

	/**
	 * 将Document转换为销售单对象
	 * 
	 * @param document
	 * @return
	 */
	public static Order toOrder(Document document) {
		if (document == null) {
			return null;
		}
		Order order = new Order();
		order.setId(Long.valueOf(document.get(SearchConstants.ORDER_ID_FIELD)));
		order.setCode(document.get(SearchConstants.ORDER_CODE_FIELD));
		order.setBuyeruu(Long.valueOf(document.get(SearchConstants.ORDER_BUYERUU_FIELD)));
		order.setBuyername(document.get(SearchConstants.ORDER_BUYERNAME_FIELD));
		Enterprise buyerEnterprise = new Enterprise();
		buyerEnterprise.setUu(Long.valueOf(document.get(SearchConstants.ORDER_BUYERENUU_FIELD)));
		buyerEnterprise.setEnName(document.get(SearchConstants.ORDER_BUYERENNAME_FIELD));
		order.setBuyerEnterprise(buyerEnterprise);
		Enterprise sellerEnterprise = new Enterprise();
		if (document.get(SearchConstants.ORDER_SELLERENUU_FIELD) != null) {
			sellerEnterprise.setUu(Long.valueOf(document.get(SearchConstants.ORDER_SELLERENUU_FIELD)));
		}
		if (document.get(SearchConstants.ORDER_SELLERENNAME_FIELD) != null) {
			sellerEnterprise.setEnName(document.get(SearchConstants.ORDER_SELLERENNAME_FIELD));
		}
		order.setSellerEnterprise(sellerEnterprise);
		Date createtime = new Date(Long.valueOf(document.get(SearchConstants.CREATETIME_FIELD)));
		order.setCreatetime(createtime);
		order.setStatus(Integer.valueOf(document.get(SearchConstants.ORDER_STATUS_FIELD)));
		order.setDetails(toDetailsSet(document.get(SearchConstants.ORDER_DETAILS_FIELD), OrderDetail.class));
		return order;
	}

	/**
	 * 将Document转换为销售发货单对象
	 * 
	 * @param document
	 * @return
	 */
	public static OrderInvoice toOrderInvoice(Document document) {
		if (document == null) {
			return null;
		}
		OrderInvoice orderInvoice = new OrderInvoice();
		orderInvoice.setId(Long.valueOf(document.get(SearchConstants.ORDER_INVOICE_ID_FIELD)));
		orderInvoice.setCode(document.get(SearchConstants.ORDER_INVOICE_CODE_FIELD));
		orderInvoice.setBuyeruu(Long.valueOf(document.get(SearchConstants.ORDER_INVOICE_BUYERUU_FIELD)));
		orderInvoice.setBuyername(document.get(SearchConstants.ORDER_INVOICE_BUYERNAME_FIELD));
		Enterprise buyerEnterprise = new Enterprise();
		buyerEnterprise.setUu(Long.valueOf(document.get(SearchConstants.ORDER_INVOICE_BUYERENUU_FIELD)));
		buyerEnterprise.setEnName(document.get(SearchConstants.ORDER_INVOICE_BUYERENNAME_FIELD));
		orderInvoice.setBuyerEnterprise(buyerEnterprise);
		Date createtime = new Date(Long.valueOf(document.get(SearchConstants.CREATETIME_FIELD)));
		orderInvoice.setCreatetime(createtime);
		orderInvoice.setStatus(Integer.valueOf(document.get(SearchConstants.ORDER_INVOICE_STATUS_FIELD)));
		orderInvoice.setDetails(toDetailsSet(document.get(SearchConstants.ORDER_INVOICE_DETAILS_FIELD),
				OrderInvoiceDetail.class));
		return orderInvoice;
	}

	/**
	 * 将Document转换为采购单对象
	 * 
	 * @param document
	 * @return
	 */
	public static Purchase toPurchase(Document document) {
		if (document == null) {
			return null;
		}
		Purchase purchase = new Purchase();
		purchase.setId(Long.valueOf(document.get(SearchConstants.PURCHASE_ID_FIELD)));
		purchase.setCode(document.get(SearchConstants.PURCHASE_CODE_FIELD));
		purchase.setSellerenuu(Long.valueOf(document.get(SearchConstants.PURCHASE_SELLERENUU_FIELD)));
		purchase.setSellerenname(document.get(SearchConstants.PURCHASE_SELLERENNAME_FIELD));
		Date createtime = new Date(Long.valueOf(document.get(SearchConstants.CREATETIME_FIELD)));
		purchase.setCreatetime(createtime);
		purchase.setStatus(Integer.valueOf(document.get(SearchConstants.PURCHASE_STATUS_FIELD)));
		purchase.setDetails(
				toDetailsSet(document.get(SearchConstants.PURCHASE_DETAILS_FIELD), PurchaseDetail.class));
		return purchase;
	}

	/**
	 * 将Document转换为采购发货单对象
	 * 
	 * @param document
	 * @return
	 */
	public static PurchaseInvoice toPurchaseInvoice(Document document) {
		if (document == null) {
			return null;
		}
		PurchaseInvoice purchaseInvoice = new PurchaseInvoice();
		purchaseInvoice.setId(Long.valueOf(document.get(SearchConstants.PURCHASE_INVOICE_ID_FIELD)));
		purchaseInvoice.setCode(document.get(SearchConstants.PURCHASE_INVOICE_CODE_FIELD));
		purchaseInvoice.setSellerenuu(Long.valueOf(document.get(SearchConstants.PURCHASE_INVOICE_SELLERENUU_FIELD)));
		purchaseInvoice.setSellerenname(document.get(SearchConstants.PURCHASE_INVOICE_SELLERENNAME_FIELD));
		Date createtime = new Date(Long.valueOf(document.get(SearchConstants.CREATETIME_FIELD)));
		purchaseInvoice.setCreatetime(createtime);
		purchaseInvoice.setStatus(Integer.valueOf(document.get(SearchConstants.PURCHASE_INVOICE_STATUS_FIELD)));
		purchaseInvoice.setDetails(toDetailsSet(document.get(SearchConstants.PURCHASE_INVOICE_DETAILS_FIELD),
				PurchaseInvoiceDetail.class));
		return purchaseInvoice;
	}

	/**
	 * 将json字符串转为明细对象
	 * 
	 * @param <T>
	 * 
	 * @param jsonString
	 *            索引中存储的明细数据
	 * @return 明细对象
	 */
	public static <T> Set<T> toDetailsSet(String jsonString, Class<T> clazz) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}
		Set<T> details = new HashSet<>();
		details.addAll(JSONObject.parseArray(jsonString, clazz));
		return details;
	}

	/**
	 * 将json字符串转为明细对象
	 * 
	 * @param jsonString
	 *            索引中存储的明细数据
	 * @return 明细对象
	 */
	public static List<BaseOrderDetail> toDetailsList(String jsonString) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}
		List<com.uas.search.model.BaseOrderDetail> details = JSONObject.parseArray(jsonString, com.uas.search.model.BaseOrderDetail.class);
		List<BaseOrderDetail> result = new ArrayList<>();
		for (com.uas.search.model.BaseOrderDetail detail : details) {
			result.add(new BaseOrderDetail(detail.getId(), detail.getDetno(), detail.getCode()));
		}
		return result;
	}
}
