Spring Boot点餐系统实战(8)–买家订单service层(下)

上一节介绍了买家订单 service 层的创建订单的实现,本文将介绍剩余的其他部分(查询一个订单,查询订单列表,取消订单,完结订单和支付订单)。

本文关键词

抽出常用方法

lambad 表达式

其他的继承上一节...

 

一、开发准备

1、修改的文件

Spring Boot点餐系统实战(8)–买家订单service层(下)

其他的继承上一节

 

二、convert 创建

OrderMaster2OrderDTOConverter.java

  1. package com.liuyanzhao.sell.converter;
  2. import com.liuyanzhao.sell.dto.OrderDTO;
  3. import com.liuyanzhao.sell.entity.OrderMaster;
  4. import org.springframework.beans.BeanUtils;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. /**
  8.  * @Author: 言曌
  9.  * @Date: 2017/11/14
  10.  * @Time: 下午10:02
  11.  */
  12. public class OrderMaster2OrderDTOConverter {
  13.     public static OrderDTO convert(OrderMaster orderMaster) {
  14.         OrderDTO orderDTO = new OrderDTO();
  15.         BeanUtils.copyProperties(orderMaster,orderDTO);
  16.         return orderDTO;
  17.     }
  18.     public static List<OrderDTO> convert(List<OrderMaster> orderMasterList) {
  19.         return orderMasterList.stream().map(e ->
  20.             convert(e)
  21.         ).collect(Collectors.toList());
  22.     }
  23. }

这个类的目的就是将 orderMaster 对象转成 orderDTO 对象,将 List<OrderMaster>  转成 List<OrderDTO> ,我们之前的做法都是直接在 Service 里写,这样代码冗余度较高。通过这个类,我们实现转换会更加方便。

 

三、ProductServiceImpl.java 的补充

我们之前在第三章里的商品信息Service层开发和测试中的增加库存和减少库存的方法还没有实现

现在补上。

  1. package com.liuyanzhao.sell.service.impl;
  2. import com.liuyanzhao.sell.dao.ProductInfoDao;
  3. import com.liuyanzhao.sell.dto.CartDTO;
  4. import com.liuyanzhao.sell.entity.ProductInfo;
  5. import com.liuyanzhao.sell.enums.ProductStatusEnum;
  6. import com.liuyanzhao.sell.enums.ResultEnum;
  7. import com.liuyanzhao.sell.exception.SellException;
  8. import com.liuyanzhao.sell.service.ProductService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.domain.Page;
  11. import org.springframework.data.domain.Pageable;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import java.util.List;
  15. /**
  16.  * @Author: 言曌
  17.  * @Date: 2017/11/12
  18.  * @Time: 上午11:39
  19.  */
  20. @Service
  21. public class ProductServiceImpl implements ProductService {
  22.     @Autowired
  23.     private ProductInfoDao dao;
  24.     @Override
  25.     public ProductInfo findOne(String productId) {
  26.         return dao.findOne(productId);
  27.     }
  28.     @Override
  29.     public List<ProductInfo> findUpAll() {
  30.         //return dao.findByProductStatus(0);
  31.         return dao.findByProductStatus(ProductStatusEnum.UP.getCode());
  32.     }
  33.     @Override
  34.     public Page<ProductInfo> findAll(Pageable pageable) {
  35.         return dao.findAll(pageable);
  36.     }
  37.     @Override
  38.     public ProductInfo save(ProductInfo productInfo) {
  39.         return dao.save(productInfo);
  40.     }
  41.     @Override  
  42.     @Transactional  
  43.     public void increaseStock(List<CartDTO> cartDTOList) {  
  44.         for(CartDTO cartDTO : cartDTOList) {  
  45.             ProductInfo productInfo = dao.findOne(cartDTO.getProductId());  
  46.             if(productInfo == null) {  
  47.                 throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);  
  48.             }  
  49.             Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();  
  50.             productInfo.setProductStock(result);  
  51.             dao.save(productInfo);  
  52.         }  
  53.     }  
  54.   
  55.     @Override  
  56.     @Transactional  
  57.     public void decreaseStock(List<CartDTO> cartDTOList) {  
  58.         for(CartDTO cartDTO : cartDTOList) {  
  59.             ProductInfo productInfo = dao.findOne(cartDTO.getProductId());  
  60.             if(productInfo == null) {  
  61.                 throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);  
  62.             }  
  63.   
  64.             Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();  
  65.             if(result < 0) {  
  66.                 throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);  
  67.             }  
  68.             productInfo.setProductStatus(result);  
  69.             dao.save(productInfo);  
  70.         }  
  71.     }  
  72. }

 

 

