上一节介绍了买家订单 service 层的创建订单的实现,本文将介绍剩余的其他部分(查询一个订单,查询订单列表,取消订单,完结订单和支付订单)。
抽出常用方法
lambad 表达式
其他的继承上一节...
1、修改的文件
其他的继承上一节
OrderMaster2OrderDTOConverter.java
这个类的目的就是将 orderMaster 对象转成 orderDTO 对象,将 List<OrderMaster> 转成 List<OrderDTO> ,我们之前的做法都是直接在 Service 里写,这样代码冗余度较高。通过这个类,我们实现转换会更加方便。
我们之前在第三章里的商品信息Service层开发和测试中的增加库存和减少库存的方法还没有实现
现在补上。
1、OrderService.java
2、OrderServiceImpl.java
注意:
① 这些方法记得加 @Transactional
② 162 行的退款操作我们暂时还没写,以 //TODO 标识
随着开发的推进,我们的枚举属性也不断增加
本文地址:https://liuyanzhao.com/6734.html
本文关键词
抽出常用方法
lambad 表达式
其他的继承上一节...
一、开发准备
1、修改的文件
其他的继承上一节
二、convert 创建
OrderMaster2OrderDTOConverter.java
- package com.liuyanzhao.sell.converter;
- import com.liuyanzhao.sell.dto.OrderDTO;
- import com.liuyanzhao.sell.entity.OrderMaster;
- import org.springframework.beans.BeanUtils;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @Author: 言曌
- * @Date: 2017/11/14
- * @Time: 下午10:02
- */
- public class OrderMaster2OrderDTOConverter {
- public static OrderDTO convert(OrderMaster orderMaster) {
- OrderDTO orderDTO = new OrderDTO();
- BeanUtils.copyProperties(orderMaster,orderDTO);
- return orderDTO;
- }
- public static List<OrderDTO> convert(List<OrderMaster> orderMasterList) {
- return orderMasterList.stream().map(e ->
- convert(e)
- ).collect(Collectors.toList());
- }
- }
这个类的目的就是将 orderMaster 对象转成 orderDTO 对象,将 List<OrderMaster> 转成 List<OrderDTO> ,我们之前的做法都是直接在 Service 里写,这样代码冗余度较高。通过这个类,我们实现转换会更加方便。
三、ProductServiceImpl.java 的补充
我们之前在第三章里的商品信息Service层开发和测试中的增加库存和减少库存的方法还没有实现
现在补上。
- package com.liuyanzhao.sell.service.impl;
- import com.liuyanzhao.sell.dao.ProductInfoDao;
- import com.liuyanzhao.sell.dto.CartDTO;
- import com.liuyanzhao.sell.entity.ProductInfo;
- import com.liuyanzhao.sell.enums.ProductStatusEnum;
- import com.liuyanzhao.sell.enums.ResultEnum;
- import com.liuyanzhao.sell.exception.SellException;
- import com.liuyanzhao.sell.service.ProductService;
- 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.util.List;
- /**
- * @Author: 言曌
- * @Date: 2017/11/12
- * @Time: 上午11:39
- */
- @Service
- public class ProductServiceImpl implements ProductService {
- @Autowired
- private ProductInfoDao dao;
- @Override
- public ProductInfo findOne(String productId) {
- return dao.findOne(productId);
- }
- @Override
- public List<ProductInfo> findUpAll() {
- //return dao.findByProductStatus(0);
- return dao.findByProductStatus(ProductStatusEnum.UP.getCode());
- }
- @Override
- public Page<ProductInfo> findAll(Pageable pageable) {
- return dao.findAll(pageable);
- }
- @Override
- public ProductInfo save(ProductInfo productInfo) {
- return dao.save(productInfo);
- }
- @Override
- @Transactional
- public void increaseStock(List<CartDTO> cartDTOList) {
- for(CartDTO cartDTO : cartDTOList) {
- ProductInfo productInfo = dao.findOne(cartDTO.getProductId());
- if(productInfo == null) {
- throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
- }
- Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
- productInfo.setProductStock(result);
- dao.save(productInfo);
- }
- }
- @Override
- @Transactional
- public void decreaseStock(List<CartDTO> cartDTOList) {
- for(CartDTO cartDTO : cartDTOList) {
- ProductInfo productInfo = dao.findOne(cartDTO.getProductId());
- if(productInfo == null) {
- throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
- }
- Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
- if(result < 0) {
- throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
- }
- productInfo.setProductStatus(result);
- dao.save(productInfo);
- }
- }
- }
四、Service 层的创建
1、OrderService.java
- package com.liuyanzhao.sell.service;
- import com.liuyanzhao.sell.dto.OrderDTO;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- /**
- * @Author: 言曌
- * @Date: 2017/11/13
- * @Time: 上午11:17
- */
- 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);
- }
2、OrderServiceImpl.java
- package com.liuyanzhao.sell.service.impl;
- import com.liuyanzhao.sell.converter.OrderMaster2OrderDTOConverter;
- import com.liuyanzhao.sell.dao.OrderDetailDao;
- import com.liuyanzhao.sell.dao.OrderMasterDao;
- import com.liuyanzhao.sell.dto.CartDTO;
- import com.liuyanzhao.sell.dto.OrderDTO;
- import com.liuyanzhao.sell.entity.OrderDetail;
- import com.liuyanzhao.sell.entity.OrderMaster;
- import com.liuyanzhao.sell.entity.ProductInfo;
- import com.liuyanzhao.sell.enums.OrderStatusEnum;
- import com.liuyanzhao.sell.enums.PayStatusEnum;
- import com.liuyanzhao.sell.enums.ResultEnum;
- import com.liuyanzhao.sell.exception.SellException;
- import com.liuyanzhao.sell.service.OrderService;
- import com.liuyanzhao.sell.service.ProductService;
- import com.liuyanzhao.sell.utils.KeyUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @Author: 言曌
- * @Date: 2017/11/13
- * @Time: 上午11:29
- */
- @Slf4j
- @Service
- public class OrderServiceImpl implements OrderService{
- @Autowired
- private ProductService productService;
- @Autowired
- private OrderDetailDao orderDetailDao;
- @Autowired
- private OrderMasterDao orderMasterDao;
- @Override
- @Transactional //事务管理
- public OrderDTO create(OrderDTO orderDTO) {
- String orderId = KeyUtil.genUniqueKey();
- BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO );
- //1、查询商品(数量,价格)
- List<OrderDetail> orderDetailList = new ArrayList<>();
- 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);
- orderDetailDao.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());
- orderMasterDao.save(orderMaster);
- //4、扣库存
- List<CartDTO> cartDTOList = new ArrayList<>();
- 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) {
- OrderMaster orderMaster = orderMasterDao.findOne(orderId);
- if(orderMaster == null) {
- throw new SellException(ResultEnum.ORDER_NOT_EXIST);
- }
- List<OrderDetail> orderDetailList = orderDetailDao.findByOrderId(orderId);
- if(CollectionUtils.isEmpty(orderDetailList)) {
- throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
- }
- OrderDTO orderDTO = new OrderDTO();
- BeanUtils.copyProperties(orderMaster,orderDTO);
- orderDTO.setOrderDetailList(orderDetailList);
- return orderDTO;
- }
- @Override
- public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
- Page<OrderMaster> orderMasterPage = orderMasterDao.findByBuyerOpenid(buyerOpenid,pageable);
- List<OrderDTO> orderDTOList = OrderMaster2OrderDTOConverter.convert(orderMasterPage.getContent());
- Page<OrderDTO> orderDTOPage = new PageImpl<OrderDTO>(orderDTOList,pageable,orderMasterPage.getTotalElements());
- return orderDTOPage;
- }
- @Override
- @Transactional
- public OrderDTO cancel(OrderDTO orderDTO) {
- OrderMaster orderMaster = new OrderMaster();
- //判断订单状态
- if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
- log.error("【取消订单】订单状态不正确,orderId={},orderStatus",orderDTO.getOrderId(),orderDTO.getOrderStatus());
- }
- //修改订单状态
- orderDTO.setOrderStatus(OrderStatusEnum.CANCEL.getCode());
- BeanUtils.copyProperties(orderDTO,orderMaster);
- OrderMaster updateResult = orderMasterDao.save(orderMaster);
- if(updateResult == null) {
- log.error("【取消订单】更新失败,orderMaster={}",orderMaster);
- throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
- }
- //返回库存
- if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
- log.error("【取消订单】订单中无商品详情,orderDTO={}",orderDTO);
- throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
- }
- List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream()
- .map(e -> new CartDTO(e.getProductId(),e.getProductQuantity()))
- .collect(Collectors.toList());
- productService.increaseStock(cartDTOList);
- //如果已支付,需要退款
- if(orderDTO.getPayStatus().equals(PayStatusEnum.SUCCESS.getCode())) {
- //TODO
- }
- return orderDTO;
- }
- @Override
- @Transactional
- public OrderDTO finish(OrderDTO orderDTO) {
- //判断订单状态
- if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
- log.error("【完结订单】订单状态不正确,orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus());
- throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
- }
- //修改订单状态
- orderDTO.setOrderStatus(OrderStatusEnum.FINISHED.getCode());
- OrderMaster orderMaster = new OrderMaster();
- BeanUtils.copyProperties(orderDTO,orderMaster);
- OrderMaster updateResult = orderMasterDao.save(orderMaster);
- if(updateResult == null) {
- log.error("【完结订单】更新失败,orderStatus={}",orderMaster);
- throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
- }
- return orderDTO;
- }
- @Override
- @Transactional
- public OrderDTO paid(OrderDTO orderDTO) {
- //判断订单状态
- if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
- log.error("【订单支付完成】订单状态不正确,orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus());
- throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
- }
- //判断支付状态
- if(!orderDTO.getPayStatus().equals(PayStatusEnum.WAIT.getCode())) {
- log.error("【订单支付完成】订单支付状态不正确,orderDTO={}",orderDTO);
- throw new SellException(ResultEnum.ORDER_PAY_STATUS_ERROR);
- }
- //修改支付状态
- orderDTO.setPayStatus(PayStatusEnum.SUCCESS.getCode());
- OrderMaster orderMaster = new OrderMaster();
- BeanUtils.copyProperties(orderDTO,orderMaster);
- OrderMaster updateResult = orderMasterDao.save(orderMaster);
- if(updateResult == null) {
- log.error("【完结订单】更新失败,orderMaster={}",orderMaster);
- throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
- }
- return orderDTO;
- }
- }
注意:
① 这些方法记得加 @Transactional
② 162 行的退款操作我们暂时还没写,以 //TODO 标识
五、ResultEnum.java 的补充
随着开发的推进,我们的枚举属性也不断增加
- package com.liuyanzhao.sell.enums;
- import lombok.Getter;
- /**
- * 返回给前端提示
- * @Author: 言曌
- * @Date: 2017/11/13
- * @Time: 下午12:05
- */
- @Getter
- public enum ResultEnum {
- PRODUCT_NOT_EXIST(10,"商品不存在"),
- PRODUCT_STOCK_ERROR(11, "库存不正确"),
- ORDER_NOT_EXIST(12,"订单不存在"),
- ORDERDETAIL_NOT_EXIST(13,"订单详情不存在"),
- ORDER_STATUS_ERROR(14,"订单状态不正确"),
- ORDER_UPDATE_FAIL(15,"订单更新失败"),
- ORDER_DETAIL_EMPTY(16,"订单详情为空"),
- ORDER_PAY_STATUS_ERROR(17,"订单支付状态不正确"),
- ;
- private Integer code;
- private String message;
- ResultEnum(Integer code, String message) {
- this.code = code;
- this.message = message;
- }
- }
本文地址:https://liuyanzhao.com/6734.html
2017年11月22日 21:59:34
买不起视频跑来看你更新了 :lol:
2017年11月22日 23:16:16
视频内容比较多,最近比较忙,不准备更新了。
2017年11月16日 23:09:23
可以可以,膜拜大神+1
2017年11月16日 23:11:06
没有啦,我还是渣渣。准备明年春招找实习,然后秋招开始校招,19年7月份毕业,憧憬有推荐吗?
2017年11月17日 09:11:06
可以去大公司就去大公司