package com.uas.search.jms;

import com.alibaba.fastjson.JSONException;
import com.uas.search.annotation.NotEmpty;
import com.uas.search.constant.SearchConstants;
import com.uas.search.dao.*;
import com.uas.search.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 对得到的队列消息进行解析的工具
 * 
 * @author sunyj
 * @since 2016年7月7日 下午6:14:03
 */
@Service
public class QueueMessageParser {

	@Autowired
	private KindDao kindDao;

	@Autowired
	private BrandDao brandDao;

	@Autowired
	private ComponentDao componentDao;

	@Autowired
	private OrderDao orderDao;

	@Autowired
	private OrderInvoiceDao orderInvoiceDao;

	@Autowired
	private PurchaseDao purchaseDao;

	@Autowired
	private PurchaseInvoiceDao purchaseInvoiceDao;

	/**
	 * 对得到的json消息进行解析
	 * 
	 * @param tableName 表名
	 * @param dataId 数据 id
	 * @param methodType 更改类型
	 * @param data 数据
	 * @return ParsedQueueMessage对象
	 * @throws JSONException
	 */
	public ParsedQueueMessage parse(@NotEmpty("tableName") String tableName, @NotEmpty("dataId") Long dataId, @NotEmpty("methodType") String methodType, String data) throws JSONException {
		ParsedQueueMessage parsedQueueMessage = new ParsedQueueMessage();
		Object object;
		try {
			// 解析数据库表的更改类型
			if (methodType.equals("insert")) {
				parsedQueueMessage.setMethodType(ParsedQueueMessage.INSERT);
			} else if (methodType.equals("update")) {
				parsedQueueMessage.setMethodType(ParsedQueueMessage.UPDATE);
			} else if (methodType.equals("delete")) {
				parsedQueueMessage.setMethodType(ParsedQueueMessage.DELETE);
			} else {
				throw new IllegalStateException("unsupported method type: " + methodType);
			}

			// 解析哪个表有更改
			if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
				object = parseKind(dataId, methodType);
			} else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
				object = parseBrand(dataId, methodType);
			} else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
				object = parseComponent(dataId, methodType);
			} else if (tableName.equals(SearchConstants.GOODS_TABLE_NAME)) {
				object = parseGoods(dataId, data);
			} else if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
				object = parseOrder(dataId, methodType);
			} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
				object = parseOrderInvoice(dataId, methodType);
			} else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
				object = parsePurchase(dataId, methodType);
			} else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
				object = parsePurchaseInvoice(dataId, methodType);
			} else {
				throw new IllegalStateException("unsupported table name: " + tableName);
			}
		} catch (Throwable e) {
			// 防止SQLRecoverableException导致应用终止
			throw new IllegalStateException("message parsing failed!", e);
		}

		parsedQueueMessage.setObject(object);
		return parsedQueueMessage;
	}

	/**
	 * 对kind类目进行解析
	 * 
	 * @param id id
	 * @param methodType 改动类型
	 * @return kind类目对象
	 * @throws JSONException
	 */
	private Kind parseKind(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		Kind kind;
		// delete操作
		// 删除后数据库中可能已经没有相应数据了，无法通过dao获取，需要手动创建对象
		if (methodType.equalsIgnoreCase("delete")) {
			kind = new Kind();
			kind.setId(id);
		} else {
			kind = kindDao.findOne(id);
		}
		return kind;
	}

	/**
	 * 对brand品牌进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return brand品牌对象
	 * @throws JSONException
	 */
	private Brand parseBrand(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		Brand brand;
		if (methodType.equalsIgnoreCase("delete")) {
			brand = new Brand();
			brand.setId(id);
		} else {
			brand = brandDao.findOne(id);
		}
		return brand;
	}

	/**
	 * 对component器件进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return component器件对象
	 * @throws JSONException
	 */
	private Component parseComponent(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		Component component;
		if (methodType.equalsIgnoreCase("delete")) {
			component = new Component();
			component.setId(id);
		} else {
			component = componentDao.findOne(id);
		}
		return component;
	}

	/**
	 * 对goods进行解析
	 *
	 * @param id id
	 * @param data 数据
	 * @return 批次对象
	 * @throws JSONException
	 */
	private Goods parseGoods(@NotEmpty("id") Long id, @NotEmpty("data") String data) throws JSONException {
		Goods goods = new Goods();
		if (data == null || data.equalsIgnoreCase("cmpId")) {
            Component component = new Component();
            component.setId(id);
            goods.setComponent(component);
		} else if (data.equalsIgnoreCase("goId")){
            TradeGoods tradeGoods = new TradeGoods();
            tradeGoods.setId(id);
			goods.setTradeGoods(tradeGoods);
		} else {
            throw new IllegalArgumentException("未指定是 cmpId 还是 goId");
        }
		return goods;
	}

	/**
	 * 对销售单进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return 销售单对象
	 * @throws JSONException
	 */
	private Order parseOrder(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		Order order;
		if (methodType.equalsIgnoreCase("delete")) {
			order = new Order();
			order.setId(id);
		} else {
			order = orderDao.findOne(id);
		}
		return order;
	}

	/**
	 * 对销售发货单进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return 销售发货单对象
	 * @throws JSONException
	 */
	private OrderInvoice parseOrderInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		OrderInvoice orderInvoice;
		if (methodType.equalsIgnoreCase("delete")) {
			orderInvoice = new OrderInvoice();
			orderInvoice.setId(id);
		} else {
			orderInvoice = orderInvoiceDao.findOne(id);
		}
		return orderInvoice;
	}

	/**
	 * 对采购单进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return 采购单对象
	 * @throws JSONException
	 */
	private Purchase parsePurchase(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		Purchase purchase;
		if (methodType.equalsIgnoreCase("delete")) {
			purchase = new Purchase();
			purchase.setId(id);
		} else {
			purchase = purchaseDao.findOne(id);
		}
		return purchase;
	}

	/**
	 * 对采购发货单进行解析
	 *
	 * @param id id
	 * @param methodType 改动类型
	 * @return 采购发货单对象
	 * @throws JSONException
	 */
	private PurchaseInvoice parsePurchaseInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
		PurchaseInvoice purchaseInvoice;
		if (methodType.equalsIgnoreCase("delete")) {
			purchaseInvoice = new PurchaseInvoice();
			purchaseInvoice.setId(id);
		} else {
			purchaseInvoice = purchaseInvoiceDao.findOne(id);
		}
		return purchaseInvoice;
	}
}
