Skip to content

Commit 782078e

Browse files
committed
Service to create order finished, needs to handle multi-thread issues, Will use Redis Lock later
1 parent a81105b commit 782078e

File tree

11 files changed

+398
-1
lines changed

11 files changed

+398
-1
lines changed

src/main/java/com/csjihe/springbootwechatordersystem/dataobject/OrderMaster.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.math.BigDecimal;
1212
import java.util.Date;
1313

14+
1415
@Entity
1516
@Data
1617
@DynamicUpdate
@@ -49,4 +50,5 @@ public class OrderMaster {
4950
/** Order updated time */
5051
/** Add @DynamicUpdate annotation */
5152
private Date updateTime;
53+
5254
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.csjihe.springbootwechatordersystem.dto;
2+
3+
4+
import lombok.Data;
5+
6+
/**
7+
* Shopping cart
8+
*/
9+
@Data
10+
public class CartDTO {
11+
12+
/** Product Id */
13+
private String productId;
14+
15+
16+
/** Product Quantity */
17+
private Integer productQuantity;
18+
19+
20+
public CartDTO(String productId, Integer productQuantity) {
21+
this.productId = productId;
22+
this.productQuantity = productQuantity;
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.csjihe.springbootwechatordersystem.dto;
2+
3+
4+
import com.csjihe.springbootwechatordersystem.dataobject.OrderDetail;
5+
import com.csjihe.springbootwechatordersystem.enums.OrderStatusEnum;
6+
import com.csjihe.springbootwechatordersystem.enums.PayStatusEnum;
7+
import lombok.Data;
8+
9+
import javax.persistence.Id;
10+
import java.math.BigDecimal;
11+
import java.util.Date;
12+
import java.util.List;
13+
14+
@Data
15+
public class OrderDTO {
16+
17+
/** Order ID */
18+
private String orderId;
19+
20+
/** Buyer Name */
21+
private String buyerName;
22+
23+
/** Buyer Phone */
24+
private String buyerPhone;
25+
26+
/** Buyer Address */
27+
private String buyerAddress;
28+
29+
/** Buyer Wechat OpenId */
30+
private String buyerOpenid;
31+
32+
/** Buyer Order Amount */
33+
private BigDecimal orderAmount;
34+
35+
/** Order status, default as new order */
36+
private Integer orderStatus;
37+
38+
/** Payment status, default as unpaid denoted as 0 */
39+
private Integer payStatus;
40+
41+
/** Order created time */
42+
/** Add @DynamicUpdate annotation */
43+
private Date createTime;
44+
45+
46+
/** Order updated time */
47+
/** Add @DynamicUpdate annotation */
48+
private Date updateTime;
49+
50+
List<OrderDetail> orderDetailList;
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.csjihe.springbootwechatordersystem.enums;
2+
3+
4+
import lombok.Getter;
5+
6+
@Getter
7+
public enum ResultEnum {
8+
9+
PRODUCT_NOT_EXIST(10, "Product Not Exist"),
10+
11+
PRODUCT_STOCK_ERROR(11, "Stock Error"),
12+
;
13+
14+
private Integer code;
15+
private String message;
16+
17+
ResultEnum(Integer code, String message) {
18+
this.code = code;
19+
this.message = message;
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.csjihe.springbootwechatordersystem.exception;
2+
3+
import com.csjihe.springbootwechatordersystem.enums.ResultEnum;
4+
5+
public class SellException extends RuntimeException{
6+
7+
private Integer code;
8+
9+
public SellException(ResultEnum resultEnum) {
10+
super(resultEnum.getMessage());
11+
this.code = resultEnum.getCode();
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.csjihe.springbootwechatordersystem.service;
2+
3+
import com.csjihe.springbootwechatordersystem.dto.OrderDTO;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
7+
public interface OrderService {
8+
9+
/** Set up an order */
10+
OrderDTO create(OrderDTO orderDTO);
11+
12+
/** Query a single order */
13+
OrderDTO findOne(String OrderId);
14+
15+
/** Query a list of orders */
16+
Page<OrderDTO> findList(String buyerOpenid, Pageable pageable);
17+
18+
/** Cancel an order */
19+
OrderDTO cancel(OrderDTO orderDTO);
20+
21+
/** Finish an order */
22+
OrderDTO finish(OrderDTO orderDTO);
23+
24+
/** Pay an order */
25+
OrderDTO paid(OrderDTO orderDTO);
26+
}

src/main/java/com/csjihe/springbootwechatordersystem/service/ProductService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.csjihe.springbootwechatordersystem.service;
22

33
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
4+
import com.csjihe.springbootwechatordersystem.dto.CartDTO;
45
import org.springframework.data.domain.Page;
56
import org.springframework.data.domain.Pageable;
67

@@ -19,7 +20,8 @@ public interface ProductService {
1920
ProductInfo save(ProductInfo productInfo);
2021

2122
// Increase stock
22-
23+
void increaseStock(List<CartDTO> cartDTOList);
2324

2425
// Decrease stock
26+
void decreaseStock(List<CartDTO> cartDTOList);
2527
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.csjihe.springbootwechatordersystem.service.implementation;
2+
3+
import com.csjihe.springbootwechatordersystem.dataobject.OrderDetail;
4+
import com.csjihe.springbootwechatordersystem.dataobject.OrderMaster;
5+
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
6+
import com.csjihe.springbootwechatordersystem.dto.CartDTO;
7+
import com.csjihe.springbootwechatordersystem.dto.OrderDTO;
8+
import com.csjihe.springbootwechatordersystem.enums.OrderStatusEnum;
9+
import com.csjihe.springbootwechatordersystem.enums.PayStatusEnum;
10+
import com.csjihe.springbootwechatordersystem.enums.ResultEnum;
11+
import com.csjihe.springbootwechatordersystem.exception.SellException;
12+
import com.csjihe.springbootwechatordersystem.repository.OrderDetailRepository;
13+
import com.csjihe.springbootwechatordersystem.repository.OrderMasterRepository;
14+
import com.csjihe.springbootwechatordersystem.service.OrderService;
15+
import com.csjihe.springbootwechatordersystem.service.ProductService;
16+
import com.csjihe.springbootwechatordersystem.utils.KeyUtil;
17+
import org.springframework.beans.BeanUtils;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.data.domain.Page;
20+
import org.springframework.data.domain.Pageable;
21+
import org.springframework.stereotype.Service;
22+
import org.springframework.transaction.annotation.Transactional;
23+
24+
25+
import java.math.BigDecimal;
26+
import java.math.BigInteger;
27+
import java.util.List;
28+
import java.util.stream.Collectors;
29+
30+
31+
@Service
32+
public class OrderServiceImpl implements OrderService {
33+
34+
@Autowired
35+
private ProductService productService;
36+
37+
@Autowired
38+
private OrderDetailRepository orderDetailRepository;
39+
40+
@Autowired
41+
private OrderMasterRepository orderMasterRepository;
42+
43+
44+
// List<CartDTO> cartDTOS = new ArrayList<>();
45+
46+
@Override
47+
@Transactional
48+
public OrderDTO create(OrderDTO orderDTO) {
49+
50+
String orderId = KeyUtil.genUniqueKey();
51+
52+
BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
53+
54+
// 1. 查询商品 数量 价格 库存 等
55+
for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
56+
ProductInfo productInfo = productService.findOne(orderDetail.getProductId());
57+
if (productInfo == null) {
58+
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
59+
}
60+
61+
// 2. 计算总价
62+
orderAmount = productInfo.getProductPrice()
63+
.multiply(new BigDecimal(orderDetail.getProductQuantity()))
64+
.add(orderAmount);
65+
66+
// 订单详情入库 OrderDetail
67+
orderDetail.setDetailId(KeyUtil.genUniqueKey());
68+
orderDetail.setOrderId(orderId);
69+
70+
// copy other Product properties from productInfo to orderDetail
71+
BeanUtils.copyProperties(productInfo, orderDetail);
72+
73+
orderDetailRepository.save(orderDetail);
74+
75+
}
76+
77+
78+
// 3. 写入订单数据库 OrderMaster
79+
OrderMaster orderMaster = new OrderMaster();
80+
BeanUtils.copyProperties(orderDTO, orderMaster);
81+
orderMaster.setOrderId(orderId);
82+
orderMaster.setOrderAmount(orderAmount);
83+
orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
84+
orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
85+
86+
87+
orderMasterRepository.save(orderMaster);
88+
89+
// 4. Update 库存
90+
List<CartDTO> cartDTOS = orderDTO.getOrderDetailList().stream().map(e ->
91+
new CartDTO(e.getProductId(), e.getProductQuantity())
92+
).collect(Collectors.toList());
93+
94+
95+
productService.decreaseStock(cartDTOS);
96+
return orderDTO;
97+
}
98+
99+
@Override
100+
public OrderDTO findOne(String OrderId) {
101+
return null;
102+
}
103+
104+
@Override
105+
public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
106+
return null;
107+
}
108+
109+
@Override
110+
public OrderDTO cancel(OrderDTO orderDTO) {
111+
return null;
112+
}
113+
114+
@Override
115+
public OrderDTO finish(OrderDTO orderDTO) {
116+
return null;
117+
}
118+
119+
@Override
120+
public OrderDTO paid(OrderDTO orderDTO) {
121+
return null;
122+
}
123+
}

src/main/java/com/csjihe/springbootwechatordersystem/service/implementation/ProductServiceImpl.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.csjihe.springbootwechatordersystem.service.implementation;
22

33
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
4+
import com.csjihe.springbootwechatordersystem.dto.CartDTO;
45
import com.csjihe.springbootwechatordersystem.enums.ProductStatusEnum;
6+
import com.csjihe.springbootwechatordersystem.enums.ResultEnum;
7+
import com.csjihe.springbootwechatordersystem.exception.SellException;
58
import com.csjihe.springbootwechatordersystem.repository.ProductInfoRepository;
69
import com.csjihe.springbootwechatordersystem.service.ProductService;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.data.domain.Page;
912
import org.springframework.data.domain.Pageable;
1013
import org.springframework.stereotype.Service;
14+
import org.springframework.transaction.annotation.Transactional;
1115

1216
import java.util.List;
1317

@@ -37,4 +41,29 @@ public Page<ProductInfo> findAll(Pageable pageable) {
3741
public ProductInfo save(ProductInfo productInfo) {
3842
return repository.save(productInfo);
3943
}
44+
45+
@Override
46+
public void increaseStock(List<CartDTO> cartDTOList) {
47+
48+
}
49+
50+
@Override
51+
@Transactional
52+
public void decreaseStock(List<CartDTO> cartDTOList) {
53+
for (CartDTO cartDTO : cartDTOList) {
54+
ProductInfo productInfo = repository.findOne(cartDTO.getProductId());
55+
if (productInfo == null) {
56+
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
57+
}
58+
59+
Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
60+
if (result < 0) {
61+
throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
62+
}
63+
64+
productInfo.setProductStock(result);
65+
66+
repository.save(productInfo);
67+
}
68+
}
4069
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.csjihe.springbootwechatordersystem.utils;
2+
3+
4+
import java.util.Random;
5+
6+
/**
7+
* Utility class generating Database key
8+
*/
9+
public class KeyUtil {
10+
11+
/**
12+
* 声称唯一primary key
13+
* format: time + randomNumber
14+
* @return
15+
*/
16+
public static synchronized String genUniqueKey() {
17+
18+
19+
Random random = new Random();
20+
21+
Integer number = random.nextInt(900000) + 100000; // 6-bit random number
22+
23+
return System.currentTimeMillis() + String.valueOf(number);
24+
}
25+
}

0 commit comments

Comments
 (0)