|
@@ -0,0 +1,1093 @@
|
|
|
+package com.caimei365.commodity.service.impl;
|
|
|
+
|
|
|
+import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Maps;
|
|
|
+import com.aliyun.opensearch.sdk.dependencies.org.json.JSONArray;
|
|
|
+import com.aliyun.opensearch.sdk.dependencies.org.json.JSONException;
|
|
|
+import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
|
|
|
+import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
|
|
|
+import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
|
|
|
+import com.aliyun.opensearch.sdk.generated.document.Command;
|
|
|
+import com.aliyun.opensearch.sdk.generated.document.DocumentConstants;
|
|
|
+import com.caimei365.commodity.components.SearchOpenService;
|
|
|
+import com.caimei365.commodity.mapper.SearchMapper;
|
|
|
+import com.caimei365.commodity.model.ResponseJson;
|
|
|
+import com.caimei365.commodity.model.search.*;
|
|
|
+import com.caimei365.commodity.service.SearchIndexService;
|
|
|
+import com.caimei365.commodity.service.SearchQueryService;
|
|
|
+import com.caimei365.commodity.utils.ImageUtils;
|
|
|
+import com.caimei365.commodity.utils.PriceUtil;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.util.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/4/6
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SearchIndexServiceImpl implements SearchIndexService {
|
|
|
+ @Value("${caimei.wwwDomain}")
|
|
|
+ private String domain;
|
|
|
+ @Resource
|
|
|
+ private SearchMapper searchMapper;
|
|
|
+ @Resource
|
|
|
+ private SearchOpenService searchOpenService;
|
|
|
+ @Resource
|
|
|
+ private SearchQueryService searchQueryService;
|
|
|
+ /**
|
|
|
+ * 初始化(添加)所有索引
|
|
|
+ *
|
|
|
+ * @return totalRecord
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> initAllIndexes() {
|
|
|
+ // 初始化主键ID,及索引记录数量
|
|
|
+ Integer mainId = 0;
|
|
|
+ // 从数据库获取商品数量
|
|
|
+ Integer productCount = searchMapper.findProductCount();
|
|
|
+ Integer productRecord = setAllProductData(mainId, productCount, true);
|
|
|
+ if (productCount > productRecord) {
|
|
|
+ log.warn("【初始化】批量添加商品文档异常,部分商品未添加。");
|
|
|
+ }
|
|
|
+ log.info(">>>>>>【商品】应加:"+productCount+",实加:"+productRecord);
|
|
|
+ mainId += productRecord;
|
|
|
+ // 从数据库获取供应商数量
|
|
|
+ Integer supplierCount = searchMapper.findSupplierCount();
|
|
|
+ Integer supplierRecord = setAllSupplierData(mainId, supplierCount, true);
|
|
|
+ if (supplierCount > supplierRecord) {
|
|
|
+ log.warn("【初始化】批量添加供应商文档异常,部分供应商未添加。");
|
|
|
+ }
|
|
|
+ log.info(">>>>>>【供应商】应加:"+supplierCount+",实加:"+supplierRecord);
|
|
|
+ mainId += supplierRecord;
|
|
|
+ // 从数据库获取项目仪器数量
|
|
|
+ Integer equipmentCount = searchMapper.findEquipmentCount();
|
|
|
+ Integer equipmentRecord = setAllEquipmentData(mainId, equipmentCount, true);
|
|
|
+ if (equipmentCount > equipmentRecord) {
|
|
|
+ log.warn("批量添加项目仪器文档异常,部分项目仪器未添加。");
|
|
|
+ }
|
|
|
+ log.info(">>>>>>【项目仪器】应加:"+equipmentCount+",实加:"+equipmentRecord);
|
|
|
+ mainId += equipmentRecord;
|
|
|
+ // 从数据库获取文章数量
|
|
|
+ Integer articleCount = searchMapper.findArticleCount();
|
|
|
+ Integer articleRecord = setAllArticleData(mainId, articleCount, true);
|
|
|
+ if (articleCount > articleRecord) {
|
|
|
+ log.warn("批量添加文章文档异常,部分文章未添加。");
|
|
|
+ }
|
|
|
+ log.info(">>>>>>【文章】应加:"+articleCount+",实加:"+articleRecord);
|
|
|
+ mainId += articleRecord;
|
|
|
+
|
|
|
+ log.info(">>>>>>>>>>>>添加总记录:" + mainId);
|
|
|
+ return ResponseJson.success(mainId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新所有商品
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateAllProductIndexes() {
|
|
|
+ // 获取失效商品数量
|
|
|
+ Integer invalidCount = searchMapper.findProductInvalidCount();
|
|
|
+ // 批量删除失效商品
|
|
|
+ batchDeleteProductDoc(invalidCount);
|
|
|
+ // 获取商品数量
|
|
|
+ Integer productCount = searchMapper.findProductCount();
|
|
|
+ // 获取最大文档ID
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId;
|
|
|
+ // 获取推送记录数
|
|
|
+ Integer productRecord = setAllProductData(mainId, productCount, false);
|
|
|
+ if (productCount > productRecord) {
|
|
|
+ log.warn("批量添加商品文档异常,部分商品未添加。");
|
|
|
+ }
|
|
|
+ log.info(">>>>>>【商品】应加:"+productCount+",实加:"+productRecord);
|
|
|
+ return ResponseJson.success(productRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新所有供应商
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateAllSupplierIndexes() {
|
|
|
+ // 获取失效数量
|
|
|
+ Integer invalidCount = searchMapper.findSupplierInvalidCount();
|
|
|
+ // 批量删除失效
|
|
|
+ batchDeleteSupplierDoc(invalidCount);
|
|
|
+ // 获取供应商数量
|
|
|
+ Integer supplierCount = searchMapper.findSupplierCount();
|
|
|
+ // 获取最大文档ID
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId;
|
|
|
+ // 获取推送记录数
|
|
|
+ Integer supplierRecord = setAllSupplierData(mainId, supplierCount, false);
|
|
|
+ if (supplierCount > supplierRecord) {
|
|
|
+ log.warn("批量添加供应商文档异常,部分供应商未添加。");
|
|
|
+ }
|
|
|
+ return ResponseJson.success(supplierRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新所有项目仪器
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateAllEquipmentIndexes() {
|
|
|
+ // 获取失效数量
|
|
|
+ Integer invalidCount = searchMapper.findEquipmentInvalidCount();
|
|
|
+ // 批量删除失效
|
|
|
+ batchDeleteEquipmentDoc(invalidCount);
|
|
|
+ // 获取项目仪器数量
|
|
|
+ Integer equipmentCount = searchMapper.findEquipmentCount();
|
|
|
+ // 获取最大文档ID
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId;
|
|
|
+ // 获取推送记录数
|
|
|
+ Integer equipmentRecord = setAllEquipmentData(mainId, equipmentCount, false);
|
|
|
+ if (equipmentCount > equipmentRecord) {
|
|
|
+ log.warn("批量添加项目仪器文档异常,部分项目仪器未添加。");
|
|
|
+ }
|
|
|
+ return ResponseJson.success(equipmentRecord);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新所有文章
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateAllArticleIndexes() {
|
|
|
+ // 获取失效数量
|
|
|
+ Integer invalidCount = searchMapper.findArticleInvalidCount();
|
|
|
+ // 批量删除失效
|
|
|
+ batchDeleteArticleDoc(invalidCount);
|
|
|
+ // 获取文章数量
|
|
|
+ Integer articleCount = searchMapper.findArticleCount();
|
|
|
+ // 获取最大文档ID
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId;
|
|
|
+ // 获取推送记录数
|
|
|
+ Integer articleRecord = setAllArticleData(mainId, articleCount, false);
|
|
|
+ if (articleCount > articleRecord) {
|
|
|
+ log.warn("批量添加文章文档异常,部分文章未添加。");
|
|
|
+ }
|
|
|
+ return ResponseJson.success(articleRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据商品Id更新
|
|
|
+ * @param productId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateProductIndexById(Integer productId){
|
|
|
+ ProductDO product = searchMapper.searchProductById(productId);
|
|
|
+ if (null != product) {
|
|
|
+ // 定义星范商品数据
|
|
|
+ List<DocumentDTO<MallProductDO>> mallProductList = new ArrayList<>();
|
|
|
+ // 星范商品设值,【必须先于商品文档设值】
|
|
|
+ Integer mallId = setMallProductDocument(mallProductList, productId);
|
|
|
+ // 定义商品文档数据
|
|
|
+ List<DocumentDTO<ProductDO>> productList = new ArrayList<>();
|
|
|
+ // 商品文档设值
|
|
|
+ setProductDocument(productList, product, mallId);
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+ // 主文档设值
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("product", productId);
|
|
|
+ if (mainId == -1) {
|
|
|
+ mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId+1;
|
|
|
+ }
|
|
|
+ // 商品文档 type=1
|
|
|
+ setMainDocument(1, mainList, mainId, productId);
|
|
|
+ try {
|
|
|
+ // 推送到阿里云
|
|
|
+ pushProductDocument(mainList, productList, mallProductList);
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(mainList.size());
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("添加商品文档异常:" + e);
|
|
|
+ return ResponseJson.error("添加商品文档异常", null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return deleteProductIndexById(productId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据供应商Id更新
|
|
|
+ *
|
|
|
+ * @param supplierId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateSupplierIndexById(Integer supplierId) {
|
|
|
+ SupplierDO supplier = searchMapper.searchSupplierById(supplierId);
|
|
|
+ if(null != supplier){
|
|
|
+ // 定义供应商文档数据
|
|
|
+ List<DocumentDTO<SupplierDO>> supplierList = new ArrayList<>();
|
|
|
+ // 供应商文档设值
|
|
|
+ setSupplierDocument(supplierList, supplier);
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+ // 主文档设值
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("shop", supplierId);
|
|
|
+ if (mainId == -1) {
|
|
|
+ mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId+1;
|
|
|
+ }
|
|
|
+ // 供应商文档 type=2
|
|
|
+ setMainDocument(2, mainList, mainId, supplierId);
|
|
|
+ try {
|
|
|
+ // 将文档列表转换成Json串,并推送到阿里云
|
|
|
+ pushSupplierDocument(mainList, supplierList);
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(mainList.size());
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("添加供应商文档异常:" + e);
|
|
|
+ return ResponseJson.error("添加供应商文档异常", null);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return deleteSupplierIndexById(supplierId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据项目仪器Id更新
|
|
|
+ *
|
|
|
+ * @param equipmentId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateEquipmentIndexById(Integer equipmentId) {
|
|
|
+ EquipmentDO equipment = searchMapper.searchEquipmentById(equipmentId);
|
|
|
+ if(null != equipment){
|
|
|
+ // 定义项目仪器文档数据
|
|
|
+ List<DocumentDTO<EquipmentDO>> equipmentList = new ArrayList<>();
|
|
|
+ // 项目仪器文档设值
|
|
|
+ setEquipmentDocument(equipmentList, equipment);
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+ // 主文档设值
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("equipment", equipmentId);
|
|
|
+ if (mainId == -1) {
|
|
|
+ mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId+1;
|
|
|
+ }
|
|
|
+ // 项目仪器文档 type=3
|
|
|
+ setMainDocument(3, mainList, mainId, equipmentId);
|
|
|
+ try {
|
|
|
+ // 将文档列表转换成Json串,并推送到阿里云
|
|
|
+ pushEquipmentDocument(mainList, equipmentList);
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(mainList.size());
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("添加项目仪器文档异常:" + e);
|
|
|
+ return ResponseJson.error("添加项目仪器文档异常", null);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return deleteEquipmentIndexById(equipmentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文章Id更新
|
|
|
+ *
|
|
|
+ * @param articleId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateArticleIndexById(Integer articleId) {
|
|
|
+ ArticleDO article = searchMapper.searchArticleById(articleId);
|
|
|
+ if(null != article){
|
|
|
+ // 定义文章文档数据
|
|
|
+ List<DocumentDTO<ArticleDO>> articleList = new ArrayList<>();
|
|
|
+ // 文章文档设值
|
|
|
+ setArticleDocument(articleList, article);
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+ // 主文档设值
|
|
|
+ Integer mainId = searchQueryService.getIdByDocId("article", articleId);
|
|
|
+ if (mainId == -1) {
|
|
|
+ mainId = searchQueryService.getIdByDocId("", null);
|
|
|
+ mainId = mainId == -1 ? 0 : mainId+1;
|
|
|
+ }
|
|
|
+ // 文章文档 type=4
|
|
|
+ setMainDocument(4, mainList, mainId, articleId);
|
|
|
+ try {
|
|
|
+ // 将主文档列表转换成Json串,并推送到阿里云
|
|
|
+ pushArticleDocument(mainList, articleList);
|
|
|
+ // 查询文章数量是否唯一,不唯一则删除后重新更新索引
|
|
|
+ Integer count = searchQueryService.getCountByDocId("article", articleId);
|
|
|
+ if (count > 1) {
|
|
|
+ log.info("文章重复,重复文章id>>>>>>>>>>>" + articleId);
|
|
|
+ deleteArticleIndexById(articleId);
|
|
|
+ updateArticleIndexById(articleId);
|
|
|
+ }
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(mainList.size());
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("添加文章文档异常:" + e);
|
|
|
+ return ResponseJson.error("添加文章文档异常", null);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return deleteArticleIndexById(articleId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据商品Id删除
|
|
|
+ *
|
|
|
+ * @param productId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteProductIndexById(Integer productId) {
|
|
|
+ // 根据商品ID获取星范ID
|
|
|
+ Integer mallId = searchMapper.findMallIdByProductId(productId);
|
|
|
+ try {
|
|
|
+ if (null != mallId){
|
|
|
+ deleteDocByTypeId("m_id", mallId, "search_product_mall");
|
|
|
+ }
|
|
|
+ deleteDocByTypeId("p_id", productId, "search_product");
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(1);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("删除商品文档异常:" + e);
|
|
|
+ return ResponseJson.error("删除商品文档异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据供应商Id删除
|
|
|
+ *
|
|
|
+ * @param supplierId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteSupplierIndexById(Integer supplierId) {
|
|
|
+ try {
|
|
|
+ deleteDocByTypeId("s_id", supplierId, "search_supplier");
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(1);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("删除供应商文档异常:" + e);
|
|
|
+ return ResponseJson.error("删除供应商文档异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据项目仪器Id删除
|
|
|
+ *
|
|
|
+ * @param equipmentId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteEquipmentIndexById(Integer equipmentId) {
|
|
|
+ try {
|
|
|
+ deleteDocByTypeId("e_id", equipmentId, "search_equipment");
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(1);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("删除项目仪器文档异常:" + e);
|
|
|
+ return ResponseJson.error("删除项目仪器文档异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文章Id删除
|
|
|
+ *
|
|
|
+ * @param articleId int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteArticleIndexById(Integer articleId) {
|
|
|
+ try {
|
|
|
+ deleteDocByTypeId("a_id", articleId, "search_article");
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(1);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("删除文章文档异常:" + e);
|
|
|
+ return ResponseJson.error("删除文章文档异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主文档Id删除
|
|
|
+ *
|
|
|
+ * @param id int
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteMainIndexById(Integer id) {
|
|
|
+ try {
|
|
|
+ deleteDocByTypeId("id", id, "search_main");
|
|
|
+ // 返回结果
|
|
|
+ return ResponseJson.success(1);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("删除主文档异常:" + e);
|
|
|
+ return ResponseJson.error("删除主文档异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文档类型key与类型Id删除
|
|
|
+ * @param type 类型key
|
|
|
+ * @param id 类型Id
|
|
|
+ * @param table 表名
|
|
|
+ * @throws OpenSearchClientException exp
|
|
|
+ * @throws OpenSearchException exp
|
|
|
+ */
|
|
|
+ private void deleteDocByTypeId(String type, Integer id, String table) throws OpenSearchClientException, OpenSearchException {
|
|
|
+ // 删除文档只需要设置需删除文档主键值即可
|
|
|
+ Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
|
|
|
+ deleteDoc.put(type, id);
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ String deleteJsonStr = setDeleteDocument(deleteDoc);
|
|
|
+ // 推送到阿里云
|
|
|
+ String deleteResult = searchOpenService.pushDocument(deleteJsonStr, table);
|
|
|
+ log.info(">>>>>>>>>>>>>>>delete document: 【"+ table +"("+ type +":" + id + "):" + deleteResult + "】");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除失效商品
|
|
|
+ * @param invalidCount int
|
|
|
+ */
|
|
|
+ private void batchDeleteProductDoc(Integer invalidCount) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(invalidCount.doubleValue() / 100);
|
|
|
+ // 定义返回结果
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取失效商品IDs
|
|
|
+ List<Integer> invalidIds = searchMapper.findProductInvalidIds();
|
|
|
+ // 根据商品IDs获取星范IDs
|
|
|
+ List<Integer> mallInvalidIds = searchMapper.findMallInvalidIdsByProductIds(invalidIds);
|
|
|
+ // 删除文档只需要设置需删除文档主键值即可
|
|
|
+ Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
|
|
|
+ if (null != mallInvalidIds) {
|
|
|
+ for (Integer mallId : mallInvalidIds) {
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ deleteDoc.put("m_id", mallId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Integer productId : invalidIds) {
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ deleteDoc.put("p_id", productId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String deleteJsonStr = setDeleteDocument(deleteDoc);
|
|
|
+ // 推送到阿里云
|
|
|
+ String deleteResult = searchOpenService.pushDocument(deleteJsonStr, "search_product");
|
|
|
+ log.info("删除文档:" + deleteResult);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.warn("删除文档异常:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置所有商品数据
|
|
|
+ * @param mainId int
|
|
|
+ * @param productCount int
|
|
|
+ * @param initFlag int
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private Integer setAllProductData(Integer mainId, Integer productCount, boolean initFlag) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(productCount.doubleValue() / 100);
|
|
|
+ Map<Integer, Integer> idsMap = Maps.newLinkedHashMap();
|
|
|
+ if (!initFlag) {
|
|
|
+ Integer record = searchQueryService.getRecordByDocType("product");
|
|
|
+ int l = (int)Math.ceil(record.doubleValue() / 500);
|
|
|
+ for (int i = 1; i <= l; i++) {
|
|
|
+ Map<Integer, Integer> idsTemp = searchQueryService.getMainIdsByDocType("product", i, 500);
|
|
|
+ idsMap.putAll(idsTemp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 定义返回结果
|
|
|
+ int productRecord = 0;
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取数据库商品列表的分页数据
|
|
|
+ List<ProductDO> dbList = searchMapper.searchProductList();
|
|
|
+ // 定义星范商品数据
|
|
|
+ List<DocumentDTO<MallProductDO>> mallProductList = new ArrayList<>();
|
|
|
+ // 定义商品文档数据
|
|
|
+ List<DocumentDTO<ProductDO>> productList = new ArrayList<>();
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+
|
|
|
+ int productMainId;
|
|
|
+ for (ProductDO product : dbList) {
|
|
|
+ // 星范商品设值,【必须先于商品文档设值】
|
|
|
+ Integer mallId = setMallProductDocument(mallProductList, product.getP_id());
|
|
|
+ // 商品文档设值
|
|
|
+ setProductDocument(productList, product, mallId);
|
|
|
+ // 主文档设值
|
|
|
+ Integer productId = product.getP_id();
|
|
|
+ if(initFlag){
|
|
|
+ productMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }else{
|
|
|
+ Integer tempId = idsMap.get(productId);
|
|
|
+ if (null==tempId || tempId<=0){
|
|
|
+ tempId = searchQueryService.getIdByDocId("product", productId);
|
|
|
+ }
|
|
|
+ if (null!=tempId && tempId>0) {
|
|
|
+ productMainId = tempId;
|
|
|
+ }else{
|
|
|
+ productMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 商品文档 type=1
|
|
|
+ setMainDocument(1, mainList, productMainId, productId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 推送到阿里云
|
|
|
+ pushProductDocument(mainList, productList, mallProductList);
|
|
|
+ // 添加到返回结果
|
|
|
+ productRecord += mainList.size();
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.warn("批量添加商品文档异常:" + e);
|
|
|
+ productRecord += 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return productRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送所有商品
|
|
|
+ * @param mainList list
|
|
|
+ * @param productList list
|
|
|
+ * @param mallProductList list
|
|
|
+ * @throws OpenSearchClientException exp
|
|
|
+ * @throws OpenSearchException exp
|
|
|
+ */
|
|
|
+ private void pushProductDocument(List<DocumentDTO<MainDO>> mainList, List<DocumentDTO<ProductDO>> productList, List<DocumentDTO<MallProductDO>> mallProductList) throws OpenSearchClientException, OpenSearchException {
|
|
|
+ // 将主文档列表转换成Json串,并推送到阿里云
|
|
|
+ String mainDocStr = new JSONArray(mainList).toString();
|
|
|
+ String mainResult = searchOpenService.pushDocument(mainDocStr, "search_main");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_main(" + mainList.size() + "):" + mainResult + "】" + mainList.toString());
|
|
|
+ // 将商品文档推送到阿里云
|
|
|
+ String productDocStr = new JSONArray(productList).toString();
|
|
|
+ String productResult = searchOpenService.pushDocument(productDocStr, "search_product");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_product(" + productList.size() + "):" + productResult + "】");
|
|
|
+ // 将星范商品推送到阿里云
|
|
|
+ if (mallProductList.size()>0) {
|
|
|
+ String mallProductStr = new JSONArray(mallProductList).toString();
|
|
|
+ String mallProductResult = searchOpenService.pushDocument(mallProductStr, "search_product_mall");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_product_mall(" + mallProductList.size() + "):" + mallProductResult + "】");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除失效供应商
|
|
|
+ * @param invalidCount int
|
|
|
+ */
|
|
|
+ private void batchDeleteSupplierDoc(Integer invalidCount) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(invalidCount.doubleValue() / 100);
|
|
|
+ // 定义返回结果
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取失效商品IDs
|
|
|
+ List<Integer> invalidIds = searchMapper.findSupplierInvalidIds();
|
|
|
+ // 删除文档只需要设置需删除文档主键值即可
|
|
|
+ Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
|
|
|
+ for (Integer productId : invalidIds) {
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ deleteDoc.put("s_id", productId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String deleteJsonStr = setDeleteDocument(deleteDoc);
|
|
|
+ // 推送到阿里云
|
|
|
+ String deleteResult = searchOpenService.pushDocument(deleteJsonStr, "search_supplier");
|
|
|
+ log.info("删除文档:" + deleteResult);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.warn("删除文档异常:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置所有供应商数据
|
|
|
+ * @param mainId int
|
|
|
+ * @param supplierCount int
|
|
|
+ * @param initFlag int
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private Integer setAllSupplierData(Integer mainId, Integer supplierCount, boolean initFlag) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(supplierCount.doubleValue() / 100);
|
|
|
+ Map<Integer, Integer> idsMap = Maps.newLinkedHashMap();
|
|
|
+ if (!initFlag) {
|
|
|
+ Integer record = searchQueryService.getRecordByDocType("shop");
|
|
|
+ int l = (int)Math.ceil(record.doubleValue() / 500);
|
|
|
+ for (int i = 1; i <= l; i++) {
|
|
|
+ Map<Integer, Integer> idsTemp = searchQueryService.getMainIdsByDocType("shop", i, 500);
|
|
|
+ idsMap.putAll(idsTemp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 定义返回结果
|
|
|
+ int supplierRecord = 0;
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取数据库供应商列表的分页数据
|
|
|
+ List<SupplierDO> dbList = searchMapper.searchSupplierList();
|
|
|
+ // 定义供应商文档数据
|
|
|
+ List<DocumentDTO<SupplierDO>> supplierList = new ArrayList<>();
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+
|
|
|
+ int supplierMainId;
|
|
|
+ for (SupplierDO supplier: dbList){
|
|
|
+ // 供应商文档设值
|
|
|
+ setSupplierDocument(supplierList, supplier);
|
|
|
+ // 主文档设值
|
|
|
+ Integer supplierId = supplier.getS_id();
|
|
|
+ if(initFlag){
|
|
|
+ supplierMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }else{
|
|
|
+ Integer tempId = idsMap.get(supplierId);
|
|
|
+ if (null==tempId || tempId<=0){
|
|
|
+ tempId = searchQueryService.getIdByDocId("shop", supplierId);
|
|
|
+ }
|
|
|
+ if (null!=tempId && tempId>0) {
|
|
|
+ supplierMainId = tempId;
|
|
|
+ }else{
|
|
|
+ supplierMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 供应商文档 type=2
|
|
|
+ setMainDocument(2, mainList, supplierMainId, supplierId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 推送到阿里云
|
|
|
+ pushSupplierDocument(mainList, supplierList);
|
|
|
+ // 添加到返回结果
|
|
|
+ supplierRecord += mainList.size();
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("批量添加供应商文档异常:" + e);
|
|
|
+ supplierRecord += 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return supplierRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送所有供应商
|
|
|
+ * @param mainList list
|
|
|
+ * @param supplierList list
|
|
|
+ * @throws OpenSearchClientException exp
|
|
|
+ * @throws OpenSearchException exp
|
|
|
+ */
|
|
|
+ private void pushSupplierDocument(List<DocumentDTO<MainDO>> mainList, List<DocumentDTO<SupplierDO>> supplierList) throws OpenSearchClientException, OpenSearchException {
|
|
|
+ // 将主文档列表转换成Json串,并推送到阿里云
|
|
|
+ String mainDocStr = new JSONArray(mainList).toString();
|
|
|
+ String mainResult = searchOpenService.pushDocument(mainDocStr, "search_main");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_main(" + mainList.size() + "):" + mainResult + "】" + mainList.toString());
|
|
|
+ // 将商品文档推送到阿里云
|
|
|
+ String supplierDocStr = new JSONArray(supplierList).toString();
|
|
|
+ String supplierResult = searchOpenService.pushDocument(supplierDocStr, "search_supplier");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_supplier(" + supplierList.size() + "):" + supplierResult + "】");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除失效项目仪器
|
|
|
+ * @param invalidCount int
|
|
|
+ */
|
|
|
+ private void batchDeleteEquipmentDoc(Integer invalidCount) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(invalidCount.doubleValue() / 100);
|
|
|
+ // 定义返回结果
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取失效商品IDs
|
|
|
+ List<Integer> invalidIds = searchMapper.findEquipmentInvalidIds();
|
|
|
+ // 删除文档只需要设置需删除文档主键值即可
|
|
|
+ Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
|
|
|
+ for (Integer productId : invalidIds) {
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ deleteDoc.put("e_id", productId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String deleteJsonStr = setDeleteDocument(deleteDoc);
|
|
|
+ // 推送到阿里云
|
|
|
+ String deleteResult = searchOpenService.pushDocument(deleteJsonStr, "search_equipment");
|
|
|
+ log.info("删除文档:" + deleteResult);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.warn("删除文档异常:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置所有项目仪器数据
|
|
|
+ * @param mainId int
|
|
|
+ * @param equipmentCount int
|
|
|
+ * @param initFlag int
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private Integer setAllEquipmentData(Integer mainId, Integer equipmentCount, boolean initFlag) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(equipmentCount.doubleValue() / 100);
|
|
|
+ Map<Integer, Integer> idsMap = Maps.newLinkedHashMap();
|
|
|
+ if (!initFlag) {
|
|
|
+ Integer record = searchQueryService.getRecordByDocType("equipment");
|
|
|
+ int l = (int)Math.ceil(record.doubleValue() / 500);
|
|
|
+ for (int i = 1; i <= l; i++) {
|
|
|
+ Map<Integer, Integer> idsTemp = searchQueryService.getMainIdsByDocType("equipment", i, 500);
|
|
|
+ idsMap.putAll(idsTemp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 定义返回结果
|
|
|
+ int equipmentRecord = 0;
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取数据库项目仪器列表的分页数据
|
|
|
+ List<EquipmentDO> dbList = searchMapper.searchEquipmentList();
|
|
|
+ // 定义项目仪器文档数据
|
|
|
+ List<DocumentDTO<EquipmentDO>> equipmentList = new ArrayList<>();
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+
|
|
|
+ int equipmentMainId;
|
|
|
+ for (EquipmentDO equipment: dbList){
|
|
|
+ // 项目仪器文档设值
|
|
|
+ setEquipmentDocument(equipmentList, equipment);
|
|
|
+ // 主文档设值
|
|
|
+ Integer equipmentId = equipment.getE_id();
|
|
|
+ if(initFlag){
|
|
|
+ equipmentMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }else{
|
|
|
+ Integer tempId = idsMap.get(equipmentId);
|
|
|
+ if (null==tempId || tempId<=0){
|
|
|
+ tempId = searchQueryService.getIdByDocId("equipment", equipmentId);
|
|
|
+ }
|
|
|
+ if (null!=tempId && tempId>0) {
|
|
|
+ equipmentMainId = tempId;
|
|
|
+ }else{
|
|
|
+ equipmentMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 项目仪器文档 type=3
|
|
|
+ setMainDocument(3, mainList, equipmentMainId, equipmentId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 将文档列表转换成Json串,并推送到阿里云
|
|
|
+ pushEquipmentDocument(mainList, equipmentList);
|
|
|
+ // 添加到返回结果
|
|
|
+ equipmentRecord += mainList.size();
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("批量添加项目仪器文档异常:" + e);
|
|
|
+ equipmentRecord += 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return equipmentRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送所有项目仪器
|
|
|
+ * @param mainList list
|
|
|
+ * @param equipmentList list
|
|
|
+ * @throws OpenSearchClientException exp
|
|
|
+ * @throws OpenSearchException exp
|
|
|
+ */
|
|
|
+ private void pushEquipmentDocument(List<DocumentDTO<MainDO>> mainList, List<DocumentDTO<EquipmentDO>> equipmentList) throws OpenSearchClientException, OpenSearchException {
|
|
|
+ // 将主文档列表转换成Json串,并推送到阿里云
|
|
|
+ String mainDocStr = new JSONArray(mainList).toString();
|
|
|
+ String mainResult = searchOpenService.pushDocument(mainDocStr, "search_main");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_main(" + mainList.size() + "):" + mainResult + "】" + mainList.toString());
|
|
|
+ // 将商品文档推送到阿里云
|
|
|
+ String equipmentDocStr = new JSONArray(equipmentList).toString();
|
|
|
+ String equipmentResult = searchOpenService.pushDocument(equipmentDocStr, "search_equipment");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_equipment(" + equipmentList.size() + "):" + equipmentResult + "】");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除失效文章
|
|
|
+ * @param invalidCount int
|
|
|
+ */
|
|
|
+ private void batchDeleteArticleDoc(Integer invalidCount) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(invalidCount.doubleValue() / 100);
|
|
|
+ // 定义返回结果
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取失效商品IDs
|
|
|
+ List<Integer> invalidIds = searchMapper.findArticleInvalidIds();
|
|
|
+ // 删除文档只需要设置需删除文档主键值即可
|
|
|
+ Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
|
|
|
+ for (Integer productId : invalidIds) {
|
|
|
+ // 根据ID(主键)删除文档
|
|
|
+ deleteDoc.put("a_id", productId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String deleteJsonStr = setDeleteDocument(deleteDoc);
|
|
|
+ // 推送到阿里云
|
|
|
+ String deleteResult = searchOpenService.pushDocument(deleteJsonStr, "search_article");
|
|
|
+ log.info("删除文档:" + deleteResult);
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.warn("删除文档异常:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置所有文章数据
|
|
|
+ * @param mainId int
|
|
|
+ * @param articleCount int
|
|
|
+ * @param initFlag int
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private Integer setAllArticleData(Integer mainId, Integer articleCount, boolean initFlag) {
|
|
|
+ // 批量推送,每100条推送一次
|
|
|
+ int loop = (int)Math.ceil(articleCount.doubleValue() / 100);
|
|
|
+ Map<Integer, Integer> idsMap = Maps.newLinkedHashMap();
|
|
|
+ if (!initFlag) {
|
|
|
+ Integer record = searchQueryService.getRecordByDocType("article");
|
|
|
+ int l = (int)Math.ceil(record.doubleValue() / 500);
|
|
|
+ for (int i = 1; i <= l; i++) {
|
|
|
+ Map<Integer, Integer> idsTemp = searchQueryService.getMainIdsByDocType("article", i, 500);
|
|
|
+ idsMap.putAll(idsTemp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 定义返回结果
|
|
|
+ int articleRecord = 0;
|
|
|
+ for (int i = 1; i <= loop; i++) {
|
|
|
+ PageHelper.startPage(i, 100);
|
|
|
+ // 获取数据库文章列表的分页数据
|
|
|
+ List<ArticleDO> dbList = searchMapper.searchArticleList();
|
|
|
+ // 定义文章文档数据
|
|
|
+ List<DocumentDTO<ArticleDO>> articleList = new ArrayList<>();
|
|
|
+ // 定义主文档入口数据
|
|
|
+ List<DocumentDTO<MainDO>> mainList = new ArrayList<>();
|
|
|
+
|
|
|
+ int articleMainId;
|
|
|
+ for (ArticleDO article: dbList){
|
|
|
+ // 文章文档设值
|
|
|
+ setArticleDocument(articleList, article);
|
|
|
+ // 主文档设值
|
|
|
+ Integer articleId = article.getA_id();
|
|
|
+ if(initFlag){
|
|
|
+ articleMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }else{
|
|
|
+ Integer tempId = idsMap.get(articleId);
|
|
|
+ if (null==tempId || tempId<=0){
|
|
|
+ tempId = searchQueryService.getIdByDocId("article", articleId);
|
|
|
+ }
|
|
|
+ if (null!=tempId && tempId>0) {
|
|
|
+ articleMainId = tempId;
|
|
|
+ }else{
|
|
|
+ articleMainId = mainId+1;
|
|
|
+ mainId+=1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 文章文档 type=4
|
|
|
+ setMainDocument(4, mainList, articleMainId, articleId);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 将文档列表转换成Json串,并推送到阿里云
|
|
|
+ pushArticleDocument(mainList, articleList);
|
|
|
+ // 添加到返回结果
|
|
|
+ articleRecord += mainList.size();
|
|
|
+ } catch (OpenSearchClientException | OpenSearchException | JSONException e) {
|
|
|
+ log.error("批量添加文章文档异常:" + e);
|
|
|
+ articleRecord += 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return articleRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送所有文章数据
|
|
|
+ * @param mainList list
|
|
|
+ * @param articleList list
|
|
|
+ * @throws OpenSearchClientException exp
|
|
|
+ * @throws OpenSearchException exp
|
|
|
+ */
|
|
|
+ private void pushArticleDocument(List<DocumentDTO<MainDO>> mainList, List<DocumentDTO<ArticleDO>> articleList) throws OpenSearchClientException, OpenSearchException {
|
|
|
+ // 将主文档列表转换成Json串,并推送到阿里云
|
|
|
+ String mainDocStr = new JSONArray(mainList).toString();
|
|
|
+ String mainResult = searchOpenService.pushDocument(mainDocStr, "search_main");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_main(" + mainList.size() + "):" + mainResult + "】" + mainList.toString());
|
|
|
+ // 将商品文档推送到阿里云
|
|
|
+ String articleDocStr = new JSONArray(articleList).toString();
|
|
|
+ String articleResult = searchOpenService.pushDocument(articleDocStr, "search_article");
|
|
|
+ log.info(">>>>>>>>>>>>>>>add document: 【search_article(" + articleList.size() + "):" + articleResult + "】");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文档设值
|
|
|
+ * @return docsJson
|
|
|
+ */
|
|
|
+ private String setDeleteDocument(Map<String, Object> deleteDoc) {
|
|
|
+ //此处设置删除文档处理
|
|
|
+ JSONObject deleteJson = new JSONObject();
|
|
|
+ deleteJson.put(DocumentConstants.DOC_KEY_CMD, Command.DELETE.toString());
|
|
|
+ deleteJson.put(DocumentConstants.DOC_KEY_FIELDS, deleteDoc);
|
|
|
+ JSONArray docsJsonArr = new JSONArray();
|
|
|
+ docsJsonArr.put(deleteJson);
|
|
|
+ return docsJsonArr.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品星范商品设值
|
|
|
+ *
|
|
|
+ * @param mallProductList list
|
|
|
+ * @param productId int
|
|
|
+ */
|
|
|
+ private Integer setMallProductDocument(List<DocumentDTO<MallProductDO>> mallProductList, Integer productId) {
|
|
|
+ // 是否是星范商品
|
|
|
+ int mallCount = searchMapper.countMallProduct(productId);
|
|
|
+ if (mallCount >= 1) {
|
|
|
+ MallProductDO mallProduct = searchMapper.searchMallProductByProductId(productId);
|
|
|
+ // 是否有阶梯价
|
|
|
+ Integer mallLadderFlag = searchMapper.getMallLadderPriceFlag(productId);
|
|
|
+ if (mallLadderFlag == 1) {
|
|
|
+ // 阶梯价
|
|
|
+ Double mallLadderPrice = searchMapper.getMallLowerLadderPrice(productId);
|
|
|
+ mallProduct.setM_price(mallLadderPrice);
|
|
|
+ }
|
|
|
+ mallProduct.setM_all(1);
|
|
|
+ // 搜索星范商品数据
|
|
|
+ DocumentDTO<MallProductDO> mallProductDoc = new DocumentDTO<>();
|
|
|
+ mallProductDoc.setCmd("add");
|
|
|
+ mallProductDoc.setFields(mallProduct);
|
|
|
+ mallProductList.add(mallProductDoc);
|
|
|
+ return mallProduct.getM_id();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品文档设值
|
|
|
+ * @param productList list
|
|
|
+ * @param product ProductDO
|
|
|
+ * @param mallId int
|
|
|
+ */
|
|
|
+ private void setProductDocument(List<DocumentDTO<ProductDO>> productList, ProductDO product, Integer mallId) {
|
|
|
+ String imagePath = ImageUtils.getImageURL("product", product.getP_image(), 0, domain);
|
|
|
+ // 搜索商品文档数据
|
|
|
+ product.setP_image(imagePath);
|
|
|
+ product.setM_id(mallId);
|
|
|
+ product.setP_all(1);
|
|
|
+ // 价格等级
|
|
|
+ product.setP_price_grade(PriceUtil.getpriceGrade(product.getP_price().doubleValue()));
|
|
|
+ DocumentDTO<ProductDO> productDoc = new DocumentDTO<>();
|
|
|
+ productDoc.setCmd("add");
|
|
|
+ productDoc.setFields(product);
|
|
|
+ productList.add(productDoc);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 供应商文档设值
|
|
|
+ *
|
|
|
+ * @param supplierList list
|
|
|
+ * @param supplier supplierDO
|
|
|
+ */
|
|
|
+ private void setSupplierDocument(List<DocumentDTO<SupplierDO>> supplierList, SupplierDO supplier) {
|
|
|
+ String imagePath = ImageUtils.getImageURL("supplierLogo", supplier.getS_logo(), 0, domain);
|
|
|
+ // 搜索供应商文档数据
|
|
|
+ supplier.setS_logo(imagePath);
|
|
|
+ supplier.setS_all(1);
|
|
|
+ DocumentDTO<SupplierDO> supplierDoc = new DocumentDTO<>();
|
|
|
+ supplierDoc.setCmd("add");
|
|
|
+ supplierDoc.setFields(supplier);
|
|
|
+ supplierList.add(supplierDoc);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 项目仪器文档设值
|
|
|
+ *
|
|
|
+ * @param equipmentList list
|
|
|
+ * @param equipment EquipmentDO
|
|
|
+ */
|
|
|
+ private void setEquipmentDocument(List<DocumentDTO<EquipmentDO>> equipmentList, EquipmentDO equipment) {
|
|
|
+ // 搜索供应商文档数据
|
|
|
+ equipment.setE_all(1);
|
|
|
+ DocumentDTO<EquipmentDO> equipmentDoc = new DocumentDTO<>();
|
|
|
+ equipmentDoc.setCmd("add");
|
|
|
+ equipmentDoc.setFields(equipment);
|
|
|
+ equipmentList.add(equipmentDoc);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文章文档设值
|
|
|
+ *
|
|
|
+ * @param articleList list
|
|
|
+ * @param article ArticleDO
|
|
|
+ */
|
|
|
+ private void setArticleDocument(List<DocumentDTO<ArticleDO>> articleList, ArticleDO article) {
|
|
|
+ String imagePath = ImageUtils.getImageURL("", article.getA_image(), 0, domain);
|
|
|
+ // 搜索供应商文档数据
|
|
|
+ String[] labelTexts = article.getA_label().split(",");
|
|
|
+ if(labelTexts.length > 0){
|
|
|
+ boolean notEmpty = StringUtil.isNotEmpty(labelTexts[0]);
|
|
|
+ if(notEmpty){
|
|
|
+ List<Integer> labelIdList = searchMapper.findLabelIdsByName(labelTexts);
|
|
|
+ Integer[] labelIds = labelIdList.toArray(new Integer[labelTexts.length]);
|
|
|
+ article.setA_label_text(labelTexts);
|
|
|
+ article.setA_label_ids(labelIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ article.setA_image(imagePath);
|
|
|
+ article.setA_all(1);
|
|
|
+ DocumentDTO<ArticleDO> articleDoc = new DocumentDTO<>();
|
|
|
+ articleDoc.setCmd("add");
|
|
|
+ articleDoc.setFields(article);
|
|
|
+ articleList.add(articleDoc);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品主文档设值
|
|
|
+ *
|
|
|
+ * @param mainList list
|
|
|
+ * @param mainId int
|
|
|
+ * @param docId int
|
|
|
+ */
|
|
|
+ private void setMainDocument(Integer type, List<DocumentDTO<MainDO>> mainList, Integer mainId, Integer docId) {
|
|
|
+ // 搜索主文档数据
|
|
|
+ DocumentDTO<MainDO> mainDoc = new DocumentDTO<>();
|
|
|
+ MainDO mainData = new MainDO();
|
|
|
+ mainData.setId(mainId);
|
|
|
+ if (type == 1) {
|
|
|
+ // 商品文档
|
|
|
+ mainData.setP_id(docId);
|
|
|
+ } else if (type == 2) {
|
|
|
+ // 供应商文档
|
|
|
+ mainData.setS_id(docId);
|
|
|
+ } else if (type == 3) {
|
|
|
+ // 项目仪器文档
|
|
|
+ mainData.setE_id(docId);
|
|
|
+ } else if (type == 4) {
|
|
|
+ // 文章文档
|
|
|
+ mainData.setA_id(docId);
|
|
|
+ }
|
|
|
+ mainDoc.setCmd("add");
|
|
|
+ mainDoc.setFields(mainData);
|
|
|
+ mainList.add(mainDoc);
|
|
|
+ }
|
|
|
+}
|