Browse Source

精简创建类目、品牌索引的代码;删除Dao的findByIds方法

sunyj 9 years ago
parent
commit
cda027c8e0

+ 0 - 14
search-console/src/main/java/com/uas/search/console/dao/BrandSimpleInfoDao.java

@@ -1,11 +1,7 @@
 package com.uas.search.console.dao;
 
-import java.util.List;
-
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
 import com.uas.search.console.model.BrandSimpleInfo;
@@ -25,14 +21,4 @@ public interface BrandSimpleInfoDao
 	 * @return
 	 */
 	public BrandSimpleInfo findById(Long id);
-
-	/**
-	 * 按id数组获取品牌
-	 * 
-	 * @param ids
-	 *            id数组
-	 * @return
-	 */
-	@Query("select b from BrandSimpleInfo b where b.id in (:ids)")
-	public List<BrandSimpleInfo> findByIds(@Param("ids") Long[] ids);
 }

+ 0 - 10
search-console/src/main/java/com/uas/search/console/dao/ComponentSimpleInfoDao.java

@@ -50,14 +50,4 @@ public interface ComponentSimpleInfoDao
 	 */
 	@Query("select c from ComponentSimpleInfo c where c.id in (:ids) order by instr(':ids', c.id)")
 	public List<ComponentSimpleInfo> findByIdsInOrder(@Param("ids") Long[] ids);
-
-	/**
-	 * 按id数组获取器件
-	 * 
-	 * @param ids
-	 *            id数组
-	 * @return
-	 */
-	@Query("select c from ComponentSimpleInfo c where c.id in (:ids)")
-	public List<ComponentSimpleInfo> findByIds(@Param("ids") Long[] ids);
 }

+ 0 - 14
search-console/src/main/java/com/uas/search/console/dao/KindSimpleInfoDao.java

@@ -1,11 +1,7 @@
 package com.uas.search.console.dao;
 
-import java.util.List;
-
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
 import com.uas.search.console.model.KindSimpleInfo;
@@ -25,14 +21,4 @@ public interface KindSimpleInfoDao
 	 * @return
 	 */
 	public KindSimpleInfo findById(Long id);
-
-	/**
-	 * 按id数组获取类目
-	 * 
-	 * @param ids
-	 *            id数组
-	 * @return
-	 */
-	@Query("select k from KindSimpleInfo k where k.id in (:ids)")
-	public List<KindSimpleInfo> findByIds(@Param("ids") Long[] ids);
 }

+ 11 - 34
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -6,6 +6,7 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
@@ -17,7 +18,6 @@ import org.apache.lucene.store.FSDirectory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
 import com.alibaba.fastjson.JSONObject;
@@ -213,20 +213,8 @@ public class IndexServiceImpl implements IndexService {
 	private Long createKindIndexs() throws IOException {
 		logger.info("正在创建类目索引...");
 		List<KindSimpleInfo> kinds = kindDao.findAll();
-		if (CollectionUtils.isEmpty(kinds)) {
-			return 0L;
-		}
 		logger.info("发现数据:" + kinds.size() + "条");
-		long count = 0;
-		for (KindSimpleInfo kind : kinds) {
-			Document document = ObjectToDocumentUtils.toDocument(kind);
-			if (document != null) {
-				indexWriter.addDocument(document);
-				count++;
-			}
-		}
-		indexWriter.commit();
-		return count;
+		return createIndexesWithObjects(kinds.toArray());
 	}
 
 	/**
@@ -238,20 +226,8 @@ public class IndexServiceImpl implements IndexService {
 	private Long createBrandIndexs() throws IOException {
 		logger.info("正在创建品牌索引...");
 		List<BrandSimpleInfo> brands = brandDao.findAll();
-		if (CollectionUtils.isEmpty(brands)) {
-			return 0L;
-		}
 		logger.info("发现数据:" + brands.size() + "条");
-		long count = 0;
-		for (BrandSimpleInfo brand : brands) {
-			Document document = ObjectToDocumentUtils.toDocument(brand);
-			if (document != null) {
-				indexWriter.addDocument(document);
-				count++;
-			}
-		}
-		indexWriter.commit();
-		return count;
+		return createIndexesWithObjects(brands.toArray());
 	}
 
 	private Long createComponentIndexesWithFiles() {
@@ -552,27 +528,28 @@ public class IndexServiceImpl implements IndexService {
 		}
 
 		String[] strs = ids.split(",");
-		Long[] longIds = new Long[strs.length];
-		for (int i = 0; i < strs.length; i++) {
-			longIds[i] = Long.parseLong(strs[i]);
+		List<Long> idList = new ArrayList<>();
+		for (String str : strs) {
+			idList.add(Long.parseLong(str));
 		}
 		if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
-			List<KindSimpleInfo> kinds = kindDao.findByIds(longIds);
+			List<KindSimpleInfo> kinds = kindDao.findAll(idList);
 			for (KindSimpleInfo kind : kinds) {
 				update(kind);
 			}
 		} else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
-			List<BrandSimpleInfo> brands = brandDao.findByIds(longIds);
+			List<BrandSimpleInfo> brands = brandDao.findAll(idList);
 			for (BrandSimpleInfo brand : brands) {
 				update(brand);
 			}
 		} else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
-			List<ComponentSimpleInfo> components = componentDao.findByIds(longIds);
+			List<ComponentSimpleInfo> components = componentDao.findAll(idList);
 			for (ComponentSimpleInfo component : components) {
 				update(component);
 			}
 		} else {
-			throw new SearchException("需指定要维护的ids,多个id以英文逗号隔开,如:321,988");
+			throw new SearchException("需指定要维护的表名:" + SearchConstants.KIND_TABLE_NAME + "、"
+					+ SearchConstants.BRAND_TABLE_NAME + "、" + SearchConstants.COMPONENT_TABLE_NAME);
 		}
 		return "维护成功";
 	}