| | |
| | | import com.mzl.flower.dto.request.payment.*; |
| | | 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 com.mzl.flower.dto.response.flower.ParamItemDTO; |
| | | import com.mzl.flower.dto.response.partner.PartnerOrderDTO; |
| | | import com.mzl.flower.dto.response.payment.*; |
| | |
| | | import com.mzl.flower.entity.coupon.CouponRecordDO; |
| | | import com.mzl.flower.entity.customer.Address; |
| | | import com.mzl.flower.entity.customer.Customer; |
| | | import com.mzl.flower.entity.flower.Flower; |
| | | import com.mzl.flower.entity.flower.FlowerSupplierSaleNum; |
| | | import com.mzl.flower.entity.menber.Member; |
| | | import com.mzl.flower.entity.partner.Partner; |
| | |
| | | @Autowired |
| | | private CouponRecordService couponRecordService; |
| | | |
| | | |
| | | public void changeFlower2Cart(CartSaveDTO dto) { |
| | | String userId = SecurityUtils.getUserId(); |
| | | String key = userId + "_" + dto.getId(); |
| | |
| | | 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 { |
| | | c.setNum(c.getNum() + dto.getNum()); |
| | | // 查看是否已经超过当前商品的限购数量 |
| | | final FlowerDTO flowerDetail = flowerService.getFlowerDetail(c.getFlowerId()); |
| | | c.setNum((c.getNum()==null?0:c.getNum()) + dto.getNum()); |
| | | |
| | | // 限购 1.首先查看购物车中的数量是否已经超过限售数量 |
| | | 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("当天的订单的花数量加上购物车的数量不能超过限售数量!"); |
| | | } |
| | | |
| | | c.update(userId); |
| | | if (c.getNum() <= 0) { |
| | | cartMapper.deleteById(c.getId()); |
| | |
| | | } |
| | | |
| | | public PreOrderDTO getPreOrderInfo(List<CartSaveDTO> flowers, Map<Long, PriceDTO> priceMap) { |
| | | String userId = SecurityUtils.getUserId(); |
| | | |
| | | if (flowers == null || flowers.size() == 0) { |
| | | throw new ValidationException("请选择商品"); |
| | | } |
| | |
| | | } |
| | | idNumMap.put(id, num); |
| | | } |
| | | |
| | | // 限购数量校验 鲜花数量校验 |
| | | flowers.forEach(cartSaveDTO -> { |
| | | // 限购数量 鲜花数量校验 |
| | | Integer completeNumToday=getFlowerCompleteNumToday(userId,cartSaveDTO.getId()); |
| | | Integer tmp=completeNumToday+cartSaveDTO.getNum(); |
| | | Flower flower=flowerMapper.selectById(cartSaveDTO.getId()); |
| | | if(null!=flower.getLimited() && tmp.compareTo(flower.getLimited())>0){ |
| | | throw new ValidationException("商品:'"+flower.getName()+"' 昨天17:00到今天17:00 超过限售数量:"+flower.getLimited()+"!"); |
| | | } |
| | | }); |
| | | |
| | | PreOrderDTO result = new PreOrderDTO(); |
| | | |
| | | List<FlowerCartListDTO> ls = flowerMapper.selectFlowerOrderList(ids); |
| | |
| | | } |
| | | |
| | | public Map commitOrder(OrderCommitDTO dto, PreOrderDTO p, Map<Long, PriceDTO> priceMap) { |
| | | String userId = SecurityUtils.getUserId(); |
| | | p.getFlowers().forEach(flower -> { |
| | | // 限购数量 鲜花数量校验 |
| | | Integer completeNumToday=getFlowerCompleteNumToday(userId,flower.getId()); |
| | | Integer tmp=completeNumToday+flower.getNum(); |
| | | if(null!=tmp && null!=flower.getLimited() |
| | | && tmp.compareTo(flower.getLimited())>0){ |
| | | throw new ValidationException("商品:'"+flower.getName()+"' 昨天17:00到今天17:00 超过限售数量:"+flower.getLimited()+"!"); |
| | | } |
| | | }); |
| | | |
| | | WxMaJscode2SessionResult session = null; |
| | | try { |
| | | session = weChatService.getWxMaSessionInfo(dto.getWxcode()); |
| | |
| | | deliveryName = d.getName(); |
| | | } |
| | | |
| | | String userId = SecurityUtils.getUserId(); |
| | | |
| | | Order order = new Order(); |
| | | order.setId(UUIDGenerator.getUUID()); |
| | | |
| | |
| | | order.setTransportFee(transportFee); |
| | | order.setDeliveryName(deliveryName); |
| | | |
| | | BigDecimal totalAmount = order.getFlowerAmount().add(p.getPacking()).add(transportFee);//使用优惠券之前的总价 |
| | | BigDecimal totalAmount = order.getFlowerAmount();//使用优惠券之前的总价,使用商品价格 |
| | | CouponRecordDO coupon = useCouponRecord(order.getId(), totalAmount, dto.getCouponRecordId()); |
| | | log.info("用户优惠券: " + coupon); |
| | | if(coupon != null){ |
| | | order.setMemberCouponId(coupon.getId()); |
| | | order.setMemberCouponCode(coupon.getCouponCode()); |
| | |
| | | order.setMemberCouponAmount(coupon.getCouponDiscountValue()); |
| | | |
| | | totalAmount = totalAmount.subtract(order.getMemberCouponAmount());//满足条件需要减去优惠券金额 |
| | | } |
| | | if(totalAmount.doubleValue() < 0){//假如扣除优惠券后金额小于0,则按0计算 |
| | | totalAmount = new BigDecimal(0); |
| | | } |
| | | totalAmount = totalAmount.add(p.getPacking()).add(transportFee); |
| | | if(totalAmount.doubleValue() <= 0){//假如总价小于等于0,则支付0.01元 |
| | | totalAmount = new BigDecimal(0.01); |
| | | } |
| | | order.setTotalAmount(totalAmount); |
| | | |
| | |
| | | Map<Long, List<ParamItemDTO>> paramMap = new HashMap<>(); |
| | | final BigDecimal flowerAmount = order.getFlowerAmount();//订单商品总价 |
| | | final BigDecimal memberCouponAmount = order.getMemberCouponAmount();//使用优惠券面值 |
| | | for (FlowerCartListDTO f : flowers) { |
| | | BigDecimal usedCouponAmount = new BigDecimal(0); |
| | | for (int i = 0; i < flowers.size(); i++) { |
| | | FlowerCartListDTO f = flowers.get(i); |
| | | |
| | | OrderItem t = new OrderItem(); |
| | | t.setId(UUIDGenerator.getUUID()); |
| | | t.setOrderId(order.getId()); |
| | |
| | | t.setTotal(f.getTotalMember());//使用会员总价 |
| | | |
| | | t.setOriginalPrice(pp.getPrice());//非会员售价 |
| | | BigDecimal couponAmount = calculateCoupon(memberCouponAmount, t.getTotal(), flowerAmount, t.getNum()); |
| | | boolean isLastOne = i == (flowers.size() - 1); |
| | | BigDecimal couponAmount = calculateCoupon(memberCouponAmount, t.getTotal() |
| | | , flowerAmount, t.getNum(), t.getPrice(), isLastOne, usedCouponAmount); |
| | | t.setCouponAmount(couponAmount);//每扎平摊的优惠券面值 |
| | | t.setRealPrice(t.getPrice().subtract(couponAmount));//退款时使用的真实成交价 |
| | | t.setRealPrice(t.getPrice().subtract(couponAmount));//退款时使用的真实成交单价 |
| | | |
| | | t.setCouponAmountTotal(couponAmount.multiply(BigDecimal.valueOf(t.getNum()))); |
| | | if(isLastOne){ |
| | | t.setCouponAmountTotal(memberCouponAmount.subtract(usedCouponAmount)); |
| | | } else { |
| | | usedCouponAmount = usedCouponAmount.add(t.getCouponAmountTotal()); |
| | | } |
| | | t.setRealTotal(t.getTotal().subtract(t.getCouponAmountTotal())); |
| | | |
| | | t.create(userId); |
| | | orderItemMapper.insert(t); |
| | |
| | | * @param itemTotalAmount |
| | | * @param totalAmount |
| | | * @param num |
| | | * @param price |
| | | * @return |
| | | */ |
| | | private BigDecimal calculateCoupon(BigDecimal couponAmount, BigDecimal itemTotalAmount |
| | | , BigDecimal totalAmount, Integer num){ |
| | | , BigDecimal totalAmount, Integer num, BigDecimal price |
| | | , boolean isLastOne, BigDecimal usedCouponAmount){ |
| | | if(couponAmount == null){ |
| | | return new BigDecimal(0); |
| | | } |
| | | BigDecimal radio = itemTotalAmount.divide(totalAmount, 2, RoundingMode.HALF_UP);//计算该商品总价在订单中的占比 |
| | | |
| | | if(couponAmount.doubleValue() >= totalAmount.doubleValue()){ |
| | | return price; |
| | | } |
| | | if(isLastOne){//最后的商品使用优惠券剩下的面值 |
| | | BigDecimal subCoupon = couponAmount.subtract(usedCouponAmount);//占有的优惠券面值 |
| | | return subCoupon.divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | /*BigDecimal radio = itemTotalAmount.divide(totalAmount, 2, RoundingMode.HALF_UP);//计算该商品总价在订单中的占比 |
| | | BigDecimal subCoupon = couponAmount.multiply(radio);//占有的优惠券面值 |
| | | return subCoupon.divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP); |
| | | return subCoupon.divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP);*/ |
| | | |
| | | //不能分步计算,分步计算会有精度问题 |
| | | return couponAmount.multiply(itemTotalAmount).divide(totalAmount.multiply(BigDecimal.valueOf(num)), 2, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | private CouponRecordDO useCouponRecord(String orderId, BigDecimal amount, String couponRecordId){ |
| | | log.info("使用用户优惠券id: " + couponRecordId + "; 订单id: " + orderId); |
| | | if(StringUtils.isNotEmpty(couponRecordId)){ |
| | | //优惠券使用和验证,如果不符合使用条件需要抛出异常。需调用优惠券使用方法 |
| | | couponRecordService.useCoupon(couponRecordId, orderId, amount); |
| | |
| | | orderIds.add(o.getId()); |
| | | } |
| | | |
| | | List<OrderItem> itemList = orderItemMapper.selectList( |
| | | new QueryWrapper<OrderItem>().in("order_id", orderIds)); |
| | | List<OrderItemListDTO> itemList = orderItemMapper.getOrderItems(orderIds); |
| | | Map<String, List<OrderItemListDTO>> map = new HashMap<>(); |
| | | for (OrderItem oi : itemList) { |
| | | for (OrderItemListDTO oi : itemList) { |
| | | String orderId = oi.getOrderId(); |
| | | List<OrderItemListDTO> ll = map.computeIfAbsent(orderId, k -> new ArrayList<>()); |
| | | OrderItemListDTO d = new OrderItemListDTO(); |
| | |
| | | if(llc == null){ |
| | | llc = new OrderCheckLocationListDTO(); |
| | | BeanUtils.copyProperties(c, llc); |
| | | rMap.put(locationId, llc); |
| | | llc.setTotalAmount(new BigDecimal(0)); |
| | | |
| | | rMap.put(locationId, llc); |
| | | result.add(llc); |
| | | } |
| | | |
| | | BigDecimal t = llc.getTotalAmount(); |
| | | if(t == null){ |
| | | t = new BigDecimal(0); |
| | | } |
| | | t = t.add(c.getTotalAmount()); |
| | | llc.setTotalAmount(t); |
| | | } |
| | | |
| | | Map<Long, String> stationMap = prepareStationMap(); |
| | |
| | | Order o = orderMapper.getUserLastOrder(userId); |
| | | return o.getReceiveTime(); |
| | | } |
| | | |
| | | public Integer getFlowerCompleteNumToday(String userId,Long flowerId){ |
| | | Integer completeNum= orderMapper.getFlowerCompleteNumToday(userId,flowerId); |
| | | return completeNum; |
| | | } |
| | | } |