|
@@ -0,0 +1,112 @@
|
|
|
|
+package com.zhentao.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.zhentao.domain.Coupon;
|
|
|
|
+import com.zhentao.domain.OrderDetail;
|
|
|
|
+import com.zhentao.domain.OrderMaster;
|
|
|
|
+import com.zhentao.domain.ProductSku;
|
|
|
|
+import com.zhentao.dto.Orderdto;
|
|
|
|
+import com.zhentao.service.CouponService;
|
|
|
|
+import com.zhentao.service.OrderDetailService;
|
|
|
|
+import com.zhentao.service.OrderMasterService;
|
|
|
|
+import com.zhentao.mapper.OrderMasterMapper;
|
|
|
|
+import com.zhentao.service.ProductSkuService;
|
|
|
|
+import com.zhentao.tool.TokenUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+* @author 86183
|
|
|
|
+* @description 针对表【order_master(订单主表)】的数据库操作Service实现
|
|
|
|
+* @createDate 2025-05-09 19:36:08
|
|
|
|
+*/
|
|
|
|
+@Service
|
|
|
|
+public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster>
|
|
|
|
+ implements OrderMasterService{
|
|
|
|
+ @Autowired
|
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ProductSkuService productSkuService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private CouponService service;
|
|
|
|
+ @Autowired
|
|
|
|
+ private OrderDetailService orderDetailService;
|
|
|
|
+ @Override
|
|
|
|
+ public void createOrder(Orderdto orderdto) {
|
|
|
|
+// 创建一个对象
|
|
|
|
+ OrderMaster orderMaster = new OrderMaster();
|
|
|
|
+// 创建出一个雪花算法id
|
|
|
|
+ long l = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
|
+ orderMaster.setOrderId(l);
|
|
|
|
+// 判断token是否为空 设置用户id
|
|
|
|
+ String userIdFromToken = TokenUtils.getUserIdFromToken(orderdto.getToken());
|
|
|
|
+ System.err.println("用户id"+userIdFromToken);
|
|
|
|
+ if (userIdFromToken==null){
|
|
|
|
+ String userId = stringRedisTemplate.opsForValue().get("userId");
|
|
|
|
+ String userIdFromToken1 = TokenUtils.getUserIdFromToken(userId);
|
|
|
|
+ orderMaster.setUserId(userIdFromToken1);
|
|
|
|
+ }else {
|
|
|
|
+ orderMaster.setUserId(userIdFromToken);
|
|
|
|
+ }
|
|
|
|
+// 查询商品信息转换Map
|
|
|
|
+ List<String> collect1 = orderdto.getOrderDetails().stream().map(OrderDetail::getSkuId).collect(Collectors.toList());
|
|
|
|
+ System.err.println("所有的skuId:"+collect1);
|
|
|
|
+ List<ProductSku> productSkus = new ArrayList<>();
|
|
|
|
+ for (String a:collect1) {
|
|
|
|
+ QueryWrapper<ProductSku> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("sku_id",a);
|
|
|
|
+ ProductSku one = productSkuService.getOne(queryWrapper);
|
|
|
|
+ productSkus.add(one);
|
|
|
|
+ }
|
|
|
|
+ System.err.println(productSkus);
|
|
|
|
+ Map<String, ProductSku> collect = productSkus.stream().collect(Collectors.toMap(ProductSku::getSkuId, p -> p));
|
|
|
|
+ System.err.println("Map集合"+collect);
|
|
|
|
+// 计算总价格
|
|
|
|
+ BigDecimal total = new BigDecimal("0");
|
|
|
|
+ for (OrderDetail o:orderdto.getOrderDetails()){
|
|
|
|
+// 获取sku的基本信息
|
|
|
|
+ ProductSku productSku = collect.get(o.getSkuId());
|
|
|
|
+// 小计
|
|
|
|
+ BigDecimal multiply = productSku.getUnitPrice().multiply(new BigDecimal(o.getQuantity()));
|
|
|
|
+ o.setAmount(multiply);
|
|
|
|
+ total = total.add(multiply);
|
|
|
|
+ }
|
|
|
|
+ orderMaster.setTotalAmount(total);
|
|
|
|
+ System.err.println("总价格"+total);
|
|
|
|
+// 扣减的优惠券
|
|
|
|
+ QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("coupon_id",orderdto.getCouponId());
|
|
|
|
+ Coupon one = service.getOne(queryWrapper);
|
|
|
|
+// 设置优惠券id
|
|
|
|
+ orderMaster.setCouponId(orderdto.getCouponId());
|
|
|
|
+// 设置优惠的金额
|
|
|
|
+ orderMaster.setDiscountAmount(one.getDiscountAmount());
|
|
|
|
+// 设置实付金额
|
|
|
|
+ orderMaster.setActualAmount(total.subtract(one.getDiscountAmount()));
|
|
|
|
+ System.err.println("订单的所有信息:"+orderMaster);
|
|
|
|
+ boolean save = this.save(orderMaster);
|
|
|
|
+ for (OrderDetail o:orderdto.getOrderDetails()) {
|
|
|
|
+ long a = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
|
+ o.setDetailId(a);
|
|
|
|
+ o.setOrderId(l);
|
|
|
|
+ orderDetailService.save(o);
|
|
|
|
+ }
|
|
|
|
+ for(ProductSku p:productSkus){
|
|
|
|
+ for (OrderDetail o:orderdto.getOrderDetails()) {
|
|
|
|
+ if (p.getSkuId().equals(o.getSkuId())){
|
|
|
|
+ p.setStock(p.getStock()-o.getQuantity());
|
|
|
|
+ productSkuService.updateById(p);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|