Просмотр исходного кода

监听product$component和v$product$cmpgoods,实时更新批次索引

sunyj 8 лет назад
Родитель
Сommit
bf75ea502a

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

@@ -14,4 +14,5 @@ import com.uas.search.console.model.ComponentSimpleInfo;
 public interface ComponentSimpleInfoDao
 		extends JpaSpecificationExecutor<ComponentSimpleInfo>, JpaRepository<ComponentSimpleInfo, Long> {
 
+	public ComponentSimpleInfo findByUuid(String uuid);
 }

+ 72 - 0
search-console/src/main/java/com/uas/search/console/dao/GoodsSimpleInfoDao.java

@@ -1,7 +1,16 @@
 package com.uas.search.console.dao;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 
+import com.uas.search.console.model.ComponentSimpleInfo;
+import com.uas.search.console.model.GoodsSimpleInfo;
+import com.uas.search.console.model.StoreSimpleInfo;
+import com.uas.search.console.model.TradeGoodsSimpleInfo;
+
 /**
  * @author sunyj
  * @since 2017年7月8日 下午7:45:01
@@ -9,4 +18,67 @@ import org.springframework.stereotype.Repository;
 @Repository
 public class GoodsSimpleInfoDao {
 
+	@Autowired
+	private TradeGoodsSimpleInfoDao tradeGoodsDao;
+
+	@Autowired
+	private ComponentSimpleInfoDao componentDao;
+
+	@Autowired
+	private StoreSimpleInfoDao storeDao;
+
+	/**
+	 * 根据批次id或者器件id获取批次
+	 * 
+	 * @param goods
+	 * @return
+	 */
+	public List<GoodsSimpleInfo> find(GoodsSimpleInfo goods) {
+		if (goods == null) {
+			return null;
+		}
+		List<GoodsSimpleInfo> goodsesList = new ArrayList<>();
+		if (goods.getGoId() != null) {
+			goodsesList.add(findByGoId(goods.getGoId()));
+		} else if (goods.getCmpId() != null) {
+			goodsesList.addAll(findByCmpId(goods.getCmpId()));
+		}
+		return goodsesList;
+	}
+
+	/**
+	 * 根据批次id获取批次信息
+	 * 
+	 * @param goId
+	 * @return
+	 */
+	private GoodsSimpleInfo findByGoId(Long goId) {
+		TradeGoodsSimpleInfo tradeGoods = tradeGoodsDao.findOne(goId);
+		ComponentSimpleInfo component = componentDao.findByUuid(tradeGoods.getCmpUuid());
+		StoreSimpleInfo store = null;
+		if (tradeGoods.getStoreId() != null) {
+			store = storeDao.findByUuid(tradeGoods.getStoreId());
+		}
+		return new GoodsSimpleInfo(tradeGoods, store, component);
+	}
+
+	/**
+	 * 根据器件id获取批次信息
+	 * 
+	 * @param cmpId
+	 * @return
+	 */
+	private List<GoodsSimpleInfo> findByCmpId(Long cmpId) {
+		ComponentSimpleInfo component = componentDao.findOne(cmpId);
+		List<TradeGoodsSimpleInfo> tradeGoodsesList = tradeGoodsDao.findByCmpUuid(component.getUuid());
+		List<GoodsSimpleInfo> goodsesList = new ArrayList<>();
+		for (TradeGoodsSimpleInfo tradeGoods : tradeGoodsesList) {
+			StoreSimpleInfo store = null;
+			if (tradeGoods.getStoreId() != null) {
+				store = storeDao.findByUuid(tradeGoods.getStoreId());
+			}
+			goodsesList.add(new GoodsSimpleInfo(tradeGoods, store, component));
+		}
+		return goodsesList;
+	}
 }

+ 18 - 0
search-console/src/main/java/com/uas/search/console/dao/StoreSimpleInfoDao.java

@@ -0,0 +1,18 @@
+package com.uas.search.console.dao;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.stereotype.Repository;
+
+import com.uas.search.console.model.StoreSimpleInfo;
+
+/**
+ * @author sunyj
+ * @since 2017年7月22日 下午8:16:07
+ */
+@Repository
+public interface StoreSimpleInfoDao
+		extends JpaSpecificationExecutor<StoreSimpleInfo>, JpaRepository<StoreSimpleInfo, Long> {
+
+	public StoreSimpleInfo findByUuid(String uuid);
+}

+ 20 - 0
search-console/src/main/java/com/uas/search/console/dao/TradeGoodsSimpleInfoDao.java

