QueueMessageParser.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package com.uas.search.console.jms;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.util.StringUtils;
  5. import com.alibaba.fastjson.JSONException;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.uas.search.console.core.util.ContextUtils;
  8. import com.uas.search.console.dao.BrandSimpleInfoDao;
  9. import com.uas.search.console.dao.ComponentSimpleInfoDao;
  10. import com.uas.search.console.dao.KindSimpleInfoDao;
  11. import com.uas.search.console.dao.OrderInvoiceSimpleInfoDao;
  12. import com.uas.search.console.dao.OrderSimpleInfoDao;
  13. import com.uas.search.console.dao.PurchaseInvoiceSimpleInfoDao;
  14. import com.uas.search.console.dao.PurchaseSimpleInfoDao;
  15. import com.uas.search.console.model.BrandSimpleInfo;
  16. import com.uas.search.console.model.ComponentSimpleInfo;
  17. import com.uas.search.console.model.KindSimpleInfo;
  18. import com.uas.search.console.model.OrderInvoiceSimpleInfo;
  19. import com.uas.search.console.model.OrderSimpleInfo;
  20. import com.uas.search.console.model.ParsedQueueMessage;
  21. import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
  22. import com.uas.search.console.model.PurchaseSimpleInfo;
  23. import com.uas.search.console.service.InnerOrderSearchService;
  24. import com.uas.search.console.service.InnerSearchService;
  25. import com.uas.search.console.util.SearchConstants;
  26. /**
  27. * 对得到的队列消息进行解析的工具
  28. *
  29. * @author sunyj
  30. * @since 2016年7月7日 下午6:14:03
  31. */
  32. @Service
  33. public class QueueMessageParser {
  34. @Autowired
  35. private KindSimpleInfoDao kindDao;
  36. @Autowired
  37. private BrandSimpleInfoDao brandDao;
  38. @Autowired
  39. private ComponentSimpleInfoDao componentDao;
  40. @Autowired
  41. private OrderSimpleInfoDao orderDao;
  42. @Autowired
  43. private OrderInvoiceSimpleInfoDao orderInvoiceDao;
  44. @Autowired
  45. private PurchaseSimpleInfoDao purchaseDao;
  46. @Autowired
  47. private PurchaseInvoiceSimpleInfoDao purchaseInvoiceDao;
  48. private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
  49. InnerSearchService.class);
  50. private InnerOrderSearchService innerOrderSearchService = ContextUtils.getApplicationContext()
  51. .getBean("orderSearchServiceImpl", InnerOrderSearchService.class);
  52. /**
  53. * 对得到的json消息进行解析
  54. *
  55. * @param message
  56. * @return ParsedQueueMessage对象
  57. * @throws JSONException
  58. */
  59. // {"method":"value1","table":"value2","param1":"value3","param2":"value4"}
  60. public ParsedQueueMessage parse(String message) throws JSONException {
  61. if (StringUtils.isEmpty(message) || message.equals("{}")) {
  62. return null;
  63. }
  64. ParsedQueueMessage parsedQueueMessage = new ParsedQueueMessage();
  65. JSONObject jsonObject = JSONObject.parseObject(message);
  66. // 解析数据库表的更改类型
  67. String method = jsonObject.getString("method");
  68. if (method.equals("insert")) {
  69. parsedQueueMessage.setMethodType(ParsedQueueMessage.INSERT);
  70. }
  71. else if (method.equals("update")) {
  72. parsedQueueMessage.setMethodType(ParsedQueueMessage.UPDATE);
  73. }
  74. else if (method.equals("delete")) {
  75. parsedQueueMessage.setMethodType(ParsedQueueMessage.DELETE);
  76. } else {
  77. return null;
  78. }
  79. // 解析哪个表有更改
  80. Object[] objects = null;
  81. String table = jsonObject.getString("table");
  82. if (table.equals(SearchConstants.KIND_TABLE_NAME)) {
  83. objects = parseKind(jsonObject);
  84. } else if (table.equals(SearchConstants.BRAND_TABLE_NAME)) {
  85. objects = parseBrand(jsonObject);
  86. } else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
  87. objects = parseComponent(jsonObject);
  88. } else if (table.equals(SearchConstants.ORDER_TABLE_NAME)) {
  89. objects = parseOrder(jsonObject);
  90. } else if (table.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
  91. objects = parseOrderInvoice(jsonObject);
  92. } else if (table.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
  93. objects = parsePurchase(jsonObject);
  94. } else if (table.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
  95. objects = parsePurchaseInvoice(jsonObject);
  96. } else {
  97. return null;
  98. }
  99. parsedQueueMessage.setObjects(objects);
  100. return parsedQueueMessage;
  101. }
  102. /**
  103. * 对kind类目进行解析
  104. *
  105. * @param jsonObject
  106. * @return kind类目对象数组
  107. * @throws JSONException
  108. */
  109. // {"method":"insert","table":"product$kind","ids":[2400,1299]}
  110. private KindSimpleInfo[] parseKind(JSONObject jsonObject) throws JSONException {
  111. // 获取本次更改的所有类目id
  112. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  113. KindSimpleInfo[] kinds = new KindSimpleInfo[ids.length];
  114. for (int i = 0; i < ids.length; i++) {
  115. Long kindId = Long.parseLong(ids[i].toString());
  116. KindSimpleInfo kind = kindDao.findById(kindId);
  117. KindSimpleInfo localKind = innerSearchService.getKind(kindId);
  118. if (jsonObject.getString("method").equals("delete")) {
  119. // 数据库中该类目仍存在
  120. if (kind != null) {
  121. // 与索引中的数据比较
  122. // 1. 索引中不存在,返回null,不进行删除操作
  123. // 2.索引中存在又与现在的数据相同,说明是事务回退或者是删除之后又重新添加了一条一样的数据
  124. // (仅限索引中存储的字段),此时也不必进行操作
  125. if (localKind == null || localKind.equals(kind)) {
  126. kind = null;
  127. }
  128. } else {
  129. kind = localKind;
  130. }
  131. }
  132. // 新增或更新操作
  133. else {
  134. // 索引中的数据与数据库一样,不进行操作,认为是事务回退或者删除之后又重新添加了一条一样的数据
  135. if (localKind != null && localKind.equals(kind)) {
  136. kind = null;
  137. }
  138. }
  139. kinds[i] = kind;
  140. }
  141. return kinds;
  142. }
  143. /**
  144. * 对brand品牌进行解析
  145. *
  146. * @param jsonObject
  147. * @return brand品牌对象数组
  148. * @throws JSONException
  149. */
  150. // {"method":"insert","table":"product$brand","ids":[124]}
  151. private BrandSimpleInfo[] parseBrand(JSONObject jsonObject) throws JSONException {
  152. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  153. BrandSimpleInfo[] brands = new BrandSimpleInfo[ids.length];
  154. for (int i = 0; i < ids.length; i++) {
  155. Long brandId = Long.parseLong(ids[i].toString());
  156. BrandSimpleInfo brand = brandDao.findById(brandId);
  157. BrandSimpleInfo localBrand = innerSearchService.getBrand(brandId);
  158. if (jsonObject.getString("method").equals("delete")) {
  159. if (brand != null) {
  160. if (localBrand == null || localBrand.equals(brand)) {
  161. brand = null;
  162. }
  163. } else {
  164. brand = localBrand;
  165. }
  166. } else {
  167. if (localBrand != null && localBrand.equals(brand)) {
  168. brand = null;
  169. }
  170. }
  171. brands[i] = brand;
  172. }
  173. return brands;
  174. }
  175. /**
  176. * 对component器件进行解析
  177. *
  178. * @param jsonObject
  179. * @return component器件对象数组
  180. * @throws JSONException
  181. */
  182. // {"method":"value1","table":"product$component","ids":[1024]}
  183. private ComponentSimpleInfo[] parseComponent(JSONObject jsonObject) throws JSONException {
  184. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  185. ComponentSimpleInfo[] components = new ComponentSimpleInfo[ids.length];
  186. for (int i = 0; i < ids.length; i++) {
  187. Long componentId = Long.parseLong(ids[i].toString());
  188. ComponentSimpleInfo component = componentDao.findById(componentId);
  189. ComponentSimpleInfo localComponent = innerSearchService.getComponent(componentId);
  190. if (jsonObject.getString("method").equals("delete")) {
  191. if (component != null) {
  192. if (localComponent == null || localComponent.equals(component)) {
  193. component = null;
  194. }
  195. } else {
  196. component = localComponent;
  197. }
  198. }
  199. // 器件较为特殊,insert和update需分开对待
  200. // update来源还可能来自属性值的变化,而equals是不比较属性值的
  201. // (如果比较的话,对于insert器件,即使是同一个,属性值在之后变化了的话,也会视为两个器件,会添加重复的器件索引)
  202. else if (jsonObject.getString("method").equals("insert")) {
  203. if (localComponent != null && localComponent.equals(component)) {
  204. component = null;
  205. }
  206. }
  207. components[i] = component;
  208. }
  209. return components;
  210. }
  211. /**
  212. * 对销售单进行解析
  213. *
  214. * @param jsonObject
  215. * @return 销售单对象数组
  216. * @throws JSONException
  217. */
  218. // {"method":"insert","table":"trade$order","ids":[124]}
  219. private OrderSimpleInfo[] parseOrder(JSONObject jsonObject) throws JSONException {
  220. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  221. OrderSimpleInfo[] orders = new OrderSimpleInfo[ids.length];
  222. for (int i = 0; i < ids.length; i++) {
  223. Long id = Long.parseLong(ids[i].toString());
  224. OrderSimpleInfo order = orderDao.findById(id);
  225. OrderSimpleInfo localOrder = innerOrderSearchService.getOrder(id);
  226. System.out.println(order);
  227. System.out.println(localOrder);
  228. // 删除操作
  229. if (jsonObject.getString("method").equals("delete")) {
  230. System.out.println("localOrder == null || localOrder.equals(order) " + localOrder == null
  231. || localOrder.equals(order));
  232. if (order != null) {
  233. // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
  234. if (localOrder == null || localOrder.equals(order)) {
  235. order = null;
  236. }
  237. } else {
  238. order = localOrder;
  239. }
  240. }
  241. // 更新或者插入操作
  242. else {
  243. System.out.println("localOrder != null && localOrder.equals(order) " + localOrder != null
  244. && localOrder.equals(order));
  245. // 本地有相同的数据,不更新索引
  246. if (localOrder != null && localOrder.equals(order)) {
  247. order = null;
  248. }
  249. }
  250. orders[i] = order;
  251. }
  252. return orders;
  253. }
  254. /**
  255. * 对销售发货单进行解析
  256. *
  257. * @param jsonObject
  258. * @return 销售发货单对象数组
  259. * @throws JSONException
  260. */
  261. // {"method":"insert","table":"trade$invoice_fmor","ids":[124]}
  262. private OrderInvoiceSimpleInfo[] parseOrderInvoice(JSONObject jsonObject) throws JSONException {
  263. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  264. OrderInvoiceSimpleInfo[] orderInvoices = new OrderInvoiceSimpleInfo[ids.length];
  265. for (int i = 0; i < ids.length; i++) {
  266. Long id = Long.parseLong(ids[i].toString());
  267. OrderInvoiceSimpleInfo orderInvoice = orderInvoiceDao.findById(id);
  268. OrderInvoiceSimpleInfo localOrderInvoice = innerOrderSearchService.getOrderInvoice(id);
  269. // 删除操作
  270. if (jsonObject.getString("method").equals("delete")) {
  271. if (orderInvoice != null) {
  272. // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
  273. if (localOrderInvoice == null || localOrderInvoice.equals(orderInvoice)) {
  274. orderInvoice = null;
  275. }
  276. } else {
  277. orderInvoice = localOrderInvoice;
  278. }
  279. }
  280. // 更新或者插入操作
  281. else {
  282. // 本地有相同的数据,不更新索引
  283. if (localOrderInvoice != null && localOrderInvoice.equals(orderInvoice)) {
  284. orderInvoice = null;
  285. }
  286. }
  287. orderInvoices[i] = orderInvoice;
  288. }
  289. return orderInvoices;
  290. }
  291. /**
  292. * 对采购单进行解析
  293. *
  294. * @param jsonObject
  295. * @return 采购单对象数组
  296. * @throws JSONException
  297. */
  298. // {"method":"insert","table":"trade$purchase","ids":[124]}
  299. private PurchaseSimpleInfo[] parsePurchase(JSONObject jsonObject) throws JSONException {
  300. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  301. PurchaseSimpleInfo[] purchases = new PurchaseSimpleInfo[ids.length];
  302. for (int i = 0; i < ids.length; i++) {
  303. Long id = Long.parseLong(ids[i].toString());
  304. PurchaseSimpleInfo purchase = purchaseDao.findById(id);
  305. PurchaseSimpleInfo localPurchase = innerOrderSearchService.getPurchase(id);
  306. // 删除操作
  307. if (jsonObject.getString("method").equals("delete")) {
  308. if (purchase != null) {
  309. // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
  310. if (localPurchase == null || localPurchase.equals(purchase)) {
  311. purchase = null;
  312. }
  313. } else {
  314. purchase = localPurchase;
  315. }
  316. }
  317. // 更新或者插入操作
  318. else {
  319. // 本地有相同的数据,不更新索引
  320. if (localPurchase != null && localPurchase.equals(purchase)) {
  321. purchase = null;
  322. }
  323. }
  324. purchases[i] = purchase;
  325. }
  326. return purchases;
  327. }
  328. /**
  329. * 对采购发货单进行解析
  330. *
  331. * @param jsonObject
  332. * @return 采购发货单对象数组
  333. * @throws JSONException
  334. */
  335. // {"method":"insert","table":"trade$invoice_fmpu","ids":[124]}
  336. private PurchaseInvoiceSimpleInfo[] parsePurchaseInvoice(JSONObject jsonObject) throws JSONException {
  337. Object[] ids = jsonObject.getJSONArray("ids").toArray();
  338. PurchaseInvoiceSimpleInfo[] purchaseInvoices = new PurchaseInvoiceSimpleInfo[ids.length];
  339. for (int i = 0; i < ids.length; i++) {
  340. Long id = Long.parseLong(ids[i].toString());
  341. PurchaseInvoiceSimpleInfo purchaseInvoice = purchaseInvoiceDao.findById(id);
  342. PurchaseInvoiceSimpleInfo localPurchaseInvoice = innerOrderSearchService.getPurchaseInvoice(id);
  343. // 删除操作
  344. if (jsonObject.getString("method").equals("delete")) {
  345. if (purchaseInvoice != null) {
  346. // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
  347. if (localPurchaseInvoice == null || localPurchaseInvoice.equals(purchaseInvoice)) {
  348. purchaseInvoice = null;
  349. }
  350. } else {
  351. purchaseInvoice = localPurchaseInvoice;
  352. }
  353. }
  354. // 更新或者插入操作
  355. else {
  356. // 本地有相同的数据,不更新索引
  357. if (localPurchaseInvoice != null && localPurchaseInvoice.equals(purchaseInvoice)) {
  358. purchaseInvoice = null;
  359. }
  360. }
  361. purchaseInvoices[i] = purchaseInvoice;
  362. }
  363. return purchaseInvoices;
  364. }
  365. }