四、Service 层的创建

1、OrderService.java

  1. package com.liuyanzhao.sell.service;
  2. import com.liuyanzhao.sell.dto.OrderDTO;
  3. import org.springframework.data.domain.Page;
  4. import org.springframework.data.domain.Pageable;
  5. /**
  6.  * @Author: 言曌
  7.  * @Date: 2017/11/13
  8.  * @Time: 上午11:17
  9.  */
  10. public interface OrderService {
  11.     //创建订单
  12.     OrderDTO create(OrderDTO orderDTO);
  13.     //查询单个订单
  14.     OrderDTO findOne(String orderId);
  15.     //查询订单列表
  16.     Page<OrderDTO> findList(String buyerOpenid, Pageable pageable);
  17.     //取消订单
  18.     OrderDTO cancel(OrderDTO orderDTO);
  19.     //完结订单
  20.     OrderDTO finish(OrderDTO orderDTO);
  21.     //支付订单
  22.     OrderDTO paid(OrderDTO orderDTO);
  23. }

 

2、OrderServiceImpl.java

  1. package com.liuyanzhao.sell.service.impl;
  2. import com.liuyanzhao.sell.converter.OrderMaster2OrderDTOConverter;
  3. import com.liuyanzhao.sell.dao.OrderDetailDao;
  4. import com.liuyanzhao.sell.dao.OrderMasterDao;
  5. import com.liuyanzhao.sell.dto.CartDTO;
  6. import com.liuyanzhao.sell.dto.OrderDTO;
  7. import com.liuyanzhao.sell.entity.OrderDetail;
  8. import com.liuyanzhao.sell.entity.OrderMaster;
  9. import com.liuyanzhao.sell.entity.ProductInfo;
  10. import com.liuyanzhao.sell.enums.OrderStatusEnum;
  11. import com.liuyanzhao.sell.enums.PayStatusEnum;
  12. import com.liuyanzhao.sell.enums.ResultEnum;
  13. import com.liuyanzhao.sell.exception.SellException;
  14. import com.liuyanzhao.sell.service.OrderService;
  15. import com.liuyanzhao.sell.service.ProductService;
  16. import com.liuyanzhao.sell.utils.KeyUtil;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.data.domain.PageImpl;
  22. import org.springframework.data.domain.Pageable;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import org.springframework.util.CollectionUtils;
  26. import java.math.BigDecimal;
  27. import java.math.BigInteger;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. /**
  32.  * @Author: 言曌
  33.  * @Date: 2017/11/13
  34.  * @Time: 上午11:29
  35.  */
  36. @Slf4j
  37. @Service
  38. public class OrderServiceImpl implements OrderService{
  39.     @Autowired
  40.     private ProductService productService;
  41.     @Autowired
  42.     private OrderDetailDao orderDetailDao;
  43.     @Autowired
  44.     private OrderMasterDao orderMasterDao;
  45.     @Override
  46.     @Transactional //事务管理
  47.     public OrderDTO create(OrderDTO orderDTO) {
  48.         String orderId = KeyUtil.genUniqueKey();
  49.         BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO );
  50.         //1、查询商品(数量,价格)
  51.         List<OrderDetail> orderDetailList = new ArrayList<>();
  52.         for(OrderDetail orderDetail:orderDTO.getOrderDetailList()) {
  53.             ProductInfo productInfo = productService.findOne(orderDetail.getProductId());
  54.             if(productInfo == null) {
  55.                 throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
  56.             }
  57.             //2、计算订单总价
  58.             orderAmount = productInfo.getProductPrice()
  59.                     .multiply(new BigDecimal(orderDetail.getProductQuantity()))
  60.                     .add(orderAmount);
  61.             //订单详情入库
  62.             orderDetail.setDetailId(KeyUtil.genUniqueKey());
  63.             orderDetail.setOrderId(orderId);
  64.             BeanUtils.copyProperties(productInfo,orderDetail);
  65.             orderDetailDao.save(orderDetail);
  66.         }
  67.         //3、写入订单数据库(orderMaster和orderDetail)
  68.         OrderMaster orderMaster = new OrderMaster();
  69.         BeanUtils.copyProperties(orderDTO,orderMaster);
  70.         orderMaster.setOrderId(orderId);
  71.         orderMaster.setOrderAmount(orderAmount);
  72.         orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
  73.         orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
  74.         orderMasterDao.save(orderMaster);
  75.         //4、扣库存
  76.         List<CartDTO> cartDTOList = new ArrayList<>();
  77.         orderDTO.getOrderDetailList().stream().map(e->
  78.             new CartDTO(e.getProductId(),e.getProductQuantity())
  79.         ).collect(Collectors.toList());
  80.         productService.decreaseStock(cartDTOList);
  81.         return orderDTO;
  82.     }
  83.     @Override
  84.     public OrderDTO findOne(String orderId) {
  85.         OrderMaster orderMaster = orderMasterDao.findOne(orderId);
  86.         if(orderMaster == null) {
  87.             throw new SellException(ResultEnum.ORDER_NOT_EXIST);
  88.         }
  89.         List<OrderDetail> orderDetailList = orderDetailDao.findByOrderId(orderId);
  90.         if(CollectionUtils.isEmpty(orderDetailList)) {
  91.             throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
  92.         }
  93.         OrderDTO orderDTO = new OrderDTO();
  94.         BeanUtils.copyProperties(orderMaster,orderDTO);
  95.         orderDTO.setOrderDetailList(orderDetailList);
  96.         return orderDTO;
  97.     }
  98.     @Override
  99.     public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
  100.         Page<OrderMaster> orderMasterPage = orderMasterDao.findByBuyerOpenid(buyerOpenid,pageable);
  101.         List<OrderDTO> orderDTOList = OrderMaster2OrderDTOConverter.convert(orderMasterPage.getContent());
  102.         Page<OrderDTO> orderDTOPage = new PageImpl<OrderDTO>(orderDTOList,pageable,orderMasterPage.getTotalElements());
  103.         return orderDTOPage;
  104.     }
  105.     @Override
  106.     @Transactional
  107.     public OrderDTO cancel(OrderDTO orderDTO) {
  108.         OrderMaster orderMaster = new OrderMaster();
  109.         //判断订单状态
  110.         if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
  111.             log.error("【取消订单】订单状态不正确,orderId={},orderStatus",orderDTO.getOrderId(),orderDTO.getOrderStatus());
  112.         }
  113.         //修改订单状态
  114.         orderDTO.setOrderStatus(OrderStatusEnum.CANCEL.getCode());
  115.         BeanUtils.copyProperties(orderDTO,orderMaster);
  116.         OrderMaster updateResult =  orderMasterDao.save(orderMaster);
  117.         if(updateResult == null) {
  118.             log.error("【取消订单】更新失败,orderMaster={}",orderMaster);
  119.             throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
  120.         }
  121.         //返回库存
  122.         if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
  123.             log.error("【取消订单】订单中无商品详情,orderDTO={}",orderDTO);
  124.             throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
  125.         }
  126.         List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream()
  127.                 .map(e -> new CartDTO(e.getProductId(),e.getProductQuantity()))
  128.                 .collect(Collectors.toList());
  129.         productService.increaseStock(cartDTOList);
  130.         //如果已支付,需要退款
  131.         if(orderDTO.getPayStatus().equals(PayStatusEnum.SUCCESS.getCode())) {
  132.             //TODO
  133.         }
  134.         return orderDTO;
  135.     }
  136.     @Override
  137.     @Transactional
  138.     public OrderDTO finish(OrderDTO orderDTO) {
  139.         //判断订单状态
  140.         if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
  141.             log.error("【完结订单】订单状态不正确,orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus());
  142.             throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
  143.         }
  144.         //修改订单状态
  145.         orderDTO.setOrderStatus(OrderStatusEnum.FINISHED.getCode());
  146.         OrderMaster orderMaster = new OrderMaster();
  147.         BeanUtils.copyProperties(orderDTO,orderMaster);
  148.         OrderMaster updateResult = orderMasterDao.save(orderMaster);
  149.         if(updateResult == null) {
  150.             log.error("【完结订单】更新失败,orderStatus={}",orderMaster);
  151.             throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
  152.         }
  153.         return orderDTO;
  154.     }
  155.     @Override
  156.     @Transactional
  157.     public OrderDTO paid(OrderDTO orderDTO) {
  158.         //判断订单状态
  159.         if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
  160.             log.error("【订单支付完成】订单状态不正确,orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus());
  161.             throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
  162.         }
  163.         //判断支付状态
  164.         if(!orderDTO.getPayStatus().equals(PayStatusEnum.WAIT.getCode())) {
  165.             log.error("【订单支付完成】订单支付状态不正确,orderDTO={}",orderDTO);
  166.             throw new SellException(ResultEnum.ORDER_PAY_STATUS_ERROR);
  167.         }
  168.         //修改支付状态
  169.         orderDTO.setPayStatus(PayStatusEnum.SUCCESS.getCode());
  170.         OrderMaster orderMaster = new OrderMaster();
  171.         BeanUtils.copyProperties(orderDTO,orderMaster);
  172.         OrderMaster updateResult = orderMasterDao.save(orderMaster);
  173.         if(updateResult == null) {
  174.             log.error("【完结订单】更新失败,orderMaster={}",orderMaster);
  175.             throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
  176.         }
  177.         return orderDTO;
  178.     }
  179. }

