package com.imooc.service.impl;
import com.imooc.converter.OrderMaster2OrderDTOConverter;
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.*;
import com.imooc.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.List;
import java.util.stream.Collectors;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private ProductService productService;
@Autowired
private OrderDetailRepository orderDetailRepository;
@Autowired
private OrderMasterRepository orderMasterRepository;
@Autowired
private PayService payService;
@Autowired
private PushMessageService pushMessageService;
@Autowired
private WebSocket webSocket;
@Override
@Transactional
public OrderDTO create(OrderDTO orderDTO) {
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);
// CartDTO cartDTO = new CartDTO(orderDetail.getProductId(), orderDetail.getProductQuantity());
// cartDTOList.add(cartDTO);
}
//3. 写入订单数据库(orderMaster和orderDetail)
OrderMaster orderMaster = new OrderMaster();
orderDTO.setOrderId(orderId);
BeanUtils.copyProperties(orderDTO, orderMaster);
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);
//发送websocket消息
webSocket.sendMessage(orderDTO.getOrderId());
return orderDTO;
}
@Override
public OrderDTO findOne(String orderId) {
OrderMaster orderMaster = orderMasterRepository.findOne(orderId);
if (orderMaster == null) {
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
List<OrderDetail> orderDetailList = orderDetailRepository.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 = orderMasterRepository.findByBuyerOpenid(buyerOpenid, pageable);
List<OrderDTO> orderDTOList = OrderMaster2OrderDTOConverter.convert(orderMasterPage.getContent());
return new PageImpl<OrderDTO>(orderDTOList, pageable, orderMasterPage.getTotalElements());
}
@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());
throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
}
//修改订单状态
orderDTO.setOrderStatus(OrderStatusEnum.CANCEL.getCode());
BeanUtils.copyProperties(orderDTO, orderMaster);
OrderMaster updateResult = orderMasterRepository.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())) {
payService.refund(orderDTO);
}
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 = orderMasterRepository.save(orderMaster);
if (updateResult == null) {
log.error("【完结订单】更新失败, orderMaster={}", orderMaster);
throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
}
//推送微信模版消息
pushMessageService.orderStatus(orderDTO);
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 ne
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源介绍】 基于springboot框架实现微信点餐系统源码(含微信支付,公众号开发,aop以及websocket思想).zip 【备注】 该项目是个人毕设项目,答辩评审分达到95分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。 该资源主要针对计算机、通信、人工智能、自动化等相关专业的学生、老师或从业者下载使用,亦可作为期末课程设计、课程大作业、毕业设计等。 项目整体具有较高的学习借鉴价值!基础能力强的可以在此基础上修改调整,以实现不同的功能。 欢迎下载,欢迎沟通,互相学习,共同进步!提供答疑!
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot框架实现微信点餐系统,含微信支付,公众号开发,aop以及websocket思想.zip (94个子文件)
project_code
pom.xml 3KB
doc
API.md 4KB
src
test
java
com
imooc
LoggerTest.java 717B
LoggerTest2.java 101B
SellApplicationTests.java 324B
main
resources
templates
category
index.ftl 1KB
list.ftl 2KB
order
list.ftl 5KB
detail.ftl 3KB
pay
create.ftl 1KB
success.ftl 107B
common
nav.ftl 1KB
header.ftl 237B
success.ftl 740B
error.ftl 736B
product
index.ftl 3KB
list.ftl 4KB
static
pay.html 1KB
api
ratings.json 7KB
seller.json 2KB
css
style.css 6KB
mp3
song.mp3 157KB
application.yml 1KB
logback-spring.xml 2KB
java
com
imooc
constant
CookieConstant.java 146B
RedisConstant.java 164B
repository
ProductInfoRepository.java 312B
ProductCategoryRepository.java 339B
OrderDetailRepository.java 299B
SellerInfoRepository.java 262B
OrderMasterRepository.java 396B
converter
OrderForm2OrderDTOConverter.java 1KB
OrderMaster2OrderDTOConverter.java 651B
handler
SellExceptionHandler.java 1003B
aspect
SellerAuthorizeAspect.java 2KB
dataobject
SellerInfo.java 283B
OrderMaster.java 1KB
ProductCategory.java 769B
ProductInfo.java 1KB
OrderDetail.java 617B
controller
SellerOrderController.java 4KB
WeixinController.java 979B
PayController.java 2KB
SellerProductController.java 5KB
SellerUserController.java 3KB
BuyerProductController.java 3KB
SellerCategoryController.java 3KB
WechatController.java 3KB
BuyerOrderController.java 3KB
utils
serializer
Date2LongSerializer.java 605B
CookieUtil.java 2KB
ResultVOUtil.java 588B
MathUtil.java 426B
KeyUtil.java 396B
EnumUtil.java 354B
JsonUtil.java 346B
SellApplication.java 297B
service
BuyerService.java 273B
OrderService.java 701B
WebSocket.java 1KB
CategoryService.java 382B
SellerService.java 273B
ProductService.java 769B
impl
PayServiceImpl.java 4KB
SellerServiceImpl.java 535B
BuyerServiceImpl.java 2KB
ProductServiceImpl.java 3KB
CategoryServiceImpl.java 1007B
OrderServiceImpl.java 9KB
PushMessageServiceImpl.java 2KB
PushMessageService.java 237B
PayService.java 338B
enums
ProductStatusEnum.java 361B
PayStatusEnum.java 347B
CodeEnum.java 79B
OrderStatusEnum.java 368B
ResultEnum.java 1KB
exception
SellerAuthorizeException.java 97B
SellException.java 402B
dto
CartDTO.java 361B
OrderDTO.java 2KB
VO
ProductInfoVO.java 500B
ResultVO.java 270B
ProductVO.java 387B
form
ProductForm.java 477B
CategoryForm.java 234B
OrderForm.java 667B
config
WechatPayConfig.java 1KB
WebSocketConfig.java 374B
WechatOpenConfig.java 1KB
WechatAccountConfig.java 945B
WechatMpConfig.java 1KB
ProjectUrlConfig.java 514B
.gitignore 249B
共 94 条
- 1
资源评论
GoogleNetᅟᅠ
- 粉丝: 4163
- 资源: 2487
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python的简易系统监控设计源码
- 基于Java全栈技术的会议室预约系统后端设计源码
- 基于FastAPI的分布式项目架构设计源码
- 基于Java和HTML的全方位人才分配系统设计源码
- ubuntu20.04 openssh-server离线安装包
- 【Nature封刊】Heat Assisted Detection and Ranging输入数据
- 快速导出项目中git修改文件记录
- 基于Java的银行科技岗AI云账户系统后端设计源码
- 基于Apicloud的HybridStart设计源码,JavaScript全栈混合应用开发框架
- 永磁同步电机基于正弦波注入的无感控制策略,基于高频脉冲注入的转子位置估计策略,MATLAB仿真,simulink仿真另有基于方波
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功