package cn.imlxy.service.impl;
import cn.imlxy.converter.OrderMaster2OrderDTOConverter;
import cn.imlxy.dao.OrderDetailDao;
import cn.imlxy.dao.OrderMasterDao;
import cn.imlxy.dto.CartDTO;
import cn.imlxy.dto.OrderDTO;
import cn.imlxy.entity.OrderDetail;
import cn.imlxy.entity.OrderMaster;
import cn.imlxy.entity.ProductInfo;
import cn.imlxy.enums.OrderStatusEnum;
import cn.imlxy.enums.PayStatusEnum;
import cn.imlxy.enums.ResultEnum;
import cn.imlxy.exception.SellException;
import cn.imlxy.service.*;
import cn.imlxy.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 javax.persistence.Transient;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName : OrderServiceImpl
* @Description : 订单
* @Author : LiuXinyu
* @Site : www.imlxy.cn
* @Date: 2020-05-11 21:46
*/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private ProductService productService;
@Autowired
private OrderDetailDao orderDetailDao;
@Autowired
private OrderMasterDao orderMasterDao;
@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);
//1、查询商品(数量、价格)
for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
ProductInfo productInfo = productService.findOne(orderDetail.getProductId());
if (productInfo == null) {
throw new SellException(ResultEnum.PRODUCT_NOT_EXIT);
// throw new ResponseBankException();
}
//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();
orderDTO.setOrderId(orderId);
BeanUtils.copyProperties(orderDTO, orderMaster);
orderMaster.setOrderAmount(orderAmount);
orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
orderMasterDao.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 = orderMasterDao.findOne(orderId);
if (orderMaster == null) {
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
List<OrderDetail> orderDetailList = orderDetailDao.findByOrOrderId(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<T> getContent(); //将所有数据返回为List
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();
// BeanUtils.copyProperties(orderDTO,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.CANCLE.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())) {
payService.refund(orderDTO);
}
return orderDTO;
}
@Override
@Transactional
public OrderDTO finish(OrderDTO orderDTO) {
//判断订单状态
if (orderDTO.getOrderStatus().equals(OrderStatusEnum.FINISHED.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("【完结订单】 更新失败,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={}", orde
没有合适的资源?快使用搜索试试~ 我知道了~
基于Spring Boot为主线的技术栈,采用RESTful风格架构的微信点餐系统.zip
共118个文件
java:90个
ftl:12个
yml:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 2 浏览量
2023-09-11
20:07:09
上传
评论
收藏 280KB ZIP 举报
温馨提示
基于springboot框架的毕业设计,可用作本科毕业设计,项目都经测试过,能跑通,可以直接运行,请放心下载使用。
资源推荐
资源详情
资源评论
收起资源包目录
基于Spring Boot为主线的技术栈,采用RESTful风格架构的微信点餐系统.zip (118个子文件)
mvnw.cmd 6KB
style.css 6KB
list.ftl 5KB
list.ftl 4KB
index.ftl 3KB
detail.ftl 3KB
list.ftl 2KB
nav.ftl 1KB
create.ftl 1KB
index.ftl 1KB
success.ftl 740B
error.ftl 736B
header.ftl 374B
success.ftl 107B
.gitignore 333B
pay.html 1KB
maven-wrapper.jar 50KB
OrderServiceImpl.java 9KB
SellerProductController.java 5KB
MavenWrapperDownloader.java 5KB
ProductServiceImpl.java 4KB
PayServiceImpl.java 4KB
SellerOrderController.java 4KB
WeChatController.java 4KB
BuyerOrderController.java 4KB
OrderServiceImplTest.java 3KB
SellerCategoryController.java 3KB
BuyerProductController.java 3KB
SellerUserController.java 3KB
RedisLock.java 3KB
SecKillServiceImpl.java 2KB
ProductServiceImplTest.java 2KB
ProductCategoryMapperTest.java 2KB
PushMessageServiceImpl.java 2KB
SellerAuthorize.java 2KB
ProductCategoryMapper.java 2KB
PayController.java 2KB
SellerExceptionHandler.java 2KB
OrderDTO.java 2KB
BuyerServiceImpl.java 2KB
CookieUtil.java 2KB
WebSocket.java 2KB
OrderForm2OrderDTOConverter.java 2KB
OrderMasterDaoTest.java 1KB
ProductCategoryServiceImplTest.java 1KB
WechatPayConfig.java 1KB
ProductInfoDaoTest.java 1KB
SecKillController.java 1KB
OrderDetailDaoTest.java 1KB
ProductInfo.java 1KB
ProductCategoryDaoTest.java 1KB
ResultEnum.java 1KB
ProductCategoryServiceImpl.java 1KB
OrderMaster.java 1KB
SellerInfoDaoTest.java 1KB
WechatOpenConfig.java 1KB
WechatMpConfig.java 1KB
WechatAccountConfig.java 1KB
PayServiceImplTest.java 947B
ProductCategory.java 876B
LoggerTest.java 853B
OrderMaster2OrderDTOConverter.java 806B
OrderForm.java 803B
ProductService.java 798B
SellerServiceImplTest.java 753B
ProductInfoVO.java 743B
Date2LongSerializer.java 739B
PushMessageServiceImplTest.java 727B
ResultVOUtil.java 706B
OrderDetail.java 686B
OrderService.java 676B
SellerServiceImpl.java 646B
ProjectUrlConfig.java 639B
ProductVO.java 631B
MathUtil.java 614B
ProductForm.java 592B
SellException.java 559B
ProductCategoryService.java 557B
ResultVO.java 546B
SellerInfo.java 529B
WebSocketConfig.java 504B
KeyUtil.java 499B
SellApplication.java 485B
EnumUtil.java 471B
JsonUtil.java 468B
CartDTO.java 465B
ProductCategoryDao.java 454B
ProductInfoDao.java 429B
SecKillService.java 384B
OrderMasterDao.java 378B
OrderStatusEnum.java 367B
CategoryForm.java 361B
ProductCategoryDao.java 357B
ProductStatusEnum.java 346B
PayStatusEnum.java 340B
SellApplicationTests.java 332B
PayService.java 317B
OrderDetailDao.java 284B
RedisConstant.java 280B
BuyerService.java 252B
共 118 条
- 1
- 2
资源评论
c++服务器开发
- 粉丝: 3175
- 资源: 4461
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- univer v3.2.0原型图(univer为海归服务平台).rp
- U行政V1.0.1.rp
- 基于JAVA+SpringBoot+Vue+MySQL的民宿管理平台 源码+数据库+论文(高分毕业设计).zip
- golang 的 redis 客户端.zip
- V4User前台1.0.rp
- Vadas小程序1.0.rp
- VIP礼物1.0.rp
- VPN APP产品设计原型 需求文档.rp
- V小护.rp
- VX读书-临摹.rp
- web模板框架_前台_后台都可用,路径问题修改.rp
- web模板框架_前台_后台都可用,时间问题不在优化了.rp
- xiaodanke(用户名:wq,密码:123).rp
- wifi.rp
- 基于JAVA+SpringBoot+Vue+MySQL的实验室管理系统 源码+数据库+论文(高分毕业设计).zip
- hiredis Redis 客户端库的绑定.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功