package com.atguigu.gulimall.order.service.impl;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.exception.NoStockException;
import com.atguigu.common.to.OrderTo;
import com.atguigu.common.to.mq.SeckillOrderTo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimall.order.constant.PayConstant;
import com.atguigu.gulimall.order.dao.OrderDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.entity.PaymentInfoEntity;
import com.atguigu.gulimall.order.enume.OrderStatusEnum;
import com.atguigu.gulimall.order.feign.CartFeignService;
import com.atguigu.gulimall.order.feign.MemberFeignService;
import com.atguigu.gulimall.order.feign.ProductFeignService;
import com.atguigu.gulimall.order.feign.WmsFeignService;
import com.atguigu.gulimall.order.interceptor.LoginUserInterceptor;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.service.PaymentInfoService;
import com.atguigu.gulimall.order.to.OrderCreateTo;
import com.atguigu.gulimall.order.to.SpuInfoVo;
import com.atguigu.gulimall.order.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.atguigu.common.constant.CartConstant.CART_PREFIX;
import static com.atguigu.gulimall.order.constant.OrderConstant.USER_ORDER_TOKEN_PREFIX;
@Slf4j
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
private ThreadLocal<OrderSubmitVo> confirmVoThreadLocal = new ThreadLocal<>();
@Autowired
private MemberFeignService memberFeignService;
@Autowired
private CartFeignService cartFeignService;
@Autowired
private WmsFeignService wmsFeignService;
@Autowired
private ProductFeignService productFeignService;
@Autowired
private OrderItemService orderItemService;
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private PaymentInfoService paymentInfoService;
@Autowired
private BestPayService bestPayService;
@Autowired
private ThreadPoolExecutor threadPoolExecutor;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<OrderEntity> page = this.page(
new Query<OrderEntity>().getPage(params),
new QueryWrapper<OrderEntity>()
);
return new PageUtils(page);
}
/**
* 订单确认页返回需要用的数据
* @return
*/
@Override
public OrderConfirmVo confirmOrder() throws ExecutionException, InterruptedException {
//构建OrderConfirmVo
OrderConfirmVo confirmVo = new OrderConfirmVo();
//获取当前用户登录的信息
MemberResponseVo memberResponseVo = LoginUserInterceptor.loginUser.get();
//TODO :获取当前线程请求头信息(解决Feign异步调用丢失请求头问题)
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//开启第一个异步任务
CompletableFuture<Void> addressFuture = CompletableFuture.runAsync(() -> {
//每一个线程都来共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//1、远程查询所有的收获地址列表
List<MemberAddressVo> address = memberFeignService.getAddress(memberResponseVo.getId());
confirmVo.setMemberAddressVos(address);
}, threadPoolExecutor);
//开启第二个异步任务
CompletableFuture<Void> cartInfoFuture = CompletableFuture.runAsync(() -> {
//每一个线程都来共享之前的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
//2、远程查询购物车所有选中的购物项
List<OrderItemVo> currentCartItems = cartFeignService.getCurrentCartItems();
confirmVo.setItems(currentCartItems);
//feign在远程调用之前要构造请求,调用很多的拦截器
}, threadPoolExecutor).thenRunAsync(() -> {
List<OrderItemVo> items = confirmVo.getItems();
//获取全部商品的id
List<Long> skuIds = items.stream()
.map((itemVo -> itemVo.getSkuId()))
.collect(Collectors.toList());
//远程查询商品库存信息
R skuHasStock = wmsFeignService.getSkuHasStock(skuIds);
List<SkuStockVo> skuStockVos = skuHasStock.getData("data", new TypeReference<List<SkuStockVo>>() {});
if (skuStockVos != null && skuStockVos.size() > 0) {
//将skuStockVos集合转换为map
Map<Long, Boolean> skuHasStockMap = skuStockVos.stream().collect(Collectors.toMap(SkuStockVo::getSkuId, SkuStockVo::getHasStock));
confirmVo.setStocks(skuHasStockMap);
}
},threadPoolExecutor);
//3、查询用户积分
Integer integration = memberResponseVo.getIntegration();
confirmVo.setIntegration(integration);
//4、价格数据自动计算
//TODO 5、防重令牌(防止表单重复提交)
//为用户设置一个token,三十分钟过期时间(存在redis)
String token = UUID.randomUUID().toString().replace("-", "");
redisTemplate.opsForValue().set(USER_ORDER_TOKEN_PREFIX+memberResponseVo.getId(),token,30, TimeUnit.MINUTES);
confirmVo.setOrderToken(token);
CompletableFuture.allOf(addressFuture,cartInfoFuture).get();
return confirmVo;
}
/**
* 提交订单
* @param vo
* @return
*/
// @Transactional(isolation = Isolation.READ_COMMITTED) 设置事务的隔离级别
// @Transactional(propagation = Propagation.REQUIRED) 设置事务的传播级别
@Transactional(rollbackFor = Exception.class)
// @GlobalTransactional(rollbackFor = Exception.class)
@Override
public SubmitOrderResponseVo submitOrder(OrderSubmitVo vo) {
confirmVoThreadLocal.set(vo);
SubmitOrderResponseVo responseVo = new SubmitOrderResponseVo();
//去创建、下订单、验令牌、验价格、锁定库存...
//获取当前用户登录的信息
MemberResponseVo memberResponseVo = LoginUserInterceptor.loginUser.get();
responseVo.setCode(0);
//1、验证令牌是否合法【令牌的对比和删除必须保证原子性】
String script = "if redis.call('get', KEYS[1]) == ARGV
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一款基于Java后端和JavaScript前端技术的谷粒商城设计源码,涵盖955个文件,其中包括678个Java文件、88个XML文件、38个JavaScript文件、24个YAML文件、22个HTML文件、20个属性文件、16个CSS文件、11个PNG图像文件、9个模板文件、6个配置文件。该商城系统以用户友好的界面和高效的后端处理为核心,旨在提供便捷的在线购物体验。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java后端和JavaScript前端技术的谷粒商城设计源码 (953个子文件)
file.conf 3KB
file.conf 3KB
file.conf 3KB
registry.conf 1KB
registry.conf 1KB
registry.conf 1KB
bootstrap.min.css 118KB
AdminLTE.min.css 88KB
screen.css 43KB
print.css 41KB
all-skins.min.css 40KB
font-awesome.min.css 30KB
swagger-ui.css 26KB
ui.jqgrid-bootstrap.css 18KB
ui.jqgrid.css 17KB
layer.css 14KB
layer.css 5KB
style.css 3KB
style.css 3KB
main.css 1KB
reset.css 773B
ui.jqgrid-bootstrap-ui.css 662B
typography.css 0B
Dockerfile 134B
fontawesome-webfont.eot 162KB
glyphicons-halflings-regular.eot 20KB
throbber.gif 9KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
expand.gif 73B
collapse.gif 69B
.gitignore 263B
reg.html 460KB
list.html 102KB
item.html 72KB
detail.html 62KB
confirm.html 42KB
index.html 29KB
cartList.html 28KB
orderList.html 28KB
login.html 8KB
pay.html 6KB
index.html 5KB
success.html 4KB
index.html 4KB
success.html 3KB
oauth2-redirect.html 2KB
generator.html 1KB
createForWxNative.html 1KB
main.html 1KB
o2c.html 479B
login.html 414B
list.html 274B
list.html 274B
favicon.ico 5KB
favicon.ico 4KB
favicon.ico 4KB
OrderServiceImpl.java 27KB
HTMLFilter.java 20KB
HTMLFilter.java 20KB
CategoryServiceImpl.java 19KB
MallSearchServiceImpl.java 16KB
SeckillServiceImpl.java 14KB
SpuInfoServiceImpl.java 13KB
HttpClientUtils.java 13KB
HttpClientUtils.java 13KB
WareSkuServiceImpl.java 12KB
AttrServiceImpl.java 11KB
HttpUtils.java 10KB
CartServiceImpl.java 10KB
MemberServiceImpl.java 9KB
GulimallSeckillApplicationTests.java 9KB
GenUtils.java 9KB
LoginController.java 7KB
IndexController.java 7KB
SkuInfoServiceImpl.java 7KB
GulimallSearchApplicationTests.java 6KB
PurchaseServiceImpl.java 6KB
DataSourceProperties.java 5KB
ScheduleUtils.java 5KB
WxApiController.java 5KB
ThreadTest.java 5KB
DateUtils.java 5KB
AttrGroupController.java 5KB
SysMenuController.java 5KB
RedisConfig.java 5KB
AttrGroupController.java 4KB
CartController.java 4KB
SysUserServiceImpl.java 4KB
MemberController.java 4KB
XssHttpServletRequestWrapper.java 4KB
ScheduleJobServiceImpl.java 4KB
AttrGroupServiceImpl.java 4KB
SysUserController.java 4KB
CategoryBrandRelationController.java 4KB
CategoryBrandRelationController.java 4KB
GulimallThirdPartyApplicationTests.java 4KB
MyRabbitMQConfig.java 4KB
SysRoleServiceImpl.java 4KB
共 953 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
lsx202406
- 粉丝: 2588
- 资源: 5608
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 材料损坏检测6-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 雷卡_KTV_s_q.apk
- HengCe-18900-2024-2030全球与中国二手半导体设备市场现状及未来发展趋势-样本.docx
- Infinite Scroll View 2.0
- 杂物检测63-YOLO(v5至v8)、COCO、CreateML、VOC数据集合集.rar
- HengCe-18900-2024-2030全球与中国氢燃料电池气体扩散层市场现状及未来发展趋势-样本.docx
- 机器狗的发展历史,介绍
- 木材钢管检测17-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 回文判断(C语言)(不必理会)
- 无人机检测24-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功