Explorar el Código

fix the updated duplicate goods error

sunyj hace 8 años
padre
commit
4504a5f63e

+ 3 - 3
mall-search/src/main/java/com/uas/search/dao/GoodsDao.java

@@ -38,10 +38,10 @@ public class GoodsDao {
             return null;
         }
         List<Goods> goodsesList = new ArrayList<>();
-        if (goods.getTradeGoods() != null && goods.getTradeGoods().getId() != null) {
-            goodsesList.add(findByGoId(goods.getTradeGoods().getId()));
-        } else if (goods.getComponent() != null && goods.getComponent().getId() != null) {
+        if (goods.getComponent() != null && goods.getComponent().getId() != null) {
             goodsesList.addAll(findByCmpId(goods.getComponent().getId()));
+        } else if (goods.getTradeGoods() != null && goods.getTradeGoods().getId() != null) {
+            goodsesList.add(findByGoId(goods.getTradeGoods().getId()));
         }
         return goodsesList;
     }

+ 24 - 8
mall-search/src/main/java/com/uas/search/service/impl/IndexServiceImpl.java

@@ -674,11 +674,11 @@ public class IndexServiceImpl implements IndexService {
 	 * @return
 	 */
 	private Term toTerm(Goods goods) {
-		if (goods.getTradeGoods() != null) {
-			return new Term(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(goods.getTradeGoods().getId()));
-		} else if (goods.getComponent() != null) {
-			return new Term(SearchConstants.GOODS_CMP_ID_FIELD, String.valueOf(goods.getComponent().getId()));
-		}
+        if (goods.getComponent() != null) {
+            return new Term(SearchConstants.GOODS_CMP_ID_FIELD, String.valueOf(goods.getComponent().getId()));
+        } else if (goods.getTradeGoods() != null) {
+            return new Term(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(goods.getTradeGoods().getId()));
+        }
 		return null;
 	}
 
@@ -710,9 +710,25 @@ public class IndexServiceImpl implements IndexService {
         }
         // 删除索引
         else if (parsedQueueMessage.isDelete()) {
-            Object maintainedObject = delete(object);
-            if (maintainedObject != null) {
-                maintainedObjects.add(maintainedObject);
+            if (object instanceof Goods) {
+                // 先删除相关批次和器件,再重新写入
+                delete(object);
+                Goods goodsObject = (Goods) object;
+                if (goodsObject.getComponent() != null && goodsObject.getComponent().getId() != null) {
+                    // 如果是器件,再重新写入
+                    List<Goods> goodsesList = goodsDao.find((Goods) object);
+                    for (Goods goods : goodsesList) {
+                        Object maintainedObject = save(goods);
+                        if (maintainedObject != null) {
+                            maintainedObjects.add(maintainedObject);
+                        }
+                    }
+                }
+            } else {
+                Object maintainedObject = delete(object);
+                if (maintainedObject != null) {
+                    maintainedObjects.add(maintainedObject);
+                }
             }
         } else {
             throw new IllegalStateException("message parsing failed!");