java点餐系统修改订单,Spring Boot点餐系统实战(7)–买家订单service层(上)

package com.liuyanzhao.sell.service.impl;

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 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.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

/**

* @Author: 言曌

* @Date: 2017/11/13

* @Time: 上午11:29

*/

@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 orderDetailList = new ArrayList<>();

for(OrderDetail orderDetail:orderDTO.getOrderDetailList()) {

ProductInfo productInfo = productService.findOne(orderDetail.getProductId());

if(productInfo == null) {

throw new SellException(ResultEnum.PRODUCT_NOT_EXISTS);

}

//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 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() {

return null;

}

@Override

public Page 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值