package com.uas.search.console.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.document.Document;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.uas.search.console.model.BaseOrderDetailSimpleInfo;
import com.uas.search.console.model.BrandSimpleInfo;
import com.uas.search.console.model.ComponentSimpleInfo;
import com.uas.search.console.model.EnterpriseSimpleInfo;
import com.uas.search.console.model.KindSimpleInfo;
import com.uas.search.console.model.OrderDetailSimpleInfo;
import com.uas.search.console.model.OrderInvoiceDetailSimpleInfo;
import com.uas.search.console.model.OrderInvoiceSimpleInfo;
import com.uas.search.console.model.OrderSimpleInfo;
import com.uas.search.console.model.PurchaseDetailSimpleInfo;
import com.uas.search.console.model.PurchaseInvoiceDetailSimpleInfo;
import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
import com.uas.search.console.model.PurchaseSimpleInfo;
import com.uas.search.model.BaseOrderDetail;

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

	/**
	 * 将Document转换为类目对象
	 * 
	 * @param document
	 * @return
	 */
	public static KindSimpleInfo toKind(Document document) {
		if (document == null) {
			return null;
		}
		KindSimpleInfo kind = new KindSimpleInfo();
		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)));
		return kind;
	}

	/**
	 * 将Document转换为品牌对象
	 * 
	 * @param document
	 * @return
	 */
	public static BrandSimpleInfo toBrand(Document document) {
		if (document == null) {
			return null;
		}
		BrandSimpleInfo brand = new BrandSimpleInfo();
		brand.setId(Long.valueOf(document.get(SearchConstants.BRAND_ID_FIELD)));
		brand.setNameCn(document.get(SearchConstants.BRAND_NAMECN_FIELD));
		brand.setNameEn(document.get(SearchConstants.BRAND_NAMEEN_FIELD));
		brand.setUuid(document.get(SearchConstants.BRAND_UUID_FIELD));
		return brand;
	}

	/**
	 * 将Document转换为器件对象
	 * 
	 * @param document
	 * @return
	 */
	public static ComponentSimpleInfo toComponent(Document document) {
		if (document == null) {
			return null;
		}
		ComponentSimpleInfo component = new ComponentSimpleInfo();
		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));
		component.setKindid(Long.valueOf(document.get(SearchConstants.COMPONENT_KINDID_FIELD)));
		component.setBrandid(Long.valueOf(document.get(SearchConstants.COMPONENT_BRANDID_FIELD)));
		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 OrderSimpleInfo toOrder(Document document) {
		if (document == null) {
			return null;
		}
		OrderSimpleInfo order = new OrderSimpleInfo();
		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));
		EnterpriseSimpleInfo buyerEnterprise = new EnterpriseSimpleInfo();
		buyerEnterprise.setUu(Long.valueOf(document.get(SearchConstants.ORDER_BUYERENUU_FIELD)));
		buyerEnterprise.setEnName(document.get(SearchConstants.ORDER_BUYERENNAME_FIELD));
		order.setBuyerEnterprise(buyerEnterprise);
		EnterpriseSimpleInfo sellerEnterprise = new EnterpriseSimpleInfo();
		sellerEnterprise.setUu(Long.valueOf(document.get(SearchConstants.ORDER_SELLERENUU_FIELD)));
		sellerEnterprise.setEnName(document.get(SearchConstants.ORDER_SELLERENNAME_FIELD));
		order.setSellerEnterprise(sellerEnterprise);
		Date createtime = new Date(Long.valueOf(document.get(SearchConstants.ORDER_CREATETIME_FIELD)));
		order.setCreatetime(createtime);
		order.setStatus(Integer.valueOf(document.get(SearchConstants.ORDER_STATUS_FIELD)));
		order.setDetails(toDetailsSet(document.get(SearchConstants.ORDER_DETAILS_FIELD), OrderDetailSimpleInfo.class));
		return order;
	}

	/**
	 * 将Document转换为销售发货单对象
	 * 
	 * @param document
	 * @return
	 */
	public static OrderInvoiceSimpleInfo toOrderInvoice(Document document) {
		if (document == null) {
			return null;
		}
		OrderInvoiceSimpleInfo orderInvoice = new OrderInvoiceSimpleInfo();
		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));
		EnterpriseSimpleInfo buyerEnterprise = new EnterpriseSimpleInfo();
		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.ORDER_INVOICE_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),
				OrderInvoiceDetailSimpleInfo.class));
		return orderInvoice;
	}

	/**
	 * 将Document转换为采购单对象
	 * 
	 * @param document
	 * @return
	 */
	public static PurchaseSimpleInfo toPurchase(Document document) {
		if (document == null) {
			return null;
		}
		PurchaseSimpleInfo purchase = new PurchaseSimpleInfo();
		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.PURCHASE_CREATETIME_FIELD)));
		purchase.setCreatetime(createtime);
		purchase.setStatus(Integer.valueOf(document.get(SearchConstants.PURCHASE_STATUS_FIELD)));
		purchase.setDetails(
				toDetailsSet(document.get(SearchConstants.PURCHASE_DETAILS_FIELD), PurchaseDetailSimpleInfo.class));
		return purchase;
	}

	/**
	 * 将Document转换为采购发货单对象
	 * 
	 * @param document
	 * @return
	 */
	public static PurchaseInvoiceSimpleInfo toPurchaseInvoice(Document document) {
		if (document == null) {
			return null;
		}
		PurchaseInvoiceSimpleInfo purchaseInvoice = new PurchaseInvoiceSimpleInfo();
		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.PURCHASE_INVOICE_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),
				PurchaseInvoiceDetailSimpleInfo.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<BaseOrderDetailSimpleInfo> details = JSONObject.parseArray(jsonString, BaseOrderDetailSimpleInfo.class);
		List<BaseOrderDetail> result = new ArrayList<>();
		for (BaseOrderDetailSimpleInfo detail : details) {
			result.add(new BaseOrderDetail(detail.getId(), detail.getDetno(), detail.getCode()));
		}
		return result;
	}
}