@@ -0,0 +1,20 @@
+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.stereotype.Repository;
+
+import com.uas.search.console.model.TradeGoodsSimpleInfo;
+
+/**
+ * @author sunyj
+ * @since 2017年7月22日 下午8:16:07
+ */
+@Repository
+public interface TradeGoodsSimpleInfoDao
+		extends JpaSpecificationExecutor<TradeGoodsSimpleInfo>, JpaRepository<TradeGoodsSimpleInfo, Long> {
+
+	public List<TradeGoodsSimpleInfo> findByCmpUuid(String cmpUuid);
+}

+ 34 - 0
search-console/src/main/java/com/uas/search/console/jms/QueueMessageParser.java

@@ -23,6 +23,7 @@ import com.uas.search.console.dao.PurchaseInvoiceSimpleInfoDao;
 import com.uas.search.console.dao.PurchaseSimpleInfoDao;
 import com.uas.search.console.model.BrandSimpleInfo;
 import com.uas.search.console.model.ComponentSimpleInfo;
+import com.uas.search.console.model.GoodsSimpleInfo;
 import com.uas.search.console.model.KindSimpleInfo;
 import com.uas.search.console.model.OrderInvoiceSimpleInfo;
 import com.uas.search.console.model.OrderSimpleInfo;
@@ -104,6 +105,8 @@ public class QueueMessageParser {
 				objects = parseBrand(jsonObject);
 			} else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
 				objects = parseComponent(jsonObject);
