Skip to content

Commit

Permalink
完成订单服务编码
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgerFan committed Aug 26, 2019
1 parent f03e7aa commit c5a43be
Show file tree
Hide file tree
Showing 24 changed files with 828 additions and 0 deletions.
31 changes: 31 additions & 0 deletions order/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
129 changes: 129 additions & 0 deletions order/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- TODO 2.0.0.M3 -->
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>cn.algerfan</groupId>
<artifactId>order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<!-- TODO Finchley.M2-->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<product-client.version>0.0.1-SNAPSHOT</product-client.version>
<product-common.version>0.0.1-SNAPSHOT</product-common.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- TODO spring-cloud-starter-feign ——> spring-cloud-starter-openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- TODO spring-cloud-starter-hystrix ——> spring-cloud-starter-netflix-hystrix-->
<!-- TODO 2.0.2.RELEASE需要引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>-->

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
20 changes: 20 additions & 0 deletions order/src/main/java/cn/algerfan/order/OrderApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.algerfan.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* @author algerfan
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "cn.algerfan.product.client")
public class OrderApplication {

public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cn.algerfan.order.controller;

import cn.algerfan.order.form.OrderForm;
import cn.algerfan.order.converter.OrderFormToOrderDto;
import cn.algerfan.order.dto.OrderDto;
import cn.algerfan.order.enums.Result;
import cn.algerfan.order.exception.OrderException;
import cn.algerfan.order.service.OrderService;
import cn.algerfan.order.util.ResultVoUtil;
import cn.algerfan.order.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author algerfan
* @time 2019 17: 44
*/
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

@Autowired
private OrderService orderService;

/**
* 1.参数校验
* 2.查询商品信息(调用商品服务)
* 3.计算总价
* 4.扣库存(调用商品服务)
* 5.订单入库
*/
@PostMapping("/create")
public ResultVo<Object> create(@Valid OrderForm orderForm,
BindingResult bindingResult) throws OrderException {
if(bindingResult.hasErrors()) {
log.error("【创建订单】参数不正确,orderForm={}", orderForm);
throw new OrderException(Result.PARAM_ERROR.getCode(),
Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
}

OrderDto orderDto = OrderFormToOrderDto.convert(orderForm);
if(CollectionUtils.isEmpty(orderDto.getOrderDetailList())) {
log.error("创建订单,购物车为空");
throw new OrderException(Result.CART_EMPTY);
}
OrderDto orderDto1 = orderService.create(orderDto);
Map<String, String> map = new HashMap<>(5);
map.put("order", orderDto1.getOrderId());
return ResultVoUtil.success(map);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cn.algerfan.order.converter;

import cn.algerfan.order.exception.OrderException;
import cn.algerfan.order.form.OrderForm;
import cn.algerfan.order.domain.OrderDetail;
import cn.algerfan.order.dto.OrderDto;
import cn.algerfan.order.enums.Result;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
* @author algerfan
* @time 2019 20: 44
*/
@Slf4j
public class OrderFormToOrderDto {

public static OrderDto convert(OrderForm orderForm) {
Gson gson = new Gson();
OrderDto orderDto = new OrderDto();
orderDto.setBuyerName(orderForm.getName());
orderDto.setBuyerAddress(orderForm.getAddress());
orderDto.setBuyerPhone(orderForm.getPhone());
orderDto.setBuyerOpenid(orderForm.getOpenid());

List<OrderDetail> orderDetailList;
try {
orderDetailList = gson.fromJson(orderForm.getItems(),
new TypeToken<List<OrderDetail>>() {}.getType());
} catch (Exception e) {
log.error("json转化错误,String={}", orderForm.getItems());
throw new OrderException(Result.PARAM_ERROR);
}
orderDto.setOrderDetailList(orderDetailList);
return orderDto;
}
}
38 changes: 38 additions & 0 deletions order/src/main/java/cn/algerfan/order/domain/OrderDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.algerfan.order.domain;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;

/**
* @author algerfan
* @time 2019 17: 17
*/
@Data
@Entity
public class OrderDetail {

@Id
private String detailId;

/** 订单id. */
private String orderId;

/** 商品id. */
private String productId;

/** 商品名称. */
private String productName;

/** 商品单价. */
private BigDecimal productPrice;

/** 商品数量. */
private Integer productQuantity;

/** 商品小图. */
private String productIcon;
}

49 changes: 49 additions & 0 deletions order/src/main/java/cn/algerfan/order/domain/OrderMaster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.algerfan.order.domain;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
import java.util.Date;

/**
* @author algerfan
* @time 2019 17: 17
*/
@Data
@Entity
public class OrderMaster {

/** 订单id. */
@Id
private String orderId;

/** 买家名字. */
private String buyerName;

/** 买家手机号. */
private String buyerPhone;

/** 买家地址. */
private String buyerAddress;

/** 买家微信Openid. */
private String buyerOpenid;

/** 订单总金额. */
private BigDecimal orderAmount;

/** 订单状态, 默认为0新下单. */
private Integer orderStatus;

/** 支付状态, 默认为0未支付. */
private Integer payStatus;

/** 创建时间. */
private Date createTime;

/** 更新时间. */
private Date updateTime;
}

Loading

0 comments on commit c5a43be

Please sign in to comment.