|
@@ -0,0 +1,87 @@
|
|
|
+package com.zt.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.zt.dto.OrderDetailsDto;
|
|
|
+import com.zt.dto.OrderDto;
|
|
|
+import com.zt.enums.CodeEnums;
|
|
|
+import com.zt.pojo.Coupon;
|
|
|
+import com.zt.pojo.GoodsSku;
|
|
|
+import com.zt.pojo.OrderDetails;
|
|
|
+import com.zt.pojo.ZkOrder;
|
|
|
+import com.zt.service.*;
|
|
|
+import com.zt.mapper.ZkOrderMapper;
|
|
|
+import com.zt.utils.AppJwtUtil;
|
|
|
+import com.zt.vo.Result;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author H
|
|
|
+* @description 针对表【zk_order】的数据库操作Service实现
|
|
|
+* @createDate 2025-05-09 16:28:09
|
|
|
+*/
|
|
|
+@Service
|
|
|
+public class ZkOrderServiceImpl extends ServiceImpl<ZkOrderMapper, ZkOrder>
|
|
|
+ implements ZkOrderService{
|
|
|
+ @Autowired
|
|
|
+ OrderDetailsService orderDetailsService;
|
|
|
+ @Autowired
|
|
|
+ ZkGoodsService goodsService;
|
|
|
+ @Autowired
|
|
|
+ GoodsSkuService goodsSkuService;
|
|
|
+ @Autowired
|
|
|
+ CouponService couponService;
|
|
|
+ @Override
|
|
|
+ public Result addOrder(OrderDto orderDto,String token) {
|
|
|
+ Claims claimsBody = AppJwtUtil.getClaimsBody(token);
|
|
|
+ Object id = claimsBody.get("id");
|
|
|
+ ZkOrder zkOrder = new ZkOrder();
|
|
|
+ zkOrder.setUserId((Long) id);
|
|
|
+ List<Long> collect = orderDto.getOrderDetailsDtoList().stream().map(OrderDetailsDto::getGoodsId).collect(Collectors.toList());
|
|
|
+ QueryWrapper<GoodsSku> queryWrapperSku=new QueryWrapper<>();
|
|
|
+ queryWrapperSku.lambda().in(GoodsSku::getGoodsId,collect);
|
|
|
+ List<GoodsSku> list = goodsSkuService.list(queryWrapperSku);
|
|
|
+ Map<Long, GoodsSku> collect1 = list.stream().collect(Collectors.toMap(GoodsSku::getGoodsId, GoodsSku -> GoodsSku));
|
|
|
+ BigDecimal zero = BigDecimal.ZERO;
|
|
|
+ for (OrderDetailsDto orderDetailsDto : orderDto.getOrderDetailsDtoList()) {
|
|
|
+ GoodsSku goodsSku = collect1.get(orderDetailsDto.getGoodsId());
|
|
|
+ BigDecimal multiply = goodsSku.getPrice().multiply(BigDecimal.valueOf(orderDetailsDto.getNum()));
|
|
|
+ zero=zero.add(multiply);
|
|
|
+ }
|
|
|
+ zkOrder.setTotalPrice(zero);
|
|
|
+ Coupon byId = couponService.getById(orderDto.getCouponId());
|
|
|
+ if (zkOrder.getTotalPrice().compareTo(byId.getTerms())>=0){
|
|
|
+ zkOrder.setPreferentialPrice(byId.getFaceValue());
|
|
|
+ zkOrder.setRealPrice(zkOrder.getTotalPrice().subtract(zkOrder.getPreferentialPrice()));
|
|
|
+ }
|
|
|
+ zkOrder.setOrderTime(new Date());
|
|
|
+ zkOrder.setPaymentTime(new Date());
|
|
|
+ this.save(zkOrder);
|
|
|
+ for (OrderDetailsDto orderDetailsDto : orderDto.getOrderDetailsDtoList()) {
|
|
|
+ OrderDetails orderDetails=new OrderDetails();
|
|
|
+ BeanUtils.copyProperties(orderDetailsDto,orderDetails);
|
|
|
+ orderDetails.setOrderId(zkOrder.getId());
|
|
|
+
|
|
|
+ QueryWrapper<GoodsSku> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(GoodsSku::getGoodsId,orderDetails.getGoodsId());
|
|
|
+ GoodsSku one = goodsSkuService.getOne(queryWrapper);
|
|
|
+
|
|
|
+ orderDetails.setPrice(one.getPrice().multiply(BigDecimal.valueOf(orderDetails.getNum())));
|
|
|
+ orderDetailsService.save(orderDetails);
|
|
|
+ }
|
|
|
+ return Result.ok(CodeEnums.SUCCESS);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|