Kaynağa Gözat

component下载调优

wangyc 6 yıl önce
ebeveyn
işleme
d21b68d5a0

+ 15 - 0
mall-search/src/main/java/com/uas/search/model/Component.java

@@ -137,6 +137,12 @@ public class Component implements Serializable, RowMapper {
 	@OrderBy("detno")
 	private Set<PropertyValue> properties;
 
+	/**
+	 * 为了下载Component时转换propertyvalue用
+	 */
+	@Transient
+	private PropertyValue propertyValue;
+
 	public Long getId() {
 		return id;
 	}
@@ -225,6 +231,14 @@ public class Component implements Serializable, RowMapper {
 		this.properties = properties;
 	}
 
+	public PropertyValue getPropertyValue() {
+		return propertyValue;
+	}
+
+	public void setPropertyValue(PropertyValue propertyValue) {
+		this.propertyValue = propertyValue;
+	}
+
 	public Long getVisitCount() {
 		return visitCount;
 	}
@@ -288,6 +302,7 @@ public class Component implements Serializable, RowMapper {
 		component.setKind(new Kind().mapRow(rs, i));
 		component.setBrandId(rs.getLong("cmp_brid"));
 		component.setBrand(new Brand().mapRow(rs, i));
+		component.setPropertyValue(new PropertyValue().mapRow(rs, i));
 		return component;
 	}
 }

+ 0 - 1
mall-search/src/main/java/com/uas/search/model/PropertyValue.java

@@ -1,7 +1,6 @@
 package com.uas.search.model;
 
 import java.io.Serializable;
-
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import javax.persistence.CascadeType;

+ 22 - 9
mall-search/src/main/java/com/uas/search/service/impl/JdbcServiceImpl.java

@@ -21,8 +21,11 @@ import com.uas.search.util.CollectionUtils;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
 import javax.persistence.Column;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.EmptyResultDataAccessException;
@@ -99,7 +102,7 @@ public class JdbcServiceImpl<T> implements JdbcService{
         List<T> data = null;
         // 器件索引
         if (SearchConstants.COMPONENT_TABLE_NAME.equals(tableName)) {
-            data = queryComponents(sql, true);
+            data = queryComponents(sql, startId, endId, true);
         }
         // Product索引
         if (PRODUCTS_PRIVATE_TABLE_NAME.equals(tableName)) {
@@ -126,15 +129,25 @@ public class JdbcServiceImpl<T> implements JdbcService{
      * @param needProperties 是否需要获取属性值
      * @return
      */
-    private List<T> queryComponents(String sql, boolean needProperties) {
-        List<Component> components = jdbcTemplate.query(sql.replace("where", "left join product$brand on product$brand.br_id=product$component.cmp_brid left join product$kind on product$kind.ki_id = product$component.cmp_kiid where"), new Component());
-        String propertyValueSql = "select * from product$propertyvalue left join product$property on product$propertyvalue.pv_propertyid = product$property.pt_id where pv_componentid = ";
-        if (!CollectionUtils.isEmpty(components) && needProperties) {
-            for (Component component : components) {
-                    component.setProperties(new HashSet<>(jdbcTemplate.query(propertyValueSql + component.getId(), new PropertyValue())));
+    private List<T> queryComponents(String sql, Long startId, Long endId, boolean needProperties) {
+        List<Component> components = new ArrayList<>();
+        if (needProperties) {
+            String propertyValueSql = "select * from product$component left join product$propertyvalue on product$propertyvalue.pv_componentid = product$component.cmp_id left join product$property on product$propertyvalue.pv_propertyid = product$property.pt_id left join product$kind on product$component.cmp_kiid = product$kind.ki_id left join product$brand on product$component.cmp_brid = product$brand.br_id where product$component.cmp_id between %d and %d";
+            List<Component> componentList = jdbcTemplate.query(String.format(propertyValueSql, startId, endId), new Component());
+            Map<Long, List<Component>> componentMap = componentList.stream().collect(Collectors.groupingBy(Component :: getId));
+            for (Entry<Long, List<Component>> entery : componentMap.entrySet()) {
+                List<Component> componentLists = componentMap.get(entery.getKey());
+                Component finalComponent = componentLists.get(0);
+                Set<PropertyValue> propertyValues = componentLists.stream().filter(
+                    c -> c.getPropertyValue() != null).map(c -> c.getPropertyValue()).collect(Collectors.toSet());
+                finalComponent.setProperties(propertyValues);
+                finalComponent.setPropertyValue(null);
+                components.add(finalComponent);
             }
+            return (List<T>)components;
+        } else {
+            return jdbcTemplate.query(sql.replace("where", "left join product$brand on product$brand.br_id=product$component.cmp_brid left join product$kind on product$kind.ki_id = product$component.cmp_kiid where"), new Component());
         }
-        return (List<T>) components;
     }
 
     /**