|
@@ -5,6 +5,7 @@ import static com.uas.search.constant.SearchConstants.GOODS_TABLE_NAME;
|
|
|
import static com.uas.search.constant.SearchConstants.PCB_GOODS_TABLE_NAME;
|
|
|
import static com.uas.search.constant.SearchConstants.PCB_TABLE_NAME;
|
|
|
import static com.uas.search.constant.SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME;
|
|
|
+import static com.uas.search.constant.SearchConstants.PRODUCTS_USERS_TABLE_NAME;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONException;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
@@ -38,6 +39,8 @@ import com.uas.search.model.Order;
|
|
|
import com.uas.search.model.OrderInvoice;
|
|
|
import com.uas.search.model.PCB;
|
|
|
import com.uas.search.model.PCBGoods;
|
|
|
+import com.uas.search.model.ProductUsersSimpleInfo;
|
|
|
+import com.uas.search.model.Products;
|
|
|
import com.uas.search.model.Purchase;
|
|
|
import com.uas.search.model.PurchaseInvoice;
|
|
|
import com.uas.search.model.TradeGoods;
|
|
@@ -191,6 +194,8 @@ public class IndexServiceImpl implements IndexService {
|
|
|
size = createPurchaseInvoiceIndexes();
|
|
|
} else if (tableName.equals(SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME)) {
|
|
|
size = createProductIndexesFromFiles();
|
|
|
+ } else if (tableName.equals(SearchConstants.PRODUCTS_USERS_TABLE_NAME)) {
|
|
|
+ size = createProductUserIndexesFromFiles();
|
|
|
} else {
|
|
|
logger.error("不支持创建该表索引:" + tableName);
|
|
|
continue;
|
|
@@ -631,6 +636,57 @@ public class IndexServiceImpl implements IndexService {
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据本地文件转换个人物料为Document
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private Long createProductUserIndexesFromFiles() throws IOException {
|
|
|
+ logger.info("正在创建物料索引...");
|
|
|
+ Long size = 0L;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ // 从本地路径读取器件数据
|
|
|
+ String productUserDataPath = SearchUtils.getDataPath(PRODUCTS_USERS_TABLE_NAME);
|
|
|
+ File[] files = new File(productUserDataPath).listFiles();
|
|
|
+ if (files == null || files.length == 0) {
|
|
|
+ logger.info("创建器件索引失败,原因:个人物料数据文件不存在!");
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+ for (File file : files) {
|
|
|
+ logger.info("读取文件: " + file.getName());
|
|
|
+ bufferedReader = new BufferedReader(new FileReader(file));
|
|
|
+ String line;
|
|
|
+ while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
|
+ ProductUsersSimpleInfo productUser;
|
|
|
+ try {
|
|
|
+ productUser = JSONObject.parseObject(line, ProductUsersSimpleInfo.class);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ throw new IllegalArgumentException(line, e);
|
|
|
+ }
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(productUser);
|
|
|
+ if (document != null) {
|
|
|
+ size++;
|
|
|
+ // 每创建10000条,打印一次进度
|
|
|
+ if (size % 10000 == 0) {
|
|
|
+ logger.info("ProductUsers indexed..................." + size);
|
|
|
+ }
|
|
|
+ indexWriter.addDocument(document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ indexWriter.commit();
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ logger.error("创建物料索引失败,原因:物料数据文件不存在!");
|
|
|
+ return 0L;
|
|
|
+ } finally {
|
|
|
+ if (bufferedReader != null) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据本地 PCB 的数据文件,转为 PCB 批次数据
|
|
|
*
|
|
@@ -762,6 +818,11 @@ public class IndexServiceImpl implements IndexService {
|
|
|
return multiDownloadData(PRODUCTS_PRIVATE_TABLE_NAME, threads, startFileIndex, endFileIndex, validateResult);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public long multiDownloadProductUser(Integer threads, Integer startFileIndex, Integer endFileIndex, DownloadHelper.ValidateResult validateResult) {
|
|
|
+ return multiDownloadData(PRODUCTS_USERS_TABLE_NAME, threads, startFileIndex, endFileIndex, validateResult);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public long multiDownloadGoods(Integer threads, Integer startFileIndex, Integer endFileIndex, DownloadHelper.ValidateResult validateResult) {
|
|
|
return multiDownloadData(GOODS_TABLE_NAME, threads, startFileIndex, endFileIndex, validateResult);
|
|
@@ -848,6 +909,14 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
DownloadHelper<V_Products> downloadHelper = new DownloadHelper<>(threads, startFileIndex, endFileIndex, tableName, sortField, new DownloadService<V_Products>() {}, validateResult, jdbcTemplate, jdbcService);
|
|
|
return downloadHelper.getResult();
|
|
|
+ } else if (tableName.equals(PRODUCTS_USERS_TABLE_NAME)) {
|
|
|
+ try {
|
|
|
+ sortField = jdbcService.abstractTransform(ProductUsersSimpleInfo.class, "id");
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ DownloadHelper<ProductUsersSimpleInfo> downloadHelper = new DownloadHelper<>(threads, startFileIndex, endFileIndex, tableName, sortField, new DownloadService<ProductUsersSimpleInfo>() {}, validateResult, jdbcTemplate, jdbcService);
|
|
|
+ return downloadHelper.getResult();
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("多线程下载不支持该表:" + tableName);
|
|
|
}
|
|
@@ -885,15 +954,14 @@ public class IndexServiceImpl implements IndexService {
|
|
|
try {
|
|
|
indexWriter = indexWriterManager.get(tableName);
|
|
|
if (obj instanceof Kind) {
|
|
|
- indexWriter.updateDocument(
|
|
|
- new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(((Kind) obj).getId())),
|
|
|
- document);
|
|
|
+ indexWriter.updateDocument(new Term(SearchConstants.KIND_ID_FIELD,
|
|
|
+ String.valueOf(((Kind) obj).getId())), document);
|
|
|
} else if (obj instanceof Brand) {
|
|
|
Brand brand = (Brand) obj;
|
|
|
indexWriter.updateDocument(new Term(SearchConstants.BRAND_ID_FIELD,
|
|
|
String.valueOf(brand.getId())), document);
|
|
|
// 更新关联批次(更新品牌权重)
|
|
|
- updateGoodsFields(new Term(SearchConstants.GOODS_BR_ID_FIELD, brand.getId().toString()), brand, null);
|
|
|
+ updateGoodsFields(new Term(SearchConstants.GOODS_BR_ID_FIELD, brand.getId().toString()), brand, null, null);
|
|
|
} else if (obj instanceof Component) {
|
|
|
indexWriter.updateDocument(new Term(SearchConstants.COMPONENT_ID_FIELD,
|
|
|
String.valueOf(((Component) obj).getId())), document);
|
|
@@ -910,8 +978,15 @@ public class IndexServiceImpl implements IndexService {
|
|
|
indexWriter.updateDocument(new Term(SearchConstants.PURCHASE_INVOICE_ID_FIELD,
|
|
|
String.valueOf(((PurchaseInvoice) obj).getId())), document);
|
|
|
} else if (obj instanceof V_Products) {
|
|
|
+ V_Products v_product = (V_Products) obj;
|
|
|
indexWriter.updateDocument(new Term(SearchConstants.PRODUCT_PRIVATE_ID_FIELD,
|
|
|
- String.valueOf(((V_Products) obj).getId())), document);
|
|
|
+ String.valueOf(v_product.getId())), document);
|
|
|
+ Products product = new Products(v_product);
|
|
|
+ updateGoodsFields(new Term(SearchConstants.GOODS_PR_ID_FIELD, product.getId().toString()), null, null, product);
|
|
|
+ updateProductUsersFields(new Term(SearchConstants.PRODUCT_USER_PRID_FIELD, v_product.getId().toString()), v_product);
|
|
|
+ } else if (obj instanceof ProductUsersSimpleInfo) {
|
|
|
+ indexWriter.updateDocument(new Term(SearchConstants.PRODUCT_USER_ID_FIELD,
|
|
|
+ String.valueOf(((ProductUsersSimpleInfo) obj).getId())), document);
|
|
|
} else {
|
|
|
throw new IllegalStateException("Message parsing failed!");
|
|
|
}
|
|
@@ -937,7 +1012,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
* @param kind 新的类目
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void updateGoodsFields(Term term, Brand brand, Kind kind) throws IOException {
|
|
|
+ private void updateGoodsFields(Term term, Brand brand, Kind kind, Products products) throws IOException {
|
|
|
if (brand == null && kind == null) {
|
|
|
return;
|
|
|
}
|
|
@@ -961,6 +1036,9 @@ public class IndexServiceImpl implements IndexService {
|
|
|
if (kind != null) {
|
|
|
goods.getComponent().setKind(kind);
|
|
|
}
|
|
|
+ if (products != null) {
|
|
|
+ goods.setProducts(products);
|
|
|
+ }
|
|
|
writer.updateDocument(new Term(idField, document.get(idField)), ObjectToDocumentUtils.toDocument(goods));
|
|
|
}
|
|
|
logger.info("更新关联批次索引:count=" + tableName + ", count=" + (size * (page - 1) + documents.getContent().size()));
|
|
@@ -981,6 +1059,46 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void updateProductUsersFields(Term term, V_Products products) throws IOException {
|
|
|
+ if (products == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String tableName = SearchConstants.PRODUCTS_USERS_TABLE_NAME;
|
|
|
+ Query query = new TermQuery(term);
|
|
|
+ int page = 1;
|
|
|
+ int size = 1000;
|
|
|
+ SPage<Document> documents = SearchUtils.getDocuments(tableName, query, page, size);
|
|
|
+ logger.info("更新关联个人物料索引:total=" + documents.getTotalElement());
|
|
|
+ int totalPage = documents.getTotalPage();
|
|
|
+ IndexWriter writer = null;
|
|
|
+ try {
|
|
|
+ writer = indexWriterManager.get(tableName);
|
|
|
+ String idField = SearchUtils.getIdField(tableName);
|
|
|
+ while (true) {
|
|
|
+ for (Document document : documents.getContent()) {
|
|
|
+ ProductUsersSimpleInfo productUser = DocumentToObjectUtils.toProductUser(document);
|
|
|
+ productUser.setPrId(products.getId());
|
|
|
+ productUser.setProduct(products);
|
|
|
+ writer.updateDocument(new Term(idField, document.get(idField)), ObjectToDocumentUtils.toDocument(productUser));
|
|
|
+ }
|
|
|
+ logger.info("更新关联批次索引:count=" + tableName + ", count=" + (size * (page - 1) + documents.getContent().size()));
|
|
|
+ writer.commit();
|
|
|
+ page++;
|
|
|
+ if (page > totalPage) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ documents = SearchUtils.getDocuments(tableName, query, page, size);
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ logger.error("", e);
|
|
|
+ } finally {
|
|
|
+ if (writer != null) {
|
|
|
+ indexSearcherManager.flushCache(tableName, writer, null);
|
|
|
+ indexWriterManager.release(tableName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Object delete(Object obj) {
|
|
|
if (obj != null) {
|
|
@@ -1015,6 +1133,9 @@ public class IndexServiceImpl implements IndexService {
|
|
|
} else if (obj instanceof V_Products) {
|
|
|
indexWriter.deleteDocuments(new Term(SearchConstants.PRODUCT_PRIVATE_ID_FIELD,
|
|
|
String.valueOf(((V_Products) obj).getId())));
|
|
|
+ } else if (obj instanceof ProductUsersSimpleInfo) {
|
|
|
+ indexWriter.deleteDocuments(new Term(SearchConstants.PRODUCT_USER_ID_FIELD,
|
|
|
+ String.valueOf(((ProductUsersSimpleInfo) obj).getId())));
|
|
|
} else {
|
|
|
throw new IllegalStateException("Message parsing failed!");
|
|
|
}
|