|  |  | 
 |  |  | import com.mzl.flower.dto.PriceDTO; | 
 |  |  | import com.mzl.flower.dto.request.menber.MemberGrowthRecordDTO; | 
 |  |  | import com.mzl.flower.dto.request.payment.*; | 
 |  |  | import com.mzl.flower.dto.response.coupon.CouponRecordVO; | 
 |  |  | import com.mzl.flower.dto.response.flower.FlowerCartListDTO; | 
 |  |  | import com.mzl.flower.dto.response.flower.FlowerCartListWrapDTO; | 
 |  |  | import com.mzl.flower.dto.response.flower.FlowerDTO; | 
 |  |  | 
 |  |  | import org.springframework.context.annotation.Lazy; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  |  | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.math.RoundingMode; | 
 |  |  | import java.time.LocalDate; | 
 |  |  | import java.time.LocalDateTime; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.time.LocalTime; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | @Service | 
 |  |  | @Transactional | 
 |  |  | 
 |  |  |                 c.setNum(dto.getNum()); | 
 |  |  |                 c.create(userId); | 
 |  |  |  | 
 |  |  |                 // 限购  1.首先查看购物车中的数量是否已经超过限售数量 | 
 |  |  |                 final FlowerDTO flowerDetail = flowerService.getFlowerDetail(c.getFlowerId()); | 
 |  |  |                 if(null!=c.getNum() && null!=flowerDetail.getLimited() | 
 |  |  |                         && c.getNum().compareTo(flowerDetail.getLimited())>0){ | 
 |  |  |                     throw new ValidationException("数量不能超过限售数量!"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 // 限购  2.查看已结单的订单的当前花朵已经购买的数量,加当前购物车的加入的数量是否超过限购的数量,查看当前5点前到昨天五点前 | 
 |  |  |                 Integer completeNumToday=getFlowerCompleteNumToday(userId,c.getFlowerId()); | 
 |  |  |                 Integer tmp=completeNumToday+c.getNum(); | 
 |  |  |                 if(null!=tmp && null!=flowerDetail.getLimited() | 
 |  |  |                       &&  tmp.compareTo(flowerDetail.getLimited())>0){ | 
 |  |  |                     throw new ValidationException("当天的订单的花数量加上购物车的数量不能超过限售数量!"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 cartMapper.insert(c); | 
 |  |  |             } else { | 
 |  |  |                 // 查看是否已经超过当前商品的限购数量 | 
 |  |  | 
 |  |  |                 // 限购  2.查看已结单的订单的当前花朵已经购买的数量,加当前购物车的加入的数量是否超过限购的数量,查看当前5点前到昨天五点前 | 
 |  |  |                 Integer completeNumToday=getFlowerCompleteNumToday(userId,c.getFlowerId()); | 
 |  |  |                 Integer tmp=completeNumToday+c.getNum(); | 
 |  |  |                 if(tmp.compareTo(flowerDetail.getLimited())>0){ | 
 |  |  |                 if(null!=tmp && null!=flowerDetail.getLimited() | 
 |  |  |                        && tmp.compareTo(flowerDetail.getLimited())>0){ | 
 |  |  |                     throw new ValidationException("当天的订单的花数量加上购物车的数量不能超过限售数量!"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  | 
 |  |  |             Integer completeNumToday=getFlowerCompleteNumToday(userId,cartSaveDTO.getId()); | 
 |  |  |             Integer tmp=completeNumToday+cartSaveDTO.getNum(); | 
 |  |  |             Flower flower=flowerMapper.selectById(cartSaveDTO.getId()); | 
 |  |  |             if(tmp.compareTo(flower.getLimited())>0){ | 
 |  |  |             if(null!=flower.getLimited() && tmp.compareTo(flower.getLimited())>0){ | 
 |  |  |                 throw new ValidationException("商品:'"+flower.getName()+"' 昨天17:00到今天17:00 超过限售数量:"+flower.getLimited()+"!"); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  | 
 |  |  |             // 限购数量 鲜花数量校验 | 
 |  |  |             Integer completeNumToday=getFlowerCompleteNumToday(userId,flower.getId()); | 
 |  |  |             Integer tmp=completeNumToday+flower.getNum(); | 
 |  |  |             if(tmp.compareTo(flower.getLimited())>0){ | 
 |  |  |             if(null!=tmp && null!=flower.getLimited() | 
 |  |  |                     && tmp.compareTo(flower.getLimited())>0){ | 
 |  |  |                 throw new ValidationException("商品:'"+flower.getName()+"' 昨天17:00到今天17:00 超过限售数量:"+flower.getLimited()+"!"); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  | 
 |  |  |         List<FlowerCartListDTO> flowers = p.getFlowers(); | 
 |  |  |         Map<Long, List<ParamItemDTO>> paramMap = new HashMap<>(); | 
 |  |  |         final BigDecimal flowerAmount = order.getFlowerAmount();//订单商品总价 | 
 |  |  |         final BigDecimal memberCouponAmount = order.getMemberCouponAmount();//使用优惠券面值 | 
 |  |  |         final BigDecimal memberCouponAmount = getAmount(order.getMemberCouponAmount());//使用优惠券面值 | 
 |  |  |         BigDecimal usedCouponAmount = new BigDecimal(0); | 
 |  |  |         for (int i = 0; i < flowers.size(); i++) { | 
 |  |  |             FlowerCartListDTO f = flowers.get(i); | 
 |  |  | 
 |  |  |     private BigDecimal calculateCoupon(BigDecimal couponAmount, BigDecimal itemTotalAmount | 
 |  |  |             , BigDecimal totalAmount, Integer num, BigDecimal price | 
 |  |  |             , boolean isLastOne, BigDecimal usedCouponAmount){ | 
 |  |  |         if(couponAmount == null){ | 
 |  |  |         if(couponAmount == null || couponAmount.doubleValue() == 0){ | 
 |  |  |             return new BigDecimal(0); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | 
 |  |  |         List<OrderListDTO> ls = getOrderListBase(page, dto); | 
 |  |  |         if (ls != null && ls.size() > 0) { | 
 |  |  |             List<String> orderIds = new ArrayList<>(); | 
 |  |  |             Map<String, OrderListDTO> orderMap = new HashMap<>(); | 
 |  |  |             for (OrderListDTO o : ls) { | 
 |  |  |                 o.setCouldRefund(couldRefund(o.getStatusBackend(), o.getPaymentTime())); | 
 |  |  |                 orderIds.add(o.getId()); | 
 |  |  |                 orderMap.put(o.getId(), o); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             List<OrderItemListDTO> itemList = orderItemMapper.getOrderItems(orderIds); | 
 |  |  | 
 |  |  |                 List<OrderItemListDTO> ll = map.computeIfAbsent(orderId, k -> new ArrayList<>()); | 
 |  |  |                 OrderItemListDTO d = new OrderItemListDTO(); | 
 |  |  |                 BeanUtils.copyProperties(oi, d); | 
 |  |  |  | 
 |  |  |                 OrderListDTO o = orderMap.get(orderId); | 
 |  |  |                 if(dto.isCustomerQuery() && o != null && StringUtils.isEmpty(o.getTransferId())){ | 
 |  |  |                     d.setReduceNum(0); | 
 |  |  |                     d.setLackNum(0); | 
 |  |  |                     d.setReplaceNum(0); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 ll.add(d); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     public Page<OrderListDTO> selectCustomerOrderList(Page page, OrderQueryDTO dto) { | 
 |  |  |         dto.setCreateBy(SecurityUtils.getUserId()); | 
 |  |  |         dto.setCustomerQuery(true); | 
 |  |  |  | 
 |  |  |         return selectOrderList(page, dto); | 
 |  |  |     } | 
 |  |  | 
 |  |  |  | 
 |  |  |         List<OrderPointGoodsListDTO> pointGoodsList = orderMapper.getPointGoodsList(id); | 
 |  |  |         dto.setPointGoodsList(pointGoodsList); | 
 |  |  |  | 
 |  |  |         //判断优惠券ID是否为空 用户优惠券id | 
 |  |  |         if(StringUtils.isNotBlank(o.getMemberCouponId())){ | 
 |  |  |             CouponRecordVO couponRecordVO = couponRecordService.getCouponRecordById(o.getMemberCouponId()); | 
 |  |  |             if(!ObjectUtils.isEmpty(couponRecordVO)){ | 
 |  |  |                 dto.setCouponDiscountType(couponRecordVO.getCouponDiscountType()); | 
 |  |  |                 dto.setMinOrderAmount("满"+couponRecordVO.getMinOrderAmount()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public Integer getFlowerCompleteNumToday(String userId,Long flowerId){ | 
 |  |  |         Integer completeNum= orderMapper.getFlowerCompleteNumToday(userId,flowerId); | 
 |  |  |  | 
 |  |  |         int completeNum = 0; | 
 |  |  |         // 获取当前时间 | 
 |  |  |         LocalDateTime now = LocalDateTime.now(); | 
 |  |  |         // 构造今天的17:00 | 
 |  |  |         LocalDateTime todayAtFivePM = LocalDateTime.of(LocalDate.now(), LocalTime.of(17, 0)); | 
 |  |  |         // 定义时间区间的开始和结束时间 | 
 |  |  |         LocalDateTime startTime; | 
 |  |  |         LocalDateTime endTime; | 
 |  |  |  | 
 |  |  |         if (now.isAfter(todayAtFivePM)) { | 
 |  |  |             // 当前时间大于今天的17:00,取今天17:00到明天17:00的区间 | 
 |  |  |             startTime = todayAtFivePM; | 
 |  |  |             endTime = todayAtFivePM.plusDays(1);  // 明天的17:00 | 
 |  |  |         } else { | 
 |  |  |             // 当前时间小于今天的17:00,取昨天17:00到今天17:00的区间 | 
 |  |  |             startTime = todayAtFivePM.minusDays(1);  // 昨天的17:00 | 
 |  |  |             endTime = todayAtFivePM;  // 今天的17:00 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 调用Mapper方法,传递开始时间和结束时间 | 
 |  |  |         completeNum = orderMapper.getFlowerCompleteNumWithinTimeRange(userId, flowerId, startTime, endTime); | 
 |  |  |  | 
 |  |  |         return completeNum; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Transactional | 
 |  |  |     public String copyOrder(String id) { | 
 |  |  |         List<OrderItem> ls = orderItemMapper.selectList( | 
 |  |  |                 new QueryWrapper<OrderItem>().eq("order_id", id)); | 
 |  |  |         StringBuilder sb=new StringBuilder(); | 
 |  |  |         ls.stream().forEach(orderItem -> { | 
 |  |  |             Cart cart = null; | 
 |  |  |             final Long flowerId = orderItem.getFlowerId(); | 
 |  |  |             final Integer num = orderItem.getNum(); | 
 |  |  |             final String userId=orderItem.getCreateBy(); | 
 |  |  |             // 查看当前的花是否已经下架 | 
 |  |  |             Flower flower = flowerMapper.selectById(flowerId); | 
 |  |  |             // 查看当前的花的限购 | 
 |  |  |             final Integer limited = flower.getLimited(); | 
 |  |  |  | 
 |  |  |             // 查看当前购物车里面是否已经存在当前花 | 
 |  |  |             cart = cartMapper.selectOne(new QueryWrapper<Cart>() | 
 |  |  |                     .eq("create_by", orderItem.getCreateBy()).eq("flower_id",flowerId)); | 
 |  |  |  | 
 |  |  |             if(null==cart){ | 
 |  |  |                 cart = new Cart(); | 
 |  |  |                 cart.setFlowerId(flowerId); | 
 |  |  |                 cart.setNum(0); | 
 |  |  |                 cart.create(orderItem.getCreateBy()); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (!flower.getStatus().equals(Constants.FLOWER_STATUS.UP.name())) { | 
 |  |  |                 sb.append(flower.getName() + "已下架"); | 
 |  |  |             }else if(flower.getStatus().equals(Constants.FLOWER_STATUS.UP.name())){ | 
 |  |  |                 // 未下架库存逻辑控制 | 
 |  |  |                 Integer tmpNum=cart.getNum()+num; | 
 |  |  |                 //查看当前的库存 | 
 |  |  |                 if (null!=flower.getStock() && flower.getStock() > 0 && flower.getStock().compareTo(tmpNum) >= 0) { | 
 |  |  |                     // 库存充足 | 
 |  |  |                     cart.setNum(tmpNum); | 
 |  |  |                 } else if (null!=flower.getStock() && flower.getStock() > 0 && flower.getStock().compareTo(tmpNum) < 0) { | 
 |  |  |                     // 库存不足,剩多少给多少 | 
 |  |  |                     cart.setNum(flower.getStock()); | 
 |  |  |                     sb.append(flower.getName() + "购物车的总数超过库存,已调整成库存数!"); | 
 |  |  |                 }else { | 
 |  |  |                     sb.append(flower.getName() + "已无库存"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 // 限购控制,如果当前的购物车的数量大于限购的话,则需要控制 | 
 |  |  |                 if(null!=limited && limited>0){ | 
 |  |  |                     Integer buyedNum= getFlowerCompleteNumToday(userId,flowerId); | 
 |  |  |                     Integer todayNum=cart.getNum()+buyedNum; | 
 |  |  |                     // | 
 |  |  |                     if(todayNum>limited){ | 
 |  |  |                         cart.setNum(limited-buyedNum<=0?0:limited-buyedNum); | 
 |  |  |                         sb.append(flower.getName() + "购物车的总数超过限购,已调整成未限购数!"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if(null!=cart){ | 
 |  |  |                 // 插入购物车 | 
 |  |  |                 if(null!=cart.getId()){ | 
 |  |  |                     // 新增 | 
 |  |  |                     cartMapper.updateById(cart); | 
 |  |  |                 }else{ | 
 |  |  |                    // 更新 | 
 |  |  |                     cartMapper.insert(cart); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         }); | 
 |  |  |  | 
 |  |  |         return sb.toString(); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  | } |