QueueMessageParser.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. @Autowired
  32. private V_ProductsDao v_productsDao;
  33. /**
  34. * 对得到的json消息进行解析
  35. *
  36. * @param tableName 表名
  37. * @param dataId 数据 id
  38. * @param methodType 更改类型
  39. * @param data 数据
  40. * @return ParsedQueueMessage对象
  41. * @throws JSONException
  42. */
  43. public ParsedQueueMessage parse(@NotEmpty("tableName") String tableName, @NotEmpty("dataId") Long dataId, @NotEmpty("methodType") String methodType, String data) throws JSONException {
  44. ParsedQueueMessage parsedQueueMessage = new ParsedQueueMessage();
  45. Object object;
  46. try {
  47. // 解析数据库表的更改类型
  48. if (methodType.equals("insert")) {
  49. parsedQueueMessage.setMethodType(ParsedQueueMessage.INSERT);
  50. } else if (methodType.equals("update")) {
  51. parsedQueueMessage.setMethodType(ParsedQueueMessage.UPDATE);
  52. } else if (methodType.equals("delete")) {
  53. parsedQueueMessage.setMethodType(ParsedQueueMessage.DELETE);
  54. } else {
  55. throw new IllegalStateException("unsupported method type: " + methodType);
  56. }
  57. // 解析哪个表有更改
  58. if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
  59. object = parseKind(dataId, methodType);
  60. } else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
  61. object = parseBrand(dataId, methodType);
  62. } else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
  63. object = parseComponent(dataId, methodType);
  64. } else if (tableName.equals(SearchConstants.GOODS_TABLE_NAME)) {
  65. object = parseGoods(dataId, data);
  66. } else if (tableName.equals(SearchConstants.PCB_GOODS_TABLE_NAME)) {
  67. object = parsePCBGoods(dataId, data);
  68. } else if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
  69. object = parseOrder(dataId, methodType);
  70. } else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
  71. object = parseOrderInvoice(dataId, methodType);
  72. } else if (tableName.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
  73. object = parsePurchase(dataId, methodType);
  74. } else if (tableName.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
  75. object = parsePurchaseInvoice(dataId, methodType);
  76. } else if (tableName.equals(SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME)) {
  77. object = parseProduct(dataId, methodType);
  78. } else {
  79. throw new IllegalStateException("unsupported table name: " + tableName);
  80. }
  81. } catch (Throwable e) {
  82. // 防止SQLRecoverableException导致应用终止
  83. throw new IllegalStateException("message parsing failed!", e);
  84. }
  85. parsedQueueMessage.setObject(object);
  86. return parsedQueueMessage;
  87. }
  88. /**
  89. * 对kind类目进行解析
  90. *
  91. * @param id id
  92. * @param methodType 改动类型
  93. * @return kind类目对象
  94. * @throws JSONException
  95. */
  96. private Kind parseKind(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  97. Kind kind;
  98. // delete操作
  99. // 删除后数据库中可能已经没有相应数据了,无法通过dao获取,需要手动创建对象
  100. if (methodType.equalsIgnoreCase("delete")) {
  101. kind = new Kind();
  102. kind.setId(id);
  103. } else {
  104. kind = kindDao.findOne(id);
  105. }
  106. return kind;
  107. }
  108. /**
  109. * 对product物料进行解析
  110. *
  111. * @param id id
  112. * @param methodType 改动类型
  113. * @return product物料对象
  114. * @throws JSONException
  115. */
  116. private V_Products parseProduct(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  117. V_Products product;
  118. // delete操作
  119. // 删除后数据库中可能已经没有相应数据了,无法通过dao获取,需要手动创建对象
  120. if (methodType.equalsIgnoreCase("delete")) {
  121. product = new V_Products();
  122. product.setId(id);
  123. } else {
  124. product = v_productsDao.findOne(id);
  125. }
  126. return product;
  127. }
  128. /**
  129. * 对brand品牌进行解析
  130. *
  131. * @param id id
  132. * @param methodType 改动类型
  133. * @return brand品牌对象
  134. * @throws JSONException
  135. */
  136. private Brand parseBrand(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  137. Brand brand;
  138. if (methodType.equalsIgnoreCase("delete")) {
  139. brand = new Brand();
  140. brand.setId(id);
  141. } else {
  142. brand = brandDao.findOne(id);
  143. }
  144. return brand;
  145. }
  146. /**
  147. * 对component器件进行解析
  148. *
  149. * @param id id
  150. * @param methodType 改动类型
  151. * @return component器件对象
  152. * @throws JSONException
  153. */
  154. private Component parseComponent(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  155. Component component;
  156. if (methodType.equalsIgnoreCase("delete")) {
  157. component = new Component();
  158. component.setId(id);
  159. } else {
  160. component = componentDao.findOne(id);
  161. }
  162. return component;
  163. }
  164. /**
  165. * 对goods进行解析
  166. *
  167. * @param id id
  168. * @param data 数据
  169. * @return 批次对象
  170. * @throws JSONException
  171. */
  172. private Goods parseGoods(@NotEmpty("id") Long id, @NotEmpty("data") String data) throws JSONException {
  173. Goods goods = new Goods();
  174. if (data == null || data.equalsIgnoreCase("cmpId")) {
  175. Component component = new Component();
  176. component.setId(id);
  177. goods.setComponent(component);
  178. } else if (data.equalsIgnoreCase("goId")){
  179. TradeGoods tradeGoods = new TradeGoods();
  180. tradeGoods.setId(id);
  181. goods.setTradeGoods(tradeGoods);
  182. } else {
  183. throw new IllegalArgumentException("未指定是 cmpId 还是 goId");
  184. }
  185. return goods;
  186. }
  187. /**
  188. * 对 PCB 批次 进行解析
  189. *
  190. * @param id id
  191. * @param data 数据
  192. * @return PCB 批次对象
  193. * @throws JSONException
  194. */
  195. private PCBGoods parsePCBGoods(@NotEmpty("id") Long id, @NotEmpty("data") String data) throws JSONException {
  196. PCBGoods goods = new PCBGoods();
  197. if (data == null || data.equalsIgnoreCase("pcbId")) {
  198. PCB pcb = new PCB();
  199. pcb.setId(id);
  200. goods.setPcb(pcb);
  201. } else if (data.equalsIgnoreCase("goId")){
  202. TradeGoods tradeGoods = new TradeGoods();
  203. tradeGoods.setId(id);
  204. goods.setTradeGoods(tradeGoods);
  205. } else {
  206. throw new IllegalArgumentException("未指定是 pcbId 还是 goId");
  207. }
  208. return goods;
  209. }
  210. /**
  211. * 对销售单进行解析
  212. *
  213. * @param id id
  214. * @param methodType 改动类型
  215. * @return 销售单对象
  216. * @throws JSONException
  217. */
  218. private Order parseOrder(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  219. Order order;
  220. if (methodType.equalsIgnoreCase("delete")) {
  221. order = new Order();
  222. order.setId(id);
  223. } else {
  224. order = orderDao.findOne(id);
  225. }
  226. return order;
  227. }
  228. /**
  229. * 对销售发货单进行解析
  230. *
  231. * @param id id
  232. * @param methodType 改动类型
  233. * @return 销售发货单对象
  234. * @throws JSONException
  235. */
  236. private OrderInvoice parseOrderInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  237. OrderInvoice orderInvoice;
  238. if (methodType.equalsIgnoreCase("delete")) {
  239. orderInvoice = new OrderInvoice();
  240. orderInvoice.setId(id);
  241. } else {
  242. orderInvoice = orderInvoiceDao.findOne(id);
  243. }
  244. return orderInvoice;
  245. }
  246. /**
  247. * 对采购单进行解析
  248. *
  249. * @param id id
  250. * @param methodType 改动类型
  251. * @return 采购单对象
  252. * @throws JSONException
  253. */
  254. private Purchase parsePurchase(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  255. Purchase purchase;
  256. if (methodType.equalsIgnoreCase("delete")) {
  257. purchase = new Purchase();
  258. purchase.setId(id);
  259. } else {
  260. purchase = purchaseDao.findOne(id);
  261. }
  262. return purchase;
  263. }
  264. /**
  265. * 对采购发货单进行解析
  266. *
  267. * @param id id
  268. * @param methodType 改动类型
  269. * @return 采购发货单对象
  270. * @throws JSONException
  271. */
  272. private PurchaseInvoice parsePurchaseInvoice(@NotEmpty("id") Long id, @NotEmpty("methodType") String methodType) throws JSONException {
  273. PurchaseInvoice purchaseInvoice;
  274. if (methodType.equalsIgnoreCase("delete")) {
  275. purchaseInvoice = new PurchaseInvoice();
  276. purchaseInvoice.setId(id);
  277. } else {
  278. purchaseInvoice = purchaseInvoiceDao.findOne(id);
  279. }
  280. return purchaseInvoice;
  281. }
  282. }