+			} else if (table.equals(SearchConstants.GOODS_TABLE_NAME)) {
+				objects = parseGoods(jsonObject);
 			} else if (table.equals(SearchConstants.ORDER_TABLE_NAME)) {
 				objects = parseOrder(jsonObject);
 			} else if (table.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
@@ -202,6 +205,37 @@ public class QueueMessageParser {
 		return components;
 	}
 
+	/**
+	 * 对goods进行解析
+	 * 
+	 * @param jsonObject
+	 * @return 批次对象数组
+	 * @throws JSONException
+	 */
+	// {"method":"value1","table":"v$product$cmpgoods","cmpIds":[142]}
+	// {"method":"value1","table":"v$product$cmpgoods","goIds":[114287]}
+	private GoodsSimpleInfo[] parseGoods(JSONObject jsonObject) throws JSONException {
+		List<GoodsSimpleInfo> goodsesList = new ArrayList<>();
+		if (jsonObject.containsKey("cmpIds")) {
+			Set<Long> ids = getIds(jsonObject.getJSONArray("cmpIds"));
+			for (Long id : ids) {
+				GoodsSimpleInfo goods = new GoodsSimpleInfo();
+				goods.setCmpId(id);
+				goodsesList.add(goods);
+			}
+		} else {
+			Set<Long> ids = getIds(jsonObject.getJSONArray("goIds"));
+			for (Long id : ids) {
+				GoodsSimpleInfo goods = new GoodsSimpleInfo();
+				goods.setGoId(id);
+				goodsesList.add(goods);
+			}
+		}
+		GoodsSimpleInfo[] goodses = new GoodsSimpleInfo[goodsesList.size()];
+		goodsesList.toArray(goodses);
+		return goodses;
+	}
+
 	/**
 	 * 对销售单进行解析
 	 * 

+ 20 - 0
search-console/src/main/java/com/uas/search/console/model/GoodsSimpleInfo.java

@@ -67,6 +67,26 @@ public class GoodsSimpleInfo implements Serializable {
 	 */
 	private BrandSimpleInfo brand;
 
+	public GoodsSimpleInfo() {
+	}
+
+	public GoodsSimpleInfo(TradeGoodsSimpleInfo tradeGoods, StoreSimpleInfo store, ComponentSimpleInfo component) {
+		if (tradeGoods != null) {
+			this.goId = tradeGoods.getId();
+			this.goReserve = tradeGoods.getReserve();
+			this.goMinPriceRMB = tradeGoods.getMinPriceRMB();
+			this.goMinPriceUSD = tradeGoods.getMinPriceUSD();
+			this.crName = tradeGoods.getCrName();
+		}
+		this.store = store;
+		if (component != null) {
+			this.cmpId = component.getId();
+			this.cmpCode = component.getCode();
+			this.kind = component.getKind();
+			this.brand = component.getBrand();
+		}
+	}
+
 	public String getId() {
 		return id;
 	}

+ 25 - 1
search-console/src/main/java/com/uas/search/console/model/StoreSimpleInfo.java

@@ -2,26 +2,50 @@ package com.uas.search.console.model;
 
 import java.io.Serializable;
 
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
 /**
  * 店铺
  * 
  * @author sunyj
  * @since 2017年7月8日 下午6:14:26
  */
+@Entity
+@Table(name = "store$info")
 public class StoreSimpleInfo implements Serializable {
 
 	private static final long serialVersionUID = 1L;
 
+	/**
+	 * id
+	 */
+	@Id
+	@Column(name = "id")
+	private Long id;
+
 	/**
 	 * uuid
 	 */
+	@Column(name = "ST_UUID")
 	private String uuid;
 
 	/**
 	 * 类型
 	 */
+	@Column(name = "ST_TYPE")
 	private String type;
 
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
 	public String getUuid() {
 		return uuid;
 	}
@@ -40,7 +64,7 @@ public class StoreSimpleInfo implements Serializable {
 
 	@Override
 	public String toString() {
-		return "StoreSimpleInfo [uuid=" + uuid + ", type=" + type + "]";
+		return "StoreSimpleInfo [id=" + id + ", uuid=" + uuid + ", type=" + type + "]";
 	}
 
 }

+ 127 - 0
search-console/src/main/java/com/uas/search/console/model/TradeGoodsSimpleInfo.java

@@ -0,0 +1,127 @@
+package com.uas.search.console.model;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+/**
+ * 批次简要信息,只用于索引的创建与查询
+ * 
+ * @author sunyj
+ * @since 2017年7月22日 下午8:06:10
+ */
+@Entity
+@Table(name = "trade$goods")
+public class TradeGoodsSimpleInfo implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * id
+	 */
+	@Id
+	@Column(name = "go_id")
+	private Long id;
+
+	/**
+	 * 批次的库存
+	 */
+	@Column(name = "go_reserve")
+	private Double reserve;
+
+	/**
+	 * 批次的人民币价格
+	 */
+	@Column(name = "go_minpricermb")
+	private Double minPriceRMB;
+
+	/**
+	 * 批次的美元价格
+	 */
+	@Column(name = "go_minpriceusd")
+	private Double minPriceUSD;
+
+	/**
+	 * 货币
+	 */
+	@Column(name = "cr_name")
+	private String crName;
+
+	/**
+	 * 货源uuid
+	 */
+	@Column(name = "go_storeid")
+	private String storeId;
+
+	/**
+	 * 器件的uuid
+	 */
+	@Column(name = "cmp_uuid")
+	private String cmpUuid;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public Double getReserve() {
+		return reserve;
+	}
+
+	public void setReserve(Double reserve) {
+		this.reserve = reserve;
+	}
+
+	public Double getMinPriceRMB() {
+		return minPriceRMB;
+	}
+
+	public void setMinPriceRMB(Double minPriceRMB) {
+		this.minPriceRMB = minPriceRMB;
+	}
+
+	public Double getMinPriceUSD() {
+		return minPriceUSD;
+	}
+
+	public void setMinPriceUSD(Double minPriceUSD) {
+		this.minPriceUSD = minPriceUSD;
+	}
+
+	public String getCrName() {
+		return crName;
+	}
+
+	public void setCrName(String crName) {
+		this.crName = crName;
+	}
+
+	public String getStoreId() {
+		return storeId;
+	}
+
+	public void setStoreId(String storeId) {
+		this.storeId = storeId;
+	}
+
+	public String getCmpUuid() {
+		return cmpUuid;
+	}
+
+	public void setCmpUuid(String cmpUuid) {
+		this.cmpUuid = cmpUuid;
+	}
+
+	@Override
+	public String toString() {
+		return "TradeGoods [id=" + id + ", reserve=" + reserve + ", minPriceRMB=" + minPriceRMB + ", minPriceUSD="
+				+ minPriceUSD + ", crName=" + crName + ", storeId=" + storeId + ", cmpUuid=" + cmpUuid + "]";
+	}
+
+}

+ 35 - 3
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -30,6 +30,7 @@ import com.uas.platform.core.model.PageParams;
 import com.uas.search.console.LuceneProperties;
 import com.uas.search.console.dao.BrandSimpleInfoDao;
 import com.uas.search.console.dao.ComponentSimpleInfoDao;
+import com.uas.search.console.dao.GoodsSimpleInfoDao;
 import com.uas.search.console.dao.KindSimpleInfoDao;
 import com.uas.search.console.dao.LuceneQueueMessageDao;
 import com.uas.search.console.dao.OrderInvoiceSimpleInfoDao;
@@ -81,6 +82,9 @@ public class IndexServiceImpl implements IndexService {
 	@Autowired
 	private ComponentSimpleInfoDao componentDao;
 
+	@Autowired
+	private GoodsSimpleInfoDao goodsDao;
+
 	@Autowired
 	private OrderSimpleInfoDao orderDao;
 
@@ -570,6 +574,8 @@ public class IndexServiceImpl implements IndexService {
 				} else if (obj instanceof ComponentSimpleInfo) {
 					indexWriter.deleteDocuments(new Term(SearchConstants.COMPONENT_ID_FIELD,
 							String.valueOf(((ComponentSimpleInfo) obj).getId())));
+				} else if (obj instanceof GoodsSimpleInfo) {
+					indexWriter.deleteDocuments(toTerm((GoodsSimpleInfo) obj));
 				} else if (obj instanceof OrderSimpleInfo) {
 					indexWriter.deleteDocuments(
 							new Term(SearchConstants.ORDER_ID_FIELD, String.valueOf(((OrderSimpleInfo) obj).getId())));
@@ -598,6 +604,21 @@ public class IndexServiceImpl implements IndexService {
 		return null;
 	}
 
+	/**
+	 * 根据goods构造Term
+	 * 
+	 * @param goods
+	 * @return
+	 */
+	private Term toTerm(GoodsSimpleInfo goods) {
+		if (goods.getGoId() != null) {
+			return new Term(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(goods.getGoId()));
+		} else if (goods.getCmpId() != null) {
+			return new Term(SearchConstants.GOODS_CMP_ID_FIELD, String.valueOf(goods.getCmpId()));
+		}
+		return null;
+	}
+
 	@Override
 	public List<Object> maintainIndexes(ParsedQueueMessage parsedQueueMessage) {
 		if (parsedQueueMessage == null) {
@@ -613,9 +634,20 @@ public class IndexServiceImpl implements IndexService {
 			// 新增、更新索引
 			if (parsedQueueMessage.isInsert() || parsedQueueMessage.isUpdate()) {
 				for (Object object : objects) {
-					Object maintainedObject = update(object);
-					if (maintainedObject != null) {
-						maintainedObjects.add(maintainedObject);
+					if (object instanceof GoodsSimpleInfo) {
+						List<GoodsSimpleInfo> goodsesList = goodsDao.find((GoodsSimpleInfo) object);
+						delete(object);
+						for (GoodsSimpleInfo goods : goodsesList) {
+							Object maintainedObject = save(goods);
+							if (maintainedObject != null) {
+								maintainedObjects.add(maintainedObject);
+							}
+						}
+					} else {
+						Object maintainedObject = update(object);
+						if (maintainedObject != null) {
+							maintainedObjects.add(maintainedObject);
+						}
 					}
 				}
 			}

+ 7 - 3
search-console/src/main/java/com/uas/search/console/util/ObjectToDocumentUtils.java

@@ -187,19 +187,22 @@ public class ObjectToDocumentUtils {
 	 * @return
 	 */
 	public static Document toDocument(GoodsSimpleInfo goods) {
-		if (goods == null || goods.getId() == null || StringUtils.isEmpty(goods.getCmpId())
-				|| StringUtils.isEmpty(goods.getCmpCode()) || goods.getKind() == null || goods.getBrand() == null) {
+		if (goods == null || StringUtils.isEmpty(goods.getCmpId()) || StringUtils.isEmpty(goods.getCmpCode())
+				|| goods.getKind() == null || goods.getBrand() == null) {
 			return null;
 		}
 
 		Document document = new Document();
-		document.add(new StringField(SearchConstants.GOODS_ID_FIELD, goods.getId(), Store.YES));
+		if (goods.getId() != null) {
+			document.add(new StringField(SearchConstants.GOODS_ID_FIELD, goods.getId(), Store.YES));
+		}
 		if (goods.getGoId() != null) {
 			document.add(
 					new StringField(SearchConstants.GOODS_GO_ID_FIELD, String.valueOf(goods.getGoId()), Store.YES));
 		}
 		if (goods.getGoReserve() != null) {
 			document.add(new DoubleDocValuesField(SearchConstants.GOODS_GO_RESERVE_FIELD, goods.getGoReserve()));
+			document.add(new DoubleField(SearchConstants.GOODS_GO_RESERVE_FIELD, goods.getGoReserve(), Store.YES));
 		}
 		if (goods.getGoMinPriceRMB() != null) {
 			document.add(
@@ -239,6 +242,7 @@ public class ObjectToDocumentUtils {
 		}
 		if (kind.getLevel() != null) {
 			document.add(new NumericDocValuesField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getIsLeaf()));
+			document.add(new LongField(SearchConstants.GOODS_KI_LEVEL_FIELD, kind.getIsLeaf(), Store.YES));
 		}
 		if (kind.getIsLeaf() != null) {
 			document.add(