| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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;
- @Autowired
- private V_ProductsDao v_productsDao;
- /**
- * 对得到的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.PCB_GOODS_TABLE_NAME)) {
- object = parsePCBGoods(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 if (tableName.equals(SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME)) {
- object = parseProduct(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;
- }
- /**
- * 对product物料进行解析
- *
- * @param id id
- * @param methodType 改动类型
- * @return product物料对象
- * @throws JSONException
- */
- private V_Products parseProduct(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
- V_Products product;
- // delete操作
- // 删除后数据库中可能已经没有相应数据了,无法通过dao获取,需要手动创建对象
- if (methodType.equalsIgnoreCase("delete")) {
- product = new V_Products();
- product.setId(id);
- } else {
- product = v_productsDao.findOne(id);
- }
- return product;
- }
- /**
- * 对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;
- }
- /**
- * 对 PCB 批次 进行解析
- *
- * @param id id
- * @param data 数据
- * @return PCB 批次对象
- * @throws JSONException
- */
- private PCBGoods parsePCBGoods(@NotEmpty("id") Long id, @NotEmpty("data") String data) throws JSONException {
- PCBGoods goods = new PCBGoods();
- if (data == null || data.equalsIgnoreCase("pcbId")) {
- PCB pcb = new PCB();
- pcb.setId(id);
- goods.setPcb(pcb);
- } else if (data.equalsIgnoreCase("goId")){
- TradeGoods tradeGoods = new TradeGoods();
- tradeGoods.setId(id);
- goods.setTradeGoods(tradeGoods);
- } else {
- throw new IllegalArgumentException("未指定是 pcbId 还是 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;
- }
- }
|