| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- package com.uas.search.console.jms;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import com.alibaba.fastjson.JSONException;
- import com.alibaba.fastjson.JSONObject;
- import com.uas.search.console.core.util.ContextUtils;
- import com.uas.search.console.dao.BrandSimpleInfoDao;
- import com.uas.search.console.dao.ComponentSimpleInfoDao;
- import com.uas.search.console.dao.KindSimpleInfoDao;
- import com.uas.search.console.dao.OrderInvoiceSimpleInfoDao;
- import com.uas.search.console.dao.OrderSimpleInfoDao;
- import com.uas.search.console.dao.PurchaseInvoiceSimpleInfoDao;
- import com.uas.search.console.dao.PurchaseSimpleInfoDao;
- import com.uas.search.console.model.BrandSimpleInfo;
- import com.uas.search.console.model.ComponentSimpleInfo;
- import com.uas.search.console.model.KindSimpleInfo;
- import com.uas.search.console.model.OrderInvoiceSimpleInfo;
- import com.uas.search.console.model.OrderSimpleInfo;
- import com.uas.search.console.model.ParsedQueueMessage;
- import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
- import com.uas.search.console.model.PurchaseSimpleInfo;
- import com.uas.search.console.service.InnerOrderSearchService;
- import com.uas.search.console.service.InnerSearchService;
- import com.uas.search.console.util.SearchConstants;
- /**
- * 对得到的队列消息进行解析的工具
- *
- * @author sunyj
- * @since 2016年7月7日 下午6:14:03
- */
- @Service
- public class QueueMessageParser {
- @Autowired
- private KindSimpleInfoDao kindDao;
- @Autowired
- private BrandSimpleInfoDao brandDao;
- @Autowired
- private ComponentSimpleInfoDao componentDao;
- @Autowired
- private OrderSimpleInfoDao orderDao;
- @Autowired
- private OrderInvoiceSimpleInfoDao orderInvoiceDao;
- @Autowired
- private PurchaseSimpleInfoDao purchaseDao;
- @Autowired
- private PurchaseInvoiceSimpleInfoDao purchaseInvoiceDao;
- private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
- InnerSearchService.class);
- private InnerOrderSearchService innerOrderSearchService = ContextUtils.getApplicationContext()
- .getBean("orderSearchServiceImpl", InnerOrderSearchService.class);
- /**
- * 对得到的json消息进行解析
- *
- * @param message
- * @return ParsedQueueMessage对象
- * @throws JSONException
- */
- // {"method":"value1","table":"value2","param1":"value3","param2":"value4"}
- public ParsedQueueMessage parse(String message) throws JSONException {
- if (StringUtils.isEmpty(message) || message.equals("{}")) {
- return null;
- }
- ParsedQueueMessage parsedQueueMessage = new ParsedQueueMessage();
- JSONObject jsonObject = JSONObject.parseObject(message);
- // 解析数据库表的更改类型
- String method = jsonObject.getString("method");
- if (method.equals("insert")) {
- parsedQueueMessage.setMethodType(ParsedQueueMessage.INSERT);
- }
- else if (method.equals("update")) {
- parsedQueueMessage.setMethodType(ParsedQueueMessage.UPDATE);
- }
- else if (method.equals("delete")) {
- parsedQueueMessage.setMethodType(ParsedQueueMessage.DELETE);
- } else {
- return null;
- }
- // 解析哪个表有更改
- Object[] objects = null;
- String table = jsonObject.getString("table");
- if (table.equals(SearchConstants.KIND_TABLE_NAME)) {
- objects = parseKind(jsonObject);
- } else if (table.equals(SearchConstants.BRAND_TABLE_NAME)) {
- objects = parseBrand(jsonObject);
- } else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
- objects = parseComponent(jsonObject);
- } else if (table.equals(SearchConstants.ORDER_TABLE_NAME)) {
- objects = parseOrder(jsonObject);
- } else if (table.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
- objects = parseOrderInvoice(jsonObject);
- } else if (table.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
- objects = parsePurchase(jsonObject);
- } else if (table.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
- objects = parsePurchaseInvoice(jsonObject);
- } else {
- return null;
- }
- parsedQueueMessage.setObjects(objects);
- return parsedQueueMessage;
- }
- /**
- * 对kind类目进行解析
- *
- * @param jsonObject
- * @return kind类目对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"product$kind","ids":[2400,1299]}
- private KindSimpleInfo[] parseKind(JSONObject jsonObject) throws JSONException {
- // 获取本次更改的所有类目id
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- KindSimpleInfo[] kinds = new KindSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long kindId = Long.parseLong(ids[i].toString());
- KindSimpleInfo kind = kindDao.findById(kindId);
- KindSimpleInfo localKind = innerSearchService.getKind(kindId);
- if (jsonObject.getString("method").equals("delete")) {
- // 数据库中该类目仍存在
- if (kind != null) {
- // 与索引中的数据比较
- // 1. 索引中不存在,返回null,不进行删除操作
- // 2.索引中存在又与现在的数据相同,说明是事务回退或者是删除之后又重新添加了一条一样的数据
- // (仅限索引中存储的字段),此时也不必进行操作
- if (localKind == null || localKind.equals(kind)) {
- kind = null;
- }
- } else {
- kind = localKind;
- }
- }
- // 新增或更新操作
- else {
- // 索引中的数据与数据库一样,不进行操作,认为是事务回退或者删除之后又重新添加了一条一样的数据
- if (localKind != null && localKind.equals(kind)) {
- kind = null;
- }
- }
- kinds[i] = kind;
- }
- return kinds;
- }
- /**
- * 对brand品牌进行解析
- *
- * @param jsonObject
- * @return brand品牌对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"product$brand","ids":[124]}
- private BrandSimpleInfo[] parseBrand(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- BrandSimpleInfo[] brands = new BrandSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long brandId = Long.parseLong(ids[i].toString());
- BrandSimpleInfo brand = brandDao.findById(brandId);
- BrandSimpleInfo localBrand = innerSearchService.getBrand(brandId);
- if (jsonObject.getString("method").equals("delete")) {
- if (brand != null) {
- if (localBrand == null || localBrand.equals(brand)) {
- brand = null;
- }
- } else {
- brand = localBrand;
- }
- } else {
- if (localBrand != null && localBrand.equals(brand)) {
- brand = null;
- }
- }
- brands[i] = brand;
- }
- return brands;
- }
- /**
- * 对component器件进行解析
- *
- * @param jsonObject
- * @return component器件对象数组
- * @throws JSONException
- */
- // {"method":"value1","table":"product$component","ids":[1024]}
- private ComponentSimpleInfo[] parseComponent(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- ComponentSimpleInfo[] components = new ComponentSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long componentId = Long.parseLong(ids[i].toString());
- ComponentSimpleInfo component = componentDao.findById(componentId);
- ComponentSimpleInfo localComponent = innerSearchService.getComponent(componentId);
- if (jsonObject.getString("method").equals("delete")) {
- if (component != null) {
- if (localComponent == null || localComponent.equals(component)) {
- component = null;
- }
- } else {
- component = localComponent;
- }
- }
- // 器件较为特殊,insert和update需分开对待
- // update来源还可能来自属性值的变化,而equals是不比较属性值的
- // (如果比较的话,对于insert器件,即使是同一个,属性值在之后变化了的话,也会视为两个器件,会添加重复的器件索引)
- else if (jsonObject.getString("method").equals("insert")) {
- if (localComponent != null && localComponent.equals(component)) {
- component = null;
- }
- }
- components[i] = component;
- }
- return components;
- }
- /**
- * 对销售单进行解析
- *
- * @param jsonObject
- * @return 销售单对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"trade$order","ids":[124]}
- private OrderSimpleInfo[] parseOrder(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- OrderSimpleInfo[] orders = new OrderSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long id = Long.parseLong(ids[i].toString());
- OrderSimpleInfo order = orderDao.findById(id);
- OrderSimpleInfo localOrder = innerOrderSearchService.getOrder(id);
- System.out.println(order);
- System.out.println(localOrder);
- // 删除操作
- if (jsonObject.getString("method").equals("delete")) {
- System.out.println("localOrder == null || localOrder.equals(order) " + localOrder == null
- || localOrder.equals(order));
- if (order != null) {
- // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
- if (localOrder == null || localOrder.equals(order)) {
- order = null;
- }
- } else {
- order = localOrder;
- }
- }
- // 更新或者插入操作
- else {
- System.out.println("localOrder != null && localOrder.equals(order) " + localOrder != null
- && localOrder.equals(order));
- // 本地有相同的数据,不更新索引
- if (localOrder != null && localOrder.equals(order)) {
- order = null;
- }
- }
- orders[i] = order;
- }
- return orders;
- }
- /**
- * 对销售发货单进行解析
- *
- * @param jsonObject
- * @return 销售发货单对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"trade$invoice_fmor","ids":[124]}
- private OrderInvoiceSimpleInfo[] parseOrderInvoice(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- OrderInvoiceSimpleInfo[] orderInvoices = new OrderInvoiceSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long id = Long.parseLong(ids[i].toString());
- OrderInvoiceSimpleInfo orderInvoice = orderInvoiceDao.findById(id);
- OrderInvoiceSimpleInfo localOrderInvoice = innerOrderSearchService.getOrderInvoice(id);
- // 删除操作
- if (jsonObject.getString("method").equals("delete")) {
- if (orderInvoice != null) {
- // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
- if (localOrderInvoice == null || localOrderInvoice.equals(orderInvoice)) {
- orderInvoice = null;
- }
- } else {
- orderInvoice = localOrderInvoice;
- }
- }
- // 更新或者插入操作
- else {
- // 本地有相同的数据,不更新索引
- if (localOrderInvoice != null && localOrderInvoice.equals(orderInvoice)) {
- orderInvoice = null;
- }
- }
- orderInvoices[i] = orderInvoice;
- }
- return orderInvoices;
- }
- /**
- * 对采购单进行解析
- *
- * @param jsonObject
- * @return 采购单对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"trade$purchase","ids":[124]}
- private PurchaseSimpleInfo[] parsePurchase(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- PurchaseSimpleInfo[] purchases = new PurchaseSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long id = Long.parseLong(ids[i].toString());
- PurchaseSimpleInfo purchase = purchaseDao.findById(id);
- PurchaseSimpleInfo localPurchase = innerOrderSearchService.getPurchase(id);
- // 删除操作
- if (jsonObject.getString("method").equals("delete")) {
- if (purchase != null) {
- // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
- if (localPurchase == null || localPurchase.equals(purchase)) {
- purchase = null;
- }
- } else {
- purchase = localPurchase;
- }
- }
- // 更新或者插入操作
- else {
- // 本地有相同的数据,不更新索引
- if (localPurchase != null && localPurchase.equals(purchase)) {
- purchase = null;
- }
- }
- purchases[i] = purchase;
- }
- return purchases;
- }
- /**
- * 对采购发货单进行解析
- *
- * @param jsonObject
- * @return 采购发货单对象数组
- * @throws JSONException
- */
- // {"method":"insert","table":"trade$invoice_fmpu","ids":[124]}
- private PurchaseInvoiceSimpleInfo[] parsePurchaseInvoice(JSONObject jsonObject) throws JSONException {
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
- PurchaseInvoiceSimpleInfo[] purchaseInvoices = new PurchaseInvoiceSimpleInfo[ids.length];
- for (int i = 0; i < ids.length; i++) {
- Long id = Long.parseLong(ids[i].toString());
- PurchaseInvoiceSimpleInfo purchaseInvoice = purchaseInvoiceDao.findById(id);
- PurchaseInvoiceSimpleInfo localPurchaseInvoice = innerOrderSearchService.getPurchaseInvoice(id);
- // 删除操作
- if (jsonObject.getString("method").equals("delete")) {
- if (purchaseInvoice != null) {
- // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
- if (localPurchaseInvoice == null || localPurchaseInvoice.equals(purchaseInvoice)) {
- purchaseInvoice = null;
- }
- } else {
- purchaseInvoice = localPurchaseInvoice;
- }
- }
- // 更新或者插入操作
- else {
- // 本地有相同的数据,不更新索引
- if (localPurchaseInvoice != null && localPurchaseInvoice.equals(purchaseInvoice)) {
- purchaseInvoice = null;
- }
- }
- purchaseInvoices[i] = purchaseInvoice;
- }
- return purchaseInvoices;
- }
- }
|