QueueMessageParser.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package com.uas.search.jms;
  2. import com.alibaba.fastjson.JSONException;
  3. import com.uas.search.annotation.NotEmpty;
  4. import com.uas.search.constant.SearchConstants;
  5. import com.uas.search.dao.*;
  6. import com.uas.search.model.*;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. /**
  10. * 对得到的队列消息进行解析的工具
  11. *
  12. * @author sunyj
  13. * @since 2016年7月7日 下午6:14:03
  14. */
  15. @Service
  16. public class QueueMessageParser {
  17. @Autowired
  18. private KindDao kindDao;
  19. @Autowired
  20. private BrandDao brandDao;
  21. @Autowired
  22. private ComponentDao componentDao;
  23. @Autowired
  24. private OrderDao orderDao;
  25. @Autowired
  26. private OrderInvoiceDao orderInvoiceDao;
  27. @Autowired
  28. private PurchaseDao purchaseDao;
  29. @Autowired
  30. private PurchaseInvoiceDao purchaseInvoiceDao;
  31. /**
  32. * 对得到的json消息进行解析
  33. *
  34. * @param tableName 表名
  35. * @param dataId 数据 id
  36. * @param methodType 更改类型
  37. * @param data 数据
  38. * @return ParsedQueueMessage对象
  39. * @throws JSONException
  40. */
  41. public ParsedQueueMessage parse(@NotEmpty("tableName") String tableName, @NotEmpty("dataId") Long dataId, @NotEmpty("methodType") String methodType, String data) throws JSONException {
  42. ParsedQueueMessage parsedQueueMessage = new ParsedQueueMessage();
  43. Object object;
  44. try {
  45. // 解析数据库表的更改类型
  46. if (methodType.equals("insert")) {
  47. parsedQueueMessage.setMethodType(ParsedQueueMessage.INSERT);
  48. } else if (methodType.equals("update")) {
  49. parsedQueueMessage.setMethodType(ParsedQueueMessage.UPDATE);
  50. } else if (methodType.equals("delete")) {
  51. parsedQueueMessage.setMethodType(ParsedQueueMessage.DELETE);
  52. } else {
  53. throw new IllegalStateException("unsupported method type: " + methodType);
  54. }
  55. // 解析哪个表有更改
  56. if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
  57. object = parseKind(dataId, methodType);
  58. } else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
  59. object = parseBrand(dataId, methodType);
  60. } else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
  61. object = parseComponent(dataId, methodType);
  62. } else if (tableName.equals(SearchConstants.GOODS_TABLE_NAME)) {
  63. object = parseGoods(dataId, data);
  64. } else if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
  65. object = parseOrder(dataId, methodType);
  66. } else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
  67. object = parseOrderInvoice(dataId, methodType);
  68. } else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
  69. object = parsePurchase(dataId, methodType);
  70. } else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
  71. object = parsePurchaseInvoice(dataId, methodType);
  72. } else {
  73. throw new IllegalStateException("unsupported table name: " + tableName);
  74. }
  75. } catch (Throwable e) {
  76. // 防止SQLRecoverableException导致应用终止
  77. throw new IllegalStateException("message parsing failed!", e);
  78. }
  79. parsedQueueMessage.setObject(object);
  80. return parsedQueueMessage;
  81. }
  82. /**
  83. * 对kind类目进行解析
  84. *
  85. * @param id id
  86. * @param methodType 改动类型
  87. * @return kind类目对象
  88. * @throws JSONException
  89. */
  90. private Kind parseKind(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  91. Kind kind;
  92. // delete操作
  93. // 删除后数据库中可能已经没有相应数据了,无法通过dao获取,需要手动创建对象
  94. if (methodType.equalsIgnoreCase("delete")) {
  95. kind = new Kind();
  96. kind.setId(id);
  97. } else {
  98. kind = kindDao.findOne(id);
  99. }
  100. return kind;
  101. }
  102. /**
  103. * 对brand品牌进行解析
  104. *
  105. * @param id id
  106. * @param methodType 改动类型
  107. * @return brand品牌对象
  108. * @throws JSONException
  109. */
  110. private Brand parseBrand(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  111. Brand brand;
  112. if (methodType.equalsIgnoreCase("delete")) {
  113. brand = new Brand();
  114. brand.setId(id);
  115. } else {
  116. brand = brandDao.findOne(id);
  117. }
  118. return brand;
  119. }
  120. /**
  121. * 对component器件进行解析
  122. *
  123. * @param id id
  124. * @param methodType 改动类型
  125. * @return component器件对象
  126. * @throws JSONException
  127. */
  128. private Component parseComponent(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  129. Component component;
  130. if (methodType.equalsIgnoreCase("delete")) {
  131. component = new Component();
  132. component.setId(id);
  133. } else {
  134. component = componentDao.findOne(id);
  135. }
  136. return component;
  137. }
  138. /**
  139. * 对goods进行解析
  140. *
  141. * @param id id
  142. * @param data 数据
  143. * @return 批次对象
  144. * @throws JSONException
  145. */
  146. private Goods parseGoods(@NotEmpty("id") Long id, @NotEmpty("data") String data) throws JSONException {
  147. Goods goods = new Goods();
  148. if (data == null || data.equalsIgnoreCase("cmpId")) {
  149. Component component = new Component();
  150. component.setId(id);
  151. goods.setComponent(component);
  152. } else if (data.equalsIgnoreCase("goId")){
  153. TradeGoods tradeGoods = new TradeGoods();
  154. tradeGoods.setId(id);
  155. goods.setTradeGoods(tradeGoods);
  156. } else {
  157. throw new IllegalArgumentException("未指定是 cmpId 还是 goId");
  158. }
  159. return goods;
  160. }
  161. /**
  162. * 对销售单进行解析
  163. *
  164. * @param id id
  165. * @param methodType 改动类型
  166. * @return 销售单对象
  167. * @throws JSONException
  168. */
  169. private Order parseOrder(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  170. Order order;
  171. if (methodType.equalsIgnoreCase("delete")) {
  172. order = new Order();
  173. order.setId(id);
  174. } else {
  175. order = orderDao.findOne(id);
  176. }
  177. return order;
  178. }
  179. /**
  180. * 对销售发货单进行解析
  181. *
  182. * @param id id
  183. * @param methodType 改动类型
  184. * @return 销售发货单对象
  185. * @throws JSONException
  186. */
  187. private OrderInvoice parseOrderInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  188. OrderInvoice orderInvoice;
  189. if (methodType.equalsIgnoreCase("delete")) {
  190. orderInvoice = new OrderInvoice();
  191. orderInvoice.setId(id);
  192. } else {
  193. orderInvoice = orderInvoiceDao.findOne(id);
  194. }
  195. return orderInvoice;
  196. }
  197. /**
  198. * 对采购单进行解析
  199. *
  200. * @param id id
  201. * @param methodType 改动类型
  202. * @return 采购单对象
  203. * @throws JSONException
  204. */
  205. private Purchase parsePurchase(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  206. Purchase purchase;
  207. if (methodType.equalsIgnoreCase("delete")) {
  208. purchase = new Purchase();
  209. purchase.setId(id);
  210. } else {
  211. purchase = purchaseDao.findOne(id);
  212. }
  213. return purchase;
  214. }
  215. /**
  216. * 对采购发货单进行解析
  217. *
  218. * @param id id
  219. * @param methodType 改动类型
  220. * @return 采购发货单对象
  221. * @throws JSONException
  222. */
  223. private PurchaseInvoice parsePurchaseInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  224. PurchaseInvoice purchaseInvoice;
  225. if (methodType.equalsIgnoreCase("delete")) {
  226. purchaseInvoice = new PurchaseInvoice();
  227. purchaseInvoice.setId(id);
  228. } else {
  229. purchaseInvoice = purchaseInvoiceDao.findOne(id);
  230. }
  231. return purchaseInvoice;
  232. }
  233. }