版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39036844/article/details/81942552
1.在Server创建interface类,OrderService。首先尽量写出能想到的有关订单的方法。
package com.imooc.service;
import com.imooc.dto.OrderDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
/**
* Created by 廖师兄
* 2017-06-11 18:23
*/
public interface OrderService {
/** 创建订单. */
OrderDTO create(OrderDTO orderDTO);
/** 查询单个订单. */
OrderDTO findOne(String orderId);
/** 查询订单列表. */
Page<OrderDTO> findList(String buyerOpenid,Pageable pageable);
/** 取消订单. */
OrderDTO cancel(OrderDTO orderDTO);
/** 完结订单. */
OrderDTO finish(OrderDTO orderDTO);
/** 支付订单. */
OrderDTO paid (OrderDTO orderDTO);
/** 查询订单列表. */
}
OrderDTO是根据订单主表设计的,如果长期使用跟数据有关的实体类,不方便。单独创建了一个用来数据传输的实体类。而且因为返回的时候可多个订单,因此根据订单详情,设置了订单详情的List<OrderDetail>类型的orderDetailList变量。
package com.imooc.dto;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.imooc.Utils.serializer.Date2LongSerializer;
import com.imooc.dataobject.OrderDetail;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
//import com.imooc.utils.EnumUtil;
//import com.imooc.utils.serializer.Date2LongSerializer;
/**
* Created by 廖师兄
* 2017-06-11 18:30
* D:数据T:传输O:对象
*/
@Data
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderDTO {
/** 订单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;
/** 创建时间. */
@JsonSerialize(using = Date2LongSerializer.class)
private Date createTime;
/** 更新时间. */
@JsonSerialize(using = Date2LongSerializer.class)
private Date updateTime;
/**订单详情*/
List<OrderDetail> orderDetailList;
// @JsonIgnore
// public OrderStatusEnum getOrderStatusEnum() {
// return EnumUtil.getByCode(orderStatus, OrderStatusEnum.class);
// }
//
// @JsonIgnore
// public PayStatusEnum getPayStatusEnum() {
// return EnumUtil.getByCode(payStatus, PayStatusEnum.class);
// }
}
创建Server类的实现类,因为用到订单查询的几个方法,所以必须引入。
package com.imooc.service.Impl;
import com.imooc.Utils.KeyUtil;
import com.imooc.dataobject.OrderDetail;
import com.imooc.dataobject.OrderMaster;
import com.imooc.dataobject.ProductInfo;
import com.imooc.dto.CartDTO;
import com.imooc.dto.OrderDTO;
import com.imooc.enums.OrderStatusEnum;
import com.imooc.enums.PayStatusEnum;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.repository.OrderDetailRepository;
import com.imooc.repository.OrderMasterRepository;
import com.imooc.service.OrderService;
import com.imooc.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Cdy1234 on 2018/8/18.
* 订单表的Service层
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ProductService productService;
@Autowired
private OrderDetailRepository orderDetailRepository;
@Autowired
private OrderMasterRepository orderMasterRepository;
/**
* 创建订单
*/
@Override
@Transactional
public OrderDTO create(OrderDTO orderDTO) {
//生成订单id
String orderId=KeyUtil.genUniqueKey();
BigDecimal orderAmount=new BigDecimal(BigInteger.ZERO);
// List<CartDTO> cartDTOList=new ArrayList<>();
//1.查询商品(数量,价格)
for(OrderDetail orderDetail:orderDTO.getOrderDetailList()){
ProductInfo productInfo= productService.findOne(orderDetail.getProductId());
if (productInfo==null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
//2.计算总价
orderAmount= productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity())).add(orderAmount);
//订单详情入库。
orderDetail.setDetailId(KeyUtil.genUniqueKey());
orderDetail.setOrderId(orderId);
BeanUtils.copyProperties(productInfo,orderDetail);
orderDetailRepository.save(orderDetail);
}
//3.写入订单数据库(orderMaster和orderDetail)
OrderMaster orderMaster=new OrderMaster();
BeanUtils.copyProperties(orderDTO,orderMaster);
orderMaster.setOrderId(orderId);
orderMaster.setOrderAmount(orderAmount);
orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
orderMasterRepository.save(orderMaster);
//4.扣库存
List<CartDTO> cartDTOList=orderDTO.getOrderDetailList().stream().map(e->new CartDTO(e.getProductId(),e.getProductQuantity())).collect(Collectors.toList());
productService.decreaseStock(cartDTOList);
return orderDTO;
}
@Override
public OrderDTO findOne(String orderId) {
return null;
}
@Override
public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
return null;
}
@Override
public OrderDTO cancel(OrderDTO orderDTO) {
return null;
}
@Override
public OrderDTO finish(OrderDTO orderDTO) {
return null;
}
@Override
public OrderDTO paid(OrderDTO orderDTO) {
return null;
}
}
测试创建订单的方法,模拟用户创建订单,购物车只需要传商品ID和商品的数量,因为可能同时下单多个商品。所以也必须使用数组来创建。
package com.imooc.service.Impl;
import com.imooc.dataobject.OrderDetail;
import com.imooc.dto.OrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Cdy1234 on 2018/8/18.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class OrderServiceImplTest {
@Autowired
private OrderServiceImpl orderService;
private final String BUYER_OPENID="110110";
@Test
public void testCreate() throws Exception {
OrderDTO orderDTO=new OrderDTO();
orderDTO.setBuyerAddress("厦门大夏408");
orderDTO.setBuyerName("邱芬芳");
orderDTO.setBuyerPhone("153xxxxx55");
orderDTO.setBuyerOpenid(BUYER_OPENID);
//购物车
List<OrderDetail>orderDetailList =new ArrayList<>();
OrderDetail o1=new OrderDetail();
o1.setProductQuantity(2);
o1.setProductId("123459");
OrderDetail o2 = new OrderDetail();
o2.setProductId("123453");
o2.setProductQuantity(2);
orderDetailList.add(o1);
orderDetailList.add(o2);
orderDTO.setOrderDetailList(orderDetailList);
OrderDTO result= orderService.create(orderDTO);
log.info("【创建订单】result={}", result);
Assert.assertNotNull(result);
}
@Test
public void testFindOne() throws Exception {
}
@Test
public void testFindList() throws Exception {
}
@Test
public void testCancel() throws Exception {
}
@Test
public void testFinish() throws Exception {
}
@Test
public void testPaid() throws Exception {
}
}
订单主表添加成功
订单详情表测试成功