注意:

① 这些方法记得加 @Transactional

② 162 行的退款操作我们暂时还没写,以 //TODO 标识

 

五、ResultEnum.java 的补充

随着开发的推进,我们的枚举属性也不断增加

  1. package com.liuyanzhao.sell.enums;
  2. import lombok.Getter;
  3. /**
  4.  * 返回给前端提示
  5.  * @Author: 言曌
  6.  * @Date: 2017/11/13
  7.  * @Time: 下午12:05
  8.  */
  9. @Getter
  10. public enum ResultEnum {
  11.     PRODUCT_NOT_EXIST(10,"商品不存在"),
  12.     PRODUCT_STOCK_ERROR(11"库存不正确"),
  13.     ORDER_NOT_EXIST(12,"订单不存在"),
  14.     ORDERDETAIL_NOT_EXIST(13,"订单详情不存在"),
  15.     ORDER_STATUS_ERROR(14,"订单状态不正确"),
  16.     ORDER_UPDATE_FAIL(15,"订单更新失败"),
  17.     ORDER_DETAIL_EMPTY(16,"订单详情为空"),
  18.     ORDER_PAY_STATUS_ERROR(17,"订单支付状态不正确"),
  19.     ;
  20.     private Integer code;
  21.     private String message;
  22.     ResultEnum(Integer code, String message) {
  23.         this.code = code;
  24.         this.message = message;
  25.     }
  26. }

 

本文地址:https://liuyanzhao.com/6734.html

  • 微信
  • 赶快加我聊天吧
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
广告也精彩
言曌
广告也精彩

发表评论

:?::razz::sad::evil::!::smile::oops::grin::eek::shock::???::cool::lol::mad::twisted::roll::wink::idea::arrow::neutral::cry::mrgreen:

目前评论:5   其中:访客  3   博主  2

    • avatar 666

      买不起视频跑来看你更新了 :lol:

        • avatar 言曌  博主

          @666 视频内容比较多,最近比较忙,不准备更新了。

        • avatar 憧憬点滴记忆

          可以可以,膜拜大神+1

            • avatar 言曌  博主

              @憧憬点滴记忆 没有啦,我还是渣渣。准备明年春招找实习,然后秋招开始校招,19年7月份毕业,憧憬有推荐吗?