后端创建订单接口完成;删除service层interface
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.cxyxiaomo.epp.common.enums;
|
||||
|
||||
public enum OrderStatus {
|
||||
PENDING("Pending"),
|
||||
PROCESSING("Processing"),
|
||||
SHIPPED("Shipped"),
|
||||
DELIVERED("Delivered"),
|
||||
CANCELLED("Cancelled");
|
||||
|
||||
private final String value;
|
||||
|
||||
private OrderStatus(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@@ -15,6 +15,7 @@ public class Good {
|
||||
String gallery;
|
||||
String brief;
|
||||
Boolean isOnSale;
|
||||
Integer sortOrder;
|
||||
String picUrl;
|
||||
Integer type;
|
||||
String unit;
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
public class Order {
|
||||
private Long id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
public class OrderDetail {
|
||||
private Long id;
|
||||
private Long orderId;
|
||||
private Long goodId;
|
||||
private Integer goodCount;
|
||||
private Double unitPrice;
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
package com.cxyxiaomo.epp.common.utils;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class SnowflakeManager {
|
||||
private static final long EPOCH_STAMP = 1262275200000L;
|
||||
private static final long SEQUENCE_BIT = 12L;
|
||||
private static final long MACHINE_BIT = 5L;
|
||||
private static final long DATA_CENTER_BIT = 5L;
|
||||
private static final long MAX_SEQUENCE_NUM = -1L ^ (-1L << SEQUENCE_BIT);
|
||||
private static final long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
|
||||
private static final long MAX_DATA_CENTER_NUM = -1L ^ (-1L << DATA_CENTER_BIT);
|
||||
private static final long MACHINE_LEFT = SEQUENCE_BIT;
|
||||
private static final long DATA_CENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
|
||||
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT + DATA_CENTER_BIT;
|
||||
private final long machineId;
|
||||
private final long dataCenterId;
|
||||
private long sequence = 0L;
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
public SnowflakeManager(long machineId, long dataCenterId) {
|
||||
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
|
||||
throw new IllegalArgumentException(String.format("machine id can't be greater than %d or less than 0", MAX_MACHINE_NUM));
|
||||
}
|
||||
if (dataCenterId > MAX_DATA_CENTER_NUM || dataCenterId < 0) {
|
||||
throw new IllegalArgumentException(String.format("data center id can't be greater than %d or less than 0", MAX_DATA_CENTER_NUM));
|
||||
}
|
||||
this.machineId = machineId;
|
||||
this.dataCenterId = dataCenterId;
|
||||
}
|
||||
|
||||
public synchronized long nextValue() throws Exception {
|
||||
String os = System.getProperty("os.name");
|
||||
SecureRandom secureRandom;
|
||||
if (os.toLowerCase().startsWith("win")) {
|
||||
// windows机器用
|
||||
secureRandom = new SecureRandom(); // SecureRandom.getInstanceStrong();
|
||||
} else {
|
||||
// linux机器用
|
||||
secureRandom = SecureRandom.getInstance("NativePRNGNonBlocking");
|
||||
}
|
||||
//SecureRandom secureRandom = SecureRandom.getInstanceStrong();
|
||||
long currentTimeMillis = this.currentTimeMillis();
|
||||
if(currentTimeMillis < this.lastTimestamp) {
|
||||
throw new Exception(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", (this.lastTimestamp-currentTimeMillis)));
|
||||
}
|
||||
|
||||
if(this.lastTimestamp == currentTimeMillis) {
|
||||
this.sequence = (this.sequence+1) & MAX_SEQUENCE_NUM;
|
||||
if (this.sequence == 0) {
|
||||
this.sequence = secureRandom.nextInt(Long.valueOf(SEQUENCE_BIT).intValue());
|
||||
currentTimeMillis = this.tilNextMillis(this.lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
this.sequence = secureRandom.nextInt(Long.valueOf(SEQUENCE_BIT).intValue());
|
||||
}
|
||||
this.lastTimestamp = currentTimeMillis;
|
||||
|
||||
// 64 Bit ID (42(Millis)+5(Data Center ID)+5(Machine ID)+12(Repeat Sequence Summation))
|
||||
long nextId = ((currentTimeMillis-EPOCH_STAMP) << TIMESTAMP_LEFT)
|
||||
| (this.dataCenterId << DATA_CENTER_LEFT)
|
||||
| (this.machineId << MACHINE_LEFT)
|
||||
| this.sequence;
|
||||
|
||||
return nextId;
|
||||
}
|
||||
|
||||
private long tilNextMillis(long lastTimestamp) {
|
||||
long currentTimeMillis = this.currentTimeMillis();
|
||||
while (currentTimeMillis <= lastTimestamp) {
|
||||
currentTimeMillis = this.currentTimeMillis();
|
||||
}
|
||||
return currentTimeMillis;
|
||||
}
|
||||
|
||||
private long currentTimeMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SnowflakeManager snowflakeManager = new SnowflakeManager(0L,0L);
|
||||
long l = snowflakeManager.nextValue();
|
||||
System.out.println(l);
|
||||
}
|
||||
}
|
@@ -24,6 +24,7 @@ public class GoodVO implements Serializable {
|
||||
String gallery;
|
||||
String brief;
|
||||
Boolean isOnSale;
|
||||
Integer sortOrder;
|
||||
String picUrl;
|
||||
Integer type;
|
||||
String unit;
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package com.cxyxiaomo.epp.common.vo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderDetailVO implements Serializable {
|
||||
private Long id;
|
||||
private Long orderId;
|
||||
private Long goodId;
|
||||
private Integer goodCount;
|
||||
private Double unitPrice;
|
||||
|
||||
public static OrderDetailVO convertFrom(OrderDetail orderDetail) {
|
||||
if (orderDetail == null) {
|
||||
return null;
|
||||
}
|
||||
OrderDetailVO orderDetailVO = new OrderDetailVO();
|
||||
// BeanUtils.copyProperties(orderDetail, orderDetailVO);
|
||||
return orderDetailVO;
|
||||
}
|
||||
|
||||
public static List<OrderDetailVO> convertFrom(List<OrderDetail> orderDetailList) {
|
||||
if (orderDetailList == null) {
|
||||
return null;
|
||||
}
|
||||
return orderDetailList.stream().map(OrderDetailVO::convertFrom).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package com.cxyxiaomo.epp.common.vo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderVO implements Serializable {
|
||||
private Long id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
|
||||
public static OrderVO convertFrom(Order order) {
|
||||
if (order == null) {
|
||||
return null;
|
||||
}
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(order, orderVO);
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
public static List<OrderVO> convertFrom(List<Order> orderList) {
|
||||
if (orderList == null) {
|
||||
return null;
|
||||
}
|
||||
List<OrderVO> goodsOrdersVOList = orderList.stream().map(OrderVO::convertFrom).collect(Collectors.toList());
|
||||
return goodsOrdersVOList;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user