Skip to content

Commit

Permalink
#3 完善测试代码,脚本,公共类等
Browse files Browse the repository at this point in the history
  • Loading branch information
lujun committed Nov 24, 2017
1 parent 8909fa6 commit c93f08a
Show file tree
Hide file tree
Showing 16 changed files with 554 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ List<Inventory> getAvailableInventoryForProductAndWarehouse(@Param("productId")
"RETURN inventory")
List<Inventory> getAvailableInventoryForProductList(@Param("productIds") String[] productIds);

@Query("MATCH (inventory:Inventory),(product:Product) \n" +
"\t product.id={productId} \n" +
@Query("MATCH (product:Product), \n" +
"\t (product)<-[:PRODUCT_STOCK]-(inventory:Inventory) WHERE product.id={productId} \n" +
"\t set inventory.inventoryNumber={productNum} \n" +
"\t RETURN inventory")
Inventory modifyProductNum(@Param("productId") String productId,@Param("productNum") long productNum);
Inventory modifyProductNum(@Param("productId") String productId,@Param("productNum") Long productNum);

@Query("MATCH (inventory:Inventory),(product:Product) \n" +
"\t WHERE product.id={productId} \n" +
"\t (product)<-[:PRODUCT_STOCK]-(inventory:Inventory) WHERE product.productId={productId} \n" +
"\t RETURN COUNT(inventory)")
Long getInventoryNumByPid(@Param("productId") String productId);

}
34 changes: 30 additions & 4 deletions inventory-service/src/main/java/demo/stock/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Stock {
@GraphId
private Long id;

@Relationship(type = "PRODUCT_TYPE", direction = "OUTGOING")
@Relationship(type = "PRODUCT_STOCK", direction = "OUTGOING")
private Product product;

private Long number; //进货数量
Expand All @@ -28,7 +28,7 @@ public class Stock {

private Date syncTime; //同步时间

private boolean sync=false; //false未同步,true已同步,默认是false
private Boolean sync = false; //false未同步,true已同步,默认是false

public Long getId() {
return id;
Expand Down Expand Up @@ -70,11 +70,11 @@ public void setStockTime(Date stockTime) {
this.stockTime = stockTime;
}

public boolean isSync() {
public Boolean isSync() {
return sync;
}

public void setSync(boolean sync) {
public void setSync(Boolean sync) {
this.sync = sync;
}

Expand All @@ -85,4 +85,30 @@ public Date getSyncTime() {
public void setSyncTime(Date syncTime) {
this.syncTime = syncTime;
}

@Override
public String toString() {
return "Stock{" +
"id=" + id +
", product=" + product +
", number=" + number +
", stockUser='" + stockUser + '\'' +
", stockTime=" + stockTime +
", syncTime=" + syncTime +
", sync=" + sync +
'}';
}

public Stock() {
}

public Stock(Product product, Long number, String stockUser, Date stockTime, Date syncTime,
Boolean sync) {
this.product = product;
this.number = number;
this.stockUser = stockUser;
this.stockTime = stockTime;
this.syncTime = syncTime;
this.sync = sync;
}
}
10 changes: 8 additions & 2 deletions inventory-service/src/main/java/demo/stock/StockRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ public interface StockRepository extends GraphRepository<Stock> {
"\t RETURN stock")
List<Stock> getStockNoSync();

@Query("MATCH (stock:Stock),(product:Product) \n" +
"\t WHERE stock.sync=false AND product.id={productId} \n" +
@Query("MATCH (product:Product), \n" +
"\t (product)<-[:PRODUCT_STOCK]-(stock:Stock) WHERE stock.sync=false AND product.productId = {productId} \n" +
"\t set stock.sync=true \n" +
"\t RETURN stock")
Stock modifyProductState(@Param("productId") String productId);

@Query("MATCH (stock:Stock),(product:Product) \n" +
"\t (product)<-[:PRODUCT_STOCK]-(stock:Stock) WHERE stock.sync=false AND product.productId = {productId} \n" +
"\t set stock.sync={state} \n" +
"\t RETURN stock")
Stock modifyProductState(@Param("productId") String productId, @Param("state") Boolean state);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package demo.v1;

import demo.product.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -33,21 +34,22 @@ public ResponseEntity getAvailableInventoryForProductIds(@RequestParam("productI
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

/**
* 通过产品编号修改库存量
*/
@RequestMapping(path = "/modifyProductNum", method = RequestMethod.GET, name = "modifyProductNum")
public ResponseEntity modifyProductNum(@RequestParam("productId") String productId,@RequestParam("productNum")
long productNum){
return Optional.ofNullable(inventoryService.modifyProductNum(productId,productNum))
public ResponseEntity modifyProductNum(@RequestParam("productId") String productId, @RequestParam("productNum")
Long productNum) {
return Optional.ofNullable(inventoryService.modifyProductNum(productId, productNum))
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

/**
* 通过产品编号获取库存量
* @param productId
* @return
*/
@RequestMapping(path = "/getInventoryNumByPid", method = RequestMethod.GET, name = "getInventoryNumByPid")
public ResponseEntity getInventoryNumByPid(@RequestParam("productId") String productId){
public ResponseEntity getInventoryNumByPid(@RequestParam("productId") String productId) {
return Optional.ofNullable(inventoryService.getInventoryNumByPid(productId))
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
Expand Down
21 changes: 14 additions & 7 deletions inventory-service/src/main/java/demo/v1/InventoryServiceV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import demo.inventory.Inventory;
import demo.inventory.InventoryRepository;
import demo.product.Product;
import demo.product.ProductRepository;

import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +24,7 @@ public class InventoryServiceV1 {

@Autowired
public InventoryServiceV1(InventoryRepository inventoryRepository,
ProductRepository productRepository, Session neo4jTemplate) {
ProductRepository productRepository, Session neo4jTemplate) {
this.inventoryRepository = inventoryRepository;
this.productRepository = productRepository;
this.neo4jTemplate = neo4jTemplate;
Expand All @@ -35,7 +37,8 @@ public Product getProduct(String productId) {
product = productRepository.getProductByProductId(productId);

if (product != null) {
Stream<Inventory> availableInventory = inventoryRepository.getAvailableInventoryForProduct(productId).stream();
Stream<Inventory> availableInventory = inventoryRepository.getAvailableInventoryForProduct(productId)
.stream();
product.setInStock(availableInventory.findAny().isPresent());
}

Expand All @@ -55,13 +58,17 @@ public List<Inventory> getAvailableInventoryForProductIds(String productIds) {
.stream().collect(Collectors.toList());
}

@HystrixCommand
public Inventory modifyProductNum(String productId,long productNum){
return inventoryRepository.modifyProductNum(productId,productNum);
@HystrixCommand(fallbackMethod = "getProductFallback")
public Inventory modifyProductNum(String productId, Long productNum) {
Long nowInventoryNum = getInventoryNumByPid(productId);
nowInventoryNum = nowInventoryNum > 0 ? nowInventoryNum : 0;
Long modifyInventoryNum = productNum + nowInventoryNum; //当前库存量加上原有库存量
return inventoryRepository.modifyProductNum(productId, modifyInventoryNum);
}

@HystrixCommand
public Long getInventoryNumByPid(String productId){
@HystrixCommand(fallbackMethod = "getProductFallback")
public Long getInventoryNumByPid(String productId) {
return inventoryRepository.getInventoryNumByPid(productId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ public class StockControllerV1 {

/**
* 获取未同步的货品
* @return
*/
@RequestMapping("/getStockNoSync")
public ResponseEntity getStockNoSync(){
public ResponseEntity getStockNoSync() {
return Optional.ofNullable(stockService.getStockNoSync())
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

/**
* 同步产品状态
* @return
*/
@RequestMapping("/modifyProductState/{productId}")
public ResponseEntity modifyProductState(@PathVariable("productId") String productId){
@RequestMapping("/modifyProductState")
public ResponseEntity modifyProductState(@PathVariable("productId") String productId) {
return Optional.ofNullable(stockService.modifyProductState(productId))
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
Expand Down
6 changes: 3 additions & 3 deletions inventory-service/src/main/java/demo/v1/StockServiceV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public class StockServiceV1 {
private StockRepository stockRepository;

@Autowired
public StockServiceV1(StockRepository stockRepository){
public StockServiceV1(StockRepository stockRepository) {
this.stockRepository = stockRepository;
}

@HystrixCommand
public List<Stock> getStockNoSync(){
public List<Stock> getStockNoSync() {
return stockRepository.getStockNoSync();
}

@HystrixCommand
public Stock modifyProductState(String productId){
public Stock modifyProductState(String productId) {
return stockRepository.modifyProductState(productId);
}

Expand Down
Loading

0 comments on commit c93f08a

Please sign in to comment.