|
@@ -1,18 +1,18 @@
|
|
|
package com.uas.search.console.b2b.jms;
|
|
package com.uas.search.console.b2b.jms;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONException;
|
|
import com.alibaba.fastjson.JSONException;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.search.b2b.service.SearchService.Table_name;
|
|
import com.uas.search.b2b.service.SearchService.Table_name;
|
|
|
-import com.uas.search.console.b2b.core.util.ContextUtils;
|
|
|
|
|
import com.uas.search.console.b2b.model.ParsedQueueMessage;
|
|
import com.uas.search.console.b2b.model.ParsedQueueMessage;
|
|
|
-import com.uas.search.console.b2b.service.InnerSearchService;
|
|
|
|
|
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
|
|
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -25,9 +25,6 @@ import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
|
|
|
@Service
|
|
@Service
|
|
|
public class QueueMessageParser {
|
|
public class QueueMessageParser {
|
|
|
|
|
|
|
|
- private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
|
|
|
|
|
- InnerSearchService.class);
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 对得到的json消息进行解析
|
|
* 对得到的json消息进行解析
|
|
|
*
|
|
*
|
|
@@ -84,39 +81,29 @@ public class QueueMessageParser {
|
|
|
* @return 实体对象列表
|
|
* @return 实体对象列表
|
|
|
*/
|
|
*/
|
|
|
private <T> List<T> parseToList(JSONObject jsonObject, Class<T> clazz) {
|
|
private <T> List<T> parseToList(JSONObject jsonObject, Class<T> clazz) {
|
|
|
- //TODO 优化:HashSet<Long>、findAll
|
|
|
|
|
- Object[] ids = jsonObject.getJSONArray("ids").toArray();
|
|
|
|
|
- List<T> list = new ArrayList<>();
|
|
|
|
|
// 获取实体的dao
|
|
// 获取实体的dao
|
|
|
JpaRepository<T, Long> dao = ClassAndTableNameUtils.getDao(clazz);
|
|
JpaRepository<T, Long> dao = ClassAndTableNameUtils.getDao(clazz);
|
|
|
|
|
+ Set<Long> ids = getIds(jsonObject.getJSONArray("ids"));
|
|
|
|
|
+ return dao.findAll(ids);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从队列消息中获取id(同时消除重复id)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param jsonArray
|
|
|
|
|
+ * 队列消息中json格式的id
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private Set<Long> getIds(JSONArray jsonArray) {
|
|
|
|
|
+ if (jsonArray == null || jsonArray.isEmpty()) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ Object[] ids = jsonArray.toArray();
|
|
|
|
|
+ Set<Long> idsSet = new HashSet<>();
|
|
|
for (int i = 0; i < ids.length; i++) {
|
|
for (int i = 0; i < ids.length; i++) {
|
|
|
- Long id = Long.parseLong(ids[i].toString());
|
|
|
|
|
- // 更新索引前,数据库中的数据与本地索引中的数据进行比较
|
|
|
|
|
- T element = dao.findOne(id);
|
|
|
|
|
- T localElement = innerSearchService.getObjectById(id, clazz);
|
|
|
|
|
- // delete操作
|
|
|
|
|
- if (jsonObject.getString("method").equalsIgnoreCase("delete")) {
|
|
|
|
|
- if (element != null) {
|
|
|
|
|
- // 删除之后,数据还存在,并且与本地索引数据一样,说明是进行了回退,或者重新插入了相同的数据,不对索引进行更新
|
|
|
|
|
- if (localElement == null || localElement.equals(element)) {
|
|
|
|
|
- element = null;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- element = localElement;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // insert和update需分开对待(update不进行下列判断,直接更新)
|
|
|
|
|
- // update来源还可能来自从表的变化,而equals是不比较从表的
|
|
|
|
|
- // (如果比较的话,对于insert操作,即使是数据没变,而从表数据在之后变化了的话,也会视为两个数据,导致索引无法删除,之后会添加重复的索引)
|
|
|
|
|
- else if (jsonObject.getString("method").equalsIgnoreCase("insert")) {
|
|
|
|
|
- // 本地有相同的数据,不更新索引
|
|
|
|
|
- if (localElement != null && localElement.equals(element)) {
|
|
|
|
|
- element = null;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- list.add(element);
|
|
|
|
|
|
|
+ idsSet.add(Long.parseLong(ids[i].toString()));
|
|
|
}
|
|
}
|
|
|
- return list;
|
|
|
|
|
|
|
+ return idsSet